MediaWiki  master
ContentHandlerTest.php
Go to the documentation of this file.
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                 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContentHandlers;
00237 
00238                 $title = Title::newFromText( $title );
00239 
00240                 try {
00241                         $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
00242 
00243                         if ( $shouldFail ) {
00244                                 $this->fail( "ContentHandler::makeContent should have failed!" );
00245                         }
00246 
00247                         $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
00248                         $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
00249                 } catch ( MWException $ex ) {
00250                         if ( !$shouldFail ) $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
00251                         else $this->assertTrue( true ); // dummy, so we don't get the "test did not perform any assertions" message.
00252                 }
00253 
00254         }
00255 
00256         /*
00257         public function testSupportsSections() {
00258                 $this->markTestIncomplete( "not yet implemented" );
00259         }
00260         */
00261 
00262         public function testRunLegacyHooks() {
00263                 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
00264 
00265                 $content = new WikitextContent( 'test text' );
00266                 $ok = ContentHandler::runLegacyHooks( 'testRunLegacyHooks', array( 'foo', &$content, 'bar' ), false );
00267 
00268                 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
00269                 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
00270         }
00271 
00272         public static function dummyHookHandler( $foo, &$text, $bar ) {
00273                 if ( $text === null || $text === false ) {
00274                         return false;
00275                 }
00276 
00277                 $text = strtoupper( $text );
00278 
00279                 return true;
00280         }
00281 }
00282 
00283 class DummyContentHandlerForTesting extends ContentHandler {
00284 
00285         public function __construct( $dataModel ) {
00286                 parent::__construct( $dataModel, array( "testing" ) );
00287         }
00288 
00296         public function serializeContent( Content $content, $format = null ) {
00297            return $content->serialize();
00298         }
00299 
00307         public function unserializeContent( $blob, $format = null ) {
00308                 $d = unserialize( $blob );
00309                 return new DummyContentForTesting( $d );
00310         }
00311 
00316         public function makeEmptyContent() {
00317                 return new DummyContentForTesting( '' );
00318         }
00319 }
00320 
00321 class DummyContentForTesting extends AbstractContent {
00322 
00323         public function __construct( $data ) {
00324                 parent::__construct( "testing" );
00325 
00326                 $this->data = $data;
00327         }
00328 
00329         public function serialize( $format = null ) {
00330                 return serialize( $this->data );
00331         }
00332 
00337         public function getTextForSearchIndex() {
00338                 return '';
00339         }
00340 
00345         public function getWikitextForTransclusion() {
00346                 return false;
00347         }
00348 
00355         public function getTextForSummary( $maxlength = 250 ) {
00356                 return '';
00357         }
00358 
00366         public function getNativeData()
00367         {
00368                 return $this->data;
00369         }
00370 
00376         public function getSize() {
00377                 return strlen( $this->data );
00378         }
00379 
00394         public function copy() {
00395                 return $this;
00396         }
00397 
00406         public function isCountable( $hasLinks = null ) {
00407                 return false;
00408         }
00409 
00420         public function getParserOutput( Title $title, $revId = null, ParserOptions $options = NULL, $generateHtml = true ) {
00421                 return new ParserOutput( $this->getNativeData() );
00422         }
00423 }