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\DatabaseSqlite
;
29 use Wikimedia\Rdbms\FakeResultWrapper
;
30 use Wikimedia\Rdbms\LoadBalancer
;
31 use Wikimedia\Rdbms\TransactionProfiler
;
37 * @group RevisionStore
39 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
44 private $testPageTitle;
54 abstract protected function getMcrMigrationStage();
59 protected function getContentHandlerUseDB() {
66 abstract protected function getMcrTablesToReset();
68 public function setUp() {
70 $this->tablesUsed
[] = 'archive';
71 $this->tablesUsed
[] = 'page';
72 $this->tablesUsed
[] = 'revision';
73 $this->tablesUsed
[] = 'comment';
75 $this->tablesUsed +
= $this->getMcrTablesToReset();
77 $this->setMwGlobals( [
78 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
79 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
80 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
81 'wgActorTableSchemaMigrationStage' => MIGRATION_OLD
,
84 $this->overrideMwServices();
87 protected function addCoreDBData() {
88 // Blank out. This would fail with a modified schema, and we don't need it.
94 protected function getTestPageTitle() {
95 if ( $this->testPageTitle
) {
96 return $this->testPageTitle
;
99 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
100 return $this->testPageTitle
;
105 protected function getTestPage() {
106 if ( $this->testPage
) {
107 return $this->testPage
;
110 $title = $this->getTestPageTitle();
111 $this->testPage
= WikiPage
::factory( $title );
113 if ( !$this->testPage
->exists() ) {
114 // Make sure we don't write to the live db.
115 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
117 $user = static::getTestSysop()->getUser();
119 $this->testPage
->doEditContent(
120 new WikitextContent( 'UTContent-' . __CLASS__
),
121 'UTPageSummary-' . __CLASS__
,
122 EDIT_NEW | EDIT_SUPPRESS_RC
,
128 return $this->testPage
;
132 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
134 private function getLoadBalancerMock( array $server ) {
135 $lb = $this->getMockBuilder( LoadBalancer
::class )
136 ->setMethods( [ 'reallyOpenConnection' ] )
137 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
140 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
141 function ( array $server, $dbNameOverride ) {
142 return $this->getDatabaseMock( $server );
150 * @return Database|PHPUnit_Framework_MockObject_MockObject
152 private function getDatabaseMock( array $params ) {
153 $db = $this->getMockBuilder( DatabaseSqlite
::class )
154 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
155 ->setConstructorArgs( [ $params ] )
158 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
159 $db->method( 'isOpen' )->willReturn( true );
164 public function provideDomainCheck() {
165 yield
[ false, 'test', '' ];
166 yield
[ 'test', 'test', '' ];
168 yield
[ false, 'test', 'foo_' ];
169 yield
[ 'test-foo_', 'test', 'foo_' ];
171 yield
[ false, 'dash-test', '' ];
172 yield
[ 'dash-test', 'dash-test', '' ];
174 yield
[ false, 'underscore_test', 'foo_' ];
175 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
179 * @dataProvider provideDomainCheck
180 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
182 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
185 'wgDBname' => $dbName,
186 'wgDBprefix' => $dbPrefix,
190 $loadBalancer = $this->getLoadBalancerMock(
193 'dbDirectory' => '*dummy*',
195 'password' => 'test',
203 'trxProfiler' => new TransactionProfiler(),
204 'connLogger' => new \Psr\Log\
NullLogger(),
205 'queryLogger' => new \Psr\Log\
NullLogger(),
206 'errorLogger' => function () {
208 'deprecationLogger' => function () {
212 'tablePrefix' => $dbPrefix,
215 $db = $loadBalancer->getConnection( DB_REPLICA
);
217 /** @var SqlBlobStore $blobStore */
218 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
219 ->disableOriginalConstructor()
222 $store = new RevisionStore(
225 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
226 MediaWikiServices
::getInstance()->getCommentStore(),
227 MediaWikiServices
::getInstance()->getContentModelStore(),
228 MediaWikiServices
::getInstance()->getSlotRoleStore(),
229 $this->getMcrMigrationStage(),
230 MediaWikiServices
::getInstance()->getActorMigration(),
234 $count = $store->countRevisionsByPageId( $db, 0 );
236 // Dummy check to make PhpUnit happy. We are really only interested in
237 // countRevisionsByPageId not failing due to the DB domain check.
238 $this->assertSame( 0, $count );
241 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
242 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
243 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
244 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
245 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
248 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
250 $r1->getPageAsLinkTarget()->getNamespace(),
251 $r2->getPageAsLinkTarget()->getNamespace()
255 $r1->getPageAsLinkTarget()->getText(),
256 $r2->getPageAsLinkTarget()->getText()
259 if ( $r1->getParentId() ) {
260 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
263 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
264 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
265 $this->assertEquals( $r1->getComment(), $r2->getComment() );
266 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
267 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
268 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
269 $this->assertEquals( $r1->getSize(), $r2->getSize() );
270 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
271 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
272 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
273 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
274 foreach ( $r1->getSlotRoles() as $role ) {
275 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
276 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
279 RevisionRecord
::DELETED_TEXT
,
280 RevisionRecord
::DELETED_COMMENT
,
281 RevisionRecord
::DELETED_USER
,
282 RevisionRecord
::DELETED_RESTRICTED
,
284 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
288 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
289 $this->assertSame( $s1->getRole(), $s2->getRole() );
290 $this->assertSame( $s1->getModel(), $s2->getModel() );
291 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
292 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
293 $this->assertSame( $s1->getSize(), $s2->getSize() );
294 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
296 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
297 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
300 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
301 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
302 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
303 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
305 foreach ( $r->getSlotRoles() as $role ) {
306 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
310 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
311 $this->assertTrue( $slot->hasAddress() );
312 $this->assertSame( $r->getId(), $slot->getRevision() );
314 $this->assertInstanceOf( Content
::class, $slot->getContent() );
318 * @param mixed[] $details
320 * @return RevisionRecord
322 private function getRevisionRecordFromDetailsArray( $details = [] ) {
323 // Convert some values that can't be provided by dataProviders
324 if ( isset( $details['user'] ) && $details['user'] === true ) {
325 $details['user'] = $this->getTestUser()->getUser();
327 if ( isset( $details['page'] ) && $details['page'] === true ) {
328 $details['page'] = $this->getTestPage()->getId();
330 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
331 $details['parent'] = $this->getTestPage()->getLatest();
334 // Create the RevisionRecord with any available data
335 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
336 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
337 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
338 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
339 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
340 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
341 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
342 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
343 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
344 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
345 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
346 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
348 if ( isset( $details['content'] ) ) {
349 foreach ( $details['content'] as $role => $content ) {
350 $rev->setContent( $role, $content );
357 public function provideInsertRevisionOn_successes() {
358 yield
'Bare minimum revision insertion' => [
360 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
362 'comment' => $this->getRandomCommentStoreComment(),
363 'timestamp' => '20171117010101',
367 yield
'Detailed revision insertion' => [
369 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
372 'comment' => $this->getRandomCommentStoreComment(),
373 'timestamp' => '20171117010101',
376 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
381 protected function getRandomCommentStoreComment() {
382 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
386 * @dataProvider provideInsertRevisionOn_successes
387 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
388 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
389 * @covers \MediaWiki\Revision\RevisionStore::insertContentRowOn
391 public function testInsertRevisionOn_successes(
392 array $revDetails = []
394 $title = $this->getTestPageTitle();
395 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
397 $this->overrideMwServices();
398 $store = MediaWikiServices
::getInstance()->getRevisionStore();
399 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
401 // is the new revision correct?
402 $this->assertRevisionCompleteness( $return );
403 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
404 $this->assertRevisionRecordsEqual( $rev, $return );
406 // can we load it from the store?
407 $loaded = $store->getRevisionById( $return->getId() );
408 $this->assertRevisionCompleteness( $loaded );
409 $this->assertRevisionRecordsEqual( $return, $loaded );
411 // can we find it directly in the database?
412 $this->assertRevisionExistsInDatabase( $return );
415 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
416 $row = $this->revisionToRow( new Revision( $rev ), [] );
418 // unset nulled fields
419 unset( $row->rev_content_model
);
420 unset( $row->rev_content_format
);
423 unset( $row->rev_comment_text
);
424 unset( $row->rev_comment_data
);
425 unset( $row->rev_comment_cid
);
426 unset( $row->rev_comment_id
);
428 $store = MediaWikiServices
::getInstance()->getRevisionStore();
429 $queryInfo = $store->getQueryInfo( [ 'user' ] );
431 $row = get_object_vars( $row );
433 $queryInfo['tables'],
435 [ 'rev_id' => $rev->getId() ],
436 [ array_values( $row ) ],
443 * @param SlotRecord $a
444 * @param SlotRecord $b
446 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
447 // Assert that the same blob address has been used.
448 $this->assertSame( $a->getAddress(), $b->getAddress() );
452 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
454 public function testInsertRevisionOn_blobAddressExists() {
455 $title = $this->getTestPageTitle();
457 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
459 'comment' => $this->getRandomCommentStoreComment(),
460 'timestamp' => '20171117010101',
464 $this->overrideMwServices();
465 $store = MediaWikiServices
::getInstance()->getRevisionStore();
467 // Insert the first revision
468 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
469 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
470 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
471 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
473 // Insert a second revision inheriting the same blob address
474 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( SlotRecord
::MAIN
) );
475 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
476 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
477 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
478 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
480 $firstMainSlot = $firstReturn->getSlot( SlotRecord
::MAIN
);
481 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::MAIN
);
483 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
485 // And that different revisions have been created.
486 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
488 // Make sure the slot rows reference the correct revision
489 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
490 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
493 public function provideInsertRevisionOn_failures() {
496 'comment' => $this->getRandomCommentStoreComment(),
497 'timestamp' => '20171117010101',
500 new InvalidArgumentException( 'main slot must be provided' )
502 yield
'no main slot' => [
504 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
505 'comment' => $this->getRandomCommentStoreComment(),
506 'timestamp' => '20171117010101',
509 new InvalidArgumentException( 'main slot must be provided' )
511 yield
'no timestamp' => [
513 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
514 'comment' => $this->getRandomCommentStoreComment(),
517 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
519 yield
'no comment' => [
521 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
522 'timestamp' => '20171117010101',
525 new IncompleteRevisionException( 'comment must not be NULL!' )
529 'slot' => SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
530 'comment' => $this->getRandomCommentStoreComment(),
531 'timestamp' => '20171117010101',
533 new IncompleteRevisionException( 'user must not be NULL!' )
538 * @dataProvider provideInsertRevisionOn_failures
539 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
541 public function testInsertRevisionOn_failures(
542 array $revDetails = [],
545 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
547 $store = MediaWikiServices
::getInstance()->getRevisionStore();
549 $this->setExpectedException(
550 get_class( $exception ),
551 $exception->getMessage(),
552 $exception->getCode()
554 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
557 public function provideNewNullRevision() {
559 Title
::newFromText( 'UTPage_notAutoCreated' ),
560 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
561 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
565 Title
::newFromText( 'UTPage_notAutoCreated' ),
566 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
567 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
573 * @dataProvider provideNewNullRevision
574 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
575 * @covers \MediaWiki\Revision\RevisionStore::findSlotContentId
577 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
578 $this->overrideMwServices();
580 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
581 $page = WikiPage
::factory( $title );
583 if ( !$page->exists() ) {
584 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
587 $revDetails['page'] = $page->getId();
588 $revDetails['timestamp'] = wfTimestampNow();
589 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
590 $revDetails['user'] = $user;
592 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
593 $store = MediaWikiServices
::getInstance()->getRevisionStore();
595 $dbw = wfGetDB( DB_MASTER
);
596 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
597 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
599 $record = $store->newNullRevision(
600 wfGetDB( DB_MASTER
),
607 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
608 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
609 $this->assertEquals( $comment, $record->getComment() );
610 $this->assertEquals( $minor, $record->isMinor() );
611 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
612 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
614 $this->assertArrayEquals(
615 $baseRev->getSlotRoles(),
616 $record->getSlotRoles()
619 foreach ( $baseRev->getSlotRoles() as $role ) {
620 $parentSlot = $baseRev->getSlot( $role );
621 $slot = $record->getSlot( $role );
623 $this->assertTrue( $slot->isInherited(), 'isInherited' );
624 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
625 $this->assertSameSlotContent( $parentSlot, $slot );
630 * @covers \MediaWiki\Revision\RevisionStore::newNullRevision
632 public function testNewNullRevision_nonExistingTitle() {
633 $store = MediaWikiServices
::getInstance()->getRevisionStore();
634 $record = $store->newNullRevision(
635 wfGetDB( DB_MASTER
),
636 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
637 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
639 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
641 $this->assertNull( $record );
645 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
647 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
648 $page = $this->getTestPage();
649 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
650 /** @var Revision $rev */
651 $rev = $status->value
['revision'];
653 $store = MediaWikiServices
::getInstance()->getRevisionStore();
654 $revisionRecord = $store->getRevisionById( $rev->getId() );
655 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
657 $this->assertGreaterThan( 0, $result );
659 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
665 * @covers \MediaWiki\Revision\RevisionStore::getRcIdIfUnpatrolled
667 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
668 // This assumes that sysops are auto patrolled
669 $sysop = $this->getTestSysop()->getUser();
670 $page = $this->getTestPage();
671 $status = $page->doEditContent(
672 new WikitextContent( __METHOD__
),
678 /** @var Revision $rev */
679 $rev = $status->value
['revision'];
681 $store = MediaWikiServices
::getInstance()->getRevisionStore();
682 $revisionRecord = $store->getRevisionById( $rev->getId() );
683 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
685 $this->assertSame( 0, $result );
689 * @covers \MediaWiki\Revision\RevisionStore::getRecentChange
691 public function testGetRecentChange() {
692 $page = $this->getTestPage();
693 $content = new WikitextContent( __METHOD__
);
694 $status = $page->doEditContent( $content, __METHOD__
);
695 /** @var Revision $rev */
696 $rev = $status->value
['revision'];
698 $store = MediaWikiServices
::getInstance()->getRevisionStore();
699 $revRecord = $store->getRevisionById( $rev->getId() );
700 $recentChange = $store->getRecentChange( $revRecord );
702 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
703 $this->assertEquals( $rev->getRecentChange(), $recentChange );
707 * @covers \MediaWiki\Revision\RevisionStore::getRevisionById
709 public function testGetRevisionById() {
710 $page = $this->getTestPage();
711 $content = new WikitextContent( __METHOD__
);
712 $status = $page->doEditContent( $content, __METHOD__
);
713 /** @var Revision $rev */
714 $rev = $status->value
['revision'];
716 $store = MediaWikiServices
::getInstance()->getRevisionStore();
717 $revRecord = $store->getRevisionById( $rev->getId() );
719 $this->assertSame( $rev->getId(), $revRecord->getId() );
720 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
721 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
725 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTitle
727 public function testGetRevisionByTitle() {
728 $page = $this->getTestPage();
729 $content = new WikitextContent( __METHOD__
);
730 $status = $page->doEditContent( $content, __METHOD__
);
731 /** @var Revision $rev */
732 $rev = $status->value
['revision'];
734 $store = MediaWikiServices
::getInstance()->getRevisionStore();
735 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
737 $this->assertSame( $rev->getId(), $revRecord->getId() );
738 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
739 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
743 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByPageId
745 public function testGetRevisionByPageId() {
746 $page = $this->getTestPage();
747 $content = new WikitextContent( __METHOD__
);
748 $status = $page->doEditContent( $content, __METHOD__
);
749 /** @var Revision $rev */
750 $rev = $status->value
['revision'];
752 $store = MediaWikiServices
::getInstance()->getRevisionStore();
753 $revRecord = $store->getRevisionByPageId( $page->getId() );
755 $this->assertSame( $rev->getId(), $revRecord->getId() );
756 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
757 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
761 * @covers \MediaWiki\Revision\RevisionStore::getRevisionByTimestamp
763 public function testGetRevisionByTimestamp() {
764 // Make sure there is 1 second between the last revision and the rev we create...
765 // Otherwise we might not get the correct revision and the test may fail...
767 $page = $this->getTestPage();
769 $content = new WikitextContent( __METHOD__
);
770 $status = $page->doEditContent( $content, __METHOD__
);
771 /** @var Revision $rev */
772 $rev = $status->value
['revision'];
774 $store = MediaWikiServices
::getInstance()->getRevisionStore();
775 $revRecord = $store->getRevisionByTimestamp(
780 $this->assertSame( $rev->getId(), $revRecord->getId() );
781 $this->assertTrue( $revRecord->getSlot( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
782 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
785 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
786 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
787 $page = WikiPage
::factory( $rev->getTitle() );
790 'rev_id' => (string)$rev->getId(),
791 'rev_page' => (string)$rev->getPage(),
792 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
793 'rev_user_text' => (string)$rev->getUserText(),
794 'rev_user' => (string)$rev->getUser(),
795 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
796 'rev_deleted' => (string)$rev->getVisibility(),
797 'rev_len' => (string)$rev->getSize(),
798 'rev_parent_id' => (string)$rev->getParentId(),
799 'rev_sha1' => (string)$rev->getSha1(),
802 if ( in_array( 'page', $options ) ) {
804 'page_namespace' => (string)$page->getTitle()->getNamespace(),
805 'page_title' => $page->getTitle()->getDBkey(),
806 'page_id' => (string)$page->getId(),
807 'page_latest' => (string)$page->getLatest(),
808 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
809 'page_len' => (string)$page->getContent()->getSize(),
813 if ( in_array( 'user', $options ) ) {
815 'user_name' => (string)$rev->getUserText(),
819 if ( in_array( 'comment', $options ) ) {
821 'rev_comment_text' => $rev->getComment(),
822 'rev_comment_data' => null,
823 'rev_comment_cid' => null,
827 if ( $rev->getId() ) {
829 'rev_id' => (string)$rev->getId(),
833 return (object)$fields;
836 protected function assertRevisionRecordMatchesRevision(
838 RevisionRecord
$record
840 $this->assertSame( $rev->getId(), $record->getId() );
841 $this->assertSame( $rev->getPage(), $record->getPageId() );
842 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
843 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
844 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
845 $this->assertSame( $rev->isMinor(), $record->isMinor() );
846 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
847 $this->assertSame( $rev->getSize(), $record->getSize() );
849 * @note As of MW 1.31, the database schema allows the parent ID to be
850 * NULL to indicate that it is unknown.
852 $expectedParent = $rev->getParentId();
853 if ( $expectedParent === null ) {
856 $this->assertSame( $expectedParent, $record->getParentId() );
857 $this->assertSame( $rev->getSha1(), $record->getSha1() );
858 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
859 $this->assertSame( $rev->getContentFormat(),
860 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
861 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
862 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
864 $revRec = $rev->getRevisionRecord();
865 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
866 $recMain = $record->getSlot( SlotRecord
::MAIN
);
868 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
869 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
870 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
872 if ( $revMain->hasOrigin() ) {
873 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
876 if ( $revMain->hasAddress() ) {
877 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
880 if ( $revMain->hasContentId() ) {
881 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
886 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
887 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
889 public function testNewRevisionFromRow_getQueryInfo() {
890 $page = $this->getTestPage();
891 $text = __METHOD__
. 'a-ä';
892 /** @var Revision $rev */
893 $rev = $page->doEditContent(
894 new WikitextContent( $text ),
896 )->value
['revision'];
898 $store = MediaWikiServices
::getInstance()->getRevisionStore();
899 $info = $store->getQueryInfo();
900 $row = $this->db
->selectRow(
903 [ 'rev_id' => $rev->getId() ],
908 $record = $store->newRevisionFromRow(
913 $this->assertRevisionRecordMatchesRevision( $rev, $record );
914 $this->assertSame( $text, $rev->getContent()->serialize() );
918 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
920 public function testNewRevisionFromRow_anonEdit() {
921 $page = $this->getTestPage();
922 $text = __METHOD__
. 'a-ä';
923 /** @var Revision $rev */
924 $rev = $page->doEditContent(
925 new WikitextContent( $text ),
927 )->value
['revision'];
929 $store = MediaWikiServices
::getInstance()->getRevisionStore();
930 $record = $store->newRevisionFromRow(
931 $this->revisionToRow( $rev ),
935 $this->assertRevisionRecordMatchesRevision( $rev, $record );
936 $this->assertSame( $text, $rev->getContent()->serialize() );
940 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
942 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
943 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
944 $this->overrideMwServices();
945 $page = $this->getTestPage();
946 $text = __METHOD__
. 'a-ä';
947 /** @var Revision $rev */
948 $rev = $page->doEditContent(
949 new WikitextContent( $text ),
951 )->value
['revision'];
953 $store = MediaWikiServices
::getInstance()->getRevisionStore();
954 $record = $store->newRevisionFromRow(
955 $this->revisionToRow( $rev ),
959 $this->assertRevisionRecordMatchesRevision( $rev, $record );
960 $this->assertSame( $text, $rev->getContent()->serialize() );
964 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
966 public function testNewRevisionFromRow_userEdit() {
967 $page = $this->getTestPage();
968 $text = __METHOD__
. 'b-ä';
969 /** @var Revision $rev */
970 $rev = $page->doEditContent(
971 new WikitextContent( $text ),
975 $this->getTestUser()->getUser()
976 )->value
['revision'];
978 $store = MediaWikiServices
::getInstance()->getRevisionStore();
979 $record = $store->newRevisionFromRow(
980 $this->revisionToRow( $rev ),
984 $this->assertRevisionRecordMatchesRevision( $rev, $record );
985 $this->assertSame( $text, $rev->getContent()->serialize() );
989 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
990 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
992 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
993 $store = MediaWikiServices
::getInstance()->getRevisionStore();
994 $title = Title
::newFromText( __METHOD__
);
995 $text = __METHOD__
. '-bä';
996 $page = WikiPage
::factory( $title );
997 /** @var Revision $orig */
998 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1000 $page->doDeleteArticle( __METHOD__
);
1002 $db = wfGetDB( DB_MASTER
);
1003 $arQuery = $store->getArchiveQueryInfo();
1005 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1006 __METHOD__
, [], $arQuery['joins']
1008 $this->assertTrue( is_object( $res ), 'query failed' );
1010 $row = $res->fetchObject();
1012 $record = $store->newRevisionFromArchiveRow( $row );
1014 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1015 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1019 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1021 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1022 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1023 $this->overrideMwServices();
1024 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1025 $title = Title
::newFromText( __METHOD__
);
1026 $text = __METHOD__
. '-bä';
1027 $page = WikiPage
::factory( $title );
1028 /** @var Revision $orig */
1029 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1030 ->value
['revision'];
1031 $page->doDeleteArticle( __METHOD__
);
1033 $db = wfGetDB( DB_MASTER
);
1034 $arQuery = $store->getArchiveQueryInfo();
1036 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1037 __METHOD__
, [], $arQuery['joins']
1039 $this->assertTrue( is_object( $res ), 'query failed' );
1041 $row = $res->fetchObject();
1043 $record = $store->newRevisionFromArchiveRow( $row );
1045 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1046 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1050 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1052 public function testNewRevisionFromArchiveRow_no_user() {
1053 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1057 'ar_page_id' => '2',
1058 'ar_namespace' => '0',
1059 'ar_title' => 'Something',
1061 'ar_text_id' => '47',
1062 'ar_timestamp' => '20180528192356',
1063 'ar_minor_edit' => '0',
1064 'ar_deleted' => '0',
1066 'ar_parent_id' => '0',
1067 'ar_sha1' => 'deadbeef',
1068 'ar_comment_text' => 'whatever',
1069 'ar_comment_data' => null,
1070 'ar_comment_cid' => null,
1072 'ar_user_text' => '', // this is the important bit
1074 'ar_content_format' => null,
1075 'ar_content_model' => null,
1078 \Wikimedia\
suppressWarnings();
1079 $record = $store->newRevisionFromArchiveRow( $row );
1080 \Wikimedia\
suppressWarnings( true );
1082 $this->assertInstanceOf( RevisionRecord
::class, $record );
1083 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1084 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1088 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1090 public function testNewRevisionFromRow_no_user() {
1091 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1092 $title = Title
::newFromText( __METHOD__
);
1097 'page_namespace' => '0',
1098 'page_title' => $title->getText(),
1099 'rev_text_id' => '47',
1100 'rev_timestamp' => '20180528192356',
1101 'rev_minor_edit' => '0',
1102 'rev_deleted' => '0',
1104 'rev_parent_id' => '0',
1105 'rev_sha1' => 'deadbeef',
1106 'rev_comment_text' => 'whatever',
1107 'rev_comment_data' => null,
1108 'rev_comment_cid' => null,
1110 'rev_user_text' => '', // this is the important bit
1111 'rev_actor' => null,
1112 'rev_content_format' => null,
1113 'rev_content_model' => null,
1116 \Wikimedia\
suppressWarnings();
1117 $record = $store->newRevisionFromRow( $row, 0, $title );
1118 \Wikimedia\
suppressWarnings( true );
1120 $this->assertNotNull( $record );
1121 $this->assertNotNull( $record->getUser() );
1122 $this->assertNotEmpty( $record->getUser()->getName() );
1126 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1128 public function testInsertRevisionOn_archive() {
1129 // This is a round trip test for deletion and undeletion of a
1130 // revision row via the archive table.
1132 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1133 $title = Title
::newFromText( __METHOD__
);
1135 $page = WikiPage
::factory( $title );
1136 /** @var Revision $origRev */
1137 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1138 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1139 ->value
['revision'];
1140 $orig = $origRev->getRevisionRecord();
1141 $page->doDeleteArticle( __METHOD__
);
1143 // re-create page, so we can later load revisions for it
1144 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1146 $db = wfGetDB( DB_MASTER
);
1147 $arQuery = $store->getArchiveQueryInfo();
1148 $row = $db->selectRow(
1149 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1150 __METHOD__
, [], $arQuery['joins']
1153 $this->assertNotFalse( $row, 'query failed' );
1155 $record = $store->newRevisionFromArchiveRow(
1159 [ 'page_id' => $title->getArticleID() ]
1162 $restored = $store->insertRevisionOn( $record, $db );
1164 // is the new revision correct?
1165 $this->assertRevisionCompleteness( $restored );
1166 $this->assertRevisionRecordsEqual( $record, $restored );
1168 // does the new revision use the original slot?
1169 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1170 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1171 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1172 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1173 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1174 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1176 // can we load it from the store?
1177 $loaded = $store->getRevisionById( $restored->getId() );
1178 $this->assertNotNull( $loaded );
1179 $this->assertRevisionCompleteness( $loaded );
1180 $this->assertRevisionRecordsEqual( $restored, $loaded );
1182 // can we find it directly in the database?
1183 $this->assertRevisionExistsInDatabase( $restored );
1187 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1189 public function testLoadRevisionFromId() {
1190 $title = Title
::newFromText( __METHOD__
);
1191 $page = WikiPage
::factory( $title );
1192 /** @var Revision $rev */
1193 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1194 ->value
['revision'];
1196 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1197 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1198 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1202 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1204 public function testLoadRevisionFromPageId() {
1205 $title = Title
::newFromText( __METHOD__
);
1206 $page = WikiPage
::factory( $title );
1207 /** @var Revision $rev */
1208 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1209 ->value
['revision'];
1211 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1212 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1213 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1217 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1219 public function testLoadRevisionFromTitle() {
1220 $title = Title
::newFromText( __METHOD__
);
1221 $page = WikiPage
::factory( $title );
1222 /** @var Revision $rev */
1223 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1224 ->value
['revision'];
1226 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1227 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1228 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1232 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1234 public function testLoadRevisionFromTimestamp() {
1235 $title = Title
::newFromText( __METHOD__
);
1236 $page = WikiPage
::factory( $title );
1237 /** @var Revision $revOne */
1238 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1239 ->value
['revision'];
1240 // Sleep to ensure different timestamps... )(evil)
1242 /** @var Revision $revTwo */
1243 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1244 ->value
['revision'];
1246 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1248 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1252 $store->loadRevisionFromTimestamp(
1253 wfGetDB( DB_MASTER
),
1255 $revOne->getTimestamp()
1260 $store->loadRevisionFromTimestamp(
1261 wfGetDB( DB_MASTER
),
1263 $revTwo->getTimestamp()
1269 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1271 public function testGetParentLengths() {
1272 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1273 /** @var Revision $revOne */
1274 $revOne = $page->doEditContent(
1275 new WikitextContent( __METHOD__
), __METHOD__
1276 )->value
['revision'];
1277 /** @var Revision $revTwo */
1278 $revTwo = $page->doEditContent(
1279 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1280 )->value
['revision'];
1282 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1285 $revOne->getId() => strlen( __METHOD__
),
1287 $store->listRevisionSizes(
1288 wfGetDB( DB_MASTER
),
1289 [ $revOne->getId() ]
1294 $revOne->getId() => strlen( __METHOD__
),
1295 $revTwo->getId() => strlen( __METHOD__
) +
1,
1297 $store->listRevisionSizes(
1298 wfGetDB( DB_MASTER
),
1299 [ $revOne->getId(), $revTwo->getId() ]
1305 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1307 public function testGetPreviousRevision() {
1308 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1309 /** @var Revision $revOne */
1310 $revOne = $page->doEditContent(
1311 new WikitextContent( __METHOD__
), __METHOD__
1312 )->value
['revision'];
1313 /** @var Revision $revTwo */
1314 $revTwo = $page->doEditContent(
1315 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1316 )->value
['revision'];
1318 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1320 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1324 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1329 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1331 public function testGetNextRevision() {
1332 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1333 /** @var Revision $revOne */
1334 $revOne = $page->doEditContent(
1335 new WikitextContent( __METHOD__
), __METHOD__
1336 )->value
['revision'];
1337 /** @var Revision $revTwo */
1338 $revTwo = $page->doEditContent(
1339 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1340 )->value
['revision'];
1342 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1345 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1348 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1353 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1355 public function testGetTimestampFromId_found() {
1356 $page = $this->getTestPage();
1357 /** @var Revision $rev */
1358 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1359 ->value
['revision'];
1361 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1362 $result = $store->getTimestampFromId(
1367 $this->assertSame( $rev->getTimestamp(), $result );
1371 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1373 public function testGetTimestampFromId_notFound() {
1374 $page = $this->getTestPage();
1375 /** @var Revision $rev */
1376 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1377 ->value
['revision'];
1379 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1380 $result = $store->getTimestampFromId(
1385 $this->assertFalse( $result );
1389 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1391 public function testCountRevisionsByPageId() {
1392 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1393 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1397 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1399 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1402 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1404 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1407 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1412 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1414 public function testCountRevisionsByTitle() {
1415 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1416 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1420 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1422 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1425 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1427 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1430 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1435 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1437 public function testUserWasLastToEdit_false() {
1438 $sysop = $this->getTestSysop()->getUser();
1439 $page = $this->getTestPage();
1440 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1442 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1443 $result = $store->userWasLastToEdit(
1444 wfGetDB( DB_MASTER
),
1449 $this->assertFalse( $result );
1453 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1455 public function testUserWasLastToEdit_true() {
1456 $startTime = wfTimestampNow();
1457 $sysop = $this->getTestSysop()->getUser();
1458 $page = $this->getTestPage();
1459 $page->doEditContent(
1460 new WikitextContent( __METHOD__
),
1467 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1468 $result = $store->userWasLastToEdit(
1469 wfGetDB( DB_MASTER
),
1474 $this->assertTrue( $result );
1478 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1480 public function testGetKnownCurrentRevision() {
1481 $page = $this->getTestPage();
1482 /** @var Revision $rev */
1483 $rev = $page->doEditContent(
1484 new WikitextContent( __METHOD__
. 'b' ),
1488 $this->getTestUser()->getUser()
1489 )->value
['revision'];
1491 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1492 $record = $store->getKnownCurrentRevision(
1497 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1500 public function provideNewMutableRevisionFromArray() {
1501 yield
'Basic array, content object' => [
1505 'timestamp' => '20171017114835',
1506 'user_text' => '111.0.1.2',
1508 'minor_edit' => false,
1512 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1513 'comment' => 'Goat Comment!',
1514 'content' => new WikitextContent( 'Some Content' ),
1517 yield
'Basic array, serialized text' => [
1521 'timestamp' => '20171017114835',
1522 'user_text' => '111.0.1.2',
1524 'minor_edit' => false,
1528 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1529 'comment' => 'Goat Comment!',
1530 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1533 yield
'Basic array, serialized text, utf-8 flags' => [
1537 'timestamp' => '20171017114835',
1538 'user_text' => '111.0.1.2',
1540 'minor_edit' => false,
1544 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1545 'comment' => 'Goat Comment!',
1546 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1550 yield
'Basic array, with title' => [
1552 'title' => Title
::newFromText( 'SomeText' ),
1553 'timestamp' => '20171017114835',
1554 'user_text' => '111.0.1.2',
1556 'minor_edit' => false,
1560 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1561 'comment' => 'Goat Comment!',
1562 'content' => new WikitextContent( 'Some Content' ),
1565 yield
'Basic array, no user field' => [
1569 'timestamp' => '20171017114835',
1570 'user_text' => '111.0.1.3',
1571 'minor_edit' => false,
1575 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1576 'comment' => 'Goat Comment!',
1577 'content' => new WikitextContent( 'Some Content' ),
1583 * @dataProvider provideNewMutableRevisionFromArray
1584 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1586 public function testNewMutableRevisionFromArray( array $array ) {
1587 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1589 // HACK: if $array['page'] is given, make sure that the page exists
1590 if ( isset( $array['page'] ) ) {
1591 $t = Title
::newFromID( $array['page'] );
1592 if ( !$t ||
!$t->exists() ) {
1593 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1594 $info = $this->insertPage( $t );
1595 $array['page'] = $info['id'];
1599 $result = $store->newMutableRevisionFromArray( $array );
1601 if ( isset( $array['id'] ) ) {
1602 $this->assertSame( $array['id'], $result->getId() );
1604 if ( isset( $array['page'] ) ) {
1605 $this->assertSame( $array['page'], $result->getPageId() );
1607 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1608 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1609 if ( isset( $array['user'] ) ) {
1610 $this->assertSame( $array['user'], $result->getUser()->getId() );
1612 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1613 $this->assertSame( $array['deleted'], $result->getVisibility() );
1614 $this->assertSame( $array['len'], $result->getSize() );
1615 $this->assertSame( $array['parent_id'], $result->getParentId() );
1616 $this->assertSame( $array['sha1'], $result->getSha1() );
1617 $this->assertSame( $array['comment'], $result->getComment()->text
);
1618 if ( isset( $array['content'] ) ) {
1619 foreach ( $array['content'] as $role => $content ) {
1621 $result->getContent( $role )->equals( $content )
1624 } elseif ( isset( $array['text'] ) ) {
1625 $this->assertSame( $array['text'],
1626 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1627 } elseif ( isset( $array['content_format'] ) ) {
1629 $array['content_format'],
1630 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1632 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1637 * @dataProvider provideNewMutableRevisionFromArray
1638 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1640 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1641 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1642 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1643 $blobStore = new SqlBlobStore( $lb, $cache );
1644 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1646 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1647 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1648 ->disableOriginalConstructor()
1650 $factory->expects( $this->any() )
1651 ->method( 'newBlobStore' )
1652 ->willReturn( $blobStore );
1653 $factory->expects( $this->any() )
1654 ->method( 'newSqlBlobStore' )
1655 ->willReturn( $blobStore );
1657 $this->setService( 'BlobStoreFactory', $factory );
1659 $this->testNewMutableRevisionFromArray( $array );