MediaWiki  REL1_24
ContentHandlerTest.php
Go to the documentation of this file.
00001 <?php
00002 
00011 class ContentHandlerTest extends MediaWikiTestCase {
00012 
00013     protected 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     protected 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(
00242                 'hallo',
00243                 'Help:Test',
00244                 null,
00245                 CONTENT_FORMAT_WIKITEXT,
00246                 CONTENT_MODEL_WIKITEXT,
00247                 'hallo',
00248                 false
00249             ),
00250             array(
00251                 'hallo',
00252                 'MediaWiki:Test.js',
00253                 null,
00254                 CONTENT_FORMAT_JAVASCRIPT,
00255                 CONTENT_MODEL_JAVASCRIPT,
00256                 'hallo',
00257                 false
00258             ),
00259             array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
00260 
00261             array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
00262             array(
00263                 'hallo',
00264                 'MediaWiki:Test.js',
00265                 CONTENT_MODEL_CSS,
00266                 null,
00267                 CONTENT_MODEL_CSS,
00268                 'hallo',
00269                 false
00270             ),
00271             array(
00272                 serialize( 'hallo' ),
00273                 'Dummy:Test',
00274                 CONTENT_MODEL_CSS,
00275                 null,
00276                 CONTENT_MODEL_CSS,
00277                 serialize( 'hallo' ),
00278                 false
00279             ),
00280 
00281             array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ),
00282             array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ),
00283             array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ),
00284         );
00285     }
00286 
00291     public function testMakeContent( $data, $title, $modelId, $format,
00292         $expectedModelId, $expectedNativeData, $shouldFail
00293     ) {
00294         $title = Title::newFromText( $title );
00295 
00296         try {
00297             $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
00298 
00299             if ( $shouldFail ) {
00300                 $this->fail( "ContentHandler::makeContent should have failed!" );
00301             }
00302 
00303             $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
00304             $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
00305         } catch ( MWException $ex ) {
00306             if ( !$shouldFail ) {
00307                 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
00308             } else {
00309                 // dummy, so we don't get the "test did not perform any assertions" message.
00310                 $this->assertTrue( true );
00311             }
00312         }
00313     }
00314 
00315     /*
00316      * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
00317      * page.
00318      */
00319     public function testGetAutosummary() {
00320         $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
00321         $title = Title::newFromText( 'Help:Test' );
00322         // Create a new content object with no content
00323         $newContent = ContentHandler::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT );
00324         // first check, if we become a blank page created summary with the right bitmask
00325         $autoSummary = $content->getAutosummary( null, $newContent, 97 );
00326         $this->assertEquals( $autoSummary, 'Created blank page' );
00327         // now check, what we become with another bitmask
00328         $autoSummary = $content->getAutosummary( null, $newContent, 92 );
00329         $this->assertEquals( $autoSummary, '' );
00330     }
00331 
00332     /*
00333     public function testSupportsSections() {
00334         $this->markTestIncomplete( "not yet implemented" );
00335     }
00336     */
00337 
00341     public function testRunLegacyHooks() {
00342         Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
00343 
00344         $content = new WikitextContent( 'test text' );
00345         $ok = ContentHandler::runLegacyHooks(
00346             'testRunLegacyHooks',
00347             array( 'foo', &$content, 'bar' ),
00348             false
00349         );
00350 
00351         $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
00352         $this->assertEquals( "TEST TEXT", $content->getNativeData() );
00353     }
00354 
00355     public static function dummyHookHandler( $foo, &$text, $bar ) {
00356         if ( $text === null || $text === false ) {
00357             return false;
00358         }
00359 
00360         $text = strtoupper( $text );
00361 
00362         return true;
00363     }
00364 }
00365 
00366 class DummyContentHandlerForTesting extends ContentHandler {
00367 
00368     public function __construct( $dataModel ) {
00369         parent::__construct( $dataModel, array( "testing" ) );
00370     }
00371 
00380     public function serializeContent( Content $content, $format = null ) {
00381         return $content->serialize();
00382     }
00383 
00392     public function unserializeContent( $blob, $format = null ) {
00393         $d = unserialize( $blob );
00394 
00395         return new DummyContentForTesting( $d );
00396     }
00397 
00402     public function makeEmptyContent() {
00403         return new DummyContentForTesting( '' );
00404     }
00405 }
00406 
00407 class DummyContentForTesting extends AbstractContent {
00408 
00409     public function __construct( $data ) {
00410         parent::__construct( "testing" );
00411 
00412         $this->data = $data;
00413     }
00414 
00415     public function serialize( $format = null ) {
00416         return serialize( $this->data );
00417     }
00418 
00424     public function getTextForSearchIndex() {
00425         return '';
00426     }
00427 
00432     public function getWikitextForTransclusion() {
00433         return false;
00434     }
00435 
00443     public function getTextForSummary( $maxlength = 250 ) {
00444         return '';
00445     }
00446 
00454     public function getNativeData() {
00455         return $this->data;
00456     }
00457 
00463     public function getSize() {
00464         return strlen( $this->data );
00465     }
00466 
00481     public function copy() {
00482         return $this;
00483     }
00484 
00493     public function isCountable( $hasLinks = null ) {
00494         return false;
00495     }
00496 
00506     public function getParserOutput( Title $title, $revId = null,
00507         ParserOptions $options = null, $generateHtml = true
00508     ) {
00509         return new ParserOutput( $this->getNativeData() );
00510     }
00511 
00521     protected function fillParserOutput( Title $title, $revId,
00522             ParserOptions $options, $generateHtml, ParserOutput &$output ) {
00523         $output = new ParserOutput( $this->getNativeData() );
00524     }
00525 }