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\RevisionSlots
;
18 use MediaWiki\Revision\RevisionStore
;
19 use MediaWiki\Revision\SlotRecord
;
20 use MediaWiki\Storage\BlobStoreFactory
;
21 use MediaWiki\Storage\SqlBlobStore
;
22 use MediaWiki\User\UserIdentityValue
;
23 use MediaWikiTestCase
;
24 use PHPUnit_Framework_MockObject_MockObject
;
29 use Wikimedia\Rdbms\Database
;
30 use Wikimedia\Rdbms\DatabaseDomain
;
31 use Wikimedia\Rdbms\DatabaseSqlite
;
32 use Wikimedia\Rdbms\FakeResultWrapper
;
33 use Wikimedia\Rdbms\LoadBalancer
;
34 use Wikimedia\Rdbms\TransactionProfiler
;
40 * @group RevisionStore
42 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
47 private $testPageTitle;
57 abstract protected function getMcrMigrationStage();
62 protected function getContentHandlerUseDB() {
69 abstract protected function getMcrTablesToReset();
71 public function setUp() {
73 $this->tablesUsed
[] = 'archive';
74 $this->tablesUsed
[] = 'page';
75 $this->tablesUsed
[] = 'revision';
76 $this->tablesUsed
[] = 'comment';
78 $this->tablesUsed +
= $this->getMcrTablesToReset();
80 $this->setMwGlobals( [
81 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
82 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
83 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
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
;
108 protected function getTestPage() {
109 if ( $this->testPage
) {
110 return $this->testPage
;
113 $title = $this->getTestPageTitle();
114 $this->testPage
= WikiPage
::factory( $title );
116 if ( !$this->testPage
->exists() ) {
117 // Make sure we don't write to the live db.
118 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
120 $user = static::getTestSysop()->getUser();
122 $this->testPage
->doEditContent(
123 new WikitextContent( 'UTContent-' . __CLASS__
),
124 'UTPageSummary-' . __CLASS__
,
125 EDIT_NEW | EDIT_SUPPRESS_RC
,
131 return $this->testPage
;
135 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
137 private function getLoadBalancerMock( array $server ) {
138 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
140 $lb = $this->getMockBuilder( LoadBalancer
::class )
141 ->setMethods( [ 'reallyOpenConnection' ] )
142 ->setConstructorArgs( [
143 [ 'servers' => [ $server ], 'localDomain' => $domain ]
147 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
148 function ( array $server, $dbNameOverride ) {
149 return $this->getDatabaseMock( $server );
157 * @return Database|PHPUnit_Framework_MockObject_MockObject
159 private function getDatabaseMock( array $params ) {
160 $db = $this->getMockBuilder( DatabaseSqlite
::class )
161 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
162 ->setConstructorArgs( [ $params ] )
165 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
166 $db->method( 'isOpen' )->willReturn( true );
171 public function provideDomainCheck() {
172 yield
[ false, 'test', '' ];
173 yield
[ 'test', 'test', '' ];
175 yield
[ false, 'test', 'foo_' ];
176 yield
[ 'test-foo_', 'test', 'foo_' ];
178 yield
[ false, 'dash-test', '' ];
179 yield
[ 'dash-test', 'dash-test', '' ];
181 yield
[ false, 'underscore_test', 'foo_' ];
182 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
186 * @dataProvider provideDomainCheck
187 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
189 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
192 'wgDBname' => $dbName,
193 'wgDBprefix' => $dbPrefix,
197 $loadBalancer = $this->getLoadBalancerMock(
200 'dbDirectory' => '*dummy*',
202 'password' => 'test',
210 'trxProfiler' => new TransactionProfiler(),
211 'connLogger' => new \Psr\Log\
NullLogger(),
212 'queryLogger' => new \Psr\Log\
NullLogger(),
213 'errorLogger' => function () {
215 'deprecationLogger' => function () {
219 'tablePrefix' => $dbPrefix,
222 $db = $loadBalancer->getConnection( DB_REPLICA
);
224 /** @var SqlBlobStore $blobStore */
225 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
226 ->disableOriginalConstructor()
229 $store = new RevisionStore(
232 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
233 MediaWikiServices
::getInstance()->getCommentStore(),
234 MediaWikiServices
::getInstance()->getContentModelStore(),
235 MediaWikiServices
::getInstance()->getSlotRoleStore(),
236 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
237 $this->getMcrMigrationStage(),
238 MediaWikiServices
::getInstance()->getActorMigration(),
242 $count = $store->countRevisionsByPageId( $db, 0 );
244 // Dummy check to make PhpUnit happy. We are really only interested in
245 // countRevisionsByPageId not failing due to the DB domain check.
246 $this->assertSame( 0, $count );
249 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
250 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
251 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
252 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
253 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
256 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
258 $r1->getPageAsLinkTarget()->getNamespace(),
259 $r2->getPageAsLinkTarget()->getNamespace()
263 $r1->getPageAsLinkTarget()->getText(),
264 $r2->getPageAsLinkTarget()->getText()
267 if ( $r1->getParentId() ) {
268 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
271 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
272 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
273 $this->assertEquals( $r1->getComment(), $r2->getComment() );
274 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
275 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
276 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
277 $this->assertEquals( $r1->getSize(), $r2->getSize() );
278 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
279 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
280 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
281 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
282 foreach ( $r1->getSlotRoles() as $role ) {
283 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
284 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
287 RevisionRecord
::DELETED_TEXT
,
288 RevisionRecord
::DELETED_COMMENT
,
289 RevisionRecord
::DELETED_USER
,
290 RevisionRecord
::DELETED_RESTRICTED
,
292 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
296 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
297 $this->assertSame( $s1->getRole(), $s2->getRole() );
298 $this->assertSame( $s1->getModel(), $s2->getModel() );
299 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
300 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
301 $this->assertSame( $s1->getSize(), $s2->getSize() );
302 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
304 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
305 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
308 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
309 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
310 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
311 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
313 foreach ( $r->getSlotRoles() as $role ) {
314 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
318 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
319 $this->assertTrue( $slot->hasAddress() );
320 $this->assertSame( $r->getId(), $slot->getRevision() );
322 $this->assertInstanceOf( Content
::class, $slot->getContent() );
326 * @param mixed[] $details
328 * @return RevisionRecord
330 private function getRevisionRecordFromDetailsArray( $details = [] ) {
331 // Convert some values that can't be provided by dataProviders
332 if ( isset( $details['user'] ) && $details['user'] === true ) {
333 $details['user'] = $this->getTestUser()->getUser();
335 if ( isset( $details['page'] ) && $details['page'] === true ) {
336 $details['page'] = $this->getTestPage()->getId();
338 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
339 $details['parent'] = $this->getTestPage()->getLatest();
342 // Create the RevisionRecord with any available data
343 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
344 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
345 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
346 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
347 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
348 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
349 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
350 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
351 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
352 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
353 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
354 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
356 if ( isset( $details['content'] ) ) {
357 foreach ( $details['content'] as $role => $content ) {
358 $rev->setContent( $role, $content );
365 public function provideInsertRevisionOn_successes() {
366 yield
'Bare minimum revision insertion' => [
368 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
370 'comment' => $this->getRandomCommentStoreComment(),
371 'timestamp' => '20171117010101',
375 yield
'Detailed revision insertion' => [
377 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
380 'comment' => $this->getRandomCommentStoreComment(),
381 'timestamp' => '20171117010101',
384 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
389 protected function getRandomCommentStoreComment() {
390 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
394 * @dataProvider provideInsertRevisionOn_successes
395 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
396 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
397 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
399 public function testInsertRevisionOn_successes(
400 array $revDetails = []
402 $title = $this->getTestPageTitle();
403 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
405 $this->overrideMwServices();
406 $store = MediaWikiServices
::getInstance()->getRevisionStore();
407 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
409 // is the new revision correct?
410 $this->assertRevisionCompleteness( $return );
411 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
412 $this->assertRevisionRecordsEqual( $rev, $return );
414 // can we load it from the store?
415 $loaded = $store->getRevisionById( $return->getId() );
416 $this->assertRevisionCompleteness( $loaded );
417 $this->assertRevisionRecordsEqual( $return, $loaded );
419 // can we find it directly in the database?
420 $this->assertRevisionExistsInDatabase( $return );
423 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
424 $row = $this->revisionToRow( new Revision( $rev ), [] );
426 // unset nulled fields
427 unset( $row->rev_content_model
);
428 unset( $row->rev_content_format
);
431 unset( $row->rev_comment_text
);
432 unset( $row->rev_comment_data
);
433 unset( $row->rev_comment_cid
);
434 unset( $row->rev_comment_id
);
436 $store = MediaWikiServices
::getInstance()->getRevisionStore();
437 $queryInfo = $store->getQueryInfo( [ 'user' ] );
439 $row = get_object_vars( $row );
441 $queryInfo['tables'],
443 [ 'rev_id' => $rev->getId() ],
444 [ array_values( $row ) ],
451 * @param SlotRecord $a
452 * @param SlotRecord $b
454 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
455 // Assert that the same blob address has been used.
456 $this->assertSame( $a->getAddress(), $b->getAddress() );
460 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
462 public function testInsertRevisionOn_blobAddressExists() {
463 $title = $this->getTestPageTitle();
465 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
467 'comment' => $this->getRandomCommentStoreComment(),
468 'timestamp' => '20171117010101',
472 $this->overrideMwServices();
473 $store = MediaWikiServices
::getInstance()->getRevisionStore();
475 // Insert the first revision
476 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
477 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
478 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
479 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
481 // Insert a second revision inheriting the same blob address
482 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
483 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
484 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
485 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
486 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
488 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
489 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
491 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
493 // And that different revisions have been created.
494 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
496 // Make sure the slot rows reference the correct revision
497 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
498 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
501 public function provideInsertRevisionOn_failures() {
504 'comment' => $this->getRandomCommentStoreComment(),
505 'timestamp' => '20171117010101',
508 new InvalidArgumentException( 'main slot must be provided' )
510 yield
'no main slot' => [
512 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
513 'comment' => $this->getRandomCommentStoreComment(),
514 'timestamp' => '20171117010101',
517 new InvalidArgumentException( 'main slot must be provided' )
519 yield
'no timestamp' => [
521 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
522 'comment' => $this->getRandomCommentStoreComment(),
525 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
527 yield
'no comment' => [
529 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
530 'timestamp' => '20171117010101',
533 new IncompleteRevisionException( 'comment must not be NULL!' )
537 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
538 'comment' => $this->getRandomCommentStoreComment(),
539 'timestamp' => '20171117010101',
541 new IncompleteRevisionException( 'user must not be NULL!' )
546 * @dataProvider provideInsertRevisionOn_failures
547 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
549 public function testInsertRevisionOn_failures(
550 array $revDetails = [],
553 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
555 $store = MediaWikiServices
::getInstance()->getRevisionStore();
557 $this->setExpectedException(
558 get_class( $exception ),
559 $exception->getMessage(),
560 $exception->getCode()
562 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
565 public function provideNewNullRevision() {
567 Title
::newFromText( 'UTPage_notAutoCreated' ),
568 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
569 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
573 Title
::newFromText( 'UTPage_notAutoCreated' ),
574 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
575 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
581 * @dataProvider provideNewNullRevision
582 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
583 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
585 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
586 $this->overrideMwServices();
588 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
589 $page = WikiPage
::factory( $title );
591 if ( !$page->exists() ) {
592 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
595 $revDetails['page'] = $page->getId();
596 $revDetails['timestamp'] = wfTimestampNow();
597 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
598 $revDetails['user'] = $user;
600 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
601 $store = MediaWikiServices
::getInstance()->getRevisionStore();
603 $dbw = wfGetDB( DB_MASTER
);
604 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
605 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
607 $record = $store->newNullRevision(
608 wfGetDB( DB_MASTER
),
615 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
616 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
617 $this->assertEquals( $comment, $record->getComment() );
618 $this->assertEquals( $minor, $record->isMinor() );
619 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
620 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
622 $this->assertArrayEquals(
623 $baseRev->getSlotRoles(),
624 $record->getSlotRoles()
627 foreach ( $baseRev->getSlotRoles() as $role ) {
628 $parentSlot = $baseRev->getSlot( $role );
629 $slot = $record->getSlot( $role );
631 $this->assertTrue( $slot->isInherited(), 'isInherited' );
632 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
633 $this->assertSameSlotContent( $parentSlot, $slot );
638 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
640 public function testNewNullRevision_nonExistingTitle() {
641 $store = MediaWikiServices
::getInstance()->getRevisionStore();
642 $record = $store->newNullRevision(
643 wfGetDB( DB_MASTER
),
644 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
645 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
647 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
649 $this->assertNull( $record );
653 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
655 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
656 $page = $this->getTestPage();
657 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
658 /** @var Revision $rev */
659 $rev = $status->value
['revision'];
661 $store = MediaWikiServices
::getInstance()->getRevisionStore();
662 $revisionRecord = $store->getRevisionById( $rev->getId() );
663 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
665 $this->assertGreaterThan( 0, $result );
667 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
673 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
675 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
676 // This assumes that sysops are auto patrolled
677 $sysop = $this->getTestSysop()->getUser();
678 $page = $this->getTestPage();
679 $status = $page->doEditContent(
680 new WikitextContent( __METHOD__
),
686 /** @var Revision $rev */
687 $rev = $status->value
['revision'];
689 $store = MediaWikiServices
::getInstance()->getRevisionStore();
690 $revisionRecord = $store->getRevisionById( $rev->getId() );
691 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
693 $this->assertSame( 0, $result );
697 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
699 public function testGetRecentChange() {
700 $page = $this->getTestPage();
701 $content = new WikitextContent( __METHOD__
);
702 $status = $page->doEditContent( $content, __METHOD__
);
703 /** @var Revision $rev */
704 $rev = $status->value
['revision'];
706 $store = MediaWikiServices
::getInstance()->getRevisionStore();
707 $revRecord = $store->getRevisionById( $rev->getId() );
708 $recentChange = $store->getRecentChange( $revRecord );
710 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
711 $this->assertEquals( $rev->getRecentChange(), $recentChange );
715 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
717 public function testGetRevisionById() {
718 $page = $this->getTestPage();
719 $content = new WikitextContent( __METHOD__
);
720 $status = $page->doEditContent( $content, __METHOD__
);
721 /** @var Revision $rev */
722 $rev = $status->value
['revision'];
724 $store = MediaWikiServices
::getInstance()->getRevisionStore();
725 $revRecord = $store->getRevisionById( $rev->getId() );
727 $this->assertSame( $rev->getId(), $revRecord->getId() );
728 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
729 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
733 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
735 public function testGetRevisionByTitle() {
736 $page = $this->getTestPage();
737 $content = new WikitextContent( __METHOD__
);
738 $status = $page->doEditContent( $content, __METHOD__
);
739 /** @var Revision $rev */
740 $rev = $status->value
['revision'];
742 $store = MediaWikiServices
::getInstance()->getRevisionStore();
743 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
745 $this->assertSame( $rev->getId(), $revRecord->getId() );
746 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
747 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
751 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
753 public function testGetRevisionByPageId() {
754 $page = $this->getTestPage();
755 $content = new WikitextContent( __METHOD__
);
756 $status = $page->doEditContent( $content, __METHOD__
);
757 /** @var Revision $rev */
758 $rev = $status->value
['revision'];
760 $store = MediaWikiServices
::getInstance()->getRevisionStore();
761 $revRecord = $store->getRevisionByPageId( $page->getId() );
763 $this->assertSame( $rev->getId(), $revRecord->getId() );
764 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
765 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
769 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
771 public function testGetRevisionByTimestamp() {
772 // Make sure there is 1 second between the last revision and the rev we create...
773 // Otherwise we might not get the correct revision and the test may fail...
775 $page = $this->getTestPage();
777 $content = new WikitextContent( __METHOD__
);
778 $status = $page->doEditContent( $content, __METHOD__
);
779 /** @var Revision $rev */
780 $rev = $status->value
['revision'];
782 $store = MediaWikiServices
::getInstance()->getRevisionStore();
783 $revRecord = $store->getRevisionByTimestamp(
788 $this->assertSame( $rev->getId(), $revRecord->getId() );
789 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
790 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
793 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
794 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
795 $page = WikiPage
::factory( $rev->getTitle() );
798 'rev_id' => (string)$rev->getId(),
799 'rev_page' => (string)$rev->getPage(),
800 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
801 'rev_user_text' => (string)$rev->getUserText(),
802 'rev_user' => (string)$rev->getUser(),
803 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
804 'rev_deleted' => (string)$rev->getVisibility(),
805 'rev_len' => (string)$rev->getSize(),
806 'rev_parent_id' => (string)$rev->getParentId(),
807 'rev_sha1' => (string)$rev->getSha1(),
810 if ( in_array( 'page', $options ) ) {
812 'page_namespace' => (string)$page->getTitle()->getNamespace(),
813 'page_title' => $page->getTitle()->getDBkey(),
814 'page_id' => (string)$page->getId(),
815 'page_latest' => (string)$page->getLatest(),
816 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
817 'page_len' => (string)$page->getContent()->getSize(),
821 if ( in_array( 'user', $options ) ) {
823 'user_name' => (string)$rev->getUserText(),
827 if ( in_array( 'comment', $options ) ) {
829 'rev_comment_text' => $rev->getComment(),
830 'rev_comment_data' => null,
831 'rev_comment_cid' => null,
835 if ( $rev->getId() ) {
837 'rev_id' => (string)$rev->getId(),
841 return (object)$fields;
844 protected function assertRevisionRecordMatchesRevision(
846 RevisionRecord
$record
848 $this->assertSame( $rev->getId(), $record->getId() );
849 $this->assertSame( $rev->getPage(), $record->getPageId() );
850 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
851 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
852 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
853 $this->assertSame( $rev->isMinor(), $record->isMinor() );
854 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
855 $this->assertSame( $rev->getSize(), $record->getSize() );
857 * @note As of MW 1.31, the database schema allows the parent ID to be
858 * NULL to indicate that it is unknown.
860 $expectedParent = $rev->getParentId();
861 if ( $expectedParent === null ) {
864 $this->assertSame( $expectedParent, $record->getParentId() );
865 $this->assertSame( $rev->getSha1(), $record->getSha1() );
866 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
867 $this->assertSame( $rev->getContentFormat(),
868 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
869 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
870 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
872 $revRec = $rev->getRevisionRecord();
873 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
874 $recMain = $record->getSlot( SlotRecord
::MAIN
);
876 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
877 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
878 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
880 if ( $revMain->hasOrigin() ) {
881 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
884 if ( $revMain->hasAddress() ) {
885 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
888 if ( $revMain->hasContentId() ) {
889 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
894 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
895 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
897 public function testNewRevisionFromRow_getQueryInfo() {
898 $page = $this->getTestPage();
899 $text = __METHOD__
. 'a-ä';
900 /** @var Revision $rev */
901 $rev = $page->doEditContent(
902 new WikitextContent( $text ),
904 )->value
['revision'];
906 $store = MediaWikiServices
::getInstance()->getRevisionStore();
907 $info = $store->getQueryInfo();
908 $row = $this->db
->selectRow(
911 [ 'rev_id' => $rev->getId() ],
916 $record = $store->newRevisionFromRow(
921 $this->assertRevisionRecordMatchesRevision( $rev, $record );
922 $this->assertSame( $text, $rev->getContent()->serialize() );
926 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
928 public function testNewRevisionFromRow_anonEdit() {
929 $page = $this->getTestPage();
930 $text = __METHOD__
. 'a-ä';
931 /** @var Revision $rev */
932 $rev = $page->doEditContent(
933 new WikitextContent( $text ),
935 )->value
['revision'];
937 $store = MediaWikiServices
::getInstance()->getRevisionStore();
938 $record = $store->newRevisionFromRow(
939 $this->revisionToRow( $rev ),
943 $this->assertRevisionRecordMatchesRevision( $rev, $record );
944 $this->assertSame( $text, $rev->getContent()->serialize() );
948 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
950 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
951 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
952 $this->overrideMwServices();
953 $page = $this->getTestPage();
954 $text = __METHOD__
. 'a-ä';
955 /** @var Revision $rev */
956 $rev = $page->doEditContent(
957 new WikitextContent( $text ),
959 )->value
['revision'];
961 $store = MediaWikiServices
::getInstance()->getRevisionStore();
962 $record = $store->newRevisionFromRow(
963 $this->revisionToRow( $rev ),
967 $this->assertRevisionRecordMatchesRevision( $rev, $record );
968 $this->assertSame( $text, $rev->getContent()->serialize() );
972 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
974 public function testNewRevisionFromRow_userEdit() {
975 $page = $this->getTestPage();
976 $text = __METHOD__
. 'b-ä';
977 /** @var Revision $rev */
978 $rev = $page->doEditContent(
979 new WikitextContent( $text ),
983 $this->getTestUser()->getUser()
984 )->value
['revision'];
986 $store = MediaWikiServices
::getInstance()->getRevisionStore();
987 $record = $store->newRevisionFromRow(
988 $this->revisionToRow( $rev ),
992 $this->assertRevisionRecordMatchesRevision( $rev, $record );
993 $this->assertSame( $text, $rev->getContent()->serialize() );
997 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
998 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
1000 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1001 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1002 $title = Title
::newFromText( __METHOD__
);
1003 $text = __METHOD__
. '-bä';
1004 $page = WikiPage
::factory( $title );
1005 /** @var Revision $orig */
1006 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1007 ->value
['revision'];
1008 $page->doDeleteArticle( __METHOD__
);
1010 $db = wfGetDB( DB_MASTER
);
1011 $arQuery = $store->getArchiveQueryInfo();
1013 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1014 __METHOD__
, [], $arQuery['joins']
1016 $this->assertTrue( is_object( $res ), 'query failed' );
1018 $row = $res->fetchObject();
1020 $record = $store->newRevisionFromArchiveRow( $row );
1022 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1023 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1027 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1029 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1030 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1031 $this->overrideMwServices();
1032 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1033 $title = Title
::newFromText( __METHOD__
);
1034 $text = __METHOD__
. '-bä';
1035 $page = WikiPage
::factory( $title );
1036 /** @var Revision $orig */
1037 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1038 ->value
['revision'];
1039 $page->doDeleteArticle( __METHOD__
);
1041 $db = wfGetDB( DB_MASTER
);
1042 $arQuery = $store->getArchiveQueryInfo();
1044 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1045 __METHOD__
, [], $arQuery['joins']
1047 $this->assertTrue( is_object( $res ), 'query failed' );
1049 $row = $res->fetchObject();
1051 $record = $store->newRevisionFromArchiveRow( $row );
1053 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1054 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1058 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1060 public function testNewRevisionFromArchiveRow_no_user() {
1061 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1065 'ar_page_id' => '2',
1066 'ar_namespace' => '0',
1067 'ar_title' => 'Something',
1069 'ar_text_id' => '47',
1070 'ar_timestamp' => '20180528192356',
1071 'ar_minor_edit' => '0',
1072 'ar_deleted' => '0',
1074 'ar_parent_id' => '0',
1075 'ar_sha1' => 'deadbeef',
1076 'ar_comment_text' => 'whatever',
1077 'ar_comment_data' => null,
1078 'ar_comment_cid' => null,
1080 'ar_user_text' => '', // this is the important bit
1082 'ar_content_format' => null,
1083 'ar_content_model' => null,
1086 \Wikimedia\
suppressWarnings();
1087 $record = $store->newRevisionFromArchiveRow( $row );
1088 \Wikimedia\
suppressWarnings( true );
1090 $this->assertInstanceOf( RevisionRecord
::class, $record );
1091 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1092 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1096 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1098 public function testNewRevisionFromRow_no_user() {
1099 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1100 $title = Title
::newFromText( __METHOD__
);
1105 'page_namespace' => '0',
1106 'page_title' => $title->getText(),
1107 'rev_text_id' => '47',
1108 'rev_timestamp' => '20180528192356',
1109 'rev_minor_edit' => '0',
1110 'rev_deleted' => '0',
1112 'rev_parent_id' => '0',
1113 'rev_sha1' => 'deadbeef',
1114 'rev_comment_text' => 'whatever',
1115 'rev_comment_data' => null,
1116 'rev_comment_cid' => null,
1118 'rev_user_text' => '', // this is the important bit
1119 'rev_actor' => null,
1120 'rev_content_format' => null,
1121 'rev_content_model' => null,
1124 \Wikimedia\
suppressWarnings();
1125 $record = $store->newRevisionFromRow( $row, 0, $title );
1126 \Wikimedia\
suppressWarnings( true );
1128 $this->assertNotNull( $record );
1129 $this->assertNotNull( $record->getUser() );
1130 $this->assertNotEmpty( $record->getUser()->getName() );
1134 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1136 public function testInsertRevisionOn_archive() {
1137 // This is a round trip test for deletion and undeletion of a
1138 // revision row via the archive table.
1140 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1141 $title = Title
::newFromText( __METHOD__
);
1143 $page = WikiPage
::factory( $title );
1144 /** @var Revision $origRev */
1145 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1146 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1147 ->value
['revision'];
1148 $orig = $origRev->getRevisionRecord();
1149 $page->doDeleteArticle( __METHOD__
);
1151 // re-create page, so we can later load revisions for it
1152 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1154 $db = wfGetDB( DB_MASTER
);
1155 $arQuery = $store->getArchiveQueryInfo();
1156 $row = $db->selectRow(
1157 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1158 __METHOD__
, [], $arQuery['joins']
1161 $this->assertNotFalse( $row, 'query failed' );
1163 $record = $store->newRevisionFromArchiveRow(
1167 [ 'page_id' => $title->getArticleID() ]
1170 $restored = $store->insertRevisionOn( $record, $db );
1172 // is the new revision correct?
1173 $this->assertRevisionCompleteness( $restored );
1174 $this->assertRevisionRecordsEqual( $record, $restored );
1176 // does the new revision use the original slot?
1177 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1178 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1179 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1180 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1181 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1182 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1184 // can we load it from the store?
1185 $loaded = $store->getRevisionById( $restored->getId() );
1186 $this->assertNotNull( $loaded );
1187 $this->assertRevisionCompleteness( $loaded );
1188 $this->assertRevisionRecordsEqual( $restored, $loaded );
1190 // can we find it directly in the database?
1191 $this->assertRevisionExistsInDatabase( $restored );
1195 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1197 public function testLoadRevisionFromId() {
1198 $title = Title
::newFromText( __METHOD__
);
1199 $page = WikiPage
::factory( $title );
1200 /** @var Revision $rev */
1201 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1202 ->value
['revision'];
1204 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1205 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1206 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1210 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1212 public function testLoadRevisionFromPageId() {
1213 $title = Title
::newFromText( __METHOD__
);
1214 $page = WikiPage
::factory( $title );
1215 /** @var Revision $rev */
1216 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1217 ->value
['revision'];
1219 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1220 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1221 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1225 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1227 public function testLoadRevisionFromTitle() {
1228 $title = Title
::newFromText( __METHOD__
);
1229 $page = WikiPage
::factory( $title );
1230 /** @var Revision $rev */
1231 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1232 ->value
['revision'];
1234 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1235 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1236 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1240 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1242 public function testLoadRevisionFromTimestamp() {
1243 $title = Title
::newFromText( __METHOD__
);
1244 $page = WikiPage
::factory( $title );
1245 /** @var Revision $revOne */
1246 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1247 ->value
['revision'];
1248 // Sleep to ensure different timestamps... )(evil)
1250 /** @var Revision $revTwo */
1251 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1252 ->value
['revision'];
1254 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1256 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1260 $store->loadRevisionFromTimestamp(
1261 wfGetDB( DB_MASTER
),
1263 $revOne->getTimestamp()
1268 $store->loadRevisionFromTimestamp(
1269 wfGetDB( DB_MASTER
),
1271 $revTwo->getTimestamp()
1277 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1279 public function testGetParentLengths() {
1280 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1281 /** @var Revision $revOne */
1282 $revOne = $page->doEditContent(
1283 new WikitextContent( __METHOD__
), __METHOD__
1284 )->value
['revision'];
1285 /** @var Revision $revTwo */
1286 $revTwo = $page->doEditContent(
1287 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1288 )->value
['revision'];
1290 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1293 $revOne->getId() => strlen( __METHOD__
),
1295 $store->listRevisionSizes(
1296 wfGetDB( DB_MASTER
),
1297 [ $revOne->getId() ]
1302 $revOne->getId() => strlen( __METHOD__
),
1303 $revTwo->getId() => strlen( __METHOD__
) +
1,
1305 $store->listRevisionSizes(
1306 wfGetDB( DB_MASTER
),
1307 [ $revOne->getId(), $revTwo->getId() ]
1313 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1315 public function testGetPreviousRevision() {
1316 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1317 /** @var Revision $revOne */
1318 $revOne = $page->doEditContent(
1319 new WikitextContent( __METHOD__
), __METHOD__
1320 )->value
['revision'];
1321 /** @var Revision $revTwo */
1322 $revTwo = $page->doEditContent(
1323 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1324 )->value
['revision'];
1326 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1328 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1332 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1337 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1339 public function testGetNextRevision() {
1340 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1341 /** @var Revision $revOne */
1342 $revOne = $page->doEditContent(
1343 new WikitextContent( __METHOD__
), __METHOD__
1344 )->value
['revision'];
1345 /** @var Revision $revTwo */
1346 $revTwo = $page->doEditContent(
1347 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1348 )->value
['revision'];
1350 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1353 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1356 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1360 public function provideNonHistoryRevision() {
1361 $title = Title
::newFromText( __METHOD__
);
1362 $rev = new MutableRevisionRecord( $title );
1365 $user = new UserIdentityValue( 7, 'Frank', 0 );
1366 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1369 'ar_rev_id' => 34567,
1372 'ar_minor_edit' => 0,
1373 'ar_timestamp' => '20180101020202',
1375 $slots = new RevisionSlots( [] );
1376 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1381 * @dataProvider provideNonHistoryRevision
1382 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1384 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1385 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1386 $this->assertNull( $store->getPreviousRevision( $rev ) );
1390 * @dataProvider provideNonHistoryRevision
1391 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1393 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1394 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1395 $this->assertNull( $store->getNextRevision( $rev ) );
1399 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1401 public function testGetTimestampFromId_found() {
1402 $page = $this->getTestPage();
1403 /** @var Revision $rev */
1404 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1405 ->value
['revision'];
1407 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1408 $result = $store->getTimestampFromId(
1413 $this->assertSame( $rev->getTimestamp(), $result );
1417 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1419 public function testGetTimestampFromId_notFound() {
1420 $page = $this->getTestPage();
1421 /** @var Revision $rev */
1422 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1423 ->value
['revision'];
1425 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1426 $result = $store->getTimestampFromId(
1431 $this->assertFalse( $result );
1435 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1437 public function testCountRevisionsByPageId() {
1438 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1439 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1443 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1445 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1448 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1450 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1453 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1458 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1460 public function testCountRevisionsByTitle() {
1461 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1462 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1466 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1468 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1471 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1473 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1476 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1481 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1483 public function testUserWasLastToEdit_false() {
1484 $sysop = $this->getTestSysop()->getUser();
1485 $page = $this->getTestPage();
1486 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1488 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1489 $result = $store->userWasLastToEdit(
1490 wfGetDB( DB_MASTER
),
1495 $this->assertFalse( $result );
1499 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1501 public function testUserWasLastToEdit_true() {
1502 $startTime = wfTimestampNow();
1503 $sysop = $this->getTestSysop()->getUser();
1504 $page = $this->getTestPage();
1505 $page->doEditContent(
1506 new WikitextContent( __METHOD__
),
1513 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1514 $result = $store->userWasLastToEdit(
1515 wfGetDB( DB_MASTER
),
1520 $this->assertTrue( $result );
1524 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1526 public function testGetKnownCurrentRevision() {
1527 $page = $this->getTestPage();
1528 /** @var Revision $rev */
1529 $rev = $page->doEditContent(
1530 new WikitextContent( __METHOD__
. 'b' ),
1534 $this->getTestUser()->getUser()
1535 )->value
['revision'];
1537 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1538 $record = $store->getKnownCurrentRevision(
1543 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1546 public function provideNewMutableRevisionFromArray() {
1547 yield
'Basic array, content object' => [
1551 'timestamp' => '20171017114835',
1552 'user_text' => '111.0.1.2',
1554 'minor_edit' => false,
1558 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1559 'comment' => 'Goat Comment!',
1560 'content' => new WikitextContent( 'Some Content' ),
1563 yield
'Basic array, serialized text' => [
1567 'timestamp' => '20171017114835',
1568 'user_text' => '111.0.1.2',
1570 'minor_edit' => false,
1574 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1575 'comment' => 'Goat Comment!',
1576 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1579 yield
'Basic array, serialized text, utf-8 flags' => [
1583 'timestamp' => '20171017114835',
1584 'user_text' => '111.0.1.2',
1586 'minor_edit' => false,
1590 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1591 'comment' => 'Goat Comment!',
1592 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1596 yield
'Basic array, with title' => [
1598 'title' => Title
::newFromText( 'SomeText' ),
1599 'timestamp' => '20171017114835',
1600 'user_text' => '111.0.1.2',
1602 'minor_edit' => false,
1606 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1607 'comment' => 'Goat Comment!',
1608 'content' => new WikitextContent( 'Some Content' ),
1611 yield
'Basic array, no user field' => [
1615 'timestamp' => '20171017114835',
1616 'user_text' => '111.0.1.3',
1617 'minor_edit' => false,
1621 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1622 'comment' => 'Goat Comment!',
1623 'content' => new WikitextContent( 'Some Content' ),
1629 * @dataProvider provideNewMutableRevisionFromArray
1630 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1632 public function testNewMutableRevisionFromArray( array $array ) {
1633 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1635 // HACK: if $array['page'] is given, make sure that the page exists
1636 if ( isset( $array['page'] ) ) {
1637 $t = Title
::newFromID( $array['page'] );
1638 if ( !$t ||
!$t->exists() ) {
1639 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1640 $info = $this->insertPage( $t );
1641 $array['page'] = $info['id'];
1645 $result = $store->newMutableRevisionFromArray( $array );
1647 if ( isset( $array['id'] ) ) {
1648 $this->assertSame( $array['id'], $result->getId() );
1650 if ( isset( $array['page'] ) ) {
1651 $this->assertSame( $array['page'], $result->getPageId() );
1653 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1654 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1655 if ( isset( $array['user'] ) ) {
1656 $this->assertSame( $array['user'], $result->getUser()->getId() );
1658 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1659 $this->assertSame( $array['deleted'], $result->getVisibility() );
1660 $this->assertSame( $array['len'], $result->getSize() );
1661 $this->assertSame( $array['parent_id'], $result->getParentId() );
1662 $this->assertSame( $array['sha1'], $result->getSha1() );
1663 $this->assertSame( $array['comment'], $result->getComment()->text
);
1664 if ( isset( $array['content'] ) ) {
1665 foreach ( $array['content'] as $role => $content ) {
1667 $result->getContent( $role )->equals( $content )
1670 } elseif ( isset( $array['text'] ) ) {
1671 $this->assertSame( $array['text'],
1672 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1673 } elseif ( isset( $array['content_format'] ) ) {
1675 $array['content_format'],
1676 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1678 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1683 * @dataProvider provideNewMutableRevisionFromArray
1684 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1686 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1687 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1688 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1689 $blobStore = new SqlBlobStore( $lb, $cache );
1690 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1692 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1693 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1694 ->disableOriginalConstructor()
1696 $factory->expects( $this->any() )
1697 ->method( 'newBlobStore' )
1698 ->willReturn( $blobStore );
1699 $factory->expects( $this->any() )
1700 ->method( 'newSqlBlobStore' )
1701 ->willReturn( $blobStore );
1703 $this->setService( 'BlobStoreFactory', $factory );
1705 $this->testNewMutableRevisionFromArray( $array );