2 use MediaWiki\MediaWikiServices
;
5 * @group ContentHandler
7 class ContentHandlerTest
extends MediaWikiTestCase
{
9 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',
25 CONTENT_MODEL_JAVASCRIPT
=> 'JavaScriptContentHandler',
26 CONTENT_MODEL_JSON
=> 'JsonContentHandler',
27 CONTENT_MODEL_CSS
=> 'CssContentHandler',
28 CONTENT_MODEL_TEXT
=> 'TextContentHandler',
29 'testing' => 'DummyContentHandlerForTesting',
30 'testing-callbacks' => function( $modelId ) {
31 return new DummyContentHandlerForTesting( $modelId );
36 // Reset namespace cache
37 MWNamespace
::getCanonicalNamespaces( true );
38 $wgContLang->resetNamespaces();
40 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
43 protected function tearDown() {
46 // Reset namespace cache
47 MWNamespace
::getCanonicalNamespaces( true );
48 $wgContLang->resetNamespaces();
50 MediaWikiServices
::getInstance()->resetServiceForTesting( 'LinkCache' );
55 public static function dataGetDefaultModelFor() {
57 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT
],
58 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
],
59 [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT
],
60 [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT
],
61 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
],
62 [ 'User:Foo', CONTENT_MODEL_WIKITEXT
],
63 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT
],
64 [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT
],
65 [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT
],
66 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
],
67 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS
],
68 [ 'User:Foo/bar.json', CONTENT_MODEL_JSON
],
69 [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT
],
70 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
],
71 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
],
72 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
],
73 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
],
74 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
],
75 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
],
76 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
],
77 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
],
78 [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON
],
79 [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT
],
84 * @dataProvider dataGetDefaultModelFor
85 * @covers ContentHandler::getDefaultModelFor
87 public function testGetDefaultModelFor( $title, $expectedModelId ) {
88 $title = Title
::newFromText( $title );
89 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
93 * @dataProvider dataGetDefaultModelFor
94 * @covers ContentHandler::getForTitle
96 public function testGetForTitle( $title, $expectedContentModel ) {
97 $title = Title
::newFromText( $title );
98 LinkCache
::singleton()->addBadLinkObj( $title );
99 $handler = ContentHandler
::getForTitle( $title );
100 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
103 public static function dataGetLocalizedName() {
108 // XXX: depends on content language
109 [ CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ],
114 * @dataProvider dataGetLocalizedName
115 * @covers ContentHandler::getLocalizedName
117 public function testGetLocalizedName( $id, $expected ) {
118 $name = ContentHandler
::getLocalizedName( $id );
121 $this->assertNotNull( $name, "no name found for content model $id" );
122 $this->assertTrue( preg_match( $expected, $name ) > 0,
123 "content model name for #$id did not match pattern $expected"
126 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
127 . "fallen back to use the model id directly."
132 public static function dataGetPageLanguage() {
133 global $wgLanguageCode;
136 [ "Main", $wgLanguageCode ],
137 [ "Dummy:Foo", $wgLanguageCode ],
138 [ "MediaWiki:common.js", 'en' ],
139 [ "User:Foo/common.js", 'en' ],
140 [ "MediaWiki:common.css", 'en' ],
141 [ "User:Foo/common.css", 'en' ],
142 [ "User:Foo", $wgLanguageCode ],
144 [ CONTENT_MODEL_JAVASCRIPT
, 'javascript' ],
149 * @dataProvider dataGetPageLanguage
150 * @covers ContentHandler::getPageLanguage
152 public function testGetPageLanguage( $title, $expected ) {
153 if ( is_string( $title ) ) {
154 $title = Title
::newFromText( $title );
155 LinkCache
::singleton()->addBadLinkObj( $title );
158 $expected = wfGetLangObj( $expected );
160 $handler = ContentHandler
::getForTitle( $title );
161 $lang = $handler->getPageLanguage( $title );
163 $this->assertEquals( $expected->getCode(), $lang->getCode() );
166 public static function dataGetContentText_Null() {
175 * @dataProvider dataGetContentText_Null
176 * @covers ContentHandler::getContentText
178 public function testGetContentText_Null( $contentHandlerTextFallback ) {
179 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
183 $text = ContentHandler
::getContentText( $content );
184 $this->assertEquals( '', $text );
187 public static function dataGetContentText_TextContent() {
196 * @dataProvider dataGetContentText_TextContent
197 * @covers ContentHandler::getContentText
199 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
200 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
202 $content = new WikitextContent( "hello world" );
204 $text = ContentHandler
::getContentText( $content );
205 $this->assertEquals( $content->getNativeData(), $text );
209 * ContentHandler::getContentText should have thrown an exception for non-text Content object
210 * @expectedException MWException
211 * @covers ContentHandler::getContentText
213 public function testGetContentText_NonTextContent_fail() {
214 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
216 $content = new DummyContentForTesting( "hello world" );
218 ContentHandler
::getContentText( $content );
222 * @covers ContentHandler::getContentText
224 public function testGetContentText_NonTextContent_serialize() {
225 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
227 $content = new DummyContentForTesting( "hello world" );
229 $text = ContentHandler
::getContentText( $content );
230 $this->assertEquals( $content->serialize(), $text );
234 * @covers ContentHandler::getContentText
236 public function testGetContentText_NonTextContent_ignore() {
237 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
239 $content = new DummyContentForTesting( "hello world" );
241 $text = ContentHandler
::getContentText( $content );
242 $this->assertNull( $text );
246 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
249 public static function dataMakeContent() {
251 [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ],
252 [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ],
253 [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
259 CONTENT_FORMAT_WIKITEXT
,
260 CONTENT_MODEL_WIKITEXT
,
268 CONTENT_FORMAT_JAVASCRIPT
,
269 CONTENT_MODEL_JAVASCRIPT
,
273 [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
275 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ],
286 serialize( 'hallo' ),
291 serialize( 'hallo' ),
295 [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ],
296 [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ],
297 [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ],
302 * @dataProvider dataMakeContent
303 * @covers ContentHandler::makeContent
305 public function testMakeContent( $data, $title, $modelId, $format,
306 $expectedModelId, $expectedNativeData, $shouldFail
308 $title = Title
::newFromText( $title );
309 LinkCache
::singleton()->addBadLinkObj( $title );
311 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
314 $this->fail( "ContentHandler::makeContent should have failed!" );
317 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
318 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
319 } catch ( MWException
$ex ) {
320 if ( !$shouldFail ) {
321 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
323 // dummy, so we don't get the "test did not perform any assertions" message.
324 $this->assertTrue( true );
330 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
333 public function testGetAutosummary() {
334 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
336 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
337 $title = Title
::newFromText( 'Help:Test' );
338 // Create a new content object with no content
339 $newContent = ContentHandler
::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT
);
340 // first check, if we become a blank page created summary with the right bitmask
341 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
342 $this->assertEquals( $autoSummary, 'Created blank page' );
343 // now check, what we become with another bitmask
344 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
345 $this->assertEquals( $autoSummary, '' );
349 public function testSupportsSections() {
350 $this->markTestIncomplete( "not yet implemented" );
354 public function testSupportsCategories() {
355 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
356 $this->assertTrue( $handler->supportsCategories(), 'content model supports categories' );
359 public function testSupportsDirectEditing() {
360 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
361 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
365 * @covers ContentHandler::runLegacyHooks
367 public function testRunLegacyHooks() {
368 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
370 $content = new WikitextContent( 'test text' );
371 $ok = ContentHandler
::runLegacyHooks(
372 'testRunLegacyHooks',
373 [ 'foo', &$content, 'bar' ],
377 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
378 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
381 public static function dummyHookHandler( $foo, &$text, $bar ) {
382 if ( $text === null ||
$text === false ) {
386 $text = strtoupper( $text );
391 public function provideGetModelForID() {
393 [ CONTENT_MODEL_WIKITEXT
, 'WikitextContentHandler' ],
394 [ CONTENT_MODEL_JAVASCRIPT
, 'JavaScriptContentHandler' ],
395 [ CONTENT_MODEL_JSON
, 'JsonContentHandler' ],
396 [ CONTENT_MODEL_CSS
, 'CssContentHandler' ],
397 [ CONTENT_MODEL_TEXT
, 'TextContentHandler' ],
398 [ 'testing', 'DummyContentHandlerForTesting' ],
399 [ 'testing-callbacks', 'DummyContentHandlerForTesting' ],
404 * @dataProvider provideGetModelForID
406 public function testGetModelForID( $modelId, $handlerClass ) {
407 $handler = ContentHandler
::getForModelID( $modelId );
409 $this->assertInstanceOf( $handlerClass, $handler );