2018-07-03 21:25:01 +00:00
|
|
|
<?php
|
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
namespace MediaWiki\Extension\CodeMirror\Tests;
|
2018-07-03 21:25:01 +00:00
|
|
|
|
2022-02-06 15:13:05 +00:00
|
|
|
use MediaWiki\Extension\CodeMirror\Hooks;
|
2023-09-19 17:59:29 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-19 04:14:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2021-11-29 18:49:29 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2021-10-12 19:29:45 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2023-09-19 17:59:29 +00:00
|
|
|
use OutputPage;
|
2018-07-03 21:25:01 +00:00
|
|
|
use RequestContext;
|
2023-09-19 17:59:29 +00:00
|
|
|
use Skin;
|
2018-07-03 21:25:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group CodeMirror
|
2023-08-09 08:51:58 +00:00
|
|
|
* @group Database
|
2022-11-14 21:28:30 +00:00
|
|
|
* @coversDefaultClass \MediaWiki\Extension\CodeMirror\Hooks
|
2018-07-03 21:25:01 +00:00
|
|
|
*/
|
2021-10-12 19:29:45 +00:00
|
|
|
class HookTest extends MediaWikiIntegrationTestCase {
|
2020-10-08 08:18:20 +00:00
|
|
|
|
|
|
|
/**
|
2022-11-14 21:28:30 +00:00
|
|
|
* @covers ::isCodeMirrorOnPage
|
|
|
|
* @covers ::onBeforePageDisplay
|
2023-09-19 17:59:29 +00:00
|
|
|
* @param bool $useCodeMirrorV6
|
|
|
|
* @param int $expectedAddModuleCalls
|
|
|
|
* @param string $expectedFirstModule
|
|
|
|
* @dataProvider provideOnBeforePageDisplay
|
2020-10-08 08:18:20 +00:00
|
|
|
*/
|
2023-09-19 17:59:29 +00:00
|
|
|
public function testOnBeforePageDisplay(
|
|
|
|
bool $useCodeMirrorV6, int $expectedAddModuleCalls, string $expectedFirstModule
|
|
|
|
) {
|
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'CodeMirrorV6' => $useCodeMirrorV6,
|
|
|
|
] );
|
2021-11-29 18:49:29 +00:00
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookup->method( 'getOption' )->willReturn( true );
|
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
2020-10-08 08:18:20 +00:00
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
$out = $this->createMock( OutputPage::class );
|
2020-10-08 08:18:20 +00:00
|
|
|
$out->method( 'getModules' )->willReturn( [] );
|
2022-04-15 20:46:08 +00:00
|
|
|
$out->method( 'getUser' )->willReturn( $this->createMock( \User::class ) );
|
|
|
|
$out->method( 'getActionName' )->willReturn( 'edit' );
|
2023-08-19 04:14:07 +00:00
|
|
|
$out->method( 'getTitle' )->willReturn( Title::makeTitle( NS_MAIN, __METHOD__ ) );
|
2023-09-19 17:59:29 +00:00
|
|
|
$request = $this->createMock( WebRequest::class );
|
|
|
|
$request->method( 'getRawVal' )->willReturn( null );
|
|
|
|
$out->method( 'getRequest' )->willReturn( $request );
|
|
|
|
$out->expects( $this->exactly( $expectedAddModuleCalls ) )
|
|
|
|
->method( 'addModules' )
|
|
|
|
->withConsecutive( [ $this->equalTo( $expectedFirstModule ) ] );
|
2020-10-08 08:18:20 +00:00
|
|
|
|
2023-09-19 17:59:29 +00:00
|
|
|
( new Hooks( $userOptionsLookup, MediaWikiServices::getInstance()->getMainConfig() ) )
|
|
|
|
->onBeforePageDisplay( $out, $this->createMock( Skin::class ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function provideOnBeforePageDisplay(): array {
|
|
|
|
return [
|
|
|
|
[ false, 2, 'ext.CodeMirror.WikiEditor' ],
|
|
|
|
[ true, 1, 'ext.CodeMirror.v6.WikiEditor' ]
|
|
|
|
];
|
2020-10-08 08:18:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 21:25:01 +00:00
|
|
|
/**
|
2022-11-14 21:28:30 +00:00
|
|
|
* @covers ::onGetPreferences
|
2018-07-03 21:25:01 +00:00
|
|
|
*/
|
|
|
|
public function testPreferenceRegistered() {
|
|
|
|
$user = self::getTestUser()->getUser();
|
2023-08-19 04:14:07 +00:00
|
|
|
$this->setMwGlobals( 'wgTitle', Title::newFromText( __METHOD__ ) );
|
2022-09-19 19:01:32 +00:00
|
|
|
$kinds = $this->getServiceContainer()->getUserOptionsManager()
|
2021-05-03 10:43:35 +00:00
|
|
|
->getOptionKinds( $user, RequestContext::getMain(), [ 'usecodemirror' => 1 ] );
|
2018-07-03 21:25:01 +00:00
|
|
|
self::assertEquals( 'registered', $kinds['usecodemirror'] );
|
|
|
|
}
|
|
|
|
}
|