MediaWiki
REL1_21
|
00001 <?php 00002 00011 class ContentHandlerTest extends MediaWikiTestCase { 00012 00013 public function setup() { 00014 global $wgContLang; 00015 parent::setup(); 00016 00017 $this->setMwGlobals( array( 00018 'wgExtraNamespaces' => array( 00019 12312 => 'Dummy', 00020 12313 => 'Dummy_talk', 00021 ), 00022 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..) 00023 // default to CONTENT_MODEL_WIKITEXT. 00024 'wgNamespaceContentModels' => array( 00025 12312 => 'testing', 00026 ), 00027 'wgContentHandlers' => array( 00028 CONTENT_MODEL_WIKITEXT => 'WikitextContentHandler', 00029 CONTENT_MODEL_JAVASCRIPT => 'JavaScriptContentHandler', 00030 CONTENT_MODEL_CSS => 'CssContentHandler', 00031 CONTENT_MODEL_TEXT => 'TextContentHandler', 00032 'testing' => 'DummyContentHandlerForTesting', 00033 ), 00034 ) ); 00035 00036 // Reset namespace cache 00037 MWNamespace::getCanonicalNamespaces( true ); 00038 $wgContLang->resetNamespaces(); 00039 } 00040 00041 public function tearDown() { 00042 global $wgContLang; 00043 00044 // Reset namespace cache 00045 MWNamespace::getCanonicalNamespaces( true ); 00046 $wgContLang->resetNamespaces(); 00047 00048 parent::tearDown(); 00049 } 00050 00051 public static function dataGetDefaultModelFor() { 00052 return array( 00053 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT ), 00054 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ), 00055 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ), 00056 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ), 00057 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ), 00058 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ), 00059 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ), 00060 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ), 00061 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ), 00062 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ), 00063 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ), 00064 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ), 00065 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ), 00066 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ), 00067 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ), 00068 ); 00069 } 00070 00074 public function testGetDefaultModelFor( $title, $expectedModelId ) { 00075 $title = Title::newFromText( $title ); 00076 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) ); 00077 } 00078 00082 public function testGetForTitle( $title, $expectedContentModel ) { 00083 $title = Title::newFromText( $title ); 00084 $handler = ContentHandler::getForTitle( $title ); 00085 $this->assertEquals( $expectedContentModel, $handler->getModelID() ); 00086 } 00087 00088 public static function dataGetLocalizedName() { 00089 return array( 00090 array( null, null ), 00091 array( "xyzzy", null ), 00092 00093 // XXX: depends on content language 00094 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ), 00095 ); 00096 } 00097 00101 public function testGetLocalizedName( $id, $expected ) { 00102 $name = ContentHandler::getLocalizedName( $id ); 00103 00104 if ( $expected ) { 00105 $this->assertNotNull( $name, "no name found for content model $id" ); 00106 $this->assertTrue( preg_match( $expected, $name ) > 0, 00107 "content model name for #$id did not match pattern $expected" 00108 ); 00109 } else { 00110 $this->assertEquals( $id, $name, "localization of unknown model $id should have " 00111 . "fallen back to use the model id directly." 00112 ); 00113 } 00114 } 00115 00116 public static function dataGetPageLanguage() { 00117 global $wgLanguageCode; 00118 00119 return array( 00120 array( "Main", $wgLanguageCode ), 00121 array( "Dummy:Foo", $wgLanguageCode ), 00122 array( "MediaWiki:common.js", 'en' ), 00123 array( "User:Foo/common.js", 'en' ), 00124 array( "MediaWiki:common.css", 'en' ), 00125 array( "User:Foo/common.css", 'en' ), 00126 array( "User:Foo", $wgLanguageCode ), 00127 00128 array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ), 00129 ); 00130 } 00131 00135 public function testGetPageLanguage( $title, $expected ) { 00136 if ( is_string( $title ) ) { 00137 $title = Title::newFromText( $title ); 00138 } 00139 00140 $expected = wfGetLangObj( $expected ); 00141 00142 $handler = ContentHandler::getForTitle( $title ); 00143 $lang = $handler->getPageLanguage( $title ); 00144 00145 $this->assertEquals( $expected->getCode(), $lang->getCode() ); 00146 } 00147 00148 public function testGetContentText_Null() { 00149 global $wgContentHandlerTextFallback; 00150 00151 $content = null; 00152 00153 $wgContentHandlerTextFallback = 'fail'; 00154 $text = ContentHandler::getContentText( $content ); 00155 $this->assertEquals( '', $text ); 00156 00157 $wgContentHandlerTextFallback = 'serialize'; 00158 $text = ContentHandler::getContentText( $content ); 00159 $this->assertEquals( '', $text ); 00160 00161 $wgContentHandlerTextFallback = 'ignore'; 00162 $text = ContentHandler::getContentText( $content ); 00163 $this->assertEquals( '', $text ); 00164 } 00165 00166 public function testGetContentText_TextContent() { 00167 global $wgContentHandlerTextFallback; 00168 00169 $content = new WikitextContent( "hello world" ); 00170 00171 $wgContentHandlerTextFallback = 'fail'; 00172 $text = ContentHandler::getContentText( $content ); 00173 $this->assertEquals( $content->getNativeData(), $text ); 00174 00175 $wgContentHandlerTextFallback = 'serialize'; 00176 $text = ContentHandler::getContentText( $content ); 00177 $this->assertEquals( $content->serialize(), $text ); 00178 00179 $wgContentHandlerTextFallback = 'ignore'; 00180 $text = ContentHandler::getContentText( $content ); 00181 $this->assertEquals( $content->getNativeData(), $text ); 00182 } 00183 00184 public function testGetContentText_NonTextContent() { 00185 global $wgContentHandlerTextFallback; 00186 00187 $content = new DummyContentForTesting( "hello world" ); 00188 00189 $wgContentHandlerTextFallback = 'fail'; 00190 00191 try { 00192 $text = ContentHandler::getContentText( $content ); 00193 00194 $this->fail( "ContentHandler::getContentText should have thrown an exception for non-text Content object" ); 00195 } catch ( MWException $ex ) { 00196 // as expected 00197 } 00198 00199 $wgContentHandlerTextFallback = 'serialize'; 00200 $text = ContentHandler::getContentText( $content ); 00201 $this->assertEquals( $content->serialize(), $text ); 00202 00203 $wgContentHandlerTextFallback = 'ignore'; 00204 $text = ContentHandler::getContentText( $content ); 00205 $this->assertNull( $text ); 00206 } 00207 00208 /* 00209 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {} 00210 */ 00211 00212 public static function dataMakeContent() { 00213 return array( 00214 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ), 00215 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ), 00216 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ), 00217 00218 array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ), 00219 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ), 00220 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ), 00221 00222 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ), 00223 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ), 00224 array( serialize( 'hallo' ), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize( 'hallo' ), false ), 00225 00226 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ), 00227 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ), 00228 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ), 00229 ); 00230 } 00231 00235 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) { 00236 $title = Title::newFromText( $title ); 00237 00238 try { 00239 $content = ContentHandler::makeContent( $data, $title, $modelId, $format ); 00240 00241 if ( $shouldFail ) { 00242 $this->fail( "ContentHandler::makeContent should have failed!" ); 00243 } 00244 00245 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' ); 00246 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' ); 00247 } catch ( MWException $ex ) { 00248 if ( !$shouldFail ) { 00249 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() ); 00250 } 00251 else { 00252 // dummy, so we don't get the "test did not perform any assertions" message. 00253 $this->assertTrue( true ); 00254 } 00255 } 00256 } 00257 00258 /* 00259 public function testSupportsSections() { 00260 $this->markTestIncomplete( "not yet implemented" ); 00261 } 00262 */ 00263 00264 public function testRunLegacyHooks() { 00265 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' ); 00266 00267 $content = new WikitextContent( 'test text' ); 00268 $ok = ContentHandler::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false ); 00269 00270 $this->assertTrue( $ok, "runLegacyHooks should have returned true" ); 00271 $this->assertEquals( "TEST TEXT", $content->getNativeData() ); 00272 } 00273 00274 public static function dummyHookHandler( $foo, &$text, $bar ) { 00275 if ( $text === null || $text === false ) { 00276 return false; 00277 } 00278 00279 $text = strtoupper( $text ); 00280 00281 return true; 00282 } 00283 } 00284 00285 class DummyContentHandlerForTesting extends ContentHandler { 00286 00287 public function __construct( $dataModel ) { 00288 parent::__construct( $dataModel, array( "testing" ) ); 00289 } 00290 00298 public function serializeContent( Content $content, $format = null ) { 00299 return $content->serialize(); 00300 } 00301 00309 public function unserializeContent( $blob, $format = null ) { 00310 $d = unserialize( $blob ); 00311 return new DummyContentForTesting( $d ); 00312 } 00313 00318 public function makeEmptyContent() { 00319 return new DummyContentForTesting( '' ); 00320 } 00321 } 00322 00323 class DummyContentForTesting extends AbstractContent { 00324 00325 public function __construct( $data ) { 00326 parent::__construct( "testing" ); 00327 00328 $this->data = $data; 00329 } 00330 00331 public function serialize( $format = null ) { 00332 return serialize( $this->data ); 00333 } 00334 00339 public function getTextForSearchIndex() { 00340 return ''; 00341 } 00342 00347 public function getWikitextForTransclusion() { 00348 return false; 00349 } 00350 00357 public function getTextForSummary( $maxlength = 250 ) { 00358 return ''; 00359 } 00360 00368 public function getNativeData() { 00369 return $this->data; 00370 } 00371 00377 public function getSize() { 00378 return strlen( $this->data ); 00379 } 00380 00395 public function copy() { 00396 return $this; 00397 } 00398 00407 public function isCountable( $hasLinks = null ) { 00408 return false; 00409 } 00410 00421 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) { 00422 return new ParserOutput( $this->getNativeData() ); 00423 } 00424 }