3 use MediaWiki\Edit\PreparedEdit
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Revision\SlotRecord
;
6 use MediaWiki\Storage\RevisionSlotsUpdate
;
7 use PHPUnit\Framework\MockObject\MockObject
;
8 use Wikimedia\TestingAccessWrapper
;
13 abstract class WikiPageDbTestBase
extends MediaWikiLangTestCase
{
15 private $pagesToDelete;
17 public function __construct( $name = null, array $data = [], $dataName = '' ) {
18 parent
::__construct( $name, $data, $dataName );
20 $this->tablesUsed
= array_merge(
43 protected function addCoreDBData() {
44 // Blank out. This would fail with a modified schema, and we don't need it.
50 abstract protected function getMcrMigrationStage();
55 abstract protected function getMcrTablesToReset();
57 protected function setUp() {
60 $this->tablesUsed +
= $this->getMcrTablesToReset();
62 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
64 'wgMultiContentRevisionSchemaMigrationStage',
65 $this->getMcrMigrationStage()
67 $this->pagesToDelete
= [];
69 $this->overrideMwServices();
72 protected function tearDown() {
73 foreach ( $this->pagesToDelete
as $p ) {
74 /* @var $p WikiPage */
78 $p->doDeleteArticle( "testing done." );
80 } catch ( MWException
$ex ) {
87 abstract protected function getContentHandlerUseDB();
90 * @param Title|string $title
91 * @param string|null $model
94 private function newPage( $title, $model = null ) {
95 if ( is_string( $title ) ) {
96 $ns = $this->getDefaultWikitextNS();
97 $title = Title
::newFromText( $title, $ns );
100 $p = new WikiPage( $title );
102 $this->pagesToDelete
[] = $p;
108 * @param string|Title|WikiPage $page
109 * @param string|Content|Content[] $content
110 * @param int|null $model
114 protected function createPage( $page, $content, $model = null, $user = null ) {
115 if ( is_string( $page ) ||
$page instanceof Title
) {
116 $page = $this->newPage( $page, $model );
120 $user = $this->getTestUser()->getUser();
123 if ( is_string( $content ) ) {
124 $content = ContentHandler
::makeContent( $content, $page->getTitle(), $model );
127 if ( !is_array( $content ) ) {
128 $content = [ 'main' => $content ];
131 $updater = $page->newPageUpdater( $user );
133 foreach ( $content as $role => $cnt ) {
134 $updater->setContent( $role, $cnt );
137 $updater->saveRevision( CommentStoreComment
::newUnsavedComment( "testing" ) );
143 * @covers WikiPage::prepareContentForEdit
145 public function testPrepareContentForEdit() {
146 $user = $this->getTestUser()->getUser();
147 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
149 $page = $this->createPage( __METHOD__
, __METHOD__
, null, $user );
150 $title = $page->getTitle();
152 $content = ContentHandler
::makeContent(
153 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
154 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
156 CONTENT_MODEL_WIKITEXT
158 $content2 = ContentHandler
::makeContent(
159 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
160 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
162 CONTENT_MODEL_WIKITEXT
165 $edit = $page->prepareContentForEdit( $content, null, $user, null, false );
167 $this->assertInstanceOf(
168 ParserOptions
::class,
172 $this->assertContains( '</a>', $edit->output
->getText(), "output" );
173 $this->assertContains(
174 'consetetur sadipscing elitr',
175 $edit->output
->getText(),
179 $this->assertTrue( $content->equals( $edit->newContent
), "newContent field" );
180 $this->assertTrue( $content->equals( $edit->pstContent
), "pstContent field" );
181 $this->assertSame( $edit->output
, $edit->output
, "output field" );
182 $this->assertSame( $edit->popts
, $edit->popts
, "popts field" );
183 $this->assertSame( null, $edit->revid
, "revid field" );
185 // Re-using the prepared info if possible
186 $sameEdit = $page->prepareContentForEdit( $content, null, $user, null, false );
187 $this->assertPreparedEditEquals( $edit, $sameEdit, 'equivalent PreparedEdit' );
188 $this->assertSame( $edit->pstContent
, $sameEdit->pstContent
, 're-use output' );
189 $this->assertSame( $edit->output
, $sameEdit->output
, 're-use output' );
191 // Not re-using the same PreparedEdit if not possible
192 $rev = $page->getRevision();
193 $edit2 = $page->prepareContentForEdit( $content2, null, $user, null, false );
194 $this->assertPreparedEditNotEquals( $edit, $edit2 );
195 $this->assertContains( 'At vero eos', $edit2->pstContent
->serialize(), "content" );
197 // Check pre-safe transform
198 $this->assertContains( '[[gubergren]]', $edit2->pstContent
->serialize() );
199 $this->assertNotContains( '~~~~', $edit2->pstContent
->serialize() );
201 $edit3 = $page->prepareContentForEdit( $content2, null, $sysop, null, false );
202 $this->assertPreparedEditNotEquals( $edit2, $edit3 );
204 // TODO: test with passing revision, then same without revision.
208 * @covers WikiPage::doEditUpdates
210 public function testDoEditUpdates() {
211 $user = $this->getTestUser()->getUser();
213 // NOTE: if site stats get out of whack and drop below 0,
214 // that causes a DB error during tear-down. So bump the
215 // numbers high enough to not drop below 0.
216 $siteStatsUpdate = SiteStatsUpdate
::factory(
217 [ 'edits' => 1000, 'articles' => 1000, 'pages' => 1000 ]
219 $siteStatsUpdate->doUpdate();
221 $page = $this->createPage( __METHOD__
, __METHOD__
);
223 $revision = new Revision(
226 'page' => $page->getId(),
227 'title' => $page->getTitle(),
228 'comment' => __METHOD__
,
229 'minor_edit' => true,
230 'text' => __METHOD__
. ' [[|foo]][[bar]]', // PST turns [[|foo]] into [[foo]]
231 'user' => $user->getId(),
232 'user_text' => $user->getName(),
233 'timestamp' => '20170707040404',
234 'content_model' => CONTENT_MODEL_WIKITEXT
,
235 'content_format' => CONTENT_FORMAT_WIKITEXT
,
239 $page->doEditUpdates( $revision, $user );
241 // TODO: test various options; needs temporary hooks
243 $dbr = wfGetDB( DB_REPLICA
);
244 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $page->getId() ] );
245 $n = $res->numRows();
248 $this->assertEquals( 1, $n, 'pagelinks should contain only one link if PST was not applied' );
252 * @covers WikiPage::doEditContent
253 * @covers WikiPage::prepareContentForEdit
255 public function testDoEditContent() {
256 $this->setMwGlobals( 'wgPageCreationLog', true );
258 $page = $this->newPage( __METHOD__
);
259 $title = $page->getTitle();
261 $user1 = $this->getTestUser()->getUser();
262 // Use the confirmed group for user2 to make sure the user is different
263 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
265 $content = ContentHandler
::makeContent(
266 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
267 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
269 CONTENT_MODEL_WIKITEXT
272 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user1 );
274 $status = $page->doEditContent( $content, "[[testing]] 1", EDIT_NEW
, false, $user1 );
276 $this->assertTrue( $status->isOK(), 'OK' );
277 $this->assertTrue( $status->value
['new'], 'new' );
278 $this->assertNotNull( $status->value
['revision'], 'revision' );
279 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
280 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
281 $this->assertTrue( $status->value
['revision']->getContent()->equals( $content ), 'equals' );
283 $rev = $page->getRevision();
284 $preparedEditAfter = $page->prepareContentForEdit( $content, $rev, $user1 );
286 $this->assertNotNull( $rev->getRecentChange() );
287 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
289 // make sure that cached ParserOutput gets re-used throughout
290 $this->assertSame( $preparedEditBefore->output
, $preparedEditAfter->output
);
292 $id = $page->getId();
294 // Test page creation logging
297 [ 'log_type', 'log_action' ],
298 [ 'log_page' => $id ],
299 [ [ 'create', 'create' ] ]
302 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
303 $this->assertTrue( $id > 0, "WikiPage should have new page id" );
304 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
305 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
307 # ------------------------
308 $dbr = wfGetDB( DB_REPLICA
);
309 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
310 $n = $res->numRows();
313 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
315 # ------------------------
316 $page = new WikiPage( $title );
318 $retrieved = $page->getContent();
319 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
321 # ------------------------
322 $page = new WikiPage( $title );
324 // try null edit, with a different user
325 $status = $page->doEditContent( $content, 'This changes nothing', EDIT_UPDATE
, false, $user2 );
326 $this->assertTrue( $status->isOK(), 'OK' );
327 $this->assertFalse( $status->value
['new'], 'new' );
328 $this->assertNull( $status->value
['revision'], 'revision' );
329 $this->assertNotNull( $page->getRevision() );
330 $this->assertTrue( $page->getRevision()->getContent()->equals( $content ), 'equals' );
332 # ------------------------
333 $content = ContentHandler
::makeContent(
334 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
335 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
337 CONTENT_MODEL_WIKITEXT
340 $status = $page->doEditContent( $content, "testing 2", EDIT_UPDATE
);
341 $this->assertTrue( $status->isOK(), 'OK' );
342 $this->assertFalse( $status->value
['new'], 'new' );
343 $this->assertNotNull( $status->value
['revision'], 'revision' );
344 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
345 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
347 $status->value
['revision']->getContent()->equals( $content ),
348 'not equals (PST must substitute signature)'
351 $rev = $page->getRevision();
352 $this->assertNotNull( $rev->getRecentChange() );
353 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
355 # ------------------------
356 $page = new WikiPage( $title );
358 $retrieved = $page->getContent();
359 $newText = $retrieved->serialize();
360 $this->assertContains( '[[gubergren]]', $newText, 'New text must replace old text.' );
361 $this->assertNotContains( '~~~~', $newText, 'PST must substitute signature.' );
363 # ------------------------
364 $dbr = wfGetDB( DB_REPLICA
);
365 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
366 $n = $res->numRows();
369 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
373 * @covers WikiPage::doEditContent
375 public function testDoEditContent_twice() {
376 $title = Title
::newFromText( __METHOD__
);
377 $page = WikiPage
::factory( $title );
378 $content = ContentHandler
::makeContent( '$1 van $2', $title );
380 // Make sure we can do the exact same save twice.
381 // This tests checks that internal caches are reset as appropriate.
382 $status1 = $page->doEditContent( $content, __METHOD__
);
383 $status2 = $page->doEditContent( $content, __METHOD__
);
385 $this->assertTrue( $status1->isOK(), 'OK' );
386 $this->assertTrue( $status2->isOK(), 'OK' );
388 $this->assertTrue( isset( $status1->value
['revision'] ), 'OK' );
389 $this->assertFalse( isset( $status2->value
['revision'] ), 'OK' );
393 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
394 * TODO: Revision deletion
396 * @covers WikiPage::doDeleteArticle
397 * @covers WikiPage::doDeleteArticleReal
399 public function testDoDeleteArticle() {
400 $page = $this->createPage(
402 "[[original text]] foo",
403 CONTENT_MODEL_WIKITEXT
405 $id = $page->getId();
407 $page->doDeleteArticle( "testing deletion" );
410 $page->getTitle()->getArticleID() > 0,
411 "Title object should now have page id 0"
413 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
416 "WikiPage::exists should return false after page was deleted"
420 "WikiPage::getContent should return null after page was deleted"
423 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
426 "Title::exists should return false after page was deleted"
430 JobQueueGroup
::destroySingletons();
432 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
435 # ------------------------
436 $dbr = wfGetDB( DB_REPLICA
);
437 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
438 $n = $res->numRows();
441 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
445 * @covers WikiPage::doDeleteArticleReal
447 public function testDoDeleteArticleReal_user0() {
448 $page = $this->createPage(
450 "[[original text]] foo",
451 CONTENT_MODEL_WIKITEXT
453 $id = $page->getId();
456 $status = $page->doDeleteArticleReal(
457 /* reason */ "testing user 0 deletion",
458 /* suppress */ false,
461 /* errorStack */ $errorStack,
464 $logId = $status->getValue();
465 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
467 [ 'logging' ] +
$actorQuery['tables'], /* table */
472 'log_user' => $actorQuery['fields']['log_user'],
473 'log_user_text' => $actorQuery['fields']['log_user_text'],
477 [ 'log_id' => $logId ],
481 'testing user 0 deletion',
484 (string)$page->getTitle()->getNamespace(),
485 $page->getTitle()->getDBkey(),
493 * @covers WikiPage::doDeleteArticleReal
495 public function testDoDeleteArticleReal_userSysop() {
496 $page = $this->createPage(
498 "[[original text]] foo",
499 CONTENT_MODEL_WIKITEXT
501 $id = $page->getId();
503 $user = $this->getTestSysop()->getUser();
505 $status = $page->doDeleteArticleReal(
506 /* reason */ "testing sysop deletion",
507 /* suppress */ false,
510 /* errorStack */ $errorStack,
513 $logId = $status->getValue();
514 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
516 [ 'logging' ] +
$actorQuery['tables'], /* table */
521 'log_user' => $actorQuery['fields']['log_user'],
522 'log_user_text' => $actorQuery['fields']['log_user_text'],
526 [ 'log_id' => $logId ],
530 'testing sysop deletion',
531 (string)$user->getId(),
533 (string)$page->getTitle()->getNamespace(),
534 $page->getTitle()->getDBkey(),
542 * TODO: Test more stuff about suppression.
544 * @covers WikiPage::doDeleteArticleReal
546 public function testDoDeleteArticleReal_suppress() {
547 $page = $this->createPage(
549 "[[original text]] foo",
550 CONTENT_MODEL_WIKITEXT
552 $id = $page->getId();
554 $user = $this->getTestSysop()->getUser();
556 $status = $page->doDeleteArticleReal(
557 /* reason */ "testing deletion",
561 /* errorStack */ $errorStack,
564 $logId = $status->getValue();
565 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
567 [ 'logging' ] +
$actorQuery['tables'], /* table */
572 'log_user' => $actorQuery['fields']['log_user'],
573 'log_user_text' => $actorQuery['fields']['log_user_text'],
577 [ 'log_id' => $logId ],
582 (string)$user->getId(),
584 (string)$page->getTitle()->getNamespace(),
585 $page->getTitle()->getDBkey(),
592 $page->getContent( Revision
::FOR_PUBLIC
),
593 "WikiPage::getContent should return null after the page was suppressed for general users"
597 $page->getContent( Revision
::FOR_THIS_USER
, null ),
598 "WikiPage::getContent should return null after the page was suppressed for user zero"
602 $page->getContent( Revision
::FOR_THIS_USER
, $user ),
603 "WikiPage::getContent should return null after the page was suppressed even for a sysop"
608 * @covers WikiPage::doDeleteUpdates
610 public function testDoDeleteUpdates() {
611 $user = $this->getTestUser()->getUser();
612 $page = $this->createPage(
614 "[[original text]] foo",
615 CONTENT_MODEL_WIKITEXT
617 $id = $page->getId();
618 $page->loadPageData(); // make sure the current revision is cached.
620 // Similar to MovePage logic
621 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
622 $page->doDeleteUpdates( $page->getId(), $page->getContent(), $page->getRevision(), $user );
625 JobQueueGroup
::destroySingletons();
627 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
630 # ------------------------
631 $dbr = wfGetDB( DB_REPLICA
);
632 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
633 $n = $res->numRows();
636 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
640 * @param string $name
642 * @return ContentHandler
644 protected function defineMockContentModelForUpdateTesting( $name ) {
645 /** @var ContentHandler|MockObject $handler */
646 $handler = $this->getMockBuilder( TextContentHandler
::class )
647 ->setConstructorArgs( [ $name ] )
649 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
653 $dataUpdate = new MWCallableUpdate( 'time' );
654 $dataUpdate->_name
= "$name data update";
656 $deletionUpdate = new MWCallableUpdate( 'time' );
657 $deletionUpdate->_name
= "$name deletion update";
659 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] );
660 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] );
661 $handler->method( 'unserializeContent' )->willReturnCallback(
662 function ( $text ) use ( $handler ) {
663 return $this->createMockContent( $handler, $text );
667 $this->mergeMwGlobalArrayValue(
668 'wgContentHandlers', [
669 $name => function () use ( $handler ){
679 * @param ContentHandler $handler
680 * @param string $text
684 protected function createMockContent( ContentHandler
$handler, $text ) {
685 /** @var Content|MockObject $content */
686 $content = $this->getMockBuilder( TextContent
::class )
687 ->setConstructorArgs( [ $text ] )
688 ->setMethods( [ 'getModel', 'getContentHandler' ] )
691 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
692 $content->method( 'getContentHandler' )->willReturn( $handler );
697 public function testGetDeletionUpdates() {
698 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
700 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
702 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
703 $page = $this->createPage(
705 [ 'main' => $mainContent1 ]
708 $dataUpdates = $page->getDeletionUpdates( $page->getRevisionRecord() );
709 $this->assertNotEmpty( $dataUpdates );
711 $updateNames = array_map( function ( $du ) {
712 return isset( $du->_name
) ?
$du->_name
: get_class( $du );
715 $this->assertContains( LinksDeletionUpdate
::class, $updateNames );
716 $this->assertContains( 'M1 deletion update', $updateNames );
720 * @covers WikiPage::getRevision
722 public function testGetRevision() {
723 $page = $this->newPage( __METHOD__
);
725 $rev = $page->getRevision();
726 $this->assertNull( $rev );
729 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
731 $rev = $page->getRevision();
733 $this->assertEquals( $page->getLatest(), $rev->getId() );
734 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
738 * @covers WikiPage::getContent
740 public function testGetContent() {
741 $page = $this->newPage( __METHOD__
);
743 $content = $page->getContent();
744 $this->assertNull( $content );
747 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
749 $content = $page->getContent();
750 $this->assertEquals( "some text", $content->getNativeData() );
754 * @covers WikiPage::exists
756 public function testExists() {
757 $page = $this->newPage( __METHOD__
);
758 $this->assertFalse( $page->exists() );
761 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
762 $this->assertTrue( $page->exists() );
764 $page = new WikiPage( $page->getTitle() );
765 $this->assertTrue( $page->exists() );
768 $page->doDeleteArticle( "done testing" );
769 $this->assertFalse( $page->exists() );
771 $page = new WikiPage( $page->getTitle() );
772 $this->assertFalse( $page->exists() );
775 public function provideHasViewableContent() {
777 [ 'WikiPageTest_testHasViewableContent', false, true ],
778 [ 'Special:WikiPageTest_testHasViewableContent', false ],
779 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
780 [ 'Special:Userlogin', true ],
781 [ 'MediaWiki:help', true ],
786 * @dataProvider provideHasViewableContent
787 * @covers WikiPage::hasViewableContent
789 public function testHasViewableContent( $title, $viewable, $create = false ) {
790 $page = $this->newPage( $title );
791 $this->assertEquals( $viewable, $page->hasViewableContent() );
794 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
795 $this->assertTrue( $page->hasViewableContent() );
797 $page = new WikiPage( $page->getTitle() );
798 $this->assertTrue( $page->hasViewableContent() );
802 public function provideGetRedirectTarget() {
804 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
806 'WikiPageTest_testGetRedirectTarget_2',
807 CONTENT_MODEL_WIKITEXT
,
808 "#REDIRECT [[hello world]]",
815 * @dataProvider provideGetRedirectTarget
816 * @covers WikiPage::getRedirectTarget
818 public function testGetRedirectTarget( $title, $model, $text, $target ) {
819 $this->setMwGlobals( [
820 'wgCapitalLinks' => true,
823 $page = $this->createPage( $title, $text, $model );
825 # sanity check, because this test seems to fail for no reason for some people.
826 $c = $page->getContent();
827 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
829 # now, test the actual redirect
830 $t = $page->getRedirectTarget();
831 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
835 * @dataProvider provideGetRedirectTarget
836 * @covers WikiPage::isRedirect
838 public function testIsRedirect( $title, $model, $text, $target ) {
839 $page = $this->createPage( $title, $text, $model );
840 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
843 public function provideIsCountable() {
847 [ 'WikiPageTest_testIsCountable',
848 CONTENT_MODEL_WIKITEXT
,
853 [ 'WikiPageTest_testIsCountable',
854 CONTENT_MODEL_WIKITEXT
,
861 [ 'WikiPageTest_testIsCountable',
862 CONTENT_MODEL_WIKITEXT
,
867 [ 'WikiPageTest_testIsCountable',
868 CONTENT_MODEL_WIKITEXT
,
875 [ 'WikiPageTest_testIsCountable',
876 CONTENT_MODEL_WIKITEXT
,
881 [ 'WikiPageTest_testIsCountable',
882 CONTENT_MODEL_WIKITEXT
,
888 // not a content namespace
889 [ 'Talk:WikiPageTest_testIsCountable',
890 CONTENT_MODEL_WIKITEXT
,
895 [ 'Talk:WikiPageTest_testIsCountable',
896 CONTENT_MODEL_WIKITEXT
,
902 // not a content namespace, different model
903 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
909 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
919 * @dataProvider provideIsCountable
920 * @covers WikiPage::isCountable
922 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
923 global $wgContentHandlerUseDB;
925 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
927 $title = Title
::newFromText( $title );
929 if ( !$wgContentHandlerUseDB
931 && ContentHandler
::getDefaultModelFor( $title ) != $model
933 $this->markTestSkipped( "Can not use non-default content model $model for "
934 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
937 $page = $this->createPage( $title, $text, $model );
939 $editInfo = $page->prepareContentForEdit( $page->getContent() );
941 $v = $page->isCountable();
942 $w = $page->isCountable( $editInfo );
947 "isCountable( null ) returned unexpected value " . var_export( $v, true )
948 . " instead of " . var_export( $expected, true )
949 . " in mode `$mode` for text \"$text\""
955 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
956 . " instead of " . var_export( $expected, true )
957 . " in mode `$mode` for text \"$text\""
961 public function provideGetParserOutput() {
964 CONTENT_MODEL_WIKITEXT
,
966 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
973 * @dataProvider provideGetParserOutput
974 * @covers WikiPage::getParserOutput
976 public function testGetParserOutput( $model, $text, $expectedHtml ) {
977 $page = $this->createPage( __METHOD__
, $text, $model );
979 $opt = $page->makeParserOptions( 'canonical' );
980 $po = $page->getParserOutput( $opt );
981 $text = $po->getText();
983 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
984 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
986 $this->assertEquals( $expectedHtml, $text );
992 * @covers WikiPage::getParserOutput
994 public function testGetParserOutput_nonexisting() {
995 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
997 $opt = new ParserOptions();
998 $po = $page->getParserOutput( $opt );
1000 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
1004 * @covers WikiPage::getParserOutput
1006 public function testGetParserOutput_badrev() {
1007 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
1009 $opt = new ParserOptions();
1010 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
1012 // @todo would be neat to also test deleted revision
1014 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
1017 public static $sections =
1031 public function dataReplaceSection() {
1032 // NOTE: assume the Help namespace to contain wikitext
1034 [ 'Help:WikiPageTest_testReplaceSection',
1035 CONTENT_MODEL_WIKITEXT
,
1040 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
1042 [ 'Help:WikiPageTest_testReplaceSection',
1043 CONTENT_MODEL_WIKITEXT
,
1050 [ 'Help:WikiPageTest_testReplaceSection',
1051 CONTENT_MODEL_WIKITEXT
,
1054 "== TEST ==\nmore fun",
1056 trim( preg_replace( '/^== test ==.*== foo ==/sm',
1057 "== TEST ==\nmore fun\n\n== foo ==",
1060 [ 'Help:WikiPageTest_testReplaceSection',
1061 CONTENT_MODEL_WIKITEXT
,
1066 trim( self
::$sections )
1068 [ 'Help:WikiPageTest_testReplaceSection',
1069 CONTENT_MODEL_WIKITEXT
,
1074 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
1080 * @dataProvider dataReplaceSection
1081 * @covers WikiPage::replaceSectionContent
1083 public function testReplaceSectionContent( $title, $model, $text, $section,
1084 $with, $sectionTitle, $expected
1086 $page = $this->createPage( $title, $text, $model );
1088 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
1089 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
1091 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
1095 * @dataProvider dataReplaceSection
1096 * @covers WikiPage::replaceSectionAtRev
1098 public function testReplaceSectionAtRev( $title, $model, $text, $section,
1099 $with, $sectionTitle, $expected
1101 $page = $this->createPage( $title, $text, $model );
1102 $baseRevId = $page->getLatest();
1104 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
1105 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
1107 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
1111 * @covers WikiPage::getOldestRevision
1113 public function testGetOldestRevision() {
1114 $page = $this->newPage( __METHOD__
);
1115 $page->doEditContent(
1116 new WikitextContent( 'one' ),
1120 $rev1 = $page->getRevision();
1122 $page = new WikiPage( $page->getTitle() );
1123 $page->doEditContent(
1124 new WikitextContent( 'two' ),
1129 $page = new WikiPage( $page->getTitle() );
1130 $page->doEditContent(
1131 new WikitextContent( 'three' ),
1137 $this->assertNotEquals(
1139 $page->getRevision()->getId(),
1140 '$page->getRevision()->getId()'
1144 $this->assertEquals(
1146 $page->getOldestRevision()->getId(),
1147 '$page->getOldestRevision()->getId()'
1152 * @covers WikiPage::doRollback
1153 * @covers WikiPage::commitRollback
1155 public function testDoRollback() {
1156 // FIXME: fails under postgres
1157 $this->markTestSkippedIfDbType( 'postgres' );
1159 $admin = $this->getTestSysop()->getUser();
1160 $user1 = $this->getTestUser()->getUser();
1161 // Use the confirmed group for user2 to make sure the user is different
1162 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
1164 // make sure we can test autopatrolling
1165 $this->setMwGlobals( 'wgUseRCPatrol', true );
1167 // TODO: MCR: test rollback of multiple slots!
1168 $page = $this->newPage( __METHOD__
);
1172 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1173 "section one", EDIT_NEW
, false, $admin );
1176 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1177 "adding section two", 0, false, $user1 );
1179 $text .= "\n\nthree";
1180 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1181 "adding section three", 0, false, $user2 );
1183 /** @var Revision $rev1 */
1184 /** @var Revision $rev2 */
1185 /** @var Revision $rev3 */
1186 $rev1 = $status1->getValue()['revision'];
1187 $rev2 = $status2->getValue()['revision'];
1188 $rev3 = $status3->getValue()['revision'];
1191 * We are having issues with doRollback spuriously failing. Apparently
1192 * the last revision somehow goes missing or not committed under some
1193 * circumstances. So, make sure the revisions have the correct usernames.
1195 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
1196 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
1197 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
1198 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
1200 // Now, try the actual rollback
1201 $token = $admin->getEditToken( 'rollback' );
1202 $rollbackErrors = $page->doRollback(
1211 if ( $rollbackErrors ) {
1213 "Rollback failed:\n" .
1214 print_r( $rollbackErrors, true ) . ";\n" .
1215 print_r( $resultDetails, true )
1219 $page = new WikiPage( $page->getTitle() );
1220 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
1221 "rollback did not revert to the correct revision" );
1222 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
1224 $rc = MediaWikiServices
::getInstance()->getRevisionStore()->getRecentChange(
1225 $page->getRevision()->getRevisionRecord()
1228 $this->assertNotNull( $rc, 'RecentChanges entry' );
1229 $this->assertEquals(
1230 RecentChange
::PRC_AUTOPATROLLED
,
1231 $rc->getAttribute( 'rc_patrolled' ),
1235 // TODO: MCR: assert origin once we write slot data
1236 // $mainSlot = $page->getRevision()->getRevisionRecord()->getSlot( SlotRecord::MAIN );
1237 // $this->assertTrue( $mainSlot->isInherited(), 'isInherited' );
1238 // $this->assertSame( $rev2->getId(), $mainSlot->getOrigin(), 'getOrigin' );
1242 * @covers WikiPage::doRollback
1243 * @covers WikiPage::commitRollback
1245 public function testDoRollbackFailureSameContent() {
1246 $admin = $this->getTestSysop()->getUser();
1249 $page = $this->newPage( __METHOD__
);
1250 $page->doEditContent(
1251 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1257 $rev1 = $page->getRevision();
1259 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
1261 $page = new WikiPage( $page->getTitle() );
1262 $page->doEditContent(
1263 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1264 "adding section two",
1270 # now, do a the rollback from the same user was doing the edit before
1271 $resultDetails = [];
1272 $token = $user1->getEditToken( 'rollback' );
1273 $errors = $page->doRollback(
1275 "testing revert same user",
1282 $this->assertEquals( [], $errors, "Rollback failed same user" );
1284 # now, try the rollback
1285 $resultDetails = [];
1286 $token = $admin->getEditToken( 'rollback' );
1287 $errors = $page->doRollback(
1296 $this->assertEquals(
1306 "Rollback not failed"
1309 $page = new WikiPage( $page->getTitle() );
1310 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1311 "rollback did not revert to the correct revision" );
1312 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1316 * Tests tagging for edits that do rollback action
1317 * @covers WikiPage::doRollback
1319 public function testDoRollbackTagging() {
1320 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
1321 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
1324 $admin = new User();
1325 $admin->setName( 'Administrator' );
1326 $admin->addToDatabase();
1328 $text = 'First line';
1329 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
1330 $page->doEditContent(
1331 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1338 $secondUser = new User();
1339 $secondUser->setName( '92.65.217.32' );
1340 $text .= '\n\nSecond line';
1341 $page = new WikiPage( $page->getTitle() );
1342 $page->doEditContent(
1343 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1344 'Adding second line',
1350 // Now, try the rollback
1351 $admin->addGroup( 'sysop' ); // Make the test user a sysop
1352 $token = $admin->getEditToken( 'rollback' );
1353 $errors = $page->doRollback(
1354 $secondUser->getName(),
1362 // If doRollback completed without errors
1363 if ( $errors === [] ) {
1364 $tags = $resultDetails[ 'tags' ];
1365 $this->assertContains( 'mw-rollback', $tags );
1369 public function provideGetAutoDeleteReason() {
1379 [ "first edit", null ],
1381 "/first edit.*only contributor/",
1387 [ "first edit", null ],
1388 [ "second edit", null ],
1390 "/second edit.*only contributor/",
1396 [ "first edit", "127.0.2.22" ],
1397 [ "second edit", "127.0.3.33" ],
1407 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1408 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1409 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1410 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1411 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1415 '/first edit:.*\.\.\."/',
1421 [ "first edit", "127.0.2.22" ],
1422 [ "", "127.0.3.33" ],
1424 "/before blanking.*first edit/",
1432 * @dataProvider provideGetAutoDeleteReason
1433 * @covers WikiPage::getAutoDeleteReason
1435 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1438 // NOTE: assume Help namespace to contain wikitext
1439 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1443 foreach ( $edits as $edit ) {
1446 if ( !empty( $edit[1] ) ) {
1447 $user->setName( $edit[1] );
1452 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1454 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1459 $reason = $page->getAutoDeleteReason( $hasHistory );
1461 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1462 $this->assertEquals( $expectedResult, $reason );
1464 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1465 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1468 $this->assertEquals( $expectedHistory, $hasHistory,
1469 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1471 $page->doDeleteArticle( "done" );
1474 public function providePreSaveTransform() {
1476 [ 'hello this is ~~~',
1477 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1479 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1480 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1486 * @covers WikiPage::factory
1488 public function testWikiPageFactory() {
1489 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1490 $page = WikiPage
::factory( $title );
1491 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1493 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1494 $page = WikiPage
::factory( $title );
1495 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1497 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1498 $page = WikiPage
::factory( $title );
1499 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1503 * @covers WikiPage::loadPageData
1504 * @covers WikiPage::wasLoadedFrom
1506 public function testLoadPageData() {
1507 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1508 $page = WikiPage
::factory( $title );
1510 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1511 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1512 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1513 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1515 $page->loadPageData( IDBAccessObject
::READ_NORMAL
);
1516 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1517 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1518 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1519 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1521 $page->loadPageData( IDBAccessObject
::READ_LATEST
);
1522 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1523 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1524 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1525 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1527 $page->loadPageData( IDBAccessObject
::READ_LOCKING
);
1528 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1529 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1530 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1531 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1533 $page->loadPageData( IDBAccessObject
::READ_EXCLUSIVE
);
1534 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1535 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1536 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1537 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1541 * @dataProvider provideCommentMigrationOnDeletion
1543 * @param int $writeStage
1544 * @param int $readStage
1546 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1547 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1548 $this->overrideMwServices();
1550 $dbr = wfGetDB( DB_REPLICA
);
1552 $page = $this->createPage(
1555 CONTENT_MODEL_WIKITEXT
1557 $revid = $page->getLatest();
1558 if ( $writeStage > MIGRATION_OLD
) {
1559 $comment_id = $dbr->selectField(
1560 'revision_comment_temp',
1561 'revcomment_comment_id',
1562 [ 'revcomment_rev' => $revid ],
1567 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1568 $this->overrideMwServices();
1570 $page->doDeleteArticle( "testing deletion" );
1572 if ( $readStage > MIGRATION_OLD
) {
1573 // Didn't leave behind any 'revision_comment_temp' rows
1574 $n = $dbr->selectField(
1575 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1577 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1579 // Copied or upgraded the comment_id, as applicable
1580 $ar_comment_id = $dbr->selectField(
1583 [ 'ar_rev_id' => $revid ],
1586 if ( $writeStage > MIGRATION_OLD
) {
1587 $this->assertSame( $comment_id, $ar_comment_id );
1589 $this->assertNotEquals( 0, $ar_comment_id );
1593 // Copied rev_comment, if applicable
1594 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1595 $ar_comment = $dbr->selectField(
1598 [ 'ar_rev_id' => $revid ],
1601 $this->assertSame( 'testing', $ar_comment );
1605 public function provideCommentMigrationOnDeletion() {
1607 [ MIGRATION_OLD
, MIGRATION_OLD
],
1608 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1609 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1610 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1611 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1612 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1613 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1614 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1615 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1616 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1617 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1618 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1619 [ MIGRATION_NEW
, MIGRATION_NEW
],
1624 * @covers WikiPage::updateCategoryCounts
1626 public function testUpdateCategoryCounts() {
1627 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1629 // Add an initial category
1630 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1632 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1633 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1634 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1636 // Add a new category
1637 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1639 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1640 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1641 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1643 // Add and remove a category
1644 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1646 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1647 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1648 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1651 public function provideUpdateRedirectOn() {
1652 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1653 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1654 yield
[ 'SomeText', false, null, false, true, 0 ];
1655 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1659 * @dataProvider provideUpdateRedirectOn
1660 * @covers WikiPage::updateRedirectOn
1662 * @param string $initialText
1663 * @param bool $initialRedirectState
1664 * @param string|null $redirectTitle
1665 * @param bool|null $lastRevIsRedirect
1666 * @param bool $expectedSuccess
1667 * @param int $expectedRowCount
1669 public function testUpdateRedirectOn(
1671 $initialRedirectState,
1677 // FIXME: fails under sqlite and postgres
1678 $this->markTestSkippedIfDbType( 'sqlite' );
1679 $this->markTestSkippedIfDbType( 'postgres' );
1680 static $pageCounter = 0;
1683 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1684 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1686 $redirectTitle = is_string( $redirectTitle )
1687 ? Title
::newFromText( $redirectTitle )
1690 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1691 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1693 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1694 * Most of core checks the page table for redirect status, so we have to be ugly and
1695 * assert a select from the table here.
1697 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1700 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1701 $this->assertSelect(
1704 [ 'rd_from' => $pageId ],
1705 [ [ strval( $expected ) ] ]
1710 * @covers WikiPage::insertRedirectEntry
1712 public function testInsertRedirectEntry_insertsRedirectEntry() {
1713 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1714 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1716 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1717 $targetTitle->mInterwiki
= 'eninter';
1718 $page->insertRedirectEntry( $targetTitle, null );
1720 $this->assertSelect(
1722 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1723 [ 'rd_from' => $page->getId() ],
1725 strval( $page->getId() ),
1726 strval( $targetTitle->getNamespace() ),
1727 strval( $targetTitle->getDBkey() ),
1728 strval( $targetTitle->getFragment() ),
1729 strval( $targetTitle->getInterwiki() ),
1735 * @covers WikiPage::insertRedirectEntry
1737 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1738 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1739 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1741 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1742 $targetTitle->mInterwiki
= 'eninter';
1743 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1745 $this->assertSelect(
1747 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1748 [ 'rd_from' => $page->getId() ],
1750 strval( $page->getId() ),
1751 strval( $targetTitle->getNamespace() ),
1752 strval( $targetTitle->getDBkey() ),
1753 strval( $targetTitle->getFragment() ),
1754 strval( $targetTitle->getInterwiki() ),
1760 * @covers WikiPage::insertRedirectEntry
1762 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1763 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1764 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1766 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1767 $targetTitle->mInterwiki
= 'eninter';
1768 $page->insertRedirectEntry( $targetTitle, 215251 );
1770 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1773 private function getRow( array $overrides = [] ) {
1777 'page_is_redirect' => '1',
1778 'page_latest' => '99',
1779 'page_namespace' => '3',
1780 'page_title' => 'JaJaTitle',
1781 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1782 'page_touched' => '20120101020202',
1783 'page_links_updated' => '20140101020202',
1785 foreach ( $overrides as $key => $value ) {
1786 $row[$key] = $value;
1788 return (object)$row;
1791 public function provideNewFromRowSuccess() {
1792 yield
'basic row' => [
1794 function ( WikiPage
$wikiPage, self
$test ) {
1795 $test->assertSame( 44, $wikiPage->getId() );
1796 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1797 $test->assertTrue( $wikiPage->isRedirect() );
1798 $test->assertSame( 99, $wikiPage->getLatest() );
1799 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1800 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1803 'edit' => [ 'autoconfirmed', 'sysop' ],
1804 'move' => [ 'sysop' ],
1806 $wikiPage->getTitle()->getAllRestrictions()
1808 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1809 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1812 yield
'different timestamp formats' => [
1814 'page_touched' => '2012-01-01 02:02:02',
1815 'page_links_updated' => '2014-01-01 02:02:02',
1817 function ( WikiPage
$wikiPage, self
$test ) {
1818 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1819 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1822 yield
'no restrictions' => [
1824 'page_restrictions' => '',
1826 function ( WikiPage
$wikiPage, self
$test ) {
1832 $wikiPage->getTitle()->getAllRestrictions()
1836 yield
'not redirect' => [
1838 'page_is_redirect' => '0',
1840 function ( WikiPage
$wikiPage, self
$test ) {
1841 $test->assertFalse( $wikiPage->isRedirect() );
1847 * @covers WikiPage::newFromRow
1848 * @covers WikiPage::loadFromRow
1849 * @dataProvider provideNewFromRowSuccess
1851 * @param object $row
1852 * @param callable $assertions
1854 public function testNewFromRow( $row, $assertions ) {
1855 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1856 $assertions( $page, $this );
1859 public function provideTestNewFromId_returnsNullOnBadPageId() {
1865 * @covers WikiPage::newFromID
1866 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1868 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1869 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1873 * @covers WikiPage::newFromID
1875 public function testNewFromId_appearsToFetchCorrectRow() {
1876 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1877 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1878 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1879 $this->assertEquals(
1880 $createdPage->getContent()->getNativeData(),
1881 $fetchedPage->getContent()->getNativeData()
1886 * @covers WikiPage::newFromID
1888 public function testNewFromId_returnsNullOnNonExistingId() {
1889 $this->assertNull( WikiPage
::newFromID( 2147483647 ) );
1892 public function provideTestInsertProtectNullRevision() {
1893 // phpcs:disable Generic.Files.LineLength
1896 [ 'edit' => 'sysop' ],
1897 [ 'edit' => '20200101040404' ],
1901 '(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)))'
1905 [ 'edit' => 'sysop', 'move' => 'something' ],
1906 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1910 '(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)))'
1916 * @dataProvider provideTestInsertProtectNullRevision
1917 * @covers WikiPage::insertProtectNullRevision
1918 * @covers WikiPage::protectDescription
1920 * @param string $revCommentMsg
1921 * @param array $limit
1922 * @param array $expiry
1923 * @param bool $cascade
1924 * @param string $reason
1925 * @param bool|null $user true if the test sysop should be used, or null
1926 * @param string $expectedComment
1928 public function testInsertProtectNullRevision(
1937 $this->setContentLang( 'qqx' );
1939 $page = $this->createPage( __METHOD__
, 'Goat' );
1941 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1943 $result = $page->insertProtectNullRevision(
1952 $this->assertTrue( $result instanceof Revision
);
1953 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1957 * @covers WikiPage::updateRevisionOn
1959 public function testUpdateRevisionOn_existingPage() {
1960 $user = $this->getTestSysop()->getUser();
1961 $page = $this->createPage( __METHOD__
, 'StartText' );
1963 $revision = new Revision(
1966 'page' => $page->getId(),
1967 'title' => $page->getTitle(),
1968 'comment' => __METHOD__
,
1969 'minor_edit' => true,
1970 'text' => __METHOD__
. '-text',
1971 'len' => strlen( __METHOD__
. '-text' ),
1972 'user' => $user->getId(),
1973 'user_text' => $user->getName(),
1974 'timestamp' => '20170707040404',
1975 'content_model' => CONTENT_MODEL_WIKITEXT
,
1976 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1980 $result = $page->updateRevisionOn( $this->db
, $revision );
1981 $this->assertTrue( $result );
1982 $this->assertSame( 9989, $page->getLatest() );
1983 $this->assertEquals( $revision, $page->getRevision() );
1987 * @covers WikiPage::updateRevisionOn
1989 public function testUpdateRevisionOn_NonExistingPage() {
1990 $user = $this->getTestSysop()->getUser();
1991 $page = $this->createPage( __METHOD__
, 'StartText' );
1992 $page->doDeleteArticle( 'reason' );
1994 $revision = new Revision(
1997 'page' => $page->getId(),
1998 'title' => $page->getTitle(),
1999 'comment' => __METHOD__
,
2000 'minor_edit' => true,
2001 'text' => __METHOD__
. '-text',
2002 'len' => strlen( __METHOD__
. '-text' ),
2003 'user' => $user->getId(),
2004 'user_text' => $user->getName(),
2005 'timestamp' => '20170707040404',
2006 'content_model' => CONTENT_MODEL_WIKITEXT
,
2007 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2011 $result = $page->updateRevisionOn( $this->db
, $revision );
2012 $this->assertFalse( $result );
2016 * @covers WikiPage::updateIfNewerOn
2018 public function testUpdateIfNewerOn_olderRevision() {
2019 $user = $this->getTestSysop()->getUser();
2020 $page = $this->createPage( __METHOD__
, 'StartText' );
2021 $initialRevision = $page->getRevision();
2023 $olderTimeStamp = wfTimestamp(
2025 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
2028 $olderRevison = new Revision(
2031 'page' => $page->getId(),
2032 'title' => $page->getTitle(),
2033 'comment' => __METHOD__
,
2034 'minor_edit' => true,
2035 'text' => __METHOD__
. '-text',
2036 'len' => strlen( __METHOD__
. '-text' ),
2037 'user' => $user->getId(),
2038 'user_text' => $user->getName(),
2039 'timestamp' => $olderTimeStamp,
2040 'content_model' => CONTENT_MODEL_WIKITEXT
,
2041 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2045 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
2046 $this->assertFalse( $result );
2050 * @covers WikiPage::updateIfNewerOn
2052 public function testUpdateIfNewerOn_newerRevision() {
2053 $user = $this->getTestSysop()->getUser();
2054 $page = $this->createPage( __METHOD__
, 'StartText' );
2055 $initialRevision = $page->getRevision();
2057 $newerTimeStamp = wfTimestamp(
2059 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
2062 $newerRevision = new Revision(
2065 'page' => $page->getId(),
2066 'title' => $page->getTitle(),
2067 'comment' => __METHOD__
,
2068 'minor_edit' => true,
2069 'text' => __METHOD__
. '-text',
2070 'len' => strlen( __METHOD__
. '-text' ),
2071 'user' => $user->getId(),
2072 'user_text' => $user->getName(),
2073 'timestamp' => $newerTimeStamp,
2074 'content_model' => CONTENT_MODEL_WIKITEXT
,
2075 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2078 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
2079 $this->assertTrue( $result );
2083 * @covers WikiPage::insertOn
2085 public function testInsertOn() {
2086 $title = Title
::newFromText( __METHOD__
);
2087 $page = new WikiPage( $title );
2089 $startTimeStamp = wfTimestampNow();
2090 $result = $page->insertOn( $this->db
);
2091 $endTimeStamp = wfTimestampNow();
2093 $this->assertInternalType( 'int', $result );
2094 $this->assertTrue( $result > 0 );
2096 $condition = [ 'page_id' => $result ];
2098 // Check the default fields have been filled
2099 $this->assertSelect(
2104 'page_restrictions',
2122 // Check the page_random field has been filled
2123 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
2124 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
2126 // Assert the touched timestamp in the DB is roughly when we inserted the page
2127 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
2129 wfTimestamp( TS_UNIX
, $startTimeStamp )
2130 <= wfTimestamp( TS_UNIX
, $pageTouched )
2133 wfTimestamp( TS_UNIX
, $endTimeStamp )
2134 >= wfTimestamp( TS_UNIX
, $pageTouched )
2137 // Try inserting the same page again and checking the result is false (no change)
2138 $result = $page->insertOn( $this->db
);
2139 $this->assertFalse( $result );
2143 * @covers WikiPage::insertOn
2145 public function testInsertOn_idSpecified() {
2146 $title = Title
::newFromText( __METHOD__
);
2147 $page = new WikiPage( $title );
2150 $result = $page->insertOn( $this->db
, $id );
2152 $this->assertSame( $id, $result );
2154 $condition = [ 'page_id' => $result ];
2156 // Check there is actually a row in the db
2157 $this->assertSelect(
2165 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
2166 // Note: Once the current dates passes the date in these tests they will fail.
2167 yield
'move something' => [
2169 [ 'move' => 'something' ],
2171 [ 'edit' => [], 'move' => [ 'something' ] ],
2174 yield
'move something, edit blank' => [
2176 [ 'move' => 'something', 'edit' => '' ],
2178 [ 'edit' => [], 'move' => [ 'something' ] ],
2181 yield
'edit sysop, with expiry' => [
2183 [ 'edit' => 'sysop' ],
2184 [ 'edit' => '21330101020202' ],
2185 [ 'edit' => [ 'sysop' ], 'move' => [] ],
2186 [ 'edit' => '21330101020202' ],
2188 yield
'move and edit, move with expiry' => [
2190 [ 'move' => 'something', 'edit' => 'another' ],
2191 [ 'move' => '22220202010101' ],
2192 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2193 [ 'move' => '22220202010101' ],
2195 yield
'move and edit, edit with infinity expiry' => [
2197 [ 'move' => 'something', 'edit' => 'another' ],
2198 [ 'edit' => 'infinity' ],
2199 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2200 [ 'edit' => 'infinity' ],
2202 yield
'non existing, create something' => [
2204 [ 'create' => 'something' ],
2206 [ 'create' => [ 'something' ] ],
2209 yield
'non existing, create something with expiry' => [
2211 [ 'create' => 'something' ],
2212 [ 'create' => '23451212112233' ],
2213 [ 'create' => [ 'something' ] ],
2214 [ 'create' => '23451212112233' ],
2219 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
2220 * @covers WikiPage::doUpdateRestrictions
2222 public function testDoUpdateRestrictions_setBasicRestrictions(
2226 array $expectedRestrictions,
2227 array $expectedRestrictionExpiries
2229 if ( $pageExists ) {
2230 $page = $this->createPage( __METHOD__
, 'ABC' );
2232 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
2234 $user = $this->getTestSysop()->getUser();
2237 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
2239 $logId = $status->getValue();
2240 $allRestrictions = $page->getTitle()->getAllRestrictions();
2242 $this->assertTrue( $status->isGood() );
2243 $this->assertInternalType( 'int', $logId );
2244 $this->assertSame( $expectedRestrictions, $allRestrictions );
2245 foreach ( $expectedRestrictionExpiries as $key => $value ) {
2246 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
2249 // Make sure the log entry looks good
2250 // log_params is not checked here
2251 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
2252 $this->assertSelect(
2253 [ 'logging' ] +
$actorQuery['tables'],
2256 'log_user' => $actorQuery['fields']['log_user'],
2257 'log_user_text' => $actorQuery['fields']['log_user_text'],
2261 [ 'log_id' => $logId ],
2264 (string)$user->getId(),
2266 (string)$page->getTitle()->getNamespace(),
2267 $page->getTitle()->getDBkey(),
2270 $actorQuery['joins']
2275 * @covers WikiPage::doUpdateRestrictions
2277 public function testDoUpdateRestrictions_failsOnReadOnly() {
2278 $page = $this->createPage( __METHOD__
, 'ABC' );
2279 $user = $this->getTestSysop()->getUser();
2283 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
2284 ->disableOriginalConstructor()
2285 ->setMethods( [ 'isReadOnly', 'getReason' ] )
2287 $readOnly->expects( $this->once() )
2288 ->method( 'isReadOnly' )
2289 ->will( $this->returnValue( true ) );
2290 $readOnly->expects( $this->once() )
2291 ->method( 'getReason' )
2292 ->will( $this->returnValue( 'Some Read Only Reason' ) );
2293 $this->setService( 'ReadOnlyMode', $readOnly );
2295 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
2296 $this->assertFalse( $status->isOK() );
2297 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
2301 * @covers WikiPage::doUpdateRestrictions
2303 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
2304 $page = $this->createPage( __METHOD__
, 'ABC' );
2305 $user = $this->getTestSysop()->getUser();
2307 $limit = [ 'edit' => 'sysop' ];
2309 $status = $page->doUpdateRestrictions(
2318 // The first entry should have a logId as it did something
2319 $this->assertTrue( $status->isGood() );
2320 $this->assertInternalType( 'int', $status->getValue() );
2322 $status = $page->doUpdateRestrictions(
2331 // The second entry should not have a logId as nothing changed
2332 $this->assertTrue( $status->isGood() );
2333 $this->assertNull( $status->getValue() );
2337 * @covers WikiPage::doUpdateRestrictions
2339 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
2340 $page = $this->createPage( __METHOD__
, 'ABC' );
2341 $user = $this->getTestSysop()->getUser();
2345 $status = $page->doUpdateRestrictions(
2346 [ 'edit' => 'sysop' ],
2353 $this->assertTrue( $status->isGood() );
2354 $this->assertInternalType( 'int', $status->getValue() );
2355 $this->assertSelect(
2357 [ 'log_type', 'log_action' ],
2358 [ 'log_id' => $status->getValue() ],
2359 [ [ 'protect', 'protect' ] ]
2362 // Modify the protection
2363 $status = $page->doUpdateRestrictions(
2364 [ 'edit' => 'somethingElse' ],
2371 $this->assertTrue( $status->isGood() );
2372 $this->assertInternalType( 'int', $status->getValue() );
2373 $this->assertSelect(
2375 [ 'log_type', 'log_action' ],
2376 [ 'log_id' => $status->getValue() ],
2377 [ [ 'protect', 'modify' ] ]
2380 // Remove the protection
2381 $status = $page->doUpdateRestrictions(
2389 $this->assertTrue( $status->isGood() );
2390 $this->assertInternalType( 'int', $status->getValue() );
2391 $this->assertSelect(
2393 [ 'log_type', 'log_action' ],
2394 [ 'log_id' => $status->getValue() ],
2395 [ [ 'protect', 'unprotect' ] ]
2400 * @covers WikiPage::newPageUpdater
2401 * @covers WikiPage::getDerivedDataUpdater
2403 public function testNewPageUpdater() {
2404 $user = $this->getTestUser()->getUser();
2405 $page = $this->newPage( __METHOD__
, __METHOD__
);
2407 /** @var Content $content */
2408 $content = $this->getMockBuilder( WikitextContent
::class )
2409 ->setConstructorArgs( [ 'Hello World' ] )
2410 ->setMethods( [ 'getParserOutput' ] )
2412 $content->expects( $this->once() )
2413 ->method( 'getParserOutput' )
2414 ->willReturn( new ParserOutput( 'HTML' ) );
2416 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user );
2418 // provide context, so the cache can be kept in place
2419 $slotsUpdate = new revisionSlotsUpdate();
2420 $slotsUpdate->modifyContent( SlotRecord
::MAIN
, $content );
2422 $updater = $page->newPageUpdater( $user, $slotsUpdate );
2423 $updater->setContent( SlotRecord
::MAIN
, $content );
2424 $revision = $updater->saveRevision(
2425 CommentStoreComment
::newUnsavedComment( 'test' ),
2429 $preparedEditAfter = $page->prepareContentForEdit( $content, $revision, $user );
2431 $this->assertSame( $revision->getId(), $page->getLatest() );
2433 // Parsed output must remain cached throughout.
2434 $this->assertSame( $preparedEditBefore->output
, $preparedEditAfter->output
);
2438 * @covers WikiPage::newPageUpdater
2439 * @covers WikiPage::getDerivedDataUpdater
2441 public function testGetDerivedDataUpdater() {
2442 $admin = $this->getTestSysop()->getUser();
2444 /** @var object $page */
2445 $page = $this->createPage( __METHOD__
, __METHOD__
);
2446 $page = TestingAccessWrapper
::newFromObject( $page );
2448 $revision = $page->getRevision()->getRevisionRecord();
2449 $user = $revision->getUser();
2451 $slotsUpdate = new RevisionSlotsUpdate();
2452 $slotsUpdate->modifyContent( SlotRecord
::MAIN
, new WikitextContent( 'Hello World' ) );
2454 // get a virgin updater
2455 $updater1 = $page->getDerivedDataUpdater( $user );
2456 $this->assertFalse( $updater1->isUpdatePrepared() );
2458 $updater1->prepareUpdate( $revision );
2460 // Re-use updater with same revision or content, even if base changed
2461 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, $revision ) );
2463 $slotsUpdate = RevisionSlotsUpdate
::newFromContent(
2464 [ SlotRecord
::MAIN
=> $revision->getContent( SlotRecord
::MAIN
) ]
2466 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, null, $slotsUpdate ) );
2468 // Don't re-use for edit if base revision ID changed
2469 $this->assertNotSame(
2471 $page->getDerivedDataUpdater( $user, null, $slotsUpdate, true )
2474 // Don't re-use with different user
2475 $updater2a = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2476 $updater2a->prepareContent( $admin, $slotsUpdate, false );
2478 $updater2b = $page->getDerivedDataUpdater( $user, null, $slotsUpdate );
2479 $updater2b->prepareContent( $user, $slotsUpdate, false );
2480 $this->assertNotSame( $updater2a, $updater2b );
2482 // Don't re-use with different content
2483 $updater3 = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2484 $updater3->prepareUpdate( $revision );
2485 $this->assertNotSame( $updater2b, $updater3 );
2487 // Don't re-use if no context given
2488 $updater4 = $page->getDerivedDataUpdater( $admin );
2489 $updater4->prepareUpdate( $revision );
2490 $this->assertNotSame( $updater3, $updater4 );
2492 // Don't re-use if AGAIN no context given
2493 $updater5 = $page->getDerivedDataUpdater( $admin );
2494 $this->assertNotSame( $updater4, $updater5 );
2496 // Don't re-use cached "virgin" unprepared updater
2497 $updater6 = $page->getDerivedDataUpdater( $admin, $revision );
2498 $this->assertNotSame( $updater5, $updater6 );
2501 protected function assertPreparedEditEquals(
2502 PreparedEdit
$edit, PreparedEdit
$edit2, $message = ''
2504 // suppress differences caused by a clock tick between generating the two PreparedEdits
2505 if ( abs( $edit->timestamp
- $edit2->timestamp
) < 3 ) {
2506 $edit2 = clone $edit2;
2507 $edit2->timestamp
= $edit->timestamp
;
2509 $this->assertEquals( $edit, $edit2, $message );
2512 protected function assertPreparedEditNotEquals(
2513 PreparedEdit
$edit, PreparedEdit
$edit2, $message = ''
2515 if ( abs( $edit->timestamp
- $edit2->timestamp
) < 3 ) {
2516 $edit2 = clone $edit2;
2517 $edit2->timestamp
= $edit->timestamp
;
2519 $this->assertNotEquals( $edit, $edit2, $message );