MediaWiki  master
WikiPageTest.php
Go to the documentation of this file.
00001 <?php
00009 class WikiPageTest extends MediaWikiLangTestCase {
00010 
00011         var $pages_to_delete;
00012 
00013         function  __construct( $name = null, array $data = array(), $dataName = '' ) {
00014                 parent::__construct( $name, $data, $dataName );
00015 
00016                 $this->tablesUsed = array_merge (
00017                         $this->tablesUsed,
00018                         array( 'page',
00019                                         'revision',
00020                                         'text',
00021 
00022                                         'recentchanges',
00023                                         'logging',
00024 
00025                                         'page_props',
00026                                         'pagelinks',
00027                                         'categorylinks',
00028                                         'langlinks',
00029                                         'externallinks',
00030                                         'imagelinks',
00031                                         'templatelinks',
00032                                         'iwlinks' ) );
00033         }
00034 
00035         protected function setUp() {
00036                 parent::setUp();
00037                 $this->pages_to_delete = array();
00038 
00039                 LinkCache::singleton()->clear(); # avoid cached redirect status, etc
00040         }
00041 
00042         protected function tearDown() {
00043                 foreach ( $this->pages_to_delete as $p ) {
00044                         /* @var $p WikiPage */
00045 
00046                         try {
00047                                 if ( $p->exists() ) {
00048                                         $p->doDeleteArticle( "testing done." );
00049                                 }
00050                         } catch ( MWException $ex ) {
00051                                 // fail silently
00052                         }
00053                 }
00054                 parent::tearDown();
00055         }
00056 
00062         protected function newPage( $title, $model = null ) {
00063                 if ( is_string( $title ) ) {
00064                         $ns = $this->getDefaultWikitextNS();
00065                         $title = Title::newFromText( $title, $ns );
00066                 }
00067 
00068                 $p = new WikiPage( $title );
00069 
00070                 $this->pages_to_delete[] = $p;
00071 
00072                 return $p;
00073         }
00074 
00075 
00083         protected function createPage( $page, $text, $model = null ) {
00084                 if ( is_string( $page ) || $page instanceof Title ) {
00085                         $page = $this->newPage( $page, $model );
00086                 }
00087 
00088                 $content = ContentHandler::makeContent( $text, $page->getTitle(), $model );
00089                 $page->doEditContent( $content, "testing", EDIT_NEW );
00090 
00091                 return $page;
00092         }
00093 
00094         public function testDoEditContent() {
00095                 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
00096                 $title = $page->getTitle();
00097 
00098                 $content = ContentHandler::makeContent( "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
00099                                                 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
00100                                                 $title, CONTENT_MODEL_WIKITEXT );
00101 
00102                 $page->doEditContent( $content, "[[testing]] 1" );
00103 
00104                 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
00105                 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
00106                 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
00107                 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
00108 
00109                 $id = $page->getId();
00110 
00111                 # ------------------------
00112                 $dbr = wfGetDB( DB_SLAVE );
00113                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00114                 $n = $res->numRows();
00115                 $res->free();
00116 
00117                 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
00118 
00119                 # ------------------------
00120                 $page = new WikiPage( $title );
00121 
00122                 $retrieved = $page->getContent();
00123                 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
00124 
00125                 # ------------------------
00126                 $content = ContentHandler::makeContent( "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
00127                                                                                                 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
00128                                                                                                 $title, CONTENT_MODEL_WIKITEXT );
00129 
00130                 $page->doEditContent( $content, "testing 2" );
00131 
00132                 # ------------------------
00133                 $page = new WikiPage( $title );
00134 
00135                 $retrieved = $page->getContent();
00136                 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
00137 
00138                 # ------------------------
00139                 $dbr = wfGetDB( DB_SLAVE );
00140                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00141                 $n = $res->numRows();
00142                 $res->free();
00143 
00144                 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
00145         }
00146 
00147         public function testDoEdit() {
00148                 $this->hideDeprecated( "WikiPage::doEdit" );
00149                 $this->hideDeprecated( "WikiPage::getText" );
00150                 $this->hideDeprecated( "Revision::getText" );
00151 
00152                 //NOTE: assume help namespace will default to wikitext
00153                 $title = Title::newFromText( "Help:WikiPageTest_testDoEdit" );
00154 
00155                 $page = $this->newPage( $title );
00156 
00157                 $text = "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
00158                                 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
00159 
00160                 $page->doEdit( $text, "[[testing]] 1" );
00161 
00162                 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
00163                 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
00164                 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
00165                 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
00166 
00167                 $id = $page->getId();
00168 
00169                 # ------------------------
00170                 $dbr = wfGetDB( DB_SLAVE );
00171                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00172                 $n = $res->numRows();
00173                 $res->free();
00174 
00175                 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
00176 
00177                 # ------------------------
00178                 $page = new WikiPage( $title );
00179 
00180                 $retrieved = $page->getText();
00181                 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
00182 
00183                 # ------------------------
00184                 $text = "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
00185                                 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.";
00186 
00187                 $page->doEdit( $text, "testing 2" );
00188 
00189                 # ------------------------
00190                 $page = new WikiPage( $title );
00191 
00192                 $retrieved = $page->getText();
00193                 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
00194 
00195                 # ------------------------
00196                 $dbr = wfGetDB( DB_SLAVE );
00197                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00198                 $n = $res->numRows();
00199                 $res->free();
00200 
00201                 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
00202         }
00203 
00204         public function testDoQuickEdit() {
00205                 global $wgUser;
00206 
00207                 $this->hideDeprecated( "WikiPage::doQuickEdit" );
00208 
00209                 //NOTE: assume help namespace will default to wikitext
00210                 $page = $this->createPage( "Help:WikiPageTest_testDoQuickEdit", "original text" );
00211 
00212                 $text = "quick text";
00213                 $page->doQuickEdit( $text, $wgUser, "testing q" );
00214 
00215                 # ---------------------
00216                 $page = new WikiPage( $page->getTitle() );
00217                 $this->assertEquals( $text, $page->getText() );
00218         }
00219 
00220         public function testDoQuickEditContent() {
00221                 global $wgUser;
00222 
00223                 $page = $this->createPage( "WikiPageTest_testDoQuickEditContent", "original text", CONTENT_MODEL_WIKITEXT );
00224 
00225                 $content = ContentHandler::makeContent( "quick text", $page->getTitle(), CONTENT_MODEL_WIKITEXT );
00226                 $page->doQuickEditContent( $content, $wgUser, "testing q" );
00227 
00228                 # ---------------------
00229                 $page = new WikiPage( $page->getTitle() );
00230                 $this->assertTrue( $content->equals( $page->getContent() ) );
00231         }
00232 
00233         public function testDoDeleteArticle() {
00234                 $page = $this->createPage( "WikiPageTest_testDoDeleteArticle", "[[original text]] foo", CONTENT_MODEL_WIKITEXT );
00235                 $id = $page->getId();
00236 
00237                 $page->doDeleteArticle( "testing deletion" );
00238 
00239                 $this->assertFalse( $page->getTitle()->getArticleID() > 0, "Title object should now have page id 0" );
00240                 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
00241                 $this->assertFalse( $page->exists(), "WikiPage::exists should return false after page was deleted" );
00242                 $this->assertNull( $page->getContent(), "WikiPage::getContent should return null after page was deleted" );
00243                 $this->assertFalse( $page->getText(), "WikiPage::getText should return false after page was deleted" );
00244 
00245                 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
00246                 $this->assertFalse( $t->exists(), "Title::exists should return false after page was deleted" );
00247 
00248                 # ------------------------
00249                 $dbr = wfGetDB( DB_SLAVE );
00250                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00251                 $n = $res->numRows();
00252                 $res->free();
00253 
00254                 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
00255         }
00256 
00257         public function testDoDeleteUpdates() {
00258                 $page = $this->createPage( "WikiPageTest_testDoDeleteArticle", "[[original text]] foo", CONTENT_MODEL_WIKITEXT );
00259                 $id = $page->getId();
00260 
00261                 $page->doDeleteUpdates( $id );
00262 
00263                 # ------------------------
00264                 $dbr = wfGetDB( DB_SLAVE );
00265                 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
00266                 $n = $res->numRows();
00267                 $res->free();
00268 
00269                 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
00270         }
00271 
00272         public function testGetRevision() {
00273                 $page = $this->newPage( "WikiPageTest_testGetRevision" );
00274 
00275                 $rev = $page->getRevision();
00276                 $this->assertNull( $rev );
00277 
00278                 # -----------------
00279                 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00280 
00281                 $rev = $page->getRevision();
00282 
00283                 $this->assertEquals( $page->getLatest(), $rev->getId() );
00284                 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
00285         }
00286 
00287         public function testGetContent() {
00288                 $page = $this->newPage( "WikiPageTest_testGetContent" );
00289 
00290                 $content = $page->getContent();
00291                 $this->assertNull( $content );
00292 
00293                 # -----------------
00294                 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00295 
00296                 $content = $page->getContent();
00297                 $this->assertEquals( "some text", $content->getNativeData() );
00298         }
00299 
00300         public function testGetText() {
00301                 $this->hideDeprecated( "WikiPage::getText" );
00302 
00303                 $page = $this->newPage( "WikiPageTest_testGetText" );
00304 
00305                 $text = $page->getText();
00306                 $this->assertFalse( $text );
00307 
00308                 # -----------------
00309                 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00310 
00311                 $text = $page->getText();
00312                 $this->assertEquals( "some text", $text );
00313         }
00314 
00315         public function testGetRawText() {
00316                 $this->hideDeprecated( "WikiPage::getRawText" );
00317 
00318                 $page = $this->newPage( "WikiPageTest_testGetRawText" );
00319 
00320                 $text = $page->getRawText();
00321                 $this->assertFalse( $text );
00322 
00323                 # -----------------
00324                 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00325 
00326                 $text = $page->getRawText();
00327                 $this->assertEquals( "some text", $text );
00328         }
00329 
00330         public function testGetContentModel() {
00331                 global $wgContentHandlerUseDB;
00332 
00333                 if ( !$wgContentHandlerUseDB ) {
00334                         $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
00335                 }
00336 
00337                 $page = $this->createPage( "WikiPageTest_testGetContentModel", "some text", CONTENT_MODEL_JAVASCRIPT );
00338 
00339                 $page = new WikiPage( $page->getTitle() );
00340                 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $page->getContentModel() );
00341         }
00342 
00343         public function testGetContentHandler() {
00344                 global $wgContentHandlerUseDB;
00345 
00346                 if ( !$wgContentHandlerUseDB ) {
00347                         $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
00348                 }
00349 
00350                 $page = $this->createPage( "WikiPageTest_testGetContentHandler", "some text", CONTENT_MODEL_JAVASCRIPT );
00351 
00352                 $page = new WikiPage( $page->getTitle() );
00353                 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
00354         }
00355 
00356         public function testExists() {
00357                 $page = $this->newPage( "WikiPageTest_testExists" );
00358                 $this->assertFalse( $page->exists() );
00359 
00360                 # -----------------
00361                 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00362                 $this->assertTrue( $page->exists() );
00363 
00364                 $page = new WikiPage( $page->getTitle() );
00365                 $this->assertTrue( $page->exists() );
00366 
00367                 # -----------------
00368                 $page->doDeleteArticle( "done testing" );
00369                 $this->assertFalse( $page->exists() );
00370 
00371                 $page = new WikiPage( $page->getTitle() );
00372                 $this->assertFalse( $page->exists() );
00373         }
00374 
00375         public static function provideHasViewableContent() {
00376                 return array(
00377                         array( 'WikiPageTest_testHasViewableContent', false, true ),
00378                         array( 'Special:WikiPageTest_testHasViewableContent', false ),
00379                         array( 'MediaWiki:WikiPageTest_testHasViewableContent', false ),
00380                         array( 'Special:Userlogin', true ),
00381                         array( 'MediaWiki:help', true ),
00382                 );
00383         }
00384 
00388         public function testHasViewableContent( $title, $viewable, $create = false ) {
00389                 $page = $this->newPage( $title );
00390                 $this->assertEquals( $viewable, $page->hasViewableContent() );
00391 
00392                 if ( $create ) {
00393                         $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
00394                         $this->assertTrue( $page->hasViewableContent() );
00395 
00396                         $page = new WikiPage( $page->getTitle() );
00397                         $this->assertTrue( $page->hasViewableContent() );
00398                 }
00399         }
00400 
00401         public static function provideGetRedirectTarget() {
00402                 return array(
00403                         array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ),
00404                         array( 'WikiPageTest_testGetRedirectTarget_2', CONTENT_MODEL_WIKITEXT, "#REDIRECT [[hello world]]", "Hello world" ),
00405                 );
00406         }
00407 
00411         public function testGetRedirectTarget( $title, $model, $text, $target ) {
00412                 $page = $this->createPage( $title, $text, $model );
00413 
00414                 # sanity check, because this test seems to fail for no reason for some people.
00415                 $c = $page->getContent();
00416                 $this->assertEquals( 'WikitextContent', get_class( $c ) );
00417 
00418                 # now, test the actual redirect
00419                 $t = $page->getRedirectTarget();
00420                 $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
00421         }
00422 
00426         public function testIsRedirect( $title, $model, $text, $target ) {
00427                 $page = $this->createPage( $title, $text, $model );
00428                 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
00429         }
00430 
00431         public static function provideIsCountable() {
00432                 return array(
00433 
00434                         // any
00435                         array( 'WikiPageTest_testIsCountable',
00436                                         CONTENT_MODEL_WIKITEXT,
00437                                         '',
00438                                         'any',
00439                                         true
00440                         ),
00441                         array( 'WikiPageTest_testIsCountable',
00442                                         CONTENT_MODEL_WIKITEXT,
00443                                         'Foo',
00444                                         'any',
00445                                         true
00446                         ),
00447 
00448                         // comma
00449                         array( 'WikiPageTest_testIsCountable',
00450                                         CONTENT_MODEL_WIKITEXT,
00451                                         'Foo',
00452                                         'comma',
00453                                         false
00454                         ),
00455                         array( 'WikiPageTest_testIsCountable',
00456                                         CONTENT_MODEL_WIKITEXT,
00457                                         'Foo, bar',
00458                                         'comma',
00459                                         true
00460                         ),
00461 
00462                         // link
00463                         array( 'WikiPageTest_testIsCountable',
00464                                         CONTENT_MODEL_WIKITEXT,
00465                                         'Foo',
00466                                         'link',
00467                                         false
00468                         ),
00469                         array( 'WikiPageTest_testIsCountable',
00470                                         CONTENT_MODEL_WIKITEXT,
00471                                         'Foo [[bar]]',
00472                                         'link',
00473                                         true
00474                         ),
00475 
00476                         // redirects
00477                         array( 'WikiPageTest_testIsCountable',
00478                                         CONTENT_MODEL_WIKITEXT,
00479                                         '#REDIRECT [[bar]]',
00480                                         'any',
00481                                         false
00482                         ),
00483                         array( 'WikiPageTest_testIsCountable',
00484                                         CONTENT_MODEL_WIKITEXT,
00485                                         '#REDIRECT [[bar]]',
00486                                         'comma',
00487                                         false
00488                         ),
00489                         array( 'WikiPageTest_testIsCountable',
00490                                         CONTENT_MODEL_WIKITEXT,
00491                                         '#REDIRECT [[bar]]',
00492                                         'link',
00493                                         false
00494                         ),
00495 
00496                         // not a content namespace
00497                         array( 'Talk:WikiPageTest_testIsCountable',
00498                                         CONTENT_MODEL_WIKITEXT,
00499                                         'Foo',
00500                                         'any',
00501                                         false
00502                         ),
00503                         array( 'Talk:WikiPageTest_testIsCountable',
00504                                         CONTENT_MODEL_WIKITEXT,
00505                                         'Foo, bar',
00506                                         'comma',
00507                                         false
00508                         ),
00509                         array( 'Talk:WikiPageTest_testIsCountable',
00510                                         CONTENT_MODEL_WIKITEXT,
00511                                         'Foo [[bar]]',
00512                                         'link',
00513                                         false
00514                         ),
00515 
00516                         // not a content namespace, different model
00517                         array( 'MediaWiki:WikiPageTest_testIsCountable.js',
00518                                         null,
00519                                         'Foo',
00520                                         'any',
00521                                         false
00522                         ),
00523                         array( 'MediaWiki:WikiPageTest_testIsCountable.js',
00524                                         null,
00525                                         'Foo, bar',
00526                                         'comma',
00527                                         false
00528                         ),
00529                         array( 'MediaWiki:WikiPageTest_testIsCountable.js',
00530                                         null,
00531                                         'Foo [[bar]]',
00532                                         'link',
00533                                         false
00534                         ),
00535                 );
00536         }
00537 
00538 
00542         public function testIsCountable( $title, $model, $text, $mode, $expected ) {
00543                 global $wgContentHandlerUseDB;
00544 
00545                 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
00546 
00547                 $title = Title::newFromText( $title );
00548 
00549                 if ( !$wgContentHandlerUseDB && ContentHandler::getDefaultModelFor( $title ) != $model ) {
00550                         $this->markTestSkipped( "Can not use non-default content model $model for "
00551                                 . $title->getPrefixedDBkey() . " with \wgArticleCountMethod disabled." );
00552                 }
00553 
00554                 $page = $this->createPage( $title, $text, $model );
00555                 $hasLinks = wfGetDB( DB_SLAVE )->selectField( 'pagelinks', 1,
00556                                         array( 'pl_from' => $page->getId() ), __METHOD__ );
00557 
00558                 $editInfo = $page->prepareContentForEdit( $page->getContent() );
00559 
00560                 $v = $page->isCountable();
00561                 $w = $page->isCountable( $editInfo );
00562 
00563                 $this->assertEquals( $expected, $v, "isCountable( null ) returned unexpected value " . var_export( $v, true )
00564                                                                                         . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
00565 
00566                 $this->assertEquals( $expected, $w, "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
00567                                                                                         . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
00568         }
00569 
00570         public static function provideGetParserOutput() {
00571                 return array(
00572                         array( CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>"),
00573                         // @todo: more...?
00574                 );
00575         }
00576 
00580         public function testGetParserOutput( $model, $text, $expectedHtml ) {
00581                 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
00582 
00583                 $opt = $page->makeParserOptions( 'canonical' );
00584                 $po = $page->getParserOutput( $opt );
00585                 $text = $po->getText();
00586 
00587                 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
00588                 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
00589 
00590                 $this->assertEquals( $expectedHtml, $text );
00591                 return $po;
00592         }
00593 
00594         public function testGetParserOutput_nonexisting( ) {
00595                 static $count = 0;
00596                 $count ++;
00597 
00598                 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
00599 
00600                 $opt = new ParserOptions();
00601                 $po = $page->getParserOutput( $opt );
00602 
00603                 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
00604         }
00605 
00606         public function testGetParserOutput_badrev( ) {
00607                 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT );
00608 
00609                 $opt = new ParserOptions();
00610                 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
00611 
00612                 //@todo: would be neat to also test deleted revision
00613 
00614                 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
00615         }
00616 
00617         static $sections =
00618 
00619                 "Intro
00620 
00621 == stuff ==
00622 hello world
00623 
00624 == test ==
00625 just a test
00626 
00627 == foo ==
00628 more stuff
00629 ";
00630 
00631 
00632         public function dataReplaceSection() {
00633                 //NOTE: assume the Help namespace to contain wikitext
00634                 return array(
00635                         array( 'Help:WikiPageTest_testReplaceSection',
00636                                         CONTENT_MODEL_WIKITEXT,
00637                                         WikiPageTest::$sections,
00638                                         "0",
00639                                         "No more",
00640                                         null,
00641                                         trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
00642                         ),
00643                         array( 'Help:WikiPageTest_testReplaceSection',
00644                                         CONTENT_MODEL_WIKITEXT,
00645                                         WikiPageTest::$sections,
00646                                         "",
00647                                         "No more",
00648                                         null,
00649                                         "No more"
00650                         ),
00651                         array( 'Help:WikiPageTest_testReplaceSection',
00652                                         CONTENT_MODEL_WIKITEXT,
00653                                         WikiPageTest::$sections,
00654                                         "2",
00655                                         "== TEST ==\nmore fun",
00656                                         null,
00657                                         trim( preg_replace( '/^== test ==.*== foo ==/sm',
00658                                                                                 "== TEST ==\nmore fun\n\n== foo ==",
00659                                                                                 WikiPageTest::$sections ) )
00660                         ),
00661                         array( 'Help:WikiPageTest_testReplaceSection',
00662                                         CONTENT_MODEL_WIKITEXT,
00663                                         WikiPageTest::$sections,
00664                                         "8",
00665                                         "No more",
00666                                         null,
00667                                         trim( WikiPageTest::$sections )
00668                         ),
00669                         array( 'Help:WikiPageTest_testReplaceSection',
00670                                         CONTENT_MODEL_WIKITEXT,
00671                                         WikiPageTest::$sections,
00672                                         "new",
00673                                         "No more",
00674                                         "New",
00675                                         trim( WikiPageTest::$sections ) . "\n\n== New ==\n\nNo more"
00676                         ),
00677                 );
00678         }
00679 
00683         public function testReplaceSection( $title, $model, $text, $section, $with, $sectionTitle, $expected ) {
00684                 $this->hideDeprecated( "WikiPage::replaceSection" );
00685 
00686                 $page = $this->createPage( $title, $text, $model );
00687                 $text = $page->replaceSection( $section, $with, $sectionTitle );
00688                 $text = trim( $text );
00689 
00690                 $this->assertEquals( $expected, $text );
00691         }
00692 
00696         public function testReplaceSectionContent( $title, $model, $text, $section, $with, $sectionTitle, $expected ) {
00697                 $page = $this->createPage( $title, $text, $model );
00698 
00699                 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
00700                 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
00701 
00702                 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
00703         }
00704 
00705         /* @todo FIXME: fix this!
00706         public function testGetUndoText() {
00707                 global $wgDiff3;
00708 
00709                 wfSuppressWarnings();
00710                 $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
00711                 wfRestoreWarnings();
00712 
00713                 if( !$haveDiff3 ) {
00714                         $this->markTestSkipped( "diff3 not installed or not found" );
00715                         return;
00716                 }
00717 
00718                 $text = "one";
00719                 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
00720                 $rev1 = $page->getRevision();
00721 
00722                 $text .= "\n\ntwo";
00723                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section two");
00724                 $rev2 = $page->getRevision();
00725 
00726                 $text .= "\n\nthree";
00727                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section three");
00728                 $rev3 = $page->getRevision();
00729 
00730                 $text .= "\n\nfour";
00731                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section four");
00732                 $rev4 = $page->getRevision();
00733 
00734                 $text .= "\n\nfive";
00735                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section five");
00736                 $rev5 = $page->getRevision();
00737 
00738                 $text .= "\n\nsix";
00739                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ), "adding section six");
00740                 $rev6 = $page->getRevision();
00741 
00742                 $undo6 = $page->getUndoText( $rev6 );
00743                 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
00744                 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
00745 
00746                 $undo3 = $page->getUndoText( $rev4, $rev2 );
00747                 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
00748                 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
00749 
00750                 $undo2 = $page->getUndoText( $rev2 );
00751                 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
00752                 $this->assertEquals( "one\n\nfive", $undo2 );
00753         }
00754         */
00755 
00759         public function broken_testDoRollback() {
00760                 $admin = new User();
00761                 $admin->setName("Admin");
00762 
00763                 $text = "one";
00764                 $page = $this->newPage( "WikiPageTest_testDoRollback" );
00765                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
00766                                                                 "section one", EDIT_NEW, false, $admin );
00767 
00768                 $user1 = new User();
00769                 $user1->setName( "127.0.1.11" );
00770                 $text .= "\n\ntwo";
00771                 $page = new WikiPage( $page->getTitle() );
00772                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
00773                                                                 "adding section two", 0, false, $user1 );
00774 
00775                 $user2 = new User();
00776                 $user2->setName( "127.0.2.13" );
00777                 $text .= "\n\nthree";
00778                 $page = new WikiPage( $page->getTitle() );
00779                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
00780                                                                 "adding section three", 0, false, $user2 );
00781 
00782                 # we are having issues with doRollback spuriously failing. apparently the last revision somehow goes missing
00783                 # or not committed under some circumstances. so, make sure the last revision has the right user name.
00784                 $dbr = wfGetDB( DB_SLAVE );
00785                 $this->assertEquals( 3, Revision::countByPageId( $dbr, $page->getId() ) );
00786 
00787                 $page = new WikiPage( $page->getTitle() );
00788                 $rev3 = $page->getRevision();
00789                 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
00790 
00791                 $rev2 = $rev3->getPrevious();
00792                 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
00793 
00794                 $rev1 = $rev2->getPrevious();
00795                 $this->assertEquals( 'Admin', $rev1->getUserText() );
00796 
00797                 # now, try the actual rollback
00798                 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
00799                 $token = $admin->getEditToken( array( $page->getTitle()->getPrefixedText(), $user2->getName() ), null );
00800                 $errors = $page->doRollback( $user2->getName(), "testing revert", $token, false, $details, $admin );
00801 
00802                 if ( $errors ) {
00803                         $this->fail( "Rollback failed:\n" . print_r( $errors, true ) . ";\n" . print_r( $details, true ) );
00804                 }
00805 
00806                 $page = new WikiPage( $page->getTitle() );
00807                 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
00808                                                                 "rollback did not revert to the correct revision" );
00809                 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
00810         }
00811 
00815         public function testDoRollback() {
00816                 $admin = new User();
00817                 $admin->setName("Admin");
00818 
00819                 $text = "one";
00820                 $page = $this->newPage( "WikiPageTest_testDoRollback" );
00821                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
00822                                                                 "section one", EDIT_NEW, false, $admin );
00823                 $rev1 = $page->getRevision();
00824 
00825                 $user1 = new User();
00826                 $user1->setName( "127.0.1.11" );
00827                 $text .= "\n\ntwo";
00828                 $page = new WikiPage( $page->getTitle() );
00829                 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
00830                                                                 "adding section two", 0, false, $user1 );
00831 
00832                 # now, try the rollback
00833                 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
00834                 $token = $admin->getEditToken( array( $page->getTitle()->getPrefixedText(), $user1->getName() ), null );
00835                 $errors = $page->doRollback( $user1->getName(), "testing revert", $token, false, $details, $admin );
00836 
00837                 if ( $errors ) {
00838                         $this->fail( "Rollback failed:\n" . print_r( $errors, true ) . ";\n" . print_r( $details, true ) );
00839                 }
00840 
00841                 $page = new WikiPage( $page->getTitle() );
00842                 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
00843                                                         "rollback did not revert to the correct revision" );
00844                 $this->assertEquals( "one", $page->getContent()->getNativeData() );
00845         }
00846 
00847         public static function provideGetAutosummary( ) {
00848                 return array(
00849                         array(
00850                                 'Hello there, world!',
00851                                 '#REDIRECT [[Foo]]',
00852                                 0,
00853                                 '/^Redirected page .*Foo/'
00854                         ),
00855 
00856                         array(
00857                                 null,
00858                                 'Hello world!',
00859                                 EDIT_NEW,
00860                                 '/^Created page .*Hello/'
00861                         ),
00862 
00863                         array(
00864                                 'Hello there, world!',
00865                                 '',
00866                                 0,
00867                                 '/^Blanked/'
00868                         ),
00869 
00870                         array(
00871                                 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut
00872                                 labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et
00873                                 ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
00874                                 'Hello world!',
00875                                 0,
00876                                 '/^Replaced .*Hello/'
00877                         ),
00878 
00879                         array(
00880                                 'foo',
00881                                 'bar',
00882                                 0,
00883                                 '/^$/'
00884                         ),
00885                 );
00886         }
00887 
00891         public function testGetAutosummary( $old, $new, $flags, $expected ) {
00892                 $this->hideDeprecated( "WikiPage::getAutosummary" );
00893 
00894                 $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
00895 
00896                 $summary = $page->getAutosummary( $old, $new, $flags );
00897 
00898                 $this->assertTrue( (bool)preg_match( $expected, $summary ),
00899                                                         "Autosummary didn't match expected pattern $expected: $summary" );
00900         }
00901 
00902         public static function provideGetAutoDeleteReason( ) {
00903                 return array(
00904                         array(
00905                                 array(),
00906                                 false,
00907                                 false
00908                         ),
00909 
00910                         array(
00911                                 array(
00912                                         array( "first edit", null ),
00913                                 ),
00914                                 "/first edit.*only contributor/",
00915                                 false
00916                         ),
00917 
00918                         array(
00919                                 array(
00920                                         array( "first edit", null ),
00921                                         array( "second edit", null ),
00922                                 ),
00923                                 "/second edit.*only contributor/",
00924                                 true
00925                         ),
00926 
00927                         array(
00928                                 array(
00929                                         array( "first edit", "127.0.2.22" ),
00930                                         array( "second edit", "127.0.3.33" ),
00931                                 ),
00932                                 "/second edit/",
00933                                 true
00934                         ),
00935 
00936                         array(
00937                                 array(
00938                                         array( "first edit: "
00939                                                  . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
00940                                                  . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. "
00941                                                  . "At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea "
00942                                                  . "takimata sanctus est Lorem ipsum dolor sit amet.'", null ),
00943                                 ),
00944                                 '/first edit:.*\.\.\."/',
00945                                 false
00946                         ),
00947 
00948                         array(
00949                                 array(
00950                                         array( "first edit", "127.0.2.22" ),
00951                                         array( "", "127.0.3.33" ),
00952                                 ),
00953                                 "/before blanking.*first edit/",
00954                                 true
00955                         ),
00956 
00957                 );
00958         }
00959 
00963         public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
00964                 global $wgUser;
00965 
00966                 //NOTE: assume Help namespace to contain wikitext
00967                 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
00968 
00969                 $c = 1;
00970 
00971                 foreach ( $edits as $edit ) {
00972                         $user = new User();
00973 
00974                         if ( !empty( $edit[1] ) ) $user->setName( $edit[1] );
00975                         else $user = $wgUser;
00976 
00977                         $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
00978 
00979                         $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
00980 
00981                         $c += 1;
00982                 }
00983 
00984                 $reason = $page->getAutoDeleteReason( $hasHistory );
00985 
00986                 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) $this->assertEquals( $expectedResult, $reason );
00987                 else $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
00988                                                                 "Autosummary didn't match expected pattern $expectedResult: $reason" );
00989 
00990                 $this->assertEquals( $expectedHistory, $hasHistory,
00991                                                         "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
00992 
00993                 $page->doDeleteArticle( "done" );
00994         }
00995 
00996         public static function providePreSaveTransform() {
00997                 return array(
00998                         array( 'hello this is ~~~',
00999                                         "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
01000                         ),
01001                         array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
01002                                         'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
01003                         ),
01004                 );
01005         }
01006 
01010         public function testPreSaveTransform( $text, $expected ) {
01011                 $this->hideDeprecated( 'WikiPage::preSaveTransform' );
01012                 $user = new User();
01013                 $user->setName("127.0.0.1");
01014 
01015                 //NOTE: assume Help namespace to contain wikitext
01016                 $page = $this->newPage( "Help:WikiPageTest_testPreloadTransform" );
01017                 $text = $page->preSaveTransform( $text, $user );
01018 
01019                 $this->assertEquals( $expected, $text );
01020         }
01021 
01022 }
01023