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();
81 'wgMultiContentRevisionSchemaMigrationStage',
82 $this->getMcrMigrationStage()
86 'wgContentHandlerUseDB',
87 $this->getContentHandlerUseDB()
90 $this->overrideMwServices();
93 protected function addCoreDBData() {
94 // Blank out. This would fail with a modified schema, and we don't need it.
100 protected function getTestPageTitle() {
101 if ( $this->testPageTitle
) {
102 return $this->testPageTitle
;
105 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
106 return $this->testPageTitle
;
111 protected function getTestPage() {
112 if ( $this->testPage
) {
113 return $this->testPage
;
116 $title = $this->getTestPageTitle();
117 $this->testPage
= WikiPage
::factory( $title );
119 if ( !$this->testPage
->exists() ) {
120 // Make sure we don't write to the live db.
121 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
123 $user = static::getTestSysop()->getUser();
125 $this->testPage
->doEditContent(
126 new WikitextContent( 'UTContent-' . __CLASS__
),
127 'UTPageSummary-' . __CLASS__
,
128 EDIT_NEW | EDIT_SUPPRESS_RC
,
134 return $this->testPage
;
138 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
140 private function getLoadBalancerMock( array $server ) {
141 $lb = $this->getMockBuilder( LoadBalancer
::class )
142 ->setMethods( [ 'reallyOpenConnection' ] )
143 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
146 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
147 function ( array $server, $dbNameOverride ) {
148 return $this->getDatabaseMock( $server );
156 * @return Database|PHPUnit_Framework_MockObject_MockObject
158 private function getDatabaseMock( array $params ) {
159 $db = $this->getMockBuilder( DatabaseSqlite
::class )
160 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
161 ->setConstructorArgs( [ $params ] )
164 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
165 $db->method( 'isOpen' )->willReturn( true );
170 public function provideDomainCheck() {
171 yield
[ false, 'test', '' ];
172 yield
[ 'test', 'test', '' ];
174 yield
[ false, 'test', 'foo_' ];
175 yield
[ 'test-foo_', 'test', 'foo_' ];
177 yield
[ false, 'dash-test', '' ];
178 yield
[ 'dash-test', 'dash-test', '' ];
180 yield
[ false, 'underscore_test', 'foo_' ];
181 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
185 * @dataProvider provideDomainCheck
186 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
188 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
191 'wgDBname' => $dbName,
192 'wgDBprefix' => $dbPrefix,
196 $loadBalancer = $this->getLoadBalancerMock(
199 'dbDirectory' => '*dummy*',
201 'password' => 'test',
209 'trxProfiler' => new TransactionProfiler(),
210 'connLogger' => new \Psr\Log\
NullLogger(),
211 'queryLogger' => new \Psr\Log\
NullLogger(),
212 'errorLogger' => function () {
214 'deprecationLogger' => function () {
218 'tablePrefix' => $dbPrefix,
221 $db = $loadBalancer->getConnection( DB_REPLICA
);
223 /** @var SqlBlobStore $blobStore */
224 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
225 ->disableOriginalConstructor()
228 $store = new RevisionStore(
231 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
232 MediaWikiServices
::getInstance()->getCommentStore(),
233 MediaWikiServices
::getInstance()->getContentModelStore(),
234 MediaWikiServices
::getInstance()->getSlotRoleStore(),
235 $this->getMcrMigrationStage(),
236 MediaWikiServices
::getInstance()->getActorMigration(),
240 $count = $store->countRevisionsByPageId( $db, 0 );
242 // Dummy check to make PhpUnit happy. We are really only interested in
243 // countRevisionsByPageId not failing due to the DB domain check.
244 $this->assertSame( 0, $count );
247 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
248 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
249 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
250 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
251 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
254 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
256 $r1->getPageAsLinkTarget()->getNamespace(),
257 $r2->getPageAsLinkTarget()->getNamespace()
261 $r1->getPageAsLinkTarget()->getText(),
262 $r2->getPageAsLinkTarget()->getText()
265 if ( $r1->getParentId() ) {
266 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
269 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
270 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
271 $this->assertEquals( $r1->getComment(), $r2->getComment() );
272 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
273 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
274 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
275 $this->assertEquals( $r1->getSize(), $r2->getSize() );
276 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
277 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
278 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
279 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
280 foreach ( $r1->getSlotRoles() as $role ) {
281 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
282 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
285 RevisionRecord
::DELETED_TEXT
,
286 RevisionRecord
::DELETED_COMMENT
,
287 RevisionRecord
::DELETED_USER
,
288 RevisionRecord
::DELETED_RESTRICTED
,
290 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
294 private function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
295 $this->assertSame( $s1->getRole(), $s2->getRole() );
296 $this->assertSame( $s1->getModel(), $s2->getModel() );
297 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
298 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
299 $this->assertSame( $s1->getSize(), $s2->getSize() );
300 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
302 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
303 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
306 private function assertRevisionCompleteness( RevisionRecord
$r ) {
307 $this->assertTrue( $r->hasSlot( 'main' ) );
308 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( 'main' ) );
309 $this->assertInstanceOf( Content
::class, $r->getContent( 'main' ) );
311 foreach ( $r->getSlotRoles() as $role ) {
312 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
316 private function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
317 $this->assertTrue( $slot->hasAddress() );
318 $this->assertSame( $r->getId(), $slot->getRevision() );
320 $this->assertInstanceOf( Content
::class, $slot->getContent() );
324 * @param mixed[] $details
326 * @return RevisionRecord
328 private function getRevisionRecordFromDetailsArray( $details = [] ) {
329 // Convert some values that can't be provided by dataProviders
330 if ( isset( $details['user'] ) && $details['user'] === true ) {
331 $details['user'] = $this->getTestUser()->getUser();
333 if ( isset( $details['page'] ) && $details['page'] === true ) {
334 $details['page'] = $this->getTestPage()->getId();
336 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
337 $details['parent'] = $this->getTestPage()->getLatest();
340 // Create the RevisionRecord with any available data
341 $rev = new MutableRevisionRecord( $this->getTestPageTitle() );
342 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
343 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
344 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
345 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
346 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
347 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
348 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
349 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
350 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
351 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
352 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
354 if ( isset( $details['content'] ) ) {
355 foreach ( $details['content'] as $role => $content ) {
356 $rev->setContent( $role, $content );
363 public function provideInsertRevisionOn_successes() {
364 yield
'Bare minimum revision insertion' => [
366 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
368 'comment' => $this->getRandomCommentStoreComment(),
369 'timestamp' => '20171117010101',
373 yield
'Detailed revision insertion' => [
375 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
378 'comment' => $this->getRandomCommentStoreComment(),
379 'timestamp' => '20171117010101',
382 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
387 protected function getRandomCommentStoreComment() {
388 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
392 * @dataProvider provideInsertRevisionOn_successes
393 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
394 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
395 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
397 public function testInsertRevisionOn_successes(
398 array $revDetails = []
400 $title = $this->getTestPageTitle();
401 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
403 $this->overrideMwServices();
404 $store = MediaWikiServices
::getInstance()->getRevisionStore();
405 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
407 // is the new revision correct?
408 $this->assertRevisionCompleteness( $return );
409 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
410 $this->assertRevisionRecordsEqual( $rev, $return );
412 // can we load it from the store?
413 $loaded = $store->getRevisionById( $return->getId() );
414 $this->assertRevisionCompleteness( $loaded );
415 $this->assertRevisionRecordsEqual( $return, $loaded );
417 // can we find it directly in the database?
418 $this->assertRevisionExistsInDatabase( $return );
421 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
422 $row = $this->revisionToRow( new Revision( $rev ), [] );
424 // unset nulled fields
425 unset( $row->rev_content_model
);
426 unset( $row->rev_content_format
);
429 unset( $row->rev_comment_text
);
430 unset( $row->rev_comment_data
);
431 unset( $row->rev_comment_cid
);
432 unset( $row->rev_comment_id
);
434 $store = MediaWikiServices
::getInstance()->getRevisionStore();
435 $queryInfo = $store->getQueryInfo( [ 'user' ] );
437 $row = get_object_vars( $row );
439 $queryInfo['tables'],
441 [ 'rev_id' => $rev->getId() ],
442 [ array_values( $row ) ],
449 * @param SlotRecord $a
450 * @param SlotRecord $b
452 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
453 // Assert that the same blob address has been used.
454 $this->assertSame( $a->getAddress(), $b->getAddress() );
458 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
460 public function testInsertRevisionOn_blobAddressExists() {
461 $title = $this->getTestPageTitle();
463 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
465 'comment' => $this->getRandomCommentStoreComment(),
466 'timestamp' => '20171117010101',
470 $this->overrideMwServices();
471 $store = MediaWikiServices
::getInstance()->getRevisionStore();
473 // Insert the first revision
474 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
475 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
476 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
477 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
479 // Insert a second revision inheriting the same blob address
480 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
481 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
482 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
483 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
484 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
486 $firstMainSlot = $firstReturn->getSlot( 'main' );
487 $secondMainSlot = $secondReturn->getSlot( 'main' );
489 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
491 // And that different revisions have been created.
492 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
494 // Make sure the slot rows reference the correct revision
495 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
496 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
499 public function provideInsertRevisionOn_failures() {
502 'comment' => $this->getRandomCommentStoreComment(),
503 'timestamp' => '20171117010101',
506 new InvalidArgumentException( 'main slot must be provided' )
508 yield
'no main slot' => [
510 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
511 'comment' => $this->getRandomCommentStoreComment(),
512 'timestamp' => '20171117010101',
515 new InvalidArgumentException( 'main slot must be provided' )
517 yield
'no timestamp' => [
519 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
520 'comment' => $this->getRandomCommentStoreComment(),
523 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
525 yield
'no comment' => [
527 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
528 'timestamp' => '20171117010101',
531 new IncompleteRevisionException( 'comment must not be NULL!' )
535 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
536 'comment' => $this->getRandomCommentStoreComment(),
537 'timestamp' => '20171117010101',
539 new IncompleteRevisionException( 'user must not be NULL!' )
544 * @dataProvider provideInsertRevisionOn_failures
545 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
547 public function testInsertRevisionOn_failures(
548 array $revDetails = [],
551 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
553 $store = MediaWikiServices
::getInstance()->getRevisionStore();
555 $this->setExpectedException(
556 get_class( $exception ),
557 $exception->getMessage(),
558 $exception->getCode()
560 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
563 public function provideNewNullRevision() {
565 Title
::newFromText( 'UTPage_notAutoCreated' ),
566 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
567 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
571 Title
::newFromText( 'UTPage_notAutoCreated' ),
572 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
573 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
579 * @dataProvider provideNewNullRevision
580 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
581 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
583 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
584 $this->overrideMwServices();
586 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
587 $page = WikiPage
::factory( $title );
589 if ( !$page->exists() ) {
590 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
593 $revDetails['page'] = $page->getId();
594 $revDetails['timestamp'] = wfTimestampNow();
595 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
596 $revDetails['user'] = $user;
598 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
599 $store = MediaWikiServices
::getInstance()->getRevisionStore();
601 $dbw = wfGetDB( DB_MASTER
);
602 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
603 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
605 $record = $store->newNullRevision(
606 wfGetDB( DB_MASTER
),
613 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
614 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
615 $this->assertEquals( $comment, $record->getComment() );
616 $this->assertEquals( $minor, $record->isMinor() );
617 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
618 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
620 $this->assertArrayEqualsIgnoringIntKeyOrder(
621 $baseRev->getSlotRoles(),
622 $record->getSlotRoles()
625 foreach ( $baseRev->getSlotRoles() as $role ) {
626 $parentSlot = $baseRev->getSlot( $role );
627 $slot = $record->getSlot( $role );
629 $this->assertTrue( $slot->isInherited(), 'isInherited' );
630 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
631 $this->assertSameSlotContent( $parentSlot, $slot );
636 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
638 public function testNewNullRevision_nonExistingTitle() {
639 $store = MediaWikiServices
::getInstance()->getRevisionStore();
640 $record = $store->newNullRevision(
641 wfGetDB( DB_MASTER
),
642 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
643 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
645 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
647 $this->assertNull( $record );
651 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
653 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
654 $page = $this->getTestPage();
655 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
656 /** @var Revision $rev */
657 $rev = $status->value
['revision'];
659 $store = MediaWikiServices
::getInstance()->getRevisionStore();
660 $revisionRecord = $store->getRevisionById( $rev->getId() );
661 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
663 $this->assertGreaterThan( 0, $result );
665 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
671 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
673 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
674 // This assumes that sysops are auto patrolled
675 $sysop = $this->getTestSysop()->getUser();
676 $page = $this->getTestPage();
677 $status = $page->doEditContent(
678 new WikitextContent( __METHOD__
),
684 /** @var Revision $rev */
685 $rev = $status->value
['revision'];
687 $store = MediaWikiServices
::getInstance()->getRevisionStore();
688 $revisionRecord = $store->getRevisionById( $rev->getId() );
689 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
691 $this->assertSame( 0, $result );
695 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
697 public function testGetRecentChange() {
698 $page = $this->getTestPage();
699 $content = new WikitextContent( __METHOD__
);
700 $status = $page->doEditContent( $content, __METHOD__
);
701 /** @var Revision $rev */
702 $rev = $status->value
['revision'];
704 $store = MediaWikiServices
::getInstance()->getRevisionStore();
705 $revRecord = $store->getRevisionById( $rev->getId() );
706 $recentChange = $store->getRecentChange( $revRecord );
708 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
709 $this->assertEquals( $rev->getRecentChange(), $recentChange );
713 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
715 public function testGetRevisionById() {
716 $page = $this->getTestPage();
717 $content = new WikitextContent( __METHOD__
);
718 $status = $page->doEditContent( $content, __METHOD__
);
719 /** @var Revision $rev */
720 $rev = $status->value
['revision'];
722 $store = MediaWikiServices
::getInstance()->getRevisionStore();
723 $revRecord = $store->getRevisionById( $rev->getId() );
725 $this->assertSame( $rev->getId(), $revRecord->getId() );
726 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
727 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
731 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
733 public function testGetRevisionByTitle() {
734 $page = $this->getTestPage();
735 $content = new WikitextContent( __METHOD__
);
736 $status = $page->doEditContent( $content, __METHOD__
);
737 /** @var Revision $rev */
738 $rev = $status->value
['revision'];
740 $store = MediaWikiServices
::getInstance()->getRevisionStore();
741 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
743 $this->assertSame( $rev->getId(), $revRecord->getId() );
744 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
745 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
749 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
751 public function testGetRevisionByPageId() {
752 $page = $this->getTestPage();
753 $content = new WikitextContent( __METHOD__
);
754 $status = $page->doEditContent( $content, __METHOD__
);
755 /** @var Revision $rev */
756 $rev = $status->value
['revision'];
758 $store = MediaWikiServices
::getInstance()->getRevisionStore();
759 $revRecord = $store->getRevisionByPageId( $page->getId() );
761 $this->assertSame( $rev->getId(), $revRecord->getId() );
762 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
763 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
767 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
769 public function testGetRevisionByTimestamp() {
770 // Make sure there is 1 second between the last revision and the rev we create...
771 // Otherwise we might not get the correct revision and the test may fail...
773 $page = $this->getTestPage();
775 $content = new WikitextContent( __METHOD__
);
776 $status = $page->doEditContent( $content, __METHOD__
);
777 /** @var Revision $rev */
778 $rev = $status->value
['revision'];
780 $store = MediaWikiServices
::getInstance()->getRevisionStore();
781 $revRecord = $store->getRevisionByTimestamp(
786 $this->assertSame( $rev->getId(), $revRecord->getId() );
787 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
788 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
791 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
792 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
793 $page = WikiPage
::factory( $rev->getTitle() );
796 'rev_id' => (string)$rev->getId(),
797 'rev_page' => (string)$rev->getPage(),
798 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
799 'rev_user_text' => (string)$rev->getUserText(),
800 'rev_user' => (string)$rev->getUser(),
801 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
802 'rev_deleted' => (string)$rev->getVisibility(),
803 'rev_len' => (string)$rev->getSize(),
804 'rev_parent_id' => (string)$rev->getParentId(),
805 'rev_sha1' => (string)$rev->getSha1(),
808 if ( in_array( 'page', $options ) ) {
810 'page_namespace' => (string)$page->getTitle()->getNamespace(),
811 'page_title' => $page->getTitle()->getDBkey(),
812 'page_id' => (string)$page->getId(),
813 'page_latest' => (string)$page->getLatest(),
814 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
815 'page_len' => (string)$page->getContent()->getSize(),
819 if ( in_array( 'user', $options ) ) {
821 'user_name' => (string)$rev->getUserText(),
825 if ( in_array( 'comment', $options ) ) {
827 'rev_comment_text' => $rev->getComment(),
828 'rev_comment_data' => null,
829 'rev_comment_cid' => null,
833 if ( $rev->getId() ) {
835 'rev_id' => (string)$rev->getId(),
839 return (object)$fields;
842 private function assertRevisionRecordMatchesRevision(
844 RevisionRecord
$record
846 $this->assertSame( $rev->getId(), $record->getId() );
847 $this->assertSame( $rev->getPage(), $record->getPageId() );
848 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
849 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
850 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
851 $this->assertSame( $rev->isMinor(), $record->isMinor() );
852 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
853 $this->assertSame( $rev->getSize(), $record->getSize() );
855 * @note As of MW 1.31, the database schema allows the parent ID to be
856 * NULL to indicate that it is unknown.
858 $expectedParent = $rev->getParentId();
859 if ( $expectedParent === null ) {
862 $this->assertSame( $expectedParent, $record->getParentId() );
863 $this->assertSame( $rev->getSha1(), $record->getSha1() );
864 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
865 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
866 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
867 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
869 $revRec = $rev->getRevisionRecord();
870 $revMain = $revRec->getSlot( 'main' );
871 $recMain = $record->getSlot( 'main' );
873 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
874 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
875 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
877 if ( $revMain->hasOrigin() ) {
878 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
881 if ( $revMain->hasAddress() ) {
882 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
885 if ( $revMain->hasContentId() ) {
886 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
891 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
893 public function testNewRevisionFromRow_anonEdit() {
894 $page = $this->getTestPage();
895 $text = __METHOD__
. 'a-ä';
896 /** @var Revision $rev */
897 $rev = $page->doEditContent(
898 new WikitextContent( $text ),
900 )->value
['revision'];
902 $store = MediaWikiServices
::getInstance()->getRevisionStore();
903 $record = $store->newRevisionFromRow(
904 $this->revisionToRow( $rev ),
908 $this->assertRevisionRecordMatchesRevision( $rev, $record );
909 $this->assertSame( $text, $rev->getContent()->serialize() );
913 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
915 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
916 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
917 $this->overrideMwServices();
918 $page = $this->getTestPage();
919 $text = __METHOD__
. 'a-ä';
920 /** @var Revision $rev */
921 $rev = $page->doEditContent(
922 new WikitextContent( $text ),
924 )->value
['revision'];
926 $store = MediaWikiServices
::getInstance()->getRevisionStore();
927 $record = $store->newRevisionFromRow(
928 $this->revisionToRow( $rev ),
932 $this->assertRevisionRecordMatchesRevision( $rev, $record );
933 $this->assertSame( $text, $rev->getContent()->serialize() );
937 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
939 public function testNewRevisionFromRow_userEdit() {
940 $page = $this->getTestPage();
941 $text = __METHOD__
. 'b-ä';
942 /** @var Revision $rev */
943 $rev = $page->doEditContent(
944 new WikitextContent( $text ),
948 $this->getTestUser()->getUser()
949 )->value
['revision'];
951 $store = MediaWikiServices
::getInstance()->getRevisionStore();
952 $record = $store->newRevisionFromRow(
953 $this->revisionToRow( $rev ),
957 $this->assertRevisionRecordMatchesRevision( $rev, $record );
958 $this->assertSame( $text, $rev->getContent()->serialize() );
962 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
964 public function testNewRevisionFromArchiveRow() {
965 $store = MediaWikiServices
::getInstance()->getRevisionStore();
966 $title = Title
::newFromText( __METHOD__
);
967 $text = __METHOD__
. '-bä';
968 $page = WikiPage
::factory( $title );
969 /** @var Revision $orig */
970 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
972 $page->doDeleteArticle( __METHOD__
);
974 $db = wfGetDB( DB_MASTER
);
975 $arQuery = $store->getArchiveQueryInfo();
977 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
978 __METHOD__
, [], $arQuery['joins']
980 $this->assertTrue( is_object( $res ), 'query failed' );
982 $row = $res->fetchObject();
984 $record = $store->newRevisionFromArchiveRow( $row );
986 $this->assertRevisionRecordMatchesRevision( $orig, $record );
987 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
991 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
993 public function testNewRevisionFromArchiveRow_legacyEncoding() {
994 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
995 $this->overrideMwServices();
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::insertRevisionOn
1024 public function testInsertRevisionOn_archive() {
1025 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1026 $title = Title
::newFromText( __METHOD__
);
1028 $page = WikiPage
::factory( $title );
1029 /** @var Revision $origRev */
1030 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1031 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1032 ->value
['revision'];
1033 $orig = $origRev->getRevisionRecord();
1034 $page->doDeleteArticle( __METHOD__
);
1036 $db = wfGetDB( DB_MASTER
);
1037 $arQuery = $store->getArchiveQueryInfo();
1038 $row = $db->selectRow(
1039 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1040 __METHOD__
, [], $arQuery['joins']
1043 $record = $store->newRevisionFromArchiveRow( $row );
1045 $restored = $store->insertRevisionOn( $record, $db );
1046 $this->assertSame( $orig->getPageId(), $restored->getPageId() );
1047 $this->assertSame( $orig->getId(), $restored->getId() );
1048 $this->assertSame( $orig->getComment()->text
, $restored->getComment()->text
);
1050 $origMain = $orig->getSlot( 'main' );
1051 $restoredMain = $restored->getSlot( 'main' );
1053 $origMain->getOrigin(),
1054 $restoredMain->getOrigin()
1057 if ( $origMain->hasContentId() ) {
1059 $origMain->getContentId(),
1060 $restoredMain->getContentId()
1064 // NOTE: we didn't restore the page row, so we can't use RevisionStore::getRevisionById
1065 $this->assertSelect(
1068 [ 'rev_id' => $orig->getId() ],
1069 [ [ $orig->getId() ] ]
1074 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1076 public function testLoadRevisionFromId() {
1077 $title = Title
::newFromText( __METHOD__
);
1078 $page = WikiPage
::factory( $title );
1079 /** @var Revision $rev */
1080 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1081 ->value
['revision'];
1083 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1084 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1085 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1089 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1091 public function testLoadRevisionFromPageId() {
1092 $title = Title
::newFromText( __METHOD__
);
1093 $page = WikiPage
::factory( $title );
1094 /** @var Revision $rev */
1095 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1096 ->value
['revision'];
1098 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1099 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1100 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1104 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1106 public function testLoadRevisionFromTitle() {
1107 $title = Title
::newFromText( __METHOD__
);
1108 $page = WikiPage
::factory( $title );
1109 /** @var Revision $rev */
1110 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1111 ->value
['revision'];
1113 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1114 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1115 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1119 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1121 public function testLoadRevisionFromTimestamp() {
1122 $title = Title
::newFromText( __METHOD__
);
1123 $page = WikiPage
::factory( $title );
1124 /** @var Revision $revOne */
1125 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1126 ->value
['revision'];
1127 // Sleep to ensure different timestamps... )(evil)
1129 /** @var Revision $revTwo */
1130 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1131 ->value
['revision'];
1133 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1135 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1139 $store->loadRevisionFromTimestamp(
1140 wfGetDB( DB_MASTER
),
1142 $revOne->getTimestamp()
1147 $store->loadRevisionFromTimestamp(
1148 wfGetDB( DB_MASTER
),
1150 $revTwo->getTimestamp()
1156 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1158 public function testGetParentLengths() {
1159 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1160 /** @var Revision $revOne */
1161 $revOne = $page->doEditContent(
1162 new WikitextContent( __METHOD__
), __METHOD__
1163 )->value
['revision'];
1164 /** @var Revision $revTwo */
1165 $revTwo = $page->doEditContent(
1166 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1167 )->value
['revision'];
1169 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1172 $revOne->getId() => strlen( __METHOD__
),
1174 $store->listRevisionSizes(
1175 wfGetDB( DB_MASTER
),
1176 [ $revOne->getId() ]
1181 $revOne->getId() => strlen( __METHOD__
),
1182 $revTwo->getId() => strlen( __METHOD__
) +
1,
1184 $store->listRevisionSizes(
1185 wfGetDB( DB_MASTER
),
1186 [ $revOne->getId(), $revTwo->getId() ]
1192 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1194 public function testGetPreviousRevision() {
1195 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1196 /** @var Revision $revOne */
1197 $revOne = $page->doEditContent(
1198 new WikitextContent( __METHOD__
), __METHOD__
1199 )->value
['revision'];
1200 /** @var Revision $revTwo */
1201 $revTwo = $page->doEditContent(
1202 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1203 )->value
['revision'];
1205 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1207 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1211 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1216 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1218 public function testGetNextRevision() {
1219 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1220 /** @var Revision $revOne */
1221 $revOne = $page->doEditContent(
1222 new WikitextContent( __METHOD__
), __METHOD__
1223 )->value
['revision'];
1224 /** @var Revision $revTwo */
1225 $revTwo = $page->doEditContent(
1226 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1227 )->value
['revision'];
1229 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1232 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1235 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1240 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1242 public function testGetTimestampFromId_found() {
1243 $page = $this->getTestPage();
1244 /** @var Revision $rev */
1245 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1246 ->value
['revision'];
1248 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1249 $result = $store->getTimestampFromId(
1254 $this->assertSame( $rev->getTimestamp(), $result );
1258 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1260 public function testGetTimestampFromId_notFound() {
1261 $page = $this->getTestPage();
1262 /** @var Revision $rev */
1263 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1264 ->value
['revision'];
1266 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1267 $result = $store->getTimestampFromId(
1272 $this->assertFalse( $result );
1276 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1278 public function testCountRevisionsByPageId() {
1279 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1280 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1284 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1286 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1289 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1291 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1294 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1299 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1301 public function testCountRevisionsByTitle() {
1302 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1303 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1307 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1309 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1312 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1314 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1317 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1322 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1324 public function testUserWasLastToEdit_false() {
1325 $sysop = $this->getTestSysop()->getUser();
1326 $page = $this->getTestPage();
1327 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1329 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1330 $result = $store->userWasLastToEdit(
1331 wfGetDB( DB_MASTER
),
1336 $this->assertFalse( $result );
1340 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1342 public function testUserWasLastToEdit_true() {
1343 $startTime = wfTimestampNow();
1344 $sysop = $this->getTestSysop()->getUser();
1345 $page = $this->getTestPage();
1346 $page->doEditContent(
1347 new WikitextContent( __METHOD__
),
1354 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1355 $result = $store->userWasLastToEdit(
1356 wfGetDB( DB_MASTER
),
1361 $this->assertTrue( $result );
1365 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1367 public function testGetKnownCurrentRevision() {
1368 $page = $this->getTestPage();
1369 /** @var Revision $rev */
1370 $rev = $page->doEditContent(
1371 new WikitextContent( __METHOD__
. 'b' ),
1375 $this->getTestUser()->getUser()
1376 )->value
['revision'];
1378 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1379 $record = $store->getKnownCurrentRevision(
1384 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1387 public function provideNewMutableRevisionFromArray() {
1388 yield
'Basic array, content object' => [
1392 'timestamp' => '20171017114835',
1393 'user_text' => '111.0.1.2',
1395 'minor_edit' => false,
1399 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1400 'comment' => 'Goat Comment!',
1401 'content' => new WikitextContent( 'Some Content' ),
1404 yield
'Basic array, serialized text' => [
1408 'timestamp' => '20171017114835',
1409 'user_text' => '111.0.1.2',
1411 'minor_edit' => false,
1415 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1416 'comment' => 'Goat Comment!',
1417 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1420 yield
'Basic array, serialized text, utf-8 flags' => [
1424 'timestamp' => '20171017114835',
1425 'user_text' => '111.0.1.2',
1427 'minor_edit' => false,
1431 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1432 'comment' => 'Goat Comment!',
1433 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1437 yield
'Basic array, with title' => [
1439 'title' => Title
::newFromText( 'SomeText' ),
1440 'timestamp' => '20171017114835',
1441 'user_text' => '111.0.1.2',
1443 'minor_edit' => false,
1447 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1448 'comment' => 'Goat Comment!',
1449 'content' => new WikitextContent( 'Some Content' ),
1452 yield
'Basic array, no user field' => [
1456 'timestamp' => '20171017114835',
1457 'user_text' => '111.0.1.3',
1458 'minor_edit' => false,
1462 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1463 'comment' => 'Goat Comment!',
1464 'content' => new WikitextContent( 'Some Content' ),
1470 * @dataProvider provideNewMutableRevisionFromArray
1471 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1473 public function testNewMutableRevisionFromArray( array $array ) {
1474 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1476 // HACK: if $array['page'] is given, make sure that the page exists
1477 if ( isset( $array['page'] ) ) {
1478 $t = Title
::newFromID( $array['page'] );
1479 if ( !$t ||
!$t->exists() ) {
1480 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1481 $info = $this->insertPage( $t );
1482 $array['page'] = $info['id'];
1486 $result = $store->newMutableRevisionFromArray( $array );
1488 if ( isset( $array['id'] ) ) {
1489 $this->assertSame( $array['id'], $result->getId() );
1491 if ( isset( $array['page'] ) ) {
1492 $this->assertSame( $array['page'], $result->getPageId() );
1494 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1495 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1496 if ( isset( $array['user'] ) ) {
1497 $this->assertSame( $array['user'], $result->getUser()->getId() );
1499 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1500 $this->assertSame( $array['deleted'], $result->getVisibility() );
1501 $this->assertSame( $array['len'], $result->getSize() );
1502 $this->assertSame( $array['parent_id'], $result->getParentId() );
1503 $this->assertSame( $array['sha1'], $result->getSha1() );
1504 $this->assertSame( $array['comment'], $result->getComment()->text
);
1505 if ( isset( $array['content'] ) ) {
1506 foreach ( $array['content'] as $role => $content ) {
1508 $result->getContent( $role )->equals( $content )
1511 } elseif ( isset( $array['text'] ) ) {
1512 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1513 } elseif ( isset( $array['content_format'] ) ) {
1515 $array['content_format'],
1516 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1518 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1523 * @dataProvider provideNewMutableRevisionFromArray
1524 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1526 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1527 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1528 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1529 $blobStore = new SqlBlobStore( $lb, $cache );
1530 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1532 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1533 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1534 ->disableOriginalConstructor()
1536 $factory->expects( $this->any() )
1537 ->method( 'newBlobStore' )
1538 ->willReturn( $blobStore );
1539 $factory->expects( $this->any() )
1540 ->method( 'newSqlBlobStore' )
1541 ->willReturn( $blobStore );
1543 $this->setService( 'BlobStoreFactory', $factory );
1545 $this->testNewMutableRevisionFromArray( $array );
1548 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1559 if ( $returnTextIdField ) {
1560 $fields[] = 'rev_text_id';
1565 protected function getCommentQueryFields() {
1567 'rev_comment_text' => 'rev_comment',
1568 'rev_comment_data' => 'NULL',
1569 'rev_comment_cid' => 'NULL',
1573 protected function getActorQueryFields() {
1575 'rev_user' => 'rev_user',
1576 'rev_user_text' => 'rev_user_text',
1577 'rev_actor' => 'NULL',
1581 protected function getContentHandlerQueryFields() {
1583 'rev_content_format',
1584 'rev_content_model',
1588 abstract public function provideGetQueryInfo();
1591 * @dataProvider provideGetQueryInfo
1592 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1594 public function testGetQueryInfo( $options, $expected ) {
1595 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1597 $queryInfo = $store->getQueryInfo( $options );
1599 $this->assertArrayEqualsIgnoringIntKeyOrder(
1600 $expected['tables'],
1601 $queryInfo['tables']
1603 $this->assertArrayEqualsIgnoringIntKeyOrder(
1604 $expected['fields'],
1605 $queryInfo['fields']
1607 $this->assertArrayEqualsIgnoringIntKeyOrder(
1613 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1627 if ( $returnTextFields ) {
1628 $fields[] = 'ar_text_id';
1633 abstract public function provideGetArchiveQueryInfo();
1636 * @dataProvider provideGetArchiveQueryInfo
1637 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1639 public function testGetArchiveQueryInfo( $expected ) {
1640 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1642 $archiveQueryInfo = $store->getArchiveQueryInfo();
1644 $this->assertArrayEqualsIgnoringIntKeyOrder(
1645 $expected['tables'],
1646 $archiveQueryInfo['tables']
1649 $this->assertArrayEqualsIgnoringIntKeyOrder(
1650 $expected['fields'],
1651 $archiveQueryInfo['fields']
1654 $this->assertArrayEqualsIgnoringIntKeyOrder(
1656 $archiveQueryInfo['joins']
1660 abstract public function provideGetSlotsQueryInfo();
1663 * @dataProvider provideGetSlotsQueryInfo
1664 * @covers \MediaWiki\Storage\RevisionStore::getSlotsQueryInfo
1666 public function testGetSlotsQueryInfo( $options, $expected ) {
1667 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1669 $archiveQueryInfo = $store->getSlotsQueryInfo( $options );
1671 $this->assertArrayEqualsIgnoringIntKeyOrder(
1672 $expected['tables'],
1673 $archiveQueryInfo['tables']
1676 $this->assertArrayEqualsIgnoringIntKeyOrder(
1677 $expected['fields'],
1678 $archiveQueryInfo['fields']
1681 $this->assertArrayEqualsIgnoringIntKeyOrder(
1683 $archiveQueryInfo['joins']
1688 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1691 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1692 * split into a string key array and an int key array before assertions occur.
1694 * @param array $expected
1695 * @param array $actual
1697 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1698 $this->objectAssociativeSort( $expected );
1699 $this->objectAssociativeSort( $actual );
1701 // Separate the int key values from the string key values so that assertion failures are
1702 // easier to understand.
1703 $expectedIntKeyValues = [];
1704 $actualIntKeyValues = [];
1706 // Remove all int keys and re add them at the end after sorting by value
1707 // This will result in all int keys being in the same order with same ints at the end of
1709 foreach ( $expected as $key => $value ) {
1710 if ( is_int( $key ) ) {
1711 unset( $expected[$key] );
1712 $expectedIntKeyValues[] = $value;
1715 foreach ( $actual as $key => $value ) {
1716 if ( is_int( $key ) ) {
1717 unset( $actual[$key] );
1718 $actualIntKeyValues[] = $value;
1722 $this->assertArrayEquals( $expected, $actual, false, true );
1723 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );