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 setUp() {
35 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
36 $this->pagesToDelete
= [];
39 protected function tearDown() {
40 foreach ( $this->pagesToDelete
as $p ) {
41 /* @var $p WikiPage */
45 $p->doDeleteArticle( "testing done." );
47 } catch ( MWException
$ex ) {
54 abstract protected function getContentHandlerUseDB();
57 * @param Title|string $title
58 * @param string|null $model
61 private function newPage( $title, $model = null ) {
62 if ( is_string( $title ) ) {
63 $ns = $this->getDefaultWikitextNS();
64 $title = Title
::newFromText( $title, $ns );
67 $p = new WikiPage( $title );
69 $this->pagesToDelete
[] = $p;
75 * @param string|Title|WikiPage $page
81 protected function createPage( $page, $text, $model = null ) {
82 if ( is_string( $page ) ||
$page instanceof Title
) {
83 $page = $this->newPage( $page, $model );
86 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
87 $page->doEditContent( $content, "testing", EDIT_NEW
);
93 * @covers WikiPage::doEditContent
94 * @covers WikiPage::doModify
95 * @covers WikiPage::doCreate
96 * @covers WikiPage::doEditUpdates
98 public function testDoEditContent() {
99 $page = $this->newPage( __METHOD__
);
100 $title = $page->getTitle();
102 $content = ContentHandler
::makeContent(
103 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
104 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
106 CONTENT_MODEL_WIKITEXT
109 $page->doEditContent( $content, "[[testing]] 1" );
111 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
112 $this->assertTrue( $page->getId() > 0, "WikiPage should have new page id" );
113 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
114 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
116 $id = $page->getId();
118 # ------------------------
119 $dbr = wfGetDB( DB_REPLICA
);
120 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
121 $n = $res->numRows();
124 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
126 # ------------------------
127 $page = new WikiPage( $title );
129 $retrieved = $page->getContent();
130 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
132 # ------------------------
133 $content = ContentHandler
::makeContent(
134 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
135 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est.",
137 CONTENT_MODEL_WIKITEXT
140 $page->doEditContent( $content, "testing 2" );
142 # ------------------------
143 $page = new WikiPage( $title );
145 $retrieved = $page->getContent();
146 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
148 # ------------------------
149 $dbr = wfGetDB( DB_REPLICA
);
150 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
151 $n = $res->numRows();
154 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
158 * @covers WikiPage::doDeleteArticle
159 * @covers WikiPage::doDeleteArticleReal
161 public function testDoDeleteArticle() {
162 $page = $this->createPage(
164 "[[original text]] foo",
165 CONTENT_MODEL_WIKITEXT
167 $id = $page->getId();
169 $page->doDeleteArticle( "testing deletion" );
172 $page->getTitle()->getArticleID() > 0,
173 "Title object should now have page id 0"
175 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
178 "WikiPage::exists should return false after page was deleted"
182 "WikiPage::getContent should return null after page was deleted"
185 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
188 "Title::exists should return false after page was deleted"
192 JobQueueGroup
::destroySingletons();
194 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
197 # ------------------------
198 $dbr = wfGetDB( DB_REPLICA
);
199 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
200 $n = $res->numRows();
203 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
207 * @covers WikiPage::doDeleteUpdates
209 public function testDoDeleteUpdates() {
210 $page = $this->createPage(
212 "[[original text]] foo",
213 CONTENT_MODEL_WIKITEXT
215 $id = $page->getId();
217 // Similar to MovePage logic
218 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
219 $page->doDeleteUpdates( $id );
222 JobQueueGroup
::destroySingletons();
224 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
227 # ------------------------
228 $dbr = wfGetDB( DB_REPLICA
);
229 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
230 $n = $res->numRows();
233 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
237 * @covers WikiPage::getRevision
239 public function testGetRevision() {
240 $page = $this->newPage( __METHOD__
);
242 $rev = $page->getRevision();
243 $this->assertNull( $rev );
246 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
248 $rev = $page->getRevision();
250 $this->assertEquals( $page->getLatest(), $rev->getId() );
251 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
255 * @covers WikiPage::getContent
257 public function testGetContent() {
258 $page = $this->newPage( __METHOD__
);
260 $content = $page->getContent();
261 $this->assertNull( $content );
264 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
266 $content = $page->getContent();
267 $this->assertEquals( "some text", $content->getNativeData() );
271 * @covers WikiPage::exists
273 public function testExists() {
274 $page = $this->newPage( __METHOD__
);
275 $this->assertFalse( $page->exists() );
278 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
279 $this->assertTrue( $page->exists() );
281 $page = new WikiPage( $page->getTitle() );
282 $this->assertTrue( $page->exists() );
285 $page->doDeleteArticle( "done testing" );
286 $this->assertFalse( $page->exists() );
288 $page = new WikiPage( $page->getTitle() );
289 $this->assertFalse( $page->exists() );
292 public function provideHasViewableContent() {
294 [ 'WikiPageTest_testHasViewableContent', false, true ],
295 [ 'Special:WikiPageTest_testHasViewableContent', false ],
296 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
297 [ 'Special:Userlogin', true ],
298 [ 'MediaWiki:help', true ],
303 * @dataProvider provideHasViewableContent
304 * @covers WikiPage::hasViewableContent
306 public function testHasViewableContent( $title, $viewable, $create = false ) {
307 $page = $this->newPage( $title );
308 $this->assertEquals( $viewable, $page->hasViewableContent() );
311 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
312 $this->assertTrue( $page->hasViewableContent() );
314 $page = new WikiPage( $page->getTitle() );
315 $this->assertTrue( $page->hasViewableContent() );
319 public function provideGetRedirectTarget() {
321 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
323 'WikiPageTest_testGetRedirectTarget_2',
324 CONTENT_MODEL_WIKITEXT
,
325 "#REDIRECT [[hello world]]",
332 * @dataProvider provideGetRedirectTarget
333 * @covers WikiPage::getRedirectTarget
335 public function testGetRedirectTarget( $title, $model, $text, $target ) {
336 $this->setMwGlobals( [
337 'wgCapitalLinks' => true,
340 $page = $this->createPage( $title, $text, $model );
342 # sanity check, because this test seems to fail for no reason for some people.
343 $c = $page->getContent();
344 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
346 # now, test the actual redirect
347 $t = $page->getRedirectTarget();
348 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
352 * @dataProvider provideGetRedirectTarget
353 * @covers WikiPage::isRedirect
355 public function testIsRedirect( $title, $model, $text, $target ) {
356 $page = $this->createPage( $title, $text, $model );
357 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
360 public function provideIsCountable() {
364 [ 'WikiPageTest_testIsCountable',
365 CONTENT_MODEL_WIKITEXT
,
370 [ 'WikiPageTest_testIsCountable',
371 CONTENT_MODEL_WIKITEXT
,
378 [ 'WikiPageTest_testIsCountable',
379 CONTENT_MODEL_WIKITEXT
,
384 [ 'WikiPageTest_testIsCountable',
385 CONTENT_MODEL_WIKITEXT
,
392 [ 'WikiPageTest_testIsCountable',
393 CONTENT_MODEL_WIKITEXT
,
398 [ 'WikiPageTest_testIsCountable',
399 CONTENT_MODEL_WIKITEXT
,
406 [ 'WikiPageTest_testIsCountable',
407 CONTENT_MODEL_WIKITEXT
,
412 [ 'WikiPageTest_testIsCountable',
413 CONTENT_MODEL_WIKITEXT
,
418 [ 'WikiPageTest_testIsCountable',
419 CONTENT_MODEL_WIKITEXT
,
425 // not a content namespace
426 [ 'Talk:WikiPageTest_testIsCountable',
427 CONTENT_MODEL_WIKITEXT
,
432 [ 'Talk:WikiPageTest_testIsCountable',
433 CONTENT_MODEL_WIKITEXT
,
438 [ 'Talk:WikiPageTest_testIsCountable',
439 CONTENT_MODEL_WIKITEXT
,
445 // not a content namespace, different model
446 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
452 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
458 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
468 * @dataProvider provideIsCountable
469 * @covers WikiPage::isCountable
471 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
472 global $wgContentHandlerUseDB;
474 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
476 $title = Title
::newFromText( $title );
478 if ( !$wgContentHandlerUseDB
480 && ContentHandler
::getDefaultModelFor( $title ) != $model
482 $this->markTestSkipped( "Can not use non-default content model $model for "
483 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
486 $page = $this->createPage( $title, $text, $model );
488 $editInfo = $page->prepareContentForEdit( $page->getContent() );
490 $v = $page->isCountable();
491 $w = $page->isCountable( $editInfo );
496 "isCountable( null ) returned unexpected value " . var_export( $v, true )
497 . " instead of " . var_export( $expected, true )
498 . " in mode `$mode` for text \"$text\""
504 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
505 . " instead of " . var_export( $expected, true )
506 . " in mode `$mode` for text \"$text\""
510 public function provideGetParserOutput() {
513 CONTENT_MODEL_WIKITEXT
,
515 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
522 * @dataProvider provideGetParserOutput
523 * @covers WikiPage::getParserOutput
525 public function testGetParserOutput( $model, $text, $expectedHtml ) {
526 $page = $this->createPage( __METHOD__
, $text, $model );
528 $opt = $page->makeParserOptions( 'canonical' );
529 $po = $page->getParserOutput( $opt );
530 $text = $po->getText();
532 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
533 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
535 $this->assertEquals( $expectedHtml, $text );
541 * @covers WikiPage::getParserOutput
543 public function testGetParserOutput_nonexisting() {
544 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
546 $opt = new ParserOptions();
547 $po = $page->getParserOutput( $opt );
549 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
553 * @covers WikiPage::getParserOutput
555 public function testGetParserOutput_badrev() {
556 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
558 $opt = new ParserOptions();
559 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
561 // @todo would be neat to also test deleted revision
563 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
566 public static $sections =
580 public function dataReplaceSection() {
581 // NOTE: assume the Help namespace to contain wikitext
583 [ 'Help:WikiPageTest_testReplaceSection',
584 CONTENT_MODEL_WIKITEXT
,
589 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
591 [ 'Help:WikiPageTest_testReplaceSection',
592 CONTENT_MODEL_WIKITEXT
,
599 [ 'Help:WikiPageTest_testReplaceSection',
600 CONTENT_MODEL_WIKITEXT
,
603 "== TEST ==\nmore fun",
605 trim( preg_replace( '/^== test ==.*== foo ==/sm',
606 "== TEST ==\nmore fun\n\n== foo ==",
609 [ 'Help:WikiPageTest_testReplaceSection',
610 CONTENT_MODEL_WIKITEXT
,
615 trim( self
::$sections )
617 [ 'Help:WikiPageTest_testReplaceSection',
618 CONTENT_MODEL_WIKITEXT
,
623 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
629 * @dataProvider dataReplaceSection
630 * @covers WikiPage::replaceSectionContent
632 public function testReplaceSectionContent( $title, $model, $text, $section,
633 $with, $sectionTitle, $expected
635 $page = $this->createPage( $title, $text, $model );
637 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
638 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
640 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
644 * @dataProvider dataReplaceSection
645 * @covers WikiPage::replaceSectionAtRev
647 public function testReplaceSectionAtRev( $title, $model, $text, $section,
648 $with, $sectionTitle, $expected
650 $page = $this->createPage( $title, $text, $model );
651 $baseRevId = $page->getLatest();
653 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
654 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
656 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
660 * @covers WikiPage::getOldestRevision
662 public function testGetOldestRevision() {
663 $page = $this->newPage( __METHOD__
);
664 $page->doEditContent(
665 new WikitextContent( 'one' ),
669 $rev1 = $page->getRevision();
671 $page = new WikiPage( $page->getTitle() );
672 $page->doEditContent(
673 new WikitextContent( 'two' ),
678 $page = new WikiPage( $page->getTitle() );
679 $page->doEditContent(
680 new WikitextContent( 'three' ),
686 $this->assertNotEquals(
688 $page->getRevision()->getId(),
689 '$page->getRevision()->getId()'
695 $page->getOldestRevision()->getId(),
696 '$page->getOldestRevision()->getId()'
701 * @covers WikiPage::doRollback
702 * @covers WikiPage::commitRollback
704 public function testDoRollback() {
705 $admin = $this->getTestSysop()->getUser();
706 $user1 = $this->getTestUser()->getUser();
707 // Use the confirmed group for user2 to make sure the user is different
708 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
710 $page = $this->newPage( __METHOD__
);
714 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
715 "section one", EDIT_NEW
, false, $admin );
718 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
719 "adding section two", 0, false, $user1 );
721 $text .= "\n\nthree";
722 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
723 "adding section three", 0, false, $user2 );
725 /** @var Revision $rev1 */
726 /** @var Revision $rev2 */
727 /** @var Revision $rev3 */
728 $rev1 = $status1->getValue()['revision'];
729 $rev2 = $status2->getValue()['revision'];
730 $rev3 = $status3->getValue()['revision'];
733 * We are having issues with doRollback spuriously failing. Apparently
734 * the last revision somehow goes missing or not committed under some
735 * circumstances. So, make sure the revisions have the correct usernames.
737 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
738 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
739 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
740 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
742 // Now, try the actual rollback
743 $token = $admin->getEditToken( 'rollback' );
744 $rollbackErrors = $page->doRollback(
753 if ( $rollbackErrors ) {
755 "Rollback failed:\n" .
756 print_r( $rollbackErrors, true ) . ";\n" .
757 print_r( $resultDetails, true )
761 $page = new WikiPage( $page->getTitle() );
762 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
763 "rollback did not revert to the correct revision" );
764 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
768 * @covers WikiPage::doRollback
769 * @covers WikiPage::commitRollback
771 public function testDoRollback_simple() {
772 $admin = $this->getTestSysop()->getUser();
775 $page = $this->newPage( __METHOD__
);
776 $page->doEditContent(
777 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
783 $rev1 = $page->getRevision();
785 $user1 = $this->getTestUser()->getUser();
787 $page = new WikiPage( $page->getTitle() );
788 $page->doEditContent(
789 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
790 "adding section two",
796 # now, try the rollback
797 $token = $admin->getEditToken( 'rollback' );
798 $errors = $page->doRollback(
808 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
809 . ";\n" . print_r( $details, true ) );
812 $page = new WikiPage( $page->getTitle() );
813 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
814 "rollback did not revert to the correct revision" );
815 $this->assertEquals( "one", $page->getContent()->getNativeData() );
819 * @covers WikiPage::doRollback
820 * @covers WikiPage::commitRollback
822 public function testDoRollbackFailureSameContent() {
823 $admin = $this->getTestSysop()->getUser();
826 $page = $this->newPage( __METHOD__
);
827 $page->doEditContent(
828 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
834 $rev1 = $page->getRevision();
836 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
838 $page = new WikiPage( $page->getTitle() );
839 $page->doEditContent(
840 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
841 "adding section two",
847 # now, do a the rollback from the same user was doing the edit before
849 $token = $user1->getEditToken( 'rollback' );
850 $errors = $page->doRollback(
852 "testing revert same user",
859 $this->assertEquals( [], $errors, "Rollback failed same user" );
861 # now, try the rollback
863 $token = $admin->getEditToken( 'rollback' );
864 $errors = $page->doRollback(
883 "Rollback not failed"
886 $page = new WikiPage( $page->getTitle() );
887 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
888 "rollback did not revert to the correct revision" );
889 $this->assertEquals( "one", $page->getContent()->getNativeData() );
893 * Tests tagging for edits that do rollback action
894 * @covers WikiPage::doRollback
896 public function testDoRollbackTagging() {
897 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
898 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
902 $admin->setName( 'Administrator' );
903 $admin->addToDatabase();
905 $text = 'First line';
906 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
907 $page->doEditContent(
908 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
915 $secondUser = new User();
916 $secondUser->setName( '92.65.217.32' );
917 $text .= '\n\nSecond line';
918 $page = new WikiPage( $page->getTitle() );
919 $page->doEditContent(
920 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
921 'Adding second line',
927 // Now, try the rollback
928 $admin->addGroup( 'sysop' ); // Make the test user a sysop
929 $token = $admin->getEditToken( 'rollback' );
930 $errors = $page->doRollback(
931 $secondUser->getName(),
939 // If doRollback completed without errors
940 if ( $errors === [] ) {
941 $tags = $resultDetails[ 'tags' ];
942 $this->assertContains( 'mw-rollback', $tags );
946 public function provideGetAutoDeleteReason() {
956 [ "first edit", null ],
958 "/first edit.*only contributor/",
964 [ "first edit", null ],
965 [ "second edit", null ],
967 "/second edit.*only contributor/",
973 [ "first edit", "127.0.2.22" ],
974 [ "second edit", "127.0.3.33" ],
984 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
985 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
986 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
987 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
988 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
992 '/first edit:.*\.\.\."/',
998 [ "first edit", "127.0.2.22" ],
999 [ "", "127.0.3.33" ],
1001 "/before blanking.*first edit/",
1009 * @dataProvider provideGetAutoDeleteReason
1010 * @covers WikiPage::getAutoDeleteReason
1012 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1015 // NOTE: assume Help namespace to contain wikitext
1016 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1020 foreach ( $edits as $edit ) {
1023 if ( !empty( $edit[1] ) ) {
1024 $user->setName( $edit[1] );
1029 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1031 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1036 $reason = $page->getAutoDeleteReason( $hasHistory );
1038 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1039 $this->assertEquals( $expectedResult, $reason );
1041 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1042 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1045 $this->assertEquals( $expectedHistory, $hasHistory,
1046 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1048 $page->doDeleteArticle( "done" );
1051 public function providePreSaveTransform() {
1053 [ 'hello this is ~~~',
1054 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1056 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1057 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1063 * @covers WikiPage::factory
1065 public function testWikiPageFactory() {
1066 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1067 $page = WikiPage
::factory( $title );
1068 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1070 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1071 $page = WikiPage
::factory( $title );
1072 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1074 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1075 $page = WikiPage
::factory( $title );
1076 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1080 * @dataProvider provideCommentMigrationOnDeletion
1082 * @param int $writeStage
1083 * @param int $readStage
1085 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1086 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1087 $dbr = wfGetDB( DB_REPLICA
);
1089 $page = $this->createPage(
1092 CONTENT_MODEL_WIKITEXT
1094 $revid = $page->getLatest();
1095 if ( $writeStage > MIGRATION_OLD
) {
1096 $comment_id = $dbr->selectField(
1097 'revision_comment_temp',
1098 'revcomment_comment_id',
1099 [ 'revcomment_rev' => $revid ],
1104 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1106 $page->doDeleteArticle( "testing deletion" );
1108 if ( $readStage > MIGRATION_OLD
) {
1109 // Didn't leave behind any 'revision_comment_temp' rows
1110 $n = $dbr->selectField(
1111 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1113 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1115 // Copied or upgraded the comment_id, as applicable
1116 $ar_comment_id = $dbr->selectField(
1119 [ 'ar_rev_id' => $revid ],
1122 if ( $writeStage > MIGRATION_OLD
) {
1123 $this->assertSame( $comment_id, $ar_comment_id );
1125 $this->assertNotEquals( 0, $ar_comment_id );
1129 // Copied rev_comment, if applicable
1130 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1131 $ar_comment = $dbr->selectField(
1134 [ 'ar_rev_id' => $revid ],
1137 $this->assertSame( 'testing', $ar_comment );
1141 public function provideCommentMigrationOnDeletion() {
1143 [ MIGRATION_OLD
, MIGRATION_OLD
],
1144 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1145 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1146 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1147 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1148 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1149 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1150 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1151 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1152 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1153 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1154 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1155 [ MIGRATION_NEW
, MIGRATION_NEW
],
1160 * @covers WikiPage::updateCategoryCounts
1162 public function testUpdateCategoryCounts() {
1163 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1165 // Add an initial category
1166 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1168 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1169 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1170 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1172 // Add a new category
1173 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1175 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1176 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1177 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1179 // Add and remove a category
1180 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1182 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1183 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1184 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1187 public function provideUpdateRedirectOn() {
1188 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1189 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1190 yield
[ 'SomeText', false, null, false, true, 0 ];
1191 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1195 * @dataProvider provideUpdateRedirectOn
1196 * @covers WikiPage::updateRedirectOn
1198 * @param string $initialText
1199 * @param bool $initialRedirectState
1200 * @param string|null $redirectTitle
1201 * @param bool|null $lastRevIsRedirect
1202 * @param bool $expectedSuccess
1203 * @param int $expectedRowCount
1205 public function testUpdateRedirectOn(
1207 $initialRedirectState,
1213 static $pageCounter = 0;
1216 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1217 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1219 $redirectTitle = is_string( $redirectTitle )
1220 ? Title
::newFromText( $redirectTitle )
1223 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1224 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1226 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1227 * Most of core checks the page table for redirect status, so we have to be ugly and
1228 * assert a select from the table here.
1230 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1233 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1234 $this->assertSelect(
1237 [ 'rd_from' => $pageId ],
1238 [ [ strval( $expected ) ] ]
1243 * @covers WikiPage::insertRedirectEntry
1245 public function testInsertRedirectEntry_insertsRedirectEntry() {
1246 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1247 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1249 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1250 $targetTitle->mInterwiki
= 'eninter';
1251 $page->insertRedirectEntry( $targetTitle, null );
1253 $this->assertSelect(
1255 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1256 [ 'rd_from' => $page->getId() ],
1258 strval( $page->getId() ),
1259 strval( $targetTitle->getNamespace() ),
1260 strval( $targetTitle->getDBkey() ),
1261 strval( $targetTitle->getFragment() ),
1262 strval( $targetTitle->getInterwiki() ),
1268 * @covers WikiPage::insertRedirectEntry
1270 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1271 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1272 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1274 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1275 $targetTitle->mInterwiki
= 'eninter';
1276 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1278 $this->assertSelect(
1280 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1281 [ 'rd_from' => $page->getId() ],
1283 strval( $page->getId() ),
1284 strval( $targetTitle->getNamespace() ),
1285 strval( $targetTitle->getDBkey() ),
1286 strval( $targetTitle->getFragment() ),
1287 strval( $targetTitle->getInterwiki() ),
1293 * @covers WikiPage::insertRedirectEntry
1295 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1296 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1297 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1299 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1300 $targetTitle->mInterwiki
= 'eninter';
1301 $page->insertRedirectEntry( $targetTitle, 215251 );
1303 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1306 private function getRow( array $overrides = [] ) {
1310 'page_is_redirect' => '1',
1311 'page_latest' => '99',
1312 'page_namespace' => '3',
1313 'page_title' => 'JaJaTitle',
1314 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1315 'page_touched' => '20120101020202',
1316 'page_links_updated' => '20140101020202',
1318 foreach ( $overrides as $key => $value ) {
1319 $row[$key] = $value;
1321 return (object)$row;
1324 public function provideNewFromRowSuccess() {
1325 yield
'basic row' => [
1327 function ( WikiPage
$wikiPage, self
$test ) {
1328 $test->assertSame( 44, $wikiPage->getId() );
1329 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1330 $test->assertTrue( $wikiPage->isRedirect() );
1331 $test->assertSame( 99, $wikiPage->getLatest() );
1332 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1333 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1336 'edit' => [ 'autoconfirmed', 'sysop' ],
1337 'move' => [ 'sysop' ],
1339 $wikiPage->getTitle()->getAllRestrictions()
1341 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1342 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1345 yield
'different timestamp formats' => [
1347 'page_touched' => '2012-01-01 02:02:02',
1348 'page_links_updated' => '2014-01-01 02:02:02',
1350 function ( WikiPage
$wikiPage, self
$test ) {
1351 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1352 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1355 yield
'no restrictions' => [
1357 'page_restrictions' => '',
1359 function ( WikiPage
$wikiPage, self
$test ) {
1365 $wikiPage->getTitle()->getAllRestrictions()
1369 yield
'not redirect' => [
1371 'page_is_redirect' => '0',
1373 function ( WikiPage
$wikiPage, self
$test ) {
1374 $test->assertFalse( $wikiPage->isRedirect() );
1380 * @covers WikiPage::newFromRow
1381 * @covers WikiPage::loadFromRow
1382 * @dataProvider provideNewFromRowSuccess
1384 * @param object $row
1385 * @param callable $assertions
1387 public function testNewFromRow( $row, $assertions ) {
1388 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1389 $assertions( $page, $this );
1392 public function provideTestNewFromId_returnsNullOnBadPageId() {
1398 * @covers WikiPage::newFromID
1399 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1401 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1402 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1406 * @covers WikiPage::newFromID
1408 public function testNewFromId_appearsToFetchCorrectRow() {
1409 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1410 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1411 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1412 $this->assertEquals(
1413 $createdPage->getContent()->getNativeData(),
1414 $fetchedPage->getContent()->getNativeData()
1419 * @covers WikiPage::newFromID
1421 public function testNewFromId_returnsNullOnNonExistingId() {
1422 $this->assertNull( WikiPage
::newFromID( 73574757437437743743 ) );
1425 public function provideTestInsertProtectNullRevision() {
1426 // phpcs:disable Generic.Files.LineLength
1429 [ 'edit' => 'sysop' ],
1430 [ 'edit' => '20200101040404' ],
1434 '(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)))'
1438 [ 'edit' => 'sysop', 'move' => 'something' ],
1439 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1443 '(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)))'
1449 * @dataProvider provideTestInsertProtectNullRevision
1450 * @covers WikiPage::insertProtectNullRevision
1451 * @covers WikiPage::protectDescription
1453 * @param string $revCommentMsg
1454 * @param array $limit
1455 * @param array $expiry
1456 * @param bool $cascade
1457 * @param string $reason
1458 * @param bool|null $user true if the test sysop should be used, or null
1459 * @param string $expectedComment
1461 public function testInsertProtectNullRevision(
1470 $this->setContentLang( 'qqx' );
1472 $page = $this->createPage( __METHOD__
, 'Goat' );
1474 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1476 $result = $page->insertProtectNullRevision(
1485 $this->assertTrue( $result instanceof Revision
);
1486 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1490 * @covers WikiPage::updateRevisionOn
1492 public function testUpdateRevisionOn_existingPage() {
1493 $user = $this->getTestSysop()->getUser();
1494 $page = $this->createPage( __METHOD__
, 'StartText' );
1496 $revision = new Revision(
1499 'page' => $page->getId(),
1500 'title' => $page->getTitle(),
1501 'comment' => __METHOD__
,
1502 'minor_edit' => true,
1503 'text' => __METHOD__
. '-text',
1504 'len' => strlen( __METHOD__
. '-text' ),
1505 'user' => $user->getId(),
1506 'user_text' => $user->getName(),
1507 'timestamp' => '20170707040404',
1508 'content_model' => CONTENT_MODEL_WIKITEXT
,
1509 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1513 $result = $page->updateRevisionOn( $this->db
, $revision );
1514 $this->assertTrue( $result );
1515 $this->assertSame( 9989, $page->getLatest() );
1516 $this->assertEquals( $revision, $page->getRevision() );
1520 * @covers WikiPage::updateRevisionOn
1522 public function testUpdateRevisionOn_NonExistingPage() {
1523 $user = $this->getTestSysop()->getUser();
1524 $page = $this->createPage( __METHOD__
, 'StartText' );
1525 $page->doDeleteArticle( 'reason' );
1527 $revision = new Revision(
1530 'page' => $page->getId(),
1531 'title' => $page->getTitle(),
1532 'comment' => __METHOD__
,
1533 'minor_edit' => true,
1534 'text' => __METHOD__
. '-text',
1535 'len' => strlen( __METHOD__
. '-text' ),
1536 'user' => $user->getId(),
1537 'user_text' => $user->getName(),
1538 'timestamp' => '20170707040404',
1539 'content_model' => CONTENT_MODEL_WIKITEXT
,
1540 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1544 $result = $page->updateRevisionOn( $this->db
, $revision );
1545 $this->assertFalse( $result );
1549 * @covers WikiPage::updateIfNewerOn
1551 public function testUpdateIfNewerOn_olderRevision() {
1552 $user = $this->getTestSysop()->getUser();
1553 $page = $this->createPage( __METHOD__
, 'StartText' );
1554 $initialRevision = $page->getRevision();
1556 $olderTimeStamp = wfTimestamp(
1558 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
1561 $olderRevison = new Revision(
1564 'page' => $page->getId(),
1565 'title' => $page->getTitle(),
1566 'comment' => __METHOD__
,
1567 'minor_edit' => true,
1568 'text' => __METHOD__
. '-text',
1569 'len' => strlen( __METHOD__
. '-text' ),
1570 'user' => $user->getId(),
1571 'user_text' => $user->getName(),
1572 'timestamp' => $olderTimeStamp,
1573 'content_model' => CONTENT_MODEL_WIKITEXT
,
1574 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1578 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
1579 $this->assertFalse( $result );
1583 * @covers WikiPage::updateIfNewerOn
1585 public function testUpdateIfNewerOn_newerRevision() {
1586 $user = $this->getTestSysop()->getUser();
1587 $page = $this->createPage( __METHOD__
, 'StartText' );
1588 $initialRevision = $page->getRevision();
1590 $newerTimeStamp = wfTimestamp(
1592 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
1595 $newerRevision = new Revision(
1598 'page' => $page->getId(),
1599 'title' => $page->getTitle(),
1600 'comment' => __METHOD__
,
1601 'minor_edit' => true,
1602 'text' => __METHOD__
. '-text',
1603 'len' => strlen( __METHOD__
. '-text' ),
1604 'user' => $user->getId(),
1605 'user_text' => $user->getName(),
1606 'timestamp' => $newerTimeStamp,
1607 'content_model' => CONTENT_MODEL_WIKITEXT
,
1608 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1611 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
1612 $this->assertTrue( $result );
1616 * @covers WikiPage::insertOn
1618 public function testInsertOn() {
1619 $title = Title
::newFromText( __METHOD__
);
1620 $page = new WikiPage( $title );
1622 $startTimeStamp = wfTimestampNow();
1623 $result = $page->insertOn( $this->db
);
1624 $endTimeStamp = wfTimestampNow();
1626 $this->assertInternalType( 'int', $result );
1627 $this->assertTrue( $result > 0 );
1629 $condition = [ 'page_id' => $result ];
1631 // Check the default fields have been filled
1632 $this->assertSelect(
1637 'page_restrictions',
1655 // Check the page_random field has been filled
1656 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
1657 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
1659 // Assert the touched timestamp in the DB is roughly when we inserted the page
1660 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
1662 wfTimestamp( TS_UNIX
, $startTimeStamp )
1663 <= wfTimestamp( TS_UNIX
, $pageTouched )
1666 wfTimestamp( TS_UNIX
, $endTimeStamp )
1667 >= wfTimestamp( TS_UNIX
, $pageTouched )
1670 // Try inserting the same page again and checking the result is false (no change)
1671 $result = $page->insertOn( $this->db
);
1672 $this->assertFalse( $result );
1676 * @covers WikiPage::insertOn
1678 public function testInsertOn_idSpecified() {
1679 $title = Title
::newFromText( __METHOD__
);
1680 $page = new WikiPage( $title );
1683 $result = $page->insertOn( $this->db
, $id );
1685 $this->assertSame( $id, $result );
1687 $condition = [ 'page_id' => $result ];
1689 // Check there is actually a row in the db
1690 $this->assertSelect(
1698 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
1699 // Note: Once the current dates passes the date in these tests they will fail.
1700 yield
'move something' => [
1702 [ 'move' => 'something' ],
1704 [ 'edit' => [], 'move' => [ 'something' ] ],
1707 yield
'move something, edit blank' => [
1709 [ 'move' => 'something', 'edit' => '' ],
1711 [ 'edit' => [], 'move' => [ 'something' ] ],
1714 yield
'edit sysop, with expiry' => [
1716 [ 'edit' => 'sysop' ],
1717 [ 'edit' => '21330101020202' ],
1718 [ 'edit' => [ 'sysop' ], 'move' => [] ],
1719 [ 'edit' => '21330101020202' ],
1721 yield
'move and edit, move with expiry' => [
1723 [ 'move' => 'something', 'edit' => 'another' ],
1724 [ 'move' => '22220202010101' ],
1725 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1726 [ 'move' => '22220202010101' ],
1728 yield
'move and edit, edit with infinity expiry' => [
1730 [ 'move' => 'something', 'edit' => 'another' ],
1731 [ 'edit' => 'infinity' ],
1732 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
1733 [ 'edit' => 'infinity' ],
1735 yield
'non existing, create something' => [
1737 [ 'create' => 'something' ],
1739 [ 'create' => [ 'something' ] ],
1742 yield
'non existing, create something with expiry' => [
1744 [ 'create' => 'something' ],
1745 [ 'create' => '23451212112233' ],
1746 [ 'create' => [ 'something' ] ],
1747 [ 'create' => '23451212112233' ],
1752 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
1753 * @covers WikiPage::doUpdateRestrictions
1755 public function testDoUpdateRestrictions_setBasicRestrictions(
1759 array $expectedRestrictions,
1760 array $expectedRestrictionExpiries
1762 if ( $pageExists ) {
1763 $page = $this->createPage( __METHOD__
, 'ABC' );
1765 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
1767 $user = $this->getTestSysop()->getUser();
1770 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
1772 $logId = $status->getValue();
1773 $allRestrictions = $page->getTitle()->getAllRestrictions();
1775 $this->assertTrue( $status->isGood() );
1776 $this->assertInternalType( 'int', $logId );
1777 $this->assertSame( $expectedRestrictions, $allRestrictions );
1778 foreach ( $expectedRestrictionExpiries as $key => $value ) {
1779 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
1782 // Make sure the log entry looks good
1783 // log_params is not checked here
1784 $this->assertSelect(
1793 [ 'log_id' => $logId ],
1796 (string)$user->getId(),
1798 (string)$page->getTitle()->getNamespace(),
1799 $page->getTitle()->getDBkey(),
1805 * @covers WikiPage::doUpdateRestrictions
1807 public function testDoUpdateRestrictions_failsOnReadOnly() {
1808 $page = $this->createPage( __METHOD__
, 'ABC' );
1809 $user = $this->getTestSysop()->getUser();
1813 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
1814 ->disableOriginalConstructor()
1815 ->setMethods( [ 'isReadOnly', 'getReason' ] )
1817 $readOnly->expects( $this->once() )
1818 ->method( 'isReadOnly' )
1819 ->will( $this->returnValue( true ) );
1820 $readOnly->expects( $this->once() )
1821 ->method( 'getReason' )
1822 ->will( $this->returnValue( 'Some Read Only Reason' ) );
1823 $this->setService( 'ReadOnlyMode', $readOnly );
1825 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
1826 $this->assertFalse( $status->isOK() );
1827 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
1831 * @covers WikiPage::doUpdateRestrictions
1833 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
1834 $page = $this->createPage( __METHOD__
, 'ABC' );
1835 $user = $this->getTestSysop()->getUser();
1837 $limit = [ 'edit' => 'sysop' ];
1839 $status = $page->doUpdateRestrictions(
1848 // The first entry should have a logId as it did something
1849 $this->assertTrue( $status->isGood() );
1850 $this->assertInternalType( 'int', $status->getValue() );
1852 $status = $page->doUpdateRestrictions(
1861 // The second entry should not have a logId as nothing changed
1862 $this->assertTrue( $status->isGood() );
1863 $this->assertNull( $status->getValue() );
1867 * @covers WikiPage::doUpdateRestrictions
1869 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
1870 $page = $this->createPage( __METHOD__
, 'ABC' );
1871 $user = $this->getTestSysop()->getUser();
1875 $status = $page->doUpdateRestrictions(
1876 [ 'edit' => 'sysop' ],
1883 $this->assertTrue( $status->isGood() );
1884 $this->assertInternalType( 'int', $status->getValue() );
1885 $this->assertSelect(
1887 [ 'log_type', 'log_action' ],
1888 [ 'log_id' => $status->getValue() ],
1889 [ [ 'protect', 'protect' ] ]
1892 // Modify the protection
1893 $status = $page->doUpdateRestrictions(
1894 [ 'edit' => 'somethingElse' ],
1901 $this->assertTrue( $status->isGood() );
1902 $this->assertInternalType( 'int', $status->getValue() );
1903 $this->assertSelect(
1905 [ 'log_type', 'log_action' ],
1906 [ 'log_id' => $status->getValue() ],
1907 [ [ 'protect', 'modify' ] ]
1910 // Remove the protection
1911 $status = $page->doUpdateRestrictions(
1919 $this->assertTrue( $status->isGood() );
1920 $this->assertInternalType( 'int', $status->getValue() );
1921 $this->assertSelect(
1923 [ 'log_type', 'log_action' ],
1924 [ 'log_id' => $status->getValue() ],
1925 [ [ 'protect', 'unprotect' ] ]