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
,
88 protected function addCoreDBData() {
89 // Blank out. This would fail with a modified schema, and we don't need it.
95 protected function getTestPageTitle() {
96 if ( $this->testPageTitle
) {
97 return $this->testPageTitle
;
100 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
101 return $this->testPageTitle
;
105 * @param string|null $pageTitle whether to force-create a new page
108 protected function getTestPage( $pageTitle = null ) {
109 if ( !is_null( $pageTitle ) && $this->testPage
) {
110 return $this->testPage
;
113 $title = is_null( $pageTitle ) ?
$this->getTestPageTitle() : Title
::newFromText( $pageTitle );
114 $page = WikiPage
::factory( $title );
116 if ( !$page->exists() ) {
117 // Make sure we don't write to the live db.
118 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
120 $user = static::getTestSysop()->getUser();
122 $page->doEditContent(
123 new WikitextContent( 'UTContent-' . __CLASS__
),
124 'UTPageSummary-' . __CLASS__
,
125 EDIT_NEW | EDIT_SUPPRESS_RC
,
131 if ( is_null( $pageTitle ) ) {
132 $this->testPage
= $page;
138 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
140 private function getLoadBalancerMock( array $server ) {
141 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
143 $lb = $this->getMockBuilder( LoadBalancer
::class )
144 ->setMethods( [ 'reallyOpenConnection' ] )
145 ->setConstructorArgs( [
146 [ 'servers' => [ $server ], 'localDomain' => $domain ]
150 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
151 function ( array $server, $dbNameOverride ) {
152 return $this->getDatabaseMock( $server );
160 * @return Database|PHPUnit_Framework_MockObject_MockObject
162 private function getDatabaseMock( array $params ) {
163 $db = $this->getMockBuilder( DatabaseSqlite
::class )
164 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
165 ->setConstructorArgs( [ $params ] )
168 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
169 $db->method( 'isOpen' )->willReturn( true );
174 public function provideDomainCheck() {
175 yield
[ false, 'test', '' ];
176 yield
[ 'test', 'test', '' ];
178 yield
[ false, 'test', 'foo_' ];
179 yield
[ 'test-foo_', 'test', 'foo_' ];
181 yield
[ false, 'dash-test', '' ];
182 yield
[ 'dash-test', 'dash-test', '' ];
184 yield
[ false, 'underscore_test', 'foo_' ];
185 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
189 * @dataProvider provideDomainCheck
190 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseDomain
192 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
195 'wgDBname' => $dbName,
196 'wgDBprefix' => $dbPrefix,
200 $loadBalancer = $this->getLoadBalancerMock(
203 'dbDirectory' => '*dummy*',
205 'password' => 'test',
213 'trxProfiler' => new TransactionProfiler(),
214 'connLogger' => new \Psr\Log\
NullLogger(),
215 'queryLogger' => new \Psr\Log\
NullLogger(),
216 'errorLogger' => function () {
218 'deprecationLogger' => function () {
222 'tablePrefix' => $dbPrefix,
225 $db = $loadBalancer->getConnection( DB_REPLICA
);
227 /** @var SqlBlobStore $blobStore */
228 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
229 ->disableOriginalConstructor()
232 $store = new RevisionStore(
235 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
236 MediaWikiServices
::getInstance()->getCommentStore(),
237 MediaWikiServices
::getInstance()->getContentModelStore(),
238 MediaWikiServices
::getInstance()->getSlotRoleStore(),
239 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
240 $this->getMcrMigrationStage(),
241 MediaWikiServices
::getInstance()->getActorMigration(),
245 $count = $store->countRevisionsByPageId( $db, 0 );
247 // Dummy check to make PhpUnit happy. We are really only interested in
248 // countRevisionsByPageId not failing due to the DB domain check.
249 $this->assertSame( 0, $count );
252 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
253 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
254 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
255 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
256 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
259 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
261 $r1->getPageAsLinkTarget()->getNamespace(),
262 $r2->getPageAsLinkTarget()->getNamespace()
266 $r1->getPageAsLinkTarget()->getText(),
267 $r2->getPageAsLinkTarget()->getText()
270 if ( $r1->getParentId() ) {
271 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
274 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
275 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
276 $this->assertEquals( $r1->getComment(), $r2->getComment() );
277 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
278 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
279 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
280 $this->assertEquals( $r1->getSize(), $r2->getSize() );
281 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
282 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
283 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
284 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
285 foreach ( $r1->getSlotRoles() as $role ) {
286 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
287 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
290 RevisionRecord
::DELETED_TEXT
,
291 RevisionRecord
::DELETED_COMMENT
,
292 RevisionRecord
::DELETED_USER
,
293 RevisionRecord
::DELETED_RESTRICTED
,
295 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
299 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
300 $this->assertSame( $s1->getRole(), $s2->getRole() );
301 $this->assertSame( $s1->getModel(), $s2->getModel() );
302 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
303 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
304 $this->assertSame( $s1->getSize(), $s2->getSize() );
305 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
307 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
308 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
311 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
312 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
313 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
314 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
316 foreach ( $r->getSlotRoles() as $role ) {
317 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
321 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
322 $this->assertTrue( $slot->hasAddress() );
323 $this->assertSame( $r->getId(), $slot->getRevision() );
325 $this->assertInstanceOf( Content
::class, $slot->getContent() );
329 * @param mixed[] $details
331 * @return RevisionRecord
333 private function getRevisionRecordFromDetailsArray( $details = [] ) {
334 // Convert some values that can't be provided by dataProviders
335 if ( isset( $details['user'] ) && $details['user'] === true ) {
336 $details['user'] = $this->getTestUser()->getUser();
338 if ( isset( $details['page'] ) && $details['page'] === true ) {
339 $details['page'] = $this->getTestPage()->getId();
341 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
342 $details['parent'] = $this->getTestPage()->getLatest();
345 // Create the RevisionRecord with any available data
346 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
347 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
348 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
349 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
350 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
351 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
352 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
353 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
354 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
355 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
356 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
357 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
359 if ( isset( $details['content'] ) ) {
360 foreach ( $details['content'] as $role => $content ) {
361 $rev->setContent( $role, $content );
368 public function provideInsertRevisionOn_successes() {
369 yield
'Bare minimum revision insertion' => [
371 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
373 'comment' => $this->getRandomCommentStoreComment(),
374 'timestamp' => '20171117010101',
378 yield
'Detailed revision insertion' => [
380 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
383 'comment' => $this->getRandomCommentStoreComment(),
384 'timestamp' => '20171117010101',
387 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
392 protected function getRandomCommentStoreComment() {
393 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
397 * @dataProvider provideInsertRevisionOn_successes
398 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
399 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
400 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
402 public function testInsertRevisionOn_successes(
403 array $revDetails = []
405 $title = $this->getTestPageTitle();
406 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
408 $store = MediaWikiServices
::getInstance()->getRevisionStore();
409 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
411 // is the new revision correct?
412 $this->assertRevisionCompleteness( $return );
413 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
414 $this->assertRevisionRecordsEqual( $rev, $return );
416 // can we load it from the store?
417 $loaded = $store->getRevisionById( $return->getId() );
418 $this->assertRevisionCompleteness( $loaded );
419 $this->assertRevisionRecordsEqual( $return, $loaded );
421 // can we find it directly in the database?
422 $this->assertRevisionExistsInDatabase( $return );
425 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
426 $row = $this->revisionToRow( new Revision( $rev ), [] );
428 // unset nulled fields
429 unset( $row->rev_content_model
);
430 unset( $row->rev_content_format
);
433 unset( $row->rev_comment_text
);
434 unset( $row->rev_comment_data
);
435 unset( $row->rev_comment_cid
);
436 unset( $row->rev_comment_id
);
438 $store = MediaWikiServices
::getInstance()->getRevisionStore();
439 $queryInfo = $store->getQueryInfo( [ 'user' ] );
441 $row = get_object_vars( $row );
443 // Use aliased fields from $queryInfo, e.g. rev_user
444 $keys = array_keys( $row );
445 $keys = array_combine( $keys, $keys );
446 $fields = array_intersect_key( $queryInfo['fields'], $keys ) +
$keys;
448 // assertSelect() fails unless the orders match.
453 $queryInfo['tables'],
455 [ 'rev_id' => $rev->getId() ],
456 [ array_values( $row ) ],
463 * @param SlotRecord $a
464 * @param SlotRecord $b
466 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
467 // Assert that the same blob address has been used.
468 $this->assertSame( $a->getAddress(), $b->getAddress() );
472 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
474 public function testInsertRevisionOn_blobAddressExists() {
475 $title = $this->getTestPageTitle();
477 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
479 'comment' => $this->getRandomCommentStoreComment(),
480 'timestamp' => '20171117010101',
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 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
598 $page = WikiPage
::factory( $title );
600 if ( !$page->exists() ) {
601 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
604 $revDetails['page'] = $page->getId();
605 $revDetails['timestamp'] = wfTimestampNow();
606 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
607 $revDetails['user'] = $user;
609 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
610 $store = MediaWikiServices
::getInstance()->getRevisionStore();
612 $dbw = wfGetDB( DB_MASTER
);
613 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
614 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
616 $record = $store->newNullRevision(
617 wfGetDB( DB_MASTER
),
624 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
625 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
626 $this->assertEquals( $comment, $record->getComment() );
627 $this->assertEquals( $minor, $record->isMinor() );
628 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
629 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
631 $this->assertArrayEquals(
632 $baseRev->getSlotRoles(),
633 $record->getSlotRoles()
636 foreach ( $baseRev->getSlotRoles() as $role ) {
637 $parentSlot = $baseRev->getSlot( $role );
638 $slot = $record->getSlot( $role );
640 $this->assertTrue( $slot->isInherited(), 'isInherited' );
641 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
642 $this->assertSameSlotContent( $parentSlot, $slot );
647 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
649 public function testNewNullRevision_nonExistingTitle() {
650 $store = MediaWikiServices
::getInstance()->getRevisionStore();
651 $record = $store->newNullRevision(
652 wfGetDB( DB_MASTER
),
653 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
654 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
656 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
658 $this->assertNull( $record );
662 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
664 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
665 $page = $this->getTestPage();
666 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
667 /** @var Revision $rev */
668 $rev = $status->value
['revision'];
670 $store = MediaWikiServices
::getInstance()->getRevisionStore();
671 $revisionRecord = $store->getRevisionById( $rev->getId() );
672 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
674 $this->assertGreaterThan( 0, $result );
676 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
682 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
684 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
685 // This assumes that sysops are auto patrolled
686 $sysop = $this->getTestSysop()->getUser();
687 $page = $this->getTestPage();
688 $status = $page->doEditContent(
689 new WikitextContent( __METHOD__
),
695 /** @var Revision $rev */
696 $rev = $status->value
['revision'];
698 $store = MediaWikiServices
::getInstance()->getRevisionStore();
699 $revisionRecord = $store->getRevisionById( $rev->getId() );
700 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
702 $this->assertSame( 0, $result );
706 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
708 public function testGetRecentChange() {
709 $page = $this->getTestPage();
710 $content = new WikitextContent( __METHOD__
);
711 $status = $page->doEditContent( $content, __METHOD__
);
712 /** @var Revision $rev */
713 $rev = $status->value
['revision'];
715 $store = MediaWikiServices
::getInstance()->getRevisionStore();
716 $revRecord = $store->getRevisionById( $rev->getId() );
717 $recentChange = $store->getRecentChange( $revRecord );
719 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
720 $this->assertEquals( $rev->getRecentChange(), $recentChange );
724 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
726 public function testGetRevisionById() {
727 $page = $this->getTestPage();
728 $content = new WikitextContent( __METHOD__
);
729 $status = $page->doEditContent( $content, __METHOD__
);
730 /** @var Revision $rev */
731 $rev = $status->value
['revision'];
733 $store = MediaWikiServices
::getInstance()->getRevisionStore();
734 $revRecord = $store->getRevisionById( $rev->getId() );
736 $this->assertSame( $rev->getId(), $revRecord->getId() );
737 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
738 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
742 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
744 public function testGetRevisionByTitle() {
745 $page = $this->getTestPage();
746 $content = new WikitextContent( __METHOD__
);
747 $status = $page->doEditContent( $content, __METHOD__
);
748 /** @var Revision $rev */
749 $rev = $status->value
['revision'];
751 $store = MediaWikiServices
::getInstance()->getRevisionStore();
752 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
754 $this->assertSame( $rev->getId(), $revRecord->getId() );
755 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
756 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
760 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
762 public function testGetRevisionByPageId() {
763 $page = $this->getTestPage();
764 $content = new WikitextContent( __METHOD__
);
765 $status = $page->doEditContent( $content, __METHOD__
);
766 /** @var Revision $rev */
767 $rev = $status->value
['revision'];
769 $store = MediaWikiServices
::getInstance()->getRevisionStore();
770 $revRecord = $store->getRevisionByPageId( $page->getId() );
772 $this->assertSame( $rev->getId(), $revRecord->getId() );
773 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
774 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
778 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
780 public function testGetRevisionByTimestamp() {
781 // Make sure there is 1 second between the last revision and the rev we create...
782 // Otherwise we might not get the correct revision and the test may fail...
784 $page = $this->getTestPage();
786 $content = new WikitextContent( __METHOD__
);
787 $status = $page->doEditContent( $content, __METHOD__
);
788 /** @var Revision $rev */
789 $rev = $status->value
['revision'];
791 $store = MediaWikiServices
::getInstance()->getRevisionStore();
792 $revRecord = $store->getRevisionByTimestamp(
797 $this->assertSame( $rev->getId(), $revRecord->getId() );
798 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
799 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
802 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
803 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
804 $page = WikiPage
::factory( $rev->getTitle() );
807 'rev_id' => (string)$rev->getId(),
808 'rev_page' => (string)$rev->getPage(),
809 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
810 'rev_user_text' => (string)$rev->getUserText(),
811 'rev_user' => (string)$rev->getUser() ?
: null,
812 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
813 'rev_deleted' => (string)$rev->getVisibility(),
814 'rev_len' => (string)$rev->getSize(),
815 'rev_parent_id' => (string)$rev->getParentId(),
816 'rev_sha1' => (string)$rev->getSha1(),
819 if ( in_array( 'page', $options ) ) {
821 'page_namespace' => (string)$page->getTitle()->getNamespace(),
822 'page_title' => $page->getTitle()->getDBkey(),
823 'page_id' => (string)$page->getId(),
824 'page_latest' => (string)$page->getLatest(),
825 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
826 'page_len' => (string)$page->getContent()->getSize(),
830 if ( in_array( 'user', $options ) ) {
832 'user_name' => (string)$rev->getUserText(),
836 if ( in_array( 'comment', $options ) ) {
838 'rev_comment_text' => $rev->getComment(),
839 'rev_comment_data' => null,
840 'rev_comment_cid' => null,
844 if ( $rev->getId() ) {
846 'rev_id' => (string)$rev->getId(),
850 return (object)$fields;
853 protected function assertRevisionRecordMatchesRevision(
855 RevisionRecord
$record
857 $this->assertSame( $rev->getId(), $record->getId() );
858 $this->assertSame( $rev->getPage(), $record->getPageId() );
859 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
860 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
861 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
862 $this->assertSame( $rev->isMinor(), $record->isMinor() );
863 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
864 $this->assertSame( $rev->getSize(), $record->getSize() );
866 * @note As of MW 1.31, the database schema allows the parent ID to be
867 * NULL to indicate that it is unknown.
869 $expectedParent = $rev->getParentId();
870 if ( $expectedParent === null ) {
873 $this->assertSame( $expectedParent, $record->getParentId() );
874 $this->assertSame( $rev->getSha1(), $record->getSha1() );
875 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
876 $this->assertSame( $rev->getContentFormat(),
877 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
878 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
879 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
881 $revRec = $rev->getRevisionRecord();
882 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
883 $recMain = $record->getSlot( SlotRecord
::MAIN
);
885 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
886 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
887 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
889 if ( $revMain->hasOrigin() ) {
890 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
893 if ( $revMain->hasAddress() ) {
894 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
897 if ( $revMain->hasContentId() ) {
898 // XXX: the content ID value is ill-defined when SCHEMA_COMPAT_WRITE_BOTH and
899 // SCHEMA_COMPAT_READ_OLD is set, since revision insertion will report the
900 // content ID used with the new schema, while loading the revision from the
901 // old schema will report an emulated ID.
902 if ( $this->getMcrMigrationStage() & SCHEMA_COMPAT_READ_NEW
) {
903 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
909 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
910 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
912 public function testNewRevisionFromRowAndSlot_getQueryInfo() {
913 $page = $this->getTestPage();
914 $text = __METHOD__
. 'o-รถ';
915 /** @var Revision $rev */
916 $rev = $page->doEditContent(
917 new WikitextContent( $text ),
919 )->value
['revision'];
921 $store = MediaWikiServices
::getInstance()->getRevisionStore();
922 $info = $store->getQueryInfo();
923 $row = $this->db
->selectRow(
926 [ 'rev_id' => $rev->getId() ],
932 $info = $store->getSlotsQueryInfo( [ 'content' ] );
933 $slotRows = $this->db
->select(
936 $this->getSlotRevisionConditions( $rev->getId() ),
942 $record = $store->newRevisionFromRowAndSlots(
944 iterator_to_array( $slotRows ),
948 $this->assertRevisionRecordMatchesRevision( $rev, $record );
949 $this->assertSame( $text, $rev->getContent()->serialize() );
953 * Conditions to use together with getSlotsQueryInfo() when selecting slot rows for a given
958 abstract protected function getSlotRevisionConditions( $revId );
961 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
962 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
963 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
965 public function testNewRevisionFromRow_getQueryInfo() {
966 $page = $this->getTestPage();
967 $text = __METHOD__
. 'a-รค';
968 /** @var Revision $rev */
969 $rev = $page->doEditContent(
970 new WikitextContent( $text ),
972 )->value
['revision'];
974 $store = MediaWikiServices
::getInstance()->getRevisionStore();
975 $info = $store->getQueryInfo();
976 $row = $this->db
->selectRow(
979 [ 'rev_id' => $rev->getId() ],
984 $record = $store->newRevisionFromRow(
989 $this->assertRevisionRecordMatchesRevision( $rev, $record );
990 $this->assertSame( $text, $rev->getContent()->serialize() );
994 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
995 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
997 public function testNewRevisionFromRow_anonEdit() {
998 $page = $this->getTestPage();
999 $text = __METHOD__
. 'a-รค';
1000 /** @var Revision $rev */
1001 $rev = $page->doEditContent(
1002 new WikitextContent( $text ),
1004 )->value
['revision'];
1006 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1007 $record = $store->newRevisionFromRow(
1008 $this->revisionToRow( $rev ),
1012 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1013 $this->assertSame( $text, $rev->getContent()->serialize() );
1017 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1018 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1020 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
1021 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1022 $page = $this->getTestPage();
1023 $text = __METHOD__
. 'a-รค';
1024 /** @var Revision $rev */
1025 $rev = $page->doEditContent(
1026 new WikitextContent( $text ),
1028 )->value
['revision'];
1030 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1031 $record = $store->newRevisionFromRow(
1032 $this->revisionToRow( $rev ),
1036 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1037 $this->assertSame( $text, $rev->getContent()->serialize() );
1041 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1042 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1044 public function testNewRevisionFromRow_userEdit() {
1045 $page = $this->getTestPage();
1046 $text = __METHOD__
. 'b-รค';
1047 /** @var Revision $rev */
1048 $rev = $page->doEditContent(
1049 new WikitextContent( $text ),
1053 $this->getTestUser()->getUser()
1054 )->value
['revision'];
1056 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1057 $record = $store->newRevisionFromRow(
1058 $this->revisionToRow( $rev ),
1062 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1063 $this->assertSame( $text, $rev->getContent()->serialize() );
1067 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1068 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1070 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1071 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1072 $title = Title
::newFromText( __METHOD__
);
1073 $text = __METHOD__
. '-bรค';
1074 $page = WikiPage
::factory( $title );
1075 /** @var Revision $orig */
1076 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1077 ->value
['revision'];
1078 $page->doDeleteArticle( __METHOD__
);
1080 $db = wfGetDB( DB_MASTER
);
1081 $arQuery = $store->getArchiveQueryInfo();
1083 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1084 __METHOD__
, [], $arQuery['joins']
1086 $this->assertTrue( is_object( $res ), 'query failed' );
1088 $row = $res->fetchObject();
1090 $record = $store->newRevisionFromArchiveRow( $row );
1092 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1093 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1097 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1099 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1100 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1101 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1102 $title = Title
::newFromText( __METHOD__
);
1103 $text = __METHOD__
. '-bรค';
1104 $page = WikiPage
::factory( $title );
1105 /** @var Revision $orig */
1106 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1107 ->value
['revision'];
1108 $page->doDeleteArticle( __METHOD__
);
1110 $db = wfGetDB( DB_MASTER
);
1111 $arQuery = $store->getArchiveQueryInfo();
1113 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1114 __METHOD__
, [], $arQuery['joins']
1116 $this->assertTrue( is_object( $res ), 'query failed' );
1118 $row = $res->fetchObject();
1120 $record = $store->newRevisionFromArchiveRow( $row );
1122 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1123 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1127 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1129 public function testNewRevisionFromArchiveRow_no_user() {
1130 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1134 'ar_page_id' => '2',
1135 'ar_namespace' => '0',
1136 'ar_title' => 'Something',
1138 'ar_text_id' => '47',
1139 'ar_timestamp' => '20180528192356',
1140 'ar_minor_edit' => '0',
1141 'ar_deleted' => '0',
1143 'ar_parent_id' => '0',
1144 'ar_sha1' => 'deadbeef',
1145 'ar_comment_text' => 'whatever',
1146 'ar_comment_data' => null,
1147 'ar_comment_cid' => null,
1149 'ar_user_text' => '', // this is the important bit
1151 'ar_content_format' => null,
1152 'ar_content_model' => null,
1155 \Wikimedia\
suppressWarnings();
1156 $record = $store->newRevisionFromArchiveRow( $row );
1157 \Wikimedia\
suppressWarnings( true );
1159 $this->assertInstanceOf( RevisionRecord
::class, $record );
1160 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1161 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1165 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1166 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRowAndSlots
1168 public function testNewRevisionFromRow_no_user() {
1169 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1170 $title = Title
::newFromText( __METHOD__
);
1175 'page_namespace' => '0',
1176 'page_title' => $title->getText(),
1177 'rev_text_id' => '47',
1178 'rev_timestamp' => '20180528192356',
1179 'rev_minor_edit' => '0',
1180 'rev_deleted' => '0',
1182 'rev_parent_id' => '0',
1183 'rev_sha1' => 'deadbeef',
1184 'rev_comment_text' => 'whatever',
1185 'rev_comment_data' => null,
1186 'rev_comment_cid' => null,
1188 'rev_user_text' => '', // this is the important bit
1189 'rev_actor' => null,
1190 'rev_content_format' => null,
1191 'rev_content_model' => null,
1194 \Wikimedia\
suppressWarnings();
1195 $record = $store->newRevisionFromRow( $row, 0, $title );
1196 \Wikimedia\
suppressWarnings( true );
1198 $this->assertNotNull( $record );
1199 $this->assertNotNull( $record->getUser() );
1200 $this->assertNotEmpty( $record->getUser()->getName() );
1204 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1206 public function testInsertRevisionOn_archive() {
1207 // This is a round trip test for deletion and undeletion of a
1208 // revision row via the archive table.
1210 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1211 $title = Title
::newFromText( __METHOD__
);
1213 $page = WikiPage
::factory( $title );
1214 /** @var Revision $origRev */
1215 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1216 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1217 ->value
['revision'];
1218 $orig = $origRev->getRevisionRecord();
1219 $page->doDeleteArticle( __METHOD__
);
1221 // re-create page, so we can later load revisions for it
1222 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1224 $db = wfGetDB( DB_MASTER
);
1225 $arQuery = $store->getArchiveQueryInfo();
1226 $row = $db->selectRow(
1227 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1228 __METHOD__
, [], $arQuery['joins']
1231 $this->assertNotFalse( $row, 'query failed' );
1233 $record = $store->newRevisionFromArchiveRow(
1237 [ 'page_id' => $title->getArticleID() ]
1240 $restored = $store->insertRevisionOn( $record, $db );
1242 // is the new revision correct?
1243 $this->assertRevisionCompleteness( $restored );
1244 $this->assertRevisionRecordsEqual( $record, $restored );
1246 // does the new revision use the original slot?
1247 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1248 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1249 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1250 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1251 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1252 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1254 // can we load it from the store?
1255 $loaded = $store->getRevisionById( $restored->getId() );
1256 $this->assertNotNull( $loaded );
1257 $this->assertRevisionCompleteness( $loaded );
1258 $this->assertRevisionRecordsEqual( $restored, $loaded );
1260 // can we find it directly in the database?
1261 $this->assertRevisionExistsInDatabase( $restored );
1265 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1267 public function testLoadRevisionFromId() {
1268 $title = Title
::newFromText( __METHOD__
);
1269 $page = WikiPage
::factory( $title );
1270 /** @var Revision $rev */
1271 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1272 ->value
['revision'];
1274 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1275 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1276 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1280 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1282 public function testLoadRevisionFromPageId() {
1283 $title = Title
::newFromText( __METHOD__
);
1284 $page = WikiPage
::factory( $title );
1285 /** @var Revision $rev */
1286 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1287 ->value
['revision'];
1289 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1290 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1291 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1295 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1297 public function testLoadRevisionFromTitle() {
1298 $title = Title
::newFromText( __METHOD__
);
1299 $page = WikiPage
::factory( $title );
1300 /** @var Revision $rev */
1301 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1302 ->value
['revision'];
1304 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1305 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1306 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1310 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1312 public function testLoadRevisionFromTimestamp() {
1313 $title = Title
::newFromText( __METHOD__
);
1314 $page = WikiPage
::factory( $title );
1315 /** @var Revision $revOne */
1316 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1317 ->value
['revision'];
1318 // Sleep to ensure different timestamps... )(evil)
1320 /** @var Revision $revTwo */
1321 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1322 ->value
['revision'];
1324 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1326 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1330 $store->loadRevisionFromTimestamp(
1331 wfGetDB( DB_MASTER
),
1333 $revOne->getTimestamp()
1338 $store->loadRevisionFromTimestamp(
1339 wfGetDB( DB_MASTER
),
1341 $revTwo->getTimestamp()
1347 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1349 public function testGetParentLengths() {
1350 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1351 /** @var Revision $revOne */
1352 $revOne = $page->doEditContent(
1353 new WikitextContent( __METHOD__
), __METHOD__
1354 )->value
['revision'];
1355 /** @var Revision $revTwo */
1356 $revTwo = $page->doEditContent(
1357 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1358 )->value
['revision'];
1360 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1363 $revOne->getId() => strlen( __METHOD__
),
1365 $store->listRevisionSizes(
1366 wfGetDB( DB_MASTER
),
1367 [ $revOne->getId() ]
1372 $revOne->getId() => strlen( __METHOD__
),
1373 $revTwo->getId() => strlen( __METHOD__
) +
1,
1375 $store->listRevisionSizes(
1376 wfGetDB( DB_MASTER
),
1377 [ $revOne->getId(), $revTwo->getId() ]
1383 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1385 public function testGetPreviousRevision() {
1386 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1387 /** @var Revision $revOne */
1388 $revOne = $page->doEditContent(
1389 new WikitextContent( __METHOD__
), __METHOD__
1390 )->value
['revision'];
1391 /** @var Revision $revTwo */
1392 $revTwo = $page->doEditContent(
1393 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1394 )->value
['revision'];
1396 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1398 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1402 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1407 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1409 public function testGetNextRevision() {
1410 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1411 /** @var Revision $revOne */
1412 $revOne = $page->doEditContent(
1413 new WikitextContent( __METHOD__
), __METHOD__
1414 )->value
['revision'];
1415 /** @var Revision $revTwo */
1416 $revTwo = $page->doEditContent(
1417 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1418 )->value
['revision'];
1420 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1423 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1426 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1430 public function provideNonHistoryRevision() {
1431 $title = Title
::newFromText( __METHOD__
);
1432 $rev = new MutableRevisionRecord( $title );
1435 $user = new UserIdentityValue( 7, 'Frank', 0 );
1436 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1439 'ar_rev_id' => 34567,
1442 'ar_minor_edit' => 0,
1443 'ar_timestamp' => '20180101020202',
1445 $slots = new RevisionSlots( [] );
1446 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1451 * @dataProvider provideNonHistoryRevision
1452 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1454 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1455 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1456 $this->assertNull( $store->getPreviousRevision( $rev ) );
1460 * @dataProvider provideNonHistoryRevision
1461 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1463 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1464 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1465 $this->assertNull( $store->getNextRevision( $rev ) );
1469 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1471 public function testGetTimestampFromId_found() {
1472 $page = $this->getTestPage();
1473 /** @var Revision $rev */
1474 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1475 ->value
['revision'];
1477 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1478 $result = $store->getTimestampFromId( $rev->getId() );
1480 $this->assertSame( $rev->getTimestamp(), $result );
1484 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1486 public function testGetTimestampFromId_notFound() {
1487 $page = $this->getTestPage();
1488 /** @var Revision $rev */
1489 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1490 ->value
['revision'];
1492 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1493 $result = $store->getTimestampFromId( $rev->getId() +
1 );
1495 $this->assertFalse( $result );
1499 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1501 public function testCountRevisionsByPageId() {
1502 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1503 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1507 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1509 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1512 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1514 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1517 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1522 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1524 public function testCountRevisionsByTitle() {
1525 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1526 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1530 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1532 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1535 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1537 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1540 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1545 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1547 public function testUserWasLastToEdit_false() {
1548 $sysop = $this->getTestSysop()->getUser();
1549 $page = $this->getTestPage();
1550 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1552 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1553 $result = $store->userWasLastToEdit(
1554 wfGetDB( DB_MASTER
),
1559 $this->assertFalse( $result );
1563 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1565 public function testUserWasLastToEdit_true() {
1566 $startTime = wfTimestampNow();
1567 $sysop = $this->getTestSysop()->getUser();
1568 $page = $this->getTestPage();
1569 $page->doEditContent(
1570 new WikitextContent( __METHOD__
),
1577 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1578 $result = $store->userWasLastToEdit(
1579 wfGetDB( DB_MASTER
),
1584 $this->assertTrue( $result );
1588 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1590 public function testGetKnownCurrentRevision() {
1591 $page = $this->getTestPage();
1592 /** @var Revision $rev */
1593 $rev = $page->doEditContent(
1594 new WikitextContent( __METHOD__
. 'b' ),
1598 $this->getTestUser()->getUser()
1599 )->value
['revision'];
1601 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1602 $record = $store->getKnownCurrentRevision(
1607 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1610 public function provideNewMutableRevisionFromArray() {
1611 yield
'Basic array, content object' => [
1615 'timestamp' => '20171017114835',
1616 'user_text' => '111.0.1.2',
1618 'minor_edit' => false,
1622 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1623 'comment' => 'Goat Comment!',
1624 'content' => new WikitextContent( 'Some Content' ),
1627 yield
'Basic array, serialized text' => [
1631 'timestamp' => '20171017114835',
1632 'user_text' => '111.0.1.2',
1634 'minor_edit' => false,
1638 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1639 'comment' => 'Goat Comment!',
1640 'text' => ( new WikitextContent( 'Sรถme Content' ) )->serialize(),
1643 yield
'Basic array, serialized text, utf-8 flags' => [
1647 'timestamp' => '20171017114835',
1648 'user_text' => '111.0.1.2',
1650 'minor_edit' => false,
1654 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1655 'comment' => 'Goat Comment!',
1656 'text' => ( new WikitextContent( 'Sรถme Content' ) )->serialize(),
1660 yield
'Basic array, with title' => [
1662 'title' => Title
::newFromText( 'SomeText' ),
1663 'timestamp' => '20171017114835',
1664 'user_text' => '111.0.1.2',
1666 'minor_edit' => false,
1670 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1671 'comment' => 'Goat Comment!',
1672 'content' => new WikitextContent( 'Some Content' ),
1675 yield
'Basic array, no user field' => [
1679 'timestamp' => '20171017114835',
1680 'user_text' => '111.0.1.3',
1681 'minor_edit' => false,
1685 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1686 'comment' => 'Goat Comment!',
1687 'content' => new WikitextContent( 'Some Content' ),
1693 * @dataProvider provideNewMutableRevisionFromArray
1694 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1696 public function testNewMutableRevisionFromArray( array $array ) {
1697 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1699 // HACK: if $array['page'] is given, make sure that the page exists
1700 if ( isset( $array['page'] ) ) {
1701 $t = Title
::newFromID( $array['page'] );
1702 if ( !$t ||
!$t->exists() ) {
1703 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1704 $info = $this->insertPage( $t );
1705 $array['page'] = $info['id'];
1709 $result = $store->newMutableRevisionFromArray( $array );
1711 if ( isset( $array['id'] ) ) {
1712 $this->assertSame( $array['id'], $result->getId() );
1714 if ( isset( $array['page'] ) ) {
1715 $this->assertSame( $array['page'], $result->getPageId() );
1717 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1718 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1719 if ( isset( $array['user'] ) ) {
1720 $this->assertSame( $array['user'], $result->getUser()->getId() );
1722 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1723 $this->assertSame( $array['deleted'], $result->getVisibility() );
1724 $this->assertSame( $array['len'], $result->getSize() );
1725 $this->assertSame( $array['parent_id'], $result->getParentId() );
1726 $this->assertSame( $array['sha1'], $result->getSha1() );
1727 $this->assertSame( $array['comment'], $result->getComment()->text
);
1728 if ( isset( $array['content'] ) ) {
1729 foreach ( $array['content'] as $role => $content ) {
1731 $result->getContent( $role )->equals( $content )
1734 } elseif ( isset( $array['text'] ) ) {
1735 $this->assertSame( $array['text'],
1736 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1737 } elseif ( isset( $array['content_format'] ) ) {
1739 $array['content_format'],
1740 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1742 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1747 * @dataProvider provideNewMutableRevisionFromArray
1748 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1750 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1751 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1752 $services = MediaWikiServices
::getInstance();
1753 $lb = $services->getDBLoadBalancer();
1754 $access = $services->getExternalStoreAccess();
1755 $blobStore = new SqlBlobStore( $lb, $access, $cache );
1756 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1758 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1759 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1760 ->disableOriginalConstructor()
1762 $factory->expects( $this->any() )
1763 ->method( 'newBlobStore' )
1764 ->willReturn( $blobStore );
1765 $factory->expects( $this->any() )
1766 ->method( 'newSqlBlobStore' )
1767 ->willReturn( $blobStore );
1769 $this->setService( 'BlobStoreFactory', $factory );
1771 $this->testNewMutableRevisionFromArray( $array );
1775 * Creates a new revision for testing caching behavior
1777 * @param WikiPage $page the page for the new revision
1778 * @param RevisionStore $store store object to use for creating the revision
1779 * @return bool|RevisionStoreRecord the revision created, or false if missing
1781 private function createRevisionStoreCacheRecord( $page, $store ) {
1782 $user = MediaWikiTestCase
::getMutableTestUser()->getUser();
1783 $updater = $page->newPageUpdater( $user );
1784 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( __METHOD__
) );
1785 $summary = CommentStoreComment
::newUnsavedComment( __METHOD__
);
1786 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
1787 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1791 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1793 public function testGetKnownCurrentRevision_userNameChange() {
1794 global $wgActorTableSchemaMigrationStage;
1796 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1797 $this->setService( 'MainWANObjectCache', $cache );
1799 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1800 $page = $this->getNonexistingTestPage();
1801 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1803 // Grab the user name
1804 $userNameBefore = $rev->getUser()->getName();
1806 // Change the user name in the database, "behind the back" of the cache
1807 $newUserName = "Renamed $userNameBefore";
1808 $this->db
->update( 'revision',
1809 [ 'rev_user_text' => $newUserName ],
1810 [ 'rev_id' => $rev->getId() ] );
1811 $this->db
->update( 'user',
1812 [ 'user_name' => $newUserName ],
1813 [ 'user_id' => $rev->getUser()->getId() ] );
1814 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1815 $this->db
->update( 'actor',
1816 [ 'actor_name' => $newUserName ],
1817 [ 'actor_user' => $rev->getUser()->getId() ] );
1820 // Reload the revision and regrab the user name.
1821 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1822 $userNameAfter = $revAfter->getUser()->getName();
1824 // The two user names should be different.
1825 // If they are the same, we are seeing a cached value, which is bad.
1826 $this->assertNotSame( $userNameBefore, $userNameAfter );
1828 // This is implied by the above assertion, but explicitly check it, for completeness
1829 $this->assertSame( $newUserName, $userNameAfter );
1833 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1835 public function testGetKnownCurrentRevision_revDelete() {
1836 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1837 $this->setService( 'MainWANObjectCache', $cache );
1839 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1840 $page = $this->getNonexistingTestPage();
1841 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1843 // Grab the deleted bitmask
1844 $deletedBefore = $rev->getVisibility();
1846 // Change the deleted bitmask in the database, "behind the back" of the cache
1847 $this->db
->update( 'revision',
1848 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1849 [ 'rev_id' => $rev->getId() ] );
1851 // Reload the revision and regrab the visibility flag.
1852 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1853 $deletedAfter = $revAfter->getVisibility();
1855 // The two deleted flags should be different.
1856 // If they are the same, we are seeing a cached value, which is bad.
1857 $this->assertNotSame( $deletedBefore, $deletedAfter );
1859 // This is implied by the above assertion, but explicitly check it, for completeness
1860 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1864 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1866 public function testNewRevisionFromRow_userNameChange() {
1867 global $wgActorTableSchemaMigrationStage;
1869 $page = $this->getTestPage();
1871 /** @var Revision $rev */
1872 $rev = $page->doEditContent(
1873 new WikitextContent( $text ),
1877 $this->getMutableTestUser()->getUser()
1878 )->value
['revision'];
1880 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1881 $record = $store->newRevisionFromRow(
1882 $this->revisionToRow( $rev ),
1887 // Grab the user name
1888 $userNameBefore = $record->getUser()->getName();
1890 // Change the user name in the database
1891 $newUserName = "Renamed $userNameBefore";
1892 $this->db
->update( 'revision',
1893 [ 'rev_user_text' => $newUserName ],
1894 [ 'rev_id' => $record->getId() ] );
1895 $this->db
->update( 'user',
1896 [ 'user_name' => $newUserName ],
1897 [ 'user_id' => $record->getUser()->getId() ] );
1898 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1899 $this->db
->update( 'actor',
1900 [ 'actor_name' => $newUserName ],
1901 [ 'actor_user' => $record->getUser()->getId() ] );
1904 // Reload the record, passing $fromCache as true to force fresh info from the db,
1905 // and regrab the user name
1906 $recordAfter = $store->newRevisionFromRow(
1907 $this->revisionToRow( $rev ),
1912 $userNameAfter = $recordAfter->getUser()->getName();
1914 // The two user names should be different.
1915 // If they are the same, we are seeing a cached value, which is bad.
1916 $this->assertNotSame( $userNameBefore, $userNameAfter );
1918 // This is implied by the above assertion, but explicitly check it, for completeness
1919 $this->assertSame( $newUserName, $userNameAfter );
1923 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1925 public function testNewRevisionFromRow_revDelete() {
1926 $page = $this->getTestPage();
1928 /** @var Revision $rev */
1929 $rev = $page->doEditContent(
1930 new WikitextContent( $text ),
1932 )->value
['revision'];
1934 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1935 $record = $store->newRevisionFromRow(
1936 $this->revisionToRow( $rev ),
1941 // Grab the deleted bitmask
1942 $deletedBefore = $record->getVisibility();
1944 // Change the deleted bitmask in the database
1945 $this->db
->update( 'revision',
1946 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1947 [ 'rev_id' => $record->getId() ] );
1949 // Reload the record, passing $fromCache as true to force fresh info from the db,
1950 // and regrab the deleted bitmask
1951 $recordAfter = $store->newRevisionFromRow(
1952 $this->revisionToRow( $rev ),
1957 $deletedAfter = $recordAfter->getVisibility();
1959 // The two deleted flags should be different, because we modified the database.
1960 $this->assertNotSame( $deletedBefore, $deletedAfter );
1962 // This is implied by the above assertion, but explicitly check it, for completeness
1963 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1966 public function provideNewRevisionsFromBatchOptions() {
1967 yield
'No preload slots or content, single page' => [
1971 yield
'Preload slots and content, single page' => [
1974 'slots' => [ SlotRecord
::MAIN
],
1978 yield
'No preload slots or content, multiple pages' => [
1982 yield
'Preload slots and content, multiple pages' => [
1985 'slots' => [ SlotRecord
::MAIN
],
1992 * @dataProvider provideNewRevisionsFromBatchOptions
1993 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
1994 * @param string|null $otherPageTitle
1995 * @param array|null $options
1996 * @throws \MWException
1998 public function testNewRevisionsFromBatch_preloadContent(
1999 $otherPageTitle = null,
2002 $page1 = $this->getTestPage();
2003 $text = __METHOD__
. 'b-รค';
2004 /** @var Revision $rev1 */
2005 $rev1 = $page1->doEditContent(
2006 new WikitextContent( $text . '1' ),
2010 $this->getTestUser()->getUser()
2011 )->value
['revision'];
2012 $page2 = $this->getTestPage( $otherPageTitle );
2013 /** @var Revision $rev2 */
2014 $rev2 = $page2->doEditContent(
2015 new WikitextContent( $text . '2' ),
2019 $this->getTestUser()->getUser()
2020 )->value
['revision'];
2021 $store = MediaWikiServices
::getInstance()->getRevisionStore();
2022 $result = $store->newRevisionsFromBatch(
2023 [ $this->revisionToRow( $rev1 ), $this->revisionToRow( $rev2 ) ],
2026 $this->assertTrue( $result->isGood() );
2027 $this->assertEmpty( $result->getErrors() );
2028 $records = $result->getValue();
2029 $this->assertRevisionRecordMatchesRevision( $rev1, $records[$rev1->getId()] );
2030 $this->assertRevisionRecordMatchesRevision( $rev2, $records[$rev2->getId()] );
2032 $this->assertSame( $text . '1',
2033 $records[$rev1->getId()]->getContent( SlotRecord
::MAIN
)->serialize() );
2034 $this->assertSame( $text . '2',
2035 $records[$rev2->getId()]->getContent( SlotRecord
::MAIN
)->serialize() );
2036 $this->assertEquals( $page1->getTitle()->getDBkey(),
2037 $records[$rev1->getId()]->getPageAsLinkTarget()->getDBkey() );
2038 $this->assertEquals( $page2->getTitle()->getDBkey(),
2039 $records[$rev2->getId()]->getPageAsLinkTarget()->getDBkey() );
2043 * @covers \MediaWiki\Revision\RevisionStore::newRevisionsFromBatch
2045 public function testNewRevisionsFromBatch_emptyBatch() {
2046 $result = MediaWikiServices
::getInstance()->getRevisionStore()
2047 ->newRevisionsFromBatch(
2050 'slots' => [ SlotRecord
::MAIN
],
2054 $this->assertTrue( $result->isGood() );
2055 $this->assertEmpty( $result->getValue() );
2056 $this->assertEmpty( $result->getErrors() );