| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\CodeMirror\Tests; |
| 4 |
|
| 5 |
use Generator; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\EditPage\EditPage; |
| 8 |
use MediaWiki\Extension\CodeMirror\Hooks; |
| 9 |
use MediaWiki\Extension\Gadgets\Gadget; |
| 10 |
use MediaWiki\Extension\Gadgets\GadgetRepo; |
| 11 |
use MediaWiki\Language\Language; |
| 12 |
use MediaWiki\Output\OutputPage; |
| 13 |
use MediaWiki\Registration\ExtensionRegistry; |
| 14 |
use MediaWiki\Request\WebRequest; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 17 |
use MediaWiki\User\User; |
| 18 |
use MediaWikiIntegrationTestCase; |
| 19 |
use PHPUnit\Framework\MockObject\MockObject; |
| 20 |
|
| 21 |
/** |
| 22 |
* @group CodeMirror |
| 23 |
* @group Database |
| 24 |
* @coversDefaultClass \MediaWiki\Extension\CodeMirror\Hooks |
| 25 |
*/ |
| 26 |
class HookTest extends MediaWikiIntegrationTestCase { |
| 27 |
|
| 28 |
/** |
| 29 |
* @covers ::shouldLoadCodeMirror |
| 30 |
* @covers ::onEditPage__showEditForm_initial |
| 31 |
* @covers ::onEditPage__showReadOnlyForm_initial |
| 32 |
* @param bool $useCodeMirrorV6 |
| 33 |
* @param int $expectedAddModuleCalls |
| 34 |
* @param string|null $expectedFirstModule |
| 35 |
* @param bool $readOnly |
| 36 |
* @dataProvider provideOnEditPageShowEditFormInitial |
| 37 |
*/ |
| 38 |
public function testOnEditPageShowEditFormInitial( |
| 39 |
bool $useCodeMirrorV6, |
| 40 |
int $expectedAddModuleCalls, |
| 41 |
?string $expectedFirstModule, |
| 42 |
bool $readOnly = false |
| 43 |
) { |
| 44 |
$this->overrideConfigValues( [ |
| 45 |
'CodeMirrorV6' => $useCodeMirrorV6, |
| 46 |
] ); |
| 47 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 48 |
$userOptionsLookup->method( 'getBoolOption' )->willReturn( true ); |
| 49 |
|
| 50 |
$out = $this->getMockOutputPage(); |
| 51 |
$out->method( 'getModules' )->willReturn( [] ); |
| 52 |
$isFirstCall = true; |
| 53 |
$out->expects( $this->exactly( $expectedAddModuleCalls ) ) |
| 54 |
->method( 'addModules' ) |
| 55 |
->willReturnCallback( function ( $modules ) use ( $expectedFirstModule, &$isFirstCall ) { |
| 56 |
if ( $isFirstCall && $modules !== null ) { |
| 57 |
$this->assertSame( $expectedFirstModule, $modules ); |
| 58 |
} |
| 59 |
$isFirstCall = false; |
| 60 |
} ); |
| 61 |
|
| 62 |
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), null ); |
| 63 |
$method = $readOnly ? 'onEditPage__showReadOnlyForm_initial' : 'onEditPage__showEditForm_initial'; |
| 64 |
$hooks->{$method}( $this->createMock( EditPage::class ), $out ); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @return Generator |
| 69 |
*/ |
| 70 |
public static function provideOnEditPageShowEditFormInitial(): Generator { |
| 71 |
// useCodeMirrorV6, expectedAddModuleCalls, expectedFirstModule, readOnly |
| 72 |
yield 'CM5' => [ false, 2, 'ext.CodeMirror.WikiEditor' ]; |
| 73 |
yield 'CM6' => [ true, 1, 'ext.CodeMirror.v6.WikiEditor.init' ]; |
| 74 |
yield 'CM5 read-only' => [ false, 0, null, true ]; |
| 75 |
yield 'CM6 read-only' => [ true, 1, 'ext.CodeMirror.v6.WikiEditor.init', true ]; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @covers ::onGetPreferences |
| 80 |
*/ |
| 81 |
public function testPreferenceRegistered() { |
| 82 |
$user = self::getTestUser()->getUser(); |
| 83 |
$context = RequestContext::getMain(); |
| 84 |
$context->setTitle( Title::newFromText( __METHOD__ ) ); |
| 85 |
$kinds = $this->getServiceContainer()->getPreferencesFactory() |
| 86 |
->getResetKinds( $user, $context, [ 'usecodemirror' => 1 ] ); |
| 87 |
self::assertEquals( 'registered', $kinds['usecodemirror'] ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* @covers ::onGetPreferences |
| 92 |
*/ |
| 93 |
public function testOnGetPreferencces(): void { |
| 94 |
$user = self::getTestUser()->getUser(); |
| 95 |
$userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup(); |
| 96 |
$config = $this->getServiceContainer()->getMainConfig(); |
| 97 |
|
| 98 |
// CodeMirror 5 |
| 99 |
$this->overrideConfigValues( [ 'CodeMirrorV6' => false ] ); |
| 100 |
$hook = new Hooks( $userOptionsLookup, $config, null ); |
| 101 |
$preferences = []; |
| 102 |
$hook->onGetPreferences( $user, $preferences ); |
| 103 |
self::assertArrayHasKey( 'usecodemirror', $preferences ); |
| 104 |
self::assertArrayHasKey( 'usecodemirror-colorblind', $preferences ); |
| 105 |
self::assertArrayNotHasKey( 'usecodemirror-summary', $preferences ); |
| 106 |
self::assertSame( 'api', $preferences['usecodemirror']['type'] ); |
| 107 |
|
| 108 |
// CodeMirror 6 |
| 109 |
$this->overrideConfigValues( [ 'CodeMirrorV6' => true ] ); |
| 110 |
$hook = new Hooks( $userOptionsLookup, $config, null ); |
| 111 |
$preferences = []; |
| 112 |
$hook->onGetPreferences( $user, $preferences ); |
| 113 |
self::assertArrayHasKey( 'usecodemirror', $preferences ); |
| 114 |
self::assertArrayHasKey( 'usecodemirror-colorblind', $preferences ); |
| 115 |
self::assertArrayHasKey( 'usecodemirror-summary', $preferences ); |
| 116 |
self::assertSame( 'toggle', $preferences['usecodemirror']['type'] ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* @covers ::shouldLoadCodeMirror |
| 121 |
* @dataProvider provideShouldLoadCodeMirror |
| 122 |
* @param array $conds |
| 123 |
* @param bool $expectation |
| 124 |
*/ |
| 125 |
public function testShouldLoadCodeMirror( array $conds, bool $expectation ): void { |
| 126 |
$conds = array_merge( [ |
| 127 |
'module' => null, |
| 128 |
'gadget' => null, |
| 129 |
'contentModel' => CONTENT_MODEL_WIKITEXT, |
| 130 |
'useV6' => false, |
| 131 |
'isRTL' => false, |
| 132 |
'usecodemirror' => true, |
| 133 |
'usebetatoolbar' => true, |
| 134 |
], $conds ); |
| 135 |
$this->overrideConfigValues( [ |
| 136 |
'CodeMirrorV6' => $conds['useV6'], |
| 137 |
] ); |
| 138 |
$out = $this->getMockOutputPage( $conds['contentModel'], $conds['isRTL'] ); |
| 139 |
$out->method( 'getModules' )->willReturn( $conds['module'] ? [ $conds['module'] ] : [] ); |
| 140 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 141 |
$userOptionsLookup->method( 'getBoolOption' ) |
| 142 |
->willReturnMap( [ |
| 143 |
[ $out->getUser(), 'usecodemirror', 0, $conds['usecodemirror'] ], |
| 144 |
[ $out->getUser(), 'usebetatoolbar', 0, $conds['usebetatoolbar'] ] |
| 145 |
] ); |
| 146 |
|
| 147 |
$extensionRegistry = $this->getMockExtensionRegistry( (bool)$conds['gadget'] ); |
| 148 |
$extensionRegistry->method( 'getAttribute' ) |
| 149 |
->with( 'CodeMirrorContentModels' ) |
| 150 |
->willReturn( [ CONTENT_MODEL_WIKITEXT ] ); |
| 151 |
|
| 152 |
$gadgetRepoMock = null; |
| 153 |
if ( $conds['gadget'] ) { |
| 154 |
$this->markTestSkippedIfExtensionNotLoaded( 'Gadgets' ); |
| 155 |
|
| 156 |
$gadgetMock = $this->createMock( Gadget::class ); |
| 157 |
$gadgetMock->expects( $this->once() ) |
| 158 |
->method( 'isEnabled' ) |
| 159 |
->willReturn( true ); |
| 160 |
$gadgetRepoMock = $this->createMock( GadgetRepo::class ); |
| 161 |
$gadgetRepoMock->expects( $this->once() ) |
| 162 |
->method( 'getGadget' ) |
| 163 |
->willReturn( $gadgetMock ); |
| 164 |
$gadgetRepoMock->expects( $this->once() ) |
| 165 |
->method( 'getGadgetIds' ) |
| 166 |
->willReturn( [ $conds['gadget'] ] ); |
| 167 |
} |
| 168 |
|
| 169 |
$hooks = new Hooks( $userOptionsLookup, $this->getServiceContainer()->getMainConfig(), $gadgetRepoMock ); |
| 170 |
self::assertSame( $expectation, $hooks->shouldLoadCodeMirror( $out, $extensionRegistry ) ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* @return Generator |
| 175 |
*/ |
| 176 |
public function provideShouldLoadCodeMirror(): Generator { |
| 177 |
// [ conditions, expectation ] |
| 178 |
yield [ [], true ]; |
| 179 |
yield [ [ 'module' => 'ext.codeEditor' ], false ]; |
| 180 |
yield [ [ 'gadget' => 'wikEd' ], false ]; |
| 181 |
yield [ [ 'contentModel' => CONTENT_FORMAT_CSS ], false ]; |
| 182 |
yield [ [ 'isRTL' => true ], false ]; |
| 183 |
yield [ [ 'isRTL' => true, 'useV6' => true ], true ]; |
| 184 |
yield [ [ 'usebetatoolbar' => false ], false ]; |
| 185 |
yield [ [ 'usebetatoolbar' => false, 'useV6' => true ], true ]; |
| 186 |
yield [ [ 'usebetatoolbar' => false, 'usecodemirror' => false, 'useV6' => true ], false ]; |
| 187 |
yield [ [ 'usecodemirror' => false ], true ]; |
| 188 |
yield [ [ 'usecodemirror' => false, 'useV6' => true ], true ]; |
| 189 |
yield [ [ 'usecodemirror' => false, 'usebetatoolbar' => false, 'useV6' => true ], false ]; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* @param string $contentModel |
| 194 |
* @param bool $isRTL |
| 195 |
* @return OutputPage&MockObject |
| 196 |
*/ |
| 197 |
private function getMockOutputPage( string $contentModel = CONTENT_MODEL_WIKITEXT, bool $isRTL = false ) { |
| 198 |
$out = $this->createMock( OutputPage::class ); |
| 199 |
$out->method( 'getUser' )->willReturn( $this->createMock( User::class ) ); |
| 200 |
$out->method( 'getActionName' )->willReturn( 'edit' ); |
| 201 |
$title = $this->createMock( Title::class ); |
| 202 |
$title->method( 'getContentModel' )->willReturn( $contentModel ); |
| 203 |
$language = $this->createMock( Language::class ); |
| 204 |
$language->method( 'isRTL' )->willReturn( $isRTL ); |
| 205 |
$title->method( 'getPageLanguage' )->willReturn( $language ); |
| 206 |
$out->method( 'getTitle' )->willReturn( $title ); |
| 207 |
$request = $this->createMock( WebRequest::class ); |
| 208 |
$request->method( 'getRawVal' )->willReturn( null ); |
| 209 |
$out->method( 'getRequest' )->willReturn( $request ); |
| 210 |
return $out; |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* @param bool $gadgetsEnabled |
| 215 |
* @return ExtensionRegistry&MockObject |
| 216 |
*/ |
| 217 |
private function getMockExtensionRegistry( bool $gadgetsEnabled ) { |
| 218 |
$mock = $this->createMock( ExtensionRegistry::class ); |
| 219 |
$mock->method( 'isLoaded' ) |
| 220 |
->with( 'Gadgets' ) |
| 221 |
->willReturn( $gadgetsEnabled ); |
| 222 |
return $mock; |
| 223 |
} |
| 224 |
} |