MediaWiki  REL1_22
TextContentTest.php
Go to the documentation of this file.
00001 <?php
00002 
00008 class TextContentTest extends MediaWikiLangTestCase {
00009     protected $context;
00010 
00011     protected function setUp() {
00012         parent::setUp();
00013 
00014         // Anon user
00015         $user = new User();
00016         $user->setName( '127.0.0.1' );
00017 
00018         $this->setMwGlobals( array(
00019             'wgUser' => $user,
00020             'wgTextModelsToParse' => array(
00021                 CONTENT_MODEL_WIKITEXT,
00022                 CONTENT_MODEL_CSS,
00023                 CONTENT_MODEL_JAVASCRIPT,
00024             ),
00025             'wgUseTidy' => false,
00026             'wgAlwaysUseTidy' => false,
00027         ) );
00028 
00029         $this->context = new RequestContext( new FauxRequest() );
00030         $this->context->setTitle( Title::newFromText( 'Test' ) );
00031         $this->context->setUser( $user );
00032     }
00033 
00034     public function newContent( $text ) {
00035         return new TextContent( $text );
00036     }
00037 
00038     public static function dataGetParserOutput() {
00039         return array(
00040             array(
00041                 'TextContentTest_testGetParserOutput',
00042                 CONTENT_MODEL_TEXT,
00043                 "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
00044                 array(
00045                     'Links' => array()
00046                 )
00047             ),
00048             // TODO: more...?
00049         );
00050     }
00051 
00056     public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
00057         $title = Title::newFromText( $title );
00058         $content = ContentHandler::makeContent( $text, $title, $model );
00059 
00060         $po = $content->getParserOutput( $title );
00061 
00062         $html = $po->getText();
00063         $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
00064 
00065         $this->assertEquals( $expectedHtml, trim( $html ) );
00066 
00067         if ( $expectedFields ) {
00068             foreach ( $expectedFields as $field => $exp ) {
00069                 $f = 'get' . ucfirst( $field );
00070                 $v = call_user_func( array( $po, $f ) );
00071 
00072                 if ( is_array( $exp ) ) {
00073                     $this->assertArrayEquals( $exp, $v );
00074                 } else {
00075                     $this->assertEquals( $exp, $v );
00076                 }
00077             }
00078         }
00079 
00080         // TODO: assert more properties
00081     }
00082 
00083     public static function dataPreSaveTransform() {
00084         return array(
00085             array(
00086                 #0: no signature resolution
00087                 'hello this is ~~~',
00088                 'hello this is ~~~',
00089             ),
00090             array(
00091                 #1: rtrim
00092                 " Foo \n ",
00093                 ' Foo',
00094             ),
00095         );
00096     }
00097 
00102     public function testPreSaveTransform( $text, $expected ) {
00103         global $wgContLang;
00104 
00105         $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
00106 
00107         $content = $this->newContent( $text );
00108         $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
00109 
00110         $this->assertEquals( $expected, $content->getNativeData() );
00111     }
00112 
00113     public static function dataPreloadTransform() {
00114         return array(
00115             array(
00116                 'hello this is ~~~',
00117                 'hello this is ~~~',
00118             ),
00119         );
00120     }
00121 
00126     public function testPreloadTransform( $text, $expected ) {
00127         global $wgContLang;
00128         $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
00129 
00130         $content = $this->newContent( $text );
00131         $content = $content->preloadTransform( $this->context->getTitle(), $options );
00132 
00133         $this->assertEquals( $expected, $content->getNativeData() );
00134     }
00135 
00136     public static function dataGetRedirectTarget() {
00137         return array(
00138             array( '#REDIRECT [[Test]]',
00139                 null,
00140             ),
00141         );
00142     }
00143 
00148     public function testGetRedirectTarget( $text, $expected ) {
00149         $content = $this->newContent( $text );
00150         $t = $content->getRedirectTarget();
00151 
00152         if ( is_null( $expected ) ) {
00153             $this->assertNull( $t, "text should not have generated a redirect target: $text" );
00154         } else {
00155             $this->assertEquals( $expected, $t->getPrefixedText() );
00156         }
00157     }
00158 
00163     public function testIsRedirect( $text, $expected ) {
00164         $content = $this->newContent( $text );
00165 
00166         $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
00167     }
00168 
00172     /*
00173     public function getRedirectChain() {
00174         $text = $this->getNativeData();
00175         return Title::newFromRedirectArray( $text );
00176     }
00177     */
00178 
00182     /*
00183     public function getUltimateRedirectTarget() {
00184         $text = $this->getNativeData();
00185         return Title::newFromRedirectRecurse( $text );
00186     }
00187     */
00188 
00189     public static function dataIsCountable() {
00190         return array(
00191             array( '',
00192                 null,
00193                 'any',
00194                 true
00195             ),
00196             array( 'Foo',
00197                 null,
00198                 'any',
00199                 true
00200             ),
00201             array( 'Foo',
00202                 null,
00203                 'comma',
00204                 false
00205             ),
00206             array( 'Foo, bar',
00207                 null,
00208                 'comma',
00209                 false
00210             ),
00211         );
00212     }
00213 
00219     public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
00220         $this->setMwGlobals( 'wgArticleCountMethod', $mode );
00221 
00222         $content = $this->newContent( $text );
00223 
00224         $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
00225 
00226         $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
00227             . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
00228     }
00229 
00230     public static function dataGetTextForSummary() {
00231         return array(
00232             array( "hello\nworld.",
00233                 16,
00234                 'hello world.',
00235             ),
00236             array( 'hello world.',
00237                 8,
00238                 'hello...',
00239             ),
00240             array( '[[hello world]].',
00241                 8,
00242                 '[[hel...',
00243             ),
00244         );
00245     }
00246 
00251     public function testGetTextForSummary( $text, $maxlength, $expected ) {
00252         $content = $this->newContent( $text );
00253 
00254         $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
00255     }
00256 
00260     public function testGetTextForSearchIndex() {
00261         $content = $this->newContent( 'hello world.' );
00262 
00263         $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
00264     }
00265 
00269     public function testCopy() {
00270         $content = $this->newContent( 'hello world.' );
00271         $copy = $content->copy();
00272 
00273         $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
00274         $this->assertEquals( 'hello world.', $copy->getNativeData() );
00275     }
00276 
00280     public function testGetSize() {
00281         $content = $this->newContent( 'hello world.' );
00282 
00283         $this->assertEquals( 12, $content->getSize() );
00284     }
00285 
00289     public function testGetNativeData() {
00290         $content = $this->newContent( 'hello world.' );
00291 
00292         $this->assertEquals( 'hello world.', $content->getNativeData() );
00293     }
00294 
00298     public function testGetWikitextForTransclusion() {
00299         $content = $this->newContent( 'hello world.' );
00300 
00301         $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
00302     }
00303 
00307     public function testGetModel() {
00308         $content = $this->newContent( "hello world." );
00309 
00310         $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
00311     }
00312 
00316     public function testGetContentHandler() {
00317         $content = $this->newContent( "hello world." );
00318 
00319         $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
00320     }
00321 
00322     public static function dataIsEmpty() {
00323         return array(
00324             array( '', true ),
00325             array( '  ', false ),
00326             array( '0', false ),
00327             array( 'hallo welt.', false ),
00328         );
00329     }
00330 
00335     public function testIsEmpty( $text, $empty ) {
00336         $content = $this->newContent( $text );
00337 
00338         $this->assertEquals( $empty, $content->isEmpty() );
00339     }
00340 
00341     public static function dataEquals() {
00342         return array(
00343             array( new TextContent( "hallo" ), null, false ),
00344             array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
00345             array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
00346             array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
00347             array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
00348         );
00349     }
00350 
00355     public function testEquals( Content $a, Content $b = null, $equal = false ) {
00356         $this->assertEquals( $equal, $a->equals( $b ) );
00357     }
00358 
00359     public static function dataGetDeletionUpdates() {
00360         return array(
00361             array( "TextContentTest_testGetSecondaryDataUpdates_1",
00362                 CONTENT_MODEL_TEXT, "hello ''world''\n",
00363                 array()
00364             ),
00365             array( "TextContentTest_testGetSecondaryDataUpdates_2",
00366                 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
00367                 array()
00368             ),
00369             // TODO: more...?
00370         );
00371     }
00372 
00377     public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
00378         $ns = $this->getDefaultWikitextNS();
00379         $title = Title::newFromText( $title, $ns );
00380 
00381         $content = ContentHandler::makeContent( $text, $title, $model );
00382 
00383         $page = WikiPage::factory( $title );
00384         $page->doEditContent( $content, '' );
00385 
00386         $updates = $content->getDeletionUpdates( $page );
00387 
00388         // make updates accessible by class name
00389         foreach ( $updates as $update ) {
00390             $class = get_class( $update );
00391             $updates[$class] = $update;
00392         }
00393 
00394         if ( !$expectedStuff ) {
00395             $this->assertTrue( true ); // make phpunit happy
00396             return;
00397         }
00398 
00399         foreach ( $expectedStuff as $class => $fieldValues ) {
00400             $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
00401 
00402             $update = $updates[$class];
00403 
00404             foreach ( $fieldValues as $field => $value ) {
00405                 $v = $update->$field; #if the field doesn't exist, just crash and burn
00406                 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
00407             }
00408         }
00409 
00410         $page->doDeleteArticle( '' );
00411     }
00412 
00413     public static function provideConvert() {
00414         return array(
00415             array( // #0
00416                 'Hallo Welt',
00417                 CONTENT_MODEL_WIKITEXT,
00418                 'lossless',
00419                 'Hallo Welt'
00420             ),
00421             array( // #1
00422                 'Hallo Welt',
00423                 CONTENT_MODEL_WIKITEXT,
00424                 'lossless',
00425                 'Hallo Welt'
00426             ),
00427             array( // #1
00428                 'Hallo Welt',
00429                 CONTENT_MODEL_CSS,
00430                 'lossless',
00431                 'Hallo Welt'
00432             ),
00433             array( // #1
00434                 'Hallo Welt',
00435                 CONTENT_MODEL_JAVASCRIPT,
00436                 'lossless',
00437                 'Hallo Welt'
00438             ),
00439         );
00440     }
00441 
00446     public function testConvert( $text, $model, $lossy, $expectedNative ) {
00447         $content = $this->newContent( $text );
00448 
00449         $converted = $content->convert( $model, $lossy );
00450 
00451         if ( $expectedNative === false ) {
00452             $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
00453         } else {
00454             $this->assertInstanceOf( 'Content', $converted );
00455             $this->assertEquals( $expectedNative, $converted->getNativeData() );
00456         }
00457     }
00458 }