3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
9 use InvalidArgumentException
;
11 use MediaWiki\Linker\LinkTarget
;
12 use MediaWiki\MediaWikiServices
;
13 use MediaWiki\Revision\IncompleteRevisionException
;
14 use MediaWiki\Revision\MutableRevisionRecord
;
15 use MediaWiki\Revision\RevisionArchiveRecord
;
16 use MediaWiki\Revision\RevisionRecord
;
17 use MediaWiki\Revision\RevisionStoreRecord
;
18 use MediaWiki\Revision\RevisionSlots
;
19 use MediaWiki\Revision\RevisionStore
;
20 use MediaWiki\Revision\SlotRecord
;
21 use MediaWiki\Storage\BlobStoreFactory
;
22 use MediaWiki\Storage\SqlBlobStore
;
23 use MediaWiki\User\UserIdentityValue
;
24 use MediaWikiTestCase
;
25 use PHPUnit_Framework_MockObject_MockObject
;
30 use Wikimedia\Rdbms\Database
;
31 use Wikimedia\Rdbms\DatabaseDomain
;
32 use Wikimedia\Rdbms\DatabaseSqlite
;
33 use Wikimedia\Rdbms\FakeResultWrapper
;
34 use Wikimedia\Rdbms\LoadBalancer
;
35 use Wikimedia\Rdbms\TransactionProfiler
;
41 * @group RevisionStore
43 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
48 private $testPageTitle;
58 abstract protected function getMcrMigrationStage();
63 protected function getContentHandlerUseDB() {
70 abstract protected function getMcrTablesToReset();
72 public function setUp() {
74 $this->tablesUsed
[] = 'archive';
75 $this->tablesUsed
[] = 'page';
76 $this->tablesUsed
[] = 'revision';
77 $this->tablesUsed
[] = 'comment';
79 $this->tablesUsed +
= $this->getMcrTablesToReset();
81 $this->setMwGlobals( [
82 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
83 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
84 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
87 $this->overrideMwServices();
90 protected function addCoreDBData() {
91 // Blank out. This would fail with a modified schema, and we don't need it.
97 protected function getTestPageTitle() {
98 if ( $this->testPageTitle
) {
99 return $this->testPageTitle
;
102 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
103 return $this->testPageTitle
;
109 protected function getTestPage() {
110 if ( $this->testPage
) {
111 return $this->testPage
;
114 $title = $this->getTestPageTitle();
115 $this->testPage
= WikiPage
::factory( $title );
117 if ( !$this->testPage
->exists() ) {
118 // Make sure we don't write to the live db.
119 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
121 $user = static::getTestSysop()->getUser();
123 $this->testPage
->doEditContent(
124 new WikitextContent( 'UTContent-' . __CLASS__
),
125 'UTPageSummary-' . __CLASS__
,
126 EDIT_NEW | EDIT_SUPPRESS_RC
,
132 return $this->testPage
;
136 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
138 private function getLoadBalancerMock( array $server ) {
139 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
141 $lb = $this->getMockBuilder( LoadBalancer
::class )
142 ->setMethods( [ 'reallyOpenConnection' ] )
143 ->setConstructorArgs( [
144 [ 'servers' => [ $server ], 'localDomain' => $domain ]
148 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
149 function ( array $server, $dbNameOverride ) {
150 return $this->getDatabaseMock( $server );
158 * @return Database|PHPUnit_Framework_MockObject_MockObject
160 private function getDatabaseMock( array $params ) {
161 $db = $this->getMockBuilder( DatabaseSqlite
::class )
162 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
163 ->setConstructorArgs( [ $params ] )
166 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
167 $db->method( 'isOpen' )->willReturn( true );
172 public function provideDomainCheck() {
173 yield
[ false, 'test', '' ];
174 yield
[ 'test', 'test', '' ];
176 yield
[ false, 'test', 'foo_' ];
177 yield
[ 'test-foo_', 'test', 'foo_' ];
179 yield
[ false, 'dash-test', '' ];
180 yield
[ 'dash-test', 'dash-test', '' ];
182 yield
[ false, 'underscore_test', 'foo_' ];
183 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
187 * @dataProvider provideDomainCheck
188 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
190 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
193 'wgDBname' => $dbName,
194 'wgDBprefix' => $dbPrefix,
198 $loadBalancer = $this->getLoadBalancerMock(
201 'dbDirectory' => '*dummy*',
203 'password' => 'test',
211 'trxProfiler' => new TransactionProfiler(),
212 'connLogger' => new \Psr\Log\
NullLogger(),
213 'queryLogger' => new \Psr\Log\
NullLogger(),
214 'errorLogger' => function () {
216 'deprecationLogger' => function () {
220 'tablePrefix' => $dbPrefix,
223 $db = $loadBalancer->getConnection( DB_REPLICA
);
225 /** @var SqlBlobStore $blobStore */
226 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
227 ->disableOriginalConstructor()
230 $store = new RevisionStore(
233 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
234 MediaWikiServices
::getInstance()->getCommentStore(),
235 MediaWikiServices
::getInstance()->getContentModelStore(),
236 MediaWikiServices
::getInstance()->getSlotRoleStore(),
237 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
238 $this->getMcrMigrationStage(),
239 MediaWikiServices
::getInstance()->getActorMigration(),
243 $count = $store->countRevisionsByPageId( $db, 0 );
245 // Dummy check to make PhpUnit happy. We are really only interested in
246 // countRevisionsByPageId not failing due to the DB domain check.
247 $this->assertSame( 0, $count );
250 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
251 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
252 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
253 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
254 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
257 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
259 $r1->getPageAsLinkTarget()->getNamespace(),
260 $r2->getPageAsLinkTarget()->getNamespace()
264 $r1->getPageAsLinkTarget()->getText(),
265 $r2->getPageAsLinkTarget()->getText()
268 if ( $r1->getParentId() ) {
269 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
272 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
273 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
274 $this->assertEquals( $r1->getComment(), $r2->getComment() );
275 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
276 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
277 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
278 $this->assertEquals( $r1->getSize(), $r2->getSize() );
279 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
280 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
281 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
282 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
283 foreach ( $r1->getSlotRoles() as $role ) {
284 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
285 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
288 RevisionRecord
::DELETED_TEXT
,
289 RevisionRecord
::DELETED_COMMENT
,
290 RevisionRecord
::DELETED_USER
,
291 RevisionRecord
::DELETED_RESTRICTED
,
293 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
297 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
298 $this->assertSame( $s1->getRole(), $s2->getRole() );
299 $this->assertSame( $s1->getModel(), $s2->getModel() );
300 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
301 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
302 $this->assertSame( $s1->getSize(), $s2->getSize() );
303 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
305 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
306 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
309 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
310 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
311 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
312 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
314 foreach ( $r->getSlotRoles() as $role ) {
315 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
319 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
320 $this->assertTrue( $slot->hasAddress() );
321 $this->assertSame( $r->getId(), $slot->getRevision() );
323 $this->assertInstanceOf( Content
::class, $slot->getContent() );
327 * @param mixed[] $details
329 * @return RevisionRecord
331 private function getRevisionRecordFromDetailsArray( $details = [] ) {
332 // Convert some values that can't be provided by dataProviders
333 if ( isset( $details['user'] ) && $details['user'] === true ) {
334 $details['user'] = $this->getTestUser()->getUser();
336 if ( isset( $details['page'] ) && $details['page'] === true ) {
337 $details['page'] = $this->getTestPage()->getId();
339 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
340 $details['parent'] = $this->getTestPage()->getLatest();
343 // Create the RevisionRecord with any available data
344 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
345 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
346 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
347 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
348 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
349 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
350 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
351 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
352 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
353 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
354 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
355 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
357 if ( isset( $details['content'] ) ) {
358 foreach ( $details['content'] as $role => $content ) {
359 $rev->setContent( $role, $content );
366 public function provideInsertRevisionOn_successes() {
367 yield
'Bare minimum revision insertion' => [
369 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
371 'comment' => $this->getRandomCommentStoreComment(),
372 'timestamp' => '20171117010101',
376 yield
'Detailed revision insertion' => [
378 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
381 'comment' => $this->getRandomCommentStoreComment(),
382 'timestamp' => '20171117010101',
385 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
390 protected function getRandomCommentStoreComment() {
391 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
395 * @dataProvider provideInsertRevisionOn_successes
396 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
397 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
398 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
400 public function testInsertRevisionOn_successes(
401 array $revDetails = []
403 $title = $this->getTestPageTitle();
404 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
406 $this->overrideMwServices();
407 $store = MediaWikiServices
::getInstance()->getRevisionStore();
408 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
410 // is the new revision correct?
411 $this->assertRevisionCompleteness( $return );
412 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
413 $this->assertRevisionRecordsEqual( $rev, $return );
415 // can we load it from the store?
416 $loaded = $store->getRevisionById( $return->getId() );
417 $this->assertRevisionCompleteness( $loaded );
418 $this->assertRevisionRecordsEqual( $return, $loaded );
420 // can we find it directly in the database?
421 $this->assertRevisionExistsInDatabase( $return );
424 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
425 $row = $this->revisionToRow( new Revision( $rev ), [] );
427 // unset nulled fields
428 unset( $row->rev_content_model
);
429 unset( $row->rev_content_format
);
432 unset( $row->rev_comment_text
);
433 unset( $row->rev_comment_data
);
434 unset( $row->rev_comment_cid
);
435 unset( $row->rev_comment_id
);
437 $store = MediaWikiServices
::getInstance()->getRevisionStore();
438 $queryInfo = $store->getQueryInfo( [ 'user' ] );
440 $row = get_object_vars( $row );
442 // Use aliased fields from $queryInfo, e.g. rev_user
443 $keys = array_keys( $row );
444 $keys = array_combine( $keys, $keys );
445 $fields = array_intersect_key( $queryInfo['fields'], $keys ) +
$keys;
447 // assertSelect() fails unless the orders match.
452 $queryInfo['tables'],
454 [ 'rev_id' => $rev->getId() ],
455 [ array_values( $row ) ],
462 * @param SlotRecord $a
463 * @param SlotRecord $b
465 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
466 // Assert that the same blob address has been used.
467 $this->assertSame( $a->getAddress(), $b->getAddress() );
471 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
473 public function testInsertRevisionOn_blobAddressExists() {
474 $title = $this->getTestPageTitle();
476 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
478 'comment' => $this->getRandomCommentStoreComment(),
479 'timestamp' => '20171117010101',
483 $this->overrideMwServices();
484 $store = MediaWikiServices
::getInstance()->getRevisionStore();
486 // Insert the first revision
487 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
488 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
489 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
490 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
492 // Insert a second revision inheriting the same blob address
493 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
494 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
495 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
496 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
497 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
499 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
500 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
502 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
504 // And that different revisions have been created.
505 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
507 // Make sure the slot rows reference the correct revision
508 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
509 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
512 public function provideInsertRevisionOn_failures() {
515 'comment' => $this->getRandomCommentStoreComment(),
516 'timestamp' => '20171117010101',
519 new InvalidArgumentException( 'main slot must be provided' )
521 yield
'no main slot' => [
523 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
524 'comment' => $this->getRandomCommentStoreComment(),
525 'timestamp' => '20171117010101',
528 new InvalidArgumentException( 'main slot must be provided' )
530 yield
'no timestamp' => [
532 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
533 'comment' => $this->getRandomCommentStoreComment(),
536 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
538 yield
'no comment' => [
540 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
541 'timestamp' => '20171117010101',
544 new IncompleteRevisionException( 'comment must not be NULL!' )
548 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
549 'comment' => $this->getRandomCommentStoreComment(),
550 'timestamp' => '20171117010101',
552 new IncompleteRevisionException( 'user must not be NULL!' )
557 * @dataProvider provideInsertRevisionOn_failures
558 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
560 public function testInsertRevisionOn_failures(
561 array $revDetails = [],
564 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
566 $store = MediaWikiServices
::getInstance()->getRevisionStore();
568 $this->setExpectedException(
569 get_class( $exception ),
570 $exception->getMessage(),
571 $exception->getCode()
573 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
576 public function provideNewNullRevision() {
578 Title
::newFromText( 'UTPage_notAutoCreated' ),
579 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
580 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
584 Title
::newFromText( 'UTPage_notAutoCreated' ),
585 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
586 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
592 * @dataProvider provideNewNullRevision
593 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
594 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
596 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
597 $this->overrideMwServices();
599 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
600 $page = WikiPage
::factory( $title );
602 if ( !$page->exists() ) {
603 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
606 $revDetails['page'] = $page->getId();
607 $revDetails['timestamp'] = wfTimestampNow();
608 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
609 $revDetails['user'] = $user;
611 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
612 $store = MediaWikiServices
::getInstance()->getRevisionStore();
614 $dbw = wfGetDB( DB_MASTER
);
615 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
616 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
618 $record = $store->newNullRevision(
619 wfGetDB( DB_MASTER
),
626 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
627 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
628 $this->assertEquals( $comment, $record->getComment() );
629 $this->assertEquals( $minor, $record->isMinor() );
630 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
631 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
633 $this->assertArrayEquals(
634 $baseRev->getSlotRoles(),
635 $record->getSlotRoles()
638 foreach ( $baseRev->getSlotRoles() as $role ) {
639 $parentSlot = $baseRev->getSlot( $role );
640 $slot = $record->getSlot( $role );
642 $this->assertTrue( $slot->isInherited(), 'isInherited' );
643 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
644 $this->assertSameSlotContent( $parentSlot, $slot );
649 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
651 public function testNewNullRevision_nonExistingTitle() {
652 $store = MediaWikiServices
::getInstance()->getRevisionStore();
653 $record = $store->newNullRevision(
654 wfGetDB( DB_MASTER
),
655 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
656 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
658 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
660 $this->assertNull( $record );
664 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
666 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
667 $page = $this->getTestPage();
668 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
669 /** @var Revision $rev */
670 $rev = $status->value
['revision'];
672 $store = MediaWikiServices
::getInstance()->getRevisionStore();
673 $revisionRecord = $store->getRevisionById( $rev->getId() );
674 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
676 $this->assertGreaterThan( 0, $result );
678 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
684 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
686 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
687 // This assumes that sysops are auto patrolled
688 $sysop = $this->getTestSysop()->getUser();
689 $page = $this->getTestPage();
690 $status = $page->doEditContent(
691 new WikitextContent( __METHOD__
),
697 /** @var Revision $rev */
698 $rev = $status->value
['revision'];
700 $store = MediaWikiServices
::getInstance()->getRevisionStore();
701 $revisionRecord = $store->getRevisionById( $rev->getId() );
702 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
704 $this->assertSame( 0, $result );
708 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
710 public function testGetRecentChange() {
711 $page = $this->getTestPage();
712 $content = new WikitextContent( __METHOD__
);
713 $status = $page->doEditContent( $content, __METHOD__
);
714 /** @var Revision $rev */
715 $rev = $status->value
['revision'];
717 $store = MediaWikiServices
::getInstance()->getRevisionStore();
718 $revRecord = $store->getRevisionById( $rev->getId() );
719 $recentChange = $store->getRecentChange( $revRecord );
721 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
722 $this->assertEquals( $rev->getRecentChange(), $recentChange );
726 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
728 public function testGetRevisionById() {
729 $page = $this->getTestPage();
730 $content = new WikitextContent( __METHOD__
);
731 $status = $page->doEditContent( $content, __METHOD__
);
732 /** @var Revision $rev */
733 $rev = $status->value
['revision'];
735 $store = MediaWikiServices
::getInstance()->getRevisionStore();
736 $revRecord = $store->getRevisionById( $rev->getId() );
738 $this->assertSame( $rev->getId(), $revRecord->getId() );
739 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
740 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
744 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
746 public function testGetRevisionByTitle() {
747 $page = $this->getTestPage();
748 $content = new WikitextContent( __METHOD__
);
749 $status = $page->doEditContent( $content, __METHOD__
);
750 /** @var Revision $rev */
751 $rev = $status->value
['revision'];
753 $store = MediaWikiServices
::getInstance()->getRevisionStore();
754 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
756 $this->assertSame( $rev->getId(), $revRecord->getId() );
757 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
758 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
762 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
764 public function testGetRevisionByPageId() {
765 $page = $this->getTestPage();
766 $content = new WikitextContent( __METHOD__
);
767 $status = $page->doEditContent( $content, __METHOD__
);
768 /** @var Revision $rev */
769 $rev = $status->value
['revision'];
771 $store = MediaWikiServices
::getInstance()->getRevisionStore();
772 $revRecord = $store->getRevisionByPageId( $page->getId() );
774 $this->assertSame( $rev->getId(), $revRecord->getId() );
775 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
776 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
780 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
782 public function testGetRevisionByTimestamp() {
783 // Make sure there is 1 second between the last revision and the rev we create...
784 // Otherwise we might not get the correct revision and the test may fail...
786 $page = $this->getTestPage();
788 $content = new WikitextContent( __METHOD__
);
789 $status = $page->doEditContent( $content, __METHOD__
);
790 /** @var Revision $rev */
791 $rev = $status->value
['revision'];
793 $store = MediaWikiServices
::getInstance()->getRevisionStore();
794 $revRecord = $store->getRevisionByTimestamp(
799 $this->assertSame( $rev->getId(), $revRecord->getId() );
800 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
801 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
804 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
805 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
806 $page = WikiPage
::factory( $rev->getTitle() );
809 'rev_id' => (string)$rev->getId(),
810 'rev_page' => (string)$rev->getPage(),
811 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
812 'rev_user_text' => (string)$rev->getUserText(),
813 'rev_user' => (string)$rev->getUser() ?
: null,
814 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
815 'rev_deleted' => (string)$rev->getVisibility(),
816 'rev_len' => (string)$rev->getSize(),
817 'rev_parent_id' => (string)$rev->getParentId(),
818 'rev_sha1' => (string)$rev->getSha1(),
821 if ( in_array( 'page', $options ) ) {
823 'page_namespace' => (string)$page->getTitle()->getNamespace(),
824 'page_title' => $page->getTitle()->getDBkey(),
825 'page_id' => (string)$page->getId(),
826 'page_latest' => (string)$page->getLatest(),
827 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
828 'page_len' => (string)$page->getContent()->getSize(),
832 if ( in_array( 'user', $options ) ) {
834 'user_name' => (string)$rev->getUserText(),
838 if ( in_array( 'comment', $options ) ) {
840 'rev_comment_text' => $rev->getComment(),
841 'rev_comment_data' => null,
842 'rev_comment_cid' => null,
846 if ( $rev->getId() ) {
848 'rev_id' => (string)$rev->getId(),
852 return (object)$fields;
855 protected function assertRevisionRecordMatchesRevision(
857 RevisionRecord
$record
859 $this->assertSame( $rev->getId(), $record->getId() );
860 $this->assertSame( $rev->getPage(), $record->getPageId() );
861 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
862 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
863 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
864 $this->assertSame( $rev->isMinor(), $record->isMinor() );
865 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
866 $this->assertSame( $rev->getSize(), $record->getSize() );
868 * @note As of MW 1.31, the database schema allows the parent ID to be
869 * NULL to indicate that it is unknown.
871 $expectedParent = $rev->getParentId();
872 if ( $expectedParent === null ) {
875 $this->assertSame( $expectedParent, $record->getParentId() );
876 $this->assertSame( $rev->getSha1(), $record->getSha1() );
877 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
878 $this->assertSame( $rev->getContentFormat(),
879 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
880 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
881 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
883 $revRec = $rev->getRevisionRecord();
884 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
885 $recMain = $record->getSlot( SlotRecord
::MAIN
);
887 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
888 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
889 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
891 if ( $revMain->hasOrigin() ) {
892 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
895 if ( $revMain->hasAddress() ) {
896 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
899 if ( $revMain->hasContentId() ) {
900 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
905 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
906 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
908 public function testNewRevisionFromRow_getQueryInfo() {
909 $page = $this->getTestPage();
910 $text = __METHOD__
. 'a-ä';
911 /** @var Revision $rev */
912 $rev = $page->doEditContent(
913 new WikitextContent( $text ),
915 )->value
['revision'];
917 $store = MediaWikiServices
::getInstance()->getRevisionStore();
918 $info = $store->getQueryInfo();
919 $row = $this->db
->selectRow(
922 [ 'rev_id' => $rev->getId() ],
927 $record = $store->newRevisionFromRow(
932 $this->assertRevisionRecordMatchesRevision( $rev, $record );
933 $this->assertSame( $text, $rev->getContent()->serialize() );
937 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
939 public function testNewRevisionFromRow_anonEdit() {
940 $page = $this->getTestPage();
941 $text = __METHOD__
. 'a-ä';
942 /** @var Revision $rev */
943 $rev = $page->doEditContent(
944 new WikitextContent( $text ),
946 )->value
['revision'];
948 $store = MediaWikiServices
::getInstance()->getRevisionStore();
949 $record = $store->newRevisionFromRow(
950 $this->revisionToRow( $rev ),
954 $this->assertRevisionRecordMatchesRevision( $rev, $record );
955 $this->assertSame( $text, $rev->getContent()->serialize() );
959 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
961 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
962 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
963 $this->overrideMwServices();
964 $page = $this->getTestPage();
965 $text = __METHOD__
. 'a-ä';
966 /** @var Revision $rev */
967 $rev = $page->doEditContent(
968 new WikitextContent( $text ),
970 )->value
['revision'];
972 $store = MediaWikiServices
::getInstance()->getRevisionStore();
973 $record = $store->newRevisionFromRow(
974 $this->revisionToRow( $rev ),
978 $this->assertRevisionRecordMatchesRevision( $rev, $record );
979 $this->assertSame( $text, $rev->getContent()->serialize() );
983 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
985 public function testNewRevisionFromRow_userEdit() {
986 $page = $this->getTestPage();
987 $text = __METHOD__
. 'b-ä';
988 /** @var Revision $rev */
989 $rev = $page->doEditContent(
990 new WikitextContent( $text ),
994 $this->getTestUser()->getUser()
995 )->value
['revision'];
997 $store = MediaWikiServices
::getInstance()->getRevisionStore();
998 $record = $store->newRevisionFromRow(
999 $this->revisionToRow( $rev ),
1003 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1004 $this->assertSame( $text, $rev->getContent()->serialize() );
1008 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1009 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1011 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1012 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1013 $title = Title
::newFromText( __METHOD__
);
1014 $text = __METHOD__
. '-bä';
1015 $page = WikiPage
::factory( $title );
1016 /** @var Revision $orig */
1017 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1018 ->value
['revision'];
1019 $page->doDeleteArticle( __METHOD__
);
1021 $db = wfGetDB( DB_MASTER
);
1022 $arQuery = $store->getArchiveQueryInfo();
1024 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1025 __METHOD__
, [], $arQuery['joins']
1027 $this->assertTrue( is_object( $res ), 'query failed' );
1029 $row = $res->fetchObject();
1031 $record = $store->newRevisionFromArchiveRow( $row );
1033 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1034 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1038 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1040 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1041 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1042 $this->overrideMwServices();
1043 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1044 $title = Title
::newFromText( __METHOD__
);
1045 $text = __METHOD__
. '-bä';
1046 $page = WikiPage
::factory( $title );
1047 /** @var Revision $orig */
1048 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1049 ->value
['revision'];
1050 $page->doDeleteArticle( __METHOD__
);
1052 $db = wfGetDB( DB_MASTER
);
1053 $arQuery = $store->getArchiveQueryInfo();
1055 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1056 __METHOD__
, [], $arQuery['joins']
1058 $this->assertTrue( is_object( $res ), 'query failed' );
1060 $row = $res->fetchObject();
1062 $record = $store->newRevisionFromArchiveRow( $row );
1064 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1065 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1069 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1071 public function testNewRevisionFromArchiveRow_no_user() {
1072 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1076 'ar_page_id' => '2',
1077 'ar_namespace' => '0',
1078 'ar_title' => 'Something',
1080 'ar_text_id' => '47',
1081 'ar_timestamp' => '20180528192356',
1082 'ar_minor_edit' => '0',
1083 'ar_deleted' => '0',
1085 'ar_parent_id' => '0',
1086 'ar_sha1' => 'deadbeef',
1087 'ar_comment_text' => 'whatever',
1088 'ar_comment_data' => null,
1089 'ar_comment_cid' => null,
1091 'ar_user_text' => '', // this is the important bit
1093 'ar_content_format' => null,
1094 'ar_content_model' => null,
1097 \Wikimedia\
suppressWarnings();
1098 $record = $store->newRevisionFromArchiveRow( $row );
1099 \Wikimedia\
suppressWarnings( true );
1101 $this->assertInstanceOf( RevisionRecord
::class, $record );
1102 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1103 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1107 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1109 public function testNewRevisionFromRow_no_user() {
1110 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1111 $title = Title
::newFromText( __METHOD__
);
1116 'page_namespace' => '0',
1117 'page_title' => $title->getText(),
1118 'rev_text_id' => '47',
1119 'rev_timestamp' => '20180528192356',
1120 'rev_minor_edit' => '0',
1121 'rev_deleted' => '0',
1123 'rev_parent_id' => '0',
1124 'rev_sha1' => 'deadbeef',
1125 'rev_comment_text' => 'whatever',
1126 'rev_comment_data' => null,
1127 'rev_comment_cid' => null,
1129 'rev_user_text' => '', // this is the important bit
1130 'rev_actor' => null,
1131 'rev_content_format' => null,
1132 'rev_content_model' => null,
1135 \Wikimedia\
suppressWarnings();
1136 $record = $store->newRevisionFromRow( $row, 0, $title );
1137 \Wikimedia\
suppressWarnings( true );
1139 $this->assertNotNull( $record );
1140 $this->assertNotNull( $record->getUser() );
1141 $this->assertNotEmpty( $record->getUser()->getName() );
1145 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1147 public function testInsertRevisionOn_archive() {
1148 // This is a round trip test for deletion and undeletion of a
1149 // revision row via the archive table.
1151 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1152 $title = Title
::newFromText( __METHOD__
);
1154 $page = WikiPage
::factory( $title );
1155 /** @var Revision $origRev */
1156 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1157 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1158 ->value
['revision'];
1159 $orig = $origRev->getRevisionRecord();
1160 $page->doDeleteArticle( __METHOD__
);
1162 // re-create page, so we can later load revisions for it
1163 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1165 $db = wfGetDB( DB_MASTER
);
1166 $arQuery = $store->getArchiveQueryInfo();
1167 $row = $db->selectRow(
1168 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1169 __METHOD__
, [], $arQuery['joins']
1172 $this->assertNotFalse( $row, 'query failed' );
1174 $record = $store->newRevisionFromArchiveRow(
1178 [ 'page_id' => $title->getArticleID() ]
1181 $restored = $store->insertRevisionOn( $record, $db );
1183 // is the new revision correct?
1184 $this->assertRevisionCompleteness( $restored );
1185 $this->assertRevisionRecordsEqual( $record, $restored );
1187 // does the new revision use the original slot?
1188 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1189 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1190 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1191 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1192 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1193 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1195 // can we load it from the store?
1196 $loaded = $store->getRevisionById( $restored->getId() );
1197 $this->assertNotNull( $loaded );
1198 $this->assertRevisionCompleteness( $loaded );
1199 $this->assertRevisionRecordsEqual( $restored, $loaded );
1201 // can we find it directly in the database?
1202 $this->assertRevisionExistsInDatabase( $restored );
1206 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1208 public function testLoadRevisionFromId() {
1209 $title = Title
::newFromText( __METHOD__
);
1210 $page = WikiPage
::factory( $title );
1211 /** @var Revision $rev */
1212 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1213 ->value
['revision'];
1215 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1216 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1217 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1221 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1223 public function testLoadRevisionFromPageId() {
1224 $title = Title
::newFromText( __METHOD__
);
1225 $page = WikiPage
::factory( $title );
1226 /** @var Revision $rev */
1227 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1228 ->value
['revision'];
1230 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1231 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1232 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1236 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1238 public function testLoadRevisionFromTitle() {
1239 $title = Title
::newFromText( __METHOD__
);
1240 $page = WikiPage
::factory( $title );
1241 /** @var Revision $rev */
1242 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1243 ->value
['revision'];
1245 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1246 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1247 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1251 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1253 public function testLoadRevisionFromTimestamp() {
1254 $title = Title
::newFromText( __METHOD__
);
1255 $page = WikiPage
::factory( $title );
1256 /** @var Revision $revOne */
1257 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1258 ->value
['revision'];
1259 // Sleep to ensure different timestamps... )(evil)
1261 /** @var Revision $revTwo */
1262 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1263 ->value
['revision'];
1265 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1267 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1271 $store->loadRevisionFromTimestamp(
1272 wfGetDB( DB_MASTER
),
1274 $revOne->getTimestamp()
1279 $store->loadRevisionFromTimestamp(
1280 wfGetDB( DB_MASTER
),
1282 $revTwo->getTimestamp()
1288 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1290 public function testGetParentLengths() {
1291 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1292 /** @var Revision $revOne */
1293 $revOne = $page->doEditContent(
1294 new WikitextContent( __METHOD__
), __METHOD__
1295 )->value
['revision'];
1296 /** @var Revision $revTwo */
1297 $revTwo = $page->doEditContent(
1298 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1299 )->value
['revision'];
1301 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1304 $revOne->getId() => strlen( __METHOD__
),
1306 $store->listRevisionSizes(
1307 wfGetDB( DB_MASTER
),
1308 [ $revOne->getId() ]
1313 $revOne->getId() => strlen( __METHOD__
),
1314 $revTwo->getId() => strlen( __METHOD__
) +
1,
1316 $store->listRevisionSizes(
1317 wfGetDB( DB_MASTER
),
1318 [ $revOne->getId(), $revTwo->getId() ]
1324 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1326 public function testGetPreviousRevision() {
1327 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1328 /** @var Revision $revOne */
1329 $revOne = $page->doEditContent(
1330 new WikitextContent( __METHOD__
), __METHOD__
1331 )->value
['revision'];
1332 /** @var Revision $revTwo */
1333 $revTwo = $page->doEditContent(
1334 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1335 )->value
['revision'];
1337 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1339 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1343 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1348 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1350 public function testGetNextRevision() {
1351 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1352 /** @var Revision $revOne */
1353 $revOne = $page->doEditContent(
1354 new WikitextContent( __METHOD__
), __METHOD__
1355 )->value
['revision'];
1356 /** @var Revision $revTwo */
1357 $revTwo = $page->doEditContent(
1358 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1359 )->value
['revision'];
1361 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1364 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1367 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1371 public function provideNonHistoryRevision() {
1372 $title = Title
::newFromText( __METHOD__
);
1373 $rev = new MutableRevisionRecord( $title );
1376 $user = new UserIdentityValue( 7, 'Frank', 0 );
1377 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1380 'ar_rev_id' => 34567,
1383 'ar_minor_edit' => 0,
1384 'ar_timestamp' => '20180101020202',
1386 $slots = new RevisionSlots( [] );
1387 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1392 * @dataProvider provideNonHistoryRevision
1393 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1395 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1396 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1397 $this->assertNull( $store->getPreviousRevision( $rev ) );
1401 * @dataProvider provideNonHistoryRevision
1402 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1404 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1405 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1406 $this->assertNull( $store->getNextRevision( $rev ) );
1410 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1412 public function testGetTimestampFromId_found() {
1413 $page = $this->getTestPage();
1414 /** @var Revision $rev */
1415 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1416 ->value
['revision'];
1418 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1419 $result = $store->getTimestampFromId( $rev->getId() );
1421 $this->assertSame( $rev->getTimestamp(), $result );
1425 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1427 public function testGetTimestampFromId_notFound() {
1428 $page = $this->getTestPage();
1429 /** @var Revision $rev */
1430 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1431 ->value
['revision'];
1433 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1434 $result = $store->getTimestampFromId( $rev->getId() +
1 );
1436 $this->assertFalse( $result );
1440 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1442 public function testCountRevisionsByPageId() {
1443 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1444 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1448 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1450 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1453 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1455 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1458 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1463 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1465 public function testCountRevisionsByTitle() {
1466 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1467 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1471 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1473 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1476 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1478 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1481 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1486 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1488 public function testUserWasLastToEdit_false() {
1489 $sysop = $this->getTestSysop()->getUser();
1490 $page = $this->getTestPage();
1491 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1493 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1494 $result = $store->userWasLastToEdit(
1495 wfGetDB( DB_MASTER
),
1500 $this->assertFalse( $result );
1504 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1506 public function testUserWasLastToEdit_true() {
1507 $startTime = wfTimestampNow();
1508 $sysop = $this->getTestSysop()->getUser();
1509 $page = $this->getTestPage();
1510 $page->doEditContent(
1511 new WikitextContent( __METHOD__
),
1518 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1519 $result = $store->userWasLastToEdit(
1520 wfGetDB( DB_MASTER
),
1525 $this->assertTrue( $result );
1529 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1531 public function testGetKnownCurrentRevision() {
1532 $page = $this->getTestPage();
1533 /** @var Revision $rev */
1534 $rev = $page->doEditContent(
1535 new WikitextContent( __METHOD__
. 'b' ),
1539 $this->getTestUser()->getUser()
1540 )->value
['revision'];
1542 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1543 $record = $store->getKnownCurrentRevision(
1548 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1551 public function provideNewMutableRevisionFromArray() {
1552 yield
'Basic array, content object' => [
1556 'timestamp' => '20171017114835',
1557 'user_text' => '111.0.1.2',
1559 'minor_edit' => false,
1563 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1564 'comment' => 'Goat Comment!',
1565 'content' => new WikitextContent( 'Some Content' ),
1568 yield
'Basic array, serialized text' => [
1572 'timestamp' => '20171017114835',
1573 'user_text' => '111.0.1.2',
1575 'minor_edit' => false,
1579 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1580 'comment' => 'Goat Comment!',
1581 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1584 yield
'Basic array, serialized text, utf-8 flags' => [
1588 'timestamp' => '20171017114835',
1589 'user_text' => '111.0.1.2',
1591 'minor_edit' => false,
1595 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1596 'comment' => 'Goat Comment!',
1597 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1601 yield
'Basic array, with title' => [
1603 'title' => Title
::newFromText( 'SomeText' ),
1604 'timestamp' => '20171017114835',
1605 'user_text' => '111.0.1.2',
1607 'minor_edit' => false,
1611 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1612 'comment' => 'Goat Comment!',
1613 'content' => new WikitextContent( 'Some Content' ),
1616 yield
'Basic array, no user field' => [
1620 'timestamp' => '20171017114835',
1621 'user_text' => '111.0.1.3',
1622 'minor_edit' => false,
1626 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1627 'comment' => 'Goat Comment!',
1628 'content' => new WikitextContent( 'Some Content' ),
1634 * @dataProvider provideNewMutableRevisionFromArray
1635 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1637 public function testNewMutableRevisionFromArray( array $array ) {
1638 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1640 // HACK: if $array['page'] is given, make sure that the page exists
1641 if ( isset( $array['page'] ) ) {
1642 $t = Title
::newFromID( $array['page'] );
1643 if ( !$t ||
!$t->exists() ) {
1644 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1645 $info = $this->insertPage( $t );
1646 $array['page'] = $info['id'];
1650 $result = $store->newMutableRevisionFromArray( $array );
1652 if ( isset( $array['id'] ) ) {
1653 $this->assertSame( $array['id'], $result->getId() );
1655 if ( isset( $array['page'] ) ) {
1656 $this->assertSame( $array['page'], $result->getPageId() );
1658 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1659 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1660 if ( isset( $array['user'] ) ) {
1661 $this->assertSame( $array['user'], $result->getUser()->getId() );
1663 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1664 $this->assertSame( $array['deleted'], $result->getVisibility() );
1665 $this->assertSame( $array['len'], $result->getSize() );
1666 $this->assertSame( $array['parent_id'], $result->getParentId() );
1667 $this->assertSame( $array['sha1'], $result->getSha1() );
1668 $this->assertSame( $array['comment'], $result->getComment()->text
);
1669 if ( isset( $array['content'] ) ) {
1670 foreach ( $array['content'] as $role => $content ) {
1672 $result->getContent( $role )->equals( $content )
1675 } elseif ( isset( $array['text'] ) ) {
1676 $this->assertSame( $array['text'],
1677 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1678 } elseif ( isset( $array['content_format'] ) ) {
1680 $array['content_format'],
1681 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1683 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1688 * @dataProvider provideNewMutableRevisionFromArray
1689 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1691 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1692 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1693 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1694 $blobStore = new SqlBlobStore( $lb, $cache );
1695 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1697 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1698 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1699 ->disableOriginalConstructor()
1701 $factory->expects( $this->any() )
1702 ->method( 'newBlobStore' )
1703 ->willReturn( $blobStore );
1704 $factory->expects( $this->any() )
1705 ->method( 'newSqlBlobStore' )
1706 ->willReturn( $blobStore );
1708 $this->setService( 'BlobStoreFactory', $factory );
1710 $this->testNewMutableRevisionFromArray( $array );
1714 * Creates a new revision for testing caching behavior
1716 * @param WikiPage $page the page for the new revision
1717 * @param RevisionStore $store store object to use for creating the revision
1718 * @return bool|RevisionStoreRecord the revision created, or false if missing
1720 private function createRevisionStoreCacheRecord( $page, $store ) {
1721 $user = MediaWikiTestCase
::getMutableTestUser()->getUser();
1722 $updater = $page->newPageUpdater( $user );
1723 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( __METHOD__
) );
1724 $summary = CommentStoreComment
::newUnsavedComment( __METHOD__
);
1725 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
1726 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1730 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1732 public function testGetKnownCurrentRevision_userNameChange() {
1733 global $wgActorTableSchemaMigrationStage;
1735 $this->overrideMwServices();
1736 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1737 $this->setService( 'MainWANObjectCache', $cache );
1739 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1740 $page = $this->getNonexistingTestPage();
1741 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1743 // Grab the user name
1744 $userNameBefore = $rev->getUser()->getName();
1746 // Change the user name in the database, "behind the back" of the cache
1747 $newUserName = "Renamed $userNameBefore";
1748 $this->db
->update( 'revision',
1749 [ 'rev_user_text' => $newUserName ],
1750 [ 'rev_id' => $rev->getId() ] );
1751 $this->db
->update( 'user',
1752 [ 'user_name' => $newUserName ],
1753 [ 'user_id' => $rev->getUser()->getId() ] );
1754 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1755 $this->db
->update( 'actor',
1756 [ 'actor_name' => $newUserName ],
1757 [ 'actor_user' => $rev->getUser()->getId() ] );
1760 // Reload the revision and regrab the user name.
1761 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1762 $userNameAfter = $revAfter->getUser()->getName();
1764 // The two user names should be different.
1765 // If they are the same, we are seeing a cached value, which is bad.
1766 $this->assertNotSame( $userNameBefore, $userNameAfter );
1768 // This is implied by the above assertion, but explicitly check it, for completeness
1769 $this->assertSame( $newUserName, $userNameAfter );
1773 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1775 public function testGetKnownCurrentRevision_revDelete() {
1776 $this->overrideMwServices();
1777 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1778 $this->setService( 'MainWANObjectCache', $cache );
1780 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1781 $page = $this->getNonexistingTestPage();
1782 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1784 // Grab the deleted bitmask
1785 $deletedBefore = $rev->getVisibility();
1787 // Change the deleted bitmask in the database, "behind the back" of the cache
1788 $this->db
->update( 'revision',
1789 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1790 [ 'rev_id' => $rev->getId() ] );
1792 // Reload the revision and regrab the visibility flag.
1793 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1794 $deletedAfter = $revAfter->getVisibility();
1796 // The two deleted flags should be different.
1797 // If they are the same, we are seeing a cached value, which is bad.
1798 $this->assertNotSame( $deletedBefore, $deletedAfter );
1800 // This is implied by the above assertion, but explicitly check it, for completeness
1801 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1805 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1807 public function testNewRevisionFromRow_userNameChange() {
1808 global $wgActorTableSchemaMigrationStage;
1810 $page = $this->getTestPage();
1812 /** @var Revision $rev */
1813 $rev = $page->doEditContent(
1814 new WikitextContent( $text ),
1818 $this->getMutableTestUser()->getUser()
1819 )->value
['revision'];
1821 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1822 $record = $store->newRevisionFromRow(
1823 $this->revisionToRow( $rev ),
1828 // Grab the user name
1829 $userNameBefore = $record->getUser()->getName();
1831 // Change the user name in the database
1832 $newUserName = "Renamed $userNameBefore";
1833 $this->db
->update( 'revision',
1834 [ 'rev_user_text' => $newUserName ],
1835 [ 'rev_id' => $record->getId() ] );
1836 $this->db
->update( 'user',
1837 [ 'user_name' => $newUserName ],
1838 [ 'user_id' => $record->getUser()->getId() ] );
1839 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1840 $this->db
->update( 'actor',
1841 [ 'actor_name' => $newUserName ],
1842 [ 'actor_user' => $record->getUser()->getId() ] );
1845 // Reload the record, passing $fromCache as true to force fresh info from the db,
1846 // and regrab the user name
1847 $recordAfter = $store->newRevisionFromRow(
1848 $this->revisionToRow( $rev ),
1853 $userNameAfter = $recordAfter->getUser()->getName();
1855 // The two user names should be different.
1856 // If they are the same, we are seeing a cached value, which is bad.
1857 $this->assertNotSame( $userNameBefore, $userNameAfter );
1859 // This is implied by the above assertion, but explicitly check it, for completeness
1860 $this->assertSame( $newUserName, $userNameAfter );
1864 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1866 public function testNewRevisionFromRow_revDelete() {
1867 $page = $this->getTestPage();
1869 /** @var Revision $rev */
1870 $rev = $page->doEditContent(
1871 new WikitextContent( $text ),
1873 )->value
['revision'];
1875 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1876 $record = $store->newRevisionFromRow(
1877 $this->revisionToRow( $rev ),
1882 // Grab the deleted bitmask
1883 $deletedBefore = $record->getVisibility();
1885 // Change the deleted bitmask in the database
1886 $this->db
->update( 'revision',
1887 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1888 [ 'rev_id' => $record->getId() ] );
1890 // Reload the record, passing $fromCache as true to force fresh info from the db,
1891 // and regrab the deleted bitmask
1892 $recordAfter = $store->newRevisionFromRow(
1893 $this->revisionToRow( $rev ),
1898 $deletedAfter = $recordAfter->getVisibility();
1900 // The two deleted flags should be different, because we modified the database.
1901 $this->assertNotSame( $deletedBefore, $deletedAfter );
1903 // This is implied by the above assertion, but explicitly check it, for completeness
1904 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );