MediaWiki  REL1_21
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 
00068         public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
00069                 $ns = $this->getDefaultWikitextNS();
00070                 $title = Title::newFromText( $title, $ns );
00071 
00072                 $content = ContentHandler::makeContent( $text, $title, $model );
00073 
00074                 $page = WikiPage::factory( $title );
00075                 $page->doEditContent( $content, '' );
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                 $page->doDeleteArticle( '' );
00097         }
00098 
00099         public static function dataGetSection() {
00100                 return array(
00101                         array( WikitextContentTest::$sections,
00102                                 "0",
00103                                 "Intro"
00104                         ),
00105                         array( WikitextContentTest::$sections,
00106                                 "2",
00107                                 "== test ==
00108 just a test"
00109                         ),
00110                         array( WikitextContentTest::$sections,
00111                                 "8",
00112                                 false
00113                         ),
00114                 );
00115         }
00116 
00120         public function testGetSection( $text, $sectionId, $expectedText ) {
00121                 $content = $this->newContent( $text );
00122 
00123                 $sectionContent = $content->getSection( $sectionId );
00124                 if ( is_object( $sectionContent ) ) {
00125                         $sectionText = $sectionContent->getNativeData();
00126                 } else {
00127                         $sectionText = $sectionContent;
00128                 }
00129 
00130                 $this->assertEquals( $expectedText, $sectionText );
00131         }
00132 
00133         public static function dataReplaceSection() {
00134                 return array(
00135                         array( WikitextContentTest::$sections,
00136                                 "0",
00137                                 "No more",
00138                                 null,
00139                                 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) )
00140                         ),
00141                         array( WikitextContentTest::$sections,
00142                                 "",
00143                                 "No more",
00144                                 null,
00145                                 "No more"
00146                         ),
00147                         array( WikitextContentTest::$sections,
00148                                 "2",
00149                                 "== TEST ==\nmore fun",
00150                                 null,
00151                                 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) )
00152                         ),
00153                         array( WikitextContentTest::$sections,
00154                                 "8",
00155                                 "No more",
00156                                 null,
00157                                 WikitextContentTest::$sections
00158                         ),
00159                         array( WikitextContentTest::$sections,
00160                                 "new",
00161                                 "No more",
00162                                 "New",
00163                                 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more"
00164                         ),
00165                 );
00166         }
00167 
00171         public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
00172                 $content = $this->newContent( $text );
00173                 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
00174 
00175                 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() );
00176         }
00177 
00178         public function testAddSectionHeader() {
00179                 $content = $this->newContent( 'hello world' );
00180                 $content = $content->addSectionHeader( 'test' );
00181 
00182                 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
00183         }
00184 
00185         public static function dataPreSaveTransform() {
00186                 return array(
00187                         array( 'hello this is ~~~',
00188                                 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
00189                         ),
00190                         array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00191                                 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
00192                         ),
00193                         array( // rtrim
00194                                 " Foo \n ",
00195                                 " Foo",
00196                         ),
00197                 );
00198         }
00199 
00200         public static function dataPreloadTransform() {
00201                 return array(
00202                         array( 'hello this is ~~~',
00203                                 "hello this is ~~~",
00204                         ),
00205                         array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
00206                                 'hello \'\'this\'\' is bar',
00207                         ),
00208                 );
00209         }
00210 
00211         public static function dataGetRedirectTarget() {
00212                 return array(
00213                         array( '#REDIRECT [[Test]]',
00214                                 'Test',
00215                         ),
00216                         array( '#REDIRECT Test',
00217                                 null,
00218                         ),
00219                         array( '* #REDIRECT [[Test]]',
00220                                 null,
00221                         ),
00222                 );
00223         }
00224 
00225         public static function dataGetTextForSummary() {
00226                 return array(
00227                         array( "hello\nworld.",
00228                                 16,
00229                                 'hello world.',
00230                         ),
00231                         array( 'hello world.',
00232                                 8,
00233                                 'hello...',
00234                         ),
00235                         array( '[[hello world]].',
00236                                 8,
00237                                 'hel...',
00238                         ),
00239                 );
00240         }
00241 
00245         /*
00246         public function getRedirectChain() {
00247                 $text = $this->getNativeData();
00248                 return Title::newFromRedirectArray( $text );
00249         }
00250         */
00251 
00255         /*
00256         public function getUltimateRedirectTarget() {
00257                 $text = $this->getNativeData();
00258                 return Title::newFromRedirectRecurse( $text );
00259         }
00260         */
00261 
00262         public static function dataIsCountable() {
00263                 return array(
00264                         array( '',
00265                                 null,
00266                                 'any',
00267                                 true
00268                         ),
00269                         array( 'Foo',
00270                                 null,
00271                                 'any',
00272                                 true
00273                         ),
00274                         array( 'Foo',
00275                                 null,
00276                                 'comma',
00277                                 false
00278                         ),
00279                         array( 'Foo, bar',
00280                                 null,
00281                                 'comma',
00282                                 true
00283                         ),
00284                         array( 'Foo',
00285                                 null,
00286                                 'link',
00287                                 false
00288                         ),
00289                         array( 'Foo [[bar]]',
00290                                 null,
00291                                 'link',
00292                                 true
00293                         ),
00294                         array( 'Foo',
00295                                 true,
00296                                 'link',
00297                                 true
00298                         ),
00299                         array( 'Foo [[bar]]',
00300                                 false,
00301                                 'link',
00302                                 false
00303                         ),
00304                         array( '#REDIRECT [[bar]]',
00305                                 true,
00306                                 'any',
00307                                 false
00308                         ),
00309                         array( '#REDIRECT [[bar]]',
00310                                 true,
00311                                 'comma',
00312                                 false
00313                         ),
00314                         array( '#REDIRECT [[bar]]',
00315                                 true,
00316                                 'link',
00317                                 false
00318                         ),
00319                 );
00320         }
00321 
00322         public function testMatchMagicWord() {
00323                 $mw = MagicWord::get( "staticredirect" );
00324 
00325                 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
00326                 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
00327 
00328                 $content = $this->newContent( "#REDIRECT [[FOO]]" );
00329                 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
00330         }
00331 
00332         public function testUpdateRedirect() {
00333                 $target = Title::newFromText( "testUpdateRedirect_target" );
00334 
00335                 // test with non-redirect page
00336                 $content = $this->newContent( "hello world." );
00337                 $newContent = $content->updateRedirect( $target );
00338 
00339                 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
00340 
00341                 // test with actual redirect
00342                 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
00343                 $newContent = $content->updateRedirect( $target );
00344 
00345                 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
00346                 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
00347 
00348                 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
00349         }
00350 
00351         public function testGetModel() {
00352                 $content = $this->newContent( "hello world." );
00353 
00354                 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() );
00355         }
00356 
00357         public function testGetContentHandler() {
00358                 $content = $this->newContent( "hello world." );
00359 
00360                 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() );
00361         }
00362 
00363         public static function dataEquals() {
00364                 return array(
00365                         array( new WikitextContent( "hallo" ), null, false ),
00366                         array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
00367                         array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
00368                         array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
00369                         array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
00370                 );
00371         }
00372 
00373         public static function dataGetDeletionUpdates() {
00374                 return array(
00375                         array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
00376                                 CONTENT_MODEL_WIKITEXT, "hello ''world''\n",
00377                                 array( 'LinksDeletionUpdate' => array() )
00378                         ),
00379                         array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
00380                                 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n",
00381                                 array( 'LinksDeletionUpdate' => array() )
00382                         ),
00383                         // @todo: more...?
00384                 );
00385         }
00386 }