3 use MediaWiki\Edit\PreparedEdit
;
4 use MediaWiki\MediaWikiServices
;
5 use MediaWiki\Storage\RevisionSlotsUpdate
;
6 use PHPUnit\Framework\MockObject\MockObject
;
7 use Wikimedia\TestingAccessWrapper
;
12 abstract class WikiPageDbTestBase
extends MediaWikiLangTestCase
{
14 private $pagesToDelete;
16 public function __construct( $name = null, array $data = [], $dataName = '' ) {
17 parent
::__construct( $name, $data, $dataName );
19 $this->tablesUsed
= array_merge(
42 protected function addCoreDBData() {
43 // Blank out. This would fail with a modified schema, and we don't need it.
49 abstract protected function getMcrMigrationStage();
54 abstract protected function getMcrTablesToReset();
56 protected function setUp() {
59 $this->tablesUsed +
= $this->getMcrTablesToReset();
61 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
63 'wgMultiContentRevisionSchemaMigrationStage',
64 $this->getMcrMigrationStage()
66 $this->pagesToDelete
= [];
68 $this->overrideMwServices();
71 protected function tearDown() {
72 foreach ( $this->pagesToDelete
as $p ) {
73 /* @var $p WikiPage */
77 $p->doDeleteArticle( "testing done." );
79 } catch ( MWException
$ex ) {
86 abstract protected function getContentHandlerUseDB();
89 * @param Title|string $title
90 * @param string|null $model
93 private function newPage( $title, $model = null ) {
94 if ( is_string( $title ) ) {
95 $ns = $this->getDefaultWikitextNS();
96 $title = Title
::newFromText( $title, $ns );
99 $p = new WikiPage( $title );
101 $this->pagesToDelete
[] = $p;
107 * @param string|Title|WikiPage $page
108 * @param string|Content|Content[] $content
109 * @param int|null $model
113 protected function createPage( $page, $content, $model = null, $user = null ) {
114 if ( is_string( $page ) ||
$page instanceof Title
) {
115 $page = $this->newPage( $page, $model );
119 $user = $this->getTestUser()->getUser();
122 if ( is_string( $content ) ) {
123 $content = ContentHandler
::makeContent( $content, $page->getTitle(), $model );
126 if ( !is_array( $content ) ) {
127 $content = [ 'main' => $content ];
130 $updater = $page->newPageUpdater( $user );
132 foreach ( $content as $role => $cnt ) {
133 $updater->setContent( $role, $cnt );
136 $updater->saveRevision( CommentStoreComment
::newUnsavedComment( "testing" ) );
142 * @covers WikiPage::prepareContentForEdit
144 public function testPrepareContentForEdit() {
145 $user = $this->getTestUser()->getUser();
146 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
148 $page = $this->createPage( __METHOD__
, __METHOD__
, null, $user );
149 $title = $page->getTitle();
151 $content = ContentHandler
::makeContent(
152 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
153 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
155 CONTENT_MODEL_WIKITEXT
157 $content2 = ContentHandler
::makeContent(
158 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
159 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
161 CONTENT_MODEL_WIKITEXT
164 $edit = $page->prepareContentForEdit( $content, null, $user, null, false );
166 $this->assertInstanceOf(
167 ParserOptions
::class,
171 $this->assertContains( '</a>', $edit->output
->getText(), "output" );
172 $this->assertContains(
173 'consetetur sadipscing elitr',
174 $edit->output
->getText(),
178 $this->assertTrue( $content->equals( $edit->newContent
), "newContent field" );
179 $this->assertTrue( $content->equals( $edit->pstContent
), "pstContent field" );
180 $this->assertSame( $edit->output
, $edit->output
, "output field" );
181 $this->assertSame( $edit->popts
, $edit->popts
, "popts field" );
182 $this->assertSame( null, $edit->revid
, "revid field" );
184 // Re-using the prepared info if possible
185 $sameEdit = $page->prepareContentForEdit( $content, null, $user, null, false );
186 $this->assertPreparedEditEquals( $edit, $sameEdit, 'equivalent PreparedEdit' );
187 $this->assertSame( $edit->pstContent
, $sameEdit->pstContent
, 're-use output' );
188 $this->assertSame( $edit->output
, $sameEdit->output
, 're-use output' );
190 // Not re-using the same PreparedEdit if not possible
191 $rev = $page->getRevision();
192 $edit2 = $page->prepareContentForEdit( $content2, null, $user, null, false );
193 $this->assertPreparedEditNotEquals( $edit, $edit2 );
194 $this->assertContains( 'At vero eos', $edit2->pstContent
->serialize(), "content" );
196 // Check pre-safe transform
197 $this->assertContains( '[[gubergren]]', $edit2->pstContent
->serialize() );
198 $this->assertNotContains( '~~~~', $edit2->pstContent
->serialize() );
200 $edit3 = $page->prepareContentForEdit( $content2, null, $sysop, null, false );
201 $this->assertPreparedEditNotEquals( $edit2, $edit3 );
203 // TODO: test with passing revision, then same without revision.
207 * @covers WikiPage::doEditUpdates
209 public function testDoEditUpdates() {
210 $user = $this->getTestUser()->getUser();
212 // NOTE: if site stats get out of whack and drop below 0,
213 // that causes a DB error during tear-down. So bump the
214 // numbers high enough to not drop below 0.
215 $siteStatsUpdate = SiteStatsUpdate
::factory(
216 [ 'edits' => 1000, 'articles' => 1000, 'pages' => 1000 ]
218 $siteStatsUpdate->doUpdate();
220 $page = $this->createPage( __METHOD__
, __METHOD__
);
222 $revision = new Revision(
225 'page' => $page->getId(),
226 'title' => $page->getTitle(),
227 'comment' => __METHOD__
,
228 'minor_edit' => true,
229 'text' => __METHOD__
. ' [[|foo]][[bar]]', // PST turns [[|foo]] into [[foo]]
230 'user' => $user->getId(),
231 'user_text' => $user->getName(),
232 'timestamp' => '20170707040404',
233 'content_model' => CONTENT_MODEL_WIKITEXT
,
234 'content_format' => CONTENT_FORMAT_WIKITEXT
,
238 $page->doEditUpdates( $revision, $user );
240 // TODO: test various options; needs temporary hooks
242 $dbr = wfGetDB( DB_REPLICA
);
243 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $page->getId() ] );
244 $n = $res->numRows();
247 $this->assertEquals( 1, $n, 'pagelinks should contain only one link if PST was not applied' );
251 * @covers WikiPage::doEditContent
252 * @covers WikiPage::prepareContentForEdit
254 public function testDoEditContent() {
255 $this->setMwGlobals( 'wgPageCreationLog', true );
257 $page = $this->newPage( __METHOD__
);
258 $title = $page->getTitle();
260 $user1 = $this->getTestUser()->getUser();
261 // Use the confirmed group for user2 to make sure the user is different
262 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
264 $content = ContentHandler
::makeContent(
265 "[[Lorem ipsum]] dolor sit amet, consetetur sadipscing elitr, sed diam "
266 . " nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
268 CONTENT_MODEL_WIKITEXT
271 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user1 );
273 $status = $page->doEditContent( $content, "[[testing]] 1", EDIT_NEW
, false, $user1 );
275 $this->assertTrue( $status->isOK(), 'OK' );
276 $this->assertTrue( $status->value
['new'], 'new' );
277 $this->assertNotNull( $status->value
['revision'], 'revision' );
278 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
279 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
280 $this->assertTrue( $status->value
['revision']->getContent()->equals( $content ), 'equals' );
282 $rev = $page->getRevision();
283 $preparedEditAfter = $page->prepareContentForEdit( $content, $rev, $user1 );
285 $this->assertNotNull( $rev->getRecentChange() );
286 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
288 // make sure that cached ParserOutput gets re-used throughout
289 $this->assertSame( $preparedEditBefore->output
, $preparedEditAfter->output
);
291 $id = $page->getId();
293 // Test page creation logging
296 [ 'log_type', 'log_action' ],
297 [ 'log_page' => $id ],
298 [ [ 'create', 'create' ] ]
301 $this->assertTrue( $title->getArticleID() > 0, "Title object should have new page id" );
302 $this->assertTrue( $id > 0, "WikiPage should have new page id" );
303 $this->assertTrue( $title->exists(), "Title object should indicate that the page now exists" );
304 $this->assertTrue( $page->exists(), "WikiPage object should indicate that the page now exists" );
306 # ------------------------
307 $dbr = wfGetDB( DB_REPLICA
);
308 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
309 $n = $res->numRows();
312 $this->assertEquals( 1, $n, 'pagelinks should contain one link from the page' );
314 # ------------------------
315 $page = new WikiPage( $title );
317 $retrieved = $page->getContent();
318 $this->assertTrue( $content->equals( $retrieved ), 'retrieved content doesn\'t equal original' );
320 # ------------------------
321 $page = new WikiPage( $title );
323 // try null edit, with a different user
324 $status = $page->doEditContent( $content, 'This changes nothing', EDIT_UPDATE
, false, $user2 );
325 $this->assertTrue( $status->isOK(), 'OK' );
326 $this->assertFalse( $status->value
['new'], 'new' );
327 $this->assertNull( $status->value
['revision'], 'revision' );
328 $this->assertNotNull( $page->getRevision() );
329 $this->assertTrue( $page->getRevision()->getContent()->equals( $content ), 'equals' );
331 # ------------------------
332 $content = ContentHandler
::makeContent(
333 "At vero eos et accusam et justo duo [[dolores]] et ea rebum. "
334 . "Stet clita kasd [[gubergren]], no sea takimata sanctus est. ~~~~",
336 CONTENT_MODEL_WIKITEXT
339 $status = $page->doEditContent( $content, "testing 2", EDIT_UPDATE
);
340 $this->assertTrue( $status->isOK(), 'OK' );
341 $this->assertFalse( $status->value
['new'], 'new' );
342 $this->assertNotNull( $status->value
['revision'], 'revision' );
343 $this->assertSame( $status->value
['revision']->getId(), $page->getRevision()->getId() );
344 $this->assertSame( $status->value
['revision']->getSha1(), $page->getRevision()->getSha1() );
346 $status->value
['revision']->getContent()->equals( $content ),
347 'not equals (PST must substitute signature)'
350 $rev = $page->getRevision();
351 $this->assertNotNull( $rev->getRecentChange() );
352 $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
354 # ------------------------
355 $page = new WikiPage( $title );
357 $retrieved = $page->getContent();
358 $newText = $retrieved->serialize();
359 $this->assertContains( '[[gubergren]]', $newText, 'New text must replace old text.' );
360 $this->assertNotContains( '~~~~', $newText, 'PST must substitute signature.' );
362 # ------------------------
363 $dbr = wfGetDB( DB_REPLICA
);
364 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
365 $n = $res->numRows();
368 $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
372 * @covers WikiPage::doEditContent
374 public function testDoEditContent_twice() {
375 $title = Title
::newFromText( __METHOD__
);
376 $page = WikiPage
::factory( $title );
377 $content = ContentHandler
::makeContent( '$1 van $2', $title );
379 // Make sure we can do the exact same save twice.
380 // This tests checks that internal caches are reset as appropriate.
381 $status1 = $page->doEditContent( $content, __METHOD__
);
382 $status2 = $page->doEditContent( $content, __METHOD__
);
384 $this->assertTrue( $status1->isOK(), 'OK' );
385 $this->assertTrue( $status2->isOK(), 'OK' );
387 $this->assertTrue( isset( $status1->value
['revision'] ), 'OK' );
388 $this->assertFalse( isset( $status2->value
['revision'] ), 'OK' );
392 * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
393 * TODO: Revision deletion
395 * @covers WikiPage::doDeleteArticle
396 * @covers WikiPage::doDeleteArticleReal
398 public function testDoDeleteArticle() {
399 $page = $this->createPage(
401 "[[original text]] foo",
402 CONTENT_MODEL_WIKITEXT
404 $id = $page->getId();
406 $page->doDeleteArticle( "testing deletion" );
409 $page->getTitle()->getArticleID() > 0,
410 "Title object should now have page id 0"
412 $this->assertFalse( $page->getId() > 0, "WikiPage should now have page id 0" );
415 "WikiPage::exists should return false after page was deleted"
419 "WikiPage::getContent should return null after page was deleted"
422 $t = Title
::newFromText( $page->getTitle()->getPrefixedText() );
425 "Title::exists should return false after page was deleted"
429 JobQueueGroup
::destroySingletons();
431 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
434 # ------------------------
435 $dbr = wfGetDB( DB_REPLICA
);
436 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
437 $n = $res->numRows();
440 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
444 * @covers WikiPage::doDeleteArticleReal
446 public function testDoDeleteArticleReal_user0() {
447 $page = $this->createPage(
449 "[[original text]] foo",
450 CONTENT_MODEL_WIKITEXT
452 $id = $page->getId();
455 $status = $page->doDeleteArticleReal(
456 /* reason */ "testing user 0 deletion",
457 /* suppress */ false,
460 /* errorStack */ $errorStack,
463 $logId = $status->getValue();
464 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
466 [ 'logging' ] +
$actorQuery['tables'], /* table */
471 'log_user' => $actorQuery['fields']['log_user'],
472 'log_user_text' => $actorQuery['fields']['log_user_text'],
476 [ 'log_id' => $logId ],
480 'testing user 0 deletion',
483 (string)$page->getTitle()->getNamespace(),
484 $page->getTitle()->getDBkey(),
492 * @covers WikiPage::doDeleteArticleReal
494 public function testDoDeleteArticleReal_userSysop() {
495 $page = $this->createPage(
497 "[[original text]] foo",
498 CONTENT_MODEL_WIKITEXT
500 $id = $page->getId();
502 $user = $this->getTestSysop()->getUser();
504 $status = $page->doDeleteArticleReal(
505 /* reason */ "testing sysop deletion",
506 /* suppress */ false,
509 /* errorStack */ $errorStack,
512 $logId = $status->getValue();
513 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
515 [ 'logging' ] +
$actorQuery['tables'], /* table */
520 'log_user' => $actorQuery['fields']['log_user'],
521 'log_user_text' => $actorQuery['fields']['log_user_text'],
525 [ 'log_id' => $logId ],
529 'testing sysop deletion',
530 (string)$user->getId(),
532 (string)$page->getTitle()->getNamespace(),
533 $page->getTitle()->getDBkey(),
541 * TODO: Test more stuff about suppression.
543 * @covers WikiPage::doDeleteArticleReal
545 public function testDoDeleteArticleReal_suppress() {
546 $page = $this->createPage(
548 "[[original text]] foo",
549 CONTENT_MODEL_WIKITEXT
551 $id = $page->getId();
553 $user = $this->getTestSysop()->getUser();
555 $status = $page->doDeleteArticleReal(
556 /* reason */ "testing deletion",
560 /* errorStack */ $errorStack,
563 $logId = $status->getValue();
564 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
566 [ 'logging' ] +
$actorQuery['tables'], /* table */
571 'log_user' => $actorQuery['fields']['log_user'],
572 'log_user_text' => $actorQuery['fields']['log_user_text'],
576 [ 'log_id' => $logId ],
581 (string)$user->getId(),
583 (string)$page->getTitle()->getNamespace(),
584 $page->getTitle()->getDBkey(),
591 $page->getContent( Revision
::FOR_PUBLIC
),
592 "WikiPage::getContent should return null after the page was suppressed for general users"
596 $page->getContent( Revision
::FOR_THIS_USER
, null ),
597 "WikiPage::getContent should return null after the page was suppressed for user zero"
601 $page->getContent( Revision
::FOR_THIS_USER
, $user ),
602 "WikiPage::getContent should return null after the page was suppressed even for a sysop"
607 * @covers WikiPage::doDeleteUpdates
609 public function testDoDeleteUpdates() {
610 $user = $this->getTestUser()->getUser();
611 $page = $this->createPage(
613 "[[original text]] foo",
614 CONTENT_MODEL_WIKITEXT
616 $id = $page->getId();
617 $page->loadPageData(); // make sure the current revision is cached.
619 // Similar to MovePage logic
620 wfGetDB( DB_MASTER
)->delete( 'page', [ 'page_id' => $id ], __METHOD__
);
621 $page->doDeleteUpdates( $page->getId(), $page->getContent(), $page->getRevision(), $user );
624 JobQueueGroup
::destroySingletons();
626 $jobs->loadParamsAndArgs( null, [ 'quiet' => true ], null );
629 # ------------------------
630 $dbr = wfGetDB( DB_REPLICA
);
631 $res = $dbr->select( 'pagelinks', '*', [ 'pl_from' => $id ] );
632 $n = $res->numRows();
635 $this->assertEquals( 0, $n, 'pagelinks should contain no more links from the page' );
639 * @param string $name
641 * @return ContentHandler
643 protected function defineMockContentModelForUpdateTesting( $name ) {
644 /** @var ContentHandler|MockObject $handler */
645 $handler = $this->getMockBuilder( TextContentHandler
::class )
646 ->setConstructorArgs( [ $name ] )
648 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
652 $dataUpdate = new MWCallableUpdate( 'time' );
653 $dataUpdate->_name
= "$name data update";
655 $deletionUpdate = new MWCallableUpdate( 'time' );
656 $deletionUpdate->_name
= "$name deletion update";
658 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] );
659 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] );
660 $handler->method( 'unserializeContent' )->willReturnCallback(
661 function ( $text ) use ( $handler ) {
662 return $this->createMockContent( $handler, $text );
666 $this->mergeMwGlobalArrayValue(
667 'wgContentHandlers', [
668 $name => function () use ( $handler ){
678 * @param ContentHandler $handler
679 * @param string $text
683 protected function createMockContent( ContentHandler
$handler, $text ) {
684 /** @var Content|MockObject $content */
685 $content = $this->getMockBuilder( TextContent
::class )
686 ->setConstructorArgs( [ $text ] )
687 ->setMethods( [ 'getModel', 'getContentHandler' ] )
690 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
691 $content->method( 'getContentHandler' )->willReturn( $handler );
696 public function testGetDeletionUpdates() {
697 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
699 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
701 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
702 $page = $this->createPage(
704 [ 'main' => $mainContent1 ]
707 $dataUpdates = $page->getDeletionUpdates( $page->getRevisionRecord() );
708 $this->assertNotEmpty( $dataUpdates );
710 $updateNames = array_map( function ( $du ) {
711 return isset( $du->_name
) ?
$du->_name
: get_class( $du );
714 $this->assertContains( LinksDeletionUpdate
::class, $updateNames );
715 $this->assertContains( 'M1 deletion update', $updateNames );
719 * @covers WikiPage::getRevision
721 public function testGetRevision() {
722 $page = $this->newPage( __METHOD__
);
724 $rev = $page->getRevision();
725 $this->assertNull( $rev );
728 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
730 $rev = $page->getRevision();
732 $this->assertEquals( $page->getLatest(), $rev->getId() );
733 $this->assertEquals( "some text", $rev->getContent()->getNativeData() );
737 * @covers WikiPage::getContent
739 public function testGetContent() {
740 $page = $this->newPage( __METHOD__
);
742 $content = $page->getContent();
743 $this->assertNull( $content );
746 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
748 $content = $page->getContent();
749 $this->assertEquals( "some text", $content->getNativeData() );
753 * @covers WikiPage::exists
755 public function testExists() {
756 $page = $this->newPage( __METHOD__
);
757 $this->assertFalse( $page->exists() );
760 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
761 $this->assertTrue( $page->exists() );
763 $page = new WikiPage( $page->getTitle() );
764 $this->assertTrue( $page->exists() );
767 $page->doDeleteArticle( "done testing" );
768 $this->assertFalse( $page->exists() );
770 $page = new WikiPage( $page->getTitle() );
771 $this->assertFalse( $page->exists() );
774 public function provideHasViewableContent() {
776 [ 'WikiPageTest_testHasViewableContent', false, true ],
777 [ 'Special:WikiPageTest_testHasViewableContent', false ],
778 [ 'MediaWiki:WikiPageTest_testHasViewableContent', false ],
779 [ 'Special:Userlogin', true ],
780 [ 'MediaWiki:help', true ],
785 * @dataProvider provideHasViewableContent
786 * @covers WikiPage::hasViewableContent
788 public function testHasViewableContent( $title, $viewable, $create = false ) {
789 $page = $this->newPage( $title );
790 $this->assertEquals( $viewable, $page->hasViewableContent() );
793 $this->createPage( $page, "some text", CONTENT_MODEL_WIKITEXT
);
794 $this->assertTrue( $page->hasViewableContent() );
796 $page = new WikiPage( $page->getTitle() );
797 $this->assertTrue( $page->hasViewableContent() );
801 public function provideGetRedirectTarget() {
803 [ 'WikiPageTest_testGetRedirectTarget_1', CONTENT_MODEL_WIKITEXT
, "hello world", null ],
805 'WikiPageTest_testGetRedirectTarget_2',
806 CONTENT_MODEL_WIKITEXT
,
807 "#REDIRECT [[hello world]]",
814 * @dataProvider provideGetRedirectTarget
815 * @covers WikiPage::getRedirectTarget
817 public function testGetRedirectTarget( $title, $model, $text, $target ) {
818 $this->setMwGlobals( [
819 'wgCapitalLinks' => true,
822 $page = $this->createPage( $title, $text, $model );
824 # sanity check, because this test seems to fail for no reason for some people.
825 $c = $page->getContent();
826 $this->assertEquals( WikitextContent
::class, get_class( $c ) );
828 # now, test the actual redirect
829 $t = $page->getRedirectTarget();
830 $this->assertEquals( $target, is_null( $t ) ?
null : $t->getPrefixedText() );
834 * @dataProvider provideGetRedirectTarget
835 * @covers WikiPage::isRedirect
837 public function testIsRedirect( $title, $model, $text, $target ) {
838 $page = $this->createPage( $title, $text, $model );
839 $this->assertEquals( !is_null( $target ), $page->isRedirect() );
842 public function provideIsCountable() {
846 [ 'WikiPageTest_testIsCountable',
847 CONTENT_MODEL_WIKITEXT
,
852 [ 'WikiPageTest_testIsCountable',
853 CONTENT_MODEL_WIKITEXT
,
860 [ 'WikiPageTest_testIsCountable',
861 CONTENT_MODEL_WIKITEXT
,
866 [ 'WikiPageTest_testIsCountable',
867 CONTENT_MODEL_WIKITEXT
,
874 [ 'WikiPageTest_testIsCountable',
875 CONTENT_MODEL_WIKITEXT
,
880 [ 'WikiPageTest_testIsCountable',
881 CONTENT_MODEL_WIKITEXT
,
887 // not a content namespace
888 [ 'Talk:WikiPageTest_testIsCountable',
889 CONTENT_MODEL_WIKITEXT
,
894 [ 'Talk:WikiPageTest_testIsCountable',
895 CONTENT_MODEL_WIKITEXT
,
901 // not a content namespace, different model
902 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
908 [ 'MediaWiki:WikiPageTest_testIsCountable.js',
918 * @dataProvider provideIsCountable
919 * @covers WikiPage::isCountable
921 public function testIsCountable( $title, $model, $text, $mode, $expected ) {
922 global $wgContentHandlerUseDB;
924 $this->setMwGlobals( 'wgArticleCountMethod', $mode );
926 $title = Title
::newFromText( $title );
928 if ( !$wgContentHandlerUseDB
930 && ContentHandler
::getDefaultModelFor( $title ) != $model
932 $this->markTestSkipped( "Can not use non-default content model $model for "
933 . $title->getPrefixedDBkey() . " with \$wgContentHandlerUseDB disabled." );
936 $page = $this->createPage( $title, $text, $model );
938 $editInfo = $page->prepareContentForEdit( $page->getContent() );
940 $v = $page->isCountable();
941 $w = $page->isCountable( $editInfo );
946 "isCountable( null ) returned unexpected value " . var_export( $v, true )
947 . " instead of " . var_export( $expected, true )
948 . " in mode `$mode` for text \"$text\""
954 "isCountable( \$editInfo ) returned unexpected value " . var_export( $v, true )
955 . " instead of " . var_export( $expected, true )
956 . " in mode `$mode` for text \"$text\""
960 public function provideGetParserOutput() {
963 CONTENT_MODEL_WIKITEXT
,
965 "<div class=\"mw-parser-output\"><p>hello <i>world</i></p></div>"
972 * @dataProvider provideGetParserOutput
973 * @covers WikiPage::getParserOutput
975 public function testGetParserOutput( $model, $text, $expectedHtml ) {
976 $page = $this->createPage( __METHOD__
, $text, $model );
978 $opt = $page->makeParserOptions( 'canonical' );
979 $po = $page->getParserOutput( $opt );
980 $text = $po->getText();
982 $text = trim( preg_replace( '/<!--.*?-->/sm', '', $text ) ); # strip injected comments
983 $text = preg_replace( '!\s*(</p>|</div>)!sm', '\1', $text ); # don't let tidy confuse us
985 $this->assertEquals( $expectedHtml, $text );
991 * @covers WikiPage::getParserOutput
993 public function testGetParserOutput_nonexisting() {
994 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
996 $opt = new ParserOptions();
997 $po = $page->getParserOutput( $opt );
999 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing pages." );
1003 * @covers WikiPage::getParserOutput
1005 public function testGetParserOutput_badrev() {
1006 $page = $this->createPage( __METHOD__
, 'dummy', CONTENT_MODEL_WIKITEXT
);
1008 $opt = new ParserOptions();
1009 $po = $page->getParserOutput( $opt, $page->getLatest() +
1234 );
1011 // @todo would be neat to also test deleted revision
1013 $this->assertFalse( $po, "getParserOutput() shall return false for non-existing revisions." );
1016 public static $sections =
1030 public function dataReplaceSection() {
1031 // NOTE: assume the Help namespace to contain wikitext
1033 [ 'Help:WikiPageTest_testReplaceSection',
1034 CONTENT_MODEL_WIKITEXT
,
1039 trim( preg_replace( '/^Intro/sm', 'No more', self
::$sections ) )
1041 [ 'Help:WikiPageTest_testReplaceSection',
1042 CONTENT_MODEL_WIKITEXT
,
1049 [ 'Help:WikiPageTest_testReplaceSection',
1050 CONTENT_MODEL_WIKITEXT
,
1053 "== TEST ==\nmore fun",
1055 trim( preg_replace( '/^== test ==.*== foo ==/sm',
1056 "== TEST ==\nmore fun\n\n== foo ==",
1059 [ 'Help:WikiPageTest_testReplaceSection',
1060 CONTENT_MODEL_WIKITEXT
,
1065 trim( self
::$sections )
1067 [ 'Help:WikiPageTest_testReplaceSection',
1068 CONTENT_MODEL_WIKITEXT
,
1073 trim( self
::$sections ) . "\n\n== New ==\n\nNo more"
1079 * @dataProvider dataReplaceSection
1080 * @covers WikiPage::replaceSectionContent
1082 public function testReplaceSectionContent( $title, $model, $text, $section,
1083 $with, $sectionTitle, $expected
1085 $page = $this->createPage( $title, $text, $model );
1087 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
1088 $c = $page->replaceSectionContent( $section, $content, $sectionTitle );
1090 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
1094 * @dataProvider dataReplaceSection
1095 * @covers WikiPage::replaceSectionAtRev
1097 public function testReplaceSectionAtRev( $title, $model, $text, $section,
1098 $with, $sectionTitle, $expected
1100 $page = $this->createPage( $title, $text, $model );
1101 $baseRevId = $page->getLatest();
1103 $content = ContentHandler
::makeContent( $with, $page->getTitle(), $page->getContentModel() );
1104 $c = $page->replaceSectionAtRev( $section, $content, $sectionTitle, $baseRevId );
1106 $this->assertEquals( $expected, is_null( $c ) ?
null : trim( $c->getNativeData() ) );
1110 * @covers WikiPage::getOldestRevision
1112 public function testGetOldestRevision() {
1113 $page = $this->newPage( __METHOD__
);
1114 $page->doEditContent(
1115 new WikitextContent( 'one' ),
1119 $rev1 = $page->getRevision();
1121 $page = new WikiPage( $page->getTitle() );
1122 $page->doEditContent(
1123 new WikitextContent( 'two' ),
1128 $page = new WikiPage( $page->getTitle() );
1129 $page->doEditContent(
1130 new WikitextContent( 'three' ),
1136 $this->assertNotEquals(
1138 $page->getRevision()->getId(),
1139 '$page->getRevision()->getId()'
1143 $this->assertEquals(
1145 $page->getOldestRevision()->getId(),
1146 '$page->getOldestRevision()->getId()'
1151 * @covers WikiPage::doRollback
1152 * @covers WikiPage::commitRollback
1154 public function testDoRollback() {
1155 // FIXME: fails under postgres
1156 $this->markTestSkippedIfDbType( 'postgres' );
1158 $admin = $this->getTestSysop()->getUser();
1159 $user1 = $this->getTestUser()->getUser();
1160 // Use the confirmed group for user2 to make sure the user is different
1161 $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
1163 // make sure we can test autopatrolling
1164 $this->setMwGlobals( 'wgUseRCPatrol', true );
1166 // TODO: MCR: test rollback of multiple slots!
1167 $page = $this->newPage( __METHOD__
);
1171 $status1 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1172 "section one", EDIT_NEW
, false, $admin );
1175 $status2 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1176 "adding section two", 0, false, $user1 );
1178 $text .= "\n\nthree";
1179 $status3 = $page->doEditContent( ContentHandler
::makeContent( $text, $page->getTitle() ),
1180 "adding section three", 0, false, $user2 );
1182 /** @var Revision $rev1 */
1183 /** @var Revision $rev2 */
1184 /** @var Revision $rev3 */
1185 $rev1 = $status1->getValue()['revision'];
1186 $rev2 = $status2->getValue()['revision'];
1187 $rev3 = $status3->getValue()['revision'];
1190 * We are having issues with doRollback spuriously failing. Apparently
1191 * the last revision somehow goes missing or not committed under some
1192 * circumstances. So, make sure the revisions have the correct usernames.
1194 $this->assertEquals( 3, Revision
::countByPageId( wfGetDB( DB_REPLICA
), $page->getId() ) );
1195 $this->assertEquals( $admin->getName(), $rev1->getUserText() );
1196 $this->assertEquals( $user1->getName(), $rev2->getUserText() );
1197 $this->assertEquals( $user2->getName(), $rev3->getUserText() );
1199 // Now, try the actual rollback
1200 $token = $admin->getEditToken( 'rollback' );
1201 $rollbackErrors = $page->doRollback(
1210 if ( $rollbackErrors ) {
1212 "Rollback failed:\n" .
1213 print_r( $rollbackErrors, true ) . ";\n" .
1214 print_r( $resultDetails, true )
1218 $page = new WikiPage( $page->getTitle() );
1219 $this->assertEquals( $rev2->getSha1(), $page->getRevision()->getSha1(),
1220 "rollback did not revert to the correct revision" );
1221 $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
1223 $rc = MediaWikiServices
::getInstance()->getRevisionStore()->getRecentChange(
1224 $page->getRevision()->getRevisionRecord()
1227 $this->assertNotNull( $rc, 'RecentChanges entry' );
1228 $this->assertEquals(
1229 RecentChange
::PRC_AUTOPATROLLED
,
1230 $rc->getAttribute( 'rc_patrolled' ),
1234 // TODO: MCR: assert origin once we write slot data
1235 // $mainSlot = $page->getRevision()->getRevisionRecord()->getSlot( 'main' );
1236 // $this->assertTrue( $mainSlot->isInherited(), 'isInherited' );
1237 // $this->assertSame( $rev2->getId(), $mainSlot->getOrigin(), 'getOrigin' );
1241 * @covers WikiPage::doRollback
1242 * @covers WikiPage::commitRollback
1244 public function testDoRollbackFailureSameContent() {
1245 $admin = $this->getTestSysop()->getUser();
1248 $page = $this->newPage( __METHOD__
);
1249 $page->doEditContent(
1250 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1256 $rev1 = $page->getRevision();
1258 $user1 = $this->getTestUser( [ 'sysop' ] )->getUser();
1260 $page = new WikiPage( $page->getTitle() );
1261 $page->doEditContent(
1262 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1263 "adding section two",
1269 # now, do a the rollback from the same user was doing the edit before
1270 $resultDetails = [];
1271 $token = $user1->getEditToken( 'rollback' );
1272 $errors = $page->doRollback(
1274 "testing revert same user",
1281 $this->assertEquals( [], $errors, "Rollback failed same user" );
1283 # now, try the rollback
1284 $resultDetails = [];
1285 $token = $admin->getEditToken( 'rollback' );
1286 $errors = $page->doRollback(
1295 $this->assertEquals(
1305 "Rollback not failed"
1308 $page = new WikiPage( $page->getTitle() );
1309 $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
1310 "rollback did not revert to the correct revision" );
1311 $this->assertEquals( "one", $page->getContent()->getNativeData() );
1315 * Tests tagging for edits that do rollback action
1316 * @covers WikiPage::doRollback
1318 public function testDoRollbackTagging() {
1319 if ( !in_array( 'mw-rollback', ChangeTags
::getSoftwareTags() ) ) {
1320 $this->markTestSkipped( 'Rollback tag deactivated, skipped the test.' );
1323 $admin = new User();
1324 $admin->setName( 'Administrator' );
1325 $admin->addToDatabase();
1327 $text = 'First line';
1328 $page = $this->newPage( 'WikiPageTest_testDoRollbackTagging' );
1329 $page->doEditContent(
1330 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1337 $secondUser = new User();
1338 $secondUser->setName( '92.65.217.32' );
1339 $text .= '\n\nSecond line';
1340 $page = new WikiPage( $page->getTitle() );
1341 $page->doEditContent(
1342 ContentHandler
::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
1343 'Adding second line',
1349 // Now, try the rollback
1350 $admin->addGroup( 'sysop' ); // Make the test user a sysop
1351 $token = $admin->getEditToken( 'rollback' );
1352 $errors = $page->doRollback(
1353 $secondUser->getName(),
1361 // If doRollback completed without errors
1362 if ( $errors === [] ) {
1363 $tags = $resultDetails[ 'tags' ];
1364 $this->assertContains( 'mw-rollback', $tags );
1368 public function provideGetAutoDeleteReason() {
1378 [ "first edit", null ],
1380 "/first edit.*only contributor/",
1386 [ "first edit", null ],
1387 [ "second edit", null ],
1389 "/second edit.*only contributor/",
1395 [ "first edit", "127.0.2.22" ],
1396 [ "second edit", "127.0.3.33" ],
1406 . "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam "
1407 . " nonumy eirmod tempor invidunt ut labore et dolore magna "
1408 . "aliquyam erat, sed diam voluptua. At vero eos et accusam "
1409 . "et justo duo dolores et ea rebum. Stet clita kasd gubergren, "
1410 . "no sea takimata sanctus est Lorem ipsum dolor sit amet.'",
1414 '/first edit:.*\.\.\."/',
1420 [ "first edit", "127.0.2.22" ],
1421 [ "", "127.0.3.33" ],
1423 "/before blanking.*first edit/",
1431 * @dataProvider provideGetAutoDeleteReason
1432 * @covers WikiPage::getAutoDeleteReason
1434 public function testGetAutoDeleteReason( $edits, $expectedResult, $expectedHistory ) {
1437 // NOTE: assume Help namespace to contain wikitext
1438 $page = $this->newPage( "Help:WikiPageTest_testGetAutoDeleteReason" );
1442 foreach ( $edits as $edit ) {
1445 if ( !empty( $edit[1] ) ) {
1446 $user->setName( $edit[1] );
1451 $content = ContentHandler
::makeContent( $edit[0], $page->getTitle(), $page->getContentModel() );
1453 $page->doEditContent( $content, "test edit $c", $c < 2 ? EDIT_NEW
: 0, false, $user );
1458 $reason = $page->getAutoDeleteReason( $hasHistory );
1460 if ( is_bool( $expectedResult ) ||
is_null( $expectedResult ) ) {
1461 $this->assertEquals( $expectedResult, $reason );
1463 $this->assertTrue( (bool)preg_match( $expectedResult, $reason ),
1464 "Autosummary didn't match expected pattern $expectedResult: $reason" );
1467 $this->assertEquals( $expectedHistory, $hasHistory,
1468 "expected \$hasHistory to be " . var_export( $expectedHistory, true ) );
1470 $page->doDeleteArticle( "done" );
1473 public function providePreSaveTransform() {
1475 [ 'hello this is ~~~',
1476 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
1478 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1479 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
1485 * @covers WikiPage::factory
1487 public function testWikiPageFactory() {
1488 $title = Title
::makeTitle( NS_FILE
, 'Someimage.png' );
1489 $page = WikiPage
::factory( $title );
1490 $this->assertEquals( WikiFilePage
::class, get_class( $page ) );
1492 $title = Title
::makeTitle( NS_CATEGORY
, 'SomeCategory' );
1493 $page = WikiPage
::factory( $title );
1494 $this->assertEquals( WikiCategoryPage
::class, get_class( $page ) );
1496 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1497 $page = WikiPage
::factory( $title );
1498 $this->assertEquals( WikiPage
::class, get_class( $page ) );
1502 * @covers WikiPage::loadPageData
1503 * @covers WikiPage::wasLoadedFrom
1505 public function testLoadPageData() {
1506 $title = Title
::makeTitle( NS_MAIN
, 'SomePage' );
1507 $page = WikiPage
::factory( $title );
1509 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1510 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1511 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1512 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1514 $page->loadPageData( IDBAccessObject
::READ_NORMAL
);
1515 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1516 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1517 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1518 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1520 $page->loadPageData( IDBAccessObject
::READ_LATEST
);
1521 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1522 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1523 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1524 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1526 $page->loadPageData( IDBAccessObject
::READ_LOCKING
);
1527 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1528 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1529 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1530 $this->assertFalse( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1532 $page->loadPageData( IDBAccessObject
::READ_EXCLUSIVE
);
1533 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_NORMAL
) );
1534 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LATEST
) );
1535 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_LOCKING
) );
1536 $this->assertTrue( $page->wasLoadedFrom( IDBAccessObject
::READ_EXCLUSIVE
) );
1540 * @dataProvider provideCommentMigrationOnDeletion
1542 * @param int $writeStage
1543 * @param int $readStage
1545 public function testCommentMigrationOnDeletion( $writeStage, $readStage ) {
1546 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $writeStage );
1547 $this->overrideMwServices();
1549 $dbr = wfGetDB( DB_REPLICA
);
1551 $page = $this->createPage(
1554 CONTENT_MODEL_WIKITEXT
1556 $revid = $page->getLatest();
1557 if ( $writeStage > MIGRATION_OLD
) {
1558 $comment_id = $dbr->selectField(
1559 'revision_comment_temp',
1560 'revcomment_comment_id',
1561 [ 'revcomment_rev' => $revid ],
1566 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', $readStage );
1567 $this->overrideMwServices();
1569 $page->doDeleteArticle( "testing deletion" );
1571 if ( $readStage > MIGRATION_OLD
) {
1572 // Didn't leave behind any 'revision_comment_temp' rows
1573 $n = $dbr->selectField(
1574 'revision_comment_temp', 'COUNT(*)', [ 'revcomment_rev' => $revid ], __METHOD__
1576 $this->assertEquals( 0, $n, 'no entry in revision_comment_temp after deletion' );
1578 // Copied or upgraded the comment_id, as applicable
1579 $ar_comment_id = $dbr->selectField(
1582 [ 'ar_rev_id' => $revid ],
1585 if ( $writeStage > MIGRATION_OLD
) {
1586 $this->assertSame( $comment_id, $ar_comment_id );
1588 $this->assertNotEquals( 0, $ar_comment_id );
1592 // Copied rev_comment, if applicable
1593 if ( $readStage <= MIGRATION_WRITE_BOTH
&& $writeStage <= MIGRATION_WRITE_BOTH
) {
1594 $ar_comment = $dbr->selectField(
1597 [ 'ar_rev_id' => $revid ],
1600 $this->assertSame( 'testing', $ar_comment );
1604 public function provideCommentMigrationOnDeletion() {
1606 [ MIGRATION_OLD
, MIGRATION_OLD
],
1607 [ MIGRATION_OLD
, MIGRATION_WRITE_BOTH
],
1608 [ MIGRATION_OLD
, MIGRATION_WRITE_NEW
],
1609 [ MIGRATION_WRITE_BOTH
, MIGRATION_OLD
],
1610 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_BOTH
],
1611 [ MIGRATION_WRITE_BOTH
, MIGRATION_WRITE_NEW
],
1612 [ MIGRATION_WRITE_BOTH
, MIGRATION_NEW
],
1613 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_BOTH
],
1614 [ MIGRATION_WRITE_NEW
, MIGRATION_WRITE_NEW
],
1615 [ MIGRATION_WRITE_NEW
, MIGRATION_NEW
],
1616 [ MIGRATION_NEW
, MIGRATION_WRITE_BOTH
],
1617 [ MIGRATION_NEW
, MIGRATION_WRITE_NEW
],
1618 [ MIGRATION_NEW
, MIGRATION_NEW
],
1623 * @covers WikiPage::updateCategoryCounts
1625 public function testUpdateCategoryCounts() {
1626 $page = new WikiPage( Title
::newFromText( __METHOD__
) );
1628 // Add an initial category
1629 $page->updateCategoryCounts( [ 'A' ], [], 0 );
1631 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1632 $this->assertEquals( 0, Category
::newFromName( 'B' )->getPageCount() );
1633 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1635 // Add a new category
1636 $page->updateCategoryCounts( [ 'B' ], [], 0 );
1638 $this->assertEquals( 1, Category
::newFromName( 'A' )->getPageCount() );
1639 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1640 $this->assertEquals( 0, Category
::newFromName( 'C' )->getPageCount() );
1642 // Add and remove a category
1643 $page->updateCategoryCounts( [ 'C' ], [ 'A' ], 0 );
1645 $this->assertEquals( 0, Category
::newFromName( 'A' )->getPageCount() );
1646 $this->assertEquals( 1, Category
::newFromName( 'B' )->getPageCount() );
1647 $this->assertEquals( 1, Category
::newFromName( 'C' )->getPageCount() );
1650 public function provideUpdateRedirectOn() {
1651 yield
[ '#REDIRECT [[Foo]]', true, null, true, true, 0 ];
1652 yield
[ '#REDIRECT [[Foo]]', true, 'Foo', true, false, 1 ];
1653 yield
[ 'SomeText', false, null, false, true, 0 ];
1654 yield
[ 'SomeText', false, 'Foo', false, false, 1 ];
1658 * @dataProvider provideUpdateRedirectOn
1659 * @covers WikiPage::updateRedirectOn
1661 * @param string $initialText
1662 * @param bool $initialRedirectState
1663 * @param string|null $redirectTitle
1664 * @param bool|null $lastRevIsRedirect
1665 * @param bool $expectedSuccess
1666 * @param int $expectedRowCount
1668 public function testUpdateRedirectOn(
1670 $initialRedirectState,
1676 // FIXME: fails under sqlite and postgres
1677 $this->markTestSkippedIfDbType( 'sqlite' );
1678 $this->markTestSkippedIfDbType( 'postgres' );
1679 static $pageCounter = 0;
1682 $page = $this->createPage( Title
::newFromText( __METHOD__
. $pageCounter ), $initialText );
1683 $this->assertSame( $initialRedirectState, $page->isRedirect() );
1685 $redirectTitle = is_string( $redirectTitle )
1686 ? Title
::newFromText( $redirectTitle )
1689 $success = $page->updateRedirectOn( $this->db
, $redirectTitle, $lastRevIsRedirect );
1690 $this->assertSame( $expectedSuccess, $success, 'Success assertion' );
1692 * updateRedirectOn explicitly updates the redirect table (and not the page table).
1693 * Most of core checks the page table for redirect status, so we have to be ugly and
1694 * assert a select from the table here.
1696 $this->assertRedirectTableCountForPageId( $page->getId(), $expectedRowCount );
1699 private function assertRedirectTableCountForPageId( $pageId, $expected ) {
1700 $this->assertSelect(
1703 [ 'rd_from' => $pageId ],
1704 [ [ strval( $expected ) ] ]
1709 * @covers WikiPage::insertRedirectEntry
1711 public function testInsertRedirectEntry_insertsRedirectEntry() {
1712 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1713 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1715 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1716 $targetTitle->mInterwiki
= 'eninter';
1717 $page->insertRedirectEntry( $targetTitle, null );
1719 $this->assertSelect(
1721 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1722 [ 'rd_from' => $page->getId() ],
1724 strval( $page->getId() ),
1725 strval( $targetTitle->getNamespace() ),
1726 strval( $targetTitle->getDBkey() ),
1727 strval( $targetTitle->getFragment() ),
1728 strval( $targetTitle->getInterwiki() ),
1734 * @covers WikiPage::insertRedirectEntry
1736 public function testInsertRedirectEntry_insertsRedirectEntryWithPageLatest() {
1737 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1738 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1740 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1741 $targetTitle->mInterwiki
= 'eninter';
1742 $page->insertRedirectEntry( $targetTitle, $page->getLatest() );
1744 $this->assertSelect(
1746 [ 'rd_from', 'rd_namespace', 'rd_title', 'rd_fragment', 'rd_interwiki' ],
1747 [ 'rd_from' => $page->getId() ],
1749 strval( $page->getId() ),
1750 strval( $targetTitle->getNamespace() ),
1751 strval( $targetTitle->getDBkey() ),
1752 strval( $targetTitle->getFragment() ),
1753 strval( $targetTitle->getInterwiki() ),
1759 * @covers WikiPage::insertRedirectEntry
1761 public function testInsertRedirectEntry_doesNotInsertIfPageLatestIncorrect() {
1762 $page = $this->createPage( Title
::newFromText( __METHOD__
), 'A' );
1763 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1765 $targetTitle = Title
::newFromText( 'SomeTarget#Frag' );
1766 $targetTitle->mInterwiki
= 'eninter';
1767 $page->insertRedirectEntry( $targetTitle, 215251 );
1769 $this->assertRedirectTableCountForPageId( $page->getId(), 0 );
1772 private function getRow( array $overrides = [] ) {
1776 'page_is_redirect' => '1',
1777 'page_latest' => '99',
1778 'page_namespace' => '3',
1779 'page_title' => 'JaJaTitle',
1780 'page_restrictions' => 'edit=autoconfirmed,sysop:move=sysop',
1781 'page_touched' => '20120101020202',
1782 'page_links_updated' => '20140101020202',
1784 foreach ( $overrides as $key => $value ) {
1785 $row[$key] = $value;
1787 return (object)$row;
1790 public function provideNewFromRowSuccess() {
1791 yield
'basic row' => [
1793 function ( WikiPage
$wikiPage, self
$test ) {
1794 $test->assertSame( 44, $wikiPage->getId() );
1795 $test->assertSame( 76, $wikiPage->getTitle()->getLength() );
1796 $test->assertTrue( $wikiPage->isRedirect() );
1797 $test->assertSame( 99, $wikiPage->getLatest() );
1798 $test->assertSame( 3, $wikiPage->getTitle()->getNamespace() );
1799 $test->assertSame( 'JaJaTitle', $wikiPage->getTitle()->getDBkey() );
1802 'edit' => [ 'autoconfirmed', 'sysop' ],
1803 'move' => [ 'sysop' ],
1805 $wikiPage->getTitle()->getAllRestrictions()
1807 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1808 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1811 yield
'different timestamp formats' => [
1813 'page_touched' => '2012-01-01 02:02:02',
1814 'page_links_updated' => '2014-01-01 02:02:02',
1816 function ( WikiPage
$wikiPage, self
$test ) {
1817 $test->assertSame( '20120101020202', $wikiPage->getTouched() );
1818 $test->assertSame( '20140101020202', $wikiPage->getLinksTimestamp() );
1821 yield
'no restrictions' => [
1823 'page_restrictions' => '',
1825 function ( WikiPage
$wikiPage, self
$test ) {
1831 $wikiPage->getTitle()->getAllRestrictions()
1835 yield
'not redirect' => [
1837 'page_is_redirect' => '0',
1839 function ( WikiPage
$wikiPage, self
$test ) {
1840 $test->assertFalse( $wikiPage->isRedirect() );
1846 * @covers WikiPage::newFromRow
1847 * @covers WikiPage::loadFromRow
1848 * @dataProvider provideNewFromRowSuccess
1850 * @param object $row
1851 * @param callable $assertions
1853 public function testNewFromRow( $row, $assertions ) {
1854 $page = WikiPage
::newFromRow( $row, 'fromdb' );
1855 $assertions( $page, $this );
1858 public function provideTestNewFromId_returnsNullOnBadPageId() {
1864 * @covers WikiPage::newFromID
1865 * @dataProvider provideTestNewFromId_returnsNullOnBadPageId
1867 public function testNewFromId_returnsNullOnBadPageId( $pageId ) {
1868 $this->assertNull( WikiPage
::newFromID( $pageId ) );
1872 * @covers WikiPage::newFromID
1874 public function testNewFromId_appearsToFetchCorrectRow() {
1875 $createdPage = $this->createPage( __METHOD__
, 'Xsfaij09' );
1876 $fetchedPage = WikiPage
::newFromID( $createdPage->getId() );
1877 $this->assertSame( $createdPage->getId(), $fetchedPage->getId() );
1878 $this->assertEquals(
1879 $createdPage->getContent()->getNativeData(),
1880 $fetchedPage->getContent()->getNativeData()
1885 * @covers WikiPage::newFromID
1887 public function testNewFromId_returnsNullOnNonExistingId() {
1888 $this->assertNull( WikiPage
::newFromID( 2147483647 ) );
1891 public function provideTestInsertProtectNullRevision() {
1892 // phpcs:disable Generic.Files.LineLength
1895 [ 'edit' => 'sysop' ],
1896 [ 'edit' => '20200101040404' ],
1900 '(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)))'
1904 [ 'edit' => 'sysop', 'move' => 'something' ],
1905 [ 'edit' => '20200101040404', 'move' => '20210101050505' ],
1909 '(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)))'
1915 * @dataProvider provideTestInsertProtectNullRevision
1916 * @covers WikiPage::insertProtectNullRevision
1917 * @covers WikiPage::protectDescription
1919 * @param string $revCommentMsg
1920 * @param array $limit
1921 * @param array $expiry
1922 * @param bool $cascade
1923 * @param string $reason
1924 * @param bool|null $user true if the test sysop should be used, or null
1925 * @param string $expectedComment
1927 public function testInsertProtectNullRevision(
1936 $this->setContentLang( 'qqx' );
1938 $page = $this->createPage( __METHOD__
, 'Goat' );
1940 $user = $user === null ?
$user : $this->getTestSysop()->getUser();
1942 $result = $page->insertProtectNullRevision(
1951 $this->assertTrue( $result instanceof Revision
);
1952 $this->assertSame( $expectedComment, $result->getComment( Revision
::RAW
) );
1956 * @covers WikiPage::updateRevisionOn
1958 public function testUpdateRevisionOn_existingPage() {
1959 $user = $this->getTestSysop()->getUser();
1960 $page = $this->createPage( __METHOD__
, 'StartText' );
1962 $revision = new Revision(
1965 'page' => $page->getId(),
1966 'title' => $page->getTitle(),
1967 'comment' => __METHOD__
,
1968 'minor_edit' => true,
1969 'text' => __METHOD__
. '-text',
1970 'len' => strlen( __METHOD__
. '-text' ),
1971 'user' => $user->getId(),
1972 'user_text' => $user->getName(),
1973 'timestamp' => '20170707040404',
1974 'content_model' => CONTENT_MODEL_WIKITEXT
,
1975 'content_format' => CONTENT_FORMAT_WIKITEXT
,
1979 $result = $page->updateRevisionOn( $this->db
, $revision );
1980 $this->assertTrue( $result );
1981 $this->assertSame( 9989, $page->getLatest() );
1982 $this->assertEquals( $revision, $page->getRevision() );
1986 * @covers WikiPage::updateRevisionOn
1988 public function testUpdateRevisionOn_NonExistingPage() {
1989 $user = $this->getTestSysop()->getUser();
1990 $page = $this->createPage( __METHOD__
, 'StartText' );
1991 $page->doDeleteArticle( 'reason' );
1993 $revision = new Revision(
1996 'page' => $page->getId(),
1997 'title' => $page->getTitle(),
1998 'comment' => __METHOD__
,
1999 'minor_edit' => true,
2000 'text' => __METHOD__
. '-text',
2001 'len' => strlen( __METHOD__
. '-text' ),
2002 'user' => $user->getId(),
2003 'user_text' => $user->getName(),
2004 'timestamp' => '20170707040404',
2005 'content_model' => CONTENT_MODEL_WIKITEXT
,
2006 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2010 $result = $page->updateRevisionOn( $this->db
, $revision );
2011 $this->assertFalse( $result );
2015 * @covers WikiPage::updateIfNewerOn
2017 public function testUpdateIfNewerOn_olderRevision() {
2018 $user = $this->getTestSysop()->getUser();
2019 $page = $this->createPage( __METHOD__
, 'StartText' );
2020 $initialRevision = $page->getRevision();
2022 $olderTimeStamp = wfTimestamp(
2024 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) - 1
2027 $olderRevison = new Revision(
2030 'page' => $page->getId(),
2031 'title' => $page->getTitle(),
2032 'comment' => __METHOD__
,
2033 'minor_edit' => true,
2034 'text' => __METHOD__
. '-text',
2035 'len' => strlen( __METHOD__
. '-text' ),
2036 'user' => $user->getId(),
2037 'user_text' => $user->getName(),
2038 'timestamp' => $olderTimeStamp,
2039 'content_model' => CONTENT_MODEL_WIKITEXT
,
2040 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2044 $result = $page->updateIfNewerOn( $this->db
, $olderRevison );
2045 $this->assertFalse( $result );
2049 * @covers WikiPage::updateIfNewerOn
2051 public function testUpdateIfNewerOn_newerRevision() {
2052 $user = $this->getTestSysop()->getUser();
2053 $page = $this->createPage( __METHOD__
, 'StartText' );
2054 $initialRevision = $page->getRevision();
2056 $newerTimeStamp = wfTimestamp(
2058 wfTimestamp( TS_UNIX
, $initialRevision->getTimestamp() ) +
1
2061 $newerRevision = new Revision(
2064 'page' => $page->getId(),
2065 'title' => $page->getTitle(),
2066 'comment' => __METHOD__
,
2067 'minor_edit' => true,
2068 'text' => __METHOD__
. '-text',
2069 'len' => strlen( __METHOD__
. '-text' ),
2070 'user' => $user->getId(),
2071 'user_text' => $user->getName(),
2072 'timestamp' => $newerTimeStamp,
2073 'content_model' => CONTENT_MODEL_WIKITEXT
,
2074 'content_format' => CONTENT_FORMAT_WIKITEXT
,
2077 $result = $page->updateIfNewerOn( $this->db
, $newerRevision );
2078 $this->assertTrue( $result );
2082 * @covers WikiPage::insertOn
2084 public function testInsertOn() {
2085 $title = Title
::newFromText( __METHOD__
);
2086 $page = new WikiPage( $title );
2088 $startTimeStamp = wfTimestampNow();
2089 $result = $page->insertOn( $this->db
);
2090 $endTimeStamp = wfTimestampNow();
2092 $this->assertInternalType( 'int', $result );
2093 $this->assertTrue( $result > 0 );
2095 $condition = [ 'page_id' => $result ];
2097 // Check the default fields have been filled
2098 $this->assertSelect(
2103 'page_restrictions',
2121 // Check the page_random field has been filled
2122 $pageRandom = $this->db
->selectField( 'page', 'page_random', $condition );
2123 $this->assertTrue( (float)$pageRandom < 1 && (float)$pageRandom > 0 );
2125 // Assert the touched timestamp in the DB is roughly when we inserted the page
2126 $pageTouched = $this->db
->selectField( 'page', 'page_touched', $condition );
2128 wfTimestamp( TS_UNIX
, $startTimeStamp )
2129 <= wfTimestamp( TS_UNIX
, $pageTouched )
2132 wfTimestamp( TS_UNIX
, $endTimeStamp )
2133 >= wfTimestamp( TS_UNIX
, $pageTouched )
2136 // Try inserting the same page again and checking the result is false (no change)
2137 $result = $page->insertOn( $this->db
);
2138 $this->assertFalse( $result );
2142 * @covers WikiPage::insertOn
2144 public function testInsertOn_idSpecified() {
2145 $title = Title
::newFromText( __METHOD__
);
2146 $page = new WikiPage( $title );
2149 $result = $page->insertOn( $this->db
, $id );
2151 $this->assertSame( $id, $result );
2153 $condition = [ 'page_id' => $result ];
2155 // Check there is actually a row in the db
2156 $this->assertSelect(
2164 public function provideTestDoUpdateRestrictions_setBasicRestrictions() {
2165 // Note: Once the current dates passes the date in these tests they will fail.
2166 yield
'move something' => [
2168 [ 'move' => 'something' ],
2170 [ 'edit' => [], 'move' => [ 'something' ] ],
2173 yield
'move something, edit blank' => [
2175 [ 'move' => 'something', 'edit' => '' ],
2177 [ 'edit' => [], 'move' => [ 'something' ] ],
2180 yield
'edit sysop, with expiry' => [
2182 [ 'edit' => 'sysop' ],
2183 [ 'edit' => '21330101020202' ],
2184 [ 'edit' => [ 'sysop' ], 'move' => [] ],
2185 [ 'edit' => '21330101020202' ],
2187 yield
'move and edit, move with expiry' => [
2189 [ 'move' => 'something', 'edit' => 'another' ],
2190 [ 'move' => '22220202010101' ],
2191 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2192 [ 'move' => '22220202010101' ],
2194 yield
'move and edit, edit with infinity expiry' => [
2196 [ 'move' => 'something', 'edit' => 'another' ],
2197 [ 'edit' => 'infinity' ],
2198 [ 'edit' => [ 'another' ], 'move' => [ 'something' ] ],
2199 [ 'edit' => 'infinity' ],
2201 yield
'non existing, create something' => [
2203 [ 'create' => 'something' ],
2205 [ 'create' => [ 'something' ] ],
2208 yield
'non existing, create something with expiry' => [
2210 [ 'create' => 'something' ],
2211 [ 'create' => '23451212112233' ],
2212 [ 'create' => [ 'something' ] ],
2213 [ 'create' => '23451212112233' ],
2218 * @dataProvider provideTestDoUpdateRestrictions_setBasicRestrictions
2219 * @covers WikiPage::doUpdateRestrictions
2221 public function testDoUpdateRestrictions_setBasicRestrictions(
2225 array $expectedRestrictions,
2226 array $expectedRestrictionExpiries
2228 if ( $pageExists ) {
2229 $page = $this->createPage( __METHOD__
, 'ABC' );
2231 $page = new WikiPage( Title
::newFromText( __METHOD__
. '-nonexist' ) );
2233 $user = $this->getTestSysop()->getUser();
2236 $status = $page->doUpdateRestrictions( $limit, $expiry, $cascade, 'aReason', $user, [] );
2238 $logId = $status->getValue();
2239 $allRestrictions = $page->getTitle()->getAllRestrictions();
2241 $this->assertTrue( $status->isGood() );
2242 $this->assertInternalType( 'int', $logId );
2243 $this->assertSame( $expectedRestrictions, $allRestrictions );
2244 foreach ( $expectedRestrictionExpiries as $key => $value ) {
2245 $this->assertSame( $value, $page->getTitle()->getRestrictionExpiry( $key ) );
2248 // Make sure the log entry looks good
2249 // log_params is not checked here
2250 $actorQuery = ActorMigration
::newMigration()->getJoin( 'log_user' );
2251 $this->assertSelect(
2252 [ 'logging' ] +
$actorQuery['tables'],
2255 'log_user' => $actorQuery['fields']['log_user'],
2256 'log_user_text' => $actorQuery['fields']['log_user_text'],
2260 [ 'log_id' => $logId ],
2263 (string)$user->getId(),
2265 (string)$page->getTitle()->getNamespace(),
2266 $page->getTitle()->getDBkey(),
2269 $actorQuery['joins']
2274 * @covers WikiPage::doUpdateRestrictions
2276 public function testDoUpdateRestrictions_failsOnReadOnly() {
2277 $page = $this->createPage( __METHOD__
, 'ABC' );
2278 $user = $this->getTestSysop()->getUser();
2282 $readOnly = $this->getMockBuilder( ReadOnlyMode
::class )
2283 ->disableOriginalConstructor()
2284 ->setMethods( [ 'isReadOnly', 'getReason' ] )
2286 $readOnly->expects( $this->once() )
2287 ->method( 'isReadOnly' )
2288 ->will( $this->returnValue( true ) );
2289 $readOnly->expects( $this->once() )
2290 ->method( 'getReason' )
2291 ->will( $this->returnValue( 'Some Read Only Reason' ) );
2292 $this->setService( 'ReadOnlyMode', $readOnly );
2294 $status = $page->doUpdateRestrictions( [], [], $cascade, 'aReason', $user, [] );
2295 $this->assertFalse( $status->isOK() );
2296 $this->assertSame( 'readonlytext', $status->getMessage()->getKey() );
2300 * @covers WikiPage::doUpdateRestrictions
2302 public function testDoUpdateRestrictions_returnsGoodIfNothingChanged() {
2303 $page = $this->createPage( __METHOD__
, 'ABC' );
2304 $user = $this->getTestSysop()->getUser();
2306 $limit = [ 'edit' => 'sysop' ];
2308 $status = $page->doUpdateRestrictions(
2317 // The first entry should have a logId as it did something
2318 $this->assertTrue( $status->isGood() );
2319 $this->assertInternalType( 'int', $status->getValue() );
2321 $status = $page->doUpdateRestrictions(
2330 // The second entry should not have a logId as nothing changed
2331 $this->assertTrue( $status->isGood() );
2332 $this->assertNull( $status->getValue() );
2336 * @covers WikiPage::doUpdateRestrictions
2338 public function testDoUpdateRestrictions_logEntryTypeAndAction() {
2339 $page = $this->createPage( __METHOD__
, 'ABC' );
2340 $user = $this->getTestSysop()->getUser();
2344 $status = $page->doUpdateRestrictions(
2345 [ 'edit' => 'sysop' ],
2352 $this->assertTrue( $status->isGood() );
2353 $this->assertInternalType( 'int', $status->getValue() );
2354 $this->assertSelect(
2356 [ 'log_type', 'log_action' ],
2357 [ 'log_id' => $status->getValue() ],
2358 [ [ 'protect', 'protect' ] ]
2361 // Modify the protection
2362 $status = $page->doUpdateRestrictions(
2363 [ 'edit' => 'somethingElse' ],
2370 $this->assertTrue( $status->isGood() );
2371 $this->assertInternalType( 'int', $status->getValue() );
2372 $this->assertSelect(
2374 [ 'log_type', 'log_action' ],
2375 [ 'log_id' => $status->getValue() ],
2376 [ [ 'protect', 'modify' ] ]
2379 // Remove the protection
2380 $status = $page->doUpdateRestrictions(
2388 $this->assertTrue( $status->isGood() );
2389 $this->assertInternalType( 'int', $status->getValue() );
2390 $this->assertSelect(
2392 [ 'log_type', 'log_action' ],
2393 [ 'log_id' => $status->getValue() ],
2394 [ [ 'protect', 'unprotect' ] ]
2399 * @covers WikiPage::newPageUpdater
2400 * @covers WikiPage::getDerivedDataUpdater
2402 public function testNewPageUpdater() {
2403 $user = $this->getTestUser()->getUser();
2404 $page = $this->newPage( __METHOD__
, __METHOD__
);
2406 /** @var Content $content */
2407 $content = $this->getMockBuilder( WikitextContent
::class )
2408 ->setConstructorArgs( [ 'Hello World' ] )
2409 ->setMethods( [ 'getParserOutput' ] )
2411 $content->expects( $this->once() )
2412 ->method( 'getParserOutput' )
2413 ->willReturn( new ParserOutput( 'HTML' ) );
2415 $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user );
2417 // provide context, so the cache can be kept in place
2418 $slotsUpdate = new revisionSlotsUpdate();
2419 $slotsUpdate->modifyContent( 'main', $content );
2421 $updater = $page->newPageUpdater( $user, $slotsUpdate );
2422 $updater->setContent( 'main', $content );
2423 $revision = $updater->saveRevision(
2424 CommentStoreComment
::newUnsavedComment( 'test' ),
2428 $preparedEditAfter = $page->prepareContentForEdit( $content, $revision, $user );
2430 $this->assertSame( $revision->getId(), $page->getLatest() );
2432 // Parsed output must remain cached throughout.
2433 $this->assertSame( $preparedEditBefore->output
, $preparedEditAfter->output
);
2437 * @covers WikiPage::newPageUpdater
2438 * @covers WikiPage::getDerivedDataUpdater
2440 public function testGetDerivedDataUpdater() {
2441 $admin = $this->getTestSysop()->getUser();
2443 /** @var object $page */
2444 $page = $this->createPage( __METHOD__
, __METHOD__
);
2445 $page = TestingAccessWrapper
::newFromObject( $page );
2447 $revision = $page->getRevision()->getRevisionRecord();
2448 $user = $revision->getUser();
2450 $slotsUpdate = new RevisionSlotsUpdate();
2451 $slotsUpdate->modifyContent( 'main', new WikitextContent( 'Hello World' ) );
2453 // get a virgin updater
2454 $updater1 = $page->getDerivedDataUpdater( $user );
2455 $this->assertFalse( $updater1->isUpdatePrepared() );
2457 $updater1->prepareUpdate( $revision );
2459 // Re-use updater with same revision or content, even if base changed
2460 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, $revision ) );
2462 $slotsUpdate = RevisionSlotsUpdate
::newFromContent(
2463 [ 'main' => $revision->getContent( 'main' ) ]
2465 $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, null, $slotsUpdate ) );
2467 // Don't re-use for edit if base revision ID changed
2468 $this->assertNotSame(
2470 $page->getDerivedDataUpdater( $user, null, $slotsUpdate, true )
2473 // Don't re-use with different user
2474 $updater2a = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2475 $updater2a->prepareContent( $admin, $slotsUpdate, false );
2477 $updater2b = $page->getDerivedDataUpdater( $user, null, $slotsUpdate );
2478 $updater2b->prepareContent( $user, $slotsUpdate, false );
2479 $this->assertNotSame( $updater2a, $updater2b );
2481 // Don't re-use with different content
2482 $updater3 = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
2483 $updater3->prepareUpdate( $revision );
2484 $this->assertNotSame( $updater2b, $updater3 );
2486 // Don't re-use if no context given
2487 $updater4 = $page->getDerivedDataUpdater( $admin );
2488 $updater4->prepareUpdate( $revision );
2489 $this->assertNotSame( $updater3, $updater4 );
2491 // Don't re-use if AGAIN no context given
2492 $updater5 = $page->getDerivedDataUpdater( $admin );
2493 $this->assertNotSame( $updater4, $updater5 );
2495 // Don't re-use cached "virgin" unprepared updater
2496 $updater6 = $page->getDerivedDataUpdater( $admin, $revision );
2497 $this->assertNotSame( $updater5, $updater6 );
2500 protected function assertPreparedEditEquals(
2501 PreparedEdit
$edit, PreparedEdit
$edit2, $message = ''
2503 // suppress differences caused by a clock tick between generating the two PreparedEdits
2504 if ( abs( $edit->timestamp
- $edit2->timestamp
) < 3 ) {
2505 $edit2 = clone $edit2;
2506 $edit2->timestamp
= $edit->timestamp
;
2508 $this->assertEquals( $edit, $edit2, $message );
2511 protected function assertPreparedEditNotEquals(
2512 PreparedEdit
$edit, PreparedEdit
$edit2, $message = ''
2514 if ( abs( $edit->timestamp
- $edit2->timestamp
) < 3 ) {
2515 $edit2 = clone $edit2;
2516 $edit2->timestamp
= $edit->timestamp
;
2518 $this->assertNotEquals( $edit, $edit2, $message );