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\RevisionRecord
;
16 use MediaWiki\Revision\RevisionStore
;
17 use MediaWiki\Revision\SlotRecord
;
18 use MediaWiki\Storage\BlobStoreFactory
;
19 use MediaWiki\Storage\SqlBlobStore
;
20 use MediaWiki\User\UserIdentityValue
;
21 use MediaWikiTestCase
;
22 use PHPUnit_Framework_MockObject_MockObject
;
27 use Wikimedia\Rdbms\Database
;
28 use Wikimedia\Rdbms\DatabaseDomain
;
29 use Wikimedia\Rdbms\DatabaseSqlite
;
30 use Wikimedia\Rdbms\FakeResultWrapper
;
31 use Wikimedia\Rdbms\LoadBalancer
;
32 use Wikimedia\Rdbms\TransactionProfiler
;
38 * @group RevisionStore
40 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
45 private $testPageTitle;
55 abstract protected function getMcrMigrationStage();
60 protected function getContentHandlerUseDB() {
67 abstract protected function getMcrTablesToReset();
69 public function setUp() {
71 $this->tablesUsed
[] = 'archive';
72 $this->tablesUsed
[] = 'page';
73 $this->tablesUsed
[] = 'revision';
74 $this->tablesUsed
[] = 'comment';
76 $this->tablesUsed +
= $this->getMcrTablesToReset();
78 $this->setMwGlobals( [
79 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
80 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
81 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
82 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD
,
85 $this->overrideMwServices();
88 protected function addCoreDBData() {
89 // Blank out. This would fail with a modified schema, and we don't need it.
95 protected function getTestPageTitle() {
96 if ( $this->testPageTitle
) {
97 return $this->testPageTitle
;
100 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
101 return $this->testPageTitle
;
106 protected function getTestPage() {
107 if ( $this->testPage
) {
108 return $this->testPage
;
111 $title = $this->getTestPageTitle();
112 $this->testPage
= WikiPage
::factory( $title );
114 if ( !$this->testPage
->exists() ) {
115 // Make sure we don't write to the live db.
116 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
118 $user = static::getTestSysop()->getUser();
120 $this->testPage
->doEditContent(
121 new WikitextContent( 'UTContent-' . __CLASS__
),
122 'UTPageSummary-' . __CLASS__
,
123 EDIT_NEW | EDIT_SUPPRESS_RC
,
129 return $this->testPage
;
133 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
135 private function getLoadBalancerMock( array $server ) {
136 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
138 $lb = $this->getMockBuilder( LoadBalancer
::class )
139 ->setMethods( [ 'reallyOpenConnection' ] )
140 ->setConstructorArgs( [
141 [ 'servers' => [ $server ], 'localDomain' => $domain ]
145 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
146 function ( array $server, $dbNameOverride ) {
147 return $this->getDatabaseMock( $server );
155 * @return Database|PHPUnit_Framework_MockObject_MockObject
157 private function getDatabaseMock( array $params ) {
158 $db = $this->getMockBuilder( DatabaseSqlite
::class )
159 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
160 ->setConstructorArgs( [ $params ] )
163 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
164 $db->method( 'isOpen' )->willReturn( true );
169 public function provideDomainCheck() {
170 yield
[ false, 'test', '' ];
171 yield
[ 'test', 'test', '' ];
173 yield
[ false, 'test', 'foo_' ];
174 yield
[ 'test-foo_', 'test', 'foo_' ];
176 yield
[ false, 'dash-test', '' ];
177 yield
[ 'dash-test', 'dash-test', '' ];
179 yield
[ false, 'underscore_test', 'foo_' ];
180 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
184 * @dataProvider provideDomainCheck
185 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
187 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
190 'wgDBname' => $dbName,
191 'wgDBprefix' => $dbPrefix,
195 $loadBalancer = $this->getLoadBalancerMock(
198 'dbDirectory' => '*dummy*',
200 'password' => 'test',
208 'trxProfiler' => new TransactionProfiler(),
209 'connLogger' => new \Psr\Log\
NullLogger(),
210 'queryLogger' => new \Psr\Log\
NullLogger(),
211 'errorLogger' => function () {
213 'deprecationLogger' => function () {
217 'tablePrefix' => $dbPrefix,
220 $db = $loadBalancer->getConnection( DB_REPLICA
);
222 /** @var SqlBlobStore $blobStore */
223 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
224 ->disableOriginalConstructor()
227 $store = new RevisionStore(
230 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
231 MediaWikiServices
::getInstance()->getCommentStore(),
232 MediaWikiServices
::getInstance()->getContentModelStore(),
233 MediaWikiServices
::getInstance()->getSlotRoleStore(),
234 $this->getMcrMigrationStage(),
235 MediaWikiServices
::getInstance()->getActorMigration(),
239 $count = $store->countRevisionsByPageId( $db, 0 );
241 // Dummy check to make PhpUnit happy. We are really only interested in
242 // countRevisionsByPageId not failing due to the DB domain check.
243 $this->assertSame( 0, $count );
246 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
247 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
248 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
249 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
250 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
253 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
255 $r1->getPageAsLinkTarget()->getNamespace(),
256 $r2->getPageAsLinkTarget()->getNamespace()
260 $r1->getPageAsLinkTarget()->getText(),
261 $r2->getPageAsLinkTarget()->getText()
264 if ( $r1->getParentId() ) {
265 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
268 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
269 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
270 $this->assertEquals( $r1->getComment(), $r2->getComment() );
271 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
272 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
273 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
274 $this->assertEquals( $r1->getSize(), $r2->getSize() );
275 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
276 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
277 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
278 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
279 foreach ( $r1->getSlotRoles() as $role ) {
280 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
281 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
284 RevisionRecord
::DELETED_TEXT
,
285 RevisionRecord
::DELETED_COMMENT
,
286 RevisionRecord
::DELETED_USER
,
287 RevisionRecord
::DELETED_RESTRICTED
,
289 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
293 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
294 $this->assertSame( $s1->getRole(), $s2->getRole() );
295 $this->assertSame( $s1->getModel(), $s2->getModel() );
296 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
297 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
298 $this->assertSame( $s1->getSize(), $s2->getSize() );
299 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
301 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
302 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
305 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
306 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
307 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
308 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
310 foreach ( $r->getSlotRoles() as $role ) {
311 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
315 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
316 $this->assertTrue( $slot->hasAddress() );
317 $this->assertSame( $r->getId(), $slot->getRevision() );
319 $this->assertInstanceOf( Content
::class, $slot->getContent() );
323 * @param mixed[] $details
325 * @return RevisionRecord
327 private function getRevisionRecordFromDetailsArray( $details = [] ) {
328 // Convert some values that can't be provided by dataProviders
329 if ( isset( $details['user'] ) && $details['user'] === true ) {
330 $details['user'] = $this->getTestUser()->getUser();
332 if ( isset( $details['page'] ) && $details['page'] === true ) {
333 $details['page'] = $this->getTestPage()->getId();
335 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
336 $details['parent'] = $this->getTestPage()->getLatest();
339 // Create the RevisionRecord with any available data
340 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
341 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
342 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
343 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
344 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
345 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
346 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
347 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
348 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
349 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
350 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
351 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
353 if ( isset( $details['content'] ) ) {
354 foreach ( $details['content'] as $role => $content ) {
355 $rev->setContent( $role, $content );
362 public function provideInsertRevisionOn_successes() {
363 yield
'Bare minimum revision insertion' => [
365 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
367 'comment' => $this->getRandomCommentStoreComment(),
368 'timestamp' => '20171117010101',
372 yield
'Detailed revision insertion' => [
374 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
377 'comment' => $this->getRandomCommentStoreComment(),
378 'timestamp' => '20171117010101',
381 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
386 protected function getRandomCommentStoreComment() {
387 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
391 * @dataProvider provideInsertRevisionOn_successes
392 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
393 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
394 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
396 public function testInsertRevisionOn_successes(
397 array $revDetails = []
399 $title = $this->getTestPageTitle();
400 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
402 $this->overrideMwServices();
403 $store = MediaWikiServices
::getInstance()->getRevisionStore();
404 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
406 // is the new revision correct?
407 $this->assertRevisionCompleteness( $return );
408 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
409 $this->assertRevisionRecordsEqual( $rev, $return );
411 // can we load it from the store?
412 $loaded = $store->getRevisionById( $return->getId() );
413 $this->assertRevisionCompleteness( $loaded );
414 $this->assertRevisionRecordsEqual( $return, $loaded );
416 // can we find it directly in the database?
417 $this->assertRevisionExistsInDatabase( $return );
420 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
421 $row = $this->revisionToRow( new Revision( $rev ), [] );
423 // unset nulled fields
424 unset( $row->rev_content_model
);
425 unset( $row->rev_content_format
);
428 unset( $row->rev_comment_text
);
429 unset( $row->rev_comment_data
);
430 unset( $row->rev_comment_cid
);
431 unset( $row->rev_comment_id
);
433 $store = MediaWikiServices
::getInstance()->getRevisionStore();
434 $queryInfo = $store->getQueryInfo( [ 'user' ] );
436 $row = get_object_vars( $row );
438 $queryInfo['tables'],
440 [ 'rev_id' => $rev->getId() ],
441 [ array_values( $row ) ],
448 * @param SlotRecord $a
449 * @param SlotRecord $b
451 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
452 // Assert that the same blob address has been used.
453 $this->assertSame( $a->getAddress(), $b->getAddress() );
457 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
459 public function testInsertRevisionOn_blobAddressExists() {
460 $title = $this->getTestPageTitle();
462 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
464 'comment' => $this->getRandomCommentStoreComment(),
465 'timestamp' => '20171117010101',
469 $this->overrideMwServices();
470 $store = MediaWikiServices
::getInstance()->getRevisionStore();
472 // Insert the first revision
473 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
474 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
475 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
476 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
478 // Insert a second revision inheriting the same blob address
479 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
480 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
481 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
482 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
483 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
485 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
486 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
488 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
490 // And that different revisions have been created.
491 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
493 // Make sure the slot rows reference the correct revision
494 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
495 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
498 public function provideInsertRevisionOn_failures() {
501 'comment' => $this->getRandomCommentStoreComment(),
502 'timestamp' => '20171117010101',
505 new InvalidArgumentException( 'main slot must be provided' )
507 yield
'no main slot' => [
509 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
510 'comment' => $this->getRandomCommentStoreComment(),
511 'timestamp' => '20171117010101',
514 new InvalidArgumentException( 'main slot must be provided' )
516 yield
'no timestamp' => [
518 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
519 'comment' => $this->getRandomCommentStoreComment(),
522 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
524 yield
'no comment' => [
526 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
527 'timestamp' => '20171117010101',
530 new IncompleteRevisionException( 'comment must not be NULL!' )
534 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
535 'comment' => $this->getRandomCommentStoreComment(),
536 'timestamp' => '20171117010101',
538 new IncompleteRevisionException( 'user must not be NULL!' )
543 * @dataProvider provideInsertRevisionOn_failures
544 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
546 public function testInsertRevisionOn_failures(
547 array $revDetails = [],
550 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
552 $store = MediaWikiServices
::getInstance()->getRevisionStore();
554 $this->setExpectedException(
555 get_class( $exception ),
556 $exception->getMessage(),
557 $exception->getCode()
559 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
562 public function provideNewNullRevision() {
564 Title
::newFromText( 'UTPage_notAutoCreated' ),
565 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
566 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
570 Title
::newFromText( 'UTPage_notAutoCreated' ),
571 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
572 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
578 * @dataProvider provideNewNullRevision
579 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
580 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
582 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
583 $this->overrideMwServices();
585 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
586 $page = WikiPage
::factory( $title );
588 if ( !$page->exists() ) {
589 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
592 $revDetails['page'] = $page->getId();
593 $revDetails['timestamp'] = wfTimestampNow();
594 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
595 $revDetails['user'] = $user;
597 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
598 $store = MediaWikiServices
::getInstance()->getRevisionStore();
600 $dbw = wfGetDB( DB_MASTER
);
601 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
602 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
604 $record = $store->newNullRevision(
605 wfGetDB( DB_MASTER
),
612 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
613 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
614 $this->assertEquals( $comment, $record->getComment() );
615 $this->assertEquals( $minor, $record->isMinor() );
616 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
617 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
619 $this->assertArrayEquals(
620 $baseRev->getSlotRoles(),
621 $record->getSlotRoles()
624 foreach ( $baseRev->getSlotRoles() as $role ) {
625 $parentSlot = $baseRev->getSlot( $role );
626 $slot = $record->getSlot( $role );
628 $this->assertTrue( $slot->isInherited(), 'isInherited' );
629 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
630 $this->assertSameSlotContent( $parentSlot, $slot );
635 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
637 public function testNewNullRevision_nonExistingTitle() {
638 $store = MediaWikiServices
::getInstance()->getRevisionStore();
639 $record = $store->newNullRevision(
640 wfGetDB( DB_MASTER
),
641 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
642 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
644 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
646 $this->assertNull( $record );
650 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
652 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
653 $page = $this->getTestPage();
654 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
655 /** @var Revision $rev */
656 $rev = $status->value
['revision'];
658 $store = MediaWikiServices
::getInstance()->getRevisionStore();
659 $revisionRecord = $store->getRevisionById( $rev->getId() );
660 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
662 $this->assertGreaterThan( 0, $result );
664 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
670 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
672 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
673 // This assumes that sysops are auto patrolled
674 $sysop = $this->getTestSysop()->getUser();
675 $page = $this->getTestPage();
676 $status = $page->doEditContent(
677 new WikitextContent( __METHOD__
),
683 /** @var Revision $rev */
684 $rev = $status->value
['revision'];
686 $store = MediaWikiServices
::getInstance()->getRevisionStore();
687 $revisionRecord = $store->getRevisionById( $rev->getId() );
688 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
690 $this->assertSame( 0, $result );
694 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
696 public function testGetRecentChange() {
697 $page = $this->getTestPage();
698 $content = new WikitextContent( __METHOD__
);
699 $status = $page->doEditContent( $content, __METHOD__
);
700 /** @var Revision $rev */
701 $rev = $status->value
['revision'];
703 $store = MediaWikiServices
::getInstance()->getRevisionStore();
704 $revRecord = $store->getRevisionById( $rev->getId() );
705 $recentChange = $store->getRecentChange( $revRecord );
707 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
708 $this->assertEquals( $rev->getRecentChange(), $recentChange );
712 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
714 public function testGetRevisionById() {
715 $page = $this->getTestPage();
716 $content = new WikitextContent( __METHOD__
);
717 $status = $page->doEditContent( $content, __METHOD__
);
718 /** @var Revision $rev */
719 $rev = $status->value
['revision'];
721 $store = MediaWikiServices
::getInstance()->getRevisionStore();
722 $revRecord = $store->getRevisionById( $rev->getId() );
724 $this->assertSame( $rev->getId(), $revRecord->getId() );
725 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
726 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
730 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
732 public function testGetRevisionByTitle() {
733 $page = $this->getTestPage();
734 $content = new WikitextContent( __METHOD__
);
735 $status = $page->doEditContent( $content, __METHOD__
);
736 /** @var Revision $rev */
737 $rev = $status->value
['revision'];
739 $store = MediaWikiServices
::getInstance()->getRevisionStore();
740 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
742 $this->assertSame( $rev->getId(), $revRecord->getId() );
743 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
744 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
748 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
750 public function testGetRevisionByPageId() {
751 $page = $this->getTestPage();
752 $content = new WikitextContent( __METHOD__
);
753 $status = $page->doEditContent( $content, __METHOD__
);
754 /** @var Revision $rev */
755 $rev = $status->value
['revision'];
757 $store = MediaWikiServices
::getInstance()->getRevisionStore();
758 $revRecord = $store->getRevisionByPageId( $page->getId() );
760 $this->assertSame( $rev->getId(), $revRecord->getId() );
761 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
762 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
766 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
768 public function testGetRevisionByTimestamp() {
769 // Make sure there is 1 second between the last revision and the rev we create...
770 // Otherwise we might not get the correct revision and the test may fail...
772 $page = $this->getTestPage();
774 $content = new WikitextContent( __METHOD__
);
775 $status = $page->doEditContent( $content, __METHOD__
);
776 /** @var Revision $rev */
777 $rev = $status->value
['revision'];
779 $store = MediaWikiServices
::getInstance()->getRevisionStore();
780 $revRecord = $store->getRevisionByTimestamp(
785 $this->assertSame( $rev->getId(), $revRecord->getId() );
786 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
787 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
790 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
791 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
792 $page = WikiPage
::factory( $rev->getTitle() );
795 'rev_id' => (string)$rev->getId(),
796 'rev_page' => (string)$rev->getPage(),
797 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
798 'rev_user_text' => (string)$rev->getUserText(),
799 'rev_user' => (string)$rev->getUser(),
800 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
801 'rev_deleted' => (string)$rev->getVisibility(),
802 'rev_len' => (string)$rev->getSize(),
803 'rev_parent_id' => (string)$rev->getParentId(),
804 'rev_sha1' => (string)$rev->getSha1(),
807 if ( in_array( 'page', $options ) ) {
809 'page_namespace' => (string)$page->getTitle()->getNamespace(),
810 'page_title' => $page->getTitle()->getDBkey(),
811 'page_id' => (string)$page->getId(),
812 'page_latest' => (string)$page->getLatest(),
813 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
814 'page_len' => (string)$page->getContent()->getSize(),
818 if ( in_array( 'user', $options ) ) {
820 'user_name' => (string)$rev->getUserText(),
824 if ( in_array( 'comment', $options ) ) {
826 'rev_comment_text' => $rev->getComment(),
827 'rev_comment_data' => null,
828 'rev_comment_cid' => null,
832 if ( $rev->getId() ) {
834 'rev_id' => (string)$rev->getId(),
838 return (object)$fields;
841 protected function assertRevisionRecordMatchesRevision(
843 RevisionRecord
$record
845 $this->assertSame( $rev->getId(), $record->getId() );
846 $this->assertSame( $rev->getPage(), $record->getPageId() );
847 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
848 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
849 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
850 $this->assertSame( $rev->isMinor(), $record->isMinor() );
851 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
852 $this->assertSame( $rev->getSize(), $record->getSize() );
854 * @note As of MW 1.31, the database schema allows the parent ID to be
855 * NULL to indicate that it is unknown.
857 $expectedParent = $rev->getParentId();
858 if ( $expectedParent === null ) {
861 $this->assertSame( $expectedParent, $record->getParentId() );
862 $this->assertSame( $rev->getSha1(), $record->getSha1() );
863 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
864 $this->assertSame( $rev->getContentFormat(),
865 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
866 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
867 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
869 $revRec = $rev->getRevisionRecord();
870 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
871 $recMain = $record->getSlot( SlotRecord
::MAIN
);
873 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
874 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
875 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
877 if ( $revMain->hasOrigin() ) {
878 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
881 if ( $revMain->hasAddress() ) {
882 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
885 if ( $revMain->hasContentId() ) {
886 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
891 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
892 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
894 public function testNewRevisionFromRow_getQueryInfo() {
895 $page = $this->getTestPage();
896 $text = __METHOD__
. 'a-ä';
897 /** @var Revision $rev */
898 $rev = $page->doEditContent(
899 new WikitextContent( $text ),
901 )->value
['revision'];
903 $store = MediaWikiServices
::getInstance()->getRevisionStore();
904 $info = $store->getQueryInfo();
905 $row = $this->db
->selectRow(
908 [ 'rev_id' => $rev->getId() ],
913 $record = $store->newRevisionFromRow(
918 $this->assertRevisionRecordMatchesRevision( $rev, $record );
919 $this->assertSame( $text, $rev->getContent()->serialize() );
923 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
925 public function testNewRevisionFromRow_anonEdit() {
926 $page = $this->getTestPage();
927 $text = __METHOD__
. 'a-ä';
928 /** @var Revision $rev */
929 $rev = $page->doEditContent(
930 new WikitextContent( $text ),
932 )->value
['revision'];
934 $store = MediaWikiServices
::getInstance()->getRevisionStore();
935 $record = $store->newRevisionFromRow(
936 $this->revisionToRow( $rev ),
940 $this->assertRevisionRecordMatchesRevision( $rev, $record );
941 $this->assertSame( $text, $rev->getContent()->serialize() );
945 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
947 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
948 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
949 $this->overrideMwServices();
950 $page = $this->getTestPage();
951 $text = __METHOD__
. 'a-ä';
952 /** @var Revision $rev */
953 $rev = $page->doEditContent(
954 new WikitextContent( $text ),
956 )->value
['revision'];
958 $store = MediaWikiServices
::getInstance()->getRevisionStore();
959 $record = $store->newRevisionFromRow(
960 $this->revisionToRow( $rev ),
964 $this->assertRevisionRecordMatchesRevision( $rev, $record );
965 $this->assertSame( $text, $rev->getContent()->serialize() );
969 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
971 public function testNewRevisionFromRow_userEdit() {
972 $page = $this->getTestPage();
973 $text = __METHOD__
. 'b-ä';
974 /** @var Revision $rev */
975 $rev = $page->doEditContent(
976 new WikitextContent( $text ),
980 $this->getTestUser()->getUser()
981 )->value
['revision'];
983 $store = MediaWikiServices
::getInstance()->getRevisionStore();
984 $record = $store->newRevisionFromRow(
985 $this->revisionToRow( $rev ),
989 $this->assertRevisionRecordMatchesRevision( $rev, $record );
990 $this->assertSame( $text, $rev->getContent()->serialize() );
994 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
995 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
997 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
998 $store = MediaWikiServices
::getInstance()->getRevisionStore();
999 $title = Title
::newFromText( __METHOD__
);
1000 $text = __METHOD__
. '-bä';
1001 $page = WikiPage
::factory( $title );
1002 /** @var Revision $orig */
1003 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1004 ->value
['revision'];
1005 $page->doDeleteArticle( __METHOD__
);
1007 $db = wfGetDB( DB_MASTER
);
1008 $arQuery = $store->getArchiveQueryInfo();
1010 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1011 __METHOD__
, [], $arQuery['joins']
1013 $this->assertTrue( is_object( $res ), 'query failed' );
1015 $row = $res->fetchObject();
1017 $record = $store->newRevisionFromArchiveRow( $row );
1019 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1020 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1024 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1026 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1027 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1028 $this->overrideMwServices();
1029 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1030 $title = Title
::newFromText( __METHOD__
);
1031 $text = __METHOD__
. '-bä';
1032 $page = WikiPage
::factory( $title );
1033 /** @var Revision $orig */
1034 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1035 ->value
['revision'];
1036 $page->doDeleteArticle( __METHOD__
);
1038 $db = wfGetDB( DB_MASTER
);
1039 $arQuery = $store->getArchiveQueryInfo();
1041 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1042 __METHOD__
, [], $arQuery['joins']
1044 $this->assertTrue( is_object( $res ), 'query failed' );
1046 $row = $res->fetchObject();
1048 $record = $store->newRevisionFromArchiveRow( $row );
1050 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1051 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1055 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1057 public function testNewRevisionFromArchiveRow_no_user() {
1058 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1062 'ar_page_id' => '2',
1063 'ar_namespace' => '0',
1064 'ar_title' => 'Something',
1066 'ar_text_id' => '47',
1067 'ar_timestamp' => '20180528192356',
1068 'ar_minor_edit' => '0',
1069 'ar_deleted' => '0',
1071 'ar_parent_id' => '0',
1072 'ar_sha1' => 'deadbeef',
1073 'ar_comment_text' => 'whatever',
1074 'ar_comment_data' => null,
1075 'ar_comment_cid' => null,
1077 'ar_user_text' => '', // this is the important bit
1079 'ar_content_format' => null,
1080 'ar_content_model' => null,
1083 \Wikimedia\
suppressWarnings();
1084 $record = $store->newRevisionFromArchiveRow( $row );
1085 \Wikimedia\
suppressWarnings( true );
1087 $this->assertInstanceOf( RevisionRecord
::class, $record );
1088 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1089 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1093 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1095 public function testNewRevisionFromRow_no_user() {
1096 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1097 $title = Title
::newFromText( __METHOD__
);
1102 'page_namespace' => '0',
1103 'page_title' => $title->getText(),
1104 'rev_text_id' => '47',
1105 'rev_timestamp' => '20180528192356',
1106 'rev_minor_edit' => '0',
1107 'rev_deleted' => '0',
1109 'rev_parent_id' => '0',
1110 'rev_sha1' => 'deadbeef',
1111 'rev_comment_text' => 'whatever',
1112 'rev_comment_data' => null,
1113 'rev_comment_cid' => null,
1115 'rev_user_text' => '', // this is the important bit
1116 'rev_actor' => null,
1117 'rev_content_format' => null,
1118 'rev_content_model' => null,
1121 \Wikimedia\
suppressWarnings();
1122 $record = $store->newRevisionFromRow( $row, 0, $title );
1123 \Wikimedia\
suppressWarnings( true );
1125 $this->assertNotNull( $record );
1126 $this->assertNotNull( $record->getUser() );
1127 $this->assertNotEmpty( $record->getUser()->getName() );
1131 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1133 public function testInsertRevisionOn_archive() {
1134 // This is a round trip test for deletion and undeletion of a
1135 // revision row via the archive table.
1137 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1138 $title = Title
::newFromText( __METHOD__
);
1140 $page = WikiPage
::factory( $title );
1141 /** @var Revision $origRev */
1142 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1143 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1144 ->value
['revision'];
1145 $orig = $origRev->getRevisionRecord();
1146 $page->doDeleteArticle( __METHOD__
);
1148 // re-create page, so we can later load revisions for it
1149 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1151 $db = wfGetDB( DB_MASTER
);
1152 $arQuery = $store->getArchiveQueryInfo();
1153 $row = $db->selectRow(
1154 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1155 __METHOD__
, [], $arQuery['joins']
1158 $this->assertNotFalse( $row, 'query failed' );
1160 $record = $store->newRevisionFromArchiveRow(
1164 [ 'page_id' => $title->getArticleID() ]
1167 $restored = $store->insertRevisionOn( $record, $db );
1169 // is the new revision correct?
1170 $this->assertRevisionCompleteness( $restored );
1171 $this->assertRevisionRecordsEqual( $record, $restored );
1173 // does the new revision use the original slot?
1174 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1175 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1176 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1177 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1178 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1179 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1181 // can we load it from the store?
1182 $loaded = $store->getRevisionById( $restored->getId() );
1183 $this->assertNotNull( $loaded );
1184 $this->assertRevisionCompleteness( $loaded );
1185 $this->assertRevisionRecordsEqual( $restored, $loaded );
1187 // can we find it directly in the database?
1188 $this->assertRevisionExistsInDatabase( $restored );
1192 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1194 public function testLoadRevisionFromId() {
1195 $title = Title
::newFromText( __METHOD__
);
1196 $page = WikiPage
::factory( $title );
1197 /** @var Revision $rev */
1198 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1199 ->value
['revision'];
1201 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1202 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1203 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1207 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1209 public function testLoadRevisionFromPageId() {
1210 $title = Title
::newFromText( __METHOD__
);
1211 $page = WikiPage
::factory( $title );
1212 /** @var Revision $rev */
1213 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1214 ->value
['revision'];
1216 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1217 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1218 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1222 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1224 public function testLoadRevisionFromTitle() {
1225 $title = Title
::newFromText( __METHOD__
);
1226 $page = WikiPage
::factory( $title );
1227 /** @var Revision $rev */
1228 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1229 ->value
['revision'];
1231 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1232 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1233 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1237 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1239 public function testLoadRevisionFromTimestamp() {
1240 $title = Title
::newFromText( __METHOD__
);
1241 $page = WikiPage
::factory( $title );
1242 /** @var Revision $revOne */
1243 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1244 ->value
['revision'];
1245 // Sleep to ensure different timestamps... )(evil)
1247 /** @var Revision $revTwo */
1248 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1249 ->value
['revision'];
1251 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1253 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1257 $store->loadRevisionFromTimestamp(
1258 wfGetDB( DB_MASTER
),
1260 $revOne->getTimestamp()
1265 $store->loadRevisionFromTimestamp(
1266 wfGetDB( DB_MASTER
),
1268 $revTwo->getTimestamp()
1274 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1276 public function testGetParentLengths() {
1277 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1278 /** @var Revision $revOne */
1279 $revOne = $page->doEditContent(
1280 new WikitextContent( __METHOD__
), __METHOD__
1281 )->value
['revision'];
1282 /** @var Revision $revTwo */
1283 $revTwo = $page->doEditContent(
1284 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1285 )->value
['revision'];
1287 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1290 $revOne->getId() => strlen( __METHOD__
),
1292 $store->listRevisionSizes(
1293 wfGetDB( DB_MASTER
),
1294 [ $revOne->getId() ]
1299 $revOne->getId() => strlen( __METHOD__
),
1300 $revTwo->getId() => strlen( __METHOD__
) +
1,
1302 $store->listRevisionSizes(
1303 wfGetDB( DB_MASTER
),
1304 [ $revOne->getId(), $revTwo->getId() ]
1310 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1312 public function testGetPreviousRevision() {
1313 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1314 /** @var Revision $revOne */
1315 $revOne = $page->doEditContent(
1316 new WikitextContent( __METHOD__
), __METHOD__
1317 )->value
['revision'];
1318 /** @var Revision $revTwo */
1319 $revTwo = $page->doEditContent(
1320 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1321 )->value
['revision'];
1323 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1325 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1329 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1334 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1336 public function testGetNextRevision() {
1337 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1338 /** @var Revision $revOne */
1339 $revOne = $page->doEditContent(
1340 new WikitextContent( __METHOD__
), __METHOD__
1341 )->value
['revision'];
1342 /** @var Revision $revTwo */
1343 $revTwo = $page->doEditContent(
1344 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1345 )->value
['revision'];
1347 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1350 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1353 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1358 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1360 public function testGetTimestampFromId_found() {
1361 $page = $this->getTestPage();
1362 /** @var Revision $rev */
1363 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1364 ->value
['revision'];
1366 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1367 $result = $store->getTimestampFromId(
1372 $this->assertSame( $rev->getTimestamp(), $result );
1376 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1378 public function testGetTimestampFromId_notFound() {
1379 $page = $this->getTestPage();
1380 /** @var Revision $rev */
1381 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1382 ->value
['revision'];
1384 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1385 $result = $store->getTimestampFromId(
1390 $this->assertFalse( $result );
1394 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1396 public function testCountRevisionsByPageId() {
1397 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1398 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1402 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1404 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1407 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1409 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1412 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1417 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1419 public function testCountRevisionsByTitle() {
1420 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1421 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1425 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1427 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1430 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1432 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1435 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1440 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1442 public function testUserWasLastToEdit_false() {
1443 $sysop = $this->getTestSysop()->getUser();
1444 $page = $this->getTestPage();
1445 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1447 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1448 $result = $store->userWasLastToEdit(
1449 wfGetDB( DB_MASTER
),
1454 $this->assertFalse( $result );
1458 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1460 public function testUserWasLastToEdit_true() {
1461 $startTime = wfTimestampNow();
1462 $sysop = $this->getTestSysop()->getUser();
1463 $page = $this->getTestPage();
1464 $page->doEditContent(
1465 new WikitextContent( __METHOD__
),
1472 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1473 $result = $store->userWasLastToEdit(
1474 wfGetDB( DB_MASTER
),
1479 $this->assertTrue( $result );
1483 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1485 public function testGetKnownCurrentRevision() {
1486 $page = $this->getTestPage();
1487 /** @var Revision $rev */
1488 $rev = $page->doEditContent(
1489 new WikitextContent( __METHOD__
. 'b' ),
1493 $this->getTestUser()->getUser()
1494 )->value
['revision'];
1496 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1497 $record = $store->getKnownCurrentRevision(
1502 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1505 public function provideNewMutableRevisionFromArray() {
1506 yield
'Basic array, content object' => [
1510 'timestamp' => '20171017114835',
1511 'user_text' => '111.0.1.2',
1513 'minor_edit' => false,
1517 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1518 'comment' => 'Goat Comment!',
1519 'content' => new WikitextContent( 'Some Content' ),
1522 yield
'Basic array, serialized text' => [
1526 'timestamp' => '20171017114835',
1527 'user_text' => '111.0.1.2',
1529 'minor_edit' => false,
1533 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1534 'comment' => 'Goat Comment!',
1535 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1538 yield
'Basic array, serialized text, utf-8 flags' => [
1542 'timestamp' => '20171017114835',
1543 'user_text' => '111.0.1.2',
1545 'minor_edit' => false,
1549 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1550 'comment' => 'Goat Comment!',
1551 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1555 yield
'Basic array, with title' => [
1557 'title' => Title
::newFromText( 'SomeText' ),
1558 'timestamp' => '20171017114835',
1559 'user_text' => '111.0.1.2',
1561 'minor_edit' => false,
1565 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1566 'comment' => 'Goat Comment!',
1567 'content' => new WikitextContent( 'Some Content' ),
1570 yield
'Basic array, no user field' => [
1574 'timestamp' => '20171017114835',
1575 'user_text' => '111.0.1.3',
1576 'minor_edit' => false,
1580 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1581 'comment' => 'Goat Comment!',
1582 'content' => new WikitextContent( 'Some Content' ),
1588 * @dataProvider provideNewMutableRevisionFromArray
1589 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1591 public function testNewMutableRevisionFromArray( array $array ) {
1592 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1594 // HACK: if $array['page'] is given, make sure that the page exists
1595 if ( isset( $array['page'] ) ) {
1596 $t = Title
::newFromID( $array['page'] );
1597 if ( !$t ||
!$t->exists() ) {
1598 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1599 $info = $this->insertPage( $t );
1600 $array['page'] = $info['id'];
1604 $result = $store->newMutableRevisionFromArray( $array );
1606 if ( isset( $array['id'] ) ) {
1607 $this->assertSame( $array['id'], $result->getId() );
1609 if ( isset( $array['page'] ) ) {
1610 $this->assertSame( $array['page'], $result->getPageId() );
1612 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1613 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1614 if ( isset( $array['user'] ) ) {
1615 $this->assertSame( $array['user'], $result->getUser()->getId() );
1617 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1618 $this->assertSame( $array['deleted'], $result->getVisibility() );
1619 $this->assertSame( $array['len'], $result->getSize() );
1620 $this->assertSame( $array['parent_id'], $result->getParentId() );
1621 $this->assertSame( $array['sha1'], $result->getSha1() );
1622 $this->assertSame( $array['comment'], $result->getComment()->text
);
1623 if ( isset( $array['content'] ) ) {
1624 foreach ( $array['content'] as $role => $content ) {
1626 $result->getContent( $role )->equals( $content )
1629 } elseif ( isset( $array['text'] ) ) {
1630 $this->assertSame( $array['text'],
1631 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1632 } elseif ( isset( $array['content_format'] ) ) {
1634 $array['content_format'],
1635 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1637 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1642 * @dataProvider provideNewMutableRevisionFromArray
1643 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1645 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1646 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1647 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1648 $blobStore = new SqlBlobStore( $lb, $cache );
1649 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1651 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1652 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1653 ->disableOriginalConstructor()
1655 $factory->expects( $this->any() )
1656 ->method( 'newBlobStore' )
1657 ->willReturn( $blobStore );
1658 $factory->expects( $this->any() )
1659 ->method( 'newSqlBlobStore' )
1660 ->willReturn( $blobStore );
1662 $this->setService( 'BlobStoreFactory', $factory );
1664 $this->testNewMutableRevisionFromArray( $array );