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_OLD
,
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 $this->getMcrMigrationStage(),
237 MediaWikiServices
::getInstance()->getActorMigration(),
241 $count = $store->countRevisionsByPageId( $db, 0 );
243 // Dummy check to make PhpUnit happy. We are really only interested in
244 // countRevisionsByPageId not failing due to the DB domain check.
245 $this->assertSame( 0, $count );
248 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
249 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
250 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
251 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
252 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
255 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
257 $r1->getPageAsLinkTarget()->getNamespace(),
258 $r2->getPageAsLinkTarget()->getNamespace()
262 $r1->getPageAsLinkTarget()->getText(),
263 $r2->getPageAsLinkTarget()->getText()
266 if ( $r1->getParentId() ) {
267 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
270 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
271 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
272 $this->assertEquals( $r1->getComment(), $r2->getComment() );
273 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
274 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
275 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
276 $this->assertEquals( $r1->getSize(), $r2->getSize() );
277 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
278 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
279 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
280 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
281 foreach ( $r1->getSlotRoles() as $role ) {
282 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
283 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
286 RevisionRecord
::DELETED_TEXT
,
287 RevisionRecord
::DELETED_COMMENT
,
288 RevisionRecord
::DELETED_USER
,
289 RevisionRecord
::DELETED_RESTRICTED
,
291 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
295 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
296 $this->assertSame( $s1->getRole(), $s2->getRole() );
297 $this->assertSame( $s1->getModel(), $s2->getModel() );
298 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
299 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
300 $this->assertSame( $s1->getSize(), $s2->getSize() );
301 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
303 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
304 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
307 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
308 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
309 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
310 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
312 foreach ( $r->getSlotRoles() as $role ) {
313 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
317 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
318 $this->assertTrue( $slot->hasAddress() );
319 $this->assertSame( $r->getId(), $slot->getRevision() );
321 $this->assertInstanceOf( Content
::class, $slot->getContent() );
325 * @param mixed[] $details
327 * @return RevisionRecord
329 private function getRevisionRecordFromDetailsArray( $details = [] ) {
330 // Convert some values that can't be provided by dataProviders
331 if ( isset( $details['user'] ) && $details['user'] === true ) {
332 $details['user'] = $this->getTestUser()->getUser();
334 if ( isset( $details['page'] ) && $details['page'] === true ) {
335 $details['page'] = $this->getTestPage()->getId();
337 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
338 $details['parent'] = $this->getTestPage()->getLatest();
341 // Create the RevisionRecord with any available data
342 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
343 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
344 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
345 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
346 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
347 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
348 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
349 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
350 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
351 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
352 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
353 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
355 if ( isset( $details['content'] ) ) {
356 foreach ( $details['content'] as $role => $content ) {
357 $rev->setContent( $role, $content );
364 public function provideInsertRevisionOn_successes() {
365 yield
'Bare minimum revision insertion' => [
367 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
369 'comment' => $this->getRandomCommentStoreComment(),
370 'timestamp' => '20171117010101',
374 yield
'Detailed revision insertion' => [
376 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
379 'comment' => $this->getRandomCommentStoreComment(),
380 'timestamp' => '20171117010101',
383 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
388 protected function getRandomCommentStoreComment() {
389 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
393 * @dataProvider provideInsertRevisionOn_successes
394 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
395 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
396 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
398 public function testInsertRevisionOn_successes(
399 array $revDetails = []
401 $title = $this->getTestPageTitle();
402 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
404 $this->overrideMwServices();
405 $store = MediaWikiServices
::getInstance()->getRevisionStore();
406 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
408 // is the new revision correct?
409 $this->assertRevisionCompleteness( $return );
410 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
411 $this->assertRevisionRecordsEqual( $rev, $return );
413 // can we load it from the store?
414 $loaded = $store->getRevisionById( $return->getId() );
415 $this->assertRevisionCompleteness( $loaded );
416 $this->assertRevisionRecordsEqual( $return, $loaded );
418 // can we find it directly in the database?
419 $this->assertRevisionExistsInDatabase( $return );
422 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
423 $row = $this->revisionToRow( new Revision( $rev ), [] );
425 // unset nulled fields
426 unset( $row->rev_content_model
);
427 unset( $row->rev_content_format
);
430 unset( $row->rev_comment_text
);
431 unset( $row->rev_comment_data
);
432 unset( $row->rev_comment_cid
);
433 unset( $row->rev_comment_id
);
435 $store = MediaWikiServices
::getInstance()->getRevisionStore();
436 $queryInfo = $store->getQueryInfo( [ 'user' ] );
438 $row = get_object_vars( $row );
440 $queryInfo['tables'],
442 [ 'rev_id' => $rev->getId() ],
443 [ array_values( $row ) ],
450 * @param SlotRecord $a
451 * @param SlotRecord $b
453 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
454 // Assert that the same blob address has been used.
455 $this->assertSame( $a->getAddress(), $b->getAddress() );
459 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
461 public function testInsertRevisionOn_blobAddressExists() {
462 $title = $this->getTestPageTitle();
464 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
466 'comment' => $this->getRandomCommentStoreComment(),
467 'timestamp' => '20171117010101',
471 $this->overrideMwServices();
472 $store = MediaWikiServices
::getInstance()->getRevisionStore();
474 // Insert the first revision
475 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
476 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
477 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
478 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
480 // Insert a second revision inheriting the same blob address
481 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
482 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
483 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
484 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
485 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
487 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
488 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
490 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
492 // And that different revisions have been created.
493 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
495 // Make sure the slot rows reference the correct revision
496 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
497 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
500 public function provideInsertRevisionOn_failures() {
503 'comment' => $this->getRandomCommentStoreComment(),
504 'timestamp' => '20171117010101',
507 new InvalidArgumentException( 'main slot must be provided' )
509 yield
'no main slot' => [
511 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
512 'comment' => $this->getRandomCommentStoreComment(),
513 'timestamp' => '20171117010101',
516 new InvalidArgumentException( 'main slot must be provided' )
518 yield
'no timestamp' => [
520 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
521 'comment' => $this->getRandomCommentStoreComment(),
524 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
526 yield
'no comment' => [
528 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
529 'timestamp' => '20171117010101',
532 new IncompleteRevisionException( 'comment must not be NULL!' )
536 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
537 'comment' => $this->getRandomCommentStoreComment(),
538 'timestamp' => '20171117010101',
540 new IncompleteRevisionException( 'user must not be NULL!' )
545 * @dataProvider provideInsertRevisionOn_failures
546 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
548 public function testInsertRevisionOn_failures(
549 array $revDetails = [],
552 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
554 $store = MediaWikiServices
::getInstance()->getRevisionStore();
556 $this->setExpectedException(
557 get_class( $exception ),
558 $exception->getMessage(),
559 $exception->getCode()
561 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
564 public function provideNewNullRevision() {
566 Title
::newFromText( 'UTPage_notAutoCreated' ),
567 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
568 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
572 Title
::newFromText( 'UTPage_notAutoCreated' ),
573 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
574 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
580 * @dataProvider provideNewNullRevision
581 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
582 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
584 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
585 $this->overrideMwServices();
587 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
588 $page = WikiPage
::factory( $title );
590 if ( !$page->exists() ) {
591 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
594 $revDetails['page'] = $page->getId();
595 $revDetails['timestamp'] = wfTimestampNow();
596 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
597 $revDetails['user'] = $user;
599 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
600 $store = MediaWikiServices
::getInstance()->getRevisionStore();
602 $dbw = wfGetDB( DB_MASTER
);
603 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
604 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
606 $record = $store->newNullRevision(
607 wfGetDB( DB_MASTER
),
614 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
615 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
616 $this->assertEquals( $comment, $record->getComment() );
617 $this->assertEquals( $minor, $record->isMinor() );
618 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
619 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
621 $this->assertArrayEquals(
622 $baseRev->getSlotRoles(),
623 $record->getSlotRoles()
626 foreach ( $baseRev->getSlotRoles() as $role ) {
627 $parentSlot = $baseRev->getSlot( $role );
628 $slot = $record->getSlot( $role );
630 $this->assertTrue( $slot->isInherited(), 'isInherited' );
631 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
632 $this->assertSameSlotContent( $parentSlot, $slot );
637 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
639 public function testNewNullRevision_nonExistingTitle() {
640 $store = MediaWikiServices
::getInstance()->getRevisionStore();
641 $record = $store->newNullRevision(
642 wfGetDB( DB_MASTER
),
643 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
644 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
646 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
648 $this->assertNull( $record );
652 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
654 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
655 $page = $this->getTestPage();
656 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
657 /** @var Revision $rev */
658 $rev = $status->value
['revision'];
660 $store = MediaWikiServices
::getInstance()->getRevisionStore();
661 $revisionRecord = $store->getRevisionById( $rev->getId() );
662 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
664 $this->assertGreaterThan( 0, $result );
666 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
672 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
674 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
675 // This assumes that sysops are auto patrolled
676 $sysop = $this->getTestSysop()->getUser();
677 $page = $this->getTestPage();
678 $status = $page->doEditContent(
679 new WikitextContent( __METHOD__
),
685 /** @var Revision $rev */
686 $rev = $status->value
['revision'];
688 $store = MediaWikiServices
::getInstance()->getRevisionStore();
689 $revisionRecord = $store->getRevisionById( $rev->getId() );
690 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
692 $this->assertSame( 0, $result );
696 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
698 public function testGetRecentChange() {
699 $page = $this->getTestPage();
700 $content = new WikitextContent( __METHOD__
);
701 $status = $page->doEditContent( $content, __METHOD__
);
702 /** @var Revision $rev */
703 $rev = $status->value
['revision'];
705 $store = MediaWikiServices
::getInstance()->getRevisionStore();
706 $revRecord = $store->getRevisionById( $rev->getId() );
707 $recentChange = $store->getRecentChange( $revRecord );
709 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
710 $this->assertEquals( $rev->getRecentChange(), $recentChange );
714 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
716 public function testGetRevisionById() {
717 $page = $this->getTestPage();
718 $content = new WikitextContent( __METHOD__
);
719 $status = $page->doEditContent( $content, __METHOD__
);
720 /** @var Revision $rev */
721 $rev = $status->value
['revision'];
723 $store = MediaWikiServices
::getInstance()->getRevisionStore();
724 $revRecord = $store->getRevisionById( $rev->getId() );
726 $this->assertSame( $rev->getId(), $revRecord->getId() );
727 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
728 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
732 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
734 public function testGetRevisionByTitle() {
735 $page = $this->getTestPage();
736 $content = new WikitextContent( __METHOD__
);
737 $status = $page->doEditContent( $content, __METHOD__
);
738 /** @var Revision $rev */
739 $rev = $status->value
['revision'];
741 $store = MediaWikiServices
::getInstance()->getRevisionStore();
742 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
744 $this->assertSame( $rev->getId(), $revRecord->getId() );
745 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
746 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
750 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
752 public function testGetRevisionByPageId() {
753 $page = $this->getTestPage();
754 $content = new WikitextContent( __METHOD__
);
755 $status = $page->doEditContent( $content, __METHOD__
);
756 /** @var Revision $rev */
757 $rev = $status->value
['revision'];
759 $store = MediaWikiServices
::getInstance()->getRevisionStore();
760 $revRecord = $store->getRevisionByPageId( $page->getId() );
762 $this->assertSame( $rev->getId(), $revRecord->getId() );
763 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
764 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
768 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
770 public function testGetRevisionByTimestamp() {
771 // Make sure there is 1 second between the last revision and the rev we create...
772 // Otherwise we might not get the correct revision and the test may fail...
774 $page = $this->getTestPage();
776 $content = new WikitextContent( __METHOD__
);
777 $status = $page->doEditContent( $content, __METHOD__
);
778 /** @var Revision $rev */
779 $rev = $status->value
['revision'];
781 $store = MediaWikiServices
::getInstance()->getRevisionStore();
782 $revRecord = $store->getRevisionByTimestamp(
787 $this->assertSame( $rev->getId(), $revRecord->getId() );
788 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
789 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
792 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
793 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
794 $page = WikiPage
::factory( $rev->getTitle() );
797 'rev_id' => (string)$rev->getId(),
798 'rev_page' => (string)$rev->getPage(),
799 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
800 'rev_user_text' => (string)$rev->getUserText(),
801 'rev_user' => (string)$rev->getUser(),
802 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
803 'rev_deleted' => (string)$rev->getVisibility(),
804 'rev_len' => (string)$rev->getSize(),
805 'rev_parent_id' => (string)$rev->getParentId(),
806 'rev_sha1' => (string)$rev->getSha1(),
809 if ( in_array( 'page', $options ) ) {
811 'page_namespace' => (string)$page->getTitle()->getNamespace(),
812 'page_title' => $page->getTitle()->getDBkey(),
813 'page_id' => (string)$page->getId(),
814 'page_latest' => (string)$page->getLatest(),
815 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
816 'page_len' => (string)$page->getContent()->getSize(),
820 if ( in_array( 'user', $options ) ) {
822 'user_name' => (string)$rev->getUserText(),
826 if ( in_array( 'comment', $options ) ) {
828 'rev_comment_text' => $rev->getComment(),
829 'rev_comment_data' => null,
830 'rev_comment_cid' => null,
834 if ( $rev->getId() ) {
836 'rev_id' => (string)$rev->getId(),
840 return (object)$fields;
843 protected function assertRevisionRecordMatchesRevision(
845 RevisionRecord
$record
847 $this->assertSame( $rev->getId(), $record->getId() );
848 $this->assertSame( $rev->getPage(), $record->getPageId() );
849 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
850 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
851 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
852 $this->assertSame( $rev->isMinor(), $record->isMinor() );
853 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
854 $this->assertSame( $rev->getSize(), $record->getSize() );
856 * @note As of MW 1.31, the database schema allows the parent ID to be
857 * NULL to indicate that it is unknown.
859 $expectedParent = $rev->getParentId();
860 if ( $expectedParent === null ) {
863 $this->assertSame( $expectedParent, $record->getParentId() );
864 $this->assertSame( $rev->getSha1(), $record->getSha1() );
865 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
866 $this->assertSame( $rev->getContentFormat(),
867 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
868 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
869 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
871 $revRec = $rev->getRevisionRecord();
872 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
873 $recMain = $record->getSlot( SlotRecord
::MAIN
);
875 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
876 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
877 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
879 if ( $revMain->hasOrigin() ) {
880 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
883 if ( $revMain->hasAddress() ) {
884 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
887 if ( $revMain->hasContentId() ) {
888 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
893 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
894 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
896 public function testNewRevisionFromRow_getQueryInfo() {
897 $page = $this->getTestPage();
898 $text = __METHOD__
. 'a-ä';
899 /** @var Revision $rev */
900 $rev = $page->doEditContent(
901 new WikitextContent( $text ),
903 )->value
['revision'];
905 $store = MediaWikiServices
::getInstance()->getRevisionStore();
906 $info = $store->getQueryInfo();
907 $row = $this->db
->selectRow(
910 [ 'rev_id' => $rev->getId() ],
915 $record = $store->newRevisionFromRow(
920 $this->assertRevisionRecordMatchesRevision( $rev, $record );
921 $this->assertSame( $text, $rev->getContent()->serialize() );
925 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
927 public function testNewRevisionFromRow_anonEdit() {
928 $page = $this->getTestPage();
929 $text = __METHOD__
. 'a-ä';
930 /** @var Revision $rev */
931 $rev = $page->doEditContent(
932 new WikitextContent( $text ),
934 )->value
['revision'];
936 $store = MediaWikiServices
::getInstance()->getRevisionStore();
937 $record = $store->newRevisionFromRow(
938 $this->revisionToRow( $rev ),
942 $this->assertRevisionRecordMatchesRevision( $rev, $record );
943 $this->assertSame( $text, $rev->getContent()->serialize() );
947 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
949 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
950 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
951 $this->overrideMwServices();
952 $page = $this->getTestPage();
953 $text = __METHOD__
. 'a-ä';
954 /** @var Revision $rev */
955 $rev = $page->doEditContent(
956 new WikitextContent( $text ),
958 )->value
['revision'];
960 $store = MediaWikiServices
::getInstance()->getRevisionStore();
961 $record = $store->newRevisionFromRow(
962 $this->revisionToRow( $rev ),
966 $this->assertRevisionRecordMatchesRevision( $rev, $record );
967 $this->assertSame( $text, $rev->getContent()->serialize() );
971 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
973 public function testNewRevisionFromRow_userEdit() {
974 $page = $this->getTestPage();
975 $text = __METHOD__
. 'b-ä';
976 /** @var Revision $rev */
977 $rev = $page->doEditContent(
978 new WikitextContent( $text ),
982 $this->getTestUser()->getUser()
983 )->value
['revision'];
985 $store = MediaWikiServices
::getInstance()->getRevisionStore();
986 $record = $store->newRevisionFromRow(
987 $this->revisionToRow( $rev ),
991 $this->assertRevisionRecordMatchesRevision( $rev, $record );
992 $this->assertSame( $text, $rev->getContent()->serialize() );
996 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
997 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
999 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
1000 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1001 $title = Title
::newFromText( __METHOD__
);
1002 $text = __METHOD__
. '-bä';
1003 $page = WikiPage
::factory( $title );
1004 /** @var Revision $orig */
1005 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1006 ->value
['revision'];
1007 $page->doDeleteArticle( __METHOD__
);
1009 $db = wfGetDB( DB_MASTER
);
1010 $arQuery = $store->getArchiveQueryInfo();
1012 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1013 __METHOD__
, [], $arQuery['joins']
1015 $this->assertTrue( is_object( $res ), 'query failed' );
1017 $row = $res->fetchObject();
1019 $record = $store->newRevisionFromArchiveRow( $row );
1021 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1022 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1026 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1028 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1029 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1030 $this->overrideMwServices();
1031 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1032 $title = Title
::newFromText( __METHOD__
);
1033 $text = __METHOD__
. '-bä';
1034 $page = WikiPage
::factory( $title );
1035 /** @var Revision $orig */
1036 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1037 ->value
['revision'];
1038 $page->doDeleteArticle( __METHOD__
);
1040 $db = wfGetDB( DB_MASTER
);
1041 $arQuery = $store->getArchiveQueryInfo();
1043 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1044 __METHOD__
, [], $arQuery['joins']
1046 $this->assertTrue( is_object( $res ), 'query failed' );
1048 $row = $res->fetchObject();
1050 $record = $store->newRevisionFromArchiveRow( $row );
1052 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1053 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1057 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1059 public function testNewRevisionFromArchiveRow_no_user() {
1060 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1064 'ar_page_id' => '2',
1065 'ar_namespace' => '0',
1066 'ar_title' => 'Something',
1068 'ar_text_id' => '47',
1069 'ar_timestamp' => '20180528192356',
1070 'ar_minor_edit' => '0',
1071 'ar_deleted' => '0',
1073 'ar_parent_id' => '0',
1074 'ar_sha1' => 'deadbeef',
1075 'ar_comment_text' => 'whatever',
1076 'ar_comment_data' => null,
1077 'ar_comment_cid' => null,
1079 'ar_user_text' => '', // this is the important bit
1081 'ar_content_format' => null,
1082 'ar_content_model' => null,
1085 \Wikimedia\
suppressWarnings();
1086 $record = $store->newRevisionFromArchiveRow( $row );
1087 \Wikimedia\
suppressWarnings( true );
1089 $this->assertInstanceOf( RevisionRecord
::class, $record );
1090 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1091 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1095 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1097 public function testNewRevisionFromRow_no_user() {
1098 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1099 $title = Title
::newFromText( __METHOD__
);
1104 'page_namespace' => '0',
1105 'page_title' => $title->getText(),
1106 'rev_text_id' => '47',
1107 'rev_timestamp' => '20180528192356',
1108 'rev_minor_edit' => '0',
1109 'rev_deleted' => '0',
1111 'rev_parent_id' => '0',
1112 'rev_sha1' => 'deadbeef',
1113 'rev_comment_text' => 'whatever',
1114 'rev_comment_data' => null,
1115 'rev_comment_cid' => null,
1117 'rev_user_text' => '', // this is the important bit
1118 'rev_actor' => null,
1119 'rev_content_format' => null,
1120 'rev_content_model' => null,
1123 \Wikimedia\
suppressWarnings();
1124 $record = $store->newRevisionFromRow( $row, 0, $title );
1125 \Wikimedia\
suppressWarnings( true );
1127 $this->assertNotNull( $record );
1128 $this->assertNotNull( $record->getUser() );
1129 $this->assertNotEmpty( $record->getUser()->getName() );
1133 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1135 public function testInsertRevisionOn_archive() {
1136 // This is a round trip test for deletion and undeletion of a
1137 // revision row via the archive table.
1139 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1140 $title = Title
::newFromText( __METHOD__
);
1142 $page = WikiPage
::factory( $title );
1143 /** @var Revision $origRev */
1144 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1145 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1146 ->value
['revision'];
1147 $orig = $origRev->getRevisionRecord();
1148 $page->doDeleteArticle( __METHOD__
);
1150 // re-create page, so we can later load revisions for it
1151 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1153 $db = wfGetDB( DB_MASTER
);
1154 $arQuery = $store->getArchiveQueryInfo();
1155 $row = $db->selectRow(
1156 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1157 __METHOD__
, [], $arQuery['joins']
1160 $this->assertNotFalse( $row, 'query failed' );
1162 $record = $store->newRevisionFromArchiveRow(
1166 [ 'page_id' => $title->getArticleID() ]
1169 $restored = $store->insertRevisionOn( $record, $db );
1171 // is the new revision correct?
1172 $this->assertRevisionCompleteness( $restored );
1173 $this->assertRevisionRecordsEqual( $record, $restored );
1175 // does the new revision use the original slot?
1176 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1177 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1178 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1179 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1180 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1181 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1183 // can we load it from the store?
1184 $loaded = $store->getRevisionById( $restored->getId() );
1185 $this->assertNotNull( $loaded );
1186 $this->assertRevisionCompleteness( $loaded );
1187 $this->assertRevisionRecordsEqual( $restored, $loaded );
1189 // can we find it directly in the database?
1190 $this->assertRevisionExistsInDatabase( $restored );
1194 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1196 public function testLoadRevisionFromId() {
1197 $title = Title
::newFromText( __METHOD__
);
1198 $page = WikiPage
::factory( $title );
1199 /** @var Revision $rev */
1200 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1201 ->value
['revision'];
1203 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1204 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1205 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1209 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1211 public function testLoadRevisionFromPageId() {
1212 $title = Title
::newFromText( __METHOD__
);
1213 $page = WikiPage
::factory( $title );
1214 /** @var Revision $rev */
1215 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1216 ->value
['revision'];
1218 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1219 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1220 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1224 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1226 public function testLoadRevisionFromTitle() {
1227 $title = Title
::newFromText( __METHOD__
);
1228 $page = WikiPage
::factory( $title );
1229 /** @var Revision $rev */
1230 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1231 ->value
['revision'];
1233 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1234 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1235 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1239 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1241 public function testLoadRevisionFromTimestamp() {
1242 $title = Title
::newFromText( __METHOD__
);
1243 $page = WikiPage
::factory( $title );
1244 /** @var Revision $revOne */
1245 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1246 ->value
['revision'];
1247 // Sleep to ensure different timestamps... )(evil)
1249 /** @var Revision $revTwo */
1250 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1251 ->value
['revision'];
1253 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1255 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1259 $store->loadRevisionFromTimestamp(
1260 wfGetDB( DB_MASTER
),
1262 $revOne->getTimestamp()
1267 $store->loadRevisionFromTimestamp(
1268 wfGetDB( DB_MASTER
),
1270 $revTwo->getTimestamp()
1276 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1278 public function testGetParentLengths() {
1279 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1280 /** @var Revision $revOne */
1281 $revOne = $page->doEditContent(
1282 new WikitextContent( __METHOD__
), __METHOD__
1283 )->value
['revision'];
1284 /** @var Revision $revTwo */
1285 $revTwo = $page->doEditContent(
1286 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1287 )->value
['revision'];
1289 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1292 $revOne->getId() => strlen( __METHOD__
),
1294 $store->listRevisionSizes(
1295 wfGetDB( DB_MASTER
),
1296 [ $revOne->getId() ]
1301 $revOne->getId() => strlen( __METHOD__
),
1302 $revTwo->getId() => strlen( __METHOD__
) +
1,
1304 $store->listRevisionSizes(
1305 wfGetDB( DB_MASTER
),
1306 [ $revOne->getId(), $revTwo->getId() ]
1312 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1314 public function testGetPreviousRevision() {
1315 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1316 /** @var Revision $revOne */
1317 $revOne = $page->doEditContent(
1318 new WikitextContent( __METHOD__
), __METHOD__
1319 )->value
['revision'];
1320 /** @var Revision $revTwo */
1321 $revTwo = $page->doEditContent(
1322 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1323 )->value
['revision'];
1325 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1327 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1331 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1336 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1338 public function testGetNextRevision() {
1339 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1340 /** @var Revision $revOne */
1341 $revOne = $page->doEditContent(
1342 new WikitextContent( __METHOD__
), __METHOD__
1343 )->value
['revision'];
1344 /** @var Revision $revTwo */
1345 $revTwo = $page->doEditContent(
1346 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1347 )->value
['revision'];
1349 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1352 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1355 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1359 public function provideNonHistoryRevision() {
1360 $title = Title
::newFromText( __METHOD__
);
1361 $rev = new MutableRevisionRecord( $title );
1364 $user = new UserIdentityValue( 7, 'Frank', 0 );
1365 $comment = CommentStoreComment
::newUnsavedComment( 'Test' );
1368 'ar_rev_id' => 34567,
1371 'ar_minor_edit' => 0,
1372 'ar_timestamp' => '20180101020202',
1374 $slots = new RevisionSlots( [] );
1375 $rev = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots );
1380 * @dataProvider provideNonHistoryRevision
1381 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1383 public function testGetPreviousRevision_bad( RevisionRecord
$rev ) {
1384 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1385 $this->assertNull( $store->getPreviousRevision( $rev ) );
1389 * @dataProvider provideNonHistoryRevision
1390 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1392 public function testGetNextRevision_bad( RevisionRecord
$rev ) {
1393 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1394 $this->assertNull( $store->getNextRevision( $rev ) );
1398 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1400 public function testGetTimestampFromId_found() {
1401 $page = $this->getTestPage();
1402 /** @var Revision $rev */
1403 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1404 ->value
['revision'];
1406 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1407 $result = $store->getTimestampFromId(
1412 $this->assertSame( $rev->getTimestamp(), $result );
1416 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1418 public function testGetTimestampFromId_notFound() {
1419 $page = $this->getTestPage();
1420 /** @var Revision $rev */
1421 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1422 ->value
['revision'];
1424 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1425 $result = $store->getTimestampFromId(
1430 $this->assertFalse( $result );
1434 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1436 public function testCountRevisionsByPageId() {
1437 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1438 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1442 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1444 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1447 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1449 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1452 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1457 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1459 public function testCountRevisionsByTitle() {
1460 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1461 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1465 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1467 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1470 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1472 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1475 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1480 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1482 public function testUserWasLastToEdit_false() {
1483 $sysop = $this->getTestSysop()->getUser();
1484 $page = $this->getTestPage();
1485 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1487 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1488 $result = $store->userWasLastToEdit(
1489 wfGetDB( DB_MASTER
),
1494 $this->assertFalse( $result );
1498 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1500 public function testUserWasLastToEdit_true() {
1501 $startTime = wfTimestampNow();
1502 $sysop = $this->getTestSysop()->getUser();
1503 $page = $this->getTestPage();
1504 $page->doEditContent(
1505 new WikitextContent( __METHOD__
),
1512 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1513 $result = $store->userWasLastToEdit(
1514 wfGetDB( DB_MASTER
),
1519 $this->assertTrue( $result );
1523 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1525 public function testGetKnownCurrentRevision() {
1526 $page = $this->getTestPage();
1527 /** @var Revision $rev */
1528 $rev = $page->doEditContent(
1529 new WikitextContent( __METHOD__
. 'b' ),
1533 $this->getTestUser()->getUser()
1534 )->value
['revision'];
1536 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1537 $record = $store->getKnownCurrentRevision(
1542 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1545 public function provideNewMutableRevisionFromArray() {
1546 yield
'Basic array, content object' => [
1550 'timestamp' => '20171017114835',
1551 'user_text' => '111.0.1.2',
1553 'minor_edit' => false,
1557 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1558 'comment' => 'Goat Comment!',
1559 'content' => new WikitextContent( 'Some Content' ),
1562 yield
'Basic array, serialized text' => [
1566 'timestamp' => '20171017114835',
1567 'user_text' => '111.0.1.2',
1569 'minor_edit' => false,
1573 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1574 'comment' => 'Goat Comment!',
1575 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1578 yield
'Basic array, serialized text, utf-8 flags' => [
1582 'timestamp' => '20171017114835',
1583 'user_text' => '111.0.1.2',
1585 'minor_edit' => false,
1589 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1590 'comment' => 'Goat Comment!',
1591 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1595 yield
'Basic array, with title' => [
1597 'title' => Title
::newFromText( 'SomeText' ),
1598 'timestamp' => '20171017114835',
1599 'user_text' => '111.0.1.2',
1601 'minor_edit' => false,
1605 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1606 'comment' => 'Goat Comment!',
1607 'content' => new WikitextContent( 'Some Content' ),
1610 yield
'Basic array, no user field' => [
1614 'timestamp' => '20171017114835',
1615 'user_text' => '111.0.1.3',
1616 'minor_edit' => false,
1620 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1621 'comment' => 'Goat Comment!',
1622 'content' => new WikitextContent( 'Some Content' ),
1628 * @dataProvider provideNewMutableRevisionFromArray
1629 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1631 public function testNewMutableRevisionFromArray( array $array ) {
1632 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1634 // HACK: if $array['page'] is given, make sure that the page exists
1635 if ( isset( $array['page'] ) ) {
1636 $t = Title
::newFromID( $array['page'] );
1637 if ( !$t ||
!$t->exists() ) {
1638 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1639 $info = $this->insertPage( $t );
1640 $array['page'] = $info['id'];
1644 $result = $store->newMutableRevisionFromArray( $array );
1646 if ( isset( $array['id'] ) ) {
1647 $this->assertSame( $array['id'], $result->getId() );
1649 if ( isset( $array['page'] ) ) {
1650 $this->assertSame( $array['page'], $result->getPageId() );
1652 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1653 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1654 if ( isset( $array['user'] ) ) {
1655 $this->assertSame( $array['user'], $result->getUser()->getId() );
1657 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1658 $this->assertSame( $array['deleted'], $result->getVisibility() );
1659 $this->assertSame( $array['len'], $result->getSize() );
1660 $this->assertSame( $array['parent_id'], $result->getParentId() );
1661 $this->assertSame( $array['sha1'], $result->getSha1() );
1662 $this->assertSame( $array['comment'], $result->getComment()->text
);
1663 if ( isset( $array['content'] ) ) {
1664 foreach ( $array['content'] as $role => $content ) {
1666 $result->getContent( $role )->equals( $content )
1669 } elseif ( isset( $array['text'] ) ) {
1670 $this->assertSame( $array['text'],
1671 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1672 } elseif ( isset( $array['content_format'] ) ) {
1674 $array['content_format'],
1675 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1677 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1682 * @dataProvider provideNewMutableRevisionFromArray
1683 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1685 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1686 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1687 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1688 $blobStore = new SqlBlobStore( $lb, $cache );
1689 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1691 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1692 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1693 ->disableOriginalConstructor()
1695 $factory->expects( $this->any() )
1696 ->method( 'newBlobStore' )
1697 ->willReturn( $blobStore );
1698 $factory->expects( $this->any() )
1699 ->method( 'newSqlBlobStore' )
1700 ->willReturn( $blobStore );
1702 $this->setService( 'BlobStoreFactory', $factory );
1704 $this->testNewMutableRevisionFromArray( $array );