4 * @group ContentHandler
6 class ContentHandlerTest
extends MediaWikiTestCase
{
8 protected function setUp() {
12 $this->setMwGlobals( [
13 'wgExtraNamespaces' => [
15 12313 => 'Dummy_talk',
17 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
18 // default to CONTENT_MODEL_WIKITEXT.
19 'wgNamespaceContentModels' => [
22 'wgContentHandlers' => [
23 CONTENT_MODEL_WIKITEXT
=> 'WikitextContentHandler',
24 CONTENT_MODEL_JAVASCRIPT
=> 'JavaScriptContentHandler',
25 CONTENT_MODEL_JSON
=> 'JsonContentHandler',
26 CONTENT_MODEL_CSS
=> 'CssContentHandler',
27 CONTENT_MODEL_TEXT
=> 'TextContentHandler',
28 'testing' => 'DummyContentHandlerForTesting',
29 'testing-callbacks' => function( $modelId ) {
30 return new DummyContentHandlerForTesting( $modelId );
35 // Reset namespace cache
36 MWNamespace
::getCanonicalNamespaces( true );
37 $wgContLang->resetNamespaces();
39 LinkCache
::destroySingleton();
42 protected function tearDown() {
45 // Reset namespace cache
46 MWNamespace
::getCanonicalNamespaces( true );
47 $wgContLang->resetNamespaces();
49 LinkCache
::destroySingleton();
54 public static function dataGetDefaultModelFor() {
56 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT
],
57 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
],
58 [ 'Help:Foo.css', CONTENT_MODEL_WIKITEXT
],
59 [ 'Help:Foo.json', CONTENT_MODEL_WIKITEXT
],
60 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
],
61 [ 'User:Foo', CONTENT_MODEL_WIKITEXT
],
62 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT
],
63 [ 'User:Foo.css', CONTENT_MODEL_WIKITEXT
],
64 [ 'User:Foo.json', CONTENT_MODEL_WIKITEXT
],
65 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
],
66 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS
],
67 [ 'User:Foo/bar.json', CONTENT_MODEL_JSON
],
68 [ 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT
],
69 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
],
70 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
],
71 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
],
72 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
],
73 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
],
74 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
],
75 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
],
76 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
],
77 [ 'MediaWiki:Foo.json', CONTENT_MODEL_JSON
],
78 [ 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT
],
83 * @dataProvider dataGetDefaultModelFor
84 * @covers ContentHandler::getDefaultModelFor
86 public function testGetDefaultModelFor( $title, $expectedModelId ) {
87 $title = Title
::newFromText( $title );
88 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
92 * @dataProvider dataGetDefaultModelFor
93 * @covers ContentHandler::getForTitle
95 public function testGetForTitle( $title, $expectedContentModel ) {
96 $title = Title
::newFromText( $title );
97 LinkCache
::singleton()->addBadLinkObj( $title );
98 $handler = ContentHandler
::getForTitle( $title );
99 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
102 public static function dataGetLocalizedName() {
107 // XXX: depends on content language
108 [ CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ],
113 * @dataProvider dataGetLocalizedName
114 * @covers ContentHandler::getLocalizedName
116 public function testGetLocalizedName( $id, $expected ) {
117 $name = ContentHandler
::getLocalizedName( $id );
120 $this->assertNotNull( $name, "no name found for content model $id" );
121 $this->assertTrue( preg_match( $expected, $name ) > 0,
122 "content model name for #$id did not match pattern $expected"
125 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
126 . "fallen back to use the model id directly."
131 public static function dataGetPageLanguage() {
132 global $wgLanguageCode;
135 [ "Main", $wgLanguageCode ],
136 [ "Dummy:Foo", $wgLanguageCode ],
137 [ "MediaWiki:common.js", 'en' ],
138 [ "User:Foo/common.js", 'en' ],
139 [ "MediaWiki:common.css", 'en' ],
140 [ "User:Foo/common.css", 'en' ],
141 [ "User:Foo", $wgLanguageCode ],
143 [ CONTENT_MODEL_JAVASCRIPT
, 'javascript' ],
148 * @dataProvider dataGetPageLanguage
149 * @covers ContentHandler::getPageLanguage
151 public function testGetPageLanguage( $title, $expected ) {
152 if ( is_string( $title ) ) {
153 $title = Title
::newFromText( $title );
154 LinkCache
::singleton()->addBadLinkObj( $title );
157 $expected = wfGetLangObj( $expected );
159 $handler = ContentHandler
::getForTitle( $title );
160 $lang = $handler->getPageLanguage( $title );
162 $this->assertEquals( $expected->getCode(), $lang->getCode() );
165 public static function dataGetContentText_Null() {
174 * @dataProvider dataGetContentText_Null
175 * @covers ContentHandler::getContentText
177 public function testGetContentText_Null( $contentHandlerTextFallback ) {
178 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
182 $text = ContentHandler
::getContentText( $content );
183 $this->assertEquals( '', $text );
186 public static function dataGetContentText_TextContent() {
195 * @dataProvider dataGetContentText_TextContent
196 * @covers ContentHandler::getContentText
198 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
199 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
201 $content = new WikitextContent( "hello world" );
203 $text = ContentHandler
::getContentText( $content );
204 $this->assertEquals( $content->getNativeData(), $text );
208 * ContentHandler::getContentText should have thrown an exception for non-text Content object
209 * @expectedException MWException
210 * @covers ContentHandler::getContentText
212 public function testGetContentText_NonTextContent_fail() {
213 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
215 $content = new DummyContentForTesting( "hello world" );
217 ContentHandler
::getContentText( $content );
221 * @covers ContentHandler::getContentText
223 public function testGetContentText_NonTextContent_serialize() {
224 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
226 $content = new DummyContentForTesting( "hello world" );
228 $text = ContentHandler
::getContentText( $content );
229 $this->assertEquals( $content->serialize(), $text );
233 * @covers ContentHandler::getContentText
235 public function testGetContentText_NonTextContent_ignore() {
236 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
238 $content = new DummyContentForTesting( "hello world" );
240 $text = ContentHandler
::getContentText( $content );
241 $this->assertNull( $text );
245 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
248 public static function dataMakeContent() {
250 [ 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ],
251 [ 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ],
252 [ serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ],
258 CONTENT_FORMAT_WIKITEXT
,
259 CONTENT_MODEL_WIKITEXT
,
267 CONTENT_FORMAT_JAVASCRIPT
,
268 CONTENT_MODEL_JAVASCRIPT
,
272 [ serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ],
274 [ 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ],
285 serialize( 'hallo' ),
290 serialize( 'hallo' ),
294 [ 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ],
295 [ 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ],
296 [ 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ],
301 * @dataProvider dataMakeContent
302 * @covers ContentHandler::makeContent
304 public function testMakeContent( $data, $title, $modelId, $format,
305 $expectedModelId, $expectedNativeData, $shouldFail
307 $title = Title
::newFromText( $title );
308 LinkCache
::singleton()->addBadLinkObj( $title );
310 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
313 $this->fail( "ContentHandler::makeContent should have failed!" );
316 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
317 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
318 } catch ( MWException
$ex ) {
319 if ( !$shouldFail ) {
320 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
322 // dummy, so we don't get the "test did not perform any assertions" message.
323 $this->assertTrue( true );
329 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
332 public function testGetAutosummary() {
333 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
335 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
336 $title = Title
::newFromText( 'Help:Test' );
337 // Create a new content object with no content
338 $newContent = ContentHandler
::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT
);
339 // first check, if we become a blank page created summary with the right bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
341 $this->assertEquals( $autoSummary, 'Created blank page' );
342 // now check, what we become with another bitmask
343 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
344 $this->assertEquals( $autoSummary, '' );
348 public function testSupportsSections() {
349 $this->markTestIncomplete( "not yet implemented" );
353 public function testSupportsDirectEditing() {
354 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
355 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
359 * @covers ContentHandler::runLegacyHooks
361 public function testRunLegacyHooks() {
362 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
364 $content = new WikitextContent( 'test text' );
365 $ok = ContentHandler
::runLegacyHooks(
366 'testRunLegacyHooks',
367 [ 'foo', &$content, 'bar' ],
371 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
372 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
375 public static function dummyHookHandler( $foo, &$text, $bar ) {
376 if ( $text === null ||
$text === false ) {
380 $text = strtoupper( $text );
385 public function provideGetModelForID() {
387 [ CONTENT_MODEL_WIKITEXT
, 'WikitextContentHandler' ],
388 [ CONTENT_MODEL_JAVASCRIPT
, 'JavaScriptContentHandler' ],
389 [ CONTENT_MODEL_JSON
, 'JsonContentHandler' ],
390 [ CONTENT_MODEL_CSS
, 'CssContentHandler' ],
391 [ CONTENT_MODEL_TEXT
, 'TextContentHandler' ],
392 [ 'testing', 'DummyContentHandlerForTesting' ],
393 [ 'testing-callbacks', 'DummyContentHandlerForTesting' ],
398 * @dataProvider provideGetModelForID
400 public function testGetModelForID( $modelId, $handlerClass ) {
401 $handler = ContentHandler
::getForModelID( $modelId );
403 $this->assertInstanceOf( $handlerClass, $handler );