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(
33 protected function addCoreDBData() {
34 // Blank out. This would fail with a modified schema, and we don't need it.
40 abstract protected function getMcrMigrationStage();
45 abstract protected function getMcrTablesToReset();
47 protected function setUp() {
50 $this->tablesUsed +
= $this->getMcrTablesToReset();
52 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
54 'wgMultiContentRevisionSchemaMigrationStage',
55 $this->getMcrMigrationStage()
57 $this->pagesToDelete
= [];
59 $this->overrideMwServices();
62 protected function tearDown() {
63 foreach ( $this->pagesToDelete
as $p ) {
64 /* @var $p WikiPage */
68 $p->doDeleteArticle( "testing done." );
70 } catch ( MWException
$ex ) {
77 abstract protected function getContentHandlerUseDB();
80 * @param Title|string $title
81 * @param string|null $model
84 private function newPage( $title, $model = null ) {
85 if ( is_string( $title ) ) {
86 $ns = $this->getDefaultWikitextNS();
87 $title = Title
::newFromText( $title, $ns );
90 $p = new WikiPage( $title );
92 $this->pagesToDelete
[] = $p;
98 * @param string|Title|WikiPage $page
104 protected function createPage( $page, $text, $model = null ) {
105 if ( is_string( $page ) ||
$page instanceof Title
) {
106 $page = $this->newPage( $page, $model );
109 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
110 $page->doEditContent( $content, "testing", EDIT_NEW
);
116 * @covers WikiPage::doEditContent
117 * @covers WikiPage::doModify
118 * @covers WikiPage::doCreate
119 * @covers WikiPage::doEditUpdates
121 public function testDoEditContent() {
122 $this->setMwGlobals( 'wgPageCreationLog', true );
124 $page = $this->newPage( __METHOD__
);
125 $title = $page->getTitle();
127 $content = ContentHandler
::makeContent(
128 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
129 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
131 CONTENT_MODEL_WIKITEXT
134 $page->doEditContent( $content, "[[testing]] 1" );
136 $id = $page->getId();
138 // Test page creation logging
141 [ 'log_type', 'log_action' ],
142 [ 'log_page' => $id ],
143 [ [ 'create', 'create' ] ]
146 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
147 $this->assertTrue( $id > 0, "WikiPage should have new page id" );
148 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
149 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
151 # ------------------------
152 $dbr = wfGetDB( DB_REPLICA
);
153 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
154 $n = $res->numRows();
157 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
159 # ------------------------
160 $page = new WikiPage( $title );
162 $retrieved = $page->getContent();
163 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
165 # ------------------------
166 $content = ContentHandler
::makeContent(
167 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
168 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
170 CONTENT_MODEL_WIKITEXT
173 $page->doEditContent( $content, "testing 2" );
175 # ------------------------
176 $page = new WikiPage( $title );
178 $retrieved = $page->getContent();
179 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
181 # ------------------------
182 $dbr = wfGetDB( DB_REPLICA
);
183 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
184 $n = $res->numRows();
187 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
191 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
192 * TODO: Revision deletion
194 * @covers WikiPage::doDeleteArticle
195 * @covers WikiPage::doDeleteArticleReal
197 public function testDoDeleteArticle() {
198 $page = $this->createPage(
200 "[[original text]] foo",
201 CONTENT_MODEL_WIKITEXT
203 $id = $page->getId();
205 $page->doDeleteArticle( "testing deletion" );
208 $page->getTitle()->getArticleID() > 0,
209 "Title object should now have page id 0"
211 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
214 "WikiPage::exists should return false after page was deleted"
218 "WikiPage::getContent should return null after page was deleted"
221 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
224 "Title::exists should return false after page was deleted"
228 JobQueueGroup
::destroySingletons();
230 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
233 # ------------------------
234 $dbr = wfGetDB( DB_REPLICA
);
235 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
236 $n = $res->numRows();
239 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
243 * @covers WikiPage::doDeleteArticleReal
245 public function testDoDeleteArticleReal_user0() {
246 $page = $this->createPage(
248 "[[original text]] foo",
249 CONTENT_MODEL_WIKITEXT
251 $id = $page->getId();
254 $status = $page->doDeleteArticleReal(
255 /* reason */ "testing user 0 deletion",
256 /* suppress */ false,
259 /* errorStack */ $errorStack,
262 $logId = $status->getValue();
263 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
265 [ 'logging' ] +
$actorQuery['tables'], /* table */
270 'log_user' => $actorQuery['fields']['log_user'],
271 'log_user_text' => $actorQuery['fields']['log_user_text'],
275 [ 'log_id' => $logId ],
279 'testing user 0 deletion',
282 (string)$page->getTitle()->getNamespace(),
283 $page->getTitle()->getDBkey(),
291 * @covers WikiPage::doDeleteArticleReal
293 public function testDoDeleteArticleReal_userSysop() {
294 $page = $this->createPage(
296 "[[original text]] foo",
297 CONTENT_MODEL_WIKITEXT
299 $id = $page->getId();
301 $user = $this->getTestSysop()->getUser();
303 $status = $page->doDeleteArticleReal(
304 /* reason */ "testing sysop deletion",
305 /* suppress */ false,
308 /* errorStack */ $errorStack,
311 $logId = $status->getValue();
312 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
314 [ 'logging' ] +
$actorQuery['tables'], /* table */
319 'log_user' => $actorQuery['fields']['log_user'],
320 'log_user_text' => $actorQuery['fields']['log_user_text'],
324 [ 'log_id' => $logId ],
328 'testing sysop deletion',
329 (string)$user->getId(),
331 (string)$page->getTitle()->getNamespace(),
332 $page->getTitle()->getDBkey(),
340 * TODO: Test more stuff about suppression.
342 * @covers WikiPage::doDeleteArticleReal
344 public function testDoDeleteArticleReal_suppress() {
345 $page = $this->createPage(
347 "[[original text]] foo",
348 CONTENT_MODEL_WIKITEXT
350 $id = $page->getId();
352 $user = $this->getTestSysop()->getUser();
354 $status = $page->doDeleteArticleReal(
355 /* reason */ "testing deletion",
359 /* errorStack */ $errorStack,
362 $logId = $status->getValue();
363 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
365 [ 'logging' ] +
$actorQuery['tables'], /* table */
370 'log_user' => $actorQuery['fields']['log_user'],
371 'log_user_text' => $actorQuery['fields']['log_user_text'],
375 [ 'log_id' => $logId ],
380 (string)$user->getId(),
382 (string)$page->getTitle()->getNamespace(),
383 $page->getTitle()->getDBkey(),
390 $page->getContent( Revision
::FOR_PUBLIC
),
391 "WikiPage::getContent should return null after the page was suppressed for general users"
395 $page->getContent( Revision
::FOR_THIS_USER
, null ),
396 "WikiPage::getContent should return null after the page was suppressed for user zero"
400 $page->getContent( Revision
::FOR_THIS_USER
, $user ),
401 "WikiPage::getContent should return null after the page was suppressed even for a sysop"
406 * @covers WikiPage::doDeleteUpdates
408 public function testDoDeleteUpdates() {
409 $page = $this->createPage(
411 "[[original text]] foo",
412 CONTENT_MODEL_WIKITEXT
414 $id = $page->getId();
416 // Similar to MovePage logic
417 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
418 $page->doDeleteUpdates( $id );
421 JobQueueGroup
::destroySingletons();
423 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
426 # ------------------------
427 $dbr = wfGetDB( DB_REPLICA
);
428 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
429 $n = $res->numRows();
432 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
436 * @covers WikiPage::getRevision
438 public function testGetRevision() {
439 $page = $this->newPage( __METHOD__
);
441 $rev = $page->getRevision();
442 $this->assertNull( $rev );
445 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
447 $rev = $page->getRevision();
449 $this->assertEquals( $page->getLatest(), $rev->getId() );
450 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
454 * @covers WikiPage::getContent
456 public function testGetContent() {
457 $page = $this->newPage( __METHOD__
);
459 $content = $page->getContent();
460 $this->assertNull( $content );
463 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
465 $content = $page->getContent();
466 $this->assertEquals( "some text", $content->getNativeData() );
470 * @covers WikiPage::exists
472 public function testExists() {
473 $page = $this->newPage( __METHOD__
);
474 $this->assertFalse( $page->exists() );
477 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
478 $this->assertTrue( $page->exists() );
480 $page = new WikiPage( $page->getTitle() );
481 $this->assertTrue( $page->exists() );
484 $page->doDeleteArticle( "done testing" );
485 $this->assertFalse( $page->exists() );
487 $page = new WikiPage( $page->getTitle() );
488 $this->assertFalse( $page->exists() );
491 public function provideHasViewableContent() {
493 [ 'WikiPageTest_testHasViewableContent', false, true ],
494 [ 'Special:WikiPageTest_testHasViewableContent', false ],
495 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
496 [ 'Special:Userlogin', true ],
497 [ 'MediaWiki:help', true ],
502 * @dataProvider provideHasViewableContent
503 * @covers WikiPage::hasViewableContent
505 public function testHasViewableContent( $title, $viewable, $create = false ) {
506 $page = $this->newPage( $title );
507 $this->assertEquals( $viewable, $page->hasViewableContent() );
510 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
511 $this->assertTrue( $page->hasViewableContent() );
513 $page = new WikiPage( $page->getTitle() );
514 $this->assertTrue( $page->hasViewableContent() );
518 public function provideGetRedirectTarget() {
520 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
522 'WikiPageTest_testGetRedirectTarget_2',
523 CONTENT_MODEL_WIKITEXT
,
524 "#REDIRECT [[hello world]]",
531 * @dataProvider provideGetRedirectTarget
532 * @covers WikiPage::getRedirectTarget
534 public function testGetRedirectTarget( $title, $model, $text, $target ) {
535 $this->setMwGlobals( [
536 'wgCapitalLinks' => true,
539 $page = $this->createPage( $title, $text, $model );
541 # sanity check, because this test seems to fail for no reason for some people.
542 $c = $page->getContent();
543 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
545 # now, test the actual redirect
546 $t = $page->getRedirectTarget();
547 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
551 * @dataProvider provideGetRedirectTarget
552 * @covers WikiPage::isRedirect
554 public function testIsRedirect( $title, $model, $text, $target ) {
555 $page = $this->createPage( $title, $text, $model );
556 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
559 public function provideIsCountable() {
563 [ 'WikiPageTest_testIsCountable',
564 CONTENT_MODEL_WIKITEXT
,
569 [ 'WikiPageTest_testIsCountable',
570 CONTENT_MODEL_WIKITEXT
,
577 [ 'WikiPageTest_testIsCountable',
578 CONTENT_MODEL_WIKITEXT
,
583 [ 'WikiPageTest_testIsCountable',
584 CONTENT_MODEL_WIKITEXT
,
591 [ 'WikiPageTest_testIsCountable',
592 CONTENT_MODEL_WIKITEXT
,
597 [ 'WikiPageTest_testIsCountable',
598 CONTENT_MODEL_WIKITEXT
,
604 // not a content namespace
605 [ 'Talk:WikiPageTest_testIsCountable',
606 CONTENT_MODEL_WIKITEXT
,
611 [ 'Talk:WikiPageTest_testIsCountable',
612 CONTENT_MODEL_WIKITEXT
,
618 // not a content namespace, different model
619 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
625 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
635 * @dataProvider provideIsCountable
636 * @covers WikiPage::isCountable
638 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
639 global $wgContentHandlerUseDB;
641 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
643 $title = Title
::newFromText( $title );
645 if ( !$wgContentHandlerUseDB
647 && ContentHandler
::getDefaultModelFor( $title ) != $model
649 $this->markTestSkipped( "Can not use non-default content model $model for "
650 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
653 $page = $this->createPage( $title, $text, $model );
655 $editInfo = $page->prepareContentForEdit( $page->getContent() );
657 $v = $page->isCountable();
658 $w = $page->isCountable( $editInfo );
663 "isCountable( null ) returned unexpected value " . var_export( $v, true )
664 . " instead of " . var_export( $expected, true )
665 . " in mode `$mode` for text \"$text\""
671 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
672 . " instead of " . var_export( $expected, true )
673 . " in mode `$mode` for text \"$text\""
677 public function provideGetParserOutput() {
680 CONTENT_MODEL_WIKITEXT
,
682 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
689 * @dataProvider provideGetParserOutput
690 * @covers WikiPage::getParserOutput
692 public function testGetParserOutput( $model, $text, $expectedHtml ) {
693 $page = $this->createPage( __METHOD__
, $text, $model );
695 $opt = $page->makeParserOptions( 'canonical' );
696 $po = $page->getParserOutput( $opt );
697 $text = $po->getText();
699 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
700 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
702 $this->assertEquals( $expectedHtml, $text );
708 * @covers WikiPage::getParserOutput
710 public function testGetParserOutput_nonexisting() {
711 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
713 $opt = new ParserOptions();
714 $po = $page->getParserOutput( $opt );
716 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
720 * @covers WikiPage::getParserOutput
722 public function testGetParserOutput_badrev() {
723 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
725 $opt = new ParserOptions();
726 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
728 // @todo would be neat to also test deleted revision
730 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
733 public static $sections =
747 public function dataReplaceSection() {
748 // NOTE: assume the Help namespace to contain wikitext
750 [ 'Help:WikiPageTest_testReplaceSection',
751 CONTENT_MODEL_WIKITEXT
,
756 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
758 [ 'Help:WikiPageTest_testReplaceSection',
759 CONTENT_MODEL_WIKITEXT
,
766 [ 'Help:WikiPageTest_testReplaceSection',
767 CONTENT_MODEL_WIKITEXT
,
770 "== TEST ==\nmore fun",
772 trim( preg_replace( '/^== test ==.*== foo ==/sm',
773 "== TEST ==\nmore fun\n\n== foo ==",
776 [ 'Help:WikiPageTest_testReplaceSection',
777 CONTENT_MODEL_WIKITEXT
,
782 trim( self
::$sections )
784 [ 'Help:WikiPageTest_testReplaceSection',
785 CONTENT_MODEL_WIKITEXT
,
790 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
796 * @dataProvider dataReplaceSection
797 * @covers WikiPage::replaceSectionContent
799 public function testReplaceSectionContent( $title, $model, $text, $section,
800 $with, $sectionTitle, $expected
802 $page = $this->createPage( $title, $text, $model );
804 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
805 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
807 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
811 * @dataProvider dataReplaceSection
812 * @covers WikiPage::replaceSectionAtRev
814 public function testReplaceSectionAtRev( $title, $model, $text, $section,
815 $with, $sectionTitle, $expected
817 $page = $this->createPage( $title, $text, $model );
818 $baseRevId = $page->getLatest();
820 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
821 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
823 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
827 * @covers WikiPage::getOldestRevision
829 public function testGetOldestRevision() {
830 $page = $this->newPage( __METHOD__
);
831 $page->doEditContent(
832 new WikitextContent( 'one' ),
836 $rev1 = $page->getRevision();
838 $page = new WikiPage( $page->getTitle() );
839 $page->doEditContent(
840 new WikitextContent( 'two' ),
845 $page = new WikiPage( $page->getTitle() );
846 $page->doEditContent(
847 new WikitextContent( 'three' ),
853 $this->assertNotEquals(
855 $page->getRevision()->getId(),
856 '$page->getRevision()->getId()'
862 $page->getOldestRevision()->getId(),
863 '$page->getOldestRevision()->getId()'
868 * @covers WikiPage::doRollback
869 * @covers WikiPage::commitRollback
871 public function testDoRollback() {
872 $admin = $this->getTestSysop()->getUser();
873 $user1 = $this->getTestUser()->getUser();
874 // Use the confirmed group for user2 to make sure the user is different
875 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
877 $page = $this->newPage( __METHOD__
);
881 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
882 "section one", EDIT_NEW
, false, $admin );
885 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
886 "adding section two", 0, false, $user1 );
888 $text .= "\n\nthree";
889 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
890 "adding section three", 0, false, $user2 );
892 /** @var Revision $rev1 */
893 /** @var Revision $rev2 */
894 /** @var Revision $rev3 */
895 $rev1 = $status1->getValue()['revision'];
896 $rev2 = $status2->getValue()['revision'];
897 $rev3 = $status3->getValue()['revision'];
900 * We are having issues with doRollback spuriously failing. Apparently
901 * the last revision somehow goes missing or not committed under some
902 * circumstances. So, make sure the revisions have the correct usernames.
904 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
905 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
906 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
907 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
909 // Now, try the actual rollback
910 $token = $admin->getEditToken( 'rollback' );
911 $rollbackErrors = $page->doRollback(
920 if ( $rollbackErrors ) {
922 "Rollback failed:\n" .
923 print_r( $rollbackErrors, true ) . ";\n" .
924 print_r( $resultDetails, true )
928 $page = new WikiPage( $page->getTitle() );
929 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
930 "rollback did not revert to the correct revision" );
931 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
935 * @covers WikiPage::doRollback
936 * @covers WikiPage::commitRollback
938 public function testDoRollback_simple() {
939 $admin = $this->getTestSysop()->getUser();
942 $page = $this->newPage( __METHOD__
);
943 $page->doEditContent(
944 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
950 $rev1 = $page->getRevision();
952 $user1 = $this->getTestUser()->getUser();
954 $page = new WikiPage( $page->getTitle() );
955 $page->doEditContent(
956 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
957 "adding section two",
963 # now, try the rollback
964 $token = $admin->getEditToken( 'rollback' );
965 $errors = $page->doRollback(
975 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
976 . ";\n" . print_r( $details, true ) );
979 $page = new WikiPage( $page->getTitle() );
980 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
981 "rollback did not revert to the correct revision" );
982 $this->assertEquals( "one", $page->getContent()->getNativeData() );
986 * @covers WikiPage::doRollback
987 * @covers WikiPage::commitRollback
989 public function testDoRollbackFailureSameContent() {
990 $admin = $this->getTestSysop()->getUser();
993 $page = $this->newPage( __METHOD__
);
994 $page->doEditContent(
995 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1001 $rev1 = $page->getRevision();
1003 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
1005 $page = new WikiPage( $page->getTitle() );
1006 $page->doEditContent(
1007 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1008 "adding section two",
1014 # now, do a the rollback from the same user was doing the edit before
1015 $resultDetails = [];
1016 $token = $user1->getEditToken( 'rollback' );
1017 $errors = $page->doRollback(
1019 "testing revert same user",
1026 $this->assertEquals( [], $errors, "Rollback failed same user" );
1028 # now, try the rollback
1029 $resultDetails = [];
1030 $token = $admin->getEditToken( 'rollback' );
1031 $errors = $page->doRollback(
1040 $this->assertEquals(
1050 "Rollback not failed"
1053 $page = new WikiPage( $page->getTitle() );
1054 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1055 "rollback did not revert to the correct revision" );
1056 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1060 * Tests tagging for edits that do rollback action
1061 * @covers WikiPage::doRollback
1063 public function testDoRollbackTagging() {
1064 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
1065 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
1068 $admin = new User();
1069 $admin->setName( 'Administrator' );
1070 $admin->addToDatabase();
1072 $text = 'First line';
1073 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
1074 $page->doEditContent(
1075 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1082 $secondUser = new User();
1083 $secondUser->setName( '92.65.217.32' );
1084 $text .= '\n\nSecond line';
1085 $page = new WikiPage( $page->getTitle() );
1086 $page->doEditContent(
1087 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1088 'Adding second line',
1094 // Now, try the rollback
1095 $admin->addGroup( 'sysop' ); // Make the test user a sysop
1096 $token = $admin->getEditToken( 'rollback' );
1097 $errors = $page->doRollback(
1098 $secondUser->getName(),
1106 // If doRollback completed without errors
1107 if ( $errors === [] ) {
1108 $tags = $resultDetails[ 'tags' ];
1109 $this->assertContains( 'mw-rollback', $tags );
1113 public function provideGetAutoDeleteReason() {
1123 [ "first edit", null ],
1125 "/first edit.*only contributor/",
1131 [ "first edit", null ],
1132 [ "second edit", null ],
1134 "/second edit.*only contributor/",
1140 [ "first edit", "127.0.2.22" ],
1141 [ "second edit", "127.0.3.33" ],
1151 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1152 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1153 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1154 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1155 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1159 '/first edit:.*\.\.\."/',
1165 [ "first edit", "127.0.2.22" ],
1166 [ "", "127.0.3.33" ],
1168 "/before blanking.*first edit/",
1176 * @dataProvider provideGetAutoDeleteReason
1177 * @covers WikiPage::getAutoDeleteReason
1179 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1182 // NOTE: assume Help namespace to contain wikitext
1183 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1187 foreach ( $edits as $edit ) {
1190 if ( !empty( $edit[1] ) ) {
1191 $user->setName( $edit[1] );
1196 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1198 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1203 $reason = $page->getAutoDeleteReason( $hasHistory );
1205 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1206 $this->assertEquals( $expectedResult, $reason );
1208 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1209 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1212 $this->assertEquals( $expectedHistory, $hasHistory,
1213 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1215 $page->doDeleteArticle( "done" );
1218 public function providePreSaveTransform() {
1220 [ 'hello this is ~~~',
1221 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1223 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1224 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1230 * @covers WikiPage::factory
1232 public function testWikiPageFactory() {
1233 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1234 $page = WikiPage
::factory( $title );
1235 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1237 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1238 $page = WikiPage
::factory( $title );
1239 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1241 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1242 $page = WikiPage
::factory( $title );
1243 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1247 * @dataProvider provideCommentMigrationOnDeletion
1249 * @param int $writeStage
1250 * @param int $readStage
1252 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1253 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1254 $this->overrideMwServices();
1256 $dbr = wfGetDB( DB_REPLICA
);
1258 $page = $this->createPage(
1261 CONTENT_MODEL_WIKITEXT
1263 $revid = $page->getLatest();
1264 if ( $writeStage > MIGRATION_OLD
) {
1265 $comment_id = $dbr->selectField(
1266 'revision_comment_temp',
1267 'revcomment_comment_id',
1268 [ 'revcomment_rev' => $revid ],
1273 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1274 $this->overrideMwServices();
1276 $page->doDeleteArticle( "testing deletion" );
1278 if ( $readStage > MIGRATION_OLD
) {
1279 // Didn't leave behind any 'revision_comment_temp' rows
1280 $n = $dbr->selectField(
1281 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1283 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1285 // Copied or upgraded the comment_id, as applicable
1286 $ar_comment_id = $dbr->selectField(
1289 [ 'ar_rev_id' => $revid ],
1292 if ( $writeStage > MIGRATION_OLD
) {
1293 $this->assertSame( $comment_id, $ar_comment_id );
1295 $this->assertNotEquals( 0, $ar_comment_id );
1299 // Copied rev_comment, if applicable
1300 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1301 $ar_comment = $dbr->selectField(
1304 [ 'ar_rev_id' => $revid ],
1307 $this->assertSame( 'testing', $ar_comment );
1311 public function provideCommentMigrationOnDeletion() {
1313 [ MIGRATION_OLD
, MIGRATION_OLD
],
1314 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1315 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1316 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1317 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1318 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1319 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1320 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1321 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1322 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1323 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1324 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1325 [ MIGRATION_NEW
, MIGRATION_NEW
],
1330 * @covers WikiPage::updateCategoryCounts
1332 public function testUpdateCategoryCounts() {
1333 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1335 // Add an initial category
1336 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1338 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1339 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1340 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1342 // Add a new category
1343 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1345 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1346 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1347 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1349 // Add and remove a category
1350 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1352 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1353 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1354 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1357 public function provideUpdateRedirectOn() {
1358 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1359 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1360 yield
[ 'SomeText', false, null, false, true, 0 ];
1361 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1365 * @dataProvider provideUpdateRedirectOn
1366 * @covers WikiPage::updateRedirectOn
1368 * @param string $initialText
1369 * @param bool $initialRedirectState
1370 * @param string|null $redirectTitle
1371 * @param bool|null $lastRevIsRedirect
1372 * @param bool $expectedSuccess
1373 * @param int $expectedRowCount
1375 public function testUpdateRedirectOn(
1377 $initialRedirectState,
1383 static $pageCounter = 0;
1386 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1387 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1389 $redirectTitle = is_string( $redirectTitle )
1390 ? Title
::newFromText( $redirectTitle )
1393 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1394 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1396 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1397 * Most of core checks the page table for redirect status, so we have to be ugly and
1398 * assert a select from the table here.
1400 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1403 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1404 $this->assertSelect(
1407 [ 'rd_from' => $pageId ],
1408 [ [ strval( $expected ) ] ]
1413 * @covers WikiPage::insertRedirectEntry
1415 public function testInsertRedirectEntry_insertsRedirectEntry() {
1416 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1417 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1419 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1420 $targetTitle->mInterwiki
= 'eninter';
1421 $page->insertRedirectEntry( $targetTitle, null );
1423 $this->assertSelect(
1425 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1426 [ 'rd_from' => $page->getId() ],
1428 strval( $page->getId() ),
1429 strval( $targetTitle->getNamespace() ),
1430 strval( $targetTitle->getDBkey() ),
1431 strval( $targetTitle->getFragment() ),
1432 strval( $targetTitle->getInterwiki() ),
1438 * @covers WikiPage::insertRedirectEntry
1440 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1441 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1442 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1444 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1445 $targetTitle->mInterwiki
= 'eninter';
1446 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1448 $this->assertSelect(
1450 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1451 [ 'rd_from' => $page->getId() ],
1453 strval( $page->getId() ),
1454 strval( $targetTitle->getNamespace() ),
1455 strval( $targetTitle->getDBkey() ),
1456 strval( $targetTitle->getFragment() ),
1457 strval( $targetTitle->getInterwiki() ),
1463 * @covers WikiPage::insertRedirectEntry
1465 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1466 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1467 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1469 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1470 $targetTitle->mInterwiki
= 'eninter';
1471 $page->insertRedirectEntry( $targetTitle, 215251 );
1473 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1476 private function getRow( array $overrides = [] ) {
1480 'page_is_redirect' => '1',
1481 'page_latest' => '99',
1482 'page_namespace' => '3',
1483 'page_title' => 'JaJaTitle',
1484 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1485 'page_touched' => '20120101020202',
1486 'page_links_updated' => '20140101020202',
1488 foreach ( $overrides as $key => $value ) {
1489 $row[$key] = $value;
1491 return (object)$row;
1494 public function provideNewFromRowSuccess() {
1495 yield
'basic row' => [
1497 function ( WikiPage
$wikiPage, self
$test ) {
1498 $test->assertSame( 44, $wikiPage->getId() );
1499 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1500 $test->assertTrue( $wikiPage->isRedirect() );
1501 $test->assertSame( 99, $wikiPage->getLatest() );
1502 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1503 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1506 'edit' => [ 'autoconfirmed', 'sysop' ],
1507 'move' => [ 'sysop' ],
1509 $wikiPage->getTitle()->getAllRestrictions()
1511 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1512 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1515 yield
'different timestamp formats' => [
1517 'page_touched' => '2012-01-01 02:02:02',
1518 'page_links_updated' => '2014-01-01 02:02:02',
1520 function ( WikiPage
$wikiPage, self
$test ) {
1521 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1522 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1525 yield
'no restrictions' => [
1527 'page_restrictions' => '',
1529 function ( WikiPage
$wikiPage, self
$test ) {
1535 $wikiPage->getTitle()->getAllRestrictions()
1539 yield
'not redirect' => [
1541 'page_is_redirect' => '0',
1543 function ( WikiPage
$wikiPage, self
$test ) {
1544 $test->assertFalse( $wikiPage->isRedirect() );
1550 * @covers WikiPage::newFromRow
1551 * @covers WikiPage::loadFromRow
1552 * @dataProvider provideNewFromRowSuccess
1554 * @param object $row
1555 * @param callable $assertions
1557 public function testNewFromRow( $row, $assertions ) {
1558 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1559 $assertions( $page, $this );
1562 public function provideTestNewFromId_returnsNullOnBadPageId() {
1568 * @covers WikiPage::newFromID
1569 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1571 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1572 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1576 * @covers WikiPage::newFromID
1578 public function testNewFromId_appearsToFetchCorrectRow() {
1579 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1580 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1581 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1582 $this->assertEquals(
1583 $createdPage->getContent()->getNativeData(),
1584 $fetchedPage->getContent()->getNativeData()
1589 * @covers WikiPage::newFromID
1591 public function testNewFromId_returnsNullOnNonExistingId() {
1592 $this->assertNull( WikiPage
::newFromID( 2147483647 ) );
1595 public function provideTestInsertProtectNullRevision() {
1596 // phpcs:disable Generic.Files.LineLength
1599 [ 'edit' => 'sysop' ],
1600 [ 'edit' => '20200101040404' ],
1604 '(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)))'
1608 [ 'edit' => 'sysop', 'move' => 'something' ],
1609 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1613 '(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)))'
1619 * @dataProvider provideTestInsertProtectNullRevision
1620 * @covers WikiPage::insertProtectNullRevision
1621 * @covers WikiPage::protectDescription
1623 * @param string $revCommentMsg
1624 * @param array $limit
1625 * @param array $expiry
1626 * @param bool $cascade
1627 * @param string $reason
1628 * @param bool|null $user true if the test sysop should be used, or null
1629 * @param string $expectedComment
1631 public function testInsertProtectNullRevision(
1640 $this->setContentLang( 'qqx' );
1642 $page = $this->createPage( __METHOD__
, 'Goat' );
1644 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1646 $result = $page->insertProtectNullRevision(
1655 $this->assertTrue( $result instanceof Revision
);
1656 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1660 * @covers WikiPage::updateRevisionOn
1662 public function testUpdateRevisionOn_existingPage() {
1663 $user = $this->getTestSysop()->getUser();
1664 $page = $this->createPage( __METHOD__
, 'StartText' );
1666 $revision = new Revision(
1669 'page' => $page->getId(),
1670 'title' => $page->getTitle(),
1671 'comment' => __METHOD__
,
1672 'minor_edit' => true,
1673 'text' => __METHOD__
. '-text',
1674 'len' => strlen( __METHOD__
. '-text' ),
1675 'user' => $user->getId(),
1676 'user_text' => $user->getName(),
1677 'timestamp' => '20170707040404',
1678 'content_model' => CONTENT_MODEL_WIKITEXT
,
1679 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1683 $result = $page->updateRevisionOn( $this->db
, $revision );
1684 $this->assertTrue( $result );
1685 $this->assertSame( 9989, $page->getLatest() );
1686 $this->assertEquals( $revision, $page->getRevision() );
1690 * @covers WikiPage::updateRevisionOn
1692 public function testUpdateRevisionOn_NonExistingPage() {
1693 $user = $this->getTestSysop()->getUser();
1694 $page = $this->createPage( __METHOD__
, 'StartText' );
1695 $page->doDeleteArticle( 'reason' );
1697 $revision = new Revision(
1700 'page' => $page->getId(),
1701 'title' => $page->getTitle(),
1702 'comment' => __METHOD__
,
1703 'minor_edit' => true,
1704 'text' => __METHOD__
. '-text',
1705 'len' => strlen( __METHOD__
. '-text' ),
1706 'user' => $user->getId(),
1707 'user_text' => $user->getName(),
1708 'timestamp' => '20170707040404',
1709 'content_model' => CONTENT_MODEL_WIKITEXT
,
1710 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1714 $result = $page->updateRevisionOn( $this->db
, $revision );
1715 $this->assertFalse( $result );
1719 * @covers WikiPage::updateIfNewerOn
1721 public function testUpdateIfNewerOn_olderRevision() {
1722 $user = $this->getTestSysop()->getUser();
1723 $page = $this->createPage( __METHOD__
, 'StartText' );
1724 $initialRevision = $page->getRevision();
1726 $olderTimeStamp = wfTimestamp(
1728 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
1731 $olderRevison = new Revision(
1734 'page' => $page->getId(),
1735 'title' => $page->getTitle(),
1736 'comment' => __METHOD__
,
1737 'minor_edit' => true,
1738 'text' => __METHOD__
. '-text',
1739 'len' => strlen( __METHOD__
. '-text' ),
1740 'user' => $user->getId(),
1741 'user_text' => $user->getName(),
1742 'timestamp' => $olderTimeStamp,
1743 'content_model' => CONTENT_MODEL_WIKITEXT
,
1744 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1748 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
1749 $this->assertFalse( $result );
1753 * @covers WikiPage::updateIfNewerOn
1755 public function testUpdateIfNewerOn_newerRevision() {
1756 $user = $this->getTestSysop()->getUser();
1757 $page = $this->createPage( __METHOD__
, 'StartText' );
1758 $initialRevision = $page->getRevision();
1760 $newerTimeStamp = wfTimestamp(
1762 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
1765 $newerRevision = new Revision(
1768 'page' => $page->getId(),
1769 'title' => $page->getTitle(),
1770 'comment' => __METHOD__
,
1771 'minor_edit' => true,
1772 'text' => __METHOD__
. '-text',
1773 'len' => strlen( __METHOD__
. '-text' ),
1774 'user' => $user->getId(),
1775 'user_text' => $user->getName(),
1776 'timestamp' => $newerTimeStamp,
1777 'content_model' => CONTENT_MODEL_WIKITEXT
,
1778 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1781 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
1782 $this->assertTrue( $result );
1786 * @covers WikiPage::insertOn
1788 public function testInsertOn() {
1789 $title = Title
::newFromText( __METHOD__
);
1790 $page = new WikiPage( $title );
1792 $startTimeStamp = wfTimestampNow();
1793 $result = $page->insertOn( $this->db
);
1794 $endTimeStamp = wfTimestampNow();
1796 $this->assertInternalType( 'int', $result );
1797 $this->assertTrue( $result > 0 );
1799 $condition = [ 'page_id' => $result ];
1801 // Check the default fields have been filled
1802 $this->assertSelect(
1807 'page_restrictions',
1825 // Check the page_random field has been filled
1826 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
1827 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
1829 // Assert the touched timestamp in the DB is roughly when we inserted the page
1830 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
1832 wfTimestamp( TS_UNIX
, $startTimeStamp )
1833 <= wfTimestamp( TS_UNIX
, $pageTouched )
1836 wfTimestamp( TS_UNIX
, $endTimeStamp )
1837 >= wfTimestamp( TS_UNIX
, $pageTouched )
1840 // Try inserting the same page again and checking the result is false (no change)
1841 $result = $page->insertOn( $this->db
);
1842 $this->assertFalse( $result );
1846 * @covers WikiPage::insertOn
1848 public function testInsertOn_idSpecified() {
1849 $title = Title
::newFromText( __METHOD__
);
1850 $page = new WikiPage( $title );
1853 $result = $page->insertOn( $this->db
, $id );
1855 $this->assertSame( $id, $result );
1857 $condition = [ 'page_id' => $result ];
1859 // Check there is actually a row in the db
1860 $this->assertSelect(
1868 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
1869 // Note: Once the current dates passes the date in these tests they will fail.
1870 yield
'move something' => [
1872 [ 'move' => 'something' ],
1874 [ 'edit' => [], 'move' => [ 'something' ] ],
1877 yield
'move something, edit blank' => [
1879 [ 'move' => 'something', 'edit' => '' ],
1881 [ 'edit' => [], 'move' => [ 'something' ] ],
1884 yield
'edit sysop, with expiry' => [
1886 [ 'edit' => 'sysop' ],
1887 [ 'edit' => '21330101020202' ],
1888 [ 'edit' => [ 'sysop' ], 'move' => [] ],
1889 [ 'edit' => '21330101020202' ],
1891 yield
'move and edit, move with expiry' => [
1893 [ 'move' => 'something', 'edit' => 'another' ],
1894 [ 'move' => '22220202010101' ],
1895 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1896 [ 'move' => '22220202010101' ],
1898 yield
'move and edit, edit with infinity expiry' => [
1900 [ 'move' => 'something', 'edit' => 'another' ],
1901 [ 'edit' => 'infinity' ],
1902 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1903 [ 'edit' => 'infinity' ],
1905 yield
'non existing, create something' => [
1907 [ 'create' => 'something' ],
1909 [ 'create' => [ 'something' ] ],
1912 yield
'non existing, create something with expiry' => [
1914 [ 'create' => 'something' ],
1915 [ 'create' => '23451212112233' ],
1916 [ 'create' => [ 'something' ] ],
1917 [ 'create' => '23451212112233' ],
1922 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
1923 * @covers WikiPage::doUpdateRestrictions
1925 public function testDoUpdateRestrictions_setBasicRestrictions(
1929 array $expectedRestrictions,
1930 array $expectedRestrictionExpiries
1932 if ( $pageExists ) {
1933 $page = $this->createPage( __METHOD__
, 'ABC' );
1935 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
1937 $user = $this->getTestSysop()->getUser();
1940 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
1942 $logId = $status->getValue();
1943 $allRestrictions = $page->getTitle()->getAllRestrictions();
1945 $this->assertTrue( $status->isGood() );
1946 $this->assertInternalType( 'int', $logId );
1947 $this->assertSame( $expectedRestrictions, $allRestrictions );
1948 foreach ( $expectedRestrictionExpiries as $key => $value ) {
1949 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
1952 // Make sure the log entry looks good
1953 // log_params is not checked here
1954 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
1955 $this->assertSelect(
1956 [ 'logging' ] +
$actorQuery['tables'],
1959 'log_user' => $actorQuery['fields']['log_user'],
1960 'log_user_text' => $actorQuery['fields']['log_user_text'],
1964 [ 'log_id' => $logId ],
1967 (string)$user->getId(),
1969 (string)$page->getTitle()->getNamespace(),
1970 $page->getTitle()->getDBkey(),
1973 $actorQuery['joins']
1978 * @covers WikiPage::doUpdateRestrictions
1980 public function testDoUpdateRestrictions_failsOnReadOnly() {
1981 $page = $this->createPage( __METHOD__
, 'ABC' );
1982 $user = $this->getTestSysop()->getUser();
1986 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
1987 ->disableOriginalConstructor()
1988 ->setMethods( [ 'isReadOnly', 'getReason' ] )
1990 $readOnly->expects( $this->once() )
1991 ->method( 'isReadOnly' )
1992 ->will( $this->returnValue( true ) );
1993 $readOnly->expects( $this->once() )
1994 ->method( 'getReason' )
1995 ->will( $this->returnValue( 'Some Read Only Reason' ) );
1996 $this->setService( 'ReadOnlyMode', $readOnly );
1998 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
1999 $this->assertFalse( $status->isOK() );
2000 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
2004 * @covers WikiPage::doUpdateRestrictions
2006 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
2007 $page = $this->createPage( __METHOD__
, 'ABC' );
2008 $user = $this->getTestSysop()->getUser();
2010 $limit = [ 'edit' => 'sysop' ];
2012 $status = $page->doUpdateRestrictions(
2021 // The first entry should have a logId as it did something
2022 $this->assertTrue( $status->isGood() );
2023 $this->assertInternalType( 'int', $status->getValue() );
2025 $status = $page->doUpdateRestrictions(
2034 // The second entry should not have a logId as nothing changed
2035 $this->assertTrue( $status->isGood() );
2036 $this->assertNull( $status->getValue() );
2040 * @covers WikiPage::doUpdateRestrictions
2042 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
2043 $page = $this->createPage( __METHOD__
, 'ABC' );
2044 $user = $this->getTestSysop()->getUser();
2048 $status = $page->doUpdateRestrictions(
2049 [ 'edit' => 'sysop' ],
2056 $this->assertTrue( $status->isGood() );
2057 $this->assertInternalType( 'int', $status->getValue() );
2058 $this->assertSelect(
2060 [ 'log_type', 'log_action' ],
2061 [ 'log_id' => $status->getValue() ],
2062 [ [ 'protect', 'protect' ] ]
2065 // Modify the protection
2066 $status = $page->doUpdateRestrictions(
2067 [ 'edit' => 'somethingElse' ],
2074 $this->assertTrue( $status->isGood() );
2075 $this->assertInternalType( 'int', $status->getValue() );
2076 $this->assertSelect(
2078 [ 'log_type', 'log_action' ],
2079 [ 'log_id' => $status->getValue() ],
2080 [ [ 'protect', 'modify' ] ]
2083 // Remove the protection
2084 $status = $page->doUpdateRestrictions(
2092 $this->assertTrue( $status->isGood() );
2093 $this->assertInternalType( 'int', $status->getValue() );
2094 $this->assertSelect(
2096 [ 'log_type', 'log_action' ],
2097 [ 'log_id' => $status->getValue() ],
2098 [ [ 'protect', 'unprotect' ] ]