MediaWiki  REL1_21
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 
00055         public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
00056                 $title = Title::newFromText( $title );
00057                 $content = ContentHandler::makeContent( $text, $title, $model );
00058 
00059                 $po = $content->getParserOutput( $title );
00060 
00061                 $html = $po->getText();
00062                 $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
00063 
00064                 $this->assertEquals( $expectedHtml, trim( $html ) );
00065 
00066                 if ( $expectedFields ) {
00067                         foreach ( $expectedFields as $field => $exp ) {
00068                                 $f = 'get' . ucfirst( $field );
00069                                 $v = call_user_func( array( $po, $f ) );
00070 
00071                                 if ( is_array( $exp ) ) {
00072                                         $this->assertArrayEquals( $exp, $v );
00073                                 } else {
00074                                         $this->assertEquals( $exp, $v );
00075                                 }
00076                         }
00077                 }
00078 
00079                 // TODO: assert more properties
00080         }
00081 
00082         public static function dataPreSaveTransform() {
00083                 return array(
00084                         array(
00085                                 #0: no signature resolution
00086                                 'hello this is ~~~',
00087                                 'hello this is ~~~',
00088                         ),
00089                         array(
00090                                 #1: rtrim
00091                                 " Foo \n ",
00092                                 ' Foo',
00093                         ),
00094                 );
00095         }
00096 
00100         public function testPreSaveTransform( $text, $expected ) {
00101                 global $wgContLang;
00102 
00103                 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
00104 
00105                 $content = $this->newContent( $text );
00106                 $content = $content->preSaveTransform( $this->context->getTitle(), $this->context->getUser(), $options );
00107 
00108                 $this->assertEquals( $expected, $content->getNativeData() );
00109         }
00110 
00111         public static function dataPreloadTransform() {
00112                 return array(
00113                         array(
00114                                 'hello this is ~~~',
00115                                 'hello this is ~~~',
00116                         ),
00117                 );
00118         }
00119 
00123         public function testPreloadTransform( $text, $expected ) {
00124                 global $wgContLang;
00125                 $options = ParserOptions::newFromUserAndLang( $this->context->getUser(), $wgContLang );
00126 
00127                 $content = $this->newContent( $text );
00128                 $content = $content->preloadTransform( $this->context->getTitle(), $options );
00129 
00130                 $this->assertEquals( $expected, $content->getNativeData() );
00131         }
00132 
00133         public static function dataGetRedirectTarget() {
00134                 return array(
00135                         array( '#REDIRECT [[Test]]',
00136                                 null,
00137                         ),
00138                 );
00139         }
00140 
00144         public function testGetRedirectTarget( $text, $expected ) {
00145                 $content = $this->newContent( $text );
00146                 $t = $content->getRedirectTarget();
00147 
00148                 if ( is_null( $expected ) ) {
00149                         $this->assertNull( $t, "text should not have generated a redirect target: $text" );
00150                 } else {
00151                         $this->assertEquals( $expected, $t->getPrefixedText() );
00152                 }
00153         }
00154 
00158         public function testIsRedirect( $text, $expected ) {
00159                 $content = $this->newContent( $text );
00160 
00161                 $this->assertEquals( !is_null( $expected ), $content->isRedirect() );
00162         }
00163 
00167         /*
00168         public function getRedirectChain() {
00169                 $text = $this->getNativeData();
00170                 return Title::newFromRedirectArray( $text );
00171         }
00172         */
00173 
00177         /*
00178         public function getUltimateRedirectTarget() {
00179                 $text = $this->getNativeData();
00180                 return Title::newFromRedirectRecurse( $text );
00181         }
00182         */
00183 
00184         public static function dataIsCountable() {
00185                 return array(
00186                         array( '',
00187                                 null,
00188                                 'any',
00189                                 true
00190                         ),
00191                         array( 'Foo',
00192                                 null,
00193                                 'any',
00194                                 true
00195                         ),
00196                         array( 'Foo',
00197                                 null,
00198                                 'comma',
00199                                 false
00200                         ),
00201                         array( 'Foo, bar',
00202                                 null,
00203                                 'comma',
00204                                 false
00205                         ),
00206                 );
00207         }
00208 
00213         public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
00214                 global $wgArticleCountMethod;
00215 
00216                 $old = $wgArticleCountMethod;
00217                 $wgArticleCountMethod = $mode;
00218 
00219                 $content = $this->newContent( $text );
00220 
00221                 $v = $content->isCountable( $hasLinks, $this->context->getTitle() );
00222                 $wgArticleCountMethod = $old;
00223 
00224                 $this->assertEquals( $expected, $v, 'isCountable() returned unexpected value ' . var_export( $v, true )
00225                         . ' instead of ' . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
00226         }
00227 
00228         public static function dataGetTextForSummary() {
00229                 return array(
00230                         array( "hello\nworld.",
00231                                 16,
00232                                 'hello world.',
00233                         ),
00234                         array( 'hello world.',
00235                                 8,
00236                                 'hello...',
00237                         ),
00238                         array( '[[hello world]].',
00239                                 8,
00240                                 '[[hel...',
00241                         ),
00242                 );
00243         }
00244 
00248         public function testGetTextForSummary( $text, $maxlength, $expected ) {
00249                 $content = $this->newContent( $text );
00250 
00251                 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
00252         }
00253 
00254         public function testGetTextForSearchIndex() {
00255                 $content = $this->newContent( 'hello world.' );
00256 
00257                 $this->assertEquals( 'hello world.', $content->getTextForSearchIndex() );
00258         }
00259 
00260         public function testCopy() {
00261                 $content = $this->newContent( 'hello world.' );
00262                 $copy = $content->copy();
00263 
00264                 $this->assertTrue( $content->equals( $copy ), 'copy must be equal to original' );
00265                 $this->assertEquals( 'hello world.', $copy->getNativeData() );
00266         }
00267 
00268         public function testGetSize() {
00269                 $content = $this->newContent( 'hello world.' );
00270 
00271                 $this->assertEquals( 12, $content->getSize() );
00272         }
00273 
00274         public function testGetNativeData() {
00275                 $content = $this->newContent( 'hello world.' );
00276 
00277                 $this->assertEquals( 'hello world.', $content->getNativeData() );
00278         }
00279 
00280         public function testGetWikitextForTransclusion() {
00281                 $content = $this->newContent( 'hello world.' );
00282 
00283                 $this->assertEquals( 'hello world.', $content->getWikitextForTransclusion() );
00284         }
00285 
00286         public function testGetModel() {
00287                 $content = $this->newContent( "hello world." );
00288 
00289                 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getModel() );
00290         }
00291 
00292         public function testGetContentHandler() {
00293                 $content = $this->newContent( "hello world." );
00294 
00295                 $this->assertEquals( CONTENT_MODEL_TEXT, $content->getContentHandler()->getModelID() );
00296         }
00297 
00298         public static function dataIsEmpty() {
00299                 return array(
00300                         array( '', true ),
00301                         array( '  ', false ),
00302                         array( '0', false ),
00303                         array( 'hallo welt.', false ),
00304                 );
00305         }
00306 
00310         public function testIsEmpty( $text, $empty ) {
00311                 $content = $this->newContent( $text );
00312 
00313                 $this->assertEquals( $empty, $content->isEmpty() );
00314         }
00315 
00316         public static function dataEquals() {
00317                 return array(
00318                         array( new TextContent( "hallo" ), null, false ),
00319                         array( new TextContent( "hallo" ), new TextContent( "hallo" ), true ),
00320                         array( new TextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
00321                         array( new TextContent( "hallo" ), new WikitextContent( "hallo" ), false ),
00322                         array( new TextContent( "hallo" ), new TextContent( "HALLO" ), false ),
00323                 );
00324         }
00325 
00329         public function testEquals( Content $a, Content $b = null, $equal = false ) {
00330                 $this->assertEquals( $equal, $a->equals( $b ) );
00331         }
00332 
00333         public static function dataGetDeletionUpdates() {
00334                 return array(
00335                         array( "TextContentTest_testGetSecondaryDataUpdates_1",
00336                                 CONTENT_MODEL_TEXT, "hello ''world''\n",
00337                                 array()
00338                         ),
00339                         array( "TextContentTest_testGetSecondaryDataUpdates_2",
00340                                 CONTENT_MODEL_TEXT, "hello [[world test 21344]]\n",
00341                                 array()
00342                         ),
00343                         // TODO: more...?
00344                 );
00345         }
00346 
00350         public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
00351                 $ns = $this->getDefaultWikitextNS();
00352                 $title = Title::newFromText( $title, $ns );
00353 
00354                 $content = ContentHandler::makeContent( $text, $title, $model );
00355 
00356                 $page = WikiPage::factory( $title );
00357                 $page->doEditContent( $content, '' );
00358 
00359                 $updates = $content->getDeletionUpdates( $page );
00360 
00361                 // make updates accessible by class name
00362                 foreach ( $updates as $update ) {
00363                         $class = get_class( $update );
00364                         $updates[$class] = $update;
00365                 }
00366 
00367                 if ( !$expectedStuff ) {
00368                         $this->assertTrue( true ); // make phpunit happy
00369                         return;
00370                 }
00371 
00372                 foreach ( $expectedStuff as $class => $fieldValues ) {
00373                         $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
00374 
00375                         $update = $updates[$class];
00376 
00377                         foreach ( $fieldValues as $field => $value ) {
00378                                 $v = $update->$field; #if the field doesn't exist, just crash and burn
00379                                 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
00380                         }
00381                 }
00382 
00383                 $page->doDeleteArticle( '' );
00384         }
00385 
00386         public static function provideConvert() {
00387                 return array(
00388                         array( // #0
00389                                 'Hallo Welt',
00390                                 CONTENT_MODEL_WIKITEXT,
00391                                 'lossless',
00392                                 'Hallo Welt'
00393                         ),
00394                         array( // #1
00395                                 'Hallo Welt',
00396                                 CONTENT_MODEL_WIKITEXT,
00397                                 'lossless',
00398                                 'Hallo Welt'
00399                         ),
00400                         array( // #1
00401                                 'Hallo Welt',
00402                                 CONTENT_MODEL_CSS,
00403                                 'lossless',
00404                                 'Hallo Welt'
00405                         ),
00406                         array( // #1
00407                                 'Hallo Welt',
00408                                 CONTENT_MODEL_JAVASCRIPT,
00409                                 'lossless',
00410                                 'Hallo Welt'
00411                         ),
00412                 );
00413         }
00414 
00418         public function testConvert( $text, $model, $lossy, $expectedNative ) {
00419                 $content = $this->newContent( $text );
00420 
00421                 $converted = $content->convert( $model, $lossy );
00422 
00423                 if ( $expectedNative === false ) {
00424                         $this->assertFalse( $converted, "conversion to $model was expected to fail!" );
00425                 } else {
00426                         $this->assertInstanceOf( 'Content', $converted );
00427                         $this->assertEquals( $expectedNative, $converted->getNativeData() );
00428                 }
00429         }
00430 
00431 }