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_OLD
,
87 $this->overrideMwServices();
90 protected function addCoreDBData() {
91 // Blank out. This would fail with a modified schema, and we don't need it.
97 protected function getTestPageTitle() {
98 if ( $this->testPageTitle
) {
99 return $this->testPageTitle
;
102 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
103 return $this->testPageTitle
;
109 protected function getTestPage() {
110 if ( $this->testPage
) {
111 return $this->testPage
;
114 $title = $this->getTestPageTitle();
115 $this->testPage
= WikiPage
::factory( $title );
117 if ( !$this->testPage
->exists() ) {
118 // Make sure we don't write to the live db.
119 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
121 $user = static::getTestSysop()->getUser();
123 $this->testPage
->doEditContent(
124 new WikitextContent( 'UTContent-' . __CLASS__
),
125 'UTPageSummary-' . __CLASS__
,
126 EDIT_NEW | EDIT_SUPPRESS_RC
,
132 return $this->testPage
;
136 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
138 private function getLoadBalancerMock( array $server ) {
139 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
141 $lb = $this->getMockBuilder( LoadBalancer
::class )
142 ->setMethods( [ 'reallyOpenConnection' ] )
143 ->setConstructorArgs( [
144 [ 'servers' => [ $server ], 'localDomain' => $domain ]
148 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
149 function ( array $server, $dbNameOverride ) {
150 return $this->getDatabaseMock( $server );
158 * @return Database|PHPUnit_Framework_MockObject_MockObject
160 private function getDatabaseMock( array $params ) {
161 $db = $this->getMockBuilder( DatabaseSqlite
::class )
162 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
163 ->setConstructorArgs( [ $params ] )
166 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
167 $db->method( 'isOpen' )->willReturn( true );
172 public function provideDomainCheck() {
173 yield
[ false, 'test', '' ];
174 yield
[ 'test', 'test', '' ];
176 yield
[ false, 'test', 'foo_' ];
177 yield
[ 'test-foo_', 'test', 'foo_' ];
179 yield
[ false, 'dash-test', '' ];
180 yield
[ 'dash-test', 'dash-test', '' ];
182 yield
[ false, 'underscore_test', 'foo_' ];
183 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
187 * @dataProvider provideDomainCheck
188 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
190 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
193 'wgDBname' => $dbName,
194 'wgDBprefix' => $dbPrefix,
198 $loadBalancer = $this->getLoadBalancerMock(
201 'dbDirectory' => '*dummy*',
203 'password' => 'test',
211 'trxProfiler' => new TransactionProfiler(),
212 'connLogger' => new \Psr\Log\
NullLogger(),
213 'queryLogger' => new \Psr\Log\
NullLogger(),
214 'errorLogger' => function () {
216 'deprecationLogger' => function () {
220 'tablePrefix' => $dbPrefix,
223 $db = $loadBalancer->getConnection( DB_REPLICA
);
225 /** @var SqlBlobStore $blobStore */
226 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
227 ->disableOriginalConstructor()
230 $store = new RevisionStore(
233 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
234 MediaWikiServices
::getInstance()->getCommentStore(),
235 MediaWikiServices
::getInstance()->getContentModelStore(),
236 MediaWikiServices
::getInstance()->getSlotRoleStore(),
237 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
238 $this->getMcrMigrationStage(),
239 MediaWikiServices
::getInstance()->getActorMigration(),
243 $count = $store->countRevisionsByPageId( $db, 0 );
245 // Dummy check to make PhpUnit happy. We are really only interested in
246 // countRevisionsByPageId not failing due to the DB domain check.
247 $this->assertSame( 0, $count );
250 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
251 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
252 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
253 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
254 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
257 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
259 $r1->getPageAsLinkTarget()->getNamespace(),
260 $r2->getPageAsLinkTarget()->getNamespace()
264 $r1->getPageAsLinkTarget()->getText(),
265 $r2->getPageAsLinkTarget()->getText()
268 if ( $r1->getParentId() ) {
269 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
272 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
273 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
274 $this->assertEquals( $r1->getComment(), $r2->getComment() );
275 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
276 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
277 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
278 $this->assertEquals( $r1->getSize(), $r2->getSize() );
279 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
280 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
281 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
282 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
283 foreach ( $r1->getSlotRoles() as $role ) {
284 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
285 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
288 RevisionRecord
::DELETED_TEXT
,
289 RevisionRecord
::DELETED_COMMENT
,
290 RevisionRecord
::DELETED_USER
,
291 RevisionRecord
::DELETED_RESTRICTED
,
293 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
297 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
298 $this->assertSame( $s1->getRole(), $s2->getRole() );
299 $this->assertSame( $s1->getModel(), $s2->getModel() );
300 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
301 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
302 $this->assertSame( $s1->getSize(), $s2->getSize() );
303 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
305 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
306 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
309 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
310 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
311 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
312 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
314 foreach ( $r->getSlotRoles() as $role ) {
315 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
319 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
320 $this->assertTrue( $slot->hasAddress() );
321 $this->assertSame( $r->getId(), $slot->getRevision() );
323 $this->assertInstanceOf( Content
::class, $slot->getContent() );
327 * @param mixed[] $details
329 * @return RevisionRecord
331 private function getRevisionRecordFromDetailsArray( $details = [] ) {
332 // Convert some values that can't be provided by dataProviders
333 if ( isset( $details['user'] ) && $details['user'] === true ) {
334 $details['user'] = $this->getTestUser()->getUser();
336 if ( isset( $details['page'] ) && $details['page'] === true ) {
337 $details['page'] = $this->getTestPage()->getId();
339 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
340 $details['parent'] = $this->getTestPage()->getLatest();
343 // Create the RevisionRecord with any available data
344 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
345 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
346 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
347 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
348 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
349 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
350 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
351 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
352 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
353 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
354 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
355 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
357 if ( isset( $details['content'] ) ) {
358 foreach ( $details['content'] as $role => $content ) {
359 $rev->setContent( $role, $content );
366 public function provideInsertRevisionOn_successes() {
367 yield
'Bare minimum revision insertion' => [
369 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
371 'comment' => $this->getRandomCommentStoreComment(),
372 'timestamp' => '20171117010101',
376 yield
'Detailed revision insertion' => [
378 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
381 'comment' => $this->getRandomCommentStoreComment(),
382 'timestamp' => '20171117010101',
385 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
390 protected function getRandomCommentStoreComment() {
391 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
395 * @dataProvider provideInsertRevisionOn_successes
396 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
397 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
398 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
400 public function testInsertRevisionOn_successes(
401 array $revDetails = []
403 $title = $this->getTestPageTitle();
404 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
406 $this->overrideMwServices();
407 $store = MediaWikiServices
::getInstance()->getRevisionStore();
408 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
410 // is the new revision correct?
411 $this->assertRevisionCompleteness( $return );
412 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
413 $this->assertRevisionRecordsEqual( $rev, $return );
415 // can we load it from the store?
416 $loaded = $store->getRevisionById( $return->getId() );
417 $this->assertRevisionCompleteness( $loaded );
418 $this->assertRevisionRecordsEqual( $return, $loaded );
420 // can we find it directly in the database?
421 $this->assertRevisionExistsInDatabase( $return );
424 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
425 $row = $this->revisionToRow( new Revision( $rev ), [] );
427 // unset nulled fields
428 unset( $row->rev_content_model
);
429 unset( $row->rev_content_format
);
432 unset( $row->rev_comment_text
);
433 unset( $row->rev_comment_data
);
434 unset( $row->rev_comment_cid
);
435 unset( $row->rev_comment_id
);
437 $store = MediaWikiServices
::getInstance()->getRevisionStore();
438 $queryInfo = $store->getQueryInfo( [ 'user' ] );
440 $row = get_object_vars( $row );
442 $queryInfo['tables'],
444 [ 'rev_id' => $rev->getId() ],
445 [ array_values( $row ) ],
452 * @param SlotRecord $a
453 * @param SlotRecord $b
455 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
456 // Assert that the same blob address has been used.
457 $this->assertSame( $a->getAddress(), $b->getAddress() );
461 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
463 public function testInsertRevisionOn_blobAddressExists() {
464 $title = $this->getTestPageTitle();
466 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
468 'comment' => $this->getRandomCommentStoreComment(),
469 'timestamp' => '20171117010101',
473 $this->overrideMwServices();
474 $store = MediaWikiServices
::getInstance()->getRevisionStore();
476 // Insert the first revision
477 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
478 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
479 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
480 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
482 // Insert a second revision inheriting the same blob address
483 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
484 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
485 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
486 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
487 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
489 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
490 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
492 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
494 // And that different revisions have been created.
495 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
497 // Make sure the slot rows reference the correct revision
498 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
499 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
502 public function provideInsertRevisionOn_failures() {
505 'comment' => $this->getRandomCommentStoreComment(),
506 'timestamp' => '20171117010101',
509 new InvalidArgumentException( 'main slot must be provided' )
511 yield
'no main slot' => [
513 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
514 'comment' => $this->getRandomCommentStoreComment(),
515 'timestamp' => '20171117010101',
518 new InvalidArgumentException( 'main slot must be provided' )
520 yield
'no timestamp' => [
522 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
523 'comment' => $this->getRandomCommentStoreComment(),
526 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
528 yield
'no comment' => [
530 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
531 'timestamp' => '20171117010101',
534 new IncompleteRevisionException( 'comment must not be NULL!' )
538 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
539 'comment' => $this->getRandomCommentStoreComment(),
540 'timestamp' => '20171117010101',
542 new IncompleteRevisionException( 'user must not be NULL!' )
547 * @dataProvider provideInsertRevisionOn_failures
548 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
550 public function testInsertRevisionOn_failures(
551 array $revDetails = [],
554 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
556 $store = MediaWikiServices
::getInstance()->getRevisionStore();
558 $this->setExpectedException(
559 get_class( $exception ),
560 $exception->getMessage(),
561 $exception->getCode()
563 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
566 public function provideNewNullRevision() {
568 Title
::newFromText( 'UTPage_notAutoCreated' ),
569 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
570 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
574 Title
::newFromText( 'UTPage_notAutoCreated' ),
575 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
576 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
582 * @dataProvider provideNewNullRevision
583 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
584 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
586 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
587 $this->overrideMwServices();
589 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
590 $page = WikiPage
::factory( $title );
592 if ( !$page->exists() ) {
593 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
596 $revDetails['page'] = $page->getId();
597 $revDetails['timestamp'] = wfTimestampNow();
598 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
599 $revDetails['user'] = $user;
601 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
602 $store = MediaWikiServices
::getInstance()->getRevisionStore();
604 $dbw = wfGetDB( DB_MASTER
);
605 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
606 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
608 $record = $store->newNullRevision(
609 wfGetDB( DB_MASTER
),
616 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
617 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
618 $this->assertEquals( $comment, $record->getComment() );
619 $this->assertEquals( $minor, $record->isMinor() );
620 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
621 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
623 $this->assertArrayEquals(
624 $baseRev->getSlotRoles(),
625 $record->getSlotRoles()
628 foreach ( $baseRev->getSlotRoles() as $role ) {
629 $parentSlot = $baseRev->getSlot( $role );
630 $slot = $record->getSlot( $role );
632 $this->assertTrue( $slot->isInherited(), 'isInherited' );
633 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
634 $this->assertSameSlotContent( $parentSlot, $slot );
639 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
641 public function testNewNullRevision_nonExistingTitle() {
642 $store = MediaWikiServices
::getInstance()->getRevisionStore();
643 $record = $store->newNullRevision(
644 wfGetDB( DB_MASTER
),
645 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
646 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
648 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
650 $this->assertNull( $record );
654 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
656 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
657 $page = $this->getTestPage();
658 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
659 /** @var Revision $rev */
660 $rev = $status->value
['revision'];
662 $store = MediaWikiServices
::getInstance()->getRevisionStore();
663 $revisionRecord = $store->getRevisionById( $rev->getId() );
664 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
666 $this->assertGreaterThan( 0, $result );
668 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
674 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
676 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
677 // This assumes that sysops are auto patrolled
678 $sysop = $this->getTestSysop()->getUser();
679 $page = $this->getTestPage();
680 $status = $page->doEditContent(
681 new WikitextContent( __METHOD__
),
687 /** @var Revision $rev */
688 $rev = $status->value
['revision'];
690 $store = MediaWikiServices
::getInstance()->getRevisionStore();
691 $revisionRecord = $store->getRevisionById( $rev->getId() );
692 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
694 $this->assertSame( 0, $result );
698 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
700 public function testGetRecentChange() {
701 $page = $this->getTestPage();
702 $content = new WikitextContent( __METHOD__
);
703 $status = $page->doEditContent( $content, __METHOD__
);
704 /** @var Revision $rev */
705 $rev = $status->value
['revision'];
707 $store = MediaWikiServices
::getInstance()->getRevisionStore();
708 $revRecord = $store->getRevisionById( $rev->getId() );
709 $recentChange = $store->getRecentChange( $revRecord );
711 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
712 $this->assertEquals( $rev->getRecentChange(), $recentChange );
716 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
718 public function testGetRevisionById() {
719 $page = $this->getTestPage();
720 $content = new WikitextContent( __METHOD__
);
721 $status = $page->doEditContent( $content, __METHOD__
);
722 /** @var Revision $rev */
723 $rev = $status->value
['revision'];
725 $store = MediaWikiServices
::getInstance()->getRevisionStore();
726 $revRecord = $store->getRevisionById( $rev->getId() );
728 $this->assertSame( $rev->getId(), $revRecord->getId() );
729 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
730 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
734 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
736 public function testGetRevisionByTitle() {
737 $page = $this->getTestPage();
738 $content = new WikitextContent( __METHOD__
);
739 $status = $page->doEditContent( $content, __METHOD__
);
740 /** @var Revision $rev */
741 $rev = $status->value
['revision'];
743 $store = MediaWikiServices
::getInstance()->getRevisionStore();
744 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
746 $this->assertSame( $rev->getId(), $revRecord->getId() );
747 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
748 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
752 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
754 public function testGetRevisionByPageId() {
755 $page = $this->getTestPage();
756 $content = new WikitextContent( __METHOD__
);
757 $status = $page->doEditContent( $content, __METHOD__
);
758 /** @var Revision $rev */
759 $rev = $status->value
['revision'];
761 $store = MediaWikiServices
::getInstance()->getRevisionStore();
762 $revRecord = $store->getRevisionByPageId( $page->getId() );
764 $this->assertSame( $rev->getId(), $revRecord->getId() );
765 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
766 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
770 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
772 public function testGetRevisionByTimestamp() {
773 // Make sure there is 1 second between the last revision and the rev we create...
774 // Otherwise we might not get the correct revision and the test may fail...
776 $page = $this->getTestPage();
778 $content = new WikitextContent( __METHOD__
);
779 $status = $page->doEditContent( $content, __METHOD__
);
780 /** @var Revision $rev */
781 $rev = $status->value
['revision'];
783 $store = MediaWikiServices
::getInstance()->getRevisionStore();
784 $revRecord = $store->getRevisionByTimestamp(
789 $this->assertSame( $rev->getId(), $revRecord->getId() );
790 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
791 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
794 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
795 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
796 $page = WikiPage
::factory( $rev->getTitle() );
799 'rev_id' => (string)$rev->getId(),
800 'rev_page' => (string)$rev->getPage(),
801 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
802 'rev_user_text' => (string)$rev->getUserText(),
803 'rev_user' => (string)$rev->getUser(),
804 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
805 'rev_deleted' => (string)$rev->getVisibility(),
806 'rev_len' => (string)$rev->getSize(),
807 'rev_parent_id' => (string)$rev->getParentId(),
808 'rev_sha1' => (string)$rev->getSha1(),
811 if ( in_array( 'page', $options ) ) {
813 'page_namespace' => (string)$page->getTitle()->getNamespace(),
814 'page_title' => $page->getTitle()->getDBkey(),
815 'page_id' => (string)$page->getId(),
816 'page_latest' => (string)$page->getLatest(),
817 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
818 'page_len' => (string)$page->getContent()->getSize(),
822 if ( in_array( 'user', $options ) ) {
824 'user_name' => (string)$rev->getUserText(),
828 if ( in_array( 'comment', $options ) ) {
830 'rev_comment_text' => $rev->getComment(),
831 'rev_comment_data' => null,
832 'rev_comment_cid' => null,
836 if ( $rev->getId() ) {
838 'rev_id' => (string)$rev->getId(),
842 return (object)$fields;
845 protected function assertRevisionRecordMatchesRevision(
847 RevisionRecord
$record
849 $this->assertSame( $rev->getId(), $record->getId() );
850 $this->assertSame( $rev->getPage(), $record->getPageId() );
851 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
852 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
853 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
854 $this->assertSame( $rev->isMinor(), $record->isMinor() );
855 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
856 $this->assertSame( $rev->getSize(), $record->getSize() );
858 * @note As of MW 1.31, the database schema allows the parent ID to be
859 * NULL to indicate that it is unknown.
861 $expectedParent = $rev->getParentId();
862 if ( $expectedParent === null ) {
865 $this->assertSame( $expectedParent, $record->getParentId() );
866 $this->assertSame( $rev->getSha1(), $record->getSha1() );
867 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
868 $this->assertSame( $rev->getContentFormat(),
869 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
870 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
871 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
873 $revRec = $rev->getRevisionRecord();
874 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
875 $recMain = $record->getSlot( SlotRecord
::MAIN
);
877 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
878 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
879 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
881 if ( $revMain->hasOrigin() ) {
882 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
885 if ( $revMain->hasAddress() ) {
886 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
889 if ( $revMain->hasContentId() ) {
890 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
895 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
896 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
898 public function testNewRevisionFromRow_getQueryInfo() {
899 $page = $this->getTestPage();
900 $text = __METHOD__
. 'a-ä';
901 /** @var Revision $rev */
902 $rev = $page->doEditContent(
903 new WikitextContent( $text ),
905 )->value
['revision'];
907 $store = MediaWikiServices
::getInstance()->getRevisionStore();
908 $info = $store->getQueryInfo();
909 $row = $this->db
->selectRow(
912 [ 'rev_id' => $rev->getId() ],
917 $record = $store->newRevisionFromRow(
922 $this->assertRevisionRecordMatchesRevision( $rev, $record );
923 $this->assertSame( $text, $rev->getContent()->serialize() );
927 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
929 public function testNewRevisionFromRow_anonEdit() {
930 $page = $this->getTestPage();
931 $text = __METHOD__
. 'a-ä';
932 /** @var Revision $rev */
933 $rev = $page->doEditContent(
934 new WikitextContent( $text ),
936 )->value
['revision'];
938 $store = MediaWikiServices
::getInstance()->getRevisionStore();
939 $record = $store->newRevisionFromRow(
940 $this->revisionToRow( $rev ),
944 $this->assertRevisionRecordMatchesRevision( $rev, $record );
945 $this->assertSame( $text, $rev->getContent()->serialize() );
949 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
951 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
952 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
953 $this->overrideMwServices();
954 $page = $this->getTestPage();
955 $text = __METHOD__
. 'a-ä';
956 /** @var Revision $rev */
957 $rev = $page->doEditContent(
958 new WikitextContent( $text ),
960 )->value
['revision'];
962 $store = MediaWikiServices
::getInstance()->getRevisionStore();
963 $record = $store->newRevisionFromRow(
964 $this->revisionToRow( $rev ),
968 $this->assertRevisionRecordMatchesRevision( $rev, $record );
969 $this->assertSame( $text, $rev->getContent()->serialize() );
973 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
975 public function testNewRevisionFromRow_userEdit() {
976 $page = $this->getTestPage();
977 $text = __METHOD__
. 'b-ä';
978 /** @var Revision $rev */
979 $rev = $page->doEditContent(
980 new WikitextContent( $text ),
984 $this->getTestUser()->getUser()
985 )->value
['revision'];
987 $store = MediaWikiServices
::getInstance()->getRevisionStore();
988 $record = $store->newRevisionFromRow(
989 $this->revisionToRow( $rev ),
993 $this->assertRevisionRecordMatchesRevision( $rev, $record );
994 $this->assertSame( $text, $rev->getContent()->serialize() );
998 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
999 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1001 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1002 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1003 $title = Title
::newFromText( __METHOD__
);
1004 $text = __METHOD__
. '-bä';
1005 $page = WikiPage
::factory( $title );
1006 /** @var Revision $orig */
1007 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1008 ->value
['revision'];
1009 $page->doDeleteArticle( __METHOD__
);
1011 $db = wfGetDB( DB_MASTER
);
1012 $arQuery = $store->getArchiveQueryInfo();
1014 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1015 __METHOD__
, [], $arQuery['joins']
1017 $this->assertTrue( is_object( $res ), 'query failed' );
1019 $row = $res->fetchObject();
1021 $record = $store->newRevisionFromArchiveRow( $row );
1023 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1024 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1028 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1030 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1031 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1032 $this->overrideMwServices();
1033 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1034 $title = Title
::newFromText( __METHOD__
);
1035 $text = __METHOD__
. '-bä';
1036 $page = WikiPage
::factory( $title );
1037 /** @var Revision $orig */
1038 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1039 ->value
['revision'];
1040 $page->doDeleteArticle( __METHOD__
);
1042 $db = wfGetDB( DB_MASTER
);
1043 $arQuery = $store->getArchiveQueryInfo();
1045 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1046 __METHOD__
, [], $arQuery['joins']
1048 $this->assertTrue( is_object( $res ), 'query failed' );
1050 $row = $res->fetchObject();
1052 $record = $store->newRevisionFromArchiveRow( $row );
1054 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1055 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1059 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1061 public function testNewRevisionFromArchiveRow_no_user() {
1062 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1066 'ar_page_id' => '2',
1067 'ar_namespace' => '0',
1068 'ar_title' => 'Something',
1070 'ar_text_id' => '47',
1071 'ar_timestamp' => '20180528192356',
1072 'ar_minor_edit' => '0',
1073 'ar_deleted' => '0',
1075 'ar_parent_id' => '0',
1076 'ar_sha1' => 'deadbeef',
1077 'ar_comment_text' => 'whatever',
1078 'ar_comment_data' => null,
1079 'ar_comment_cid' => null,
1081 'ar_user_text' => '', // this is the important bit
1083 'ar_content_format' => null,
1084 'ar_content_model' => null,
1087 \Wikimedia\
suppressWarnings();
1088 $record = $store->newRevisionFromArchiveRow( $row );
1089 \Wikimedia\
suppressWarnings( true );
1091 $this->assertInstanceOf( RevisionRecord
::class, $record );
1092 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1093 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1097 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1099 public function testNewRevisionFromRow_no_user() {
1100 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1101 $title = Title
::newFromText( __METHOD__
);
1106 'page_namespace' => '0',
1107 'page_title' => $title->getText(),
1108 'rev_text_id' => '47',
1109 'rev_timestamp' => '20180528192356',
1110 'rev_minor_edit' => '0',
1111 'rev_deleted' => '0',
1113 'rev_parent_id' => '0',
1114 'rev_sha1' => 'deadbeef',
1115 'rev_comment_text' => 'whatever',
1116 'rev_comment_data' => null,
1117 'rev_comment_cid' => null,
1119 'rev_user_text' => '', // this is the important bit
1120 'rev_actor' => null,
1121 'rev_content_format' => null,
1122 'rev_content_model' => null,
1125 \Wikimedia\
suppressWarnings();
1126 $record = $store->newRevisionFromRow( $row, 0, $title );
1127 \Wikimedia\
suppressWarnings( true );
1129 $this->assertNotNull( $record );
1130 $this->assertNotNull( $record->getUser() );
1131 $this->assertNotEmpty( $record->getUser()->getName() );
1135 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1137 public function testInsertRevisionOn_archive() {
1138 // This is a round trip test for deletion and undeletion of a
1139 // revision row via the archive table.
1141 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1142 $title = Title
::newFromText( __METHOD__
);
1144 $page = WikiPage
::factory( $title );
1145 /** @var Revision $origRev */
1146 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1147 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1148 ->value
['revision'];
1149 $orig = $origRev->getRevisionRecord();
1150 $page->doDeleteArticle( __METHOD__
);
1152 // re-create page, so we can later load revisions for it
1153 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1155 $db = wfGetDB( DB_MASTER
);
1156 $arQuery = $store->getArchiveQueryInfo();
1157 $row = $db->selectRow(
1158 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1159 __METHOD__
, [], $arQuery['joins']
1162 $this->assertNotFalse( $row, 'query failed' );
1164 $record = $store->newRevisionFromArchiveRow(
1168 [ 'page_id' => $title->getArticleID() ]
1171 $restored = $store->insertRevisionOn( $record, $db );
1173 // is the new revision correct?
1174 $this->assertRevisionCompleteness( $restored );
1175 $this->assertRevisionRecordsEqual( $record, $restored );
1177 // does the new revision use the original slot?
1178 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1179 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1180 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1181 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1182 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1183 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1185 // can we load it from the store?
1186 $loaded = $store->getRevisionById( $restored->getId() );
1187 $this->assertNotNull( $loaded );
1188 $this->assertRevisionCompleteness( $loaded );
1189 $this->assertRevisionRecordsEqual( $restored, $loaded );
1191 // can we find it directly in the database?
1192 $this->assertRevisionExistsInDatabase( $restored );
1196 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1198 public function testLoadRevisionFromId() {
1199 $title = Title
::newFromText( __METHOD__
);
1200 $page = WikiPage
::factory( $title );
1201 /** @var Revision $rev */
1202 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1203 ->value
['revision'];
1205 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1206 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1207 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1211 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1213 public function testLoadRevisionFromPageId() {
1214 $title = Title
::newFromText( __METHOD__
);
1215 $page = WikiPage
::factory( $title );
1216 /** @var Revision $rev */
1217 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1218 ->value
['revision'];
1220 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1221 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1222 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1226 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1228 public function testLoadRevisionFromTitle() {
1229 $title = Title
::newFromText( __METHOD__
);
1230 $page = WikiPage
::factory( $title );
1231 /** @var Revision $rev */
1232 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1233 ->value
['revision'];
1235 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1236 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1237 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1241 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1243 public function testLoadRevisionFromTimestamp() {
1244 $title = Title
::newFromText( __METHOD__
);
1245 $page = WikiPage
::factory( $title );
1246 /** @var Revision $revOne */
1247 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1248 ->value
['revision'];
1249 // Sleep to ensure different timestamps... )(evil)
1251 /** @var Revision $revTwo */
1252 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1253 ->value
['revision'];
1255 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1257 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1261 $store->loadRevisionFromTimestamp(
1262 wfGetDB( DB_MASTER
),
1264 $revOne->getTimestamp()
1269 $store->loadRevisionFromTimestamp(
1270 wfGetDB( DB_MASTER
),
1272 $revTwo->getTimestamp()
1278 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1280 public function testGetParentLengths() {
1281 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1282 /** @var Revision $revOne */
1283 $revOne = $page->doEditContent(
1284 new WikitextContent( __METHOD__
), __METHOD__
1285 )->value
['revision'];
1286 /** @var Revision $revTwo */
1287 $revTwo = $page->doEditContent(
1288 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1289 )->value
['revision'];
1291 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1294 $revOne->getId() => strlen( __METHOD__
),
1296 $store->listRevisionSizes(
1297 wfGetDB( DB_MASTER
),
1298 [ $revOne->getId() ]
1303 $revOne->getId() => strlen( __METHOD__
),
1304 $revTwo->getId() => strlen( __METHOD__
) +
1,
1306 $store->listRevisionSizes(
1307 wfGetDB( DB_MASTER
),
1308 [ $revOne->getId(), $revTwo->getId() ]
1314 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1316 public function testGetPreviousRevision() {
1317 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1318 /** @var Revision $revOne */
1319 $revOne = $page->doEditContent(
1320 new WikitextContent( __METHOD__
), __METHOD__
1321 )->value
['revision'];
1322 /** @var Revision $revTwo */
1323 $revTwo = $page->doEditContent(
1324 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1325 )->value
['revision'];
1327 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1329 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1333 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1338 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1340 public function testGetNextRevision() {
1341 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1342 /** @var Revision $revOne */
1343 $revOne = $page->doEditContent(
1344 new WikitextContent( __METHOD__
), __METHOD__
1345 )->value
['revision'];
1346 /** @var Revision $revTwo */
1347 $revTwo = $page->doEditContent(
1348 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1349 )->value
['revision'];
1351 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1354 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1357 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1361 public function provideNonHistoryRevision() {
1362 $title = Title
::newFromText( __METHOD__
);
1363 $rev = new MutableRevisionRecord( $title );
1366 $user = new UserIdentityValue( 7, 'Frank', 0 );
1367 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1370 'ar_rev_id' => 34567,
1373 'ar_minor_edit' => 0,
1374 'ar_timestamp' => '20180101020202',
1376 $slots = new RevisionSlots( [] );
1377 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1382 * @dataProvider provideNonHistoryRevision
1383 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1385 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1386 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1387 $this->assertNull( $store->getPreviousRevision( $rev ) );
1391 * @dataProvider provideNonHistoryRevision
1392 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1394 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1395 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1396 $this->assertNull( $store->getNextRevision( $rev ) );
1400 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1402 public function testGetTimestampFromId_found() {
1403 $page = $this->getTestPage();
1404 /** @var Revision $rev */
1405 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1406 ->value
['revision'];
1408 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1409 $result = $store->getTimestampFromId(
1414 $this->assertSame( $rev->getTimestamp(), $result );
1418 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1420 public function testGetTimestampFromId_notFound() {
1421 $page = $this->getTestPage();
1422 /** @var Revision $rev */
1423 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1424 ->value
['revision'];
1426 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1427 $result = $store->getTimestampFromId(
1432 $this->assertFalse( $result );
1436 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1438 public function testCountRevisionsByPageId() {
1439 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1440 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1444 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1446 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1449 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1451 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1454 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1459 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1461 public function testCountRevisionsByTitle() {
1462 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1463 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1467 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1469 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1472 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1474 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1477 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1482 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1484 public function testUserWasLastToEdit_false() {
1485 $sysop = $this->getTestSysop()->getUser();
1486 $page = $this->getTestPage();
1487 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1489 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1490 $result = $store->userWasLastToEdit(
1491 wfGetDB( DB_MASTER
),
1496 $this->assertFalse( $result );
1500 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1502 public function testUserWasLastToEdit_true() {
1503 $startTime = wfTimestampNow();
1504 $sysop = $this->getTestSysop()->getUser();
1505 $page = $this->getTestPage();
1506 $page->doEditContent(
1507 new WikitextContent( __METHOD__
),
1514 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1515 $result = $store->userWasLastToEdit(
1516 wfGetDB( DB_MASTER
),
1521 $this->assertTrue( $result );
1525 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1527 public function testGetKnownCurrentRevision() {
1528 $page = $this->getTestPage();
1529 /** @var Revision $rev */
1530 $rev = $page->doEditContent(
1531 new WikitextContent( __METHOD__
. 'b' ),
1535 $this->getTestUser()->getUser()
1536 )->value
['revision'];
1538 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1539 $record = $store->getKnownCurrentRevision(
1544 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1547 public function provideNewMutableRevisionFromArray() {
1548 yield
'Basic array, content object' => [
1552 'timestamp' => '20171017114835',
1553 'user_text' => '111.0.1.2',
1555 'minor_edit' => false,
1559 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1560 'comment' => 'Goat Comment!',
1561 'content' => new WikitextContent( 'Some Content' ),
1564 yield
'Basic array, serialized text' => [
1568 'timestamp' => '20171017114835',
1569 'user_text' => '111.0.1.2',
1571 'minor_edit' => false,
1575 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1576 'comment' => 'Goat Comment!',
1577 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1580 yield
'Basic array, serialized text, utf-8 flags' => [
1584 'timestamp' => '20171017114835',
1585 'user_text' => '111.0.1.2',
1587 'minor_edit' => false,
1591 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1592 'comment' => 'Goat Comment!',
1593 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1597 yield
'Basic array, with title' => [
1599 'title' => Title
::newFromText( 'SomeText' ),
1600 'timestamp' => '20171017114835',
1601 'user_text' => '111.0.1.2',
1603 'minor_edit' => false,
1607 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1608 'comment' => 'Goat Comment!',
1609 'content' => new WikitextContent( 'Some Content' ),
1612 yield
'Basic array, no user field' => [
1616 'timestamp' => '20171017114835',
1617 'user_text' => '111.0.1.3',
1618 'minor_edit' => false,
1622 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1623 'comment' => 'Goat Comment!',
1624 'content' => new WikitextContent( 'Some Content' ),
1630 * @dataProvider provideNewMutableRevisionFromArray
1631 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1633 public function testNewMutableRevisionFromArray( array $array ) {
1634 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1636 // HACK: if $array['page'] is given, make sure that the page exists
1637 if ( isset( $array['page'] ) ) {
1638 $t = Title
::newFromID( $array['page'] );
1639 if ( !$t ||
!$t->exists() ) {
1640 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1641 $info = $this->insertPage( $t );
1642 $array['page'] = $info['id'];
1646 $result = $store->newMutableRevisionFromArray( $array );
1648 if ( isset( $array['id'] ) ) {
1649 $this->assertSame( $array['id'], $result->getId() );
1651 if ( isset( $array['page'] ) ) {
1652 $this->assertSame( $array['page'], $result->getPageId() );
1654 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1655 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1656 if ( isset( $array['user'] ) ) {
1657 $this->assertSame( $array['user'], $result->getUser()->getId() );
1659 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1660 $this->assertSame( $array['deleted'], $result->getVisibility() );
1661 $this->assertSame( $array['len'], $result->getSize() );
1662 $this->assertSame( $array['parent_id'], $result->getParentId() );
1663 $this->assertSame( $array['sha1'], $result->getSha1() );
1664 $this->assertSame( $array['comment'], $result->getComment()->text
);
1665 if ( isset( $array['content'] ) ) {
1666 foreach ( $array['content'] as $role => $content ) {
1668 $result->getContent( $role )->equals( $content )
1671 } elseif ( isset( $array['text'] ) ) {
1672 $this->assertSame( $array['text'],
1673 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1674 } elseif ( isset( $array['content_format'] ) ) {
1676 $array['content_format'],
1677 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1679 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1684 * @dataProvider provideNewMutableRevisionFromArray
1685 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1687 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1688 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1689 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1690 $blobStore = new SqlBlobStore( $lb, $cache );
1691 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1693 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1694 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1695 ->disableOriginalConstructor()
1697 $factory->expects( $this->any() )
1698 ->method( 'newBlobStore' )
1699 ->willReturn( $blobStore );
1700 $factory->expects( $this->any() )
1701 ->method( 'newSqlBlobStore' )
1702 ->willReturn( $blobStore );
1704 $this->setService( 'BlobStoreFactory', $factory );
1706 $this->testNewMutableRevisionFromArray( $array );
1710 * Creates a new revision for testing caching behavior
1712 * @param WikiPage $page the page for the new revision
1713 * @param RevisionStore $store store object to use for creating the revision
1714 * @return bool|RevisionStoreRecord the revision created, or false if missing
1716 private function createRevisionStoreCacheRecord( $page, $store ) {
1717 $user = MediaWikiTestCase
::getMutableTestUser()->getUser();
1718 $updater = $page->newPageUpdater( $user );
1719 $updater->setContent( SlotRecord
::MAIN
, new WikitextContent( __METHOD__
) );
1720 $summary = CommentStoreComment
::newUnsavedComment( __METHOD__
);
1721 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
1722 return $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1726 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1728 public function testGetKnownCurrentRevision_userNameChange() {
1729 global $wgActorTableSchemaMigrationStage;
1731 $this->overrideMwServices();
1732 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1733 $this->setService( 'MainWANObjectCache', $cache );
1735 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1736 $page = $this->getNonexistingTestPage();
1737 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1739 // Grab the user name
1740 $userNameBefore = $rev->getUser()->getName();
1742 // Change the user name in the database, "behind the back" of the cache
1743 $newUserName = "Renamed $userNameBefore";
1744 $this->db
->update( 'revision',
1745 [ 'rev_user_text' => $newUserName ],
1746 [ 'rev_id' => $rev->getId() ] );
1747 $this->db
->update( 'user',
1748 [ 'user_name' => $newUserName ],
1749 [ 'user_id' => $rev->getUser()->getId() ] );
1750 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1751 $this->db
->update( 'actor',
1752 [ 'actor_name' => $newUserName ],
1753 [ 'actor_user' => $rev->getUser()->getId() ] );
1756 // Reload the revision and regrab the user name.
1757 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1758 $userNameAfter = $revAfter->getUser()->getName();
1760 // The two user names should be different.
1761 // If they are the same, we are seeing a cached value, which is bad.
1762 $this->assertNotSame( $userNameBefore, $userNameAfter );
1764 // This is implied by the above assertion, but explicitly check it, for completeness
1765 $this->assertSame( $newUserName, $userNameAfter );
1769 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1771 public function testGetKnownCurrentRevision_revDelete() {
1772 $this->overrideMwServices();
1773 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1774 $this->setService( 'MainWANObjectCache', $cache );
1776 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1777 $page = $this->getNonexistingTestPage();
1778 $rev = $this->createRevisionStoreCacheRecord( $page, $store );
1780 // Grab the deleted bitmask
1781 $deletedBefore = $rev->getVisibility();
1783 // Change the deleted bitmask in the database, "behind the back" of the cache
1784 $this->db
->update( 'revision',
1785 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1786 [ 'rev_id' => $rev->getId() ] );
1788 // Reload the revision and regrab the visibility flag.
1789 $revAfter = $store->getKnownCurrentRevision( $page->getTitle(), $rev->getId() );
1790 $deletedAfter = $revAfter->getVisibility();
1792 // The two deleted flags should be different.
1793 // If they are the same, we are seeing a cached value, which is bad.
1794 $this->assertNotSame( $deletedBefore, $deletedAfter );
1796 // This is implied by the above assertion, but explicitly check it, for completeness
1797 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );
1801 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1803 public function testNewRevisionFromRow_userNameChange() {
1804 global $wgActorTableSchemaMigrationStage;
1806 $page = $this->getTestPage();
1808 /** @var Revision $rev */
1809 $rev = $page->doEditContent(
1810 new WikitextContent( $text ),
1812 )->value
['revision'];
1814 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1815 $record = $store->newRevisionFromRow(
1816 $this->revisionToRow( $rev ),
1821 // Grab the user name
1822 $userNameBefore = $record->getUser()->getName();
1824 // Change the user name in the database
1825 $newUserName = "Renamed $userNameBefore";
1826 $this->db
->update( 'revision',
1827 [ 'rev_user_text' => $newUserName ],
1828 [ 'rev_id' => $record->getId() ] );
1829 $this->db
->update( 'user',
1830 [ 'user_name' => $newUserName ],
1831 [ 'user_id' => $record->getUser()->getId() ] );
1832 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1833 $this->db
->update( 'actor',
1834 [ 'actor_name' => $newUserName ],
1835 [ 'actor_user' => $record->getUser()->getId() ] );
1838 // Reload the record, passing $fromCache as true to force fresh info from the db,
1839 // and regrab the user name
1840 $recordAfter = $store->newRevisionFromRow(
1841 $this->revisionToRow( $rev ),
1846 $userNameAfter = $recordAfter->getUser()->getName();
1848 // The two user names should be different.
1849 // If they are the same, we are seeing a cached value, which is bad.
1850 $this->assertNotSame( $userNameBefore, $userNameAfter );
1852 // This is implied by the above assertion, but explicitly check it, for completeness
1853 $this->assertSame( $newUserName, $userNameAfter );
1857 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1859 public function testNewRevisionFromRow_revDelete() {
1860 $page = $this->getTestPage();
1862 /** @var Revision $rev */
1863 $rev = $page->doEditContent(
1864 new WikitextContent( $text ),
1866 )->value
['revision'];
1868 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1869 $record = $store->newRevisionFromRow(
1870 $this->revisionToRow( $rev ),
1875 // Grab the deleted bitmask
1876 $deletedBefore = $record->getVisibility();
1878 // Change the deleted bitmask in the database
1879 $this->db
->update( 'revision',
1880 [ 'rev_deleted' => RevisionRecord
::DELETED_TEXT
],
1881 [ 'rev_id' => $record->getId() ] );
1883 // Reload the record, passing $fromCache as true to force fresh info from the db,
1884 // and regrab the deleted bitmask
1885 $recordAfter = $store->newRevisionFromRow(
1886 $this->revisionToRow( $rev ),
1891 $deletedAfter = $recordAfter->getVisibility();
1893 // The two deleted flags should be different, because we modified the database.
1894 $this->assertNotSame( $deletedBefore, $deletedAfter );
1896 // This is implied by the above assertion, but explicitly check it, for completeness
1897 $this->assertSame( RevisionRecord
::DELETED_TEXT
, $deletedAfter );