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_CSS
=> 'CssContentHandler',
26 CONTENT_MODEL_TEXT
=> 'TextContentHandler',
27 'testing' => 'DummyContentHandlerForTesting',
31 // Reset namespace cache
32 MWNamespace
::getCanonicalNamespaces( true );
33 $wgContLang->resetNamespaces();
35 LinkCache
::destroySingleton();
38 protected function tearDown() {
41 // Reset namespace cache
42 MWNamespace
::getCanonicalNamespaces( true );
43 $wgContLang->resetNamespaces();
45 LinkCache
::destroySingleton();
50 public static function dataGetDefaultModelFor() {
52 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT
),
53 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT
),
54 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT
),
55 array( 'User:Foo', CONTENT_MODEL_WIKITEXT
),
56 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT
),
57 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT
),
58 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS
),
59 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT
),
60 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT
),
61 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT
),
62 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT
),
63 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS
),
64 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT
),
65 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT
),
66 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT
),
71 * @dataProvider dataGetDefaultModelFor
72 * @covers ContentHandler::getDefaultModelFor
74 public function testGetDefaultModelFor( $title, $expectedModelId ) {
75 $title = Title
::newFromText( $title );
76 $this->assertEquals( $expectedModelId, ContentHandler
::getDefaultModelFor( $title ) );
80 * @dataProvider dataGetDefaultModelFor
81 * @covers ContentHandler::getForTitle
83 public function testGetForTitle( $title, $expectedContentModel ) {
84 $title = Title
::newFromText( $title );
85 LinkCache
::singleton()->addBadLinkObj( $title );
86 $handler = ContentHandler
::getForTitle( $title );
87 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
90 public static function dataGetLocalizedName() {
93 array( "xyzzy", null ),
95 // XXX: depends on content language
96 array( CONTENT_MODEL_JAVASCRIPT
, '/javascript/i' ),
101 * @dataProvider dataGetLocalizedName
102 * @covers ContentHandler::getLocalizedName
104 public function testGetLocalizedName( $id, $expected ) {
105 $name = ContentHandler
::getLocalizedName( $id );
108 $this->assertNotNull( $name, "no name found for content model $id" );
109 $this->assertTrue( preg_match( $expected, $name ) > 0,
110 "content model name for #$id did not match pattern $expected"
113 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
114 . "fallen back to use the model id directly."
119 public static function dataGetPageLanguage() {
120 global $wgLanguageCode;
123 array( "Main", $wgLanguageCode ),
124 array( "Dummy:Foo", $wgLanguageCode ),
125 array( "MediaWiki:common.js", 'en' ),
126 array( "User:Foo/common.js", 'en' ),
127 array( "MediaWiki:common.css", 'en' ),
128 array( "User:Foo/common.css", 'en' ),
129 array( "User:Foo", $wgLanguageCode ),
131 array( CONTENT_MODEL_JAVASCRIPT
, 'javascript' ),
136 * @dataProvider dataGetPageLanguage
137 * @covers ContentHandler::getPageLanguage
139 public function testGetPageLanguage( $title, $expected ) {
140 if ( is_string( $title ) ) {
141 $title = Title
::newFromText( $title );
142 LinkCache
::singleton()->addBadLinkObj( $title );
145 $expected = wfGetLangObj( $expected );
147 $handler = ContentHandler
::getForTitle( $title );
148 $lang = $handler->getPageLanguage( $title );
150 $this->assertEquals( $expected->getCode(), $lang->getCode() );
153 public static function dataGetContentText_Null() {
156 array( 'serialize' ),
162 * @dataProvider dataGetContentText_Null
163 * @covers ContentHandler::getContentText
165 public function testGetContentText_Null( $contentHandlerTextFallback ) {
166 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
170 $text = ContentHandler
::getContentText( $content );
171 $this->assertEquals( '', $text );
174 public static function dataGetContentText_TextContent() {
177 array( 'serialize' ),
183 * @dataProvider dataGetContentText_TextContent
184 * @covers ContentHandler::getContentText
186 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
187 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
189 $content = new WikitextContent( "hello world" );
191 $text = ContentHandler
::getContentText( $content );
192 $this->assertEquals( $content->getNativeData(), $text );
196 * ContentHandler::getContentText should have thrown an exception for non-text Content object
197 * @expectedException MWException
198 * @covers ContentHandler::getContentText
200 public function testGetContentText_NonTextContent_fail() {
201 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
203 $content = new DummyContentForTesting( "hello world" );
205 ContentHandler
::getContentText( $content );
209 * @covers ContentHandler::getContentText
211 public function testGetContentText_NonTextContent_serialize() {
212 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
214 $content = new DummyContentForTesting( "hello world" );
216 $text = ContentHandler
::getContentText( $content );
217 $this->assertEquals( $content->serialize(), $text );
221 * @covers ContentHandler::getContentText
223 public function testGetContentText_NonTextContent_ignore() {
224 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
226 $content = new DummyContentForTesting( "hello world" );
228 $text = ContentHandler
::getContentText( $content );
229 $this->assertNull( $text );
233 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
236 public static function dataMakeContent() {
238 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT
, 'hallo', false ),
239 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT
, 'hallo', false ),
240 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
246 CONTENT_FORMAT_WIKITEXT
,
247 CONTENT_MODEL_WIKITEXT
,
255 CONTENT_FORMAT_JAVASCRIPT
,
256 CONTENT_MODEL_JAVASCRIPT
,
260 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
262 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS
, null, CONTENT_MODEL_CSS
, 'hallo', false ),
273 serialize( 'hallo' ),
278 serialize( 'hallo' ),
282 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT
, "testing", null, null, true ),
283 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS
, "testing", null, null, true ),
284 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT
, "testing", null, null, true ),
289 * @dataProvider dataMakeContent
290 * @covers ContentHandler::makeContent
292 public function testMakeContent( $data, $title, $modelId, $format,
293 $expectedModelId, $expectedNativeData, $shouldFail
295 $title = Title
::newFromText( $title );
296 LinkCache
::singleton()->addBadLinkObj( $title );
298 $content = ContentHandler
::makeContent( $data, $title, $modelId, $format );
301 $this->fail( "ContentHandler::makeContent should have failed!" );
304 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
305 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
306 } catch ( MWException
$ex ) {
307 if ( !$shouldFail ) {
308 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
310 // dummy, so we don't get the "test did not perform any assertions" message.
311 $this->assertTrue( true );
317 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
320 public function testGetAutosummary() {
321 $this->setMwGlobals( 'wgContLang', Language
::factory( 'en' ) );
323 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT
);
324 $title = Title
::newFromText( 'Help:Test' );
325 // Create a new content object with no content
326 $newContent = ContentHandler
::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT
);
327 // first check, if we become a blank page created summary with the right bitmask
328 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
329 $this->assertEquals( $autoSummary, 'Created blank page' );
330 // now check, what we become with another bitmask
331 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
332 $this->assertEquals( $autoSummary, '' );
336 public function testSupportsSections() {
337 $this->markTestIncomplete( "not yet implemented" );
341 public function testSupportsDirectEditing() {
342 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON
);
343 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
347 * @covers ContentHandler::runLegacyHooks
349 public function testRunLegacyHooks() {
350 Hooks
::register( 'testRunLegacyHooks', __CLASS__
. '::dummyHookHandler' );
352 $content = new WikitextContent( 'test text' );
353 $ok = ContentHandler
::runLegacyHooks(
354 'testRunLegacyHooks',
355 array( 'foo', &$content, 'bar' ),
359 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
360 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
363 public static function dummyHookHandler( $foo, &$text, $bar ) {
364 if ( $text === null ||
$text === false ) {
368 $text = strtoupper( $text );