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;
|
2024-03-01 00:36:39 +00:00
|
|
|
use Generator;
|
|
|
|
use Language;
|
2024-03-07 20:02:00 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2024-03-01 00:36:39 +00:00
|
|
|
use MediaWiki\EditPage\EditPage;
|
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-11-29 12:38:30 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-10-11 19:53:54 +00:00
|
|
|
use MediaWiki\User\User;
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
2024-03-01 00:36:39 +00:00
|
|
|
* @covers ::onEditPage__showEditForm_initial
|
2024-01-10 00:02:44 +00:00
|
|
|
* @covers ::onEditPage__showReadOnlyForm_initial
|
2023-09-19 17:59:29 +00:00
|
|
|
* @param bool $useCodeMirrorV6
|
|
|
|
* @param int $expectedAddModuleCalls
|
2024-03-01 00:36:39 +00:00
|
|
|
* @param string|null $expectedFirstModule
|
2024-01-10 00:02:44 +00:00
|
|
|
* @param bool $readOnly
|
2024-03-01 00:36:39 +00:00
|
|
|
* @dataProvider provideOnEditPageShowEditFormInitial
|
2020-10-08 08:18:20 +00:00
|
|
|
*/
|
2024-03-01 00:36:39 +00:00
|
|
|
public function testOnEditPageShowEditFormInitial(
|
2024-01-10 00:02:44 +00:00
|
|
|
bool $useCodeMirrorV6,
|
|
|
|
int $expectedAddModuleCalls,
|
|
|
|
?string $expectedFirstModule,
|
|
|
|
bool $readOnly = false
|
2023-09-19 17:59:29 +00:00
|
|
|
) {
|
|
|
|
$this->overrideConfigValues( [
|
|
|
|
'CodeMirrorV6' => $useCodeMirrorV6,
|
|
|
|
] );
|
2021-11-29 18:49:29 +00:00
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookup->method( 'getOption' )->willReturn( true );
|
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( [] );
|
2024-01-17 16:21:42 +00:00
|
|
|
$isFirstCall = true;
|
2023-09-19 17:59:29 +00:00
|
|
|
$out->expects( $this->exactly( $expectedAddModuleCalls ) )
|
|
|
|
->method( 'addModules' )
|
2024-01-17 16:21:42 +00:00
|
|
|
->willReturnCallback( function ( $modules ) use ( $expectedFirstModule, &$isFirstCall ) {
|
2024-03-01 00:36:39 +00:00
|
|
|
if ( $isFirstCall && $modules !== null ) {
|
2024-01-17 16:21:42 +00:00
|
|
|
$this->assertSame( $expectedFirstModule, $modules );
|
|
|
|
}
|
|
|
|
$isFirstCall = false;
|
|
|
|
} );
|
2020-10-08 08:18:20 +00:00
|
|
|
|
2024-03-01 00:36:39 +00:00
|
|
|
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() );
|
2024-01-10 00:02:44 +00:00
|
|
|
$method = $readOnly ? 'onEditPage__showReadOnlyForm_initial' : 'onEditPage__showEditForm_initial';
|
|
|
|
$hooks->{$method}( $this->createMock( EditPage::class ), $out );
|
2023-09-19 17:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-01 00:36:39 +00:00
|
|
|
* @return Generator
|
2023-09-19 17:59:29 +00:00
|
|
|
*/
|
2024-03-01 00:36:39 +00:00
|
|
|
public static function provideOnEditPageShowEditFormInitial(): Generator {
|
2024-01-10 00:02:44 +00:00
|
|
|
// useCodeMirrorV6, expectedAddModuleCalls, expectedFirstModule, readOnly
|
2024-03-01 00:36:39 +00:00
|
|
|
yield 'CM5' => [ false, 2, 'ext.CodeMirror.WikiEditor' ];
|
|
|
|
yield 'CM6' => [ true, 1, 'ext.CodeMirror.v6.WikiEditor' ];
|
2024-01-10 00:02:44 +00:00
|
|
|
yield 'CM5 read-only' => [ false, 0, null, true ];
|
|
|
|
yield 'CM6 read-only' => [ true, 1, 'ext.CodeMirror.v6.WikiEditor', true ];
|
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();
|
2024-03-06 15:59:33 +00:00
|
|
|
$context = RequestContext::getMain();
|
|
|
|
$context->setTitle( Title::newFromText( __METHOD__ ) );
|
2022-09-19 19:01:32 +00:00
|
|
|
$kinds = $this->getServiceContainer()->getUserOptionsManager()
|
2024-03-06 15:59:33 +00:00
|
|
|
->getOptionKinds( $user, $context, [ '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
|
2024-01-10 00:02:44 +00:00
|
|
|
* @param array $conds
|
2024-03-01 00:36:39 +00:00
|
|
|
* @param bool $expectation
|
2023-10-10 19:23:03 +00:00
|
|
|
*/
|
2024-01-10 00:02:44 +00:00
|
|
|
public function testShouldLoadCodeMirror( array $conds, bool $expectation ): void {
|
|
|
|
$conds = array_merge( [
|
|
|
|
'module' => null,
|
|
|
|
'gadget' => null,
|
|
|
|
'contentModel' => CONTENT_MODEL_WIKITEXT,
|
|
|
|
'useV6' => false,
|
|
|
|
'isRTL' => false
|
|
|
|
], $conds );
|
2024-03-01 00:36:39 +00:00
|
|
|
$this->overrideConfigValues( [
|
2024-01-10 00:02:44 +00:00
|
|
|
'CodeMirrorV6' => $conds['useV6'],
|
2024-03-01 00:36:39 +00:00
|
|
|
] );
|
2024-01-10 00:02:44 +00:00
|
|
|
$out = $this->getMockOutputPage( $conds['contentModel'], $conds['isRTL'] );
|
|
|
|
$out->method( 'getModules' )->willReturn( $conds['module'] ? [ $conds['module'] ] : [] );
|
2023-10-10 19:23:03 +00:00
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
$userOptionsLookup->method( 'getOption' )->willReturn( true );
|
|
|
|
|
2024-01-10 00:02:44 +00:00
|
|
|
if ( $conds['gadget'] && !ExtensionRegistry::getInstance()->isLoaded( 'Gadgets' ) ) {
|
2023-10-10 19:23:03 +00:00
|
|
|
$this->markTestSkipped( 'Skipped as Gadgets extension is not available' );
|
|
|
|
}
|
|
|
|
|
2024-01-10 00:02:44 +00:00
|
|
|
$extensionRegistry = $this->getMockExtensionRegistry( (bool)$conds['gadget'] );
|
2024-03-01 00:36:39 +00:00
|
|
|
$extensionRegistry->method( 'getAttribute' )
|
|
|
|
->with( 'CodeMirrorContentModels' )
|
|
|
|
->willReturn( [ CONTENT_MODEL_WIKITEXT ] );
|
2023-10-10 19:23:03 +00:00
|
|
|
|
2024-01-10 00:02:44 +00:00
|
|
|
if ( $conds['gadget'] ) {
|
2023-10-10 19:23:03 +00:00
|
|
|
$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' )
|
2024-01-10 00:02:44 +00:00
|
|
|
->willReturn( [ $conds['gadget'] ] );
|
2023-10-10 19:23:03 +00:00
|
|
|
GadgetRepo::setSingleton( $gadgetRepoMock );
|
|
|
|
}
|
|
|
|
|
2024-01-10 00:02:44 +00:00
|
|
|
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig() );
|
2023-10-10 19:23:03 +00:00
|
|
|
self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-01 00:36:39 +00:00
|
|
|
* @return Generator
|
2023-10-10 19:23:03 +00:00
|
|
|
*/
|
2024-03-01 00:36:39 +00:00
|
|
|
public function provideShouldLoadCodeMirror(): Generator {
|
2024-01-10 00:02:44 +00:00
|
|
|
// [ conditions, expectation ]
|
|
|
|
yield [ [], true ];
|
|
|
|
yield [ [ 'module' => 'ext.codeEditor' ], false ];
|
|
|
|
yield [ [ 'gadget' => 'wikEd' ], false ];
|
|
|
|
yield [ [ 'contentModel' => CONTENT_FORMAT_CSS ], false ];
|
|
|
|
yield [ [ 'isRTL' => true ], false ];
|
|
|
|
yield [ [ 'isRTL' => true, 'useV6' => true ], true ];
|
2023-10-10 19:23:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-01 00:36:39 +00:00
|
|
|
* @param string $contentModel
|
|
|
|
* @param bool $isRTL
|
2023-10-10 19:23:03 +00:00
|
|
|
* @return OutputPage|MockObject
|
|
|
|
*/
|
2024-03-01 00:36:39 +00:00
|
|
|
private function getMockOutputPage( string $contentModel = CONTENT_MODEL_WIKITEXT, bool $isRTL = false ) {
|
2023-10-10 19:23:03 +00:00
|
|
|
$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' );
|
2024-03-01 00:36:39 +00:00
|
|
|
$title = $this->createMock( Title::class );
|
|
|
|
$title->method( 'getContentModel' )->willReturn( $contentModel );
|
|
|
|
$language = $this->createMock( Language::class );
|
|
|
|
$language->method( 'isRTL' )->willReturn( $isRTL );
|
|
|
|
$title->method( 'getPageLanguage' )->willReturn( $language );
|
|
|
|
$out->method( 'getTitle' )->willReturn( $title );
|
2023-10-10 19:23:03 +00:00
|
|
|
$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
|
|
|
}
|