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