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
;
30 * @group ContentHandler
32 * @covers \MediaWiki\Content\ContentHandler
34 class ContentHandlerTest
extends MediaWikiIntegrationTestCase
{
36 protected function setUp(): void
{
39 $this->overrideConfigValues( [
40 MainConfigNames
::ExtraNamespaces
=> [
42 12313 => 'Dummy_talk',
44 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
45 // default to CONTENT_MODEL_WIKITEXT.
46 MainConfigNames
::NamespaceContentModels
=> [
49 MainConfigNames
::ContentHandlers
=> [
50 CONTENT_MODEL_WIKITEXT
=> [
51 'class' => WikitextContentHandler
::class,
59 'ParsoidParserFactory',
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 );
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]]' );
79 public static function dataGetDefaultModelFor() {
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
],
108 * @dataProvider dataGetDefaultModelFor
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 ) );
116 public static function dataGetLocalizedName() {
121 // XXX: depends on content language
122 [ CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ],
127 * @dataProvider dataGetLocalizedName
129 public function testGetLocalizedName( $id, $expected ) {
130 $name = ContentHandler
::getLocalizedName( $id );
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"
138 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
139 . "fallen back to use the model id directly."
144 public static function dataGetPageLanguage() {
145 global $wgLanguageCode;
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 ],
159 * @dataProvider dataGetPageLanguage
161 public function testGetPageLanguage( $title, $expected ) {
162 $title = Title
::newFromText( $title );
163 $this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
165 $handler = $this->getServiceContainer()
166 ->getContentHandlerFactory()
167 ->getContentHandler( $title->getContentModel() );
168 $lang = $handler->getPageLanguage( $title );
170 $this->assertInstanceOf( Language
::class, $lang );
171 $this->assertEquals( $expected, $lang->getCode() );
174 public function testGetContentText_Null() {
175 $this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentText' );
177 $text = ContentHandler
::getContentText( $content );
178 $this->assertSame( '', $text );
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 );
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 );
195 public static function dataMakeContent() {
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 ],
205 CONTENT_FORMAT_WIKITEXT
,
206 CONTENT_MODEL_WIKITEXT
,
213 CONTENT_FORMAT_JAVASCRIPT
,
214 CONTENT_MODEL_JAVASCRIPT
,
217 [ 'hallo', 'Dummy:Test', null, "testing", "testing", false ],
219 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, false ],
229 serialize( 'hallo' ),
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 ],
244 * @dataProvider dataMakeContent
246 public function testMakeContent( $data, $title, $modelId, $format,
247 $expectedModelId, $shouldFail
249 $title = Title
::newFromText( $title );
250 $this->getServiceContainer()->getLinkCache()->addBadLinkObj( $title );
252 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
255 $this->fail( "ContentHandler::makeContent should have failed!" );
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() );
264 // dummy, so we don't get the "test did not perform any assertions" message.
265 $this->assertTrue( true );
271 * getAutoSummary() should set "Created blank page" summary if we save an empy string.
273 public function testGetAutosummary() {
274 $this->setContentLang( 'en' );
276 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
277 $title = Title
::makeTitle( NS_HELP
, 'Test' );
278 // Create a new content object with no content
279 $newContent = ContentHandler
::makeContent( '', $title, CONTENT_MODEL_WIKITEXT
, null );
280 // first check, if we become a blank page created summary with the right bitmask
281 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
283 wfMessage( 'autosumm-newblank' )->inContentLanguage()->text(),
286 // now check, what we become with another bitmask
287 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
288 $this->assertSame( '', $autoSummary );
292 * Test software tag that is added when content model of the page changes
294 public function testGetChangeTag() {
295 $this->overrideConfigValue( MainConfigNames
::SoftwareTags
, [ 'mw-contentmodelchange' => true ] );
296 $wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
297 // Create old content object with javascript content model
298 $oldContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT
, null );
299 // Create new content object with wikitext content model
300 $newContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_WIKITEXT
, null );
301 // Get the tag for this edit
302 $tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE
);
303 $this->assertSame( 'mw-contentmodelchange', $tag );
306 public function testSupportsCategories() {
307 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
308 $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
311 public function testSupportsDirectEditing() {
312 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
313 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
316 public static function dummyHookHandler( $foo, &$text, $bar ) {
317 if ( $text === null ||
$text === false ) {
321 $text = strtoupper( $text );
326 public static function provideGetModelForID() {
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 ],
339 * @dataProvider provideGetModelForID
341 public function testGetModelForID( $modelId, $handlerClass ) {
342 $handler = $this->getServiceContainer()->getContentHandlerFactory()
343 ->getContentHandler( $modelId );
345 $this->assertInstanceOf( $handlerClass, $handler );
348 public function testGetFieldsForSearchIndex() {
349 $searchEngine = $this->newSearchEngine();
351 $handler = $this->getMockBuilder( ContentHandler
::class )
353 [ 'serializeContent', 'unserializeContent', 'makeEmptyContent' ]
355 ->disableOriginalConstructor()
358 $fields = $handler->getFieldsForSearchIndex( $searchEngine );
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 );
367 private function newSearchEngine() {
368 $searchEngine = $this->createMock( SearchEngine
::class );
370 $searchEngine->method( 'makeSearchFieldMapping' )
371 ->willReturnCallback( static function ( $name, $type ) {
372 return new DummySearchIndexFieldDefinition( $name, $type );
375 return $searchEngine;
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 );
383 $this->setTemporaryHook( 'SearchDataForIndex',
386 ContentHandler
$handler,
388 ParserOutput
$output,
391 $fields['testDataField'] = 'test content';
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'] );
405 public function testParserOutputForIndexing() {
406 $opportunisticUpdateHook =
407 $this->createMock( OpportunisticLinksUpdateHook
::class );
408 // WikiPage::triggerOpportunisticLinksUpdate should not be triggered when
409 // getParserOutputForIndexing is called
410 $opportunisticUpdateHook->expects( $this->never() )
411 ->method( 'onOpportunisticLinksUpdate' )
412 ->willReturn( false );
413 $this->setTemporaryHook( 'OpportunisticLinksUpdate', $opportunisticUpdateHook );
415 $title = Title
::makeTitle( NS_MAIN
, 'Smithee' );
416 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
417 $revision = $page->getRevisionRecord();
419 $out = $page->getContentHandler()->getParserOutputForIndexing( $page, null, $revision );
420 $this->assertInstanceOf( ParserOutput
::class, $out );
421 $this->assertStringContainsString( 'one who smiths', $out->getRawText() );
424 public function testGetContentModelsHook() {
425 $this->setTemporaryHook( 'GetContentModels', static function ( &$models ) {
426 $models[] = 'Ferrari';
428 $this->hideDeprecated( 'MediaWiki\\Content\\ContentHandler::getContentModels' );
429 $this->assertContains( 'Ferrari', ContentHandler
::getContentModels() );
432 public function testGetSlotDiffRenderer_default() {
433 $this->mergeMwGlobalArrayValue( 'wgHooks', [
434 'GetSlotDiffRenderer' => [],
437 // test default renderer
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 )
448 $slotDiffRenderer = $contentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
449 $this->assertInstanceOf( TextSlotDiffRenderer
::class, $slotDiffRenderer );
452 public function testGetSlotDiffRenderer_bc() {
453 $this->mergeMwGlobalArrayValue( 'wgHooks', [
454 'GetSlotDiffRenderer' => [],
458 $customDifferenceEngine = $this->createMock( DifferenceEngine
::class );
459 // hack to track object identity across cloning
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 /** @var ContentHandler $customContentHandler */
468 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
469 $this->assertInstanceOf( DifferenceEngineSlotDiffRenderer
::class, $slotDiffRenderer );
471 $customDifferenceEngine->objectId
,
472 TestingAccessWrapper
::newFromObject( $slotDiffRenderer )->differenceEngine
->objectId
476 public function testGetSlotDiffRenderer_nobc() {
477 $this->mergeMwGlobalArrayValue( 'wgHooks', [
478 'GetSlotDiffRenderer' => [],
481 // test that B/C renderer does not get used when getSlotDiffRendererInternal is overridden
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 /** @var ContentHandler $customContentHandler2 */
495 $this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
496 $slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext
::getMain() );
497 $this->assertSame( $customSlotDiffRenderer, $slotDiffRenderer );
500 public function testGetSlotDiffRenderer_hook() {
501 $this->mergeMwGlobalArrayValue( 'wgHooks', [
502 'GetSlotDiffRenderer' => [],
505 // test that the hook handler takes precedence
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 /** @var ContentHandler $customContentHandler */
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 /** @var ContentHandler $customContentHandler2 */
528 $customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer
::class )
529 ->disableOriginalConstructor()
530 ->getMockForAbstractClass();
531 $this->setTemporaryHook( 'GetSlotDiffRenderer',
532 static function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) {
533 $slotDiffRenderer = $customSlotDiffRenderer2;
536 $this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
537 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
538 $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
540 $this->hideDeprecated( 'ContentHandler::getSlotDiffRendererInternal' );
541 $slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext
::getMain() );
542 $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
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' ];
553 * Superseded by OutputPageTest::testGetJsVarsAboutPageLang
555 * @dataProvider providerGetPageViewLanguage
557 public function testGetPageViewLanguage( $namespace, $lang, $variant, $expected ) {
558 $contentHandler = $this->getMockBuilder( ContentHandler
::class )
559 ->disableOriginalConstructor()
560 ->getMockForAbstractClass();
562 $title = Title
::makeTitle( $namespace, 'SimpleTitle' );
564 $this->overrideConfigValue( MainConfigNames
::DefaultLanguageVariant
, $variant );
566 $this->setUserLang( $lang );
567 $this->setContentLang( $lang );
569 $pageViewLanguage = $contentHandler->getPageViewLanguage( $title );
570 $this->assertEquals( $expected, $pageViewLanguage->getCode() );
573 public static function provideValidateSave() {
574 yield
'wikitext' => [
575 new WikitextContent( 'hello world' ),
579 yield
'valid json' => [
580 new JsonContent( '{ "0": "bar" }' ),
584 yield
'invalid json' => [
585 new JsonContent( 'foo' ),
591 * @dataProvider provideValidateSave
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 );
599 $status = $contentHandler->validateSave( $content, $validateParams );
600 $this->assertEquals( $expectedResult, $status->isOK() );