Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / page / WikiPageTest.php
blob6b911bf3a67af38937afa0febf158add65f55635
1 <?php
3 /**
4 * @group ContentHandler
5 * @group Database
6 * ^--- important, causes temporary tables to be used instead of the real database
7 * @group medium
8 */
9 class WikiPageTest extends MediaWikiLangTestCase {
11 protected $pages_to_delete;
13 function __construct( $name = null, array $data = [], $dataName = '' ) {
14 parent::__construct( $name, $data, $dataName );
16 $this->tablesUsed = array_merge(
17 $this->tablesUsed,
18 [ 'page',
19 'revision',
20 'text',
22 'recentchanges',
23 'logging',
25 'page_props',
26 'pagelinks',
27 'categorylinks',
28 'langlinks',
29 'externallinks',
30 'imagelinks',
31 'templatelinks',
32 'iwlinks' ] );
35 protected function setUp() {
36 parent::setUp();
37 $this->pages_to_delete = [];
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 */
46 try {
47 if ( $p->exists() ) {
48 $p->doDeleteArticle( "testing done." );
50 } catch ( MWException $ex ) {
51 // fail silently
54 parent::tearDown();
57 /**
58 * @param Title|string $title
59 * @param string|null $model
60 * @return WikiPage
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;
72 return $p;
75 /**
76 * @param string|Title|WikiPage $page
77 * @param string $text
78 * @param int $model
80 * @return WikiPage
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 );
90 return $page;
93 /**
94 * @covers WikiPage::doEditContent
95 * @covers WikiPage::doModify
96 * @covers WikiPage::doCreate
97 * @covers WikiPage::doEditUpdates
99 public function testDoEditContent() {
100 $page = $this->newPage( "WikiPageTest_testDoEditContent" );
101 $title = $page->getTitle();
103 $content = ContentHandler::makeContent(
104 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
105 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
106 $title,
107 CONTENT_MODEL_WIKITEXT
110 $page->doEditContent( $content, "[[testing]] 1" );
112 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
113 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
114 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
115 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
117 $id = $page->getId();
119 # ------------------------
120 $dbr = wfGetDB( DB_SLAVE );
121 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
122 $n = $res->numRows();
123 $res->free();
125 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
127 # ------------------------
128 $page = new WikiPage( $title );
130 $retrieved = $page->getContent();
131 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
133 # ------------------------
134 $content = ContentHandler::makeContent(
135 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
136 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
137 $title,
138 CONTENT_MODEL_WIKITEXT
141 $page->doEditContent( $content, "testing 2" );
143 # ------------------------
144 $page = new WikiPage( $title );
146 $retrieved = $page->getContent();
147 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
149 # ------------------------
150 $dbr = wfGetDB( DB_SLAVE );
151 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
152 $n = $res->numRows();
153 $res->free();
155 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
159 * @covers WikiPage::doDeleteArticle
161 public function testDoDeleteArticle() {
162 $page = $this->createPage(
163 "WikiPageTest_testDoDeleteArticle",
164 "[[original text]] foo",
165 CONTENT_MODEL_WIKITEXT
167 $id = $page->getId();
169 $page->doDeleteArticle( "testing deletion" );
171 $this->assertFalse(
172 $page->getTitle()->getArticleID() > 0,
173 "Title object should now have page id 0"
175 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
176 $this->assertFalse(
177 $page->exists(),
178 "WikiPage::exists should return false after page was deleted"
180 $this->assertNull(
181 $page->getContent(),
182 "WikiPage::getContent should return null after page was deleted"
185 $t = Title::newFromText( $page->getTitle()->getPrefixedText() );
186 $this->assertFalse(
187 $t->exists(),
188 "Title::exists should return false after page was deleted"
191 // Run the job queue
192 JobQueueGroup::destroySingletons();
193 $jobs = new RunJobs;
194 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
195 $jobs->execute();
197 # ------------------------
198 $dbr = wfGetDB( DB_SLAVE );
199 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
200 $n = $res->numRows();
201 $res->free();
203 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
207 * @covers WikiPage::doDeleteUpdates
209 public function testDoDeleteUpdates() {
210 $page = $this->createPage(
211 "WikiPageTest_testDoDeleteArticle",
212 "[[original text]] foo",
213 CONTENT_MODEL_WIKITEXT
215 $id = $page->getId();
217 // Similar to MovePage logic
218 wfGetDB( DB_MASTER )->delete( 'page', [ 'page_id' => $id ], __METHOD__ );
219 $page->doDeleteUpdates( $id );
221 // Run the job queue
222 JobQueueGroup::destroySingletons();
223 $jobs = new RunJobs;
224 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
225 $jobs->execute();
227 # ------------------------
228 $dbr = wfGetDB( DB_SLAVE );
229 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
230 $n = $res->numRows();
231 $res->free();
233 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
237 * @covers WikiPage::getRevision
239 public function testGetRevision() {
240 $page = $this->newPage( "WikiPageTest_testGetRevision" );
242 $rev = $page->getRevision();
243 $this->assertNull( $rev );
245 # -----------------
246 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
248 $rev = $page->getRevision();
250 $this->assertEquals( $page->getLatest(), $rev->getId() );
251 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
255 * @covers WikiPage::getContent
257 public function testGetContent() {
258 $page = $this->newPage( "WikiPageTest_testGetContent" );
260 $content = $page->getContent();
261 $this->assertNull( $content );
263 # -----------------
264 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
266 $content = $page->getContent();
267 $this->assertEquals( "some text", $content->getNativeData() );
271 * @covers WikiPage::getContentModel
273 public function testGetContentModel() {
274 global $wgContentHandlerUseDB;
276 if ( !$wgContentHandlerUseDB ) {
277 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
280 $page = $this->createPage(
281 "WikiPageTest_testGetContentModel",
282 "some text",
283 CONTENT_MODEL_JAVASCRIPT
286 $page = new WikiPage( $page->getTitle() );
287 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $page->getContentModel() );
291 * @covers WikiPage::getContentHandler
293 public function testGetContentHandler() {
294 global $wgContentHandlerUseDB;
296 if ( !$wgContentHandlerUseDB ) {
297 $this->markTestSkipped( '$wgContentHandlerUseDB is disabled' );
300 $page = $this->createPage(
301 "WikiPageTest_testGetContentHandler",
302 "some text",
303 CONTENT_MODEL_JAVASCRIPT
306 $page = new WikiPage( $page->getTitle() );
307 $this->assertEquals( 'JavaScriptContentHandler', get_class( $page->getContentHandler() ) );
311 * @covers WikiPage::exists
313 public function testExists() {
314 $page = $this->newPage( "WikiPageTest_testExists" );
315 $this->assertFalse( $page->exists() );
317 # -----------------
318 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
319 $this->assertTrue( $page->exists() );
321 $page = new WikiPage( $page->getTitle() );
322 $this->assertTrue( $page->exists() );
324 # -----------------
325 $page->doDeleteArticle( "done testing" );
326 $this->assertFalse( $page->exists() );
328 $page = new WikiPage( $page->getTitle() );
329 $this->assertFalse( $page->exists() );
332 public static function provideHasViewableContent() {
333 return [
334 [ 'WikiPageTest_testHasViewableContent', false, true ],
335 [ 'Special:WikiPageTest_testHasViewableContent', false ],
336 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
337 [ 'Special:Userlogin', true ],
338 [ 'MediaWiki:help', true ],
343 * @dataProvider provideHasViewableContent
344 * @covers WikiPage::hasViewableContent
346 public function testHasViewableContent( $title, $viewable, $create = false ) {
347 $page = $this->newPage( $title );
348 $this->assertEquals( $viewable, $page->hasViewableContent() );
350 if ( $create ) {
351 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT );
352 $this->assertTrue( $page->hasViewableContent() );
354 $page = new WikiPage( $page->getTitle() );
355 $this->assertTrue( $page->hasViewableContent() );
359 public static function provideGetRedirectTarget() {
360 return [
361 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT, "hello world", null ],
363 'WikiPageTest_testGetRedirectTarget_2',
364 CONTENT_MODEL_WIKITEXT,
365 "#REDIRECT [[hello world]]",
366 "Hello world"
372 * @dataProvider provideGetRedirectTarget
373 * @covers WikiPage::getRedirectTarget
375 public function testGetRedirectTarget( $title, $model, $text, $target ) {
376 $this->setMwGlobals( [
377 'wgCapitalLinks' => true,
378 ] );
380 $page = $this->createPage( $title, $text, $model );
382 # sanity check, because this test seems to fail for no reason for some people.
383 $c = $page->getContent();
384 $this->assertEquals( 'WikitextContent', get_class( $c ) );
386 # now, test the actual redirect
387 $t = $page->getRedirectTarget();
388 $this->assertEquals( $target, is_null( $t ) ? null : $t->getPrefixedText() );
392 * @dataProvider provideGetRedirectTarget
393 * @covers WikiPage::isRedirect
395 public function testIsRedirect( $title, $model, $text, $target ) {
396 $page = $this->createPage( $title, $text, $model );
397 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
400 public static function provideIsCountable() {
401 return [
403 // any
404 [ 'WikiPageTest_testIsCountable',
405 CONTENT_MODEL_WIKITEXT,
407 'any',
408 true
410 [ 'WikiPageTest_testIsCountable',
411 CONTENT_MODEL_WIKITEXT,
412 'Foo',
413 'any',
414 true
417 // comma
418 [ 'WikiPageTest_testIsCountable',
419 CONTENT_MODEL_WIKITEXT,
420 'Foo',
421 'comma',
422 false
424 [ 'WikiPageTest_testIsCountable',
425 CONTENT_MODEL_WIKITEXT,
426 'Foo, bar',
427 'comma',
428 true
431 // link
432 [ 'WikiPageTest_testIsCountable',
433 CONTENT_MODEL_WIKITEXT,
434 'Foo',
435 'link',
436 false
438 [ 'WikiPageTest_testIsCountable',
439 CONTENT_MODEL_WIKITEXT,
440 'Foo [[bar]]',
441 'link',
442 true
445 // redirects
446 [ 'WikiPageTest_testIsCountable',
447 CONTENT_MODEL_WIKITEXT,
448 '#REDIRECT [[bar]]',
449 'any',
450 false
452 [ 'WikiPageTest_testIsCountable',
453 CONTENT_MODEL_WIKITEXT,
454 '#REDIRECT [[bar]]',
455 'comma',
456 false
458 [ 'WikiPageTest_testIsCountable',
459 CONTENT_MODEL_WIKITEXT,
460 '#REDIRECT [[bar]]',
461 'link',
462 false
465 // not a content namespace
466 [ 'Talk:WikiPageTest_testIsCountable',
467 CONTENT_MODEL_WIKITEXT,
468 'Foo',
469 'any',
470 false
472 [ 'Talk:WikiPageTest_testIsCountable',
473 CONTENT_MODEL_WIKITEXT,
474 'Foo, bar',
475 'comma',
476 false
478 [ 'Talk:WikiPageTest_testIsCountable',
479 CONTENT_MODEL_WIKITEXT,
480 'Foo [[bar]]',
481 'link',
482 false
485 // not a content namespace, different model
486 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
487 null,
488 'Foo',
489 'any',
490 false
492 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
493 null,
494 'Foo, bar',
495 'comma',
496 false
498 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
499 null,
500 'Foo [[bar]]',
501 'link',
502 false
508 * @dataProvider provideIsCountable
509 * @covers WikiPage::isCountable
511 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
512 global $wgContentHandlerUseDB;
514 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
516 $title = Title::newFromText( $title );
518 if ( !$wgContentHandlerUseDB
519 && $model
520 && ContentHandler::getDefaultModelFor( $title ) != $model
522 $this->markTestSkipped( "Can not use non-default content model $model for "
523 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
526 $page = $this->createPage( $title, $text, $model );
528 $editInfo = $page->prepareContentForEdit( $page->getContent() );
530 $v = $page->isCountable();
531 $w = $page->isCountable( $editInfo );
533 $this->assertEquals(
534 $expected,
536 "isCountable( null ) returned unexpected value " . var_export( $v, true )
537 . " instead of " . var_export( $expected, true )
538 . " in mode `$mode` for text \"$text\""
541 $this->assertEquals(
542 $expected,
544 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
545 . " instead of " . var_export( $expected, true )
546 . " in mode `$mode` for text \"$text\""
550 public static function provideGetParserOutput() {
551 return [
552 [ CONTENT_MODEL_WIKITEXT, "hello ''world''\n", "<p>hello <i>world</i></p>" ],
553 // @todo more...?
558 * @dataProvider provideGetParserOutput
559 * @covers WikiPage::getParserOutput
561 public function testGetParserOutput( $model, $text, $expectedHtml ) {
562 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', $text, $model );
564 $opt = $page->makeParserOptions( 'canonical' );
565 $po = $page->getParserOutput( $opt );
566 $text = $po->getText();
568 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
569 $text = preg_replace( '!\s*(</p>)!sm', '\1', $text ); # don't let tidy confuse us
571 $this->assertEquals( $expectedHtml, $text );
573 return $po;
577 * @covers WikiPage::getParserOutput
579 public function testGetParserOutput_nonexisting() {
580 static $count = 0;
581 $count++;
583 $page = new WikiPage( new Title( "WikiPageTest_testGetParserOutput_nonexisting_$count" ) );
585 $opt = new ParserOptions();
586 $po = $page->getParserOutput( $opt );
588 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
592 * @covers WikiPage::getParserOutput
594 public function testGetParserOutput_badrev() {
595 $page = $this->createPage( 'WikiPageTest_testGetParserOutput', "dummy", CONTENT_MODEL_WIKITEXT );
597 $opt = new ParserOptions();
598 $po = $page->getParserOutput( $opt, $page->getLatest() + 1234 );
600 // @todo would be neat to also test deleted revision
602 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
605 public static $sections =
607 "Intro
609 == stuff ==
610 hello world
612 == test ==
613 just a test
615 == foo ==
616 more stuff
619 public function dataReplaceSection() {
620 // NOTE: assume the Help namespace to contain wikitext
621 return [
622 [ 'Help:WikiPageTest_testReplaceSection',
623 CONTENT_MODEL_WIKITEXT,
624 WikiPageTest::$sections,
625 "0",
626 "No more",
627 null,
628 trim( preg_replace( '/^Intro/sm', 'No more', WikiPageTest::$sections ) )
630 [ 'Help:WikiPageTest_testReplaceSection',
631 CONTENT_MODEL_WIKITEXT,
632 WikiPageTest::$sections,
634 "No more",
635 null,
636 "No more"
638 [ 'Help:WikiPageTest_testReplaceSection',
639 CONTENT_MODEL_WIKITEXT,
640 WikiPageTest::$sections,
641 "2",
642 "== TEST ==\nmore fun",
643 null,
644 trim( preg_replace( '/^== test ==.*== foo ==/sm',
645 "== TEST ==\nmore fun\n\n== foo ==",
646 WikiPageTest::$sections ) )
648 [ 'Help:WikiPageTest_testReplaceSection',
649 CONTENT_MODEL_WIKITEXT,
650 WikiPageTest::$sections,
651 "8",
652 "No more",
653 null,
654 trim( WikiPageTest::$sections )
656 [ 'Help:WikiPageTest_testReplaceSection',
657 CONTENT_MODEL_WIKITEXT,
658 WikiPageTest::$sections,
659 "new",
660 "No more",
661 "New",
662 trim( WikiPageTest::$sections ) . "\n\n== New ==\n\nNo more"
668 * @dataProvider dataReplaceSection
669 * @covers WikiPage::replaceSectionContent
671 public function testReplaceSectionContent( $title, $model, $text, $section,
672 $with, $sectionTitle, $expected
674 $page = $this->createPage( $title, $text, $model );
676 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
677 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
679 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
683 * @dataProvider dataReplaceSection
684 * @covers WikiPage::replaceSectionAtRev
686 public function testReplaceSectionAtRev( $title, $model, $text, $section,
687 $with, $sectionTitle, $expected
689 $page = $this->createPage( $title, $text, $model );
690 $baseRevId = $page->getLatest();
692 $content = ContentHandler::makeContent( $with, $page->getTitle(), $page->getContentModel() );
693 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
695 $this->assertEquals( $expected, is_null( $c ) ? null : trim( $c->getNativeData() ) );
698 /* @todo FIXME: fix this!
699 public function testGetUndoText() {
700 $this->markTestSkippedIfNoDiff3();
702 $text = "one";
703 $page = $this->createPage( "WikiPageTest_testGetUndoText", $text );
704 $rev1 = $page->getRevision();
706 $text .= "\n\ntwo";
707 $page->doEditContent(
708 ContentHandler::makeContent( $text, $page->getTitle() ),
709 "adding section two"
711 $rev2 = $page->getRevision();
713 $text .= "\n\nthree";
714 $page->doEditContent(
715 ContentHandler::makeContent( $text, $page->getTitle() ),
716 "adding section three"
718 $rev3 = $page->getRevision();
720 $text .= "\n\nfour";
721 $page->doEditContent(
722 ContentHandler::makeContent( $text, $page->getTitle() ),
723 "adding section four"
725 $rev4 = $page->getRevision();
727 $text .= "\n\nfive";
728 $page->doEditContent(
729 ContentHandler::makeContent( $text, $page->getTitle() ),
730 "adding section five"
732 $rev5 = $page->getRevision();
734 $text .= "\n\nsix";
735 $page->doEditContent(
736 ContentHandler::makeContent( $text, $page->getTitle() ),
737 "adding section six"
739 $rev6 = $page->getRevision();
741 $undo6 = $page->getUndoText( $rev6 );
742 if ( $undo6 === false ) $this->fail( "getUndoText failed for rev6" );
743 $this->assertEquals( "one\n\ntwo\n\nthree\n\nfour\n\nfive", $undo6 );
745 $undo3 = $page->getUndoText( $rev4, $rev2 );
746 if ( $undo3 === false ) $this->fail( "getUndoText failed for rev4..rev2" );
747 $this->assertEquals( "one\n\ntwo\n\nfive", $undo3 );
749 $undo2 = $page->getUndoText( $rev2 );
750 if ( $undo2 === false ) $this->fail( "getUndoText failed for rev2" );
751 $this->assertEquals( "one\n\nfive", $undo2 );
756 * @covers WikiPage::getOldestRevision
758 public function testGetOldestRevision() {
759 $page = $this->newPage( "WikiPageTest_testGetOldestRevision" );
760 $page->doEditContent(
761 new WikitextContent( 'one' ),
762 "first edit",
763 EDIT_NEW
765 $rev1 = $page->getRevision();
767 $page = new WikiPage( $page->getTitle() );
768 $page->doEditContent(
769 new WikitextContent( 'two' ),
770 "second edit",
771 EDIT_UPDATE
774 $page = new WikiPage( $page->getTitle() );
775 $page->doEditContent(
776 new WikitextContent( 'three' ),
777 "third edit",
778 EDIT_UPDATE
781 // sanity check
782 $this->assertNotEquals(
783 $rev1->getId(),
784 $page->getRevision()->getId(),
785 '$page->getRevision()->getId()'
788 // actual test
789 $this->assertEquals(
790 $rev1->getId(),
791 $page->getOldestRevision()->getId(),
792 '$page->getOldestRevision()->getId()'
797 * @todo FIXME: this is a better rollback test than the one below, but it
798 * keeps failing in jenkins for some reason.
800 public function broken_testDoRollback() {
801 $admin = new User();
802 $admin->setName( "Admin" );
804 $text = "one";
805 $page = $this->newPage( "WikiPageTest_testDoRollback" );
806 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
807 "section one", EDIT_NEW, false, $admin );
809 $user1 = new User();
810 $user1->setName( "127.0.1.11" );
811 $text .= "\n\ntwo";
812 $page = new WikiPage( $page->getTitle() );
813 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
814 "adding section two", 0, false, $user1 );
816 $user2 = new User();
817 $user2->setName( "127.0.2.13" );
818 $text .= "\n\nthree";
819 $page = new WikiPage( $page->getTitle() );
820 $page->doEditContent( ContentHandler::makeContent( $text, $page->getTitle() ),
821 "adding section three", 0, false, $user2 );
823 # we are having issues with doRollback spuriously failing. Apparently
824 # the last revision somehow goes missing or not committed under some
825 # circumstances. So, make sure the last revision has the right user name.
826 $dbr = wfGetDB( DB_SLAVE );
827 $this->assertEquals( 3, Revision::countByPageId( $dbr, $page->getId() ) );
829 $page = new WikiPage( $page->getTitle() );
830 $rev3 = $page->getRevision();
831 $this->assertEquals( '127.0.2.13', $rev3->getUserText() );
833 $rev2 = $rev3->getPrevious();
834 $this->assertEquals( '127.0.1.11', $rev2->getUserText() );
836 $rev1 = $rev2->getPrevious();
837 $this->assertEquals( 'Admin', $rev1->getUserText() );
839 # now, try the actual rollback
840 $admin->addToDatabase();
841 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
842 $token = $admin->getEditToken(
843 [ $page->getTitle()->getPrefixedText(), $user2->getName() ],
844 null
846 $errors = $page->doRollback(
847 $user2->getName(),
848 "testing revert",
849 $token,
850 false,
851 $details,
852 $admin
855 if ( $errors ) {
856 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
857 . ";\n" . print_r( $details, true ) );
860 $page = new WikiPage( $page->getTitle() );
861 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
862 "rollback did not revert to the correct revision" );
863 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
867 * @todo FIXME: the above rollback test is better, but it keeps failing in jenkins for some reason.
868 * @covers WikiPage::doRollback
870 public function testDoRollback() {
871 $admin = new User();
872 $admin->setName( "Admin" );
873 $admin->addToDatabase();
875 $text = "one";
876 $page = $this->newPage( "WikiPageTest_testDoRollback" );
877 $page->doEditContent(
878 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
879 "section one",
880 EDIT_NEW,
881 false,
882 $admin
884 $rev1 = $page->getRevision();
886 $user1 = new User();
887 $user1->setName( "127.0.1.11" );
888 $text .= "\n\ntwo";
889 $page = new WikiPage( $page->getTitle() );
890 $page->doEditContent(
891 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
892 "adding section two",
894 false,
895 $user1
898 # now, try the rollback
899 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
900 $token = $admin->getEditToken( 'rollback' );
901 $errors = $page->doRollback(
902 $user1->getName(),
903 "testing revert",
904 $token,
905 false,
906 $details,
907 $admin
910 if ( $errors ) {
911 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
912 . ";\n" . print_r( $details, true ) );
915 $page = new WikiPage( $page->getTitle() );
916 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
917 "rollback did not revert to the correct revision" );
918 $this->assertEquals( "one", $page->getContent()->getNativeData() );
922 * @covers WikiPage::doRollback
924 public function testDoRollbackFailureSameContent() {
925 $admin = new User();
926 $admin->setName( "Admin" );
927 $admin->addToDatabase();
928 $admin->addGroup( "sysop" ); # XXX: make the test user a sysop...
930 $text = "one";
931 $page = $this->newPage( "WikiPageTest_testDoRollback" );
932 $page->doEditContent(
933 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
934 "section one",
935 EDIT_NEW,
936 false,
937 $admin
939 $rev1 = $page->getRevision();
941 $user1 = new User();
942 $user1->setName( "127.0.1.11" );
943 $user1->addToDatabase();
944 $user1->addGroup( "sysop" ); # XXX: make the test user a sysop...
945 $text .= "\n\ntwo";
946 $page = new WikiPage( $page->getTitle() );
947 $page->doEditContent(
948 ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
949 "adding section two",
951 false,
952 $user1
955 # now, do a the rollback from the same user was doing the edit before
956 $resultDetails = [];
957 $token = $user1->getEditToken( 'rollback' );
958 $errors = $page->doRollback(
959 $user1->getName(),
960 "testing revert same user",
961 $token,
962 false,
963 $resultDetails,
964 $admin
967 $this->assertEquals( [], $errors, "Rollback failed same user" );
969 # now, try the rollback
970 $resultDetails = [];
971 $token = $admin->getEditToken( 'rollback' );
972 $errors = $page->doRollback(
973 $user1->getName(),
974 "testing revert",
975 $token,
976 false,
977 $resultDetails,
978 $admin
981 $this->assertEquals( [ [ 'alreadyrolled', 'WikiPageTest testDoRollback',
982 '127.0.1.11', 'Admin' ] ], $errors, "Rollback not failed" );
984 $page = new WikiPage( $page->getTitle() );
985 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
986 "rollback did not revert to the correct revision" );
987 $this->assertEquals( "one", $page->getContent()->getNativeData() );
990 public static function provideGetAutoDeleteReason() {
991 return [
994 false,
995 false
1000 [ "first edit", null ],
1002 "/first edit.*only contributor/",
1003 false
1008 [ "first edit", null ],
1009 [ "second edit", null ],
1011 "/second edit.*only contributor/",
1012 true
1017 [ "first edit", "127.0.2.22" ],
1018 [ "second edit", "127.0.3.33" ],
1020 "/second edit/",
1021 true
1027 "first edit: "
1028 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1029 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1030 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1031 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1032 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1033 null
1036 '/first edit:.*\.\.\."/',
1037 false
1042 [ "first edit", "127.0.2.22" ],
1043 [ "", "127.0.3.33" ],
1045 "/before blanking.*first edit/",
1046 true
1053 * @dataProvider provideGetAutoDeleteReason
1054 * @covers WikiPage::getAutoDeleteReason
1056 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1057 global $wgUser;
1059 // NOTE: assume Help namespace to contain wikitext
1060 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1062 $c = 1;
1064 foreach ( $edits as $edit ) {
1065 $user = new User();
1067 if ( !empty( $edit[1] ) ) {
1068 $user->setName( $edit[1] );
1069 } else {
1070 $user = $wgUser;
1073 $content = ContentHandler::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1075 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW : 0, false, $user );
1077 $c += 1;
1080 $reason = $page->getAutoDeleteReason( $hasHistory );
1082 if ( is_bool( $expectedResult ) || is_null( $expectedResult ) ) {
1083 $this->assertEquals( $expectedResult, $reason );
1084 } else {
1085 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1086 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1089 $this->assertEquals( $expectedHistory, $hasHistory,
1090 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1092 $page->doDeleteArticle( "done" );
1095 public static function providePreSaveTransform() {
1096 return [
1097 [ 'hello this is ~~~',
1098 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1100 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1101 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1107 * @covers WikiPage::factory
1109 public function testWikiPageFactory() {
1110 $title = Title::makeTitle( NS_FILE, 'Someimage.png' );
1111 $page = WikiPage::factory( $title );
1112 $this->assertEquals( 'WikiFilePage', get_class( $page ) );
1114 $title = Title::makeTitle( NS_CATEGORY, 'SomeCategory' );
1115 $page = WikiPage::factory( $title );
1116 $this->assertEquals( 'WikiCategoryPage', get_class( $page ) );
1118 $title = Title::makeTitle( NS_MAIN, 'SomePage' );
1119 $page = WikiPage::factory( $title );
1120 $this->assertEquals( 'WikiPage', get_class( $page ) );