2 use MediaWiki\MediaWikiServices
;
3 use Wikimedia\TestingAccessWrapper
;
6 * @group ContentHandler
9 class ContentHandlerTest
extends MediaWikiTestCase
{
11 protected function setUp() {
14 $this->setMwGlobals( [
15 'wgExtraNamespaces' => [
17 12313 => 'Dummy_talk',
19 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
20 // default to CONTENT_MODEL_WIKITEXT.
21 'wgNamespaceContentModels' => [
24 'wgContentHandlers' => [
25 CONTENT_MODEL_WIKITEXT
=> WikitextContentHandler
::class,
26 CONTENT_MODEL_JAVASCRIPT
=> JavaScriptContentHandler
::class,
27 CONTENT_MODEL_JSON
=> JsonContentHandler
::class,
28 CONTENT_MODEL_CSS
=> CssContentHandler
::class,
29 CONTENT_MODEL_TEXT
=> TextContentHandler
::class,
30 'testing' => DummyContentHandlerForTesting
::class,
31 'testing-callbacks' => function ( $modelId ) {
32 return new DummyContentHandlerForTesting( $modelId );
38 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
41 protected function tearDown() {
43 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
48 public function addDBDataOnce() {
49 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
50 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
53 public static function dataGetDefaultModelFor() {
55 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT
],
56 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
],
57 [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT
],
58 [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT
],
59 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
],
60 [ 'User:Foo', CONTENT_MODEL_WIKITEXT
],
61 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT
],
62 [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT
],
63 [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT
],
64 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
],
65 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS
],
66 [ 'User:Foo/bar.json', CONTENT_MODEL_JSON
],
67 [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT
],
68 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
],
69 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
],
70 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
],
71 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
],
72 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
],
73 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
],
74 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
],
75 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
],
76 [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON
],
77 [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT
],
82 * @dataProvider dataGetDefaultModelFor
83 * @covers ContentHandler::getDefaultModelFor
85 public function testGetDefaultModelFor( $title, $expectedModelId ) {
86 $title = Title
::newFromText( $title );
87 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
91 * @dataProvider dataGetDefaultModelFor
92 * @covers ContentHandler::getForTitle
94 public function testGetForTitle( $title, $expectedContentModel ) {
95 $title = Title
::newFromText( $title );
96 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
97 $handler = ContentHandler
::getForTitle( $title );
98 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
101 public static function dataGetLocalizedName() {
106 // XXX: depends on content language
107 [ CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ],
112 * @dataProvider dataGetLocalizedName
113 * @covers ContentHandler::getLocalizedName
115 public function testGetLocalizedName( $id, $expected ) {
116 $name = ContentHandler
::getLocalizedName( $id );
119 $this->assertNotNull( $name, "no name found for content model $id" );
120 $this->assertTrue( preg_match( $expected, $name ) > 0,
121 "content model name for #$id did not match pattern $expected"
124 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
125 . "fallen back to use the model id directly."
130 public static function dataGetPageLanguage() {
131 global $wgLanguageCode;
134 [ "Main", $wgLanguageCode ],
135 [ "Dummy:Foo", $wgLanguageCode ],
136 [ "MediaWiki:common.js", 'en' ],
137 [ "User:Foo/common.js", 'en' ],
138 [ "MediaWiki:common.css", 'en' ],
139 [ "User:Foo/common.css", 'en' ],
140 [ "User:Foo", $wgLanguageCode ],
142 [ CONTENT_MODEL_JAVASCRIPT
, 'javascript' ],
147 * @dataProvider dataGetPageLanguage
148 * @covers ContentHandler::getPageLanguage
150 public function testGetPageLanguage( $title, $expected ) {
151 if ( is_string( $title ) ) {
152 $title = Title
::newFromText( $title );
153 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
156 $expected = wfGetLangObj( $expected );
158 $handler = ContentHandler
::getForTitle( $title );
159 $lang = $handler->getPageLanguage( $title );
161 $this->assertEquals( $expected->getCode(), $lang->getCode() );
164 public static function dataGetContentText_Null() {
173 * @dataProvider dataGetContentText_Null
174 * @covers ContentHandler::getContentText
176 public function testGetContentText_Null( $contentHandlerTextFallback ) {
177 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
181 $text = ContentHandler
::getContentText( $content );
182 $this->assertEquals( '', $text );
185 public static function dataGetContentText_TextContent() {
194 * @dataProvider dataGetContentText_TextContent
195 * @covers ContentHandler::getContentText
197 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
198 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
200 $content = new WikitextContent( "hello world" );
202 $text = ContentHandler
::getContentText( $content );
203 $this->assertEquals( $content->getNativeData(), $text );
207 * ContentHandler::getContentText should have thrown an exception for non-text Content object
208 * @expectedException MWException
209 * @covers ContentHandler::getContentText
211 public function testGetContentText_NonTextContent_fail() {
212 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
214 $content = new DummyContentForTesting( "hello world" );
216 ContentHandler
::getContentText( $content );
220 * @covers ContentHandler::getContentText
222 public function testGetContentText_NonTextContent_serialize() {
223 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
225 $content = new DummyContentForTesting( "hello world" );
227 $text = ContentHandler
::getContentText( $content );
228 $this->assertEquals( $content->serialize(), $text );
232 * @covers ContentHandler::getContentText
234 public function testGetContentText_NonTextContent_ignore() {
235 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
237 $content = new DummyContentForTesting( "hello world" );
239 $text = ContentHandler
::getContentText( $content );
240 $this->assertNull( $text );
243 public static function dataMakeContent() {
245 [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ],
246 [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ],
247 [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
253 CONTENT_FORMAT_WIKITEXT
,
254 CONTENT_MODEL_WIKITEXT
,
262 CONTENT_FORMAT_JAVASCRIPT
,
263 CONTENT_MODEL_JAVASCRIPT
,
267 [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
269 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ],
280 serialize( 'hallo' ),
285 serialize( 'hallo' ),
289 [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ],
290 [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ],
291 [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ],
296 * @dataProvider dataMakeContent
297 * @covers ContentHandler::makeContent
299 public function testMakeContent( $data, $title, $modelId, $format,
300 $expectedModelId, $expectedNativeData, $shouldFail
302 $title = Title
::newFromText( $title );
303 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
305 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
308 $this->fail( "ContentHandler::makeContent should have failed!" );
311 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
312 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
313 } catch ( MWException
$ex ) {
314 if ( !$shouldFail ) {
315 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
317 // dummy, so we don't get the "test did not perform any assertions" message.
318 $this->assertTrue( true );
324 * @covers ContentHandler::getAutosummary
326 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
329 public function testGetAutosummary() {
330 $this->setContentLang( 'en' );
332 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
333 $title = Title
::newFromText( 'Help:Test' );
334 // Create a new content object with no content
335 $newContent = ContentHandler
::makeContent( '', $title, CONTENT_MODEL_WIKITEXT
, null );
336 // first check, if we become a blank page created summary with the right bitmask
337 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
338 $this->assertEquals( $autoSummary,
339 wfMessage( 'autosumm-newblank' )->inContentLanguage()->text() );
340 // now check, what we become with another bitmask
341 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
342 $this->assertEquals( $autoSummary, '' );
346 * Test software tag that is added when content model of the page changes
347 * @covers ContentHandler::getChangeTag
349 public function testGetChangeTag() {
350 $this->setMwGlobals( 'wgSoftwareTags', [ 'mw-contentmodelchange' => true ] );
351 $wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
352 // Create old content object with javascript content model
353 $oldContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT
, null );
354 // Create new content object with wikitext content model
355 $newContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_WIKITEXT
, null );
356 // Get the tag for this edit
357 $tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE
);
358 $this->assertSame( $tag, 'mw-contentmodelchange' );
362 * @covers ContentHandler::supportsCategories
364 public function testSupportsCategories() {
365 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
366 $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
370 * @covers ContentHandler::supportsDirectEditing
372 public function testSupportsDirectEditing() {
373 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
374 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
377 public static function dummyHookHandler( $foo, &$text, $bar ) {
378 if ( $text === null ||
$text === false ) {
382 $text = strtoupper( $text );
387 public function provideGetModelForID() {
389 [ CONTENT_MODEL_WIKITEXT
, WikitextContentHandler
::class ],
390 [ CONTENT_MODEL_JAVASCRIPT
, JavaScriptContentHandler
::class ],
391 [ CONTENT_MODEL_JSON
, JsonContentHandler
::class ],
392 [ CONTENT_MODEL_CSS
, CssContentHandler
::class ],
393 [ CONTENT_MODEL_TEXT
, TextContentHandler
::class ],
394 [ 'testing', DummyContentHandlerForTesting
::class ],
395 [ 'testing-callbacks', DummyContentHandlerForTesting
::class ],
400 * @covers ContentHandler::getForModelID
401 * @dataProvider provideGetModelForID
403 public function testGetModelForID( $modelId, $handlerClass ) {
404 $handler = ContentHandler
::getForModelID( $modelId );
406 $this->assertInstanceOf( $handlerClass, $handler );
410 * @covers ContentHandler::getFieldsForSearchIndex
412 public function testGetFieldsForSearchIndex() {
413 $searchEngine = $this->newSearchEngine();
415 $handler = ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
417 $fields = $handler->getFieldsForSearchIndex( $searchEngine );
419 $this->assertArrayHasKey( 'category', $fields );
420 $this->assertArrayHasKey( 'external_link', $fields );
421 $this->assertArrayHasKey( 'outgoing_link', $fields );
422 $this->assertArrayHasKey( 'template', $fields );
423 $this->assertArrayHasKey( 'content_model', $fields );
426 private function newSearchEngine() {
427 $searchEngine = $this->getMockBuilder( SearchEngine
::class )
430 $searchEngine->expects( $this->any() )
431 ->method( 'makeSearchFieldMapping' )
432 ->will( $this->returnCallback( function ( $name, $type ) {
433 return new DummySearchIndexFieldDefinition( $name, $type );
436 return $searchEngine;
440 * @covers ContentHandler::getDataForSearchIndex
442 public function testDataIndexFields() {
443 $mockEngine = $this->createMock( SearchEngine
::class );
444 $title = Title
::newFromText( 'Not_Main_Page', NS_MAIN
);
445 $page = new WikiPage( $title );
447 $this->setTemporaryHook( 'SearchDataForIndex',
450 ContentHandler
$handler,
452 ParserOutput
$output,
455 $fields['testDataField'] = 'test content';
458 $output = $page->getContent()->getParserOutput( $title );
459 $data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine );
460 $this->assertArrayHasKey( 'text', $data );
461 $this->assertArrayHasKey( 'text_bytes', $data );
462 $this->assertArrayHasKey( 'language', $data );
463 $this->assertArrayHasKey( 'testDataField', $data );
464 $this->assertEquals( 'test content', $data['testDataField'] );
465 $this->assertEquals( 'wikitext', $data['content_model'] );
469 * @covers ContentHandler::getParserOutputForIndexing
471 public function testParserOutputForIndexing() {
472 $title = Title
::newFromText( 'Smithee', NS_MAIN
);
473 $page = new WikiPage( $title );
475 $out = $page->getContentHandler()->getParserOutputForIndexing( $page );
476 $this->assertInstanceOf( ParserOutput
::class, $out );
477 $this->assertContains( 'one who smiths', $out->getRawText() );
481 * @covers ContentHandler::getContentModels
483 public function testGetContentModelsHook() {
484 $this->setTemporaryHook( 'GetContentModels', function ( &$models ) {
485 $models[] = 'Ferrari';
487 $this->assertContains( 'Ferrari', ContentHandler
::getContentModels() );
491 * @covers ContentHandler::getSlotDiffRenderer
493 public function testGetSlotDiffRenderer_default() {
494 $this->mergeMwGlobalArrayValue( 'wgHooks', [
495 'GetSlotDiffRenderer' => [],
498 // test default renderer
499 $contentHandler = new WikitextContentHandler( CONTENT_MODEL_WIKITEXT
);
500 $slotDiffRenderer = $contentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
501 $this->assertInstanceOf( TextSlotDiffRenderer
::class, $slotDiffRenderer );
505 * @covers ContentHandler::getSlotDiffRenderer
507 public function testGetSlotDiffRenderer_bc() {
508 $this->mergeMwGlobalArrayValue( 'wgHooks', [
509 'GetSlotDiffRenderer' => [],
513 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
514 ->disableOriginalConstructor()
516 // hack to track object identity across cloning
517 $customDifferenceEngine->objectId
= 12345;
518 $customContentHandler = $this->getMockBuilder( ContentHandler
::class )
519 ->setConstructorArgs( [ 'foo', [] ] )
520 ->setMethods( [ 'createDifferenceEngine' ] )
521 ->getMockForAbstractClass();
522 $customContentHandler->expects( $this->any() )
523 ->method( 'createDifferenceEngine' )
524 ->willReturn( $customDifferenceEngine );
525 /** @var $customContentHandler ContentHandler */
526 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
527 $this->assertInstanceOf( DifferenceEngineSlotDiffRenderer
::class, $slotDiffRenderer );
529 $customDifferenceEngine->objectId
,
530 TestingAccessWrapper
::newFromObject( $slotDiffRenderer )->differenceEngine
->objectId
535 * @covers ContentHandler::getSlotDiffRenderer
537 public function testGetSlotDiffRenderer_nobc() {
538 $this->mergeMwGlobalArrayValue( 'wgHooks', [
539 'GetSlotDiffRenderer' => [],
542 // test that B/C renderer does not get used when getSlotDiffRendererInternal is overridden
543 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
544 ->disableOriginalConstructor()
546 $customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer
::class )
547 ->disableOriginalConstructor()
548 ->getMockForAbstractClass();
549 $customContentHandler2 = $this->getMockBuilder( ContentHandler
::class )
550 ->setConstructorArgs( [ 'bar', [] ] )
551 ->setMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
552 ->getMockForAbstractClass();
553 $customContentHandler2->expects( $this->any() )
554 ->method( 'createDifferenceEngine' )
555 ->willReturn( $customDifferenceEngine );
556 $customContentHandler2->expects( $this->any() )
557 ->method( 'getSlotDiffRendererInternal' )
558 ->willReturn( $customSlotDiffRenderer );
559 /** @var $customContentHandler2 ContentHandler */
560 $slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext
::getMain() );
561 $this->assertSame( $customSlotDiffRenderer, $slotDiffRenderer );
565 * @covers ContentHandler::getSlotDiffRenderer
567 public function testGetSlotDiffRenderer_hook() {
568 $this->mergeMwGlobalArrayValue( 'wgHooks', [
569 'GetSlotDiffRenderer' => [],
572 // test that the hook handler takes precedence
573 $customDifferenceEngine = $this->getMockBuilder( DifferenceEngine
::class )
574 ->disableOriginalConstructor()
576 $customContentHandler = $this->getMockBuilder( ContentHandler
::class )
577 ->setConstructorArgs( [ 'foo', [] ] )
578 ->setMethods( [ 'createDifferenceEngine' ] )
579 ->getMockForAbstractClass();
580 $customContentHandler->expects( $this->any() )
581 ->method( 'createDifferenceEngine' )
582 ->willReturn( $customDifferenceEngine );
583 /** @var $customContentHandler ContentHandler */
585 $customSlotDiffRenderer = $this->getMockBuilder( SlotDiffRenderer
::class )
586 ->disableOriginalConstructor()
587 ->getMockForAbstractClass();
588 $customContentHandler2 = $this->getMockBuilder( ContentHandler
::class )
589 ->setConstructorArgs( [ 'bar', [] ] )
590 ->setMethods( [ 'createDifferenceEngine', 'getSlotDiffRendererInternal' ] )
591 ->getMockForAbstractClass();
592 $customContentHandler2->expects( $this->any() )
593 ->method( 'createDifferenceEngine' )
594 ->willReturn( $customDifferenceEngine );
595 $customContentHandler2->expects( $this->any() )
596 ->method( 'getSlotDiffRendererInternal' )
597 ->willReturn( $customSlotDiffRenderer );
598 /** @var $customContentHandler2 ContentHandler */
600 $customSlotDiffRenderer2 = $this->getMockBuilder( SlotDiffRenderer
::class )
601 ->disableOriginalConstructor()
602 ->getMockForAbstractClass();
603 $this->setTemporaryHook( 'GetSlotDiffRenderer',
604 function ( $handler, &$slotDiffRenderer ) use ( $customSlotDiffRenderer2 ) {
605 $slotDiffRenderer = $customSlotDiffRenderer2;
608 $slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext
::getMain() );
609 $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );
610 $slotDiffRenderer = $customContentHandler2->getSlotDiffRenderer( RequestContext
::getMain() );
611 $this->assertSame( $customSlotDiffRenderer2, $slotDiffRenderer );