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 MediaWikiTestCase
;
21 use PHPUnit_Framework_MockObject_MockObject
;
26 use Wikimedia\Rdbms\Database
;
27 use Wikimedia\Rdbms\DatabaseSqlite
;
28 use Wikimedia\Rdbms\FakeResultWrapper
;
29 use Wikimedia\Rdbms\LoadBalancer
;
30 use Wikimedia\Rdbms\TransactionProfiler
;
36 * @group RevisionStore
38 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
43 private $testPageTitle;
53 abstract protected function getMcrMigrationStage();
58 protected function getContentHandlerUseDB() {
65 abstract protected function getMcrTablesToReset();
67 public function needsDB() {
71 public function setUp() {
73 $this->tablesUsed
[] = 'archive';
74 $this->tablesUsed
[] = 'page';
75 $this->tablesUsed
[] = 'revision';
76 $this->tablesUsed
[] = 'comment';
78 $this->tablesUsed +
= $this->getMcrTablesToReset();
80 $this->setMwGlobals( [
81 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
82 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
83 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
84 'wgActorTableSchemaMigrationStage' => MIGRATION_OLD
,
87 $this->overrideMwServices();
90 protected function addCoreDBData() {
91 // Blank out. This would fail with a modified schema, and we don't need it.
97 protected function getTestPageTitle() {
98 if ( $this->testPageTitle
) {
99 return $this->testPageTitle
;
102 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
103 return $this->testPageTitle
;
108 protected function getTestPage() {
109 if ( $this->testPage
) {
110 return $this->testPage
;
113 $title = $this->getTestPageTitle();
114 $this->testPage
= WikiPage
::factory( $title );
116 if ( !$this->testPage
->exists() ) {
117 // Make sure we don't write to the live db.
118 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
120 $user = static::getTestSysop()->getUser();
122 $this->testPage
->doEditContent(
123 new WikitextContent( 'UTContent-' . __CLASS__
),
124 'UTPageSummary-' . __CLASS__
,
125 EDIT_NEW | EDIT_SUPPRESS_RC
,
131 return $this->testPage
;
135 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
137 private function getLoadBalancerMock( array $server ) {
138 $lb = $this->getMockBuilder( LoadBalancer
::class )
139 ->setMethods( [ 'reallyOpenConnection' ] )
140 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
143 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
144 function ( array $server, $dbNameOverride ) {
145 return $this->getDatabaseMock( $server );
153 * @return Database|PHPUnit_Framework_MockObject_MockObject
155 private function getDatabaseMock( array $params ) {
156 $db = $this->getMockBuilder( DatabaseSqlite
::class )
157 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
158 ->setConstructorArgs( [ $params ] )
161 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
162 $db->method( 'isOpen' )->willReturn( true );
167 public function provideDomainCheck() {
168 yield
[ false, 'test', '' ];
169 yield
[ 'test', 'test', '' ];
171 yield
[ false, 'test', 'foo_' ];
172 yield
[ 'test-foo_', 'test', 'foo_' ];
174 yield
[ false, 'dash-test', '' ];
175 yield
[ 'dash-test', 'dash-test', '' ];
177 yield
[ false, 'underscore_test', 'foo_' ];
178 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
182 * @dataProvider provideDomainCheck
183 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
185 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
188 'wgDBname' => $dbName,
189 'wgDBprefix' => $dbPrefix,
193 $loadBalancer = $this->getLoadBalancerMock(
196 'dbDirectory' => '*dummy*',
198 'password' => 'test',
206 'trxProfiler' => new TransactionProfiler(),
207 'connLogger' => new \Psr\Log\
NullLogger(),
208 'queryLogger' => new \Psr\Log\
NullLogger(),
209 'errorLogger' => function () {
211 'deprecationLogger' => function () {
215 'tablePrefix' => $dbPrefix,
218 $db = $loadBalancer->getConnection( DB_REPLICA
);
220 /** @var SqlBlobStore $blobStore */
221 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
222 ->disableOriginalConstructor()
225 $store = new RevisionStore(
228 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
229 MediaWikiServices
::getInstance()->getCommentStore(),
230 MediaWikiServices
::getInstance()->getContentModelStore(),
231 MediaWikiServices
::getInstance()->getSlotRoleStore(),
232 $this->getMcrMigrationStage(),
233 MediaWikiServices
::getInstance()->getActorMigration(),
237 $count = $store->countRevisionsByPageId( $db, 0 );
239 // Dummy check to make PhpUnit happy. We are really only interested in
240 // countRevisionsByPageId not failing due to the DB domain check.
241 $this->assertSame( 0, $count );
244 protected function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
245 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
246 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
247 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
248 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
251 protected function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
253 $r1->getPageAsLinkTarget()->getNamespace(),
254 $r2->getPageAsLinkTarget()->getNamespace()
258 $r1->getPageAsLinkTarget()->getText(),
259 $r2->getPageAsLinkTarget()->getText()
262 if ( $r1->getParentId() ) {
263 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
266 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
267 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
268 $this->assertEquals( $r1->getComment(), $r2->getComment() );
269 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
270 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
271 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
272 $this->assertEquals( $r1->getSize(), $r2->getSize() );
273 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
274 $this->assertArrayEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
275 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
276 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
277 foreach ( $r1->getSlotRoles() as $role ) {
278 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
279 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
282 RevisionRecord
::DELETED_TEXT
,
283 RevisionRecord
::DELETED_COMMENT
,
284 RevisionRecord
::DELETED_USER
,
285 RevisionRecord
::DELETED_RESTRICTED
,
287 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
291 protected function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
292 $this->assertSame( $s1->getRole(), $s2->getRole() );
293 $this->assertSame( $s1->getModel(), $s2->getModel() );
294 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
295 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
296 $this->assertSame( $s1->getSize(), $s2->getSize() );
297 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
299 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
300 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
303 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
304 $this->assertTrue( $r->hasSlot( 'main' ) );
305 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( 'main' ) );
306 $this->assertInstanceOf( Content
::class, $r->getContent( 'main' ) );
308 foreach ( $r->getSlotRoles() as $role ) {
309 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
313 protected function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
314 $this->assertTrue( $slot->hasAddress() );
315 $this->assertSame( $r->getId(), $slot->getRevision() );
317 $this->assertInstanceOf( Content
::class, $slot->getContent() );
321 * @param mixed[] $details
323 * @return RevisionRecord
325 private function getRevisionRecordFromDetailsArray( $details = [] ) {
326 // Convert some values that can't be provided by dataProviders
327 if ( isset( $details['user'] ) && $details['user'] === true ) {
328 $details['user'] = $this->getTestUser()->getUser();
330 if ( isset( $details['page'] ) && $details['page'] === true ) {
331 $details['page'] = $this->getTestPage()->getId();
333 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
334 $details['parent'] = $this->getTestPage()->getLatest();
337 // Create the RevisionRecord with any available data
338 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
339 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
340 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
341 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
342 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
343 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
344 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
345 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
346 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
347 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
348 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
349 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
351 if ( isset( $details['content'] ) ) {
352 foreach ( $details['content'] as $role => $content ) {
353 $rev->setContent( $role, $content );
360 public function provideInsertRevisionOn_successes() {
361 yield
'Bare minimum revision insertion' => [
363 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
365 'comment' => $this->getRandomCommentStoreComment(),
366 'timestamp' => '20171117010101',
370 yield
'Detailed revision insertion' => [
372 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
375 'comment' => $this->getRandomCommentStoreComment(),
376 'timestamp' => '20171117010101',
379 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
384 protected function getRandomCommentStoreComment() {
385 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
389 * @dataProvider provideInsertRevisionOn_successes
390 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
391 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
392 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
394 public function testInsertRevisionOn_successes(
395 array $revDetails = []
397 $title = $this->getTestPageTitle();
398 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
400 $this->overrideMwServices();
401 $store = MediaWikiServices
::getInstance()->getRevisionStore();
402 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
404 // is the new revision correct?
405 $this->assertRevisionCompleteness( $return );
406 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
407 $this->assertRevisionRecordsEqual( $rev, $return );
409 // can we load it from the store?
410 $loaded = $store->getRevisionById( $return->getId() );
411 $this->assertRevisionCompleteness( $loaded );
412 $this->assertRevisionRecordsEqual( $return, $loaded );
414 // can we find it directly in the database?
415 $this->assertRevisionExistsInDatabase( $return );
418 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
419 $row = $this->revisionToRow( new Revision( $rev ), [] );
421 // unset nulled fields
422 unset( $row->rev_content_model
);
423 unset( $row->rev_content_format
);
426 unset( $row->rev_comment_text
);
427 unset( $row->rev_comment_data
);
428 unset( $row->rev_comment_cid
);
429 unset( $row->rev_comment_id
);
431 $store = MediaWikiServices
::getInstance()->getRevisionStore();
432 $queryInfo = $store->getQueryInfo( [ 'user' ] );
434 $row = get_object_vars( $row );
436 $queryInfo['tables'],
438 [ 'rev_id' => $rev->getId() ],
439 [ array_values( $row ) ],
446 * @param SlotRecord $a
447 * @param SlotRecord $b
449 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
450 // Assert that the same blob address has been used.
451 $this->assertSame( $a->getAddress(), $b->getAddress() );
455 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
457 public function testInsertRevisionOn_blobAddressExists() {
458 $title = $this->getTestPageTitle();
460 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
462 'comment' => $this->getRandomCommentStoreComment(),
463 'timestamp' => '20171117010101',
467 $this->overrideMwServices();
468 $store = MediaWikiServices
::getInstance()->getRevisionStore();
470 // Insert the first revision
471 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
472 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
473 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
474 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
476 // Insert a second revision inheriting the same blob address
477 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
478 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
479 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
480 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
481 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
483 $firstMainSlot = $firstReturn->getSlot( 'main' );
484 $secondMainSlot = $secondReturn->getSlot( 'main' );
486 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
488 // And that different revisions have been created.
489 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
491 // Make sure the slot rows reference the correct revision
492 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
493 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
496 public function provideInsertRevisionOn_failures() {
499 'comment' => $this->getRandomCommentStoreComment(),
500 'timestamp' => '20171117010101',
503 new InvalidArgumentException( 'main slot must be provided' )
505 yield
'no main slot' => [
507 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
508 'comment' => $this->getRandomCommentStoreComment(),
509 'timestamp' => '20171117010101',
512 new InvalidArgumentException( 'main slot must be provided' )
514 yield
'no timestamp' => [
516 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
517 'comment' => $this->getRandomCommentStoreComment(),
520 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
522 yield
'no comment' => [
524 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
525 'timestamp' => '20171117010101',
528 new IncompleteRevisionException( 'comment must not be NULL!' )
532 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
533 'comment' => $this->getRandomCommentStoreComment(),
534 'timestamp' => '20171117010101',
536 new IncompleteRevisionException( 'user must not be NULL!' )
541 * @dataProvider provideInsertRevisionOn_failures
542 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
544 public function testInsertRevisionOn_failures(
545 array $revDetails = [],
548 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
550 $store = MediaWikiServices
::getInstance()->getRevisionStore();
552 $this->setExpectedException(
553 get_class( $exception ),
554 $exception->getMessage(),
555 $exception->getCode()
557 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
560 public function provideNewNullRevision() {
562 Title
::newFromText( 'UTPage_notAutoCreated' ),
563 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
564 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
568 Title
::newFromText( 'UTPage_notAutoCreated' ),
569 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
570 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
576 * @dataProvider provideNewNullRevision
577 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
578 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
580 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
581 $this->overrideMwServices();
583 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
584 $page = WikiPage
::factory( $title );
586 if ( !$page->exists() ) {
587 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
590 $revDetails['page'] = $page->getId();
591 $revDetails['timestamp'] = wfTimestampNow();
592 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
593 $revDetails['user'] = $user;
595 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
596 $store = MediaWikiServices
::getInstance()->getRevisionStore();
598 $dbw = wfGetDB( DB_MASTER
);
599 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
600 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
602 $record = $store->newNullRevision(
603 wfGetDB( DB_MASTER
),
610 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
611 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
612 $this->assertEquals( $comment, $record->getComment() );
613 $this->assertEquals( $minor, $record->isMinor() );
614 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
615 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
617 $this->assertArrayEquals(
618 $baseRev->getSlotRoles(),
619 $record->getSlotRoles()
622 foreach ( $baseRev->getSlotRoles() as $role ) {
623 $parentSlot = $baseRev->getSlot( $role );
624 $slot = $record->getSlot( $role );
626 $this->assertTrue( $slot->isInherited(), 'isInherited' );
627 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
628 $this->assertSameSlotContent( $parentSlot, $slot );
633 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
635 public function testNewNullRevision_nonExistingTitle() {
636 $store = MediaWikiServices
::getInstance()->getRevisionStore();
637 $record = $store->newNullRevision(
638 wfGetDB( DB_MASTER
),
639 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
640 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
642 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
644 $this->assertNull( $record );
648 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
650 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
651 $page = $this->getTestPage();
652 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
653 /** @var Revision $rev */
654 $rev = $status->value
['revision'];
656 $store = MediaWikiServices
::getInstance()->getRevisionStore();
657 $revisionRecord = $store->getRevisionById( $rev->getId() );
658 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
660 $this->assertGreaterThan( 0, $result );
662 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
668 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
670 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
671 // This assumes that sysops are auto patrolled
672 $sysop = $this->getTestSysop()->getUser();
673 $page = $this->getTestPage();
674 $status = $page->doEditContent(
675 new WikitextContent( __METHOD__
),
681 /** @var Revision $rev */
682 $rev = $status->value
['revision'];
684 $store = MediaWikiServices
::getInstance()->getRevisionStore();
685 $revisionRecord = $store->getRevisionById( $rev->getId() );
686 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
688 $this->assertSame( 0, $result );
692 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
694 public function testGetRecentChange() {
695 $page = $this->getTestPage();
696 $content = new WikitextContent( __METHOD__
);
697 $status = $page->doEditContent( $content, __METHOD__
);
698 /** @var Revision $rev */
699 $rev = $status->value
['revision'];
701 $store = MediaWikiServices
::getInstance()->getRevisionStore();
702 $revRecord = $store->getRevisionById( $rev->getId() );
703 $recentChange = $store->getRecentChange( $revRecord );
705 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
706 $this->assertEquals( $rev->getRecentChange(), $recentChange );
710 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
712 public function testGetRevisionById() {
713 $page = $this->getTestPage();
714 $content = new WikitextContent( __METHOD__
);
715 $status = $page->doEditContent( $content, __METHOD__
);
716 /** @var Revision $rev */
717 $rev = $status->value
['revision'];
719 $store = MediaWikiServices
::getInstance()->getRevisionStore();
720 $revRecord = $store->getRevisionById( $rev->getId() );
722 $this->assertSame( $rev->getId(), $revRecord->getId() );
723 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
724 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
728 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
730 public function testGetRevisionByTitle() {
731 $page = $this->getTestPage();
732 $content = new WikitextContent( __METHOD__
);
733 $status = $page->doEditContent( $content, __METHOD__
);
734 /** @var Revision $rev */
735 $rev = $status->value
['revision'];
737 $store = MediaWikiServices
::getInstance()->getRevisionStore();
738 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
740 $this->assertSame( $rev->getId(), $revRecord->getId() );
741 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
742 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
746 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
748 public function testGetRevisionByPageId() {
749 $page = $this->getTestPage();
750 $content = new WikitextContent( __METHOD__
);
751 $status = $page->doEditContent( $content, __METHOD__
);
752 /** @var Revision $rev */
753 $rev = $status->value
['revision'];
755 $store = MediaWikiServices
::getInstance()->getRevisionStore();
756 $revRecord = $store->getRevisionByPageId( $page->getId() );
758 $this->assertSame( $rev->getId(), $revRecord->getId() );
759 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
760 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
764 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
766 public function testGetRevisionByTimestamp() {
767 // Make sure there is 1 second between the last revision and the rev we create...
768 // Otherwise we might not get the correct revision and the test may fail...
770 $page = $this->getTestPage();
772 $content = new WikitextContent( __METHOD__
);
773 $status = $page->doEditContent( $content, __METHOD__
);
774 /** @var Revision $rev */
775 $rev = $status->value
['revision'];
777 $store = MediaWikiServices
::getInstance()->getRevisionStore();
778 $revRecord = $store->getRevisionByTimestamp(
783 $this->assertSame( $rev->getId(), $revRecord->getId() );
784 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
785 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
788 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
789 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
790 $page = WikiPage
::factory( $rev->getTitle() );
793 'rev_id' => (string)$rev->getId(),
794 'rev_page' => (string)$rev->getPage(),
795 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
796 'rev_user_text' => (string)$rev->getUserText(),
797 'rev_user' => (string)$rev->getUser(),
798 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
799 'rev_deleted' => (string)$rev->getVisibility(),
800 'rev_len' => (string)$rev->getSize(),
801 'rev_parent_id' => (string)$rev->getParentId(),
802 'rev_sha1' => (string)$rev->getSha1(),
805 if ( in_array( 'page', $options ) ) {
807 'page_namespace' => (string)$page->getTitle()->getNamespace(),
808 'page_title' => $page->getTitle()->getDBkey(),
809 'page_id' => (string)$page->getId(),
810 'page_latest' => (string)$page->getLatest(),
811 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
812 'page_len' => (string)$page->getContent()->getSize(),
816 if ( in_array( 'user', $options ) ) {
818 'user_name' => (string)$rev->getUserText(),
822 if ( in_array( 'comment', $options ) ) {
824 'rev_comment_text' => $rev->getComment(),
825 'rev_comment_data' => null,
826 'rev_comment_cid' => null,
830 if ( $rev->getId() ) {
832 'rev_id' => (string)$rev->getId(),
836 return (object)$fields;
839 protected function assertRevisionRecordMatchesRevision(
841 RevisionRecord
$record
843 $this->assertSame( $rev->getId(), $record->getId() );
844 $this->assertSame( $rev->getPage(), $record->getPageId() );
845 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
846 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
847 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
848 $this->assertSame( $rev->isMinor(), $record->isMinor() );
849 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
850 $this->assertSame( $rev->getSize(), $record->getSize() );
852 * @note As of MW 1.31, the database schema allows the parent ID to be
853 * NULL to indicate that it is unknown.
855 $expectedParent = $rev->getParentId();
856 if ( $expectedParent === null ) {
859 $this->assertSame( $expectedParent, $record->getParentId() );
860 $this->assertSame( $rev->getSha1(), $record->getSha1() );
861 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
862 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
863 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
864 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
866 $revRec = $rev->getRevisionRecord();
867 $revMain = $revRec->getSlot( 'main' );
868 $recMain = $record->getSlot( 'main' );
870 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
871 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
872 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
874 if ( $revMain->hasOrigin() ) {
875 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
878 if ( $revMain->hasAddress() ) {
879 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
882 if ( $revMain->hasContentId() ) {
883 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
888 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
889 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
891 public function testNewRevisionFromRow_getQueryInfo() {
892 $page = $this->getTestPage();
893 $text = __METHOD__
. 'a-ä';
894 /** @var Revision $rev */
895 $rev = $page->doEditContent(
896 new WikitextContent( $text ),
898 )->value
['revision'];
900 $store = MediaWikiServices
::getInstance()->getRevisionStore();
901 $info = $store->getQueryInfo();
902 $row = $this->db
->selectRow(
905 [ 'rev_id' => $rev->getId() ],
910 $record = $store->newRevisionFromRow(
915 $this->assertRevisionRecordMatchesRevision( $rev, $record );
916 $this->assertSame( $text, $rev->getContent()->serialize() );
920 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
922 public function testNewRevisionFromRow_anonEdit() {
923 $page = $this->getTestPage();
924 $text = __METHOD__
. 'a-ä';
925 /** @var Revision $rev */
926 $rev = $page->doEditContent(
927 new WikitextContent( $text ),
929 )->value
['revision'];
931 $store = MediaWikiServices
::getInstance()->getRevisionStore();
932 $record = $store->newRevisionFromRow(
933 $this->revisionToRow( $rev ),
937 $this->assertRevisionRecordMatchesRevision( $rev, $record );
938 $this->assertSame( $text, $rev->getContent()->serialize() );
942 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
944 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
945 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
946 $this->overrideMwServices();
947 $page = $this->getTestPage();
948 $text = __METHOD__
. 'a-ä';
949 /** @var Revision $rev */
950 $rev = $page->doEditContent(
951 new WikitextContent( $text ),
953 )->value
['revision'];
955 $store = MediaWikiServices
::getInstance()->getRevisionStore();
956 $record = $store->newRevisionFromRow(
957 $this->revisionToRow( $rev ),
961 $this->assertRevisionRecordMatchesRevision( $rev, $record );
962 $this->assertSame( $text, $rev->getContent()->serialize() );
966 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
968 public function testNewRevisionFromRow_userEdit() {
969 $page = $this->getTestPage();
970 $text = __METHOD__
. 'b-ä';
971 /** @var Revision $rev */
972 $rev = $page->doEditContent(
973 new WikitextContent( $text ),
977 $this->getTestUser()->getUser()
978 )->value
['revision'];
980 $store = MediaWikiServices
::getInstance()->getRevisionStore();
981 $record = $store->newRevisionFromRow(
982 $this->revisionToRow( $rev ),
986 $this->assertRevisionRecordMatchesRevision( $rev, $record );
987 $this->assertSame( $text, $rev->getContent()->serialize() );
991 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
992 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
994 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
995 $store = MediaWikiServices
::getInstance()->getRevisionStore();
996 $title = Title
::newFromText( __METHOD__
);
997 $text = __METHOD__
. '-bä';
998 $page = WikiPage
::factory( $title );
999 /** @var Revision $orig */
1000 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1001 ->value
['revision'];
1002 $page->doDeleteArticle( __METHOD__
);
1004 $db = wfGetDB( DB_MASTER
);
1005 $arQuery = $store->getArchiveQueryInfo();
1007 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1008 __METHOD__
, [], $arQuery['joins']
1010 $this->assertTrue( is_object( $res ), 'query failed' );
1012 $row = $res->fetchObject();
1014 $record = $store->newRevisionFromArchiveRow( $row );
1016 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1017 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1021 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1023 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1024 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1025 $this->overrideMwServices();
1026 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1027 $title = Title
::newFromText( __METHOD__
);
1028 $text = __METHOD__
. '-bä';
1029 $page = WikiPage
::factory( $title );
1030 /** @var Revision $orig */
1031 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1032 ->value
['revision'];
1033 $page->doDeleteArticle( __METHOD__
);
1035 $db = wfGetDB( DB_MASTER
);
1036 $arQuery = $store->getArchiveQueryInfo();
1038 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1039 __METHOD__
, [], $arQuery['joins']
1041 $this->assertTrue( is_object( $res ), 'query failed' );
1043 $row = $res->fetchObject();
1045 $record = $store->newRevisionFromArchiveRow( $row );
1047 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1048 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1052 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
1053 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1055 public function testInsertRevisionOn_archive() {
1056 // This is a round trip test for deletion and undeletion of a
1057 // revision row via the archive table.
1059 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1060 $title = Title
::newFromText( __METHOD__
);
1062 $page = WikiPage
::factory( $title );
1063 /** @var Revision $origRev */
1064 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1065 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1066 ->value
['revision'];
1067 $orig = $origRev->getRevisionRecord();
1068 $page->doDeleteArticle( __METHOD__
);
1070 // re-create page, so we can later load revisions for it
1071 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1073 $db = wfGetDB( DB_MASTER
);
1074 $arQuery = $store->getArchiveQueryInfo();
1075 $row = $db->selectRow(
1076 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1077 __METHOD__
, [], $arQuery['joins']
1080 $this->assertNotFalse( $row, 'query failed' );
1082 $record = $store->newRevisionFromArchiveRow(
1086 [ 'page_id' => $title->getArticleID() ]
1089 $restored = $store->insertRevisionOn( $record, $db );
1091 // is the new revision correct?
1092 $this->assertRevisionCompleteness( $restored );
1093 $this->assertRevisionRecordsEqual( $record, $restored );
1095 // does the new revision use the original slot?
1096 $recMain = $record->getSlot( 'main' );
1097 $restMain = $restored->getSlot( 'main' );
1098 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1099 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1100 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1101 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1103 // can we load it from the store?
1104 $loaded = $store->getRevisionById( $restored->getId() );
1105 $this->assertNotNull( $loaded );
1106 $this->assertRevisionCompleteness( $loaded );
1107 $this->assertRevisionRecordsEqual( $restored, $loaded );
1109 // can we find it directly in the database?
1110 $this->assertRevisionExistsInDatabase( $restored );
1114 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1116 public function testLoadRevisionFromId() {
1117 $title = Title
::newFromText( __METHOD__
);
1118 $page = WikiPage
::factory( $title );
1119 /** @var Revision $rev */
1120 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1121 ->value
['revision'];
1123 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1124 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1125 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1129 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1131 public function testLoadRevisionFromPageId() {
1132 $title = Title
::newFromText( __METHOD__
);
1133 $page = WikiPage
::factory( $title );
1134 /** @var Revision $rev */
1135 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1136 ->value
['revision'];
1138 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1139 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1140 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1144 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1146 public function testLoadRevisionFromTitle() {
1147 $title = Title
::newFromText( __METHOD__
);
1148 $page = WikiPage
::factory( $title );
1149 /** @var Revision $rev */
1150 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1151 ->value
['revision'];
1153 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1154 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1155 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1159 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1161 public function testLoadRevisionFromTimestamp() {
1162 $title = Title
::newFromText( __METHOD__
);
1163 $page = WikiPage
::factory( $title );
1164 /** @var Revision $revOne */
1165 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1166 ->value
['revision'];
1167 // Sleep to ensure different timestamps... )(evil)
1169 /** @var Revision $revTwo */
1170 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1171 ->value
['revision'];
1173 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1175 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1179 $store->loadRevisionFromTimestamp(
1180 wfGetDB( DB_MASTER
),
1182 $revOne->getTimestamp()
1187 $store->loadRevisionFromTimestamp(
1188 wfGetDB( DB_MASTER
),
1190 $revTwo->getTimestamp()
1196 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1198 public function testGetParentLengths() {
1199 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1200 /** @var Revision $revOne */
1201 $revOne = $page->doEditContent(
1202 new WikitextContent( __METHOD__
), __METHOD__
1203 )->value
['revision'];
1204 /** @var Revision $revTwo */
1205 $revTwo = $page->doEditContent(
1206 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1207 )->value
['revision'];
1209 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1212 $revOne->getId() => strlen( __METHOD__
),
1214 $store->listRevisionSizes(
1215 wfGetDB( DB_MASTER
),
1216 [ $revOne->getId() ]
1221 $revOne->getId() => strlen( __METHOD__
),
1222 $revTwo->getId() => strlen( __METHOD__
) +
1,
1224 $store->listRevisionSizes(
1225 wfGetDB( DB_MASTER
),
1226 [ $revOne->getId(), $revTwo->getId() ]
1232 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1234 public function testGetPreviousRevision() {
1235 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1236 /** @var Revision $revOne */
1237 $revOne = $page->doEditContent(
1238 new WikitextContent( __METHOD__
), __METHOD__
1239 )->value
['revision'];
1240 /** @var Revision $revTwo */
1241 $revTwo = $page->doEditContent(
1242 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1243 )->value
['revision'];
1245 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1247 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1251 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1256 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1258 public function testGetNextRevision() {
1259 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1260 /** @var Revision $revOne */
1261 $revOne = $page->doEditContent(
1262 new WikitextContent( __METHOD__
), __METHOD__
1263 )->value
['revision'];
1264 /** @var Revision $revTwo */
1265 $revTwo = $page->doEditContent(
1266 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1267 )->value
['revision'];
1269 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1272 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1275 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1280 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1282 public function testGetTimestampFromId_found() {
1283 $page = $this->getTestPage();
1284 /** @var Revision $rev */
1285 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1286 ->value
['revision'];
1288 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1289 $result = $store->getTimestampFromId(
1294 $this->assertSame( $rev->getTimestamp(), $result );
1298 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1300 public function testGetTimestampFromId_notFound() {
1301 $page = $this->getTestPage();
1302 /** @var Revision $rev */
1303 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1304 ->value
['revision'];
1306 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1307 $result = $store->getTimestampFromId(
1312 $this->assertFalse( $result );
1316 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1318 public function testCountRevisionsByPageId() {
1319 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1320 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1324 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1326 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1329 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1331 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1334 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1339 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1341 public function testCountRevisionsByTitle() {
1342 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1343 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1347 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1349 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1352 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1354 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1357 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1362 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1364 public function testUserWasLastToEdit_false() {
1365 $sysop = $this->getTestSysop()->getUser();
1366 $page = $this->getTestPage();
1367 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1369 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1370 $result = $store->userWasLastToEdit(
1371 wfGetDB( DB_MASTER
),
1376 $this->assertFalse( $result );
1380 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1382 public function testUserWasLastToEdit_true() {
1383 $startTime = wfTimestampNow();
1384 $sysop = $this->getTestSysop()->getUser();
1385 $page = $this->getTestPage();
1386 $page->doEditContent(
1387 new WikitextContent( __METHOD__
),
1394 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1395 $result = $store->userWasLastToEdit(
1396 wfGetDB( DB_MASTER
),
1401 $this->assertTrue( $result );
1405 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1407 public function testGetKnownCurrentRevision() {
1408 $page = $this->getTestPage();
1409 /** @var Revision $rev */
1410 $rev = $page->doEditContent(
1411 new WikitextContent( __METHOD__
. 'b' ),
1415 $this->getTestUser()->getUser()
1416 )->value
['revision'];
1418 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1419 $record = $store->getKnownCurrentRevision(
1424 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1427 public function provideNewMutableRevisionFromArray() {
1428 yield
'Basic array, content object' => [
1432 'timestamp' => '20171017114835',
1433 'user_text' => '111.0.1.2',
1435 'minor_edit' => false,
1439 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1440 'comment' => 'Goat Comment!',
1441 'content' => new WikitextContent( 'Some Content' ),
1444 yield
'Basic array, serialized text' => [
1448 'timestamp' => '20171017114835',
1449 'user_text' => '111.0.1.2',
1451 'minor_edit' => false,
1455 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1456 'comment' => 'Goat Comment!',
1457 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1460 yield
'Basic array, serialized text, utf-8 flags' => [
1464 'timestamp' => '20171017114835',
1465 'user_text' => '111.0.1.2',
1467 'minor_edit' => false,
1471 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1472 'comment' => 'Goat Comment!',
1473 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1477 yield
'Basic array, with title' => [
1479 'title' => Title
::newFromText( 'SomeText' ),
1480 'timestamp' => '20171017114835',
1481 'user_text' => '111.0.1.2',
1483 'minor_edit' => false,
1487 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1488 'comment' => 'Goat Comment!',
1489 'content' => new WikitextContent( 'Some Content' ),
1492 yield
'Basic array, no user field' => [
1496 'timestamp' => '20171017114835',
1497 'user_text' => '111.0.1.3',
1498 'minor_edit' => false,
1502 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1503 'comment' => 'Goat Comment!',
1504 'content' => new WikitextContent( 'Some Content' ),
1510 * @dataProvider provideNewMutableRevisionFromArray
1511 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1513 public function testNewMutableRevisionFromArray( array $array ) {
1514 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1516 // HACK: if $array['page'] is given, make sure that the page exists
1517 if ( isset( $array['page'] ) ) {
1518 $t = Title
::newFromID( $array['page'] );
1519 if ( !$t ||
!$t->exists() ) {
1520 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1521 $info = $this->insertPage( $t );
1522 $array['page'] = $info['id'];
1526 $result = $store->newMutableRevisionFromArray( $array );
1528 if ( isset( $array['id'] ) ) {
1529 $this->assertSame( $array['id'], $result->getId() );
1531 if ( isset( $array['page'] ) ) {
1532 $this->assertSame( $array['page'], $result->getPageId() );
1534 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1535 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1536 if ( isset( $array['user'] ) ) {
1537 $this->assertSame( $array['user'], $result->getUser()->getId() );
1539 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1540 $this->assertSame( $array['deleted'], $result->getVisibility() );
1541 $this->assertSame( $array['len'], $result->getSize() );
1542 $this->assertSame( $array['parent_id'], $result->getParentId() );
1543 $this->assertSame( $array['sha1'], $result->getSha1() );
1544 $this->assertSame( $array['comment'], $result->getComment()->text
);
1545 if ( isset( $array['content'] ) ) {
1546 foreach ( $array['content'] as $role => $content ) {
1548 $result->getContent( $role )->equals( $content )
1551 } elseif ( isset( $array['text'] ) ) {
1552 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1553 } elseif ( isset( $array['content_format'] ) ) {
1555 $array['content_format'],
1556 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1558 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1563 * @dataProvider provideNewMutableRevisionFromArray
1564 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1566 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1567 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1568 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1569 $blobStore = new SqlBlobStore( $lb, $cache );
1570 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1572 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1573 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1574 ->disableOriginalConstructor()
1576 $factory->expects( $this->any() )
1577 ->method( 'newBlobStore' )
1578 ->willReturn( $blobStore );
1579 $factory->expects( $this->any() )
1580 ->method( 'newSqlBlobStore' )
1581 ->willReturn( $blobStore );
1583 $this->setService( 'BlobStoreFactory', $factory );
1585 $this->testNewMutableRevisionFromArray( $array );