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 needsDB() {
72 public function setUp() {
74 $this->tablesUsed
[] = 'archive';
75 $this->tablesUsed
[] = 'page';
76 $this->tablesUsed
[] = 'revision';
77 $this->tablesUsed
[] = 'comment';
79 $this->tablesUsed +
= $this->getMcrTablesToReset();
81 $this->setMwGlobals( [
82 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
83 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
84 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
85 'wgActorTableSchemaMigrationStage' => MIGRATION_OLD
,
88 $this->overrideMwServices();
91 protected function addCoreDBData() {
92 // Blank out. This would fail with a modified schema, and we don't need it.
98 protected function getTestPageTitle() {
99 if ( $this->testPageTitle
) {
100 return $this->testPageTitle
;
103 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
104 return $this->testPageTitle
;
109 protected function getTestPage() {
110 if ( $this->testPage
) {
111 return $this->testPage
;
114 $title = $this->getTestPageTitle();
115 $this->testPage
= WikiPage
::factory( $title );
117 if ( !$this->testPage
->exists() ) {
118 // Make sure we don't write to the live db.
119 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
121 $user = static::getTestSysop()->getUser();
123 $this->testPage
->doEditContent(
124 new WikitextContent( 'UTContent-' . __CLASS__
),
125 'UTPageSummary-' . __CLASS__
,
126 EDIT_NEW | EDIT_SUPPRESS_RC
,
132 return $this->testPage
;
136 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
138 private function getLoadBalancerMock( array $server ) {
139 $lb = $this->getMockBuilder( LoadBalancer
::class )
140 ->setMethods( [ 'reallyOpenConnection' ] )
141 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
144 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
145 function ( array $server, $dbNameOverride ) {
146 return $this->getDatabaseMock( $server );
154 * @return Database|PHPUnit_Framework_MockObject_MockObject
156 private function getDatabaseMock( array $params ) {
157 $db = $this->getMockBuilder( DatabaseSqlite
::class )
158 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
159 ->setConstructorArgs( [ $params ] )
162 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
163 $db->method( 'isOpen' )->willReturn( true );
168 public function provideDomainCheck() {
169 yield
[ false, 'test', '' ];
170 yield
[ 'test', 'test', '' ];
172 yield
[ false, 'test', 'foo_' ];
173 yield
[ 'test-foo_', 'test', 'foo_' ];
175 yield
[ false, 'dash-test', '' ];
176 yield
[ 'dash-test', 'dash-test', '' ];
178 yield
[ false, 'underscore_test', 'foo_' ];
179 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
183 * @dataProvider provideDomainCheck
184 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
186 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
189 'wgDBname' => $dbName,
190 'wgDBprefix' => $dbPrefix,
194 $loadBalancer = $this->getLoadBalancerMock(
197 'dbDirectory' => '*dummy*',
199 'password' => 'test',
207 'trxProfiler' => new TransactionProfiler(),
208 'connLogger' => new \Psr\Log\
NullLogger(),
209 'queryLogger' => new \Psr\Log\
NullLogger(),
210 'errorLogger' => function () {
212 'deprecationLogger' => function () {
216 'tablePrefix' => $dbPrefix,
219 $db = $loadBalancer->getConnection( DB_REPLICA
);
221 /** @var SqlBlobStore $blobStore */
222 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
223 ->disableOriginalConstructor()
226 $store = new RevisionStore(
229 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
230 MediaWikiServices
::getInstance()->getCommentStore(),
231 MediaWikiServices
::getInstance()->getContentModelStore(),
232 MediaWikiServices
::getInstance()->getSlotRoleStore(),
233 $this->getMcrMigrationStage(),
234 MediaWikiServices
::getInstance()->getActorMigration(),
238 $count = $store->countRevisionsByPageId( $db, 0 );
240 // Dummy check to make PhpUnit happy. We are really only interested in
241 // countRevisionsByPageId not failing due to the DB domain check.
242 $this->assertSame( 0, $count );
245 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
246 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
247 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
248 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
249 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
252 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
254 $r1->getPageAsLinkTarget()->getNamespace(),
255 $r2->getPageAsLinkTarget()->getNamespace()
259 $r1->getPageAsLinkTarget()->getText(),
260 $r2->getPageAsLinkTarget()->getText()
263 if ( $r1->getParentId() ) {
264 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
267 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
268 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
269 $this->assertEquals( $r1->getComment(), $r2->getComment() );
270 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
271 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
272 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
273 $this->assertEquals( $r1->getSize(), $r2->getSize() );
274 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
275 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
276 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
277 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
278 foreach ( $r1->getSlotRoles() as $role ) {
279 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
280 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
283 RevisionRecord
::DELETED_TEXT
,
284 RevisionRecord
::DELETED_COMMENT
,
285 RevisionRecord
::DELETED_USER
,
286 RevisionRecord
::DELETED_RESTRICTED
,
288 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
292 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
293 $this->assertSame( $s1->getRole(), $s2->getRole() );
294 $this->assertSame( $s1->getModel(), $s2->getModel() );
295 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
296 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
297 $this->assertSame( $s1->getSize(), $s2->getSize() );
298 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
300 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
301 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
304 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
305 $this->assertTrue( $r->hasSlot( 'main' ) );
306 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( 'main' ) );
307 $this->assertInstanceOf( Content
::class, $r->getContent( 'main' ) );
309 foreach ( $r->getSlotRoles() as $role ) {
310 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
314 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
315 $this->assertTrue( $slot->hasAddress() );
316 $this->assertSame( $r->getId(), $slot->getRevision() );
318 $this->assertInstanceOf( Content
::class, $slot->getContent() );
322 * @param mixed[] $details
324 * @return RevisionRecord
326 private function getRevisionRecordFromDetailsArray( $details = [] ) {
327 // Convert some values that can't be provided by dataProviders
328 if ( isset( $details['user'] ) && $details['user'] === true ) {
329 $details['user'] = $this->getTestUser()->getUser();
331 if ( isset( $details['page'] ) && $details['page'] === true ) {
332 $details['page'] = $this->getTestPage()->getId();
334 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
335 $details['parent'] = $this->getTestPage()->getLatest();
338 // Create the RevisionRecord with any available data
339 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
340 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
341 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
342 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
343 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
344 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
345 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
346 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
347 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
348 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
349 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
350 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
352 if ( isset( $details['content'] ) ) {
353 foreach ( $details['content'] as $role => $content ) {
354 $rev->setContent( $role, $content );
361 public function provideInsertRevisionOn_successes() {
362 yield
'Bare minimum revision insertion' => [
364 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
366 'comment' => $this->getRandomCommentStoreComment(),
367 'timestamp' => '20171117010101',
371 yield
'Detailed revision insertion' => [
373 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
376 'comment' => $this->getRandomCommentStoreComment(),
377 'timestamp' => '20171117010101',
380 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
385 protected function getRandomCommentStoreComment() {
386 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
390 * @dataProvider provideInsertRevisionOn_successes
391 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
392 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
393 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
395 public function testInsertRevisionOn_successes(
396 array $revDetails = []
398 $title = $this->getTestPageTitle();
399 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
401 $this->overrideMwServices();
402 $store = MediaWikiServices
::getInstance()->getRevisionStore();
403 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
405 // is the new revision correct?
406 $this->assertRevisionCompleteness( $return );
407 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
408 $this->assertRevisionRecordsEqual( $rev, $return );
410 // can we load it from the store?
411 $loaded = $store->getRevisionById( $return->getId() );
412 $this->assertRevisionCompleteness( $loaded );
413 $this->assertRevisionRecordsEqual( $return, $loaded );
415 // can we find it directly in the database?
416 $this->assertRevisionExistsInDatabase( $return );
419 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
420 $row = $this->revisionToRow( new Revision( $rev ), [] );
422 // unset nulled fields
423 unset( $row->rev_content_model
);
424 unset( $row->rev_content_format
);
427 unset( $row->rev_comment_text
);
428 unset( $row->rev_comment_data
);
429 unset( $row->rev_comment_cid
);
430 unset( $row->rev_comment_id
);
432 $store = MediaWikiServices
::getInstance()->getRevisionStore();
433 $queryInfo = $store->getQueryInfo( [ 'user' ] );
435 $row = get_object_vars( $row );
437 $queryInfo['tables'],
439 [ 'rev_id' => $rev->getId() ],
440 [ array_values( $row ) ],
447 * @param SlotRecord $a
448 * @param SlotRecord $b
450 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
451 // Assert that the same blob address has been used.
452 $this->assertSame( $a->getAddress(), $b->getAddress() );
456 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
458 public function testInsertRevisionOn_blobAddressExists() {
459 $title = $this->getTestPageTitle();
461 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
463 'comment' => $this->getRandomCommentStoreComment(),
464 'timestamp' => '20171117010101',
468 $this->overrideMwServices();
469 $store = MediaWikiServices
::getInstance()->getRevisionStore();
471 // Insert the first revision
472 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
473 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
474 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
475 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
477 // Insert a second revision inheriting the same blob address
478 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
479 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
480 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
481 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
482 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
484 $firstMainSlot = $firstReturn->getSlot( 'main' );
485 $secondMainSlot = $secondReturn->getSlot( 'main' );
487 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
489 // And that different revisions have been created.
490 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
492 // Make sure the slot rows reference the correct revision
493 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
494 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
497 public function provideInsertRevisionOn_failures() {
500 'comment' => $this->getRandomCommentStoreComment(),
501 'timestamp' => '20171117010101',
504 new InvalidArgumentException( 'main slot must be provided' )
506 yield
'no main slot' => [
508 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
509 'comment' => $this->getRandomCommentStoreComment(),
510 'timestamp' => '20171117010101',
513 new InvalidArgumentException( 'main slot must be provided' )
515 yield
'no timestamp' => [
517 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
518 'comment' => $this->getRandomCommentStoreComment(),
521 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
523 yield
'no comment' => [
525 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
526 'timestamp' => '20171117010101',
529 new IncompleteRevisionException( 'comment must not be NULL!' )
533 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
534 'comment' => $this->getRandomCommentStoreComment(),
535 'timestamp' => '20171117010101',
537 new IncompleteRevisionException( 'user must not be NULL!' )
542 * @dataProvider provideInsertRevisionOn_failures
543 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
545 public function testInsertRevisionOn_failures(
546 array $revDetails = [],
549 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
551 $store = MediaWikiServices
::getInstance()->getRevisionStore();
553 $this->setExpectedException(
554 get_class( $exception ),
555 $exception->getMessage(),
556 $exception->getCode()
558 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
561 public function provideNewNullRevision() {
563 Title
::newFromText( 'UTPage_notAutoCreated' ),
564 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
565 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
569 Title
::newFromText( 'UTPage_notAutoCreated' ),
570 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
571 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
577 * @dataProvider provideNewNullRevision
578 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
579 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
581 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
582 $this->overrideMwServices();
584 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
585 $page = WikiPage
::factory( $title );
587 if ( !$page->exists() ) {
588 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
591 $revDetails['page'] = $page->getId();
592 $revDetails['timestamp'] = wfTimestampNow();
593 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
594 $revDetails['user'] = $user;
596 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
597 $store = MediaWikiServices
::getInstance()->getRevisionStore();
599 $dbw = wfGetDB( DB_MASTER
);
600 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
601 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
603 $record = $store->newNullRevision(
604 wfGetDB( DB_MASTER
),
611 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
612 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
613 $this->assertEquals( $comment, $record->getComment() );
614 $this->assertEquals( $minor, $record->isMinor() );
615 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
616 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
618 $this->assertArrayEquals(
619 $baseRev->getSlotRoles(),
620 $record->getSlotRoles()
623 foreach ( $baseRev->getSlotRoles() as $role ) {
624 $parentSlot = $baseRev->getSlot( $role );
625 $slot = $record->getSlot( $role );
627 $this->assertTrue( $slot->isInherited(), 'isInherited' );
628 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
629 $this->assertSameSlotContent( $parentSlot, $slot );
634 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
636 public function testNewNullRevision_nonExistingTitle() {
637 $store = MediaWikiServices
::getInstance()->getRevisionStore();
638 $record = $store->newNullRevision(
639 wfGetDB( DB_MASTER
),
640 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
641 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
643 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
645 $this->assertNull( $record );
649 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
651 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
652 $page = $this->getTestPage();
653 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
654 /** @var Revision $rev */
655 $rev = $status->value
['revision'];
657 $store = MediaWikiServices
::getInstance()->getRevisionStore();
658 $revisionRecord = $store->getRevisionById( $rev->getId() );
659 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
661 $this->assertGreaterThan( 0, $result );
663 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
669 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
671 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
672 // This assumes that sysops are auto patrolled
673 $sysop = $this->getTestSysop()->getUser();
674 $page = $this->getTestPage();
675 $status = $page->doEditContent(
676 new WikitextContent( __METHOD__
),
682 /** @var Revision $rev */
683 $rev = $status->value
['revision'];
685 $store = MediaWikiServices
::getInstance()->getRevisionStore();
686 $revisionRecord = $store->getRevisionById( $rev->getId() );
687 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
689 $this->assertSame( 0, $result );
693 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
695 public function testGetRecentChange() {
696 $page = $this->getTestPage();
697 $content = new WikitextContent( __METHOD__
);
698 $status = $page->doEditContent( $content, __METHOD__
);
699 /** @var Revision $rev */
700 $rev = $status->value
['revision'];
702 $store = MediaWikiServices
::getInstance()->getRevisionStore();
703 $revRecord = $store->getRevisionById( $rev->getId() );
704 $recentChange = $store->getRecentChange( $revRecord );
706 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
707 $this->assertEquals( $rev->getRecentChange(), $recentChange );
711 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
713 public function testGetRevisionById() {
714 $page = $this->getTestPage();
715 $content = new WikitextContent( __METHOD__
);
716 $status = $page->doEditContent( $content, __METHOD__
);
717 /** @var Revision $rev */
718 $rev = $status->value
['revision'];
720 $store = MediaWikiServices
::getInstance()->getRevisionStore();
721 $revRecord = $store->getRevisionById( $rev->getId() );
723 $this->assertSame( $rev->getId(), $revRecord->getId() );
724 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
725 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
729 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
731 public function testGetRevisionByTitle() {
732 $page = $this->getTestPage();
733 $content = new WikitextContent( __METHOD__
);
734 $status = $page->doEditContent( $content, __METHOD__
);
735 /** @var Revision $rev */
736 $rev = $status->value
['revision'];
738 $store = MediaWikiServices
::getInstance()->getRevisionStore();
739 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
741 $this->assertSame( $rev->getId(), $revRecord->getId() );
742 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
743 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
747 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
749 public function testGetRevisionByPageId() {
750 $page = $this->getTestPage();
751 $content = new WikitextContent( __METHOD__
);
752 $status = $page->doEditContent( $content, __METHOD__
);
753 /** @var Revision $rev */
754 $rev = $status->value
['revision'];
756 $store = MediaWikiServices
::getInstance()->getRevisionStore();
757 $revRecord = $store->getRevisionByPageId( $page->getId() );
759 $this->assertSame( $rev->getId(), $revRecord->getId() );
760 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
761 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
765 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
767 public function testGetRevisionByTimestamp() {
768 // Make sure there is 1 second between the last revision and the rev we create...
769 // Otherwise we might not get the correct revision and the test may fail...
771 $page = $this->getTestPage();
773 $content = new WikitextContent( __METHOD__
);
774 $status = $page->doEditContent( $content, __METHOD__
);
775 /** @var Revision $rev */
776 $rev = $status->value
['revision'];
778 $store = MediaWikiServices
::getInstance()->getRevisionStore();
779 $revRecord = $store->getRevisionByTimestamp(
784 $this->assertSame( $rev->getId(), $revRecord->getId() );
785 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
786 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
789 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
790 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
791 $page = WikiPage
::factory( $rev->getTitle() );
794 'rev_id' => (string)$rev->getId(),
795 'rev_page' => (string)$rev->getPage(),
796 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
797 'rev_user_text' => (string)$rev->getUserText(),
798 'rev_user' => (string)$rev->getUser(),
799 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
800 'rev_deleted' => (string)$rev->getVisibility(),
801 'rev_len' => (string)$rev->getSize(),
802 'rev_parent_id' => (string)$rev->getParentId(),
803 'rev_sha1' => (string)$rev->getSha1(),
806 if ( in_array( 'page', $options ) ) {
808 'page_namespace' => (string)$page->getTitle()->getNamespace(),
809 'page_title' => $page->getTitle()->getDBkey(),
810 'page_id' => (string)$page->getId(),
811 'page_latest' => (string)$page->getLatest(),
812 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
813 'page_len' => (string)$page->getContent()->getSize(),
817 if ( in_array( 'user', $options ) ) {
819 'user_name' => (string)$rev->getUserText(),
823 if ( in_array( 'comment', $options ) ) {
825 'rev_comment_text' => $rev->getComment(),
826 'rev_comment_data' => null,
827 'rev_comment_cid' => null,
831 if ( $rev->getId() ) {
833 'rev_id' => (string)$rev->getId(),
837 return (object)$fields;
840 protected function assertRevisionRecordMatchesRevision(
842 RevisionRecord
$record
844 $this->assertSame( $rev->getId(), $record->getId() );
845 $this->assertSame( $rev->getPage(), $record->getPageId() );
846 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
847 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
848 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
849 $this->assertSame( $rev->isMinor(), $record->isMinor() );
850 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
851 $this->assertSame( $rev->getSize(), $record->getSize() );
853 * @note As of MW 1.31, the database schema allows the parent ID to be
854 * NULL to indicate that it is unknown.
856 $expectedParent = $rev->getParentId();
857 if ( $expectedParent === null ) {
860 $this->assertSame( $expectedParent, $record->getParentId() );
861 $this->assertSame( $rev->getSha1(), $record->getSha1() );
862 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
863 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
864 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
865 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
867 $revRec = $rev->getRevisionRecord();
868 $revMain = $revRec->getSlot( 'main' );
869 $recMain = $record->getSlot( 'main' );
871 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
872 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
873 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
875 if ( $revMain->hasOrigin() ) {
876 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
879 if ( $revMain->hasAddress() ) {
880 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
883 if ( $revMain->hasContentId() ) {
884 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
889 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
890 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
892 public function testNewRevisionFromRow_getQueryInfo() {
893 $page = $this->getTestPage();
894 $text = __METHOD__
. 'a-ä';
895 /** @var Revision $rev */
896 $rev = $page->doEditContent(
897 new WikitextContent( $text ),
899 )->value
['revision'];
901 $store = MediaWikiServices
::getInstance()->getRevisionStore();
902 $info = $store->getQueryInfo();
903 $row = $this->db
->selectRow(
906 [ 'rev_id' => $rev->getId() ],
911 $record = $store->newRevisionFromRow(
916 $this->assertRevisionRecordMatchesRevision( $rev, $record );
917 $this->assertSame( $text, $rev->getContent()->serialize() );
921 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
923 public function testNewRevisionFromRow_anonEdit() {
924 $page = $this->getTestPage();
925 $text = __METHOD__
. 'a-ä';
926 /** @var Revision $rev */
927 $rev = $page->doEditContent(
928 new WikitextContent( $text ),
930 )->value
['revision'];
932 $store = MediaWikiServices
::getInstance()->getRevisionStore();
933 $record = $store->newRevisionFromRow(
934 $this->revisionToRow( $rev ),
938 $this->assertRevisionRecordMatchesRevision( $rev, $record );
939 $this->assertSame( $text, $rev->getContent()->serialize() );
943 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
945 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
946 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
947 $this->overrideMwServices();
948 $page = $this->getTestPage();
949 $text = __METHOD__
. 'a-ä';
950 /** @var Revision $rev */
951 $rev = $page->doEditContent(
952 new WikitextContent( $text ),
954 )->value
['revision'];
956 $store = MediaWikiServices
::getInstance()->getRevisionStore();
957 $record = $store->newRevisionFromRow(
958 $this->revisionToRow( $rev ),
962 $this->assertRevisionRecordMatchesRevision( $rev, $record );
963 $this->assertSame( $text, $rev->getContent()->serialize() );
967 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
969 public function testNewRevisionFromRow_userEdit() {
970 $page = $this->getTestPage();
971 $text = __METHOD__
. 'b-ä';
972 /** @var Revision $rev */
973 $rev = $page->doEditContent(
974 new WikitextContent( $text ),
978 $this->getTestUser()->getUser()
979 )->value
['revision'];
981 $store = MediaWikiServices
::getInstance()->getRevisionStore();
982 $record = $store->newRevisionFromRow(
983 $this->revisionToRow( $rev ),
987 $this->assertRevisionRecordMatchesRevision( $rev, $record );
988 $this->assertSame( $text, $rev->getContent()->serialize() );
992 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
993 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
995 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
996 $store = MediaWikiServices
::getInstance()->getRevisionStore();
997 $title = Title
::newFromText( __METHOD__
);
998 $text = __METHOD__
. '-bä';
999 $page = WikiPage
::factory( $title );
1000 /** @var Revision $orig */
1001 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1002 ->value
['revision'];
1003 $page->doDeleteArticle( __METHOD__
);
1005 $db = wfGetDB( DB_MASTER
);
1006 $arQuery = $store->getArchiveQueryInfo();
1008 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1009 __METHOD__
, [], $arQuery['joins']
1011 $this->assertTrue( is_object( $res ), 'query failed' );
1013 $row = $res->fetchObject();
1015 $record = $store->newRevisionFromArchiveRow( $row );
1017 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1018 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1022 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1024 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1025 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1026 $this->overrideMwServices();
1027 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1028 $title = Title
::newFromText( __METHOD__
);
1029 $text = __METHOD__
. '-bä';
1030 $page = WikiPage
::factory( $title );
1031 /** @var Revision $orig */
1032 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1033 ->value
['revision'];
1034 $page->doDeleteArticle( __METHOD__
);
1036 $db = wfGetDB( DB_MASTER
);
1037 $arQuery = $store->getArchiveQueryInfo();
1039 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1040 __METHOD__
, [], $arQuery['joins']
1042 $this->assertTrue( is_object( $res ), 'query failed' );
1044 $row = $res->fetchObject();
1046 $record = $store->newRevisionFromArchiveRow( $row );
1048 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1049 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1053 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1055 public function testNewRevisionFromArchiveRow_no_user() {
1056 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1060 'ar_page_id' => '2',
1061 'ar_namespace' => '0',
1062 'ar_title' => 'Something',
1064 'ar_text_id' => '47',
1065 'ar_timestamp' => '20180528192356',
1066 'ar_minor_edit' => '0',
1067 'ar_deleted' => '0',
1069 'ar_parent_id' => '0',
1070 'ar_sha1' => 'deadbeef',
1071 'ar_comment_text' => 'whatever',
1072 'ar_comment_data' => null,
1073 'ar_comment_cid' => null,
1075 'ar_user_text' => '', // this is the important bit
1077 'ar_content_format' => null,
1078 'ar_content_model' => null,
1081 \Wikimedia\
suppressWarnings();
1082 $record = $store->newRevisionFromArchiveRow( $row );
1083 \Wikimedia\
suppressWarnings( true );
1085 $this->assertInstanceOf( RevisionRecord
::class, $record );
1086 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1087 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1091 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
1093 public function testNewRevisionFromRow_no_user() {
1094 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1095 $title = Title
::newFromText( __METHOD__
);
1100 'page_namespace' => '0',
1101 'page_title' => $title->getText(),
1102 'rev_text_id' => '47',
1103 'rev_timestamp' => '20180528192356',
1104 'rev_minor_edit' => '0',
1105 'rev_deleted' => '0',
1107 'rev_parent_id' => '0',
1108 'rev_sha1' => 'deadbeef',
1109 'rev_comment_text' => 'whatever',
1110 'rev_comment_data' => null,
1111 'rev_comment_cid' => null,
1113 'rev_user_text' => '', // this is the important bit
1114 'rev_actor' => null,
1115 'rev_content_format' => null,
1116 'rev_content_model' => null,
1119 \Wikimedia\
suppressWarnings();
1120 $record = $store->newRevisionFromRow( $row, 0, $title );
1121 \Wikimedia\
suppressWarnings( true );
1123 $this->assertNotNull( $record );
1124 $this->assertNotNull( $record->getUser() );
1125 $this->assertNotEmpty( $record->getUser()->getName() );
1129 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1131 public function testInsertRevisionOn_archive() {
1132 // This is a round trip test for deletion and undeletion of a
1133 // revision row via the archive table.
1135 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1136 $title = Title
::newFromText( __METHOD__
);
1138 $page = WikiPage
::factory( $title );
1139 /** @var Revision $origRev */
1140 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1141 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1142 ->value
['revision'];
1143 $orig = $origRev->getRevisionRecord();
1144 $page->doDeleteArticle( __METHOD__
);
1146 // re-create page, so we can later load revisions for it
1147 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1149 $db = wfGetDB( DB_MASTER
);
1150 $arQuery = $store->getArchiveQueryInfo();
1151 $row = $db->selectRow(
1152 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1153 __METHOD__
, [], $arQuery['joins']
1156 $this->assertNotFalse( $row, 'query failed' );
1158 $record = $store->newRevisionFromArchiveRow(
1162 [ 'page_id' => $title->getArticleID() ]
1165 $restored = $store->insertRevisionOn( $record, $db );
1167 // is the new revision correct?
1168 $this->assertRevisionCompleteness( $restored );
1169 $this->assertRevisionRecordsEqual( $record, $restored );
1171 // does the new revision use the original slot?
1172 $recMain = $record->getSlot( 'main' );
1173 $restMain = $restored->getSlot( 'main' );
1174 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1175 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1176 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1177 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1179 // can we load it from the store?
1180 $loaded = $store->getRevisionById( $restored->getId() );
1181 $this->assertNotNull( $loaded );
1182 $this->assertRevisionCompleteness( $loaded );
1183 $this->assertRevisionRecordsEqual( $restored, $loaded );
1185 // can we find it directly in the database?
1186 $this->assertRevisionExistsInDatabase( $restored );
1190 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1192 public function testLoadRevisionFromId() {
1193 $title = Title
::newFromText( __METHOD__
);
1194 $page = WikiPage
::factory( $title );
1195 /** @var Revision $rev */
1196 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1197 ->value
['revision'];
1199 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1200 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1201 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1205 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1207 public function testLoadRevisionFromPageId() {
1208 $title = Title
::newFromText( __METHOD__
);
1209 $page = WikiPage
::factory( $title );
1210 /** @var Revision $rev */
1211 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1212 ->value
['revision'];
1214 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1215 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1216 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1220 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1222 public function testLoadRevisionFromTitle() {
1223 $title = Title
::newFromText( __METHOD__
);
1224 $page = WikiPage
::factory( $title );
1225 /** @var Revision $rev */
1226 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1227 ->value
['revision'];
1229 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1230 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1231 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1235 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1237 public function testLoadRevisionFromTimestamp() {
1238 $title = Title
::newFromText( __METHOD__
);
1239 $page = WikiPage
::factory( $title );
1240 /** @var Revision $revOne */
1241 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1242 ->value
['revision'];
1243 // Sleep to ensure different timestamps... )(evil)
1245 /** @var Revision $revTwo */
1246 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1247 ->value
['revision'];
1249 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1251 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1255 $store->loadRevisionFromTimestamp(
1256 wfGetDB( DB_MASTER
),
1258 $revOne->getTimestamp()
1263 $store->loadRevisionFromTimestamp(
1264 wfGetDB( DB_MASTER
),
1266 $revTwo->getTimestamp()
1272 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1274 public function testGetParentLengths() {
1275 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1276 /** @var Revision $revOne */
1277 $revOne = $page->doEditContent(
1278 new WikitextContent( __METHOD__
), __METHOD__
1279 )->value
['revision'];
1280 /** @var Revision $revTwo */
1281 $revTwo = $page->doEditContent(
1282 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1283 )->value
['revision'];
1285 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1288 $revOne->getId() => strlen( __METHOD__
),
1290 $store->listRevisionSizes(
1291 wfGetDB( DB_MASTER
),
1292 [ $revOne->getId() ]
1297 $revOne->getId() => strlen( __METHOD__
),
1298 $revTwo->getId() => strlen( __METHOD__
) +
1,
1300 $store->listRevisionSizes(
1301 wfGetDB( DB_MASTER
),
1302 [ $revOne->getId(), $revTwo->getId() ]
1308 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1310 public function testGetPreviousRevision() {
1311 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1312 /** @var Revision $revOne */
1313 $revOne = $page->doEditContent(
1314 new WikitextContent( __METHOD__
), __METHOD__
1315 )->value
['revision'];
1316 /** @var Revision $revTwo */
1317 $revTwo = $page->doEditContent(
1318 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1319 )->value
['revision'];
1321 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1323 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1327 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1332 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1334 public function testGetNextRevision() {
1335 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1336 /** @var Revision $revOne */
1337 $revOne = $page->doEditContent(
1338 new WikitextContent( __METHOD__
), __METHOD__
1339 )->value
['revision'];
1340 /** @var Revision $revTwo */
1341 $revTwo = $page->doEditContent(
1342 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1343 )->value
['revision'];
1345 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1348 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1351 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1356 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1358 public function testGetTimestampFromId_found() {
1359 $page = $this->getTestPage();
1360 /** @var Revision $rev */
1361 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1362 ->value
['revision'];
1364 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1365 $result = $store->getTimestampFromId(
1370 $this->assertSame( $rev->getTimestamp(), $result );
1374 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1376 public function testGetTimestampFromId_notFound() {
1377 $page = $this->getTestPage();
1378 /** @var Revision $rev */
1379 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1380 ->value
['revision'];
1382 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1383 $result = $store->getTimestampFromId(
1388 $this->assertFalse( $result );
1392 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1394 public function testCountRevisionsByPageId() {
1395 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1396 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1400 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1402 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1405 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1407 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1410 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1415 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1417 public function testCountRevisionsByTitle() {
1418 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1419 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1423 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1425 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1428 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1430 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1433 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1438 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1440 public function testUserWasLastToEdit_false() {
1441 $sysop = $this->getTestSysop()->getUser();
1442 $page = $this->getTestPage();
1443 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1445 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1446 $result = $store->userWasLastToEdit(
1447 wfGetDB( DB_MASTER
),
1452 $this->assertFalse( $result );
1456 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1458 public function testUserWasLastToEdit_true() {
1459 $startTime = wfTimestampNow();
1460 $sysop = $this->getTestSysop()->getUser();
1461 $page = $this->getTestPage();
1462 $page->doEditContent(
1463 new WikitextContent( __METHOD__
),
1470 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1471 $result = $store->userWasLastToEdit(
1472 wfGetDB( DB_MASTER
),
1477 $this->assertTrue( $result );
1481 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1483 public function testGetKnownCurrentRevision() {
1484 $page = $this->getTestPage();
1485 /** @var Revision $rev */
1486 $rev = $page->doEditContent(
1487 new WikitextContent( __METHOD__
. 'b' ),
1491 $this->getTestUser()->getUser()
1492 )->value
['revision'];
1494 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1495 $record = $store->getKnownCurrentRevision(
1500 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1503 public function provideNewMutableRevisionFromArray() {
1504 yield
'Basic array, content object' => [
1508 'timestamp' => '20171017114835',
1509 'user_text' => '111.0.1.2',
1511 'minor_edit' => false,
1515 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1516 'comment' => 'Goat Comment!',
1517 'content' => new WikitextContent( 'Some Content' ),
1520 yield
'Basic array, serialized text' => [
1524 'timestamp' => '20171017114835',
1525 'user_text' => '111.0.1.2',
1527 'minor_edit' => false,
1531 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1532 'comment' => 'Goat Comment!',
1533 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1536 yield
'Basic array, serialized text, utf-8 flags' => [
1540 'timestamp' => '20171017114835',
1541 'user_text' => '111.0.1.2',
1543 'minor_edit' => false,
1547 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1548 'comment' => 'Goat Comment!',
1549 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1553 yield
'Basic array, with title' => [
1555 'title' => Title
::newFromText( 'SomeText' ),
1556 'timestamp' => '20171017114835',
1557 'user_text' => '111.0.1.2',
1559 'minor_edit' => false,
1563 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1564 'comment' => 'Goat Comment!',
1565 'content' => new WikitextContent( 'Some Content' ),
1568 yield
'Basic array, no user field' => [
1572 'timestamp' => '20171017114835',
1573 'user_text' => '111.0.1.3',
1574 'minor_edit' => false,
1578 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1579 'comment' => 'Goat Comment!',
1580 'content' => new WikitextContent( 'Some Content' ),
1586 * @dataProvider provideNewMutableRevisionFromArray
1587 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1589 public function testNewMutableRevisionFromArray( array $array ) {
1590 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1592 // HACK: if $array['page'] is given, make sure that the page exists
1593 if ( isset( $array['page'] ) ) {
1594 $t = Title
::newFromID( $array['page'] );
1595 if ( !$t ||
!$t->exists() ) {
1596 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1597 $info = $this->insertPage( $t );
1598 $array['page'] = $info['id'];
1602 $result = $store->newMutableRevisionFromArray( $array );
1604 if ( isset( $array['id'] ) ) {
1605 $this->assertSame( $array['id'], $result->getId() );
1607 if ( isset( $array['page'] ) ) {
1608 $this->assertSame( $array['page'], $result->getPageId() );
1610 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1611 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1612 if ( isset( $array['user'] ) ) {
1613 $this->assertSame( $array['user'], $result->getUser()->getId() );
1615 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1616 $this->assertSame( $array['deleted'], $result->getVisibility() );
1617 $this->assertSame( $array['len'], $result->getSize() );
1618 $this->assertSame( $array['parent_id'], $result->getParentId() );
1619 $this->assertSame( $array['sha1'], $result->getSha1() );
1620 $this->assertSame( $array['comment'], $result->getComment()->text
);
1621 if ( isset( $array['content'] ) ) {
1622 foreach ( $array['content'] as $role => $content ) {
1624 $result->getContent( $role )->equals( $content )
1627 } elseif ( isset( $array['text'] ) ) {
1628 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1629 } elseif ( isset( $array['content_format'] ) ) {
1631 $array['content_format'],
1632 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1634 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1639 * @dataProvider provideNewMutableRevisionFromArray
1640 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1642 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1643 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1644 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1645 $blobStore = new SqlBlobStore( $lb, $cache );
1646 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1648 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1649 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1650 ->disableOriginalConstructor()
1652 $factory->expects( $this->any() )
1653 ->method( 'newBlobStore' )
1654 ->willReturn( $blobStore );
1655 $factory->expects( $this->any() )
1656 ->method( 'newSqlBlobStore' )
1657 ->willReturn( $blobStore );
1659 $this->setService( 'BlobStoreFactory', $factory );
1661 $this->testNewMutableRevisionFromArray( $array );