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
|
|
|
|
2023-10-10 19:23:03 +00:00
|
|
|
use ExtensionRegistry;
|
2022-02-06 15:13:05 +00:00
|
|
|
use MediaWiki\Extension\CodeMirror\Hooks;
|
2023-10-10 19:23:03 +00:00
|
|
|
use MediaWiki\Extension\Gadgets\Gadget;
|
|
|
|
use MediaWiki\Extension\Gadgets\GadgetRepo;
|
2023-10-11 19:53:54 +00:00
|
|
|
use MediaWiki\Output\OutputPage;
|
2023-09-19 17:59:29 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-08-19 04:14:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-10-11 19:53:54 +00:00
|
|
|
use MediaWiki\User\User;
|
2021-11-29 18:49:29 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2021-10-12 19:29:45 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2023-10-10 19:23:03 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
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
|
|
|
|
|
|
|
/**
|
2023-10-10 19:23:03 +00:00
|
|
|
* @covers ::shouldLoadCodeMirror
|
2022-11-14 21:28:30 +00:00
|
|
|
* @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-10-10 19:23:03 +00:00
|
|
|
$out = $this->getMockOutputPage();
|
2020-10-08 08:18:20 +00:00
|
|
|
$out->method( 'getModules' )->willReturn( [] );
|
2023-09-19 17:59:29 +00:00
|
|
|
$out->expects( $this->exactly( $expectedAddModuleCalls ) )
|
|
|
|
->method( 'addModules' )
|
|
|
|
->withConsecutive( [ $this->equalTo( $expectedFirstModule ) ] );
|
2020-10-08 08:18:20 +00:00
|
|
|
|
2023-10-11 19:53:54 +00:00
|
|
|
( new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() ) )
|
2023-09-19 17:59:29 +00:00
|
|
|
->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'] );
|
|
|
|
}
|
2023-10-10 19:23:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers ::shouldLoadCodeMirror
|
|
|
|
* @dataProvider provideShouldLoadCodeMirror
|
|
|
|
*/
|
|
|
|
public function testShouldLoadCodeMirror( ?string $module, ?string $gadget, bool $expectation ): void {
|
|
|
|
$out = $this->getMockOutputPage();
|
|
|
|
$out->method( 'getModules' )->willReturn( $module ? [ $module ] : [] );
|
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookup->method( 'getOption' )->willReturn( true );
|
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
|
|
|
|
|
|
|
if ( $gadget && !ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ) {
|
|
|
|
$this->markTestSkipped( 'Skipped as Gadgets extension is not available' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$extensionRegistry = $this->getMockExtensionRegistry( (bool)$gadget );
|
|
|
|
|
|
|
|
if ( $gadget ) {
|
|
|
|
$gadgetMock = $this->createMock( Gadget::class );
|
|
|
|
$gadgetMock->expects( $this->once() )
|
|
|
|
->method( 'isEnabled' )
|
|
|
|
->willReturn( true );
|
|
|
|
$gadgetRepoMock = $this->createMock( GadgetRepo::class );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadget' )
|
|
|
|
->willReturn( $gadgetMock );
|
|
|
|
$gadgetRepoMock->expects( $this->once() )
|
|
|
|
->method( 'getGadgetIds' )
|
|
|
|
->willReturn( [ $gadget ] );
|
|
|
|
GadgetRepo::setSingleton( $gadgetRepoMock );
|
|
|
|
}
|
|
|
|
|
|
|
|
$hooks = new Hooks(
|
|
|
|
$userOptionsLookup,
|
2023-10-11 19:53:54 +00:00
|
|
|
$this->getServiceContainer()->getMainConfig()
|
2023-10-10 19:23:03 +00:00
|
|
|
);
|
|
|
|
self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function provideShouldLoadCodeMirror(): array {
|
|
|
|
return [
|
|
|
|
[ null, null, true ],
|
|
|
|
[ 'ext.codeEditor', null, false ],
|
|
|
|
[ null, 'wikEd', false ]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return OutputPage|MockObject
|
|
|
|
*/
|
|
|
|
private function getMockOutputPage() {
|
|
|
|
$out = $this->createMock( OutputPage::class );
|
2023-10-11 19:53:54 +00:00
|
|
|
$out->method( 'getUser' )->willReturn( $this->createMock( User::class ) );
|
2023-10-10 19:23:03 +00:00
|
|
|
$out->method( 'getActionName' )->willReturn( 'edit' );
|
|
|
|
$out->method( 'getTitle' )->willReturn( Title::makeTitle( NS_MAIN, __METHOD__ ) );
|
|
|
|
$request = $this->createMock( WebRequest::class );
|
|
|
|
$request->method( 'getRawVal' )->willReturn( null );
|
|
|
|
$out->method( 'getRequest' )->willReturn( $request );
|
|
|
|
return $out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $gadgetsEnabled
|
|
|
|
* @return MockObject|ExtensionRegistry
|
|
|
|
*/
|
|
|
|
private function getMockExtensionRegistry( bool $gadgetsEnabled ) {
|
|
|
|
$mock = $this->createMock( ExtensionRegistry::class );
|
|
|
|
$mock->method( 'isLoaded' )
|
|
|
|
->with( 'Gadgets' )
|
|
|
|
->willReturn( $gadgetsEnabled );
|
|
|
|
return $mock;
|
|
|
|
}
|
2018-07-03 21:25:01 +00:00
|
|
|
}
|