MediaWiki  master
WikitextContentTest.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class WikitextContentTest extends TextContentTest {
00010 
00011         static $sections =
00012 
00013 "Intro
00014 
00015 == stuff ==
00016 hello world
00017 
00018 == test ==
00019 just a test
00020 
00021 == foo ==
00022 more stuff
00023 ";
00024 
00025         public function newContent( $text ) {
00026                 return new WikitextContent( $text );
00027         }
00028 
00029         public static function dataGetParserOutput() {
00030                 return array(
00031                         array(
00032                                 "WikitextContentTest_testGetParserOutput",
00033                                 CONTENT_MODEL_WIKITEXT,
00034                                 "hello ''world''\n",
00035                                 "<p>hello <i>world</i>\n</p>"
00036                         ),
00037                         // TODO: more...?
00038                 );
00039         }
00040 
00041         public static function dataGetSecondaryDataUpdates() {
00042                 return array(
00043                         array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
00044                                 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
00045                                 array(
00046                                         'LinksUpdate' => array(
00047                                                 'mRecursive' => true,
00048                                                 'mLinks' => array()
00049                                         )
00050                                 )
00051                         ),
00052                         array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
00053                                 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
00054                                 array(
00055                                         'LinksUpdate' => array(
00056                                                 'mRecursive' => true,
00057                                                 'mLinks' => array(
00058                                                         array( 'World_test_21344' => 0 )
00059                                                 )
00060                                         )
00061                                 )
00062                         ),
00063                         // TODO: more...?
00064                 );
00065         }
00066 
00071         public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
00072                 $title = Title::newFromText( $title );
00073                 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
00074 
00075                 $content = ContentHandler::makeContent( $text, $title, $model );
00076 
00077                 $updates = $content->getSecondaryDataUpdates( $title );
00078 
00079                 // make updates accessible by class name
00080                 foreach ( $updates as $update ) {
00081                         $class = get_class( $update );
00082                         $updates[$class] = $update;
00083                 }
00084 
00085                 foreach ( $expectedStuff as $class => $fieldValues ) {
00086                         $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
00087 
00088                         $update = $updates[$class];
00089 
00090                         foreach ( $fieldValues as $field => $value ) {
00091                                 $v = $update->$field; #if the field doesn't exist, just crash and burn
00092                                 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
00093                         }
00094                 }
00095         }
00096 
00097         public static function dataGetSection() {
00098                 return array(
00099                         array( WikitextContentTest::$sections,
00100                                         "0",
00101                                         "Intro"
00102                         ),
00103                         array( WikitextContentTest::$sections,
00104                                         "2",
00105 "== test ==
00106 just a test"
00107                         ),
00108                         array( WikitextContentTest::$sections,
00109                                         "8",
00110                                         false
00111                         ),
00112                 );
00113         }
00114 
00118         public function testGetSection( $text, $sectionId, $expectedText ) {
00119                 $content = $this->newContent( $text );
00120 
00121                 $sectionContent = $content->getSection( $sectionId );
00122                 if ( is_object( $sectionContent ) ) {
00123                         $sectionText = $sectionContent->getNativeData();
00124                 } else {
00125                         $sectionText = $sectionContent;
00126                 }
00127 
00128                 $this->assertEquals( $expectedText, $sectionText );
00129         }
00130 
00131         public static function dataReplaceSection() {
00132                 return array(
00133                         array( WikitextContentTest::$sections,
00134                                "0",
00135                                "No more",
00136                                null,
00137                                trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
00138                         ),
00139                         array( WikitextContentTest::$sections,
00140                                "",
00141                                "No more",
00142                                null,
00143                                "No more"
00144                         ),
00145                         array( WikitextContentTest::$sections,
00146                                "2",
00147                                "== TEST ==\nmore fun",
00148                                null,
00149                                trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
00150                         ),
00151                         array( WikitextContentTest::$sections,
00152                                "8",
00153                                "No more",
00154                                null,
00155                                WikitextContentTest::$sections
00156                         ),
00157                         array( WikitextContentTest::$sections,
00158                                "new",
00159                                "No more",
00160                                "New",
00161                                trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
00162                         ),
00163                 );
00164         }
00165 
00169         public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
00170                 $content = $this->newContent( $text );
00171                 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
00172 
00173                 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
00174         }
00175 
00176         public function testAddSectionHeader( ) {
00177                 $content = $this->newContent( 'hello world' );
00178                 $content = $content->addSectionHeader( 'test' );
00179 
00180                 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
00181         }
00182 
00183         public static function dataPreSaveTransform() {
00184                 return array(
00185                         array( 'hello this is ~~~',
00186                                "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
00187                         ),
00188                         array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00189                                'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00190                         ),
00191                         array( // rtrim
00192                                 " Foo \n ",
00193                                 " Foo",
00194                         ),
00195                 );
00196         }
00197 
00198         public static function dataPreloadTransform() {
00199                 return array(
00200                         array( 'hello this is ~~~',
00201                                "hello this is ~~~",
00202                         ),
00203                         array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
00204                                'hello \'\'this\'\' is bar',
00205                         ),
00206                 );
00207         }
00208 
00209         public static function dataGetRedirectTarget() {
00210                 return array(
00211                         array( '#REDIRECT [[Test]]',
00212                                'Test',
00213                         ),
00214                         array( '#REDIRECT Test',
00215                                null,
00216                         ),
00217                         array( '* #REDIRECT [[Test]]',
00218                                null,
00219                         ),
00220                 );
00221         }
00222 
00223         public static function dataGetTextForSummary() {
00224                 return array(
00225                         array( "hello\nworld.",
00226                                 16,
00227                                 'hello world.',
00228                         ),
00229                         array( 'hello world.',
00230                                 8,
00231                                 'hello...',
00232                         ),
00233                         array( '[[hello world]].',
00234                                 8,
00235                                 'hel...',
00236                         ),
00237                 );
00238         }
00239 
00243         /*
00244         public function getRedirectChain() {
00245                 $text = $this->getNativeData();
00246                 return Title::newFromRedirectArray( $text );
00247         }
00248         */
00249 
00253         /*
00254         public function getUltimateRedirectTarget() {
00255                 $text = $this->getNativeData();
00256                 return Title::newFromRedirectRecurse( $text );
00257         }
00258         */
00259 
00260         public static function dataIsCountable() {
00261                 return array(
00262                         array( '',
00263                                null,
00264                                'any',
00265                                true
00266                         ),
00267                         array( 'Foo',
00268                                null,
00269                                'any',
00270                                true
00271                         ),
00272                         array( 'Foo',
00273                                null,
00274                                'comma',
00275                                false
00276                         ),
00277                         array( 'Foo, bar',
00278                                null,
00279                                'comma',
00280                                true
00281                         ),
00282                         array( 'Foo',
00283                                null,
00284                                'link',
00285                                false
00286                         ),
00287                         array( 'Foo [[bar]]',
00288                                null,
00289                                'link',
00290                                true
00291                         ),
00292                         array( 'Foo',
00293                                true,
00294                                'link',
00295                                true
00296                         ),
00297                         array( 'Foo [[bar]]',
00298                                false,
00299                                'link',
00300                                false
00301                         ),
00302                         array( '#REDIRECT [[bar]]',
00303                                true,
00304                                'any',
00305                                false
00306                         ),
00307                         array( '#REDIRECT [[bar]]',
00308                                true,
00309                                'comma',
00310                                false
00311                         ),
00312                         array( '#REDIRECT [[bar]]',
00313                                true,
00314                                'link',
00315                                false
00316                         ),
00317                 );
00318         }
00319 
00320         public function testMatchMagicWord( ) {
00321                 $mw = MagicWord::get( "staticredirect" );
00322 
00323                 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
00324                 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
00325 
00326                 $content = $this->newContent( "#REDIRECT [[FOO]]" );
00327                 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
00328         }
00329 
00330         public function testUpdateRedirect( ) {
00331                 $target = Title::newFromText( "testUpdateRedirect_target" );
00332 
00333                 // test with non-redirect page
00334                 $content = $this->newContent( "hello world." );
00335                 $newContent = $content->updateRedirect( $target );
00336 
00337                 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
00338 
00339                 // test with actual redirect
00340                 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
00341                 $newContent = $content->updateRedirect( $target );
00342 
00343                 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
00344                 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
00345 
00346                 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
00347         }
00348 
00349         public function testGetModel() {
00350                 $content = $this->newContent( "hello world." );
00351 
00352                 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
00353         }
00354 
00355         public function testGetContentHandler() {
00356                 $content = $this->newContent( "hello world." );
00357 
00358                 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
00359         }
00360 
00361         public static function dataEquals( ) {
00362                 return array(
00363                         array( new WikitextContent( "hallo" ), null, false ),
00364                         array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
00365                         array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
00366                         array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
00367                         array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
00368                 );
00369         }
00370 
00371         public static function dataGetDeletionUpdates() {
00372                 return array(
00373                         array("WikitextContentTest_testGetSecondaryDataUpdates_1",
00374                                 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
00375                                 array( 'LinksDeletionUpdate' => array( ) )
00376                         ),
00377                         array("WikitextContentTest_testGetSecondaryDataUpdates_2",
00378                                 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
00379                                 array( 'LinksDeletionUpdate' => array( ) )
00380                         ),
00381                         // @todo: more...?
00382                 );
00383         }
00384 
00385 }