2 use MediaWiki\MediaWikiServices
;
5 * @group ContentHandler
8 class ContentHandlerTest
extends MediaWikiTestCase
{
10 protected function setUp() {
13 $this->setMwGlobals( [
14 'wgExtraNamespaces' => [
16 12313 => 'Dummy_talk',
18 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
19 // default to CONTENT_MODEL_WIKITEXT.
20 'wgNamespaceContentModels' => [
23 'wgContentHandlers' => [
24 CONTENT_MODEL_WIKITEXT
=> WikitextContentHandler
::class,
25 CONTENT_MODEL_JAVASCRIPT
=> JavaScriptContentHandler
::class,
26 CONTENT_MODEL_JSON
=> JsonContentHandler
::class,
27 CONTENT_MODEL_CSS
=> CssContentHandler
::class,
28 CONTENT_MODEL_TEXT
=> TextContentHandler
::class,
29 'testing' => DummyContentHandlerForTesting
::class,
30 'testing-callbacks' => function ( $modelId ) {
31 return new DummyContentHandlerForTesting( $modelId );
37 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
40 protected function tearDown() {
42 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
47 public function addDBDataOnce() {
48 $this->insertPage( 'Not_Main_Page', 'This is not a main page' );
49 $this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]' );
52 public static function dataGetDefaultModelFor() {
54 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT
],
55 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
],
56 [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT
],
57 [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT
],
58 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
],
59 [ 'User:Foo', CONTENT_MODEL_WIKITEXT
],
60 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT
],
61 [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT
],
62 [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT
],
63 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
],
64 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS
],
65 [ 'User:Foo/bar.json', CONTENT_MODEL_JSON
],
66 [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT
],
67 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
],
68 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
],
69 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
],
70 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
],
71 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
],
72 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
],
73 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
],
74 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
],
75 [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON
],
76 [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT
],
81 * @dataProvider dataGetDefaultModelFor
82 * @covers ContentHandler::getDefaultModelFor
84 public function testGetDefaultModelFor( $title, $expectedModelId ) {
85 $title = Title
::newFromText( $title );
86 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
90 * @dataProvider dataGetDefaultModelFor
91 * @covers ContentHandler::getForTitle
93 public function testGetForTitle( $title, $expectedContentModel ) {
94 $title = Title
::newFromText( $title );
95 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
96 $handler = ContentHandler
::getForTitle( $title );
97 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
100 public static function dataGetLocalizedName() {
105 // XXX: depends on content language
106 [ CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ],
111 * @dataProvider dataGetLocalizedName
112 * @covers ContentHandler::getLocalizedName
114 public function testGetLocalizedName( $id, $expected ) {
115 $name = ContentHandler
::getLocalizedName( $id );
118 $this->assertNotNull( $name, "no name found for content model $id" );
119 $this->assertTrue( preg_match( $expected, $name ) > 0,
120 "content model name for #$id did not match pattern $expected"
123 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
124 . "fallen back to use the model id directly."
129 public static function dataGetPageLanguage() {
130 global $wgLanguageCode;
133 [ "Main", $wgLanguageCode ],
134 [ "Dummy:Foo", $wgLanguageCode ],
135 [ "MediaWiki:common.js", 'en' ],
136 [ "User:Foo/common.js", 'en' ],
137 [ "MediaWiki:common.css", 'en' ],
138 [ "User:Foo/common.css", 'en' ],
139 [ "User:Foo", $wgLanguageCode ],
141 [ CONTENT_MODEL_JAVASCRIPT
, 'javascript' ],
146 * @dataProvider dataGetPageLanguage
147 * @covers ContentHandler::getPageLanguage
149 public function testGetPageLanguage( $title, $expected ) {
150 if ( is_string( $title ) ) {
151 $title = Title
::newFromText( $title );
152 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
155 $expected = wfGetLangObj( $expected );
157 $handler = ContentHandler
::getForTitle( $title );
158 $lang = $handler->getPageLanguage( $title );
160 $this->assertEquals( $expected->getCode(), $lang->getCode() );
163 public static function dataGetContentText_Null() {
172 * @dataProvider dataGetContentText_Null
173 * @covers ContentHandler::getContentText
175 public function testGetContentText_Null( $contentHandlerTextFallback ) {
176 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
180 $text = ContentHandler
::getContentText( $content );
181 $this->assertEquals( '', $text );
184 public static function dataGetContentText_TextContent() {
193 * @dataProvider dataGetContentText_TextContent
194 * @covers ContentHandler::getContentText
196 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
197 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
199 $content = new WikitextContent( "hello world" );
201 $text = ContentHandler
::getContentText( $content );
202 $this->assertEquals( $content->getNativeData(), $text );
206 * ContentHandler::getContentText should have thrown an exception for non-text Content object
207 * @expectedException MWException
208 * @covers ContentHandler::getContentText
210 public function testGetContentText_NonTextContent_fail() {
211 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
213 $content = new DummyContentForTesting( "hello world" );
215 ContentHandler
::getContentText( $content );
219 * @covers ContentHandler::getContentText
221 public function testGetContentText_NonTextContent_serialize() {
222 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
224 $content = new DummyContentForTesting( "hello world" );
226 $text = ContentHandler
::getContentText( $content );
227 $this->assertEquals( $content->serialize(), $text );
231 * @covers ContentHandler::getContentText
233 public function testGetContentText_NonTextContent_ignore() {
234 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
236 $content = new DummyContentForTesting( "hello world" );
238 $text = ContentHandler
::getContentText( $content );
239 $this->assertNull( $text );
242 public static function dataMakeContent() {
244 [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ],
245 [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ],
246 [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
252 CONTENT_FORMAT_WIKITEXT
,
253 CONTENT_MODEL_WIKITEXT
,
261 CONTENT_FORMAT_JAVASCRIPT
,
262 CONTENT_MODEL_JAVASCRIPT
,
266 [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
268 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ],
279 serialize( 'hallo' ),
284 serialize( 'hallo' ),
288 [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ],
289 [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ],
290 [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ],
295 * @dataProvider dataMakeContent
296 * @covers ContentHandler::makeContent
298 public function testMakeContent( $data, $title, $modelId, $format,
299 $expectedModelId, $expectedNativeData, $shouldFail
301 $title = Title
::newFromText( $title );
302 MediaWikiServices
::getInstance()->getLinkCache()->addBadLinkObj( $title );
304 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
307 $this->fail( "ContentHandler::makeContent should have failed!" );
310 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
311 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
312 } catch ( MWException
$ex ) {
313 if ( !$shouldFail ) {
314 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
316 // dummy, so we don't get the "test did not perform any assertions" message.
317 $this->assertTrue( true );
323 * @covers ContentHandler::getAutosummary
325 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
328 public function testGetAutosummary() {
329 $this->setContentLang( 'en' );
331 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
332 $title = Title
::newFromText( 'Help:Test' );
333 // Create a new content object with no content
334 $newContent = ContentHandler
::makeContent( '', $title, CONTENT_MODEL_WIKITEXT
, null );
335 // first check, if we become a blank page created summary with the right bitmask
336 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
337 $this->assertEquals( $autoSummary,
338 wfMessage( 'autosumm-newblank' )->inContentLanguage()->text() );
339 // now check, what we become with another bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
341 $this->assertEquals( $autoSummary, '' );
345 * Test software tag that is added when content model of the page changes
346 * @covers ContentHandler::getChangeTag
348 public function testGetChangeTag() {
349 $this->setMwGlobals( 'wgSoftwareTags', [ 'mw-contentmodelchange' => true ] );
350 $wikitextContentHandler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
351 // Create old content object with javascript content model
352 $oldContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_JAVASCRIPT
, null );
353 // Create new content object with wikitext content model
354 $newContent = ContentHandler
::makeContent( '', null, CONTENT_MODEL_WIKITEXT
, null );
355 // Get the tag for this edit
356 $tag = $wikitextContentHandler->getChangeTag( $oldContent, $newContent, EDIT_UPDATE
);
357 $this->assertSame( $tag, 'mw-contentmodelchange' );
361 * @covers ContentHandler::supportsCategories
363 public function testSupportsCategories() {
364 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
365 $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
369 * @covers ContentHandler::supportsDirectEditing
371 public function testSupportsDirectEditing() {
372 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
373 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
376 public static function dummyHookHandler( $foo, &$text, $bar ) {
377 if ( $text === null ||
$text === false ) {
381 $text = strtoupper( $text );
386 public function provideGetModelForID() {
388 [ CONTENT_MODEL_WIKITEXT
, WikitextContentHandler
::class ],
389 [ CONTENT_MODEL_JAVASCRIPT
, JavaScriptContentHandler
::class ],
390 [ CONTENT_MODEL_JSON
, JsonContentHandler
::class ],
391 [ CONTENT_MODEL_CSS
, CssContentHandler
::class ],
392 [ CONTENT_MODEL_TEXT
, TextContentHandler
::class ],
393 [ 'testing', DummyContentHandlerForTesting
::class ],
394 [ 'testing-callbacks', DummyContentHandlerForTesting
::class ],
399 * @covers ContentHandler::getForModelID
400 * @dataProvider provideGetModelForID
402 public function testGetModelForID( $modelId, $handlerClass ) {
403 $handler = ContentHandler
::getForModelID( $modelId );
405 $this->assertInstanceOf( $handlerClass, $handler );
409 * @covers ContentHandler::getFieldsForSearchIndex
411 public function testGetFieldsForSearchIndex() {
412 $searchEngine = $this->newSearchEngine();
414 $handler = ContentHandler
::getForModelID( CONTENT_MODEL_WIKITEXT
);
416 $fields = $handler->getFieldsForSearchIndex( $searchEngine );
418 $this->assertArrayHasKey( 'category', $fields );
419 $this->assertArrayHasKey( 'external_link', $fields );
420 $this->assertArrayHasKey( 'outgoing_link', $fields );
421 $this->assertArrayHasKey( 'template', $fields );
422 $this->assertArrayHasKey( 'content_model', $fields );
425 private function newSearchEngine() {
426 $searchEngine = $this->getMockBuilder( SearchEngine
::class )
429 $searchEngine->expects( $this->any() )
430 ->method( 'makeSearchFieldMapping' )
431 ->will( $this->returnCallback( function ( $name, $type ) {
432 return new DummySearchIndexFieldDefinition( $name, $type );
435 return $searchEngine;
439 * @covers ContentHandler::getDataForSearchIndex
441 public function testDataIndexFields() {
442 $mockEngine = $this->createMock( SearchEngine
::class );
443 $title = Title
::newFromText( 'Not_Main_Page', NS_MAIN
);
444 $page = new WikiPage( $title );
446 $this->setTemporaryHook( 'SearchDataForIndex',
449 ContentHandler
$handler,
451 ParserOutput
$output,
454 $fields['testDataField'] = 'test content';
457 $output = $page->getContent()->getParserOutput( $title );
458 $data = $page->getContentHandler()->getDataForSearchIndex( $page, $output, $mockEngine );
459 $this->assertArrayHasKey( 'text', $data );
460 $this->assertArrayHasKey( 'text_bytes', $data );
461 $this->assertArrayHasKey( 'language', $data );
462 $this->assertArrayHasKey( 'testDataField', $data );
463 $this->assertEquals( 'test content', $data['testDataField'] );
464 $this->assertEquals( 'wikitext', $data['content_model'] );
468 * @covers ContentHandler::getParserOutputForIndexing
470 public function testParserOutputForIndexing() {
471 $title = Title
::newFromText( 'Smithee', NS_MAIN
);
472 $page = new WikiPage( $title );
474 $out = $page->getContentHandler()->getParserOutputForIndexing( $page );
475 $this->assertInstanceOf( ParserOutput
::class, $out );
476 $this->assertContains( 'one who smiths', $out->getRawText() );
480 * @covers ContentHandler::getContentModels
482 public function testGetContentModelsHook() {
483 $this->setTemporaryHook( 'GetContentModels', function ( &$models ) {
484 $models[] = 'Ferrari';
486 $this->assertContains( 'Ferrari', ContentHandler
::getContentModels() );