3 abstract class WikiPageDbTestBase
extends MediaWikiLangTestCase
{
5 private $pagesToDelete;
7 public function __construct( $name = null, array $data = [], $dataName = '' ) {
8 parent
::__construct( $name, $data, $dataName );
10 $this->tablesUsed
= array_merge(
36 abstract protected function getMcrMigrationStage();
41 abstract protected function getMcrTablesToReset();
43 protected function setUp() {
46 $this->tablesUsed +
= $this->getMcrTablesToReset();
48 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
50 'wgMultiContentRevisionSchemaMigrationStage',
51 $this->getMcrMigrationStage()
53 $this->pagesToDelete
= [];
55 $this->overrideMwServices();
58 protected function tearDown() {
59 foreach ( $this->pagesToDelete
as $p ) {
60 /* @var $p WikiPage */
64 $p->doDeleteArticle( "testing done." );
66 } catch ( MWException
$ex ) {
73 abstract protected function getContentHandlerUseDB();
76 * @param Title|string $title
77 * @param string|null $model
80 private function newPage( $title, $model = null ) {
81 if ( is_string( $title ) ) {
82 $ns = $this->getDefaultWikitextNS();
83 $title = Title
::newFromText( $title, $ns );
86 $p = new WikiPage( $title );
88 $this->pagesToDelete
[] = $p;
94 * @param string|Title|WikiPage $page
100 protected function createPage( $page, $text, $model = null ) {
101 if ( is_string( $page ) ||
$page instanceof Title
) {
102 $page = $this->newPage( $page, $model );
105 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
106 $page->doEditContent( $content, "testing", EDIT_NEW
);
112 * @covers WikiPage::doEditContent
113 * @covers WikiPage::doModify
114 * @covers WikiPage::doCreate
115 * @covers WikiPage::doEditUpdates
117 public function testDoEditContent() {
118 $this->setMwGlobals( 'wgPageCreationLog', true );
120 $page = $this->newPage( __METHOD__
);
121 $title = $page->getTitle();
123 $content = ContentHandler
::makeContent(
124 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
125 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
127 CONTENT_MODEL_WIKITEXT
130 $page->doEditContent( $content, "[[testing]] 1" );
132 $id = $page->getId();
134 // Test page creation logging
137 [ 'log_type', 'log_action' ],
138 [ 'log_page' => $id ],
139 [ [ 'create', 'create' ] ]
142 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
143 $this->assertTrue( $id > 0, "WikiPage should have new page id" );
144 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
145 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
147 # ------------------------
148 $dbr = wfGetDB( DB_REPLICA
);
149 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
150 $n = $res->numRows();
153 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
155 # ------------------------
156 $page = new WikiPage( $title );
158 $retrieved = $page->getContent();
159 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
161 # ------------------------
162 $content = ContentHandler
::makeContent(
163 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
164 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
166 CONTENT_MODEL_WIKITEXT
169 $page->doEditContent( $content, "testing 2" );
171 # ------------------------
172 $page = new WikiPage( $title );
174 $retrieved = $page->getContent();
175 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
177 # ------------------------
178 $dbr = wfGetDB( DB_REPLICA
);
179 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
180 $n = $res->numRows();
183 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
187 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
188 * TODO: Revision deletion
190 * @covers WikiPage::doDeleteArticle
191 * @covers WikiPage::doDeleteArticleReal
193 public function testDoDeleteArticle() {
194 $page = $this->createPage(
196 "[[original text]] foo",
197 CONTENT_MODEL_WIKITEXT
199 $id = $page->getId();
201 $page->doDeleteArticle( "testing deletion" );
204 $page->getTitle()->getArticleID() > 0,
205 "Title object should now have page id 0"
207 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
210 "WikiPage::exists should return false after page was deleted"
214 "WikiPage::getContent should return null after page was deleted"
217 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
220 "Title::exists should return false after page was deleted"
224 JobQueueGroup
::destroySingletons();
226 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
229 # ------------------------
230 $dbr = wfGetDB( DB_REPLICA
);
231 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
232 $n = $res->numRows();
235 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
239 * @covers WikiPage::doDeleteArticleReal
241 public function testDoDeleteArticleReal_user0() {
242 $page = $this->createPage(
244 "[[original text]] foo",
245 CONTENT_MODEL_WIKITEXT
247 $id = $page->getId();
250 $status = $page->doDeleteArticleReal(
251 /* reason */ "testing user 0 deletion",
252 /* suppress */ false,
255 /* errorStack */ $errorStack,
258 $logId = $status->getValue();
259 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
261 [ 'logging' ] +
$actorQuery['tables'], /* table */
266 'log_user' => $actorQuery['fields']['log_user'],
267 'log_user_text' => $actorQuery['fields']['log_user_text'],
271 [ 'log_id' => $logId ],
275 'testing user 0 deletion',
278 (string)$page->getTitle()->getNamespace(),
279 $page->getTitle()->getDBkey(),
287 * @covers WikiPage::doDeleteArticleReal
289 public function testDoDeleteArticleReal_userSysop() {
290 $page = $this->createPage(
292 "[[original text]] foo",
293 CONTENT_MODEL_WIKITEXT
295 $id = $page->getId();
297 $user = $this->getTestSysop()->getUser();
299 $status = $page->doDeleteArticleReal(
300 /* reason */ "testing sysop deletion",
301 /* suppress */ false,
304 /* errorStack */ $errorStack,
307 $logId = $status->getValue();
308 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
310 [ 'logging' ] +
$actorQuery['tables'], /* table */
315 'log_user' => $actorQuery['fields']['log_user'],
316 'log_user_text' => $actorQuery['fields']['log_user_text'],
320 [ 'log_id' => $logId ],
324 'testing sysop deletion',
325 (string)$user->getId(),
327 (string)$page->getTitle()->getNamespace(),
328 $page->getTitle()->getDBkey(),
336 * TODO: Test more stuff about suppression.
338 * @covers WikiPage::doDeleteArticleReal
340 public function testDoDeleteArticleReal_suppress() {
341 $page = $this->createPage(
343 "[[original text]] foo",
344 CONTENT_MODEL_WIKITEXT
346 $id = $page->getId();
348 $user = $this->getTestSysop()->getUser();
350 $status = $page->doDeleteArticleReal(
351 /* reason */ "testing deletion",
355 /* errorStack */ $errorStack,
358 $logId = $status->getValue();
359 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
361 [ 'logging' ] +
$actorQuery['tables'], /* table */
366 'log_user' => $actorQuery['fields']['log_user'],
367 'log_user_text' => $actorQuery['fields']['log_user_text'],
371 [ 'log_id' => $logId ],
376 (string)$user->getId(),
378 (string)$page->getTitle()->getNamespace(),
379 $page->getTitle()->getDBkey(),
386 $page->getContent( Revision
::FOR_PUBLIC
),
387 "WikiPage::getContent should return null after the page was suppressed for general users"
391 $page->getContent( Revision
::FOR_THIS_USER
, null ),
392 "WikiPage::getContent should return null after the page was suppressed for user zero"
396 $page->getContent( Revision
::FOR_THIS_USER
, $user ),
397 "WikiPage::getContent should return null after the page was suppressed even for a sysop"
402 * @covers WikiPage::doDeleteUpdates
404 public function testDoDeleteUpdates() {
405 $page = $this->createPage(
407 "[[original text]] foo",
408 CONTENT_MODEL_WIKITEXT
410 $id = $page->getId();
412 // Similar to MovePage logic
413 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
414 $page->doDeleteUpdates( $id );
417 JobQueueGroup
::destroySingletons();
419 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
422 # ------------------------
423 $dbr = wfGetDB( DB_REPLICA
);
424 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
425 $n = $res->numRows();
428 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
432 * @covers WikiPage::getRevision
434 public function testGetRevision() {
435 $page = $this->newPage( __METHOD__
);
437 $rev = $page->getRevision();
438 $this->assertNull( $rev );
441 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
443 $rev = $page->getRevision();
445 $this->assertEquals( $page->getLatest(), $rev->getId() );
446 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
450 * @covers WikiPage::getContent
452 public function testGetContent() {
453 $page = $this->newPage( __METHOD__
);
455 $content = $page->getContent();
456 $this->assertNull( $content );
459 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
461 $content = $page->getContent();
462 $this->assertEquals( "some text", $content->getNativeData() );
466 * @covers WikiPage::exists
468 public function testExists() {
469 $page = $this->newPage( __METHOD__
);
470 $this->assertFalse( $page->exists() );
473 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
474 $this->assertTrue( $page->exists() );
476 $page = new WikiPage( $page->getTitle() );
477 $this->assertTrue( $page->exists() );
480 $page->doDeleteArticle( "done testing" );
481 $this->assertFalse( $page->exists() );
483 $page = new WikiPage( $page->getTitle() );
484 $this->assertFalse( $page->exists() );
487 public function provideHasViewableContent() {
489 [ 'WikiPageTest_testHasViewableContent', false, true ],
490 [ 'Special:WikiPageTest_testHasViewableContent', false ],
491 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
492 [ 'Special:Userlogin', true ],
493 [ 'MediaWiki:help', true ],
498 * @dataProvider provideHasViewableContent
499 * @covers WikiPage::hasViewableContent
501 public function testHasViewableContent( $title, $viewable, $create = false ) {
502 $page = $this->newPage( $title );
503 $this->assertEquals( $viewable, $page->hasViewableContent() );
506 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
507 $this->assertTrue( $page->hasViewableContent() );
509 $page = new WikiPage( $page->getTitle() );
510 $this->assertTrue( $page->hasViewableContent() );
514 public function provideGetRedirectTarget() {
516 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
518 'WikiPageTest_testGetRedirectTarget_2',
519 CONTENT_MODEL_WIKITEXT
,
520 "#REDIRECT [[hello world]]",
527 * @dataProvider provideGetRedirectTarget
528 * @covers WikiPage::getRedirectTarget
530 public function testGetRedirectTarget( $title, $model, $text, $target ) {
531 $this->setMwGlobals( [
532 'wgCapitalLinks' => true,
535 $page = $this->createPage( $title, $text, $model );
537 # sanity check, because this test seems to fail for no reason for some people.
538 $c = $page->getContent();
539 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
541 # now, test the actual redirect
542 $t = $page->getRedirectTarget();
543 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
547 * @dataProvider provideGetRedirectTarget
548 * @covers WikiPage::isRedirect
550 public function testIsRedirect( $title, $model, $text, $target ) {
551 $page = $this->createPage( $title, $text, $model );
552 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
555 public function provideIsCountable() {
559 [ 'WikiPageTest_testIsCountable',
560 CONTENT_MODEL_WIKITEXT
,
565 [ 'WikiPageTest_testIsCountable',
566 CONTENT_MODEL_WIKITEXT
,
573 [ 'WikiPageTest_testIsCountable',
574 CONTENT_MODEL_WIKITEXT
,
579 [ 'WikiPageTest_testIsCountable',
580 CONTENT_MODEL_WIKITEXT
,
587 [ 'WikiPageTest_testIsCountable',
588 CONTENT_MODEL_WIKITEXT
,
593 [ 'WikiPageTest_testIsCountable',
594 CONTENT_MODEL_WIKITEXT
,
600 // not a content namespace
601 [ 'Talk:WikiPageTest_testIsCountable',
602 CONTENT_MODEL_WIKITEXT
,
607 [ 'Talk:WikiPageTest_testIsCountable',
608 CONTENT_MODEL_WIKITEXT
,
614 // not a content namespace, different model
615 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
621 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
631 * @dataProvider provideIsCountable
632 * @covers WikiPage::isCountable
634 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
635 global $wgContentHandlerUseDB;
637 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
639 $title = Title
::newFromText( $title );
641 if ( !$wgContentHandlerUseDB
643 && ContentHandler
::getDefaultModelFor( $title ) != $model
645 $this->markTestSkipped( "Can not use non-default content model $model for "
646 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
649 $page = $this->createPage( $title, $text, $model );
651 $editInfo = $page->prepareContentForEdit( $page->getContent() );
653 $v = $page->isCountable();
654 $w = $page->isCountable( $editInfo );
659 "isCountable( null ) returned unexpected value " . var_export( $v, true )
660 . " instead of " . var_export( $expected, true )
661 . " in mode `$mode` for text \"$text\""
667 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
668 . " instead of " . var_export( $expected, true )
669 . " in mode `$mode` for text \"$text\""
673 public function provideGetParserOutput() {
676 CONTENT_MODEL_WIKITEXT
,
678 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
685 * @dataProvider provideGetParserOutput
686 * @covers WikiPage::getParserOutput
688 public function testGetParserOutput( $model, $text, $expectedHtml ) {
689 $page = $this->createPage( __METHOD__
, $text, $model );
691 $opt = $page->makeParserOptions( 'canonical' );
692 $po = $page->getParserOutput( $opt );
693 $text = $po->getText();
695 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
696 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
698 $this->assertEquals( $expectedHtml, $text );
704 * @covers WikiPage::getParserOutput
706 public function testGetParserOutput_nonexisting() {
707 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
709 $opt = new ParserOptions();
710 $po = $page->getParserOutput( $opt );
712 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
716 * @covers WikiPage::getParserOutput
718 public function testGetParserOutput_badrev() {
719 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
721 $opt = new ParserOptions();
722 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
724 // @todo would be neat to also test deleted revision
726 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
729 public static $sections =
743 public function dataReplaceSection() {
744 // NOTE: assume the Help namespace to contain wikitext
746 [ 'Help:WikiPageTest_testReplaceSection',
747 CONTENT_MODEL_WIKITEXT
,
752 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
754 [ 'Help:WikiPageTest_testReplaceSection',
755 CONTENT_MODEL_WIKITEXT
,
762 [ 'Help:WikiPageTest_testReplaceSection',
763 CONTENT_MODEL_WIKITEXT
,
766 "== TEST ==\nmore fun",
768 trim( preg_replace( '/^== test ==.*== foo ==/sm',
769 "== TEST ==\nmore fun\n\n== foo ==",
772 [ 'Help:WikiPageTest_testReplaceSection',
773 CONTENT_MODEL_WIKITEXT
,
778 trim( self
::$sections )
780 [ 'Help:WikiPageTest_testReplaceSection',
781 CONTENT_MODEL_WIKITEXT
,
786 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
792 * @dataProvider dataReplaceSection
793 * @covers WikiPage::replaceSectionContent
795 public function testReplaceSectionContent( $title, $model, $text, $section,
796 $with, $sectionTitle, $expected
798 $page = $this->createPage( $title, $text, $model );
800 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
801 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
803 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
807 * @dataProvider dataReplaceSection
808 * @covers WikiPage::replaceSectionAtRev
810 public function testReplaceSectionAtRev( $title, $model, $text, $section,
811 $with, $sectionTitle, $expected
813 $page = $this->createPage( $title, $text, $model );
814 $baseRevId = $page->getLatest();
816 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
817 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
819 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
823 * @covers WikiPage::getOldestRevision
825 public function testGetOldestRevision() {
826 $page = $this->newPage( __METHOD__
);
827 $page->doEditContent(
828 new WikitextContent( 'one' ),
832 $rev1 = $page->getRevision();
834 $page = new WikiPage( $page->getTitle() );
835 $page->doEditContent(
836 new WikitextContent( 'two' ),
841 $page = new WikiPage( $page->getTitle() );
842 $page->doEditContent(
843 new WikitextContent( 'three' ),
849 $this->assertNotEquals(
851 $page->getRevision()->getId(),
852 '$page->getRevision()->getId()'
858 $page->getOldestRevision()->getId(),
859 '$page->getOldestRevision()->getId()'
864 * @covers WikiPage::doRollback
865 * @covers WikiPage::commitRollback
867 public function testDoRollback() {
868 $admin = $this->getTestSysop()->getUser();
869 $user1 = $this->getTestUser()->getUser();
870 // Use the confirmed group for user2 to make sure the user is different
871 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
873 $page = $this->newPage( __METHOD__
);
877 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
878 "section one", EDIT_NEW
, false, $admin );
881 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
882 "adding section two", 0, false, $user1 );
884 $text .= "\n\nthree";
885 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
886 "adding section three", 0, false, $user2 );
888 /** @var Revision $rev1 */
889 /** @var Revision $rev2 */
890 /** @var Revision $rev3 */
891 $rev1 = $status1->getValue()['revision'];
892 $rev2 = $status2->getValue()['revision'];
893 $rev3 = $status3->getValue()['revision'];
896 * We are having issues with doRollback spuriously failing. Apparently
897 * the last revision somehow goes missing or not committed under some
898 * circumstances. So, make sure the revisions have the correct usernames.
900 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
901 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
902 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
903 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
905 // Now, try the actual rollback
906 $token = $admin->getEditToken( 'rollback' );
907 $rollbackErrors = $page->doRollback(
916 if ( $rollbackErrors ) {
918 "Rollback failed:\n" .
919 print_r( $rollbackErrors, true ) . ";\n" .
920 print_r( $resultDetails, true )
924 $page = new WikiPage( $page->getTitle() );
925 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
926 "rollback did not revert to the correct revision" );
927 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
931 * @covers WikiPage::doRollback
932 * @covers WikiPage::commitRollback
934 public function testDoRollback_simple() {
935 $admin = $this->getTestSysop()->getUser();
938 $page = $this->newPage( __METHOD__
);
939 $page->doEditContent(
940 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
946 $rev1 = $page->getRevision();
948 $user1 = $this->getTestUser()->getUser();
950 $page = new WikiPage( $page->getTitle() );
951 $page->doEditContent(
952 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
953 "adding section two",
959 # now, try the rollback
960 $token = $admin->getEditToken( 'rollback' );
961 $errors = $page->doRollback(
971 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
972 . ";\n" . print_r( $details, true ) );
975 $page = new WikiPage( $page->getTitle() );
976 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
977 "rollback did not revert to the correct revision" );
978 $this->assertEquals( "one", $page->getContent()->getNativeData() );
982 * @covers WikiPage::doRollback
983 * @covers WikiPage::commitRollback
985 public function testDoRollbackFailureSameContent() {
986 $admin = $this->getTestSysop()->getUser();
989 $page = $this->newPage( __METHOD__
);
990 $page->doEditContent(
991 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
997 $rev1 = $page->getRevision();
999 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
1001 $page = new WikiPage( $page->getTitle() );
1002 $page->doEditContent(
1003 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1004 "adding section two",
1010 # now, do a the rollback from the same user was doing the edit before
1011 $resultDetails = [];
1012 $token = $user1->getEditToken( 'rollback' );
1013 $errors = $page->doRollback(
1015 "testing revert same user",
1022 $this->assertEquals( [], $errors, "Rollback failed same user" );
1024 # now, try the rollback
1025 $resultDetails = [];
1026 $token = $admin->getEditToken( 'rollback' );
1027 $errors = $page->doRollback(
1036 $this->assertEquals(
1046 "Rollback not failed"
1049 $page = new WikiPage( $page->getTitle() );
1050 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1051 "rollback did not revert to the correct revision" );
1052 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1056 * Tests tagging for edits that do rollback action
1057 * @covers WikiPage::doRollback
1059 public function testDoRollbackTagging() {
1060 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
1061 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
1064 $admin = new User();
1065 $admin->setName( 'Administrator' );
1066 $admin->addToDatabase();
1068 $text = 'First line';
1069 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
1070 $page->doEditContent(
1071 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1078 $secondUser = new User();
1079 $secondUser->setName( '92.65.217.32' );
1080 $text .= '\n\nSecond line';
1081 $page = new WikiPage( $page->getTitle() );
1082 $page->doEditContent(
1083 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1084 'Adding second line',
1090 // Now, try the rollback
1091 $admin->addGroup( 'sysop' ); // Make the test user a sysop
1092 $token = $admin->getEditToken( 'rollback' );
1093 $errors = $page->doRollback(
1094 $secondUser->getName(),
1102 // If doRollback completed without errors
1103 if ( $errors === [] ) {
1104 $tags = $resultDetails[ 'tags' ];
1105 $this->assertContains( 'mw-rollback', $tags );
1109 public function provideGetAutoDeleteReason() {
1119 [ "first edit", null ],
1121 "/first edit.*only contributor/",
1127 [ "first edit", null ],
1128 [ "second edit", null ],
1130 "/second edit.*only contributor/",
1136 [ "first edit", "127.0.2.22" ],
1137 [ "second edit", "127.0.3.33" ],
1147 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1148 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1149 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1150 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1151 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1155 '/first edit:.*\.\.\."/',
1161 [ "first edit", "127.0.2.22" ],
1162 [ "", "127.0.3.33" ],
1164 "/before blanking.*first edit/",
1172 * @dataProvider provideGetAutoDeleteReason
1173 * @covers WikiPage::getAutoDeleteReason
1175 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1178 // NOTE: assume Help namespace to contain wikitext
1179 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1183 foreach ( $edits as $edit ) {
1186 if ( !empty( $edit[1] ) ) {
1187 $user->setName( $edit[1] );
1192 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1194 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1199 $reason = $page->getAutoDeleteReason( $hasHistory );
1201 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1202 $this->assertEquals( $expectedResult, $reason );
1204 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1205 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1208 $this->assertEquals( $expectedHistory, $hasHistory,
1209 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1211 $page->doDeleteArticle( "done" );
1214 public function providePreSaveTransform() {
1216 [ 'hello this is ~~~',
1217 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1219 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1220 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1226 * @covers WikiPage::factory
1228 public function testWikiPageFactory() {
1229 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1230 $page = WikiPage
::factory( $title );
1231 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1233 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1234 $page = WikiPage
::factory( $title );
1235 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1237 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1238 $page = WikiPage
::factory( $title );
1239 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1243 * @dataProvider provideCommentMigrationOnDeletion
1245 * @param int $writeStage
1246 * @param int $readStage
1248 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1249 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1250 $this->overrideMwServices();
1252 $dbr = wfGetDB( DB_REPLICA
);
1254 $page = $this->createPage(
1257 CONTENT_MODEL_WIKITEXT
1259 $revid = $page->getLatest();
1260 if ( $writeStage > MIGRATION_OLD
) {
1261 $comment_id = $dbr->selectField(
1262 'revision_comment_temp',
1263 'revcomment_comment_id',
1264 [ 'revcomment_rev' => $revid ],
1269 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1270 $this->overrideMwServices();
1272 $page->doDeleteArticle( "testing deletion" );
1274 if ( $readStage > MIGRATION_OLD
) {
1275 // Didn't leave behind any 'revision_comment_temp' rows
1276 $n = $dbr->selectField(
1277 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1279 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1281 // Copied or upgraded the comment_id, as applicable
1282 $ar_comment_id = $dbr->selectField(
1285 [ 'ar_rev_id' => $revid ],
1288 if ( $writeStage > MIGRATION_OLD
) {
1289 $this->assertSame( $comment_id, $ar_comment_id );
1291 $this->assertNotEquals( 0, $ar_comment_id );
1295 // Copied rev_comment, if applicable
1296 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1297 $ar_comment = $dbr->selectField(
1300 [ 'ar_rev_id' => $revid ],
1303 $this->assertSame( 'testing', $ar_comment );
1307 public function provideCommentMigrationOnDeletion() {
1309 [ MIGRATION_OLD
, MIGRATION_OLD
],
1310 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1311 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1312 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1313 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1314 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1315 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1316 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1317 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1318 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1319 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1320 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1321 [ MIGRATION_NEW
, MIGRATION_NEW
],
1326 * @covers WikiPage::updateCategoryCounts
1328 public function testUpdateCategoryCounts() {
1329 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1331 // Add an initial category
1332 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1334 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1335 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1336 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1338 // Add a new category
1339 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1341 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1342 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1343 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1345 // Add and remove a category
1346 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1348 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1349 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1350 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1353 public function provideUpdateRedirectOn() {
1354 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1355 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1356 yield
[ 'SomeText', false, null, false, true, 0 ];
1357 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1361 * @dataProvider provideUpdateRedirectOn
1362 * @covers WikiPage::updateRedirectOn
1364 * @param string $initialText
1365 * @param bool $initialRedirectState
1366 * @param string|null $redirectTitle
1367 * @param bool|null $lastRevIsRedirect
1368 * @param bool $expectedSuccess
1369 * @param int $expectedRowCount
1371 public function testUpdateRedirectOn(
1373 $initialRedirectState,
1379 static $pageCounter = 0;
1382 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1383 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1385 $redirectTitle = is_string( $redirectTitle )
1386 ? Title
::newFromText( $redirectTitle )
1389 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1390 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1392 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1393 * Most of core checks the page table for redirect status, so we have to be ugly and
1394 * assert a select from the table here.
1396 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1399 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1400 $this->assertSelect(
1403 [ 'rd_from' => $pageId ],
1404 [ [ strval( $expected ) ] ]
1409 * @covers WikiPage::insertRedirectEntry
1411 public function testInsertRedirectEntry_insertsRedirectEntry() {
1412 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1413 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1415 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1416 $targetTitle->mInterwiki
= 'eninter';
1417 $page->insertRedirectEntry( $targetTitle, null );
1419 $this->assertSelect(
1421 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1422 [ 'rd_from' => $page->getId() ],
1424 strval( $page->getId() ),
1425 strval( $targetTitle->getNamespace() ),
1426 strval( $targetTitle->getDBkey() ),
1427 strval( $targetTitle->getFragment() ),
1428 strval( $targetTitle->getInterwiki() ),
1434 * @covers WikiPage::insertRedirectEntry
1436 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1437 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1438 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1440 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1441 $targetTitle->mInterwiki
= 'eninter';
1442 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1444 $this->assertSelect(
1446 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1447 [ 'rd_from' => $page->getId() ],
1449 strval( $page->getId() ),
1450 strval( $targetTitle->getNamespace() ),
1451 strval( $targetTitle->getDBkey() ),
1452 strval( $targetTitle->getFragment() ),
1453 strval( $targetTitle->getInterwiki() ),
1459 * @covers WikiPage::insertRedirectEntry
1461 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1462 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1463 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1465 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1466 $targetTitle->mInterwiki
= 'eninter';
1467 $page->insertRedirectEntry( $targetTitle, 215251 );
1469 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1472 private function getRow( array $overrides = [] ) {
1476 'page_is_redirect' => '1',
1477 'page_latest' => '99',
1478 'page_namespace' => '3',
1479 'page_title' => 'JaJaTitle',
1480 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1481 'page_touched' => '20120101020202',
1482 'page_links_updated' => '20140101020202',
1484 foreach ( $overrides as $key => $value ) {
1485 $row[$key] = $value;
1487 return (object)$row;
1490 public function provideNewFromRowSuccess() {
1491 yield
'basic row' => [
1493 function ( WikiPage
$wikiPage, self
$test ) {
1494 $test->assertSame( 44, $wikiPage->getId() );
1495 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1496 $test->assertTrue( $wikiPage->isRedirect() );
1497 $test->assertSame( 99, $wikiPage->getLatest() );
1498 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1499 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1502 'edit' => [ 'autoconfirmed', 'sysop' ],
1503 'move' => [ 'sysop' ],
1505 $wikiPage->getTitle()->getAllRestrictions()
1507 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1508 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1511 yield
'different timestamp formats' => [
1513 'page_touched' => '2012-01-01 02:02:02',
1514 'page_links_updated' => '2014-01-01 02:02:02',
1516 function ( WikiPage
$wikiPage, self
$test ) {
1517 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1518 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1521 yield
'no restrictions' => [
1523 'page_restrictions' => '',
1525 function ( WikiPage
$wikiPage, self
$test ) {
1531 $wikiPage->getTitle()->getAllRestrictions()
1535 yield
'not redirect' => [
1537 'page_is_redirect' => '0',
1539 function ( WikiPage
$wikiPage, self
$test ) {
1540 $test->assertFalse( $wikiPage->isRedirect() );
1546 * @covers WikiPage::newFromRow
1547 * @covers WikiPage::loadFromRow
1548 * @dataProvider provideNewFromRowSuccess
1550 * @param object $row
1551 * @param callable $assertions
1553 public function testNewFromRow( $row, $assertions ) {
1554 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1555 $assertions( $page, $this );
1558 public function provideTestNewFromId_returnsNullOnBadPageId() {
1564 * @covers WikiPage::newFromID
1565 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1567 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1568 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1572 * @covers WikiPage::newFromID
1574 public function testNewFromId_appearsToFetchCorrectRow() {
1575 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1576 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1577 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1578 $this->assertEquals(
1579 $createdPage->getContent()->getNativeData(),
1580 $fetchedPage->getContent()->getNativeData()
1585 * @covers WikiPage::newFromID
1587 public function testNewFromId_returnsNullOnNonExistingId() {
1588 $this->assertNull( WikiPage
::newFromID( 2147483647 ) );
1591 public function provideTestInsertProtectNullRevision() {
1592 // phpcs:disable Generic.Files.LineLength
1595 [ 'edit' => 'sysop' ],
1596 [ 'edit' => '20200101040404' ],
1600 '(goat-message-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Reason(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04)))'
1604 [ 'edit' => 'sysop', 'move' => 'something' ],
1605 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1609 '(goat-key: WikiPageDbTestBase::testInsertProtectNullRevision, UTSysop)(colon-separator)Goat Goat(word-separator)(parentheses: (protect-summary-desc: (restriction-edit), (protect-level-sysop), (protect-expiring: 04:04, 1 (january) 2020, 1 (january) 2020, 04:04))(word-separator)(protect-summary-desc: (restriction-move), (protect-level-something), (protect-expiring: 05:05, 1 (january) 2021, 1 (january) 2021, 05:05)))'
1615 * @dataProvider provideTestInsertProtectNullRevision
1616 * @covers WikiPage::insertProtectNullRevision
1617 * @covers WikiPage::protectDescription
1619 * @param string $revCommentMsg
1620 * @param array $limit
1621 * @param array $expiry
1622 * @param bool $cascade
1623 * @param string $reason
1624 * @param bool|null $user true if the test sysop should be used, or null
1625 * @param string $expectedComment
1627 public function testInsertProtectNullRevision(
1636 $this->setContentLang( 'qqx' );
1638 $page = $this->createPage( __METHOD__
, 'Goat' );
1640 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1642 $result = $page->insertProtectNullRevision(
1651 $this->assertTrue( $result instanceof Revision
);
1652 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1656 * @covers WikiPage::updateRevisionOn
1658 public function testUpdateRevisionOn_existingPage() {
1659 $user = $this->getTestSysop()->getUser();
1660 $page = $this->createPage( __METHOD__
, 'StartText' );
1662 $revision = new Revision(
1665 'page' => $page->getId(),
1666 'title' => $page->getTitle(),
1667 'comment' => __METHOD__
,
1668 'minor_edit' => true,
1669 'text' => __METHOD__
. '-text',
1670 'len' => strlen( __METHOD__
. '-text' ),
1671 'user' => $user->getId(),
1672 'user_text' => $user->getName(),
1673 'timestamp' => '20170707040404',
1674 'content_model' => CONTENT_MODEL_WIKITEXT
,
1675 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1679 $result = $page->updateRevisionOn( $this->db
, $revision );
1680 $this->assertTrue( $result );
1681 $this->assertSame( 9989, $page->getLatest() );
1682 $this->assertEquals( $revision, $page->getRevision() );
1686 * @covers WikiPage::updateRevisionOn
1688 public function testUpdateRevisionOn_NonExistingPage() {
1689 $user = $this->getTestSysop()->getUser();
1690 $page = $this->createPage( __METHOD__
, 'StartText' );
1691 $page->doDeleteArticle( 'reason' );
1693 $revision = new Revision(
1696 'page' => $page->getId(),
1697 'title' => $page->getTitle(),
1698 'comment' => __METHOD__
,
1699 'minor_edit' => true,
1700 'text' => __METHOD__
. '-text',
1701 'len' => strlen( __METHOD__
. '-text' ),
1702 'user' => $user->getId(),
1703 'user_text' => $user->getName(),
1704 'timestamp' => '20170707040404',
1705 'content_model' => CONTENT_MODEL_WIKITEXT
,
1706 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1710 $result = $page->updateRevisionOn( $this->db
, $revision );
1711 $this->assertFalse( $result );
1715 * @covers WikiPage::updateIfNewerOn
1717 public function testUpdateIfNewerOn_olderRevision() {
1718 $user = $this->getTestSysop()->getUser();
1719 $page = $this->createPage( __METHOD__
, 'StartText' );
1720 $initialRevision = $page->getRevision();
1722 $olderTimeStamp = wfTimestamp(
1724 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
1727 $olderRevison = new Revision(
1730 'page' => $page->getId(),
1731 'title' => $page->getTitle(),
1732 'comment' => __METHOD__
,
1733 'minor_edit' => true,
1734 'text' => __METHOD__
. '-text',
1735 'len' => strlen( __METHOD__
. '-text' ),
1736 'user' => $user->getId(),
1737 'user_text' => $user->getName(),
1738 'timestamp' => $olderTimeStamp,
1739 'content_model' => CONTENT_MODEL_WIKITEXT
,
1740 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1744 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
1745 $this->assertFalse( $result );
1749 * @covers WikiPage::updateIfNewerOn
1751 public function testUpdateIfNewerOn_newerRevision() {
1752 $user = $this->getTestSysop()->getUser();
1753 $page = $this->createPage( __METHOD__
, 'StartText' );
1754 $initialRevision = $page->getRevision();
1756 $newerTimeStamp = wfTimestamp(
1758 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
1761 $newerRevision = new Revision(
1764 'page' => $page->getId(),
1765 'title' => $page->getTitle(),
1766 'comment' => __METHOD__
,
1767 'minor_edit' => true,
1768 'text' => __METHOD__
. '-text',
1769 'len' => strlen( __METHOD__
. '-text' ),
1770 'user' => $user->getId(),
1771 'user_text' => $user->getName(),
1772 'timestamp' => $newerTimeStamp,
1773 'content_model' => CONTENT_MODEL_WIKITEXT
,
1774 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1777 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
1778 $this->assertTrue( $result );
1782 * @covers WikiPage::insertOn
1784 public function testInsertOn() {
1785 $title = Title
::newFromText( __METHOD__
);
1786 $page = new WikiPage( $title );
1788 $startTimeStamp = wfTimestampNow();
1789 $result = $page->insertOn( $this->db
);
1790 $endTimeStamp = wfTimestampNow();
1792 $this->assertInternalType( 'int', $result );
1793 $this->assertTrue( $result > 0 );
1795 $condition = [ 'page_id' => $result ];
1797 // Check the default fields have been filled
1798 $this->assertSelect(
1803 'page_restrictions',
1821 // Check the page_random field has been filled
1822 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
1823 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
1825 // Assert the touched timestamp in the DB is roughly when we inserted the page
1826 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
1828 wfTimestamp( TS_UNIX
, $startTimeStamp )
1829 <= wfTimestamp( TS_UNIX
, $pageTouched )
1832 wfTimestamp( TS_UNIX
, $endTimeStamp )
1833 >= wfTimestamp( TS_UNIX
, $pageTouched )
1836 // Try inserting the same page again and checking the result is false (no change)
1837 $result = $page->insertOn( $this->db
);
1838 $this->assertFalse( $result );
1842 * @covers WikiPage::insertOn
1844 public function testInsertOn_idSpecified() {
1845 $title = Title
::newFromText( __METHOD__
);
1846 $page = new WikiPage( $title );
1849 $result = $page->insertOn( $this->db
, $id );
1851 $this->assertSame( $id, $result );
1853 $condition = [ 'page_id' => $result ];
1855 // Check there is actually a row in the db
1856 $this->assertSelect(
1864 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
1865 // Note: Once the current dates passes the date in these tests they will fail.
1866 yield
'move something' => [
1868 [ 'move' => 'something' ],
1870 [ 'edit' => [], 'move' => [ 'something' ] ],
1873 yield
'move something, edit blank' => [
1875 [ 'move' => 'something', 'edit' => '' ],
1877 [ 'edit' => [], 'move' => [ 'something' ] ],
1880 yield
'edit sysop, with expiry' => [
1882 [ 'edit' => 'sysop' ],
1883 [ 'edit' => '21330101020202' ],
1884 [ 'edit' => [ 'sysop' ], 'move' => [] ],
1885 [ 'edit' => '21330101020202' ],
1887 yield
'move and edit, move with expiry' => [
1889 [ 'move' => 'something', 'edit' => 'another' ],
1890 [ 'move' => '22220202010101' ],
1891 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1892 [ 'move' => '22220202010101' ],
1894 yield
'move and edit, edit with infinity expiry' => [
1896 [ 'move' => 'something', 'edit' => 'another' ],
1897 [ 'edit' => 'infinity' ],
1898 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1899 [ 'edit' => 'infinity' ],
1901 yield
'non existing, create something' => [
1903 [ 'create' => 'something' ],
1905 [ 'create' => [ 'something' ] ],
1908 yield
'non existing, create something with expiry' => [
1910 [ 'create' => 'something' ],
1911 [ 'create' => '23451212112233' ],
1912 [ 'create' => [ 'something' ] ],
1913 [ 'create' => '23451212112233' ],
1918 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
1919 * @covers WikiPage::doUpdateRestrictions
1921 public function testDoUpdateRestrictions_setBasicRestrictions(
1925 array $expectedRestrictions,
1926 array $expectedRestrictionExpiries
1928 if ( $pageExists ) {
1929 $page = $this->createPage( __METHOD__
, 'ABC' );
1931 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
1933 $user = $this->getTestSysop()->getUser();
1936 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
1938 $logId = $status->getValue();
1939 $allRestrictions = $page->getTitle()->getAllRestrictions();
1941 $this->assertTrue( $status->isGood() );
1942 $this->assertInternalType( 'int', $logId );
1943 $this->assertSame( $expectedRestrictions, $allRestrictions );
1944 foreach ( $expectedRestrictionExpiries as $key => $value ) {
1945 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
1948 // Make sure the log entry looks good
1949 // log_params is not checked here
1950 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
1951 $this->assertSelect(
1952 [ 'logging' ] +
$actorQuery['tables'],
1955 'log_user' => $actorQuery['fields']['log_user'],
1956 'log_user_text' => $actorQuery['fields']['log_user_text'],
1960 [ 'log_id' => $logId ],
1963 (string)$user->getId(),
1965 (string)$page->getTitle()->getNamespace(),
1966 $page->getTitle()->getDBkey(),
1969 $actorQuery['joins']
1974 * @covers WikiPage::doUpdateRestrictions
1976 public function testDoUpdateRestrictions_failsOnReadOnly() {
1977 $page = $this->createPage( __METHOD__
, 'ABC' );
1978 $user = $this->getTestSysop()->getUser();
1982 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
1983 ->disableOriginalConstructor()
1984 ->setMethods( [ 'isReadOnly', 'getReason' ] )
1986 $readOnly->expects( $this->once() )
1987 ->method( 'isReadOnly' )
1988 ->will( $this->returnValue( true ) );
1989 $readOnly->expects( $this->once() )
1990 ->method( 'getReason' )
1991 ->will( $this->returnValue( 'Some Read Only Reason' ) );
1992 $this->setService( 'ReadOnlyMode', $readOnly );
1994 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
1995 $this->assertFalse( $status->isOK() );
1996 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
2000 * @covers WikiPage::doUpdateRestrictions
2002 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
2003 $page = $this->createPage( __METHOD__
, 'ABC' );
2004 $user = $this->getTestSysop()->getUser();
2006 $limit = [ 'edit' => 'sysop' ];
2008 $status = $page->doUpdateRestrictions(
2017 // The first entry should have a logId as it did something
2018 $this->assertTrue( $status->isGood() );
2019 $this->assertInternalType( 'int', $status->getValue() );
2021 $status = $page->doUpdateRestrictions(
2030 // The second entry should not have a logId as nothing changed
2031 $this->assertTrue( $status->isGood() );
2032 $this->assertNull( $status->getValue() );
2036 * @covers WikiPage::doUpdateRestrictions
2038 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
2039 $page = $this->createPage( __METHOD__
, 'ABC' );
2040 $user = $this->getTestSysop()->getUser();
2044 $status = $page->doUpdateRestrictions(
2045 [ 'edit' => 'sysop' ],
2052 $this->assertTrue( $status->isGood() );
2053 $this->assertInternalType( 'int', $status->getValue() );
2054 $this->assertSelect(
2056 [ 'log_type', 'log_action' ],
2057 [ 'log_id' => $status->getValue() ],
2058 [ [ 'protect', 'protect' ] ]
2061 // Modify the protection
2062 $status = $page->doUpdateRestrictions(
2063 [ 'edit' => 'somethingElse' ],
2070 $this->assertTrue( $status->isGood() );
2071 $this->assertInternalType( 'int', $status->getValue() );
2072 $this->assertSelect(
2074 [ 'log_type', 'log_action' ],
2075 [ 'log_id' => $status->getValue() ],
2076 [ [ 'protect', 'modify' ] ]
2079 // Remove the protection
2080 $status = $page->doUpdateRestrictions(
2088 $this->assertTrue( $status->isGood() );
2089 $this->assertInternalType( 'int', $status->getValue() );
2090 $this->assertSelect(
2092 [ 'log_type', 'log_action' ],
2093 [ 'log_id' => $status->getValue() ],
2094 [ [ 'protect', 'unprotect' ] ]