3 use MediaWiki\Storage\RevisionSlotsUpdate
;
4 use Wikimedia\TestingAccessWrapper
;
9 abstract class WikiPageDbTestBase
extends MediaWikiLangTestCase
{
11 private $pagesToDelete;
13 public function __construct( $name = null, array $data = [], $dataName = '' ) {
14 parent
::__construct( $name, $data, $dataName );
16 $this->tablesUsed
= array_merge(
39 protected function addCoreDBData() {
40 // Blank out. This would fail with a modified schema, and we don't need it.
46 abstract protected function getMcrMigrationStage();
51 abstract protected function getMcrTablesToReset();
53 protected function setUp() {
56 $this->tablesUsed +
= $this->getMcrTablesToReset();
58 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
60 'wgMultiContentRevisionSchemaMigrationStage',
61 $this->getMcrMigrationStage()
63 $this->pagesToDelete
= [];
65 $this->overrideMwServices();
68 protected function tearDown() {
69 foreach ( $this->pagesToDelete
as $p ) {
70 /* @var $p WikiPage */
74 $p->doDeleteArticle( "testing done." );
76 } catch ( MWException
$ex ) {
83 abstract protected function getContentHandlerUseDB();
86 * @param Title|string $title
87 * @param string|null $model
90 private function newPage( $title, $model = null ) {
91 if ( is_string( $title ) ) {
92 $ns = $this->getDefaultWikitextNS();
93 $title = Title
::newFromText( $title, $ns );
96 $p = new WikiPage( $title );
98 $this->pagesToDelete
[] = $p;
104 * @param string|Title|WikiPage $page
105 * @param string $text
110 protected function createPage( $page, $text, $model = null, $user = null ) {
111 if ( is_string( $page ) ||
$page instanceof Title
) {
112 $page = $this->newPage( $page, $model );
115 $content = ContentHandler
::makeContent( $text, $page->getTitle(), $model );
116 $page->doEditContent( $content, "testing", EDIT_NEW
, false, $user );
122 * @covers WikiPage::prepareContentForEdit
124 public function testPrepareContentForEdit() {
125 $user = $this->getTestUser()->getUser();
126 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
128 $page = $this->createPage( __METHOD__
, __METHOD__
, null, $user );
129 $title = $page->getTitle();
131 $content = ContentHandler
::makeContent(
132 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
133 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
135 CONTENT_MODEL_WIKITEXT
137 $content2 = ContentHandler
::makeContent(
138 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
139 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
141 CONTENT_MODEL_WIKITEXT
144 $edit = $page->prepareContentForEdit( $content, null, $user, null, false );
146 $this->assertInstanceOf(
147 ParserOptions
::class,
151 $this->assertContains( '</a>', $edit->output
->getText(), "output" );
152 $this->assertContains(
153 'consetetur sadipscing elitr',
154 $edit->output
->getText(),
158 $this->assertTrue( $content->equals( $edit->newContent
), "newContent field" );
159 $this->assertTrue( $content->equals( $edit->pstContent
), "pstContent field" );
160 $this->assertSame( $edit->output
, $edit->output
, "output field" );
161 $this->assertSame( $edit->popts
, $edit->popts
, "popts field" );
162 $this->assertSame( null, $edit->revid
, "revid field" );
164 // Re-using the prepared info if possible
165 $sameEdit = $page->prepareContentForEdit( $content, null, $user, null, false );
166 $this->assertEquals( $edit, $sameEdit, 'equivalent PreparedEdit' );
167 $this->assertSame( $edit->pstContent
, $sameEdit->pstContent
, 're-use output' );
168 $this->assertSame( $edit->output
, $sameEdit->output
, 're-use output' );
170 // Not re-using the same PreparedEdit if not possible
171 $rev = $page->getRevision();
172 $edit2 = $page->prepareContentForEdit( $content2, null, $user, null, false );
173 $this->assertNotEquals( $edit, $edit2 );
174 $this->assertContains( 'At vero eos', $edit2->pstContent
->serialize(), "content" );
176 // Check pre-safe transform
177 $this->assertContains( '[[gubergren]]', $edit2->pstContent
->serialize() );
178 $this->assertNotContains( '~~~~', $edit2->pstContent
->serialize() );
180 $edit3 = $page->prepareContentForEdit( $content2, null, $sysop, null, false );
181 $this->assertNotEquals( $edit2, $edit3 );
183 // TODO: test with passing revision, then same without revision.
187 * @covers WikiPage::doEditUpdates
189 public function testDoEditUpdates() {
190 $user = $this->getTestUser()->getUser();
192 // NOTE: if site stats get out of whack and drop below 0,
193 // that causes a DB error during tear-down. So bump the
194 // numbers high enough to not drop below 0.
195 $siteStatsUpdate = SiteStatsUpdate
::factory(
196 [ 'edits' => 1000, 'articles' => 1000, 'pages' => 1000 ]
198 $siteStatsUpdate->doUpdate();
200 $page = $this->createPage( __METHOD__
, __METHOD__
);
202 $revision = new Revision(
205 'page' => $page->getId(),
206 'title' => $page->getTitle(),
207 'comment' => __METHOD__
,
208 'minor_edit' => true,
209 'text' => __METHOD__
. ' [[|foo]][[bar]]', // PST turns [[|foo]] into [[foo]]
210 'user' => $user->getId(),
211 'user_text' => $user->getName(),
212 'timestamp' => '20170707040404',
213 'content_model' => CONTENT_MODEL_WIKITEXT
,
214 'content_format' => CONTENT_FORMAT_WIKITEXT
,
218 $page->doEditUpdates( $revision, $user );
220 // TODO: test various options; needs temporary hooks
222 $dbr = wfGetDB( DB_REPLICA
);
223 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $page->getId() ] );
224 $n = $res->numRows();
227 $this->assertEquals( 1, $n, 'pagelinks should contain only one link if PST was not applied' );
231 * @covers WikiPage::doEditContent
232 * @covers WikiPage::prepareContentForEdit
234 public function testDoEditContent() {
235 $this->setMwGlobals( 'wgPageCreationLog', true );
237 $page = $this->newPage( __METHOD__
);
238 $title = $page->getTitle();
240 $user1 = $this->getTestUser()->getUser();
241 // Use the confirmed group for user2 to make sure the user is different
242 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
244 $content = ContentHandler
::makeContent(
245 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
246 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
248 CONTENT_MODEL_WIKITEXT
251 $status = $page->doEditContent( $content, "[[testing]] 1", EDIT_NEW
, false, $user1 );
253 $this->assertTrue( $status->isOK(), 'OK' );
254 $this->assertTrue( $status->value
['new'], 'new' );
255 $this->assertNotNull( $status->value
['revision'], 'revision' );
256 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
257 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
258 $this->assertTrue( $status->value
['revision']->getContent()->equals( $content ), 'equals' );
260 $rev = $page->getRevision();
261 $this->assertNotNull( $rev->getRecentChange() );
262 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
264 $id = $page->getId();
266 // Test page creation logging
269 [ 'log_type', 'log_action' ],
270 [ 'log_page' => $id ],
271 [ [ 'create', 'create' ] ]
274 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
275 $this->assertTrue( $id > 0, "WikiPage should have new page id" );
276 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
277 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
279 # ------------------------
280 $dbr = wfGetDB( DB_REPLICA
);
281 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
282 $n = $res->numRows();
285 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
287 # ------------------------
288 $page = new WikiPage( $title );
290 $retrieved = $page->getContent();
291 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
293 # ------------------------
294 $page = new WikiPage( $title );
296 // try null edit, with a different user
297 $status = $page->doEditContent( $content, 'This changes nothing', EDIT_UPDATE
, false, $user2 );
298 $this->assertTrue( $status->isOK(), 'OK' );
299 $this->assertFalse( $status->value
['new'], 'new' );
300 $this->assertNull( $status->value
['revision'], 'revision' );
301 $this->assertNotNull( $page->getRevision() );
302 $this->assertTrue( $page->getRevision()->getContent()->equals( $content ), 'equals' );
304 # ------------------------
305 $content = ContentHandler
::makeContent(
306 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
307 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
309 CONTENT_MODEL_WIKITEXT
312 $status = $page->doEditContent( $content, "testing 2", EDIT_UPDATE
);
313 $this->assertTrue( $status->isOK(), 'OK' );
314 $this->assertFalse( $status->value
['new'], 'new' );
315 $this->assertNotNull( $status->value
['revision'], 'revision' );
316 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
317 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
319 $status->value
['revision']->getContent()->equals( $content ),
320 'not equals (PST must substitute signature)'
323 $rev = $page->getRevision();
324 $this->assertNotNull( $rev->getRecentChange() );
325 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
327 # ------------------------
328 $page = new WikiPage( $title );
330 $retrieved = $page->getContent();
331 $newText = $retrieved->serialize();
332 $this->assertContains( '[[gubergren]]', $newText, 'New text must replace old text.' );
333 $this->assertNotContains( '~~~~', $newText, 'PST must substitute signature.' );
335 # ------------------------
336 $dbr = wfGetDB( DB_REPLICA
);
337 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
338 $n = $res->numRows();
341 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
345 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
346 * TODO: Revision deletion
348 * @covers WikiPage::doDeleteArticle
349 * @covers WikiPage::doDeleteArticleReal
351 public function testDoDeleteArticle() {
352 $page = $this->createPage(
354 "[[original text]] foo",
355 CONTENT_MODEL_WIKITEXT
357 $id = $page->getId();
359 $page->doDeleteArticle( "testing deletion" );
362 $page->getTitle()->getArticleID() > 0,
363 "Title object should now have page id 0"
365 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
368 "WikiPage::exists should return false after page was deleted"
372 "WikiPage::getContent should return null after page was deleted"
375 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
378 "Title::exists should return false after page was deleted"
382 JobQueueGroup
::destroySingletons();
384 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
387 # ------------------------
388 $dbr = wfGetDB( DB_REPLICA
);
389 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
390 $n = $res->numRows();
393 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
397 * @covers WikiPage::doDeleteArticleReal
399 public function testDoDeleteArticleReal_user0() {
400 $page = $this->createPage(
402 "[[original text]] foo",
403 CONTENT_MODEL_WIKITEXT
405 $id = $page->getId();
408 $status = $page->doDeleteArticleReal(
409 /* reason */ "testing user 0 deletion",
410 /* suppress */ false,
413 /* errorStack */ $errorStack,
416 $logId = $status->getValue();
417 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
419 [ 'logging' ] +
$actorQuery['tables'], /* table */
424 'log_user' => $actorQuery['fields']['log_user'],
425 'log_user_text' => $actorQuery['fields']['log_user_text'],
429 [ 'log_id' => $logId ],
433 'testing user 0 deletion',
436 (string)$page->getTitle()->getNamespace(),
437 $page->getTitle()->getDBkey(),
445 * @covers WikiPage::doDeleteArticleReal
447 public function testDoDeleteArticleReal_userSysop() {
448 $page = $this->createPage(
450 "[[original text]] foo",
451 CONTENT_MODEL_WIKITEXT
453 $id = $page->getId();
455 $user = $this->getTestSysop()->getUser();
457 $status = $page->doDeleteArticleReal(
458 /* reason */ "testing sysop deletion",
459 /* suppress */ false,
462 /* errorStack */ $errorStack,
465 $logId = $status->getValue();
466 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
468 [ 'logging' ] +
$actorQuery['tables'], /* table */
473 'log_user' => $actorQuery['fields']['log_user'],
474 'log_user_text' => $actorQuery['fields']['log_user_text'],
478 [ 'log_id' => $logId ],
482 'testing sysop deletion',
483 (string)$user->getId(),
485 (string)$page->getTitle()->getNamespace(),
486 $page->getTitle()->getDBkey(),
494 * TODO: Test more stuff about suppression.
496 * @covers WikiPage::doDeleteArticleReal
498 public function testDoDeleteArticleReal_suppress() {
499 $page = $this->createPage(
501 "[[original text]] foo",
502 CONTENT_MODEL_WIKITEXT
504 $id = $page->getId();
506 $user = $this->getTestSysop()->getUser();
508 $status = $page->doDeleteArticleReal(
509 /* reason */ "testing deletion",
513 /* errorStack */ $errorStack,
516 $logId = $status->getValue();
517 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
519 [ 'logging' ] +
$actorQuery['tables'], /* table */
524 'log_user' => $actorQuery['fields']['log_user'],
525 'log_user_text' => $actorQuery['fields']['log_user_text'],
529 [ 'log_id' => $logId ],
534 (string)$user->getId(),
536 (string)$page->getTitle()->getNamespace(),
537 $page->getTitle()->getDBkey(),
544 $page->getContent( Revision
::FOR_PUBLIC
),
545 "WikiPage::getContent should return null after the page was suppressed for general users"
549 $page->getContent( Revision
::FOR_THIS_USER
, null ),
550 "WikiPage::getContent should return null after the page was suppressed for user zero"
554 $page->getContent( Revision
::FOR_THIS_USER
, $user ),
555 "WikiPage::getContent should return null after the page was suppressed even for a sysop"
560 * @covers WikiPage::doDeleteUpdates
562 public function testDoDeleteUpdates() {
563 $page = $this->createPage(
565 "[[original text]] foo",
566 CONTENT_MODEL_WIKITEXT
568 $id = $page->getId();
570 // Similar to MovePage logic
571 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
572 $page->doDeleteUpdates( $id );
575 JobQueueGroup
::destroySingletons();
577 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
580 # ------------------------
581 $dbr = wfGetDB( DB_REPLICA
);
582 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
583 $n = $res->numRows();
586 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
590 * @covers WikiPage::getRevision
592 public function testGetRevision() {
593 $page = $this->newPage( __METHOD__
);
595 $rev = $page->getRevision();
596 $this->assertNull( $rev );
599 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
601 $rev = $page->getRevision();
603 $this->assertEquals( $page->getLatest(), $rev->getId() );
604 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
608 * @covers WikiPage::getContent
610 public function testGetContent() {
611 $page = $this->newPage( __METHOD__
);
613 $content = $page->getContent();
614 $this->assertNull( $content );
617 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
619 $content = $page->getContent();
620 $this->assertEquals( "some text", $content->getNativeData() );
624 * @covers WikiPage::exists
626 public function testExists() {
627 $page = $this->newPage( __METHOD__
);
628 $this->assertFalse( $page->exists() );
631 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
632 $this->assertTrue( $page->exists() );
634 $page = new WikiPage( $page->getTitle() );
635 $this->assertTrue( $page->exists() );
638 $page->doDeleteArticle( "done testing" );
639 $this->assertFalse( $page->exists() );
641 $page = new WikiPage( $page->getTitle() );
642 $this->assertFalse( $page->exists() );
645 public function provideHasViewableContent() {
647 [ 'WikiPageTest_testHasViewableContent', false, true ],
648 [ 'Special:WikiPageTest_testHasViewableContent', false ],
649 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
650 [ 'Special:Userlogin', true ],
651 [ 'MediaWiki:help', true ],
656 * @dataProvider provideHasViewableContent
657 * @covers WikiPage::hasViewableContent
659 public function testHasViewableContent( $title, $viewable, $create = false ) {
660 $page = $this->newPage( $title );
661 $this->assertEquals( $viewable, $page->hasViewableContent() );
664 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
665 $this->assertTrue( $page->hasViewableContent() );
667 $page = new WikiPage( $page->getTitle() );
668 $this->assertTrue( $page->hasViewableContent() );
672 public function provideGetRedirectTarget() {
674 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
676 'WikiPageTest_testGetRedirectTarget_2',
677 CONTENT_MODEL_WIKITEXT
,
678 "#REDIRECT [[hello world]]",
685 * @dataProvider provideGetRedirectTarget
686 * @covers WikiPage::getRedirectTarget
688 public function testGetRedirectTarget( $title, $model, $text, $target ) {
689 $this->setMwGlobals( [
690 'wgCapitalLinks' => true,
693 $page = $this->createPage( $title, $text, $model );
695 # sanity check, because this test seems to fail for no reason for some people.
696 $c = $page->getContent();
697 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
699 # now, test the actual redirect
700 $t = $page->getRedirectTarget();
701 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
705 * @dataProvider provideGetRedirectTarget
706 * @covers WikiPage::isRedirect
708 public function testIsRedirect( $title, $model, $text, $target ) {
709 $page = $this->createPage( $title, $text, $model );
710 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
713 public function provideIsCountable() {
717 [ 'WikiPageTest_testIsCountable',
718 CONTENT_MODEL_WIKITEXT
,
723 [ 'WikiPageTest_testIsCountable',
724 CONTENT_MODEL_WIKITEXT
,
731 [ 'WikiPageTest_testIsCountable',
732 CONTENT_MODEL_WIKITEXT
,
737 [ 'WikiPageTest_testIsCountable',
738 CONTENT_MODEL_WIKITEXT
,
745 [ 'WikiPageTest_testIsCountable',
746 CONTENT_MODEL_WIKITEXT
,
751 [ 'WikiPageTest_testIsCountable',
752 CONTENT_MODEL_WIKITEXT
,
758 // not a content namespace
759 [ 'Talk:WikiPageTest_testIsCountable',
760 CONTENT_MODEL_WIKITEXT
,
765 [ 'Talk:WikiPageTest_testIsCountable',
766 CONTENT_MODEL_WIKITEXT
,
772 // not a content namespace, different model
773 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
779 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
789 * @dataProvider provideIsCountable
790 * @covers WikiPage::isCountable
792 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
793 global $wgContentHandlerUseDB;
795 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
797 $title = Title
::newFromText( $title );
799 if ( !$wgContentHandlerUseDB
801 && ContentHandler
::getDefaultModelFor( $title ) != $model
803 $this->markTestSkipped( "Can not use non-default content model $model for "
804 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
807 $page = $this->createPage( $title, $text, $model );
809 $editInfo = $page->prepareContentForEdit( $page->getContent() );
811 $v = $page->isCountable();
812 $w = $page->isCountable( $editInfo );
817 "isCountable( null ) returned unexpected value " . var_export( $v, true )
818 . " instead of " . var_export( $expected, true )
819 . " in mode `$mode` for text \"$text\""
825 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
826 . " instead of " . var_export( $expected, true )
827 . " in mode `$mode` for text \"$text\""
831 public function provideGetParserOutput() {
834 CONTENT_MODEL_WIKITEXT
,
836 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
843 * @dataProvider provideGetParserOutput
844 * @covers WikiPage::getParserOutput
846 public function testGetParserOutput( $model, $text, $expectedHtml ) {
847 $page = $this->createPage( __METHOD__
, $text, $model );
849 $opt = $page->makeParserOptions( 'canonical' );
850 $po = $page->getParserOutput( $opt );
851 $text = $po->getText();
853 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
854 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
856 $this->assertEquals( $expectedHtml, $text );
862 * @covers WikiPage::getParserOutput
864 public function testGetParserOutput_nonexisting() {
865 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
867 $opt = new ParserOptions();
868 $po = $page->getParserOutput( $opt );
870 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
874 * @covers WikiPage::getParserOutput
876 public function testGetParserOutput_badrev() {
877 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
879 $opt = new ParserOptions();
880 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
882 // @todo would be neat to also test deleted revision
884 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
887 public static $sections =
901 public function dataReplaceSection() {
902 // NOTE: assume the Help namespace to contain wikitext
904 [ 'Help:WikiPageTest_testReplaceSection',
905 CONTENT_MODEL_WIKITEXT
,
910 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
912 [ 'Help:WikiPageTest_testReplaceSection',
913 CONTENT_MODEL_WIKITEXT
,
920 [ 'Help:WikiPageTest_testReplaceSection',
921 CONTENT_MODEL_WIKITEXT
,
924 "== TEST ==\nmore fun",
926 trim( preg_replace( '/^== test ==.*== foo ==/sm',
927 "== TEST ==\nmore fun\n\n== foo ==",
930 [ 'Help:WikiPageTest_testReplaceSection',
931 CONTENT_MODEL_WIKITEXT
,
936 trim( self
::$sections )
938 [ 'Help:WikiPageTest_testReplaceSection',
939 CONTENT_MODEL_WIKITEXT
,
944 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
950 * @dataProvider dataReplaceSection
951 * @covers WikiPage::replaceSectionContent
953 public function testReplaceSectionContent( $title, $model, $text, $section,
954 $with, $sectionTitle, $expected
956 $page = $this->createPage( $title, $text, $model );
958 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
959 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
961 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
965 * @dataProvider dataReplaceSection
966 * @covers WikiPage::replaceSectionAtRev
968 public function testReplaceSectionAtRev( $title, $model, $text, $section,
969 $with, $sectionTitle, $expected
971 $page = $this->createPage( $title, $text, $model );
972 $baseRevId = $page->getLatest();
974 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
975 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
977 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
981 * @covers WikiPage::getOldestRevision
983 public function testGetOldestRevision() {
984 $page = $this->newPage( __METHOD__
);
985 $page->doEditContent(
986 new WikitextContent( 'one' ),
990 $rev1 = $page->getRevision();
992 $page = new WikiPage( $page->getTitle() );
993 $page->doEditContent(
994 new WikitextContent( 'two' ),
999 $page = new WikiPage( $page->getTitle() );
1000 $page->doEditContent(
1001 new WikitextContent( 'three' ),
1007 $this->assertNotEquals(
1009 $page->getRevision()->getId(),
1010 '$page->getRevision()->getId()'
1014 $this->assertEquals(
1016 $page->getOldestRevision()->getId(),
1017 '$page->getOldestRevision()->getId()'
1022 * @covers WikiPage::doRollback
1023 * @covers WikiPage::commitRollback
1025 public function testDoRollback() {
1026 $admin = $this->getTestSysop()->getUser();
1027 $user1 = $this->getTestUser()->getUser();
1028 // Use the confirmed group for user2 to make sure the user is different
1029 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
1031 $page = $this->newPage( __METHOD__
);
1035 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1036 "section one", EDIT_NEW
, false, $admin );
1039 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1040 "adding section two", 0, false, $user1 );
1042 $text .= "\n\nthree";
1043 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1044 "adding section three", 0, false, $user2 );
1046 /** @var Revision $rev1 */
1047 /** @var Revision $rev2 */
1048 /** @var Revision $rev3 */
1049 $rev1 = $status1->getValue()['revision'];
1050 $rev2 = $status2->getValue()['revision'];
1051 $rev3 = $status3->getValue()['revision'];
1054 * We are having issues with doRollback spuriously failing. Apparently
1055 * the last revision somehow goes missing or not committed under some
1056 * circumstances. So, make sure the revisions have the correct usernames.
1058 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
1059 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
1060 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
1061 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
1063 // Now, try the actual rollback
1064 $token = $admin->getEditToken( 'rollback' );
1065 $rollbackErrors = $page->doRollback(
1074 if ( $rollbackErrors ) {
1076 "Rollback failed:\n" .
1077 print_r( $rollbackErrors, true ) . ";\n" .
1078 print_r( $resultDetails, true )
1082 $page = new WikiPage( $page->getTitle() );
1083 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
1084 "rollback did not revert to the correct revision" );
1085 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
1089 * @covers WikiPage::doRollback
1090 * @covers WikiPage::commitRollback
1092 public function testDoRollback_simple() {
1093 $admin = $this->getTestSysop()->getUser();
1096 $page = $this->newPage( __METHOD__
);
1097 $page->doEditContent(
1098 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1104 $rev1 = $page->getRevision();
1106 $user1 = $this->getTestUser()->getUser();
1108 $page = new WikiPage( $page->getTitle() );
1109 $page->doEditContent(
1110 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1111 "adding section two",
1117 # now, try the rollback
1118 $token = $admin->getEditToken( 'rollback' );
1119 $errors = $page->doRollback(
1129 $this->fail( "Rollback failed:\n" . print_r( $errors, true )
1130 . ";\n" . print_r( $details, true ) );
1133 $page = new WikiPage( $page->getTitle() );
1134 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1135 "rollback did not revert to the correct revision" );
1136 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1140 * @covers WikiPage::doRollback
1141 * @covers WikiPage::commitRollback
1143 public function testDoRollbackFailureSameContent() {
1144 $admin = $this->getTestSysop()->getUser();
1147 $page = $this->newPage( __METHOD__
);
1148 $page->doEditContent(
1149 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1155 $rev1 = $page->getRevision();
1157 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
1159 $page = new WikiPage( $page->getTitle() );
1160 $page->doEditContent(
1161 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1162 "adding section two",
1168 # now, do a the rollback from the same user was doing the edit before
1169 $resultDetails = [];
1170 $token = $user1->getEditToken( 'rollback' );
1171 $errors = $page->doRollback(
1173 "testing revert same user",
1180 $this->assertEquals( [], $errors, "Rollback failed same user" );
1182 # now, try the rollback
1183 $resultDetails = [];
1184 $token = $admin->getEditToken( 'rollback' );
1185 $errors = $page->doRollback(
1194 $this->assertEquals(
1204 "Rollback not failed"
1207 $page = new WikiPage( $page->getTitle() );
1208 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1209 "rollback did not revert to the correct revision" );
1210 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1214 * Tests tagging for edits that do rollback action
1215 * @covers WikiPage::doRollback
1217 public function testDoRollbackTagging() {
1218 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
1219 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
1222 $admin = new User();
1223 $admin->setName( 'Administrator' );
1224 $admin->addToDatabase();
1226 $text = 'First line';
1227 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
1228 $page->doEditContent(
1229 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1236 $secondUser = new User();
1237 $secondUser->setName( '92.65.217.32' );
1238 $text .= '\n\nSecond line';
1239 $page = new WikiPage( $page->getTitle() );
1240 $page->doEditContent(
1241 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1242 'Adding second line',
1248 // Now, try the rollback
1249 $admin->addGroup( 'sysop' ); // Make the test user a sysop
1250 $token = $admin->getEditToken( 'rollback' );
1251 $errors = $page->doRollback(
1252 $secondUser->getName(),
1260 // If doRollback completed without errors
1261 if ( $errors === [] ) {
1262 $tags = $resultDetails[ 'tags' ];
1263 $this->assertContains( 'mw-rollback', $tags );
1267 public function provideGetAutoDeleteReason() {
1277 [ "first edit", null ],
1279 "/first edit.*only contributor/",
1285 [ "first edit", null ],
1286 [ "second edit", null ],
1288 "/second edit.*only contributor/",
1294 [ "first edit", "127.0.2.22" ],
1295 [ "second edit", "127.0.3.33" ],
1305 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1306 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1307 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1308 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1309 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1313 '/first edit:.*\.\.\."/',
1319 [ "first edit", "127.0.2.22" ],
1320 [ "", "127.0.3.33" ],
1322 "/before blanking.*first edit/",
1330 * @dataProvider provideGetAutoDeleteReason
1331 * @covers WikiPage::getAutoDeleteReason
1333 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1336 // NOTE: assume Help namespace to contain wikitext
1337 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1341 foreach ( $edits as $edit ) {
1344 if ( !empty( $edit[1] ) ) {
1345 $user->setName( $edit[1] );
1350 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1352 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1357 $reason = $page->getAutoDeleteReason( $hasHistory );
1359 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1360 $this->assertEquals( $expectedResult, $reason );
1362 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1363 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1366 $this->assertEquals( $expectedHistory, $hasHistory,
1367 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1369 $page->doDeleteArticle( "done" );
1372 public function providePreSaveTransform() {
1374 [ 'hello this is ~~~',
1375 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1377 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1378 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1384 * @covers WikiPage::factory
1386 public function testWikiPageFactory() {
1387 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1388 $page = WikiPage
::factory( $title );
1389 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1391 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1392 $page = WikiPage
::factory( $title );
1393 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1395 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1396 $page = WikiPage
::factory( $title );
1397 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1401 * @covers WikiPage::loadPageData
1402 * @covers WikiPage::wasLoadedFrom
1404 public function testLoadPageData() {
1405 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1406 $page = WikiPage
::factory( $title );
1408 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1409 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1410 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1411 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1413 $page->loadPageData( IDBAccessObject
::READ_NORMAL
);
1414 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1415 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1416 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1417 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1419 $page->loadPageData( IDBAccessObject
::READ_LATEST
);
1420 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1421 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1422 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1423 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1425 $page->loadPageData( IDBAccessObject
::READ_LOCKING
);
1426 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1427 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1428 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1429 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1431 $page->loadPageData( IDBAccessObject
::READ_EXCLUSIVE
);
1432 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1433 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1434 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1435 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1439 * @dataProvider provideCommentMigrationOnDeletion
1441 * @param int $writeStage
1442 * @param int $readStage
1444 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1445 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1446 $this->overrideMwServices();
1448 $dbr = wfGetDB( DB_REPLICA
);
1450 $page = $this->createPage(
1453 CONTENT_MODEL_WIKITEXT
1455 $revid = $page->getLatest();
1456 if ( $writeStage > MIGRATION_OLD
) {
1457 $comment_id = $dbr->selectField(
1458 'revision_comment_temp',
1459 'revcomment_comment_id',
1460 [ 'revcomment_rev' => $revid ],
1465 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1466 $this->overrideMwServices();
1468 $page->doDeleteArticle( "testing deletion" );
1470 if ( $readStage > MIGRATION_OLD
) {
1471 // Didn't leave behind any 'revision_comment_temp' rows
1472 $n = $dbr->selectField(
1473 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1475 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1477 // Copied or upgraded the comment_id, as applicable
1478 $ar_comment_id = $dbr->selectField(
1481 [ 'ar_rev_id' => $revid ],
1484 if ( $writeStage > MIGRATION_OLD
) {
1485 $this->assertSame( $comment_id, $ar_comment_id );
1487 $this->assertNotEquals( 0, $ar_comment_id );
1491 // Copied rev_comment, if applicable
1492 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1493 $ar_comment = $dbr->selectField(
1496 [ 'ar_rev_id' => $revid ],
1499 $this->assertSame( 'testing', $ar_comment );
1503 public function provideCommentMigrationOnDeletion() {
1505 [ MIGRATION_OLD
, MIGRATION_OLD
],
1506 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1507 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1508 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1509 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1510 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1511 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1512 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1513 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1514 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1515 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1516 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1517 [ MIGRATION_NEW
, MIGRATION_NEW
],
1522 * @covers WikiPage::updateCategoryCounts
1524 public function testUpdateCategoryCounts() {
1525 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1527 // Add an initial category
1528 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1530 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1531 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1532 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1534 // Add a new category
1535 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1537 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1538 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1539 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1541 // Add and remove a category
1542 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1544 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1545 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1546 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1549 public function provideUpdateRedirectOn() {
1550 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1551 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1552 yield
[ 'SomeText', false, null, false, true, 0 ];
1553 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1557 * @dataProvider provideUpdateRedirectOn
1558 * @covers WikiPage::updateRedirectOn
1560 * @param string $initialText
1561 * @param bool $initialRedirectState
1562 * @param string|null $redirectTitle
1563 * @param bool|null $lastRevIsRedirect
1564 * @param bool $expectedSuccess
1565 * @param int $expectedRowCount
1567 public function testUpdateRedirectOn(
1569 $initialRedirectState,
1575 static $pageCounter = 0;
1578 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1579 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1581 $redirectTitle = is_string( $redirectTitle )
1582 ? Title
::newFromText( $redirectTitle )
1585 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1586 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1588 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1589 * Most of core checks the page table for redirect status, so we have to be ugly and
1590 * assert a select from the table here.
1592 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1595 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1596 $this->assertSelect(
1599 [ 'rd_from' => $pageId ],
1600 [ [ strval( $expected ) ] ]
1605 * @covers WikiPage::insertRedirectEntry
1607 public function testInsertRedirectEntry_insertsRedirectEntry() {
1608 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1609 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1611 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1612 $targetTitle->mInterwiki
= 'eninter';
1613 $page->insertRedirectEntry( $targetTitle, null );
1615 $this->assertSelect(
1617 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1618 [ 'rd_from' => $page->getId() ],
1620 strval( $page->getId() ),
1621 strval( $targetTitle->getNamespace() ),
1622 strval( $targetTitle->getDBkey() ),
1623 strval( $targetTitle->getFragment() ),
1624 strval( $targetTitle->getInterwiki() ),
1630 * @covers WikiPage::insertRedirectEntry
1632 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1633 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1634 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1636 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1637 $targetTitle->mInterwiki
= 'eninter';
1638 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1640 $this->assertSelect(
1642 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1643 [ 'rd_from' => $page->getId() ],
1645 strval( $page->getId() ),
1646 strval( $targetTitle->getNamespace() ),
1647 strval( $targetTitle->getDBkey() ),
1648 strval( $targetTitle->getFragment() ),
1649 strval( $targetTitle->getInterwiki() ),
1655 * @covers WikiPage::insertRedirectEntry
1657 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1658 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1659 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1661 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1662 $targetTitle->mInterwiki
= 'eninter';
1663 $page->insertRedirectEntry( $targetTitle, 215251 );
1665 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1668 private function getRow( array $overrides = [] ) {
1672 'page_is_redirect' => '1',
1673 'page_latest' => '99',
1674 'page_namespace' => '3',
1675 'page_title' => 'JaJaTitle',
1676 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1677 'page_touched' => '20120101020202',
1678 'page_links_updated' => '20140101020202',
1680 foreach ( $overrides as $key => $value ) {
1681 $row[$key] = $value;
1683 return (object)$row;
1686 public function provideNewFromRowSuccess() {
1687 yield
'basic row' => [
1689 function ( WikiPage
$wikiPage, self
$test ) {
1690 $test->assertSame( 44, $wikiPage->getId() );
1691 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1692 $test->assertTrue( $wikiPage->isRedirect() );
1693 $test->assertSame( 99, $wikiPage->getLatest() );
1694 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1695 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1698 'edit' => [ 'autoconfirmed', 'sysop' ],
1699 'move' => [ 'sysop' ],
1701 $wikiPage->getTitle()->getAllRestrictions()
1703 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1704 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1707 yield
'different timestamp formats' => [
1709 'page_touched' => '2012-01-01 02:02:02',
1710 'page_links_updated' => '2014-01-01 02:02:02',
1712 function ( WikiPage
$wikiPage, self
$test ) {
1713 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1714 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1717 yield
'no restrictions' => [
1719 'page_restrictions' => '',
1721 function ( WikiPage
$wikiPage, self
$test ) {
1727 $wikiPage->getTitle()->getAllRestrictions()
1731 yield
'not redirect' => [
1733 'page_is_redirect' => '0',
1735 function ( WikiPage
$wikiPage, self
$test ) {
1736 $test->assertFalse( $wikiPage->isRedirect() );
1742 * @covers WikiPage::newFromRow
1743 * @covers WikiPage::loadFromRow
1744 * @dataProvider provideNewFromRowSuccess
1746 * @param object $row
1747 * @param callable $assertions
1749 public function testNewFromRow( $row, $assertions ) {
1750 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1751 $assertions( $page, $this );
1754 public function provideTestNewFromId_returnsNullOnBadPageId() {
1760 * @covers WikiPage::newFromID
1761 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1763 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1764 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1768 * @covers WikiPage::newFromID
1770 public function testNewFromId_appearsToFetchCorrectRow() {
1771 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1772 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1773 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1774 $this->assertEquals(
1775 $createdPage->getContent()->getNativeData(),
1776 $fetchedPage->getContent()->getNativeData()
1781 * @covers WikiPage::newFromID
1783 public function testNewFromId_returnsNullOnNonExistingId() {
1784 $this->assertNull( WikiPage
::newFromID( 2147483647 ) );
1787 public function provideTestInsertProtectNullRevision() {
1788 // phpcs:disable Generic.Files.LineLength
1791 [ 'edit' => 'sysop' ],
1792 [ 'edit' => '20200101040404' ],
1796 '(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)))'
1800 [ 'edit' => 'sysop', 'move' => 'something' ],
1801 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1805 '(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)))'
1811 * @dataProvider provideTestInsertProtectNullRevision
1812 * @covers WikiPage::insertProtectNullRevision
1813 * @covers WikiPage::protectDescription
1815 * @param string $revCommentMsg
1816 * @param array $limit
1817 * @param array $expiry
1818 * @param bool $cascade
1819 * @param string $reason
1820 * @param bool|null $user true if the test sysop should be used, or null
1821 * @param string $expectedComment
1823 public function testInsertProtectNullRevision(
1832 $this->setContentLang( 'qqx' );
1834 $page = $this->createPage( __METHOD__
, 'Goat' );
1836 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1838 $result = $page->insertProtectNullRevision(
1847 $this->assertTrue( $result instanceof Revision
);
1848 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1852 * @covers WikiPage::updateRevisionOn
1854 public function testUpdateRevisionOn_existingPage() {
1855 $user = $this->getTestSysop()->getUser();
1856 $page = $this->createPage( __METHOD__
, 'StartText' );
1858 $revision = new Revision(
1861 'page' => $page->getId(),
1862 'title' => $page->getTitle(),
1863 'comment' => __METHOD__
,
1864 'minor_edit' => true,
1865 'text' => __METHOD__
. '-text',
1866 'len' => strlen( __METHOD__
. '-text' ),
1867 'user' => $user->getId(),
1868 'user_text' => $user->getName(),
1869 'timestamp' => '20170707040404',
1870 'content_model' => CONTENT_MODEL_WIKITEXT
,
1871 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1875 $result = $page->updateRevisionOn( $this->db
, $revision );
1876 $this->assertTrue( $result );
1877 $this->assertSame( 9989, $page->getLatest() );
1878 $this->assertEquals( $revision, $page->getRevision() );
1882 * @covers WikiPage::updateRevisionOn
1884 public function testUpdateRevisionOn_NonExistingPage() {
1885 $user = $this->getTestSysop()->getUser();
1886 $page = $this->createPage( __METHOD__
, 'StartText' );
1887 $page->doDeleteArticle( 'reason' );
1889 $revision = new Revision(
1892 'page' => $page->getId(),
1893 'title' => $page->getTitle(),
1894 'comment' => __METHOD__
,
1895 'minor_edit' => true,
1896 'text' => __METHOD__
. '-text',
1897 'len' => strlen( __METHOD__
. '-text' ),
1898 'user' => $user->getId(),
1899 'user_text' => $user->getName(),
1900 'timestamp' => '20170707040404',
1901 'content_model' => CONTENT_MODEL_WIKITEXT
,
1902 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1906 $result = $page->updateRevisionOn( $this->db
, $revision );
1907 $this->assertFalse( $result );
1911 * @covers WikiPage::updateIfNewerOn
1913 public function testUpdateIfNewerOn_olderRevision() {
1914 $user = $this->getTestSysop()->getUser();
1915 $page = $this->createPage( __METHOD__
, 'StartText' );
1916 $initialRevision = $page->getRevision();
1918 $olderTimeStamp = wfTimestamp(
1920 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
1923 $olderRevison = new Revision(
1926 'page' => $page->getId(),
1927 'title' => $page->getTitle(),
1928 'comment' => __METHOD__
,
1929 'minor_edit' => true,
1930 'text' => __METHOD__
. '-text',
1931 'len' => strlen( __METHOD__
. '-text' ),
1932 'user' => $user->getId(),
1933 'user_text' => $user->getName(),
1934 'timestamp' => $olderTimeStamp,
1935 'content_model' => CONTENT_MODEL_WIKITEXT
,
1936 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1940 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
1941 $this->assertFalse( $result );
1945 * @covers WikiPage::updateIfNewerOn
1947 public function testUpdateIfNewerOn_newerRevision() {
1948 $user = $this->getTestSysop()->getUser();
1949 $page = $this->createPage( __METHOD__
, 'StartText' );
1950 $initialRevision = $page->getRevision();
1952 $newerTimeStamp = wfTimestamp(
1954 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
1957 $newerRevision = new Revision(
1960 'page' => $page->getId(),
1961 'title' => $page->getTitle(),
1962 'comment' => __METHOD__
,
1963 'minor_edit' => true,
1964 'text' => __METHOD__
. '-text',
1965 'len' => strlen( __METHOD__
. '-text' ),
1966 'user' => $user->getId(),
1967 'user_text' => $user->getName(),
1968 'timestamp' => $newerTimeStamp,
1969 'content_model' => CONTENT_MODEL_WIKITEXT
,
1970 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1973 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
1974 $this->assertTrue( $result );
1978 * @covers WikiPage::insertOn
1980 public function testInsertOn() {
1981 $title = Title
::newFromText( __METHOD__
);
1982 $page = new WikiPage( $title );
1984 $startTimeStamp = wfTimestampNow();
1985 $result = $page->insertOn( $this->db
);
1986 $endTimeStamp = wfTimestampNow();
1988 $this->assertInternalType( 'int', $result );
1989 $this->assertTrue( $result > 0 );
1991 $condition = [ 'page_id' => $result ];
1993 // Check the default fields have been filled
1994 $this->assertSelect(
1999 'page_restrictions',
2017 // Check the page_random field has been filled
2018 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
2019 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
2021 // Assert the touched timestamp in the DB is roughly when we inserted the page
2022 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
2024 wfTimestamp( TS_UNIX
, $startTimeStamp )
2025 <= wfTimestamp( TS_UNIX
, $pageTouched )
2028 wfTimestamp( TS_UNIX
, $endTimeStamp )
2029 >= wfTimestamp( TS_UNIX
, $pageTouched )
2032 // Try inserting the same page again and checking the result is false (no change)
2033 $result = $page->insertOn( $this->db
);
2034 $this->assertFalse( $result );
2038 * @covers WikiPage::insertOn
2040 public function testInsertOn_idSpecified() {
2041 $title = Title
::newFromText( __METHOD__
);
2042 $page = new WikiPage( $title );
2045 $result = $page->insertOn( $this->db
, $id );
2047 $this->assertSame( $id, $result );
2049 $condition = [ 'page_id' => $result ];
2051 // Check there is actually a row in the db
2052 $this->assertSelect(
2060 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
2061 // Note: Once the current dates passes the date in these tests they will fail.
2062 yield
'move something' => [
2064 [ 'move' => 'something' ],
2066 [ 'edit' => [], 'move' => [ 'something' ] ],
2069 yield
'move something, edit blank' => [
2071 [ 'move' => 'something', 'edit' => '' ],
2073 [ 'edit' => [], 'move' => [ 'something' ] ],
2076 yield
'edit sysop, with expiry' => [
2078 [ 'edit' => 'sysop' ],
2079 [ 'edit' => '21330101020202' ],
2080 [ 'edit' => [ 'sysop' ], 'move' => [] ],
2081 [ 'edit' => '21330101020202' ],
2083 yield
'move and edit, move with expiry' => [
2085 [ 'move' => 'something', 'edit' => 'another' ],
2086 [ 'move' => '22220202010101' ],
2087 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2088 [ 'move' => '22220202010101' ],
2090 yield
'move and edit, edit with infinity expiry' => [
2092 [ 'move' => 'something', 'edit' => 'another' ],
2093 [ 'edit' => 'infinity' ],
2094 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2095 [ 'edit' => 'infinity' ],
2097 yield
'non existing, create something' => [
2099 [ 'create' => 'something' ],
2101 [ 'create' => [ 'something' ] ],
2104 yield
'non existing, create something with expiry' => [
2106 [ 'create' => 'something' ],
2107 [ 'create' => '23451212112233' ],
2108 [ 'create' => [ 'something' ] ],
2109 [ 'create' => '23451212112233' ],
2114 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
2115 * @covers WikiPage::doUpdateRestrictions
2117 public function testDoUpdateRestrictions_setBasicRestrictions(
2121 array $expectedRestrictions,
2122 array $expectedRestrictionExpiries
2124 if ( $pageExists ) {
2125 $page = $this->createPage( __METHOD__
, 'ABC' );
2127 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
2129 $user = $this->getTestSysop()->getUser();
2132 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
2134 $logId = $status->getValue();
2135 $allRestrictions = $page->getTitle()->getAllRestrictions();
2137 $this->assertTrue( $status->isGood() );
2138 $this->assertInternalType( 'int', $logId );
2139 $this->assertSame( $expectedRestrictions, $allRestrictions );
2140 foreach ( $expectedRestrictionExpiries as $key => $value ) {
2141 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
2144 // Make sure the log entry looks good
2145 // log_params is not checked here
2146 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
2147 $this->assertSelect(
2148 [ 'logging' ] +
$actorQuery['tables'],
2151 'log_user' => $actorQuery['fields']['log_user'],
2152 'log_user_text' => $actorQuery['fields']['log_user_text'],
2156 [ 'log_id' => $logId ],
2159 (string)$user->getId(),
2161 (string)$page->getTitle()->getNamespace(),
2162 $page->getTitle()->getDBkey(),
2165 $actorQuery['joins']
2170 * @covers WikiPage::doUpdateRestrictions
2172 public function testDoUpdateRestrictions_failsOnReadOnly() {
2173 $page = $this->createPage( __METHOD__
, 'ABC' );
2174 $user = $this->getTestSysop()->getUser();
2178 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
2179 ->disableOriginalConstructor()
2180 ->setMethods( [ 'isReadOnly', 'getReason' ] )
2182 $readOnly->expects( $this->once() )
2183 ->method( 'isReadOnly' )
2184 ->will( $this->returnValue( true ) );
2185 $readOnly->expects( $this->once() )
2186 ->method( 'getReason' )
2187 ->will( $this->returnValue( 'Some Read Only Reason' ) );
2188 $this->setService( 'ReadOnlyMode', $readOnly );
2190 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
2191 $this->assertFalse( $status->isOK() );
2192 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
2196 * @covers WikiPage::doUpdateRestrictions
2198 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
2199 $page = $this->createPage( __METHOD__
, 'ABC' );
2200 $user = $this->getTestSysop()->getUser();
2202 $limit = [ 'edit' => 'sysop' ];
2204 $status = $page->doUpdateRestrictions(
2213 // The first entry should have a logId as it did something
2214 $this->assertTrue( $status->isGood() );
2215 $this->assertInternalType( 'int', $status->getValue() );
2217 $status = $page->doUpdateRestrictions(
2226 // The second entry should not have a logId as nothing changed
2227 $this->assertTrue( $status->isGood() );
2228 $this->assertNull( $status->getValue() );
2232 * @covers WikiPage::doUpdateRestrictions
2234 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
2235 $page = $this->createPage( __METHOD__
, 'ABC' );
2236 $user = $this->getTestSysop()->getUser();
2240 $status = $page->doUpdateRestrictions(
2241 [ 'edit' => 'sysop' ],
2248 $this->assertTrue( $status->isGood() );
2249 $this->assertInternalType( 'int', $status->getValue() );
2250 $this->assertSelect(
2252 [ 'log_type', 'log_action' ],
2253 [ 'log_id' => $status->getValue() ],
2254 [ [ 'protect', 'protect' ] ]
2257 // Modify the protection
2258 $status = $page->doUpdateRestrictions(
2259 [ 'edit' => 'somethingElse' ],
2266 $this->assertTrue( $status->isGood() );
2267 $this->assertInternalType( 'int', $status->getValue() );
2268 $this->assertSelect(
2270 [ 'log_type', 'log_action' ],
2271 [ 'log_id' => $status->getValue() ],
2272 [ [ 'protect', 'modify' ] ]
2275 // Remove the protection
2276 $status = $page->doUpdateRestrictions(
2284 $this->assertTrue( $status->isGood() );
2285 $this->assertInternalType( 'int', $status->getValue() );
2286 $this->assertSelect(
2288 [ 'log_type', 'log_action' ],
2289 [ 'log_id' => $status->getValue() ],
2290 [ [ 'protect', 'unprotect' ] ]
2295 * @covers WikiPage::newPageUpdater
2296 * @covers WikiPage::getDerivedDataUpdater
2298 public function testNewPageUpdater() {
2299 $user = $this->getTestUser()->getUser();
2300 $page = $this->newPage( __METHOD__
, __METHOD__
);
2302 /** @var Content $content */
2303 $content = $this->getMockBuilder( WikitextContent
::class )
2304 ->setConstructorArgs( [ 'Hello World' ] )
2305 ->setMethods( [ 'getParserOutput' ] )
2307 $content->expects( $this->once() )
2308 ->method( 'getParserOutput' )
2309 ->willReturn( new ParserOutput( 'HTML' ) );
2311 $updater = $page->newPageUpdater( $user );
2312 $updater->setContent( 'main', $content );
2313 $revision = $updater->saveRevision(
2314 CommentStoreComment
::newUnsavedComment( 'test' ),
2318 $this->assertSame( $revision->getId(), $page->getLatest() );
2322 * @covers WikiPage::newPageUpdater
2323 * @covers WikiPage::getDerivedDataUpdater
2325 public function testGetDerivedDataUpdater() {
2326 $admin = $this->getTestSysop()->getUser();
2328 /** @var object $page */
2329 $page = $this->createPage( __METHOD__
, __METHOD__
);
2330 $page = TestingAccessWrapper
::newFromObject( $page );
2332 $revision = $page->getRevision()->getRevisionRecord();
2333 $user = $revision->getUser();
2335 $slotsUpdate = new RevisionSlotsUpdate();
2336 $slotsUpdate->modifyContent( 'main', new WikitextContent( 'Hello World' ) );
2338 // get a virgin updater
2339 $updater1 = $page->getDerivedDataUpdater( $user );
2340 $this->assertFalse( $updater1->isUpdatePrepared() );
2342 $updater1->prepareUpdate( $revision );
2344 // Re-use updater with same revision or content
2345 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, $revision ) );
2347 $slotsUpdate = RevisionSlotsUpdate
::newFromContent(
2348 [ 'main' => $revision->getContent( 'main' ) ]
2350 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, null, $slotsUpdate ) );
2352 // Don't re-use with different user
2353 $updater2a = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2354 $updater2a->prepareContent( $admin, $slotsUpdate, false );
2356 $updater2b = $page->getDerivedDataUpdater( $user, null, $slotsUpdate );
2357 $updater2b->prepareContent( $user, $slotsUpdate, false );
2358 $this->assertNotSame( $updater2a, $updater2b );
2360 // Don't re-use with different content
2361 $updater3 = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2362 $updater3->prepareUpdate( $revision );
2363 $this->assertNotSame( $updater2b, $updater3 );
2365 // Don't re-use if no context given
2366 $updater4 = $page->getDerivedDataUpdater( $admin );
2367 $updater4->prepareUpdate( $revision );
2368 $this->assertNotSame( $updater3, $updater4 );
2370 // Don't re-use if AGAIN no context given
2371 $updater5 = $page->getDerivedDataUpdater( $admin );
2372 $this->assertNotSame( $updater4, $updater5 );
2374 // Don't re-use cached "virgin" unprepared updater
2375 $updater6 = $page->getDerivedDataUpdater( $admin, $revision );
2376 $this->assertNotSame( $updater5, $updater6 );