| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Content\ContentHandler; |
| 4 |
use MediaWiki\Content\CssContentHandler; |
| 5 |
use MediaWiki\Content\JavaScriptContentHandler; |
| 6 |
use MediaWiki\Content\JsonContent; |
| 7 |
use MediaWiki\Content\JsonContentHandler; |
| 8 |
use MediaWiki\Content\TextContentHandler; |
| 9 |
use MediaWiki\Content\ValidationParams; |
| 10 |
use MediaWiki\Content\WikitextContent; |
| 11 |
use MediaWiki\Content\WikitextContentHandler; |
| 12 |
use MediaWiki\Context\RequestContext; |
| 13 |
use MediaWiki\Language\Language; |
| 14 |
use MediaWiki\Languages\LanguageNameUtils; |
| 15 |
use MediaWiki\Linker\LinkRenderer; |
| 16 |
use MediaWiki\MainConfigNames; |
| 17 |
use MediaWiki\Page\Hook\OpportunisticLinksUpdateHook; |
| 18 |
use MediaWiki\Page\PageIdentity; |
| 19 |
use MediaWiki\Page\PageIdentityValue; |
| 20 |
use MediaWiki\Parser\MagicWordFactory; |
| 21 |
use MediaWiki\Parser\ParserFactory; |
| 22 |
use MediaWiki\Parser\ParserOutput; |
| 23 |
use MediaWiki\Parser\Parsoid\ParsoidParserFactory; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use MediaWiki\Title\TitleFactory; |
| 26 |
use Wikimedia\TestingAccessWrapper; |
| 27 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class ContentHandlerTest extends MediaWikiIntegrationTestCase { |
| 35 |
|
| 36 |
protected function setUp(): void { |
| 37 |
parent::setUp(); |
| 38 |
|
| 39 |
$this->overrideConfigValues( [ |
| 40 |
MainConfigNames::ExtraNamespaces => [ |
| 41 |
12312 => 'Dummy', |
| 42 |
12313 => 'Dummy_talk', |
| 43 |
], |
| 44 |
|
| 45 |
|
| 46 |
MainConfigNames::NamespaceContentModels => [ |
| 47 |
12312 => 'testing', |
| 48 |
], |
| 49 |
MainConfigNames::ContentHandlers => [ |
| 50 |
CONTENT_MODEL_WIKITEXT => [ |
| 51 |
'class' => WikitextContentHandler::class, |
| 52 |
'services' => [ |
| 53 |
'TitleFactory', |
| 54 |
'ParserFactory', |
| 55 |
'GlobalIdGenerator', |
| 56 |
'LanguageNameUtils', |
| 57 |
'LinkRenderer', |
| 58 |
'MagicWordFactory', |
| 59 |
'ParsoidParserFactory', |
| 60 |
], |
| 61 |
], |
| 62 |
CONTENT_MODEL_JAVASCRIPT => JavaScriptContentHandler::class, |
| 63 |
CONTENT_MODEL_JSON => JsonContentHandler::class, |
| 64 |
CONTENT_MODEL_CSS => CssContentHandler::class, |
| 65 |
CONTENT_MODEL_TEXT => TextContentHandler::class, |
| 66 |
'testing' => DummyContentHandlerForTesting::class, |
| 67 |
'testing-callbacks' => static function ( $modelId ) { |
| 68 |
return new DummyContentHandlerForTesting( $modelId ); |
| 69 |
} |
| 70 |
], |
| 71 |
] ); |
| 72 |
} |
| 73 |
|
| 74 |
public function addDBDataOnce() { |
| 75 |
$this->insertPage( 'Not_Main_Page', 'This is not a main page' ); |
| 76 |
$this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' ); |
| 77 |
} |
| 78 |
|
| 79 |
public static function dataGetDefaultModelFor() { |
| 80 |
return [ |
| 81 |
[ 'Help:Foo', CONTENT_MODEL_WIKITEXT ], |
| 82 |
[ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ], |
| 83 |
[ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ], |
| 84 |
[ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ], |
| 85 |
[ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ], |
| 86 |
[ 'User:Foo', CONTENT_MODEL_WIKITEXT ], |
| 87 |
[ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ], |
| 88 |
[ 'User:Foo.css', CONTENT_MODEL_WIKITEXT ], |
| 89 |
[ 'User:Foo.json', CONTENT_MODEL_WIKITEXT ], |
| 90 |
[ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ], |
| 91 |
[ 'User:Foo/bar.css', CONTENT_MODEL_CSS ], |
| 92 |
[ 'User:Foo/bar.json', CONTENT_MODEL_JSON ], |
| 93 |
[ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ], |
| 94 |
[ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ], |
| 95 |
[ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ], |
| 96 |
[ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ], |
| 97 |
[ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ], |
| 98 |
[ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ], |
| 99 |
[ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ], |
| 100 |
[ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ], |
| 101 |
[ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ], |
| 102 |
[ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ], |
| 103 |
[ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ], |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function testGetDefaultModelFor( $title, $expectedModelId ) { |
| 111 |
$title = Title::newFromText( $title ); |
| 112 |
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getDefaultModelFor' ); |
| 113 |
$this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) ); |
| 114 |
} |
| 115 |
|
| 116 |
public static function dataGetLocalizedName() { |
| 117 |
return [ |
| 118 |
[ null, null ], |
| 119 |
[ "xyzzy", null ], |
| 120 |
|
| 121 |
|
| 122 |
[ CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ], |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function testGetLocalizedName( $id, $expected ) { |
| 130 |
$name = ContentHandler::getLocalizedName( $id ); |
| 131 |
|
| 132 |
if ( $expected ) { |
| 133 |
$this->assertNotNull( $name, "no name found for content model $id" ); |
| 134 |
$this->assertTrue( preg_match( $expected, $name ) > 0, |
| 135 |
"content model name for #$id did not match pattern $expected" |
| 136 |
); |
| 137 |
} else { |
| 138 |
$this->assertEquals( $id, $name, "localization of unknown model $id should have " |
| 139 |
. "fallen back to use the model id directly." |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public static function dataGetPageLanguage() { |
| 145 |
global $wgLanguageCode; |
| 146 |
|
| 147 |
return [ |
| 148 |
[ "Main", $wgLanguageCode ], |
| 149 |
[ "Dummy:Foo", $wgLanguageCode ], |
| 150 |
[ "MediaWiki:common.js", 'en' ], |
| 151 |
[ "User:Foo/common.js", 'en' ], |
| 152 |
[ "MediaWiki:common.css", 'en' ], |
| 153 |
[ "User:Foo/common.css", 'en' ], |
| 154 |
[ "User:Foo", $wgLanguageCode ], |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function testGetPageLanguage( $title, $expected ) { |
| 162 |
$title = Title::newFromText( $title ); |
| 163 |
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title ); |
| 164 |
|
| 165 |
$handler = $this->getServiceContainer() |
| 166 |
->getContentHandlerFactory() |
| 167 |
->getContentHandler( $title->getContentModel() ); |
| 168 |
$lang = $handler->getPageLanguage( $title ); |
| 169 |
|
| 170 |
$this->assertInstanceOf( Language::class, $lang ); |
| 171 |
$this->assertEquals( $expected, $lang->getCode() ); |
| 172 |
} |
| 173 |
|
| 174 |
public function testGetContentText_Null() { |
| 175 |
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentText' ); |
| 176 |
$content = null; |
| 177 |
$text = ContentHandler::getContentText( $content ); |
| 178 |
$this->assertSame( '', $text ); |
| 179 |
} |
| 180 |
|
| 181 |
public function testGetContentText_TextContent() { |
| 182 |
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentText' ); |
| 183 |
$content = new WikitextContent( "hello world" ); |
| 184 |
$text = ContentHandler::getContentText( $content ); |
| 185 |
$this->assertEquals( $content->getText(), $text ); |
| 186 |
} |
| 187 |
|
| 188 |
public function testGetContentText_NonTextContent() { |
| 189 |
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentText' ); |
| 190 |
$content = new DummyContentForTesting( "hello world" ); |
| 191 |
$text = ContentHandler::getContentText( $content ); |
| 192 |
$this->assertNull( $text ); |
| 193 |
} |
| 194 |
|
| 195 |
public static function dataMakeContent() { |
| 196 |
return [ |
| 197 |
[ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, false ], |
| 198 |
[ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, false ], |
| 199 |
[ 'hallo', 'Dummy:Test', null, null, "testing", false ], |
| 200 |
|
| 201 |
[ |
| 202 |
'hallo', |
| 203 |
'Help:Test', |
| 204 |
null, |
| 205 |
CONTENT_FORMAT_WIKITEXT, |
| 206 |
CONTENT_MODEL_WIKITEXT, |
| 207 |
false |
| 208 |
], |
| 209 |
[ |
| 210 |
'hallo', |
| 211 |
'MediaWiki:Test.js', |
| 212 |
null, |
| 213 |
CONTENT_FORMAT_JAVASCRIPT, |
| 214 |
CONTENT_MODEL_JAVASCRIPT, |
| 215 |
false |
| 216 |
], |
| 217 |
[ 'hallo', 'Dummy:Test', null, "testing", "testing", false ], |
| 218 |
|
| 219 |
[ 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, false ], |
| 220 |
[ |
| 221 |
'hallo', |
| 222 |
'MediaWiki:Test.js', |
| 223 |
CONTENT_MODEL_CSS, |
| 224 |
null, |
| 225 |
CONTENT_MODEL_CSS, |
| 226 |
false |
| 227 |
], |
| 228 |
[ |
| 229 |
serialize( 'hallo' ), |
| 230 |
'Dummy:Test', |
| 231 |
CONTENT_MODEL_CSS, |
| 232 |
null, |
| 233 |
CONTENT_MODEL_CSS, |
| 234 |
false |
| 235 |
], |
| 236 |
|
| 237 |
[ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, true ], |
| 238 |
[ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, true ], |
| 239 |
[ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, true ], |
| 240 |
]; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
public function testMakeContent( $data, $title, $modelId, $format, |
| 247 |
$expectedModelId, $shouldFail |
| 248 |
) { |
| 249 |
$title = Title::newFromText( $title ); |
| 250 |
$this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title ); |
| 251 |
try { |
| 252 |
$content = ContentHandler::makeContent( $data, $title, $modelId, $format ); |
| 253 |
|
| 254 |
if ( $shouldFail ) { |
| 255 |
$this->fail( "ContentHandler::makeContent should have failed!" ); |
| 256 |
} |
| 257 |
|
| 258 |
$this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' ); |
| 259 |
$this->assertEquals( $data, $content->serialize(), 'bad serialized data' ); |
| 260 |
} catch ( MWException $ex ) { |
| 261 |
if ( !$shouldFail ) { |
| 262 |
$this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() ); |
| 263 |
} else { |
| 264 |
|
| 265 |
$this->assertTrue( true ); |
| 266 |
} |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public function testGetAutosummary() { |
| 274 |
$this->setContentLang( 'en' ); |
| 275 |
|
| 276 |
$content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT ); |
| 277 |
$title = Title::makeTitle( NS_HELP, 'Test' ); |
| 278 |
|
| 279 |
$newContent = ContentHandler::makeContent( '', $title, CONTENT_MODEL_WIKITEXT, null ); |
| 280 |
|
| 281 |
$autoSummary = $content->getAutosummary( null, $newContent, 97 ); |
| 282 |
$this->assertEquals( |
| 283 |
wfMessage( 'autosumm-newblank' )->inContentLanguage()->text(), |
| 284 |
$autoSummary |
| 285 |
); |
| 286 |
|
| 287 |
$autoSummary = $content->getAutosummary( null, $newContent, 92 ); |
| 288 |
$this->assertSame( '', $autoSummary ); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function testGetChangeTag() { |
| 295 |
$this->overrideConfigValue( MainConfigNames::SoftwareTags, [ 'mw-contentmodelchange' => true ] ); |
| 296 |
$wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT ); |
| 297 |
|
| 298 |
$oldContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT, null ); |
| 299 |
|
| 300 |
$newContent = ContentHandler::makeContent( '', null, CONTENT_MODEL_WIKITEXT, null ); |
| 301 |
|
| 302 |
$tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE ); |
| 303 |
$this->assertSame( 'mw-contentmodelchange', $tag ); |
| 304 |
} |
| 305 |
|
| 306 |
public function testSupportsCategories() { |
| 307 |
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT ); |
| 308 |
$this->assertTrue( $handler->supportsCategories(), 'content model supports categories' ); |
| 309 |
} |
| 310 |
|
| 311 |
public function testSupportsDirectEditing() { |
| 312 |
$handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON ); |
| 313 |
$this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' ); |
| 314 |
} |
| 315 |
|
| 316 |
public static function dummyHookHandler( $foo, &$text, $bar ) { |
| 317 |
if ( $text === null || $text === false ) { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
$text = strtoupper( $text ); |
| 322 |
|
| 323 |
return true; |
| 324 |
} |
| 325 |
|
| 326 |
public static function provideGetModelForID() { |
| 327 |
return [ |
| 328 |
[ CONTENT_MODEL_WIKITEXT, WikitextContentHandler::class ], |
| 329 |
[ CONTENT_MODEL_JAVASCRIPT, JavaScriptContentHandler::class ], |
| 330 |
[ CONTENT_MODEL_JSON, JsonContentHandler::class ], |
| 331 |
[ CONTENT_MODEL_CSS, CssContentHandler::class ], |
| 332 |
[ CONTENT_MODEL_TEXT, TextContentHandler::class ], |
| 333 |
[ 'testing', DummyContentHandlerForTesting::class ], |
| 334 |
[ 'testing-callbacks', DummyContentHandlerForTesting::class ], |
| 335 |
]; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
public function testGetModelForID( $modelId, $handlerClass ) { |
| 342 |
$handler = $this->getServiceContainer()->getContentHandlerFactory() |
| 343 |
->getContentHandler( $modelId ); |
| 344 |
|
| 345 |
$this->assertInstanceOf( $handlerClass, $handler ); |
| 346 |
} |
| 347 |
|
| 348 |
public function testGetFieldsForSearchIndex() { |
| 349 |
$searchEngine = $this->newSearchEngine(); |
| 350 |
|
| 351 |
$handler = $this->getMockBuilder( ContentHandler::class ) |
| 352 |
->onlyMethods( |
| 353 |
[ 'serializeContent', 'unserializeContent', 'makeEmptyContent' ] |
| 354 |
) |
| 355 |
->disableOriginalConstructor() |
| 356 |
->getMock(); |
| 357 |
|
| 358 |
$fields = $handler->getFieldsForSearchIndex( $searchEngine ); |
| 359 |
|
| 360 |
$this->assertArrayHasKey( 'category', $fields ); |
| 361 |
$this->assertArrayHasKey( 'external_link', $fields ); |
| 362 |
$this->assertArrayHasKey( 'outgoing_link', $fields ); |
| 363 |
$this->assertArrayHasKey( 'template', $fields ); |
| 364 |
$this->assertArrayHasKey( 'content_model', $fields ); |
| 365 |
} |
| 366 |
|
| 367 |
private function newSearchEngine() { |
| 368 |
$searchEngine = $this->createMock( SearchEngine::class ); |
| 369 |
|
| 370 |
$searchEngine->method( 'makeSearchFieldMapping' ) |
| 371 |
->willReturnCallback( static function ( $name, $type ) { |
| 372 |
return new DummySearchIndexFieldDefinition( $name, $type ); |
| 373 |
} ); |
| 374 |
|
| 375 |
return $searchEngine; |
| 376 |
} |
| 377 |
|
| 378 |
public function testDataIndexFields() { |
| 379 |
$mockEngine = $this->createMock( SearchEngine::class ); |
| 380 |
$title = Title::makeTitle( NS_MAIN, 'Not_Main_Page' ); |
| 381 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 382 |
|
| 383 |
$this->setTemporaryHook( 'SearchDataForIndex', |
| 384 |
static function ( |
| 385 |
&$fields, |
| 386 |
ContentHandler $handler, |
| 387 |
WikiPage $page, |
| 388 |
ParserOutput $output, |
| 389 |
SearchEngine $engine |
| 390 |
) { |
| 391 |
$fields['testDataField'] = 'test content'; |
| 392 |
} ); |
| 393 |
|
| 394 |
$revision = $page->getRevisionRecord(); |
| 395 |
$output = $page->getContentHandler()->getParserOutputForIndexing( $page, null, $revision ); |
| 396 |
$data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine, $revision ); |
| 397 |
$this->assertArrayHasKey( 'text', $data ); |
| 398 |
$this->assertArrayHasKey( 'text_bytes', $data ); |
| 399 |
$this->assertArrayHasKey( 'language', $data ); |
| 400 |
$this->assertArrayHasKey( 'testDataField', $data ); |
| 401 |
$this->assertEquals( 'test content', $data['testDataField'] ); |
| 402 |
$this->assertEquals( 'wikitext', $data['content_model'] ); |
| 403 |
} |
| 404 |
|
| 405 |
public function testParserOutputForIndexing() { |
| 406 |
$opportunisticUpdateHook = |
| 407 |
$this->createMock( OpportunisticLinksUpdateHook::class ); |
| 408 |
|
| 409 |
|
| 410 |
$opportunisticUpdateHook->expects( $this->never() ) |
| 411 |
->method( 'onOpportunisticLinksUpdate' ) |
| 412 |
->willReturn( false ); |
| 413 |
$this->setTemporaryHook( 'OpportunisticLinksUpdate', $opportunisticUpdateHook ); |
| 414 |
|
| 415 |
$title = Title::makeTitle( NS_MAIN, 'Smithee' ); |
| 416 |
$page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title ); |
| 417 |
$revision = $page->getRevisionRecord(); |
| 418 |
|
| 419 |
$out = $page->getContentHandler()->getParserOutputForIndexing( $page, null, $revision ); |
| 420 |
$this->assertInstanceOf( ParserOutput::class, $out ); |
| 421 |
$this->assertStringContainsString( 'one who smiths', $out->getRawText() ); |
| 422 |
} |
| 423 |
|
| 424 |
public function testGetContentModelsHook() { |
| 425 |
$this->setTemporaryHook( 'GetContentModels', static function ( &$models ) { |
| 426 |
$models[] = 'Ferrari'; |
| 427 |
} ); |
| 428 |
$this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentModels' ); |
| 429 |
$this->assertContains( 'Ferrari', ContentHandler::getContentModels() ); |
| 430 |
} |
| 431 |
|
| 432 |
public function testGetSlotDiffRenderer_default() { |
| 433 |
$this->mergeMwGlobalArrayValue( 'wgHooks', [ |
| 434 |
'GetSlotDiffRenderer' => [], |
| 435 |
] ); |
| 436 |
|
| 437 |
|
| 438 |
$contentHandler = new WikitextContentHandler( |
| 439 |
CONTENT_MODEL_WIKITEXT, |
| 440 |
$this->createMock( TitleFactory::class ), |
| 441 |
$this->createMock( ParserFactory::class ), |
| 442 |
$this->createMock( GlobalIdGenerator::class ), |
| 443 |
$this->createMock( LanguageNameUtils::class ), |
| 444 |
$this->createMock( LinkRenderer::class ), |
| 445 |
$this->createMock( MagicWordFactory::class ), |
| 446 |
$this->createMock( ParsoidParserFactory::class ) |
| 447 |
); |
| 448 |
$slotDiffRenderer = $contentHandler->getSlotDiffRenderer( RequestContext::getMain() ); |
| 449 |
$this->assertInstanceOf( TextSlotDiffRenderer::class, $slotDiffRenderer ); |
| 450 |
} |
| 451 |
|
| 452 |
public function testGetSlotDiffRenderer_bc() { |
| 453 |
$this->mergeMwGlobalArrayValue( 'wgHooks', [ |
| 454 |
'GetSlotDiffRenderer' => [], |
| 455 |
] ); |
| 456 |
|
| 457 |
|
| 458 |
$customDifferenceEngine = $this->createMock( DifferenceEngine::class ); |
| 459 |
|
| 460 |
$customDifferenceEngine->objectId = 12345; |
| 461 |
$customContentHandler = $this->getMockBuilder( ContentHandler::class ) |
| 462 |
->setConstructorArgs( [ 'foo', [] ] ) |
| 463 |
->onlyMethods( [ 'createDifferenceEngine' ] ) |
| 464 |
->getMockForAbstractClass(); |
| 465 |
$customContentHandler->method( 'createDifferenceEngine' ) |
| 466 |
->willReturn( $customDifferenceEngine ); |
| 467 |
|
| 468 |
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() ); |
| 469 |
$this->assertInstanceOf( DifferenceEngineSlotDiffRenderer::class, $slotDiffRenderer ); |
| 470 |
$this->assertSame( |
| 471 |
$customDifferenceEngine->objectId, |
| 472 |
TestingAccessWrapper::newFromObject( $slotDiffRenderer )->differenceEngine->objectId |
| 473 |
); |
| 474 |
} |
| 475 |
|
| 476 |
public function testGetSlotDiffRenderer_nobc() { |
| 477 |
$this->mergeMwGlobalArrayValue( 'wgHooks', [ |
| 478 |
'GetSlotDiffRenderer' => [], |
| 479 |
] ); |
| 480 |
|
| 481 |
|
| 482 |
$customDifferenceEngine = $this->createMock( DifferenceEngine::class ); |
| 483 |
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class ) |
| 484 |
->disableOriginalConstructor() |
| 485 |
->getMockForAbstractClass(); |
| 486 |
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class ) |
| 487 |
->setConstructorArgs( [ 'bar', [] ] ) |
| 488 |
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] ) |
| 489 |
->getMockForAbstractClass(); |
| 490 |
$customContentHandler2->method( 'createDifferenceEngine' ) |
| 491 |
->willReturn( $customDifferenceEngine ); |
| 492 |
$customContentHandler2->method( 'getSlotDiffRendererInternal' ) |
| 493 |
->willReturn( $customSlotDiffRenderer ); |
| 494 |
|
| 495 |
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' ); |
| 496 |
$slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext::getMain() ); |
| 497 |
$this->assertSame( $customSlotDiffRenderer, $slotDiffRenderer ); |
| 498 |
} |
| 499 |
|
| 500 |
public function testGetSlotDiffRenderer_hook() { |
| 501 |
$this->mergeMwGlobalArrayValue( 'wgHooks', [ |
| 502 |
'GetSlotDiffRenderer' => [], |
| 503 |
] ); |
| 504 |
|
| 505 |
|
| 506 |
$customDifferenceEngine = $this->createMock( DifferenceEngine::class ); |
| 507 |
$customContentHandler = $this->getMockBuilder( ContentHandler::class ) |
| 508 |
->setConstructorArgs( [ 'foo', [] ] ) |
| 509 |
->onlyMethods( [ 'createDifferenceEngine' ] ) |
| 510 |
->getMockForAbstractClass(); |
| 511 |
$customContentHandler->method( 'createDifferenceEngine' ) |
| 512 |
->willReturn( $customDifferenceEngine ); |
| 513 |
|
| 514 |
|
| 515 |
$customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer::class ) |
| 516 |
->disableOriginalConstructor() |
| 517 |
->getMockForAbstractClass(); |
| 518 |
$customContentHandler2 = $this->getMockBuilder( ContentHandler::class ) |
| 519 |
->setConstructorArgs( [ 'bar', [] ] ) |
| 520 |
->onlyMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] ) |
| 521 |
->getMockForAbstractClass(); |
| 522 |
$customContentHandler2->method( 'createDifferenceEngine' ) |
| 523 |
->willReturn( $customDifferenceEngine ); |
| 524 |
$customContentHandler2->method( 'getSlotDiffRendererInternal' ) |
| 525 |
->willReturn( $customSlotDiffRenderer ); |
| 526 |
|
| 527 |
|
| 528 |
$customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer::class ) |
| 529 |
->disableOriginalConstructor() |
| 530 |
->getMockForAbstractClass(); |
| 531 |
$this->setTemporaryHook( 'GetSlotDiffRenderer', |
| 532 |
static function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) { |
| 533 |
$slotDiffRenderer = $customSlotDiffRenderer2; |
| 534 |
} ); |
| 535 |
|
| 536 |
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' ); |
| 537 |
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() ); |
| 538 |
$this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer ); |
| 539 |
|
| 540 |
$this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' ); |
| 541 |
$slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext::getMain() ); |
| 542 |
$this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer ); |
| 543 |
} |
| 544 |
|
| 545 |
public static function providerGetPageViewLanguage() { |
| 546 |
yield [ NS_FILE, 'sr', 'sr-ec', 'sr-ec' ]; |
| 547 |
yield [ NS_FILE, 'sr', 'sr', 'sr' ]; |
| 548 |
yield [ NS_MEDIAWIKI, 'sr-ec', 'sr', 'sr-ec' ]; |
| 549 |
yield [ NS_MEDIAWIKI, 'sr', 'sr-ec', 'sr' ]; |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
public function testGetPageViewLanguage( $namespace, $lang, $variant, $expected ) { |
| 558 |
$contentHandler = $this->getMockBuilder( ContentHandler::class ) |
| 559 |
->disableOriginalConstructor() |
| 560 |
->getMockForAbstractClass(); |
| 561 |
|
| 562 |
$title = Title::makeTitle( $namespace, 'SimpleTitle' ); |
| 563 |
|
| 564 |
$this->overrideConfigValue( MainConfigNames::DefaultLanguageVariant, $variant ); |
| 565 |
|
| 566 |
$this->setUserLang( $lang ); |
| 567 |
$this->setContentLang( $lang ); |
| 568 |
|
| 569 |
$pageViewLanguage = $contentHandler->getPageViewLanguage( $title ); |
| 570 |
$this->assertEquals( $expected, $pageViewLanguage->getCode() ); |
| 571 |
} |
| 572 |
|
| 573 |
public static function provideValidateSave() { |
| 574 |
yield 'wikitext' => [ |
| 575 |
new WikitextContent( 'hello world' ), |
| 576 |
true |
| 577 |
]; |
| 578 |
|
| 579 |
yield 'valid json' => [ |
| 580 |
new JsonContent( '{ "0": "bar" }' ), |
| 581 |
true |
| 582 |
]; |
| 583 |
|
| 584 |
yield 'invalid json' => [ |
| 585 |
new JsonContent( 'foo' ), |
| 586 |
false |
| 587 |
]; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
public function testValidateSave( $content, $expectedResult ) { |
| 594 |
$page = new PageIdentityValue( 0, 1, 'Foo', PageIdentity::LOCAL ); |
| 595 |
$contentHandlerFactory = $this->getServiceContainer()->getContentHandlerFactory(); |
| 596 |
$contentHandler = $contentHandlerFactory->getContentHandler( $content->getModel() ); |
| 597 |
$validateParams = new ValidationParams( $page, 0 ); |
| 598 |
|
| 599 |
$status = $contentHandler->validateSave( $content, $validateParams ); |
| 600 |
$this->assertEquals( $expectedResult, $status->isOK() ); |
| 601 |
} |
| 602 |
} |