MediaWiki
REL1_22
|
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 00075 public function testGetDefaultModelFor( $title, $expectedModelId ) { 00076 $title = Title::newFromText( $title ); 00077 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) ); 00078 } 00079 00084 public function testGetForTitle( $title, $expectedContentModel ) { 00085 $title = Title::newFromText( $title ); 00086 $handler = ContentHandler::getForTitle( $title ); 00087 $this->assertEquals( $expectedContentModel, $handler->getModelID() ); 00088 } 00089 00090 public static function dataGetLocalizedName() { 00091 return array( 00092 array( null, null ), 00093 array( "xyzzy", null ), 00094 00095 // XXX: depends on content language 00096 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ), 00097 ); 00098 } 00099 00104 public function testGetLocalizedName( $id, $expected ) { 00105 $name = ContentHandler::getLocalizedName( $id ); 00106 00107 if ( $expected ) { 00108 $this->assertNotNull( $name, "no name found for content model $id" ); 00109 $this->assertTrue( preg_match( $expected, $name ) > 0, 00110 "content model name for #$id did not match pattern $expected" 00111 ); 00112 } else { 00113 $this->assertEquals( $id, $name, "localization of unknown model $id should have " 00114 . "fallen back to use the model id directly." 00115 ); 00116 } 00117 } 00118 00119 public static function dataGetPageLanguage() { 00120 global $wgLanguageCode; 00121 00122 return array( 00123 array( "Main", $wgLanguageCode ), 00124 array( "Dummy:Foo", $wgLanguageCode ), 00125 array( "MediaWiki:common.js", 'en' ), 00126 array( "User:Foo/common.js", 'en' ), 00127 array( "MediaWiki:common.css", 'en' ), 00128 array( "User:Foo/common.css", 'en' ), 00129 array( "User:Foo", $wgLanguageCode ), 00130 00131 array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ), 00132 ); 00133 } 00134 00139 public function testGetPageLanguage( $title, $expected ) { 00140 if ( is_string( $title ) ) { 00141 $title = Title::newFromText( $title ); 00142 } 00143 00144 $expected = wfGetLangObj( $expected ); 00145 00146 $handler = ContentHandler::getForTitle( $title ); 00147 $lang = $handler->getPageLanguage( $title ); 00148 00149 $this->assertEquals( $expected->getCode(), $lang->getCode() ); 00150 } 00151 00152 public static function dataGetContentText_Null() { 00153 return array( 00154 array( 'fail' ), 00155 array( 'serialize' ), 00156 array( 'ignore' ), 00157 ); 00158 } 00159 00164 public function testGetContentText_Null( $contentHandlerTextFallback ) { 00165 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback ); 00166 00167 $content = null; 00168 00169 $text = ContentHandler::getContentText( $content ); 00170 $this->assertEquals( '', $text ); 00171 } 00172 00173 public static function dataGetContentText_TextContent() { 00174 return array( 00175 array( 'fail' ), 00176 array( 'serialize' ), 00177 array( 'ignore' ), 00178 ); 00179 } 00180 00185 public function testGetContentText_TextContent( $contentHandlerTextFallback ) { 00186 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback ); 00187 00188 $content = new WikitextContent( "hello world" ); 00189 00190 $text = ContentHandler::getContentText( $content ); 00191 $this->assertEquals( $content->getNativeData(), $text ); 00192 } 00193 00199 public function testGetContentText_NonTextContent_fail() { 00200 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' ); 00201 00202 $content = new DummyContentForTesting( "hello world" ); 00203 00204 ContentHandler::getContentText( $content ); 00205 } 00206 00210 public function testGetContentText_NonTextContent_serialize() { 00211 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' ); 00212 00213 $content = new DummyContentForTesting( "hello world" ); 00214 00215 $text = ContentHandler::getContentText( $content ); 00216 $this->assertEquals( $content->serialize(), $text ); 00217 } 00218 00222 public function testGetContentText_NonTextContent_ignore() { 00223 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' ); 00224 00225 $content = new DummyContentForTesting( "hello world" ); 00226 00227 $text = ContentHandler::getContentText( $content ); 00228 $this->assertNull( $text ); 00229 } 00230 00231 /* 00232 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {} 00233 */ 00234 00235 public static function dataMakeContent() { 00236 return array( 00237 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ), 00238 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ), 00239 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ), 00240 00241 array( 'hallo', 'Help:Test', null, CONTENT_FORMAT_WIKITEXT, CONTENT_MODEL_WIKITEXT, 'hallo', false ), 00242 array( 'hallo', 'MediaWiki:Test.js', null, CONTENT_FORMAT_JAVASCRIPT, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ), 00243 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ), 00244 00245 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ), 00246 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ), 00247 array( serialize( 'hallo' ), 'Dummy:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, serialize( 'hallo' ), false ), 00248 00249 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ), 00250 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ), 00251 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ), 00252 ); 00253 } 00254 00259 public function testMakeContent( $data, $title, $modelId, $format, $expectedModelId, $expectedNativeData, $shouldFail ) { 00260 $title = Title::newFromText( $title ); 00261 00262 try { 00263 $content = ContentHandler::makeContent( $data, $title, $modelId, $format ); 00264 00265 if ( $shouldFail ) { 00266 $this->fail( "ContentHandler::makeContent should have failed!" ); 00267 } 00268 00269 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' ); 00270 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' ); 00271 } catch ( MWException $ex ) { 00272 if ( !$shouldFail ) { 00273 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() ); 00274 } else { 00275 // dummy, so we don't get the "test did not perform any assertions" message. 00276 $this->assertTrue( true ); 00277 } 00278 } 00279 } 00280 00281 /* 00282 public function testSupportsSections() { 00283 $this->markTestIncomplete( "not yet implemented" ); 00284 } 00285 */ 00286 00290 public function testRunLegacyHooks() { 00291 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' ); 00292 00293 $content = new WikitextContent( 'test text' ); 00294 $ok = ContentHandler::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false ); 00295 00296 $this->assertTrue( $ok, "runLegacyHooks should have returned true" ); 00297 $this->assertEquals( "TEST TEXT", $content->getNativeData() ); 00298 } 00299 00300 public static function dummyHookHandler( $foo, &$text, $bar ) { 00301 if ( $text === null || $text === false ) { 00302 return false; 00303 } 00304 00305 $text = strtoupper( $text ); 00306 00307 return true; 00308 } 00309 } 00310 00311 class DummyContentHandlerForTesting extends ContentHandler { 00312 00313 public function __construct( $dataModel ) { 00314 parent::__construct( $dataModel, array( "testing" ) ); 00315 } 00316 00324 public function serializeContent( Content $content, $format = null ) { 00325 return $content->serialize(); 00326 } 00327 00335 public function unserializeContent( $blob, $format = null ) { 00336 $d = unserialize( $blob ); 00337 00338 return new DummyContentForTesting( $d ); 00339 } 00340 00345 public function makeEmptyContent() { 00346 return new DummyContentForTesting( '' ); 00347 } 00348 } 00349 00350 class DummyContentForTesting extends AbstractContent { 00351 00352 public function __construct( $data ) { 00353 parent::__construct( "testing" ); 00354 00355 $this->data = $data; 00356 } 00357 00358 public function serialize( $format = null ) { 00359 return serialize( $this->data ); 00360 } 00361 00366 public function getTextForSearchIndex() { 00367 return ''; 00368 } 00369 00374 public function getWikitextForTransclusion() { 00375 return false; 00376 } 00377 00384 public function getTextForSummary( $maxlength = 250 ) { 00385 return ''; 00386 } 00387 00395 public function getNativeData() { 00396 return $this->data; 00397 } 00398 00404 public function getSize() { 00405 return strlen( $this->data ); 00406 } 00407 00422 public function copy() { 00423 return $this; 00424 } 00425 00434 public function isCountable( $hasLinks = null ) { 00435 return false; 00436 } 00437 00448 public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) { 00449 return new ParserOutput( $this->getNativeData() ); 00450 } 00451 }