3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
9 use InvalidArgumentException
;
11 use MediaWiki\Linker\LinkTarget
;
12 use MediaWiki\MediaWikiServices
;
13 use MediaWiki\Storage\BlobStoreFactory
;
14 use MediaWiki\Storage\IncompleteRevisionException
;
15 use MediaWiki\Storage\MutableRevisionRecord
;
16 use MediaWiki\Storage\RevisionRecord
;
17 use MediaWiki\Storage\RevisionStore
;
18 use MediaWiki\Storage\SlotRecord
;
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\Storage\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( 'main' ) );
302 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( 'main' ) );
303 $this->assertInstanceOf( Content
::class, $r->getContent( '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( 'main', new WikitextContent( 'Chicken' ) ),
362 'comment' => $this->getRandomCommentStoreComment(),
363 'timestamp' => '20171117010101',
367 yield
'Detailed revision insertion' => [
369 'slot' => SlotRecord
::newUnsaved( '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\Storage\RevisionStore::insertRevisionOn
388 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
389 * @covers \MediaWiki\Storage\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\Storage\RevisionStore::insertRevisionOn
454 public function testInsertRevisionOn_blobAddressExists() {
455 $title = $this->getTestPageTitle();
457 'slot' => SlotRecord
::newUnsaved( '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( '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( 'main' );
481 $secondMainSlot = $secondReturn->getSlot( '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( '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( 'main', new WikitextContent( 'Chicken' ) ),
522 'timestamp' => '20171117010101',
525 new IncompleteRevisionException( 'comment must not be NULL!' )
529 'slot' => SlotRecord
::newUnsaved( '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\Storage\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\Storage\RevisionStore::newNullRevision
575 * @covers \MediaWiki\Storage\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\Storage\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\Storage\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\Storage\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\Storage\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\Storage\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( 'main' )->getContent()->equals( $content ) );
721 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
725 * @covers \MediaWiki\Storage\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( 'main' )->getContent()->equals( $content ) );
739 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
743 * @covers \MediaWiki\Storage\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( 'main' )->getContent()->equals( $content ) );
757 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
761 * @covers \MediaWiki\Storage\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( '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(), $record->getContent( 'main' )->getDefaultFormat() );
860 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
861 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
863 $revRec = $rev->getRevisionRecord();
864 $revMain = $revRec->getSlot( 'main' );
865 $recMain = $record->getSlot( 'main' );
867 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
868 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
869 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
871 if ( $revMain->hasOrigin() ) {
872 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
875 if ( $revMain->hasAddress() ) {
876 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
879 if ( $revMain->hasContentId() ) {
880 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
885 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
886 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
888 public function testNewRevisionFromRow_getQueryInfo() {
889 $page = $this->getTestPage();
890 $text = __METHOD__
. 'a-ä';
891 /** @var Revision $rev */
892 $rev = $page->doEditContent(
893 new WikitextContent( $text ),
895 )->value
['revision'];
897 $store = MediaWikiServices
::getInstance()->getRevisionStore();
898 $info = $store->getQueryInfo();
899 $row = $this->db
->selectRow(
902 [ 'rev_id' => $rev->getId() ],
907 $record = $store->newRevisionFromRow(
912 $this->assertRevisionRecordMatchesRevision( $rev, $record );
913 $this->assertSame( $text, $rev->getContent()->serialize() );
917 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
919 public function testNewRevisionFromRow_anonEdit() {
920 $page = $this->getTestPage();
921 $text = __METHOD__
. 'a-ä';
922 /** @var Revision $rev */
923 $rev = $page->doEditContent(
924 new WikitextContent( $text ),
926 )->value
['revision'];
928 $store = MediaWikiServices
::getInstance()->getRevisionStore();
929 $record = $store->newRevisionFromRow(
930 $this->revisionToRow( $rev ),
934 $this->assertRevisionRecordMatchesRevision( $rev, $record );
935 $this->assertSame( $text, $rev->getContent()->serialize() );
939 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
941 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
942 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
943 $this->overrideMwServices();
944 $page = $this->getTestPage();
945 $text = __METHOD__
. 'a-ä';
946 /** @var Revision $rev */
947 $rev = $page->doEditContent(
948 new WikitextContent( $text ),
950 )->value
['revision'];
952 $store = MediaWikiServices
::getInstance()->getRevisionStore();
953 $record = $store->newRevisionFromRow(
954 $this->revisionToRow( $rev ),
958 $this->assertRevisionRecordMatchesRevision( $rev, $record );
959 $this->assertSame( $text, $rev->getContent()->serialize() );
963 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
965 public function testNewRevisionFromRow_userEdit() {
966 $page = $this->getTestPage();
967 $text = __METHOD__
. 'b-ä';
968 /** @var Revision $rev */
969 $rev = $page->doEditContent(
970 new WikitextContent( $text ),
974 $this->getTestUser()->getUser()
975 )->value
['revision'];
977 $store = MediaWikiServices
::getInstance()->getRevisionStore();
978 $record = $store->newRevisionFromRow(
979 $this->revisionToRow( $rev ),
983 $this->assertRevisionRecordMatchesRevision( $rev, $record );
984 $this->assertSame( $text, $rev->getContent()->serialize() );
988 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
989 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
991 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
992 $store = MediaWikiServices
::getInstance()->getRevisionStore();
993 $title = Title
::newFromText( __METHOD__
);
994 $text = __METHOD__
. '-bä';
995 $page = WikiPage
::factory( $title );
996 /** @var Revision $orig */
997 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
999 $page->doDeleteArticle( __METHOD__
);
1001 $db = wfGetDB( DB_MASTER
);
1002 $arQuery = $store->getArchiveQueryInfo();
1004 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1005 __METHOD__
, [], $arQuery['joins']
1007 $this->assertTrue( is_object( $res ), 'query failed' );
1009 $row = $res->fetchObject();
1011 $record = $store->newRevisionFromArchiveRow( $row );
1013 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1014 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1018 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1020 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1021 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1022 $this->overrideMwServices();
1023 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1024 $title = Title
::newFromText( __METHOD__
);
1025 $text = __METHOD__
. '-bä';
1026 $page = WikiPage
::factory( $title );
1027 /** @var Revision $orig */
1028 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1029 ->value
['revision'];
1030 $page->doDeleteArticle( __METHOD__
);
1032 $db = wfGetDB( DB_MASTER
);
1033 $arQuery = $store->getArchiveQueryInfo();
1035 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1036 __METHOD__
, [], $arQuery['joins']
1038 $this->assertTrue( is_object( $res ), 'query failed' );
1040 $row = $res->fetchObject();
1042 $record = $store->newRevisionFromArchiveRow( $row );
1044 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1045 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1049 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1051 public function testNewRevisionFromArchiveRow_no_user() {
1052 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1056 'ar_page_id' => '2',
1057 'ar_namespace' => '0',
1058 'ar_title' => 'Something',
1060 'ar_text_id' => '47',
1061 'ar_timestamp' => '20180528192356',
1062 'ar_minor_edit' => '0',
1063 'ar_deleted' => '0',
1065 'ar_parent_id' => '0',
1066 'ar_sha1' => 'deadbeef',
1067 'ar_comment_text' => 'whatever',
1068 'ar_comment_data' => null,
1069 'ar_comment_cid' => null,
1071 'ar_user_text' => '', // this is the important bit
1073 'ar_content_format' => null,
1074 'ar_content_model' => null,
1077 \Wikimedia\
suppressWarnings();
1078 $record = $store->newRevisionFromArchiveRow( $row );
1079 \Wikimedia\
suppressWarnings( true );
1081 $this->assertInstanceOf( RevisionRecord
::class, $record );
1082 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1083 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1087 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
1089 public function testNewRevisionFromRow_no_user() {
1090 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1091 $title = Title
::newFromText( __METHOD__
);
1096 'page_namespace' => '0',
1097 'page_title' => $title->getText(),
1098 'rev_text_id' => '47',
1099 'rev_timestamp' => '20180528192356',
1100 'rev_minor_edit' => '0',
1101 'rev_deleted' => '0',
1103 'rev_parent_id' => '0',
1104 'rev_sha1' => 'deadbeef',
1105 'rev_comment_text' => 'whatever',
1106 'rev_comment_data' => null,
1107 'rev_comment_cid' => null,
1109 'rev_user_text' => '', // this is the important bit
1110 'rev_actor' => null,
1111 'rev_content_format' => null,
1112 'rev_content_model' => null,
1115 \Wikimedia\
suppressWarnings();
1116 $record = $store->newRevisionFromRow( $row, 0, $title );
1117 \Wikimedia\
suppressWarnings( true );
1119 $this->assertNotNull( $record );
1120 $this->assertNotNull( $record->getUser() );
1121 $this->assertNotEmpty( $record->getUser()->getName() );
1125 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1127 public function testInsertRevisionOn_archive() {
1128 // This is a round trip test for deletion and undeletion of a
1129 // revision row via the archive table.
1131 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1132 $title = Title
::newFromText( __METHOD__
);
1134 $page = WikiPage
::factory( $title );
1135 /** @var Revision $origRev */
1136 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1137 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1138 ->value
['revision'];
1139 $orig = $origRev->getRevisionRecord();
1140 $page->doDeleteArticle( __METHOD__
);
1142 // re-create page, so we can later load revisions for it
1143 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1145 $db = wfGetDB( DB_MASTER
);
1146 $arQuery = $store->getArchiveQueryInfo();
1147 $row = $db->selectRow(
1148 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1149 __METHOD__
, [], $arQuery['joins']
1152 $this->assertNotFalse( $row, 'query failed' );
1154 $record = $store->newRevisionFromArchiveRow(
1158 [ 'page_id' => $title->getArticleID() ]
1161 $restored = $store->insertRevisionOn( $record, $db );
1163 // is the new revision correct?
1164 $this->assertRevisionCompleteness( $restored );
1165 $this->assertRevisionRecordsEqual( $record, $restored );
1167 // does the new revision use the original slot?
1168 $recMain = $record->getSlot( 'main' );
1169 $restMain = $restored->getSlot( 'main' );
1170 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1171 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1172 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1173 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1175 // can we load it from the store?
1176 $loaded = $store->getRevisionById( $restored->getId() );
1177 $this->assertNotNull( $loaded );
1178 $this->assertRevisionCompleteness( $loaded );
1179 $this->assertRevisionRecordsEqual( $restored, $loaded );
1181 // can we find it directly in the database?
1182 $this->assertRevisionExistsInDatabase( $restored );
1186 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1188 public function testLoadRevisionFromId() {
1189 $title = Title
::newFromText( __METHOD__
);
1190 $page = WikiPage
::factory( $title );
1191 /** @var Revision $rev */
1192 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1193 ->value
['revision'];
1195 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1196 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1197 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1201 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1203 public function testLoadRevisionFromPageId() {
1204 $title = Title
::newFromText( __METHOD__
);
1205 $page = WikiPage
::factory( $title );
1206 /** @var Revision $rev */
1207 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1208 ->value
['revision'];
1210 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1211 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1212 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1216 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1218 public function testLoadRevisionFromTitle() {
1219 $title = Title
::newFromText( __METHOD__
);
1220 $page = WikiPage
::factory( $title );
1221 /** @var Revision $rev */
1222 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1223 ->value
['revision'];
1225 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1226 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1227 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1231 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1233 public function testLoadRevisionFromTimestamp() {
1234 $title = Title
::newFromText( __METHOD__
);
1235 $page = WikiPage
::factory( $title );
1236 /** @var Revision $revOne */
1237 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1238 ->value
['revision'];
1239 // Sleep to ensure different timestamps... )(evil)
1241 /** @var Revision $revTwo */
1242 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1243 ->value
['revision'];
1245 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1247 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1251 $store->loadRevisionFromTimestamp(
1252 wfGetDB( DB_MASTER
),
1254 $revOne->getTimestamp()
1259 $store->loadRevisionFromTimestamp(
1260 wfGetDB( DB_MASTER
),
1262 $revTwo->getTimestamp()
1268 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1270 public function testGetParentLengths() {
1271 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1272 /** @var Revision $revOne */
1273 $revOne = $page->doEditContent(
1274 new WikitextContent( __METHOD__
), __METHOD__
1275 )->value
['revision'];
1276 /** @var Revision $revTwo */
1277 $revTwo = $page->doEditContent(
1278 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1279 )->value
['revision'];
1281 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1284 $revOne->getId() => strlen( __METHOD__
),
1286 $store->listRevisionSizes(
1287 wfGetDB( DB_MASTER
),
1288 [ $revOne->getId() ]
1293 $revOne->getId() => strlen( __METHOD__
),
1294 $revTwo->getId() => strlen( __METHOD__
) +
1,
1296 $store->listRevisionSizes(
1297 wfGetDB( DB_MASTER
),
1298 [ $revOne->getId(), $revTwo->getId() ]
1304 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1306 public function testGetPreviousRevision() {
1307 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1308 /** @var Revision $revOne */
1309 $revOne = $page->doEditContent(
1310 new WikitextContent( __METHOD__
), __METHOD__
1311 )->value
['revision'];
1312 /** @var Revision $revTwo */
1313 $revTwo = $page->doEditContent(
1314 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1315 )->value
['revision'];
1317 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1319 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1323 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1328 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1330 public function testGetNextRevision() {
1331 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1332 /** @var Revision $revOne */
1333 $revOne = $page->doEditContent(
1334 new WikitextContent( __METHOD__
), __METHOD__
1335 )->value
['revision'];
1336 /** @var Revision $revTwo */
1337 $revTwo = $page->doEditContent(
1338 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1339 )->value
['revision'];
1341 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1344 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1347 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1352 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1354 public function testGetTimestampFromId_found() {
1355 $page = $this->getTestPage();
1356 /** @var Revision $rev */
1357 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1358 ->value
['revision'];
1360 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1361 $result = $store->getTimestampFromId(
1366 $this->assertSame( $rev->getTimestamp(), $result );
1370 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1372 public function testGetTimestampFromId_notFound() {
1373 $page = $this->getTestPage();
1374 /** @var Revision $rev */
1375 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1376 ->value
['revision'];
1378 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1379 $result = $store->getTimestampFromId(
1384 $this->assertFalse( $result );
1388 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1390 public function testCountRevisionsByPageId() {
1391 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1392 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1396 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1398 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1401 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1403 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1406 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1411 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1413 public function testCountRevisionsByTitle() {
1414 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1415 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1419 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1421 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1424 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1426 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1429 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1434 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1436 public function testUserWasLastToEdit_false() {
1437 $sysop = $this->getTestSysop()->getUser();
1438 $page = $this->getTestPage();
1439 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1441 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1442 $result = $store->userWasLastToEdit(
1443 wfGetDB( DB_MASTER
),
1448 $this->assertFalse( $result );
1452 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1454 public function testUserWasLastToEdit_true() {
1455 $startTime = wfTimestampNow();
1456 $sysop = $this->getTestSysop()->getUser();
1457 $page = $this->getTestPage();
1458 $page->doEditContent(
1459 new WikitextContent( __METHOD__
),
1466 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1467 $result = $store->userWasLastToEdit(
1468 wfGetDB( DB_MASTER
),
1473 $this->assertTrue( $result );
1477 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1479 public function testGetKnownCurrentRevision() {
1480 $page = $this->getTestPage();
1481 /** @var Revision $rev */
1482 $rev = $page->doEditContent(
1483 new WikitextContent( __METHOD__
. 'b' ),
1487 $this->getTestUser()->getUser()
1488 )->value
['revision'];
1490 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1491 $record = $store->getKnownCurrentRevision(
1496 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1499 public function provideNewMutableRevisionFromArray() {
1500 yield
'Basic array, content object' => [
1504 'timestamp' => '20171017114835',
1505 'user_text' => '111.0.1.2',
1507 'minor_edit' => false,
1511 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1512 'comment' => 'Goat Comment!',
1513 'content' => new WikitextContent( 'Some Content' ),
1516 yield
'Basic array, serialized text' => [
1520 'timestamp' => '20171017114835',
1521 'user_text' => '111.0.1.2',
1523 'minor_edit' => false,
1527 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1528 'comment' => 'Goat Comment!',
1529 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1532 yield
'Basic array, serialized text, utf-8 flags' => [
1536 'timestamp' => '20171017114835',
1537 'user_text' => '111.0.1.2',
1539 'minor_edit' => false,
1543 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1544 'comment' => 'Goat Comment!',
1545 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1549 yield
'Basic array, with title' => [
1551 'title' => Title
::newFromText( 'SomeText' ),
1552 'timestamp' => '20171017114835',
1553 'user_text' => '111.0.1.2',
1555 'minor_edit' => false,
1559 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1560 'comment' => 'Goat Comment!',
1561 'content' => new WikitextContent( 'Some Content' ),
1564 yield
'Basic array, no user field' => [
1568 'timestamp' => '20171017114835',
1569 'user_text' => '111.0.1.3',
1570 'minor_edit' => false,
1574 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1575 'comment' => 'Goat Comment!',
1576 'content' => new WikitextContent( 'Some Content' ),
1582 * @dataProvider provideNewMutableRevisionFromArray
1583 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1585 public function testNewMutableRevisionFromArray( array $array ) {
1586 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1588 // HACK: if $array['page'] is given, make sure that the page exists
1589 if ( isset( $array['page'] ) ) {
1590 $t = Title
::newFromID( $array['page'] );
1591 if ( !$t ||
!$t->exists() ) {
1592 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1593 $info = $this->insertPage( $t );
1594 $array['page'] = $info['id'];
1598 $result = $store->newMutableRevisionFromArray( $array );
1600 if ( isset( $array['id'] ) ) {
1601 $this->assertSame( $array['id'], $result->getId() );
1603 if ( isset( $array['page'] ) ) {
1604 $this->assertSame( $array['page'], $result->getPageId() );
1606 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1607 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1608 if ( isset( $array['user'] ) ) {
1609 $this->assertSame( $array['user'], $result->getUser()->getId() );
1611 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1612 $this->assertSame( $array['deleted'], $result->getVisibility() );
1613 $this->assertSame( $array['len'], $result->getSize() );
1614 $this->assertSame( $array['parent_id'], $result->getParentId() );
1615 $this->assertSame( $array['sha1'], $result->getSha1() );
1616 $this->assertSame( $array['comment'], $result->getComment()->text
);
1617 if ( isset( $array['content'] ) ) {
1618 foreach ( $array['content'] as $role => $content ) {
1620 $result->getContent( $role )->equals( $content )
1623 } elseif ( isset( $array['text'] ) ) {
1624 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1625 } elseif ( isset( $array['content_format'] ) ) {
1627 $array['content_format'],
1628 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1630 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1635 * @dataProvider provideNewMutableRevisionFromArray
1636 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1638 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1639 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1640 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1641 $blobStore = new SqlBlobStore( $lb, $cache );
1642 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1644 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1645 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1646 ->disableOriginalConstructor()
1648 $factory->expects( $this->any() )
1649 ->method( 'newBlobStore' )
1650 ->willReturn( $blobStore );
1651 $factory->expects( $this->any() )
1652 ->method( 'newSqlBlobStore' )
1653 ->willReturn( $blobStore );
1655 $this->setService( 'BlobStoreFactory', $factory );
1657 $this->testNewMutableRevisionFromArray( $array );