4 * @group ContentHandler
6 * ^--- important, causes temporary tables to be used instead of the real database
9 class WikiPageTest
extends MediaWikiLangTestCase
{
11 protected $pages_to_delete;
13 function __construct( $name = null, array $data = array(), $dataName = '' ) {
14 parent
::__construct( $name, $data, $dataName );
16 $this->tablesUsed
= array_merge(
35 protected function setUp() {
37 $this->pages_to_delete
= array();
39 LinkCache
::singleton()->clear(); # avoid cached redirect status, etc
42 protected function tearDown() {
43 foreach ( $this->pages_to_delete
as $p ) {
44 /* @var $p WikiPage */
48 $p->doDeleteArticle( "testing done." );
50 } catch ( MWException
$ex ) {
59 * @param string $model
62 protected function newPage( $title, $model = null ) {
63 if ( is_string( $title ) ) {
64 $ns = $this->getDefaultWikitextNS();
65 $title = Title
::newFromText( $title, $ns );
68 $p = new WikiPage( $title );
70 $this->pages_to_delete
[] = $p;
76 * @param string|Title|WikiPage $page
82 protected function createPage( $page, $text, $model = null ) {
83 if ( is_string( $page ) ||
$page instanceof Title
) {
84 $page = $this->newPage( $page, $model );
87 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
88 $page->doEditContent( $content, "testing", EDIT_NEW
);
94 * @covers WikiPage::doEditContent
96 public function testDoEditContent() {
97 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
98 $title = $page->getTitle();
100 $content = ContentHandler
::makeContent(
101 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
102 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
104 CONTENT_MODEL_WIKITEXT
107 $page->doEditContent( $content, "[[testing]] 1" );
109 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
110 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
111 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
112 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
114 $id = $page->getId();
116 # ------------------------
117 $dbr = wfGetDB( DB_SLAVE
);
118 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
119 $n = $res->numRows();
122 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
124 # ------------------------
125 $page = new WikiPage( $title );
127 $retrieved = $page->getContent();
128 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
130 # ------------------------
131 $content = ContentHandler
::makeContent(
132 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
133 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
135 CONTENT_MODEL_WIKITEXT
138 $page->doEditContent( $content, "testing 2" );
140 # ------------------------
141 $page = new WikiPage( $title );
143 $retrieved = $page->getContent();
144 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
146 # ------------------------
147 $dbr = wfGetDB( DB_SLAVE
);
148 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
149 $n = $res->numRows();
152 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
156 * @covers WikiPage::doEdit
158 public function testDoEdit() {
159 $this->hideDeprecated( "WikiPage::doEdit" );
160 $this->hideDeprecated( "WikiPage::getText" );
161 $this->hideDeprecated( "Revision::getText" );
163 //NOTE: assume help namespace will default to wikitext
164 $title = Title
::newFromText( "Help:WikiPageTest_testDoEdit" );
166 $page = $this->newPage( $title );
168 $text = "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
169 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.";
171 $page->doEdit( $text, "[[testing]] 1" );
173 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
174 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
175 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
176 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
178 $id = $page->getId();
180 # ------------------------
181 $dbr = wfGetDB( DB_SLAVE
);
182 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
183 $n = $res->numRows();
186 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
188 # ------------------------
189 $page = new WikiPage( $title );
191 $retrieved = $page->getText();
192 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
194 # ------------------------
195 $text = "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
196 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.";
198 $page->doEdit( $text, "testing 2" );
200 # ------------------------
201 $page = new WikiPage( $title );
203 $retrieved = $page->getText();
204 $this->assertEquals( $text, $retrieved, 'retrieved text doesn\'t equal original' );
206 # ------------------------
207 $dbr = wfGetDB( DB_SLAVE
);
208 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
209 $n = $res->numRows();
212 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
216 * @covers WikiPage::doQuickEdit
218 public function testDoQuickEdit() {
221 $this->hideDeprecated( "WikiPage::doQuickEdit" );
223 //NOTE: assume help namespace will default to wikitext
224 $page = $this->createPage( "Help:WikiPageTest_testDoQuickEdit", "original text" );
226 $text = "quick text";
227 $page->doQuickEdit( $text, $wgUser, "testing q" );
229 # ---------------------
230 $page = new WikiPage( $page->getTitle() );
231 $this->assertEquals( $text, $page->getText() );
235 * @covers WikiPage::doQuickEditContent
237 public function testDoQuickEditContent() {
240 $page = $this->createPage(
241 "WikiPageTest_testDoQuickEditContent",
243 CONTENT_MODEL_WIKITEXT
246 $content = ContentHandler
::makeContent(
249 CONTENT_MODEL_WIKITEXT
251 $page->doQuickEditContent( $content, $wgUser, "testing q" );
253 # ---------------------
254 $page = new WikiPage( $page->getTitle() );
255 $this->assertTrue( $content->equals( $page->getContent() ) );
259 * @covers WikiPage::doDeleteArticle
261 public function testDoDeleteArticle() {
262 $page = $this->createPage(
263 "WikiPageTest_testDoDeleteArticle",
264 "[[original text]] foo",
265 CONTENT_MODEL_WIKITEXT
267 $id = $page->getId();
269 $page->doDeleteArticle( "testing deletion" );
272 $page->getTitle()->getArticleID() > 0,
273 "Title object should now have page id 0"
275 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
278 "WikiPage::exists should return false after page was deleted"
282 "WikiPage::getContent should return null after page was deleted"
286 "WikiPage::getText should return false after page was deleted"
289 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
292 "Title::exists should return false after page was deleted"
295 # ------------------------
296 $dbr = wfGetDB( DB_SLAVE
);
297 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
298 $n = $res->numRows();
301 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
305 * @covers WikiPage::doDeleteUpdates
307 public function testDoDeleteUpdates() {
308 $page = $this->createPage(
309 "WikiPageTest_testDoDeleteArticle",
310 "[[original text]] foo",
311 CONTENT_MODEL_WIKITEXT
313 $id = $page->getId();
315 $page->doDeleteUpdates( $id );
317 # ------------------------
318 $dbr = wfGetDB( DB_SLAVE
);
319 $res = $dbr->select( 'pagelinks', '*', array( 'pl_from' => $id ) );
320 $n = $res->numRows();
323 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
327 * @covers WikiPage::getRevision
329 public function testGetRevision() {
330 $page = $this->newPage( "WikiPageTest_testGetRevision" );
332 $rev = $page->getRevision();
333 $this->assertNull( $rev );
336 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
338 $rev = $page->getRevision();
340 $this->assertEquals( $page->getLatest(), $rev->getId() );
341 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
345 * @covers WikiPage::getContent
347 public function testGetContent() {
348 $page = $this->newPage( "WikiPageTest_testGetContent" );
350 $content = $page->getContent();
351 $this->assertNull( $content );
354 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
356 $content = $page->getContent();
357 $this->assertEquals( "some text", $content->getNativeData() );
361 * @covers WikiPage::getText
363 public function testGetText() {
364 $this->hideDeprecated( "WikiPage::getText" );
366 $page = $this->newPage( "WikiPageTest_testGetText" );
368 $text = $page->getText();
369 $this->assertFalse( $text );
372 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
374 $text = $page->getText();
375 $this->assertEquals( "some text", $text );
379 * @covers WikiPage::getRawText
381 public function testGetRawText() {
382 $this->hideDeprecated( "WikiPage::getRawText" );
384 $page = $this->newPage( "WikiPageTest_testGetRawText" );
386 $text = $page->getRawText();
387 $this->assertFalse( $text );
390 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
392 $text = $page->getRawText();
393 $this->assertEquals( "some text", $text );
397 * @covers WikiPage::getContentModel
399 public function testGetContentModel() {
400 global $wgContentHandlerUseDB;
402 if ( !$wgContentHandlerUseDB ) {
403 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
406 $page = $this->createPage(
407 "WikiPageTest_testGetContentModel",
409 CONTENT_MODEL_JAVASCRIPT
412 $page = new WikiPage( $page->getTitle() );
413 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $page->getContentModel() );
417 * @covers WikiPage::getContentHandler
419 public function testGetContentHandler() {
420 global $wgContentHandlerUseDB;
422 if ( !$wgContentHandlerUseDB ) {
423 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
426 $page = $this->createPage(
427 "WikiPageTest_testGetContentHandler",
429 CONTENT_MODEL_JAVASCRIPT
432 $page = new WikiPage( $page->getTitle() );
433 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
437 * @covers WikiPage::exists
439 public function testExists() {
440 $page = $this->newPage( "WikiPageTest_testExists" );
441 $this->assertFalse( $page->exists() );
444 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
445 $this->assertTrue( $page->exists() );
447 $page = new WikiPage( $page->getTitle() );
448 $this->assertTrue( $page->exists() );
451 $page->doDeleteArticle( "done testing" );
452 $this->assertFalse( $page->exists() );
454 $page = new WikiPage( $page->getTitle() );
455 $this->assertFalse( $page->exists() );
458 public static function provideHasViewableContent() {
460 array( 'WikiPageTest_testHasViewableContent', false, true ),
461 array( 'Special:WikiPageTest_testHasViewableContent', false ),
462 array( 'MediaWiki:WikiPageTest_testHasViewableContent', false ),
463 array( 'Special:Userlogin', true ),
464 array( 'MediaWiki:help', true ),
469 * @dataProvider provideHasViewableContent
470 * @covers WikiPage::hasViewableContent
472 public function testHasViewableContent( $title, $viewable, $create = false ) {
473 $page = $this->newPage( $title );
474 $this->assertEquals( $viewable, $page->hasViewableContent() );
477 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
478 $this->assertTrue( $page->hasViewableContent() );
480 $page = new WikiPage( $page->getTitle() );
481 $this->assertTrue( $page->hasViewableContent() );
485 public static function provideGetRedirectTarget() {
487 array( 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ),
489 'WikiPageTest_testGetRedirectTarget_2',
490 CONTENT_MODEL_WIKITEXT
,
491 "#REDIRECT [[hello world]]",
498 * @dataProvider provideGetRedirectTarget
499 * @covers WikiPage::getRedirectTarget
501 public function testGetRedirectTarget( $title, $model, $text, $target ) {
502 $page = $this->createPage( $title, $text, $model );
504 # sanity check, because this test seems to fail for no reason for some people.
505 $c = $page->getContent();
506 $this->assertEquals( 'WikitextContent', get_class( $c ) );
508 # now, test the actual redirect
509 $t = $page->getRedirectTarget();
510 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
514 * @dataProvider provideGetRedirectTarget
515 * @covers WikiPage::isRedirect
517 public function testIsRedirect( $title, $model, $text, $target ) {
518 $page = $this->createPage( $title, $text, $model );
519 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
522 public static function provideIsCountable() {
526 array( 'WikiPageTest_testIsCountable',
527 CONTENT_MODEL_WIKITEXT
,
532 array( 'WikiPageTest_testIsCountable',
533 CONTENT_MODEL_WIKITEXT
,
540 array( 'WikiPageTest_testIsCountable',
541 CONTENT_MODEL_WIKITEXT
,
546 array( 'WikiPageTest_testIsCountable',
547 CONTENT_MODEL_WIKITEXT
,
554 array( 'WikiPageTest_testIsCountable',
555 CONTENT_MODEL_WIKITEXT
,
560 array( 'WikiPageTest_testIsCountable',
561 CONTENT_MODEL_WIKITEXT
,
568 array( 'WikiPageTest_testIsCountable',
569 CONTENT_MODEL_WIKITEXT
,
574 array( 'WikiPageTest_testIsCountable',
575 CONTENT_MODEL_WIKITEXT
,
580 array( 'WikiPageTest_testIsCountable',
581 CONTENT_MODEL_WIKITEXT
,
587 // not a content namespace
588 array( 'Talk:WikiPageTest_testIsCountable',
589 CONTENT_MODEL_WIKITEXT
,
594 array( 'Talk:WikiPageTest_testIsCountable',
595 CONTENT_MODEL_WIKITEXT
,
600 array( 'Talk:WikiPageTest_testIsCountable',
601 CONTENT_MODEL_WIKITEXT
,
607 // not a content namespace, different model
608 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
614 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
620 array( 'MediaWiki:WikiPageTest_testIsCountable.js',
630 * @dataProvider provideIsCountable
631 * @covers WikiPage::isCountable
633 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
634 global $wgContentHandlerUseDB;
636 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
638 $title = Title
::newFromText( $title );
640 if ( !$wgContentHandlerUseDB
642 && ContentHandler
::getDefaultModelFor( $title ) != $model
644 $this->markTestSkipped( "Can not use non-default content model $model for "
645 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
648 $page = $this->createPage( $title, $text, $model );
650 $editInfo = $page->prepareContentForEdit( $page->getContent() );
652 $v = $page->isCountable();
653 $w = $page->isCountable( $editInfo );
658 "isCountable( null ) returned unexpected value " . var_export( $v, true )
659 . " instead of " . var_export( $expected, true )
660 . " in mode `$mode` for text \"$text\""
666 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
667 . " instead of " . var_export( $expected, true )
668 . " in mode `$mode` for text \"$text\""
672 public static function provideGetParserOutput() {
674 array( CONTENT_MODEL_WIKITEXT
, "hello ''world''\n", "<p>hello <i>world</i></p>" ),
680 * @dataProvider provideGetParserOutput
681 * @covers WikiPage::getParserOutput
683 public function testGetParserOutput( $model, $text, $expectedHtml ) {
684 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
686 $opt = $page->makeParserOptions( 'canonical' );
687 $po = $page->getParserOutput( $opt );
688 $text = $po->getText();
690 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
691 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
693 $this->assertEquals( $expectedHtml, $text );
699 * @covers WikiPage::getParserOutput
701 public function testGetParserOutput_nonexisting() {
705 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
707 $opt = new ParserOptions();
708 $po = $page->getParserOutput( $opt );
710 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
714 * @covers WikiPage::getParserOutput
716 public function testGetParserOutput_badrev() {
717 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT
);
719 $opt = new ParserOptions();
720 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
722 // @todo would be neat to also test deleted revision
724 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
727 public static $sections =
741 public function dataReplaceSection() {
742 //NOTE: assume the Help namespace to contain wikitext
744 array( 'Help:WikiPageTest_testReplaceSection',
745 CONTENT_MODEL_WIKITEXT
,
746 WikiPageTest
::$sections,
750 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest
::$sections ) )
752 array( 'Help:WikiPageTest_testReplaceSection',
753 CONTENT_MODEL_WIKITEXT
,
754 WikiPageTest
::$sections,
760 array( 'Help:WikiPageTest_testReplaceSection',
761 CONTENT_MODEL_WIKITEXT
,
762 WikiPageTest
::$sections,
764 "== TEST ==\nmore fun",
766 trim( preg_replace( '/^== test ==.*== foo ==/sm',
767 "== TEST ==\nmore fun\n\n== foo ==",
768 WikiPageTest
::$sections ) )
770 array( 'Help:WikiPageTest_testReplaceSection',
771 CONTENT_MODEL_WIKITEXT
,
772 WikiPageTest
::$sections,
776 trim( WikiPageTest
::$sections )
778 array( 'Help:WikiPageTest_testReplaceSection',
779 CONTENT_MODEL_WIKITEXT
,
780 WikiPageTest
::$sections,
784 trim( WikiPageTest
::$sections ) . "\n\n== New ==\n\nNo more"
790 * @dataProvider dataReplaceSection
791 * @covers WikiPage::replaceSection
793 public function testReplaceSection( $title, $model, $text, $section, $with,
794 $sectionTitle, $expected
796 $this->hideDeprecated( "WikiPage::replaceSection" );
798 $page = $this->createPage( $title, $text, $model );
799 $text = $page->replaceSection( $section, $with, $sectionTitle );
800 $text = trim( $text );
802 $this->assertEquals( $expected, $text );
806 * @dataProvider dataReplaceSection
807 * @covers WikiPage::replaceSectionContent
809 public function testReplaceSectionContent( $title, $model, $text, $section,
810 $with, $sectionTitle, $expected
812 $page = $this->createPage( $title, $text, $model );
814 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
815 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
817 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
820 /* @todo FIXME: fix this!
821 public function testGetUndoText() {
822 $this->checkHasDiff3();
825 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
826 $rev1 = $page->getRevision();
829 $page->doEditContent(
830 ContentHandler::makeContent( $text, $page->getTitle() ),
833 $rev2 = $page->getRevision();
835 $text .= "\n\nthree";
836 $page->doEditContent(
837 ContentHandler::makeContent( $text, $page->getTitle() ),
838 "adding section three"
840 $rev3 = $page->getRevision();
843 $page->doEditContent(
844 ContentHandler::makeContent( $text, $page->getTitle() ),
845 "adding section four"
847 $rev4 = $page->getRevision();
850 $page->doEditContent(
851 ContentHandler::makeContent( $text, $page->getTitle() ),
852 "adding section five"
854 $rev5 = $page->getRevision();
857 $page->doEditContent(
858 ContentHandler::makeContent( $text, $page->getTitle() ),
861 $rev6 = $page->getRevision();
863 $undo6 = $page->getUndoText( $rev6 );
864 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
865 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
867 $undo3 = $page->getUndoText( $rev4, $rev2 );
868 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
869 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
871 $undo2 = $page->getUndoText( $rev2 );
872 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
873 $this->assertEquals( "one\n\nfive", $undo2 );
878 * @todo FIXME: this is a better rollback test than the one below, but it
879 * keeps failing in jenkins for some reason.
881 public function broken_testDoRollback() {
883 $admin->setName( "Admin" );
886 $page = $this->newPage( "WikiPageTest_testDoRollback" );
887 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
888 "section one", EDIT_NEW
, false, $admin );
891 $user1->setName( "127.0.1.11" );
893 $page = new WikiPage( $page->getTitle() );
894 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
895 "adding section two", 0, false, $user1 );
898 $user2->setName( "127.0.2.13" );
899 $text .= "\n\nthree";
900 $page = new WikiPage( $page->getTitle() );
901 $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
902 "adding section three", 0, false, $user2 );
904 # we are having issues with doRollback spuriously failing. Apparently
905 # the last revision somehow goes missing or not committed under some
906 # circumstances. So, make sure the last revision has the right user name.
907 $dbr = wfGetDB( DB_SLAVE
);
908 $this->assertEquals( 3, Revision
::countByPageId( $dbr, $page->getId() ) );
910 $page = new WikiPage( $page->getTitle() );
911 $rev3 = $page->getRevision();
912 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
914 $rev2 = $rev3->getPrevious();
915 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
917 $rev1 = $rev2->getPrevious();
918 $this->assertEquals( 'Admin', $rev1->getUserText() );
920 # now, try the actual rollback
921 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
922 $token = $admin->getEditToken(
923 array( $page->getTitle()->getPrefixedText(), $user2->getName() ),
926 $errors = $page->doRollback(
936 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
937 . ";\n" . print_r( $details, true ) );
940 $page = new WikiPage( $page->getTitle() );
941 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
942 "rollback did not revert to the correct revision" );
943 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
947 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
948 * @covers WikiPage::doRollback
950 public function testDoRollback() {
952 $admin->setName( "Admin" );
955 $page = $this->newPage( "WikiPageTest_testDoRollback" );
956 $page->doEditContent(
957 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
963 $rev1 = $page->getRevision();
966 $user1->setName( "127.0.1.11" );
968 $page = new WikiPage( $page->getTitle() );
969 $page->doEditContent(
970 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
971 "adding section two",
977 # now, try the rollback
978 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
979 $token = $admin->getEditToken(
980 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
983 $errors = $page->doRollback(
993 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
994 . ";\n" . print_r( $details, true ) );
997 $page = new WikiPage( $page->getTitle() );
998 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
999 "rollback did not revert to the correct revision" );
1000 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1004 * @covers WikiPage::doRollback
1006 public function testDoRollbackFailureSameContent() {
1007 $admin = new User();
1008 $admin->setName( "Admin" );
1009 $admin->addGroup( "sysop" ); #XXX: make the test user a sysop...
1012 $page = $this->newPage( "WikiPageTest_testDoRollback" );
1013 $page->doEditContent(
1014 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1020 $rev1 = $page->getRevision();
1022 $user1 = new User();
1023 $user1->setName( "127.0.1.11" );
1024 $user1->addGroup( "sysop" ); #XXX: make the test user a sysop...
1026 $page = new WikiPage( $page->getTitle() );
1027 $page->doEditContent(
1028 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1029 "adding section two",
1035 # now, do a the rollback from the same user was doing the edit before
1036 $resultDetails = array();
1037 $token = $user1->getEditToken(
1038 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1041 $errors = $page->doRollback(
1043 "testing revert same user",
1050 $this->assertEquals( array(), $errors, "Rollback failed same user" );
1052 # now, try the rollback
1053 $resultDetails = array();
1054 $token = $admin->getEditToken(
1055 array( $page->getTitle()->getPrefixedText(), $user1->getName() ),
1058 $errors = $page->doRollback(
1067 $this->assertEquals( array( array( 'alreadyrolled', 'WikiPageTest testDoRollback',
1068 '127.0.1.11', 'Admin' ) ), $errors, "Rollback not failed" );
1070 $page = new WikiPage( $page->getTitle() );
1071 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1072 "rollback did not revert to the correct revision" );
1073 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1076 public static function provideGetAutosummary() {
1079 'Hello there, world!',
1080 '#REDIRECT [[Foo]]',
1082 '/^Redirected page .*Foo/'
1089 '/^Created page .*Hello/'
1093 'Hello there, world!',
1100 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy
1101 eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
1102 voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet
1103 clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.',
1106 '/^Replaced .*Hello/'
1119 * @dataProvider provideGetAutoSummary
1120 * @covers WikiPage::getAutosummary
1122 public function testGetAutosummary( $old, $new, $flags, $expected ) {
1123 $this->hideDeprecated( "WikiPage::getAutosummary" );
1125 $page = $this->newPage( "WikiPageTest_testGetAutosummary" );
1127 $summary = $page->getAutosummary( $old, $new, $flags );
1129 $this->assertTrue( (bool)preg_match( $expected, $summary ),
1130 "Autosummary didn't match expected pattern $expected: $summary" );
1133 public static function provideGetAutoDeleteReason() {
1143 array( "first edit", null ),
1145 "/first edit.*only contributor/",
1151 array( "first edit", null ),
1152 array( "second edit", null ),
1154 "/second edit.*only contributor/",
1160 array( "first edit", "127.0.2.22" ),
1161 array( "second edit", "127.0.3.33" ),
1171 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1172 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1173 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1174 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1175 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1179 '/first edit:.*\.\.\."/',
1185 array( "first edit", "127.0.2.22" ),
1186 array( "", "127.0.3.33" ),
1188 "/before blanking.*first edit/",
1196 * @dataProvider provideGetAutoDeleteReason
1197 * @covers WikiPage::getAutoDeleteReason
1199 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1202 //NOTE: assume Help namespace to contain wikitext
1203 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1207 foreach ( $edits as $edit ) {
1210 if ( !empty( $edit[1] ) ) {
1211 $user->setName( $edit[1] );
1216 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1218 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1223 $reason = $page->getAutoDeleteReason( $hasHistory );
1225 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1226 $this->assertEquals( $expectedResult, $reason );
1228 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1229 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1232 $this->assertEquals( $expectedHistory, $hasHistory,
1233 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1235 $page->doDeleteArticle( "done" );
1238 public static function providePreSaveTransform() {
1240 array( 'hello this is ~~~',
1241 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1243 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1244 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1250 * @dataProvider providePreSaveTransform
1251 * @covers WikiPage::preSaveTransform
1253 public function testPreSaveTransform( $text, $expected ) {
1254 $this->hideDeprecated( 'WikiPage::preSaveTransform' );
1256 $user->setName( "127.0.0.1" );
1258 //NOTE: assume Help namespace to contain wikitext
1259 $page = $this->newPage( "Help:WikiPageTest_testPreloadTransform" );
1260 $text = $page->preSaveTransform( $text, $user );
1262 $this->assertEquals( $expected, $text );
1266 * @covers WikiPage::factory
1268 public function testWikiPageFactory() {
1269 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1270 $page = WikiPage
::factory( $title );
1271 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1273 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1274 $page = WikiPage
::factory( $title );
1275 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1277 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1278 $page = WikiPage
::factory( $title );
1279 $this->assertEquals( 'WikiPage', get_class( $page ) );