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::checkDatabaseDomain
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 // XXX: the content ID value is ill-defined when SCHEMA_COMPAT_WRITE_BOTH and
901 // SCHEMA_COMPAT_READ_OLD is set, since revision insertion will report the
902 // content ID used with the new schema, while loading the revision from the
903 // old schema will report an emulated ID.
904 if ( $this->getMcrMigrationStage() & SCHEMA_COMPAT_READ_NEW
) {
905 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
911 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
912 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
914 public function testNewRevisionFromRowAndSlot_getQueryInfo() {
915 $page = $this->getTestPage();
916 $text = __METHOD__
. 'o-ö';
917 /** @var Revision $rev */
918 $rev = $page->doEditContent(
919 new WikitextContent( $text ),
921 )->value
['revision'];
923 $store = MediaWikiServices
::getInstance()->getRevisionStore();
924 $info = $store->getQueryInfo();
925 $row = $this->db
->selectRow(
928 [ 'rev_id' => $rev->getId() ],
934 $info = $store->getSlotsQueryInfo( [ 'content' ] );
935 $slotRows = $this->db
->select(
938 $this->getSlotRevisionConditions( $rev->getId() ),
944 $record = $store->newRevisionFromRowAndSlots(
946 iterator_to_array( $slotRows ),
950 $this->assertRevisionRecordMatchesRevision( $rev, $record );
951 $this->assertSame( $text, $rev->getContent()->serialize() );
955 * Conditions to use together with getSlotsQueryInfo() when selecting slot rows for a given
960 abstract protected function getSlotRevisionConditions( $revId );
963 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
964 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
965 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
967 public function testNewRevisionFromRow_getQueryInfo() {
968 $page = $this->getTestPage();
969 $text = __METHOD__
. 'a-ä';
970 /** @var Revision $rev */
971 $rev = $page->doEditContent(
972 new WikitextContent( $text ),
974 )->value
['revision'];
976 $store = MediaWikiServices
::getInstance()->getRevisionStore();
977 $info = $store->getQueryInfo();
978 $row = $this->db
->selectRow(
981 [ 'rev_id' => $rev->getId() ],
986 $record = $store->newRevisionFromRow(
991 $this->assertRevisionRecordMatchesRevision( $rev, $record );
992 $this->assertSame( $text, $rev->getContent()->serialize() );
996 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
997 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
999 public function testNewRevisionFromRow_anonEdit() {
1000 $page = $this->getTestPage();
1001 $text = __METHOD__
. 'a-ä';
1002 /** @var Revision $rev */
1003 $rev = $page->doEditContent(
1004 new WikitextContent( $text ),
1006 )->value
['revision'];
1008 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1009 $record = $store->newRevisionFromRow(
1010 $this->revisionToRow( $rev ),
1014 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1015 $this->assertSame( $text, $rev->getContent()->serialize() );
1019 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1020 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1022 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1023 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1024 $this->overrideMwServices();
1025 $page = $this->getTestPage();
1026 $text = __METHOD__
. 'a-ä';
1027 /** @var Revision $rev */
1028 $rev = $page->doEditContent(
1029 new WikitextContent( $text ),
1031 )->value
['revision'];
1033 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1034 $record = $store->newRevisionFromRow(
1035 $this->revisionToRow( $rev ),
1039 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1040 $this->assertSame( $text, $rev->getContent()->serialize() );
1044 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1045 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1047 public function testNewRevisionFromRow_userEdit() {
1048 $page = $this->getTestPage();
1049 $text = __METHOD__
. 'b-ä';
1050 /** @var Revision $rev */
1051 $rev = $page->doEditContent(
1052 new WikitextContent( $text ),
1056 $this->getTestUser()->getUser()
1057 )->value
['revision'];
1059 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1060 $record = $store->newRevisionFromRow(
1061 $this->revisionToRow( $rev ),
1065 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1066 $this->assertSame( $text, $rev->getContent()->serialize() );
1070 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1071 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1073 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1074 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1075 $title = Title
::newFromText( __METHOD__
);
1076 $text = __METHOD__
. '-bä';
1077 $page = WikiPage
::factory( $title );
1078 /** @var Revision $orig */
1079 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1080 ->value
['revision'];
1081 $page->doDeleteArticle( __METHOD__
);
1083 $db = wfGetDB( DB_MASTER
);
1084 $arQuery = $store->getArchiveQueryInfo();
1086 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1087 __METHOD__
, [], $arQuery['joins']
1089 $this->assertTrue( is_object( $res ), 'query failed' );
1091 $row = $res->fetchObject();
1093 $record = $store->newRevisionFromArchiveRow( $row );
1095 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1096 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1100 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1102 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1103 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1104 $this->overrideMwServices();
1105 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1106 $title = Title
::newFromText( __METHOD__
);
1107 $text = __METHOD__
. '-bä';
1108 $page = WikiPage
::factory( $title );
1109 /** @var Revision $orig */
1110 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1111 ->value
['revision'];
1112 $page->doDeleteArticle( __METHOD__
);
1114 $db = wfGetDB( DB_MASTER
);
1115 $arQuery = $store->getArchiveQueryInfo();
1117 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1118 __METHOD__
, [], $arQuery['joins']
1120 $this->assertTrue( is_object( $res ), 'query failed' );
1122 $row = $res->fetchObject();
1124 $record = $store->newRevisionFromArchiveRow( $row );
1126 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1127 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1131 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1133 public function testNewRevisionFromArchiveRow_no_user() {
1134 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1138 'ar_page_id' => '2',
1139 'ar_namespace' => '0',
1140 'ar_title' => 'Something',
1142 'ar_text_id' => '47',
1143 'ar_timestamp' => '20180528192356',
1144 'ar_minor_edit' => '0',
1145 'ar_deleted' => '0',
1147 'ar_parent_id' => '0',
1148 'ar_sha1' => 'deadbeef',
1149 'ar_comment_text' => 'whatever',
1150 'ar_comment_data' => null,
1151 'ar_comment_cid' => null,
1153 'ar_user_text' => '', // this is the important bit
1155 'ar_content_format' => null,
1156 'ar_content_model' => null,
1159 \Wikimedia\
suppressWarnings();
1160 $record = $store->newRevisionFromArchiveRow( $row );
1161 \Wikimedia\
suppressWarnings( true );
1163 $this->assertInstanceOf( RevisionRecord
::class, $record );
1164 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1165 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1169 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1170 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1172 public function testNewRevisionFromRow_no_user() {
1173 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1174 $title = Title
::newFromText( __METHOD__
);
1179 'page_namespace' => '0',
1180 'page_title' => $title->getText(),
1181 'rev_text_id' => '47',
1182 'rev_timestamp' => '20180528192356',
1183 'rev_minor_edit' => '0',
1184 'rev_deleted' => '0',
1186 'rev_parent_id' => '0',
1187 'rev_sha1' => 'deadbeef',
1188 'rev_comment_text' => 'whatever',
1189 'rev_comment_data' => null,
1190 'rev_comment_cid' => null,
1192 'rev_user_text' => '', // this is the important bit
1193 'rev_actor' => null,
1194 'rev_content_format' => null,
1195 'rev_content_model' => null,
1198 \Wikimedia\
suppressWarnings();
1199 $record = $store->newRevisionFromRow( $row, 0, $title );
1200 \Wikimedia\
suppressWarnings( true );
1202 $this->assertNotNull( $record );
1203 $this->assertNotNull( $record->getUser() );
1204 $this->assertNotEmpty( $record->getUser()->getName() );
1208 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1210 public function testInsertRevisionOn_archive() {
1211 // This is a round trip test for deletion and undeletion of a
1212 // revision row via the archive table.
1214 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1215 $title = Title
::newFromText( __METHOD__
);
1217 $page = WikiPage
::factory( $title );
1218 /** @var Revision $origRev */
1219 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1220 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1221 ->value
['revision'];
1222 $orig = $origRev->getRevisionRecord();
1223 $page->doDeleteArticle( __METHOD__
);
1225 // re-create page, so we can later load revisions for it
1226 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1228 $db = wfGetDB( DB_MASTER
);
1229 $arQuery = $store->getArchiveQueryInfo();
1230 $row = $db->selectRow(
1231 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1232 __METHOD__
, [], $arQuery['joins']
1235 $this->assertNotFalse( $row, 'query failed' );
1237 $record = $store->newRevisionFromArchiveRow(
1241 [ 'page_id' => $title->getArticleID() ]
1244 $restored = $store->insertRevisionOn( $record, $db );
1246 // is the new revision correct?
1247 $this->assertRevisionCompleteness( $restored );
1248 $this->assertRevisionRecordsEqual( $record, $restored );
1250 // does the new revision use the original slot?
1251 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1252 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1253 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1254 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1255 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1256 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1258 // can we load it from the store?
1259 $loaded = $store->getRevisionById( $restored->getId() );
1260 $this->assertNotNull( $loaded );
1261 $this->assertRevisionCompleteness( $loaded );
1262 $this->assertRevisionRecordsEqual( $restored, $loaded );
1264 // can we find it directly in the database?
1265 $this->assertRevisionExistsInDatabase( $restored );
1269 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1271 public function testLoadRevisionFromId() {
1272 $title = Title
::newFromText( __METHOD__
);
1273 $page = WikiPage
::factory( $title );
1274 /** @var Revision $rev */
1275 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1276 ->value
['revision'];
1278 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1279 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1280 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1284 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1286 public function testLoadRevisionFromPageId() {
1287 $title = Title
::newFromText( __METHOD__
);
1288 $page = WikiPage
::factory( $title );
1289 /** @var Revision $rev */
1290 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1291 ->value
['revision'];
1293 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1294 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1295 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1299 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1301 public function testLoadRevisionFromTitle() {
1302 $title = Title
::newFromText( __METHOD__
);
1303 $page = WikiPage
::factory( $title );
1304 /** @var Revision $rev */
1305 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1306 ->value
['revision'];
1308 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1309 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1310 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1314 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1316 public function testLoadRevisionFromTimestamp() {
1317 $title = Title
::newFromText( __METHOD__
);
1318 $page = WikiPage
::factory( $title );
1319 /** @var Revision $revOne */
1320 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1321 ->value
['revision'];
1322 // Sleep to ensure different timestamps... )(evil)
1324 /** @var Revision $revTwo */
1325 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1326 ->value
['revision'];
1328 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1330 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1334 $store->loadRevisionFromTimestamp(
1335 wfGetDB( DB_MASTER
),
1337 $revOne->getTimestamp()
1342 $store->loadRevisionFromTimestamp(
1343 wfGetDB( DB_MASTER
),
1345 $revTwo->getTimestamp()
1351 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1353 public function testGetParentLengths() {
1354 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1355 /** @var Revision $revOne */
1356 $revOne = $page->doEditContent(
1357 new WikitextContent( __METHOD__
), __METHOD__
1358 )->value
['revision'];
1359 /** @var Revision $revTwo */
1360 $revTwo = $page->doEditContent(
1361 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1362 )->value
['revision'];
1364 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1367 $revOne->getId() => strlen( __METHOD__
),
1369 $store->listRevisionSizes(
1370 wfGetDB( DB_MASTER
),
1371 [ $revOne->getId() ]
1376 $revOne->getId() => strlen( __METHOD__
),
1377 $revTwo->getId() => strlen( __METHOD__
) +
1,
1379 $store->listRevisionSizes(
1380 wfGetDB( DB_MASTER
),
1381 [ $revOne->getId(), $revTwo->getId() ]
1387 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1389 public function testGetPreviousRevision() {
1390 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1391 /** @var Revision $revOne */
1392 $revOne = $page->doEditContent(
1393 new WikitextContent( __METHOD__
), __METHOD__
1394 )->value
['revision'];
1395 /** @var Revision $revTwo */
1396 $revTwo = $page->doEditContent(
1397 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1398 )->value
['revision'];
1400 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1402 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1406 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1411 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1413 public function testGetNextRevision() {
1414 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1415 /** @var Revision $revOne */
1416 $revOne = $page->doEditContent(
1417 new WikitextContent( __METHOD__
), __METHOD__
1418 )->value
['revision'];
1419 /** @var Revision $revTwo */
1420 $revTwo = $page->doEditContent(
1421 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1422 )->value
['revision'];
1424 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1427 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1430 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1434 public function provideNonHistoryRevision() {
1435 $title = Title
::newFromText( __METHOD__
);
1436 $rev = new MutableRevisionRecord( $title );
1439 $user = new UserIdentityValue( 7, 'Frank', 0 );
1440 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1443 'ar_rev_id' => 34567,
1446 'ar_minor_edit' => 0,
1447 'ar_timestamp' => '20180101020202',
1449 $slots = new RevisionSlots( [] );
1450 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1455 * @dataProvider provideNonHistoryRevision
1456 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1458 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1459 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1460 $this->assertNull( $store->getPreviousRevision( $rev ) );
1464 * @dataProvider provideNonHistoryRevision
1465 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1467 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1468 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1469 $this->assertNull( $store->getNextRevision( $rev ) );
1473 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1475 public function testGetTimestampFromId_found() {
1476 $page = $this->getTestPage();
1477 /** @var Revision $rev */
1478 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1479 ->value
['revision'];
1481 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1482 $result = $store->getTimestampFromId( $rev->getId() );
1484 $this->assertSame( $rev->getTimestamp(), $result );
1488 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1490 public function testGetTimestampFromId_notFound() {
1491 $page = $this->getTestPage();
1492 /** @var Revision $rev */
1493 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1494 ->value
['revision'];
1496 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1497 $result = $store->getTimestampFromId( $rev->getId() +
1 );
1499 $this->assertFalse( $result );
1503 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1505 public function testCountRevisionsByPageId() {
1506 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1507 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1511 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1513 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1516 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1518 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1521 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1526 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1528 public function testCountRevisionsByTitle() {
1529 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1530 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1534 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1536 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1539 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1541 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1544 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1549 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1551 public function testUserWasLastToEdit_false() {
1552 $sysop = $this->getTestSysop()->getUser();
1553 $page = $this->getTestPage();
1554 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1556 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1557 $result = $store->userWasLastToEdit(
1558 wfGetDB( DB_MASTER
),
1563 $this->assertFalse( $result );
1567 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1569 public function testUserWasLastToEdit_true() {
1570 $startTime = wfTimestampNow();
1571 $sysop = $this->getTestSysop()->getUser();
1572 $page = $this->getTestPage();
1573 $page->doEditContent(
1574 new WikitextContent( __METHOD__
),
1581 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1582 $result = $store->userWasLastToEdit(
1583 wfGetDB( DB_MASTER
),
1588 $this->assertTrue( $result );
1592 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1594 public function testGetKnownCurrentRevision() {
1595 $page = $this->getTestPage();
1596 /** @var Revision $rev */
1597 $rev = $page->doEditContent(
1598 new WikitextContent( __METHOD__
. 'b' ),
1602 $this->getTestUser()->getUser()
1603 )->value
['revision'];
1605 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1606 $record = $store->getKnownCurrentRevision(
1611 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1614 public function provideNewMutableRevisionFromArray() {
1615 yield
'Basic array, content object' => [
1619 'timestamp' => '20171017114835',
1620 'user_text' => '111.0.1.2',
1622 'minor_edit' => false,
1626 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1627 'comment' => 'Goat Comment!',
1628 'content' => new WikitextContent( 'Some Content' ),
1631 yield
'Basic array, serialized text' => [
1635 'timestamp' => '20171017114835',
1636 'user_text' => '111.0.1.2',
1638 'minor_edit' => false,
1642 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1643 'comment' => 'Goat Comment!',
1644 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1647 yield
'Basic array, serialized text, utf-8 flags' => [
1651 'timestamp' => '20171017114835',
1652 'user_text' => '111.0.1.2',
1654 'minor_edit' => false,
1658 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1659 'comment' => 'Goat Comment!',
1660 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1664 yield
'Basic array, with title' => [
1666 'title' => Title
::newFromText( 'SomeText' ),
1667 'timestamp' => '20171017114835',
1668 'user_text' => '111.0.1.2',
1670 'minor_edit' => false,
1674 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1675 'comment' => 'Goat Comment!',
1676 'content' => new WikitextContent( 'Some Content' ),
1679 yield
'Basic array, no user field' => [
1683 'timestamp' => '20171017114835',
1684 'user_text' => '111.0.1.3',
1685 'minor_edit' => false,
1689 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1690 'comment' => 'Goat Comment!',
1691 'content' => new WikitextContent( 'Some Content' ),
1697 * @dataProvider provideNewMutableRevisionFromArray
1698 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1700 public function testNewMutableRevisionFromArray( array $array ) {
1701 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1703 // HACK: if $array['page'] is given, make sure that the page exists
1704 if ( isset( $array['page'] ) ) {
1705 $t = Title
::newFromID( $array['page'] );
1706 if ( !$t ||
!$t->exists() ) {
1707 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1708 $info = $this->insertPage( $t );
1709 $array['page'] = $info['id'];
1713 $result = $store->newMutableRevisionFromArray( $array );
1715 if ( isset( $array['id'] ) ) {
1716 $this->assertSame( $array['id'], $result->getId() );
1718 if ( isset( $array['page'] ) ) {
1719 $this->assertSame( $array['page'], $result->getPageId() );
1721 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1722 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1723 if ( isset( $array['user'] ) ) {
1724 $this->assertSame( $array['user'], $result->getUser()->getId() );
1726 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1727 $this->assertSame( $array['deleted'], $result->getVisibility() );
1728 $this->assertSame( $array['len'], $result->getSize() );
1729 $this->assertSame( $array['parent_id'], $result->getParentId() );
1730 $this->assertSame( $array['sha1'], $result->getSha1() );
1731 $this->assertSame( $array['comment'], $result->getComment()->text
);
1732 if ( isset( $array['content'] ) ) {
1733 foreach ( $array['content'] as $role => $content ) {
1735 $result->getContent( $role )->equals( $content )
1738 } elseif ( isset( $array['text'] ) ) {
1739 $this->assertSame( $array['text'],
1740 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1741 } elseif ( isset( $array['content_format'] ) ) {
1743 $array['content_format'],
1744 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1746 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1751 * @dataProvider provideNewMutableRevisionFromArray
1752 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1754 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1755 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1756 $services = MediaWikiServices
::getInstance();
1757 $lb = $services->getDBLoadBalancer();
1758 $access = $services->getExternalStoreAccess();
1759 $blobStore = new SqlBlobStore( $lb, $access, $cache );
1760 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1762 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1763 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1764 ->disableOriginalConstructor()
1766 $factory->expects( $this->any() )
1767 ->method( 'newBlobStore' )
1768 ->willReturn( $blobStore );
1769 $factory->expects( $this->any() )
1770 ->method( 'newSqlBlobStore' )
1771 ->willReturn( $blobStore );
1773 $this->setService( 'BlobStoreFactory', $factory );
1775 $this->testNewMutableRevisionFromArray( $array );
1779 * Creates a new revision for testing caching behavior
1781 * @param WikiPage $page the page for the new revision
1782 * @param RevisionStore $store store object to use for creating the revision
1783 * @return bool|RevisionStoreRecord the revision created, or false if missing
1785 private function createRevisionStoreCacheRecord( $page, $store ) {
1786 $user = MediaWikiTestCase
::getMutableTestUser()->getUser();
1787 $updater = $page->newPageUpdater( $user );
1788 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( __METHOD__
) );
1789 $summary = CommentStoreComment
::newUnsavedComment( __METHOD__
);
1790 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
1791 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1795 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1797 public function testGetKnownCurrentRevision_userNameChange() {
1798 global $wgActorTableSchemaMigrationStage;
1800 $this->overrideMwServices();
1801 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1802 $this->setService( 'MainWANObjectCache', $cache );
1804 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1805 $page = $this->getNonexistingTestPage();
1806 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1808 // Grab the user name
1809 $userNameBefore = $rev->getUser()->getName();
1811 // Change the user name in the database, "behind the back" of the cache
1812 $newUserName = "Renamed $userNameBefore";
1813 $this->db
->update( 'revision',
1814 [ 'rev_user_text' => $newUserName ],
1815 [ 'rev_id' => $rev->getId() ] );
1816 $this->db
->update( 'user',
1817 [ 'user_name' => $newUserName ],
1818 [ 'user_id' => $rev->getUser()->getId() ] );
1819 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1820 $this->db
->update( 'actor',
1821 [ 'actor_name' => $newUserName ],
1822 [ 'actor_user' => $rev->getUser()->getId() ] );
1825 // Reload the revision and regrab the user name.
1826 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1827 $userNameAfter = $revAfter->getUser()->getName();
1829 // The two user names should be different.
1830 // If they are the same, we are seeing a cached value, which is bad.
1831 $this->assertNotSame( $userNameBefore, $userNameAfter );
1833 // This is implied by the above assertion, but explicitly check it, for completeness
1834 $this->assertSame( $newUserName, $userNameAfter );
1838 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1840 public function testGetKnownCurrentRevision_revDelete() {
1841 $this->overrideMwServices();
1842 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1843 $this->setService( 'MainWANObjectCache', $cache );
1845 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1846 $page = $this->getNonexistingTestPage();
1847 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1849 // Grab the deleted bitmask
1850 $deletedBefore = $rev->getVisibility();
1852 // Change the deleted bitmask in the database, "behind the back" of the cache
1853 $this->db
->update( 'revision',
1854 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1855 [ 'rev_id' => $rev->getId() ] );
1857 // Reload the revision and regrab the visibility flag.
1858 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1859 $deletedAfter = $revAfter->getVisibility();
1861 // The two deleted flags should be different.
1862 // If they are the same, we are seeing a cached value, which is bad.
1863 $this->assertNotSame( $deletedBefore, $deletedAfter );
1865 // This is implied by the above assertion, but explicitly check it, for completeness
1866 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1870 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1872 public function testNewRevisionFromRow_userNameChange() {
1873 global $wgActorTableSchemaMigrationStage;
1875 $page = $this->getTestPage();
1877 /** @var Revision $rev */
1878 $rev = $page->doEditContent(
1879 new WikitextContent( $text ),
1883 $this->getMutableTestUser()->getUser()
1884 )->value
['revision'];
1886 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1887 $record = $store->newRevisionFromRow(
1888 $this->revisionToRow( $rev ),
1893 // Grab the user name
1894 $userNameBefore = $record->getUser()->getName();
1896 // Change the user name in the database
1897 $newUserName = "Renamed $userNameBefore";
1898 $this->db
->update( 'revision',
1899 [ 'rev_user_text' => $newUserName ],
1900 [ 'rev_id' => $record->getId() ] );
1901 $this->db
->update( 'user',
1902 [ 'user_name' => $newUserName ],
1903 [ 'user_id' => $record->getUser()->getId() ] );
1904 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1905 $this->db
->update( 'actor',
1906 [ 'actor_name' => $newUserName ],
1907 [ 'actor_user' => $record->getUser()->getId() ] );
1910 // Reload the record, passing $fromCache as true to force fresh info from the db,
1911 // and regrab the user name
1912 $recordAfter = $store->newRevisionFromRow(
1913 $this->revisionToRow( $rev ),
1918 $userNameAfter = $recordAfter->getUser()->getName();
1920 // The two user names should be different.
1921 // If they are the same, we are seeing a cached value, which is bad.
1922 $this->assertNotSame( $userNameBefore, $userNameAfter );
1924 // This is implied by the above assertion, but explicitly check it, for completeness
1925 $this->assertSame( $newUserName, $userNameAfter );
1929 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1931 public function testNewRevisionFromRow_revDelete() {
1932 $page = $this->getTestPage();
1934 /** @var Revision $rev */
1935 $rev = $page->doEditContent(
1936 new WikitextContent( $text ),
1938 )->value
['revision'];
1940 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1941 $record = $store->newRevisionFromRow(
1942 $this->revisionToRow( $rev ),
1947 // Grab the deleted bitmask
1948 $deletedBefore = $record->getVisibility();
1950 // Change the deleted bitmask in the database
1951 $this->db
->update( 'revision',
1952 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1953 [ 'rev_id' => $record->getId() ] );
1955 // Reload the record, passing $fromCache as true to force fresh info from the db,
1956 // and regrab the deleted bitmask
1957 $recordAfter = $store->newRevisionFromRow(
1958 $this->revisionToRow( $rev ),
1963 $deletedAfter = $recordAfter->getVisibility();
1965 // The two deleted flags should be different, because we modified the database.
1966 $this->assertNotSame( $deletedBefore, $deletedAfter );
1968 // This is implied by the above assertion, but explicitly check it, for completeness
1969 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );