4 * @group ContentHandler
6 class ContentHandlerTest
extends MediaWikiTestCase
{
8 protected function setUp() {
12 $this->setMwGlobals( array(
13 'wgExtraNamespaces' => array(
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' => array(
22 'wgContentHandlers' => array(
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',
32 // Reset namespace cache
33 MWNamespace
::getCanonicalNamespaces( true );
34 $wgContLang->resetNamespaces();
36 LinkCache
::destroySingleton();
39 protected function tearDown() {
42 // Reset namespace cache
43 MWNamespace
::getCanonicalNamespaces( true );
44 $wgContLang->resetNamespaces();
46 LinkCache
::destroySingleton();
51 public static function dataGetDefaultModelFor() {
53 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT
),
54 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
),
55 array( 'Help:Foo.css', CONTENT_MODEL_WIKITEXT
),
56 array( 'Help:Foo.json', CONTENT_MODEL_WIKITEXT
),
57 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
58 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
59 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
60 array( 'User:Foo.css', CONTENT_MODEL_WIKITEXT
),
61 array( 'User:Foo.json', CONTENT_MODEL_WIKITEXT
),
62 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
63 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
64 array( 'User:Foo/bar.json', CONTENT_MODEL_JSON
),
65 array( 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT
),
66 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
67 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
68 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
69 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
70 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
71 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
72 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
73 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
74 array( 'MediaWiki:Foo.json', CONTENT_MODEL_JSON
),
75 array( 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT
),
80 * @dataProvider dataGetDefaultModelFor
81 * @covers ContentHandler::getDefaultModelFor
83 public function testGetDefaultModelFor( $title, $expectedModelId ) {
84 $title = Title
::newFromText( $title );
85 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
89 * @dataProvider dataGetDefaultModelFor
90 * @covers ContentHandler::getForTitle
92 public function testGetForTitle( $title, $expectedContentModel ) {
93 $title = Title
::newFromText( $title );
94 LinkCache
::singleton()->addBadLinkObj( $title );
95 $handler = ContentHandler
::getForTitle( $title );
96 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
99 public static function dataGetLocalizedName() {
102 array( "xyzzy", null ),
104 // XXX: depends on content language
105 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ),
110 * @dataProvider dataGetLocalizedName
111 * @covers ContentHandler::getLocalizedName
113 public function testGetLocalizedName( $id, $expected ) {
114 $name = ContentHandler
::getLocalizedName( $id );
117 $this->assertNotNull( $name, "no name found for content model $id" );
118 $this->assertTrue( preg_match( $expected, $name ) > 0,
119 "content model name for #$id did not match pattern $expected"
122 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
123 . "fallen back to use the model id directly."
128 public static function dataGetPageLanguage() {
129 global $wgLanguageCode;
132 array( "Main", $wgLanguageCode ),
133 array( "Dummy:Foo", $wgLanguageCode ),
134 array( "MediaWiki:common.js", 'en' ),
135 array( "User:Foo/common.js", 'en' ),
136 array( "MediaWiki:common.css", 'en' ),
137 array( "User:Foo/common.css", 'en' ),
138 array( "User:Foo", $wgLanguageCode ),
140 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
145 * @dataProvider dataGetPageLanguage
146 * @covers ContentHandler::getPageLanguage
148 public function testGetPageLanguage( $title, $expected ) {
149 if ( is_string( $title ) ) {
150 $title = Title
::newFromText( $title );
151 LinkCache
::singleton()->addBadLinkObj( $title );
154 $expected = wfGetLangObj( $expected );
156 $handler = ContentHandler
::getForTitle( $title );
157 $lang = $handler->getPageLanguage( $title );
159 $this->assertEquals( $expected->getCode(), $lang->getCode() );
162 public static function dataGetContentText_Null() {
165 array( 'serialize' ),
171 * @dataProvider dataGetContentText_Null
172 * @covers ContentHandler::getContentText
174 public function testGetContentText_Null( $contentHandlerTextFallback ) {
175 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
179 $text = ContentHandler
::getContentText( $content );
180 $this->assertEquals( '', $text );
183 public static function dataGetContentText_TextContent() {
186 array( 'serialize' ),
192 * @dataProvider dataGetContentText_TextContent
193 * @covers ContentHandler::getContentText
195 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
196 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
198 $content = new WikitextContent( "hello world" );
200 $text = ContentHandler
::getContentText( $content );
201 $this->assertEquals( $content->getNativeData(), $text );
205 * ContentHandler::getContentText should have thrown an exception for non-text Content object
206 * @expectedException MWException
207 * @covers ContentHandler::getContentText
209 public function testGetContentText_NonTextContent_fail() {
210 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
212 $content = new DummyContentForTesting( "hello world" );
214 ContentHandler
::getContentText( $content );
218 * @covers ContentHandler::getContentText
220 public function testGetContentText_NonTextContent_serialize() {
221 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
223 $content = new DummyContentForTesting( "hello world" );
225 $text = ContentHandler
::getContentText( $content );
226 $this->assertEquals( $content->serialize(), $text );
230 * @covers ContentHandler::getContentText
232 public function testGetContentText_NonTextContent_ignore() {
233 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
235 $content = new DummyContentForTesting( "hello world" );
237 $text = ContentHandler
::getContentText( $content );
238 $this->assertNull( $text );
242 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
245 public static function dataMakeContent() {
247 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
248 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
249 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
255 CONTENT_FORMAT_WIKITEXT
,
256 CONTENT_MODEL_WIKITEXT
,
264 CONTENT_FORMAT_JAVASCRIPT
,
265 CONTENT_MODEL_JAVASCRIPT
,
269 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
271 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
282 serialize( 'hallo' ),
287 serialize( 'hallo' ),
291 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
292 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
293 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
298 * @dataProvider dataMakeContent
299 * @covers ContentHandler::makeContent
301 public function testMakeContent( $data, $title, $modelId, $format,
302 $expectedModelId, $expectedNativeData, $shouldFail
304 $title = Title
::newFromText( $title );
305 LinkCache
::singleton()->addBadLinkObj( $title );
307 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
310 $this->fail( "ContentHandler::makeContent should have failed!" );
313 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
314 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
315 } catch ( MWException
$ex ) {
316 if ( !$shouldFail ) {
317 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
319 // dummy, so we don't get the "test did not perform any assertions" message.
320 $this->assertTrue( true );
326 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
329 public function testGetAutosummary() {
330 $this->setMwGlobals( 'wgContLang', Language
::factory( '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, null, null, CONTENT_MODEL_WIKITEXT
);
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, 'Created blank page' );
339 // now check, what we become with another bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
341 $this->assertEquals( $autoSummary, '' );
345 public function testSupportsSections() {
346 $this->markTestIncomplete( "not yet implemented" );
350 public function testSupportsDirectEditing() {
351 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
352 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
356 * @covers ContentHandler::runLegacyHooks
358 public function testRunLegacyHooks() {
359 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
361 $content = new WikitextContent( 'test text' );
362 $ok = ContentHandler
::runLegacyHooks(
363 'testRunLegacyHooks',
364 array( 'foo', &$content, 'bar' ),
368 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
369 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
372 public static function dummyHookHandler( $foo, &$text, $bar ) {
373 if ( $text === null ||
$text === false ) {
377 $text = strtoupper( $text );