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->assertArrayEqualsIgnoringIntKeyOrder( $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 // FIXME: fails under postgres
401 $this->markTestSkippedIfDbType( 'postgres' );
403 $title = $this->getTestPageTitle();
404 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
406 $this->overrideMwServices();
407 $store = MediaWikiServices
::getInstance()->getRevisionStore();
408 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
410 // is the new revision correct?
411 $this->assertRevisionCompleteness( $return );
412 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
413 $this->assertRevisionRecordsEqual( $rev, $return );
415 // can we load it from the store?
416 $loaded = $store->getRevisionById( $return->getId() );
417 $this->assertRevisionCompleteness( $loaded );
418 $this->assertRevisionRecordsEqual( $return, $loaded );
420 // can we find it directly in the database?
421 $this->assertRevisionExistsInDatabase( $return );
424 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
425 $row = $this->revisionToRow( new Revision( $rev ), [] );
427 // unset nulled fields
428 unset( $row->rev_content_model
);
429 unset( $row->rev_content_format
);
432 unset( $row->rev_comment_text
);
433 unset( $row->rev_comment_data
);
434 unset( $row->rev_comment_cid
);
435 unset( $row->rev_comment_id
);
437 $store = MediaWikiServices
::getInstance()->getRevisionStore();
438 $queryInfo = $store->getQueryInfo( [ 'user' ] );
440 $row = get_object_vars( $row );
442 $queryInfo['tables'],
444 [ 'rev_id' => $rev->getId() ],
445 [ array_values( $row ) ],
452 * @param SlotRecord $a
453 * @param SlotRecord $b
455 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
456 // Assert that the same blob address has been used.
457 $this->assertSame( $a->getAddress(), $b->getAddress() );
461 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
463 public function testInsertRevisionOn_blobAddressExists() {
464 $title = $this->getTestPageTitle();
466 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
468 'comment' => $this->getRandomCommentStoreComment(),
469 'timestamp' => '20171117010101',
473 $this->overrideMwServices();
474 $store = MediaWikiServices
::getInstance()->getRevisionStore();
476 // Insert the first revision
477 $revOne = $this->getRevisionRecordFromDetailsArray( $revDetails );
478 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
479 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
480 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
482 // Insert a second revision inheriting the same blob address
483 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
484 $revTwo = $this->getRevisionRecordFromDetailsArray( $revDetails );
485 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
486 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
487 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
489 $firstMainSlot = $firstReturn->getSlot( 'main' );
490 $secondMainSlot = $secondReturn->getSlot( 'main' );
492 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
494 // And that different revisions have been created.
495 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
497 // Make sure the slot rows reference the correct revision
498 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
499 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
502 public function provideInsertRevisionOn_failures() {
505 'comment' => $this->getRandomCommentStoreComment(),
506 'timestamp' => '20171117010101',
509 new InvalidArgumentException( 'main slot must be provided' )
511 yield
'no main slot' => [
513 'slot' => SlotRecord
::newUnsaved( 'aux', new WikitextContent( 'Turkey' ) ),
514 'comment' => $this->getRandomCommentStoreComment(),
515 'timestamp' => '20171117010101',
518 new InvalidArgumentException( 'main slot must be provided' )
520 yield
'no timestamp' => [
522 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
523 'comment' => $this->getRandomCommentStoreComment(),
526 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
528 yield
'no comment' => [
530 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
531 'timestamp' => '20171117010101',
534 new IncompleteRevisionException( 'comment must not be NULL!' )
538 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
539 'comment' => $this->getRandomCommentStoreComment(),
540 'timestamp' => '20171117010101',
542 new IncompleteRevisionException( 'user must not be NULL!' )
547 * @dataProvider provideInsertRevisionOn_failures
548 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
550 public function testInsertRevisionOn_failures(
551 array $revDetails = [],
554 $rev = $this->getRevisionRecordFromDetailsArray( $revDetails );
556 $store = MediaWikiServices
::getInstance()->getRevisionStore();
558 $this->setExpectedException(
559 get_class( $exception ),
560 $exception->getMessage(),
561 $exception->getCode()
563 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
566 public function provideNewNullRevision() {
568 Title
::newFromText( 'UTPage_notAutoCreated' ),
569 [ 'content' => [ 'main' => new WikitextContent( 'Flubber1' ) ] ],
570 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
574 Title
::newFromText( 'UTPage_notAutoCreated' ),
575 [ 'content' => [ 'main' => new WikitextContent( 'Flubber2' ) ] ],
576 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
582 * @dataProvider provideNewNullRevision
583 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
584 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
586 public function testNewNullRevision( Title
$title, $revDetails, $comment, $minor = false ) {
587 $this->overrideMwServices();
589 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
590 $page = WikiPage
::factory( $title );
592 if ( !$page->exists() ) {
593 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
, EDIT_NEW
);
596 $revDetails['page'] = $page->getId();
597 $revDetails['timestamp'] = wfTimestampNow();
598 $revDetails['comment'] = CommentStoreComment
::newUnsavedComment( 'Base' );
599 $revDetails['user'] = $user;
601 $baseRev = $this->getRevisionRecordFromDetailsArray( $revDetails );
602 $store = MediaWikiServices
::getInstance()->getRevisionStore();
604 $dbw = wfGetDB( DB_MASTER
);
605 $baseRev = $store->insertRevisionOn( $baseRev, $dbw );
606 $page->updateRevisionOn( $dbw, new Revision( $baseRev ), $page->getLatest() );
608 $record = $store->newNullRevision(
609 wfGetDB( DB_MASTER
),
616 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
617 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
618 $this->assertEquals( $comment, $record->getComment() );
619 $this->assertEquals( $minor, $record->isMinor() );
620 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
621 $this->assertEquals( $baseRev->getId(), $record->getParentId() );
623 $this->assertArrayEqualsIgnoringIntKeyOrder(
624 $baseRev->getSlotRoles(),
625 $record->getSlotRoles()
628 foreach ( $baseRev->getSlotRoles() as $role ) {
629 $parentSlot = $baseRev->getSlot( $role );
630 $slot = $record->getSlot( $role );
632 $this->assertTrue( $slot->isInherited(), 'isInherited' );
633 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
634 $this->assertSameSlotContent( $parentSlot, $slot );
639 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
641 public function testNewNullRevision_nonExistingTitle() {
642 $store = MediaWikiServices
::getInstance()->getRevisionStore();
643 $record = $store->newNullRevision(
644 wfGetDB( DB_MASTER
),
645 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
646 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
648 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
650 $this->assertNull( $record );
654 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
656 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
657 $page = $this->getTestPage();
658 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
659 /** @var Revision $rev */
660 $rev = $status->value
['revision'];
662 $store = MediaWikiServices
::getInstance()->getRevisionStore();
663 $revisionRecord = $store->getRevisionById( $rev->getId() );
664 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
666 $this->assertGreaterThan( 0, $result );
668 $store->getRecentChange( $revisionRecord )->getAttribute( 'rc_id' ),
674 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
676 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
677 // This assumes that sysops are auto patrolled
678 $sysop = $this->getTestSysop()->getUser();
679 $page = $this->getTestPage();
680 $status = $page->doEditContent(
681 new WikitextContent( __METHOD__
),
687 /** @var Revision $rev */
688 $rev = $status->value
['revision'];
690 $store = MediaWikiServices
::getInstance()->getRevisionStore();
691 $revisionRecord = $store->getRevisionById( $rev->getId() );
692 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
694 $this->assertSame( 0, $result );
698 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
700 public function testGetRecentChange() {
701 $page = $this->getTestPage();
702 $content = new WikitextContent( __METHOD__
);
703 $status = $page->doEditContent( $content, __METHOD__
);
704 /** @var Revision $rev */
705 $rev = $status->value
['revision'];
707 $store = MediaWikiServices
::getInstance()->getRevisionStore();
708 $revRecord = $store->getRevisionById( $rev->getId() );
709 $recentChange = $store->getRecentChange( $revRecord );
711 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
712 $this->assertEquals( $rev->getRecentChange(), $recentChange );
716 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
718 public function testGetRevisionById() {
719 $page = $this->getTestPage();
720 $content = new WikitextContent( __METHOD__
);
721 $status = $page->doEditContent( $content, __METHOD__
);
722 /** @var Revision $rev */
723 $rev = $status->value
['revision'];
725 $store = MediaWikiServices
::getInstance()->getRevisionStore();
726 $revRecord = $store->getRevisionById( $rev->getId() );
728 $this->assertSame( $rev->getId(), $revRecord->getId() );
729 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
730 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
734 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
736 public function testGetRevisionByTitle() {
737 $page = $this->getTestPage();
738 $content = new WikitextContent( __METHOD__
);
739 $status = $page->doEditContent( $content, __METHOD__
);
740 /** @var Revision $rev */
741 $rev = $status->value
['revision'];
743 $store = MediaWikiServices
::getInstance()->getRevisionStore();
744 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
746 $this->assertSame( $rev->getId(), $revRecord->getId() );
747 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
748 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
752 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
754 public function testGetRevisionByPageId() {
755 $page = $this->getTestPage();
756 $content = new WikitextContent( __METHOD__
);
757 $status = $page->doEditContent( $content, __METHOD__
);
758 /** @var Revision $rev */
759 $rev = $status->value
['revision'];
761 $store = MediaWikiServices
::getInstance()->getRevisionStore();
762 $revRecord = $store->getRevisionByPageId( $page->getId() );
764 $this->assertSame( $rev->getId(), $revRecord->getId() );
765 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
766 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
770 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
772 public function testGetRevisionByTimestamp() {
773 // Make sure there is 1 second between the last revision and the rev we create...
774 // Otherwise we might not get the correct revision and the test may fail...
776 $page = $this->getTestPage();
778 $content = new WikitextContent( __METHOD__
);
779 $status = $page->doEditContent( $content, __METHOD__
);
780 /** @var Revision $rev */
781 $rev = $status->value
['revision'];
783 $store = MediaWikiServices
::getInstance()->getRevisionStore();
784 $revRecord = $store->getRevisionByTimestamp(
789 $this->assertSame( $rev->getId(), $revRecord->getId() );
790 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
791 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
794 protected function revisionToRow( Revision
$rev, $options = [ 'page', 'user', 'comment' ] ) {
795 // XXX: the WikiPage object loads another RevisionRecord from the database. Not great.
796 $page = WikiPage
::factory( $rev->getTitle() );
799 'rev_id' => (string)$rev->getId(),
800 'rev_page' => (string)$rev->getPage(),
801 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
802 'rev_user_text' => (string)$rev->getUserText(),
803 'rev_user' => (string)$rev->getUser(),
804 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
805 'rev_deleted' => (string)$rev->getVisibility(),
806 'rev_len' => (string)$rev->getSize(),
807 'rev_parent_id' => (string)$rev->getParentId(),
808 'rev_sha1' => (string)$rev->getSha1(),
811 if ( in_array( 'page', $options ) ) {
813 'page_namespace' => (string)$page->getTitle()->getNamespace(),
814 'page_title' => $page->getTitle()->getDBkey(),
815 'page_id' => (string)$page->getId(),
816 'page_latest' => (string)$page->getLatest(),
817 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
818 'page_len' => (string)$page->getContent()->getSize(),
822 if ( in_array( 'user', $options ) ) {
824 'user_name' => (string)$rev->getUserText(),
828 if ( in_array( 'comment', $options ) ) {
830 'rev_comment_text' => $rev->getComment(),
831 'rev_comment_data' => null,
832 'rev_comment_cid' => null,
836 if ( $rev->getId() ) {
838 'rev_id' => (string)$rev->getId(),
842 return (object)$fields;
845 private function assertRevisionRecordMatchesRevision(
847 RevisionRecord
$record
849 $this->assertSame( $rev->getId(), $record->getId() );
850 $this->assertSame( $rev->getPage(), $record->getPageId() );
851 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
852 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
853 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
854 $this->assertSame( $rev->isMinor(), $record->isMinor() );
855 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
856 $this->assertSame( $rev->getSize(), $record->getSize() );
858 * @note As of MW 1.31, the database schema allows the parent ID to be
859 * NULL to indicate that it is unknown.
861 $expectedParent = $rev->getParentId();
862 if ( $expectedParent === null ) {
865 $this->assertSame( $expectedParent, $record->getParentId() );
866 $this->assertSame( $rev->getSha1(), $record->getSha1() );
867 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
868 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
869 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
870 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
872 $revRec = $rev->getRevisionRecord();
873 $revMain = $revRec->getSlot( 'main' );
874 $recMain = $record->getSlot( 'main' );
876 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
877 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
878 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
880 if ( $revMain->hasOrigin() ) {
881 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
884 if ( $revMain->hasAddress() ) {
885 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
888 if ( $revMain->hasContentId() ) {
889 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
894 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
896 public function testNewRevisionFromRow_anonEdit() {
897 $page = $this->getTestPage();
898 $text = __METHOD__
. 'a-ä';
899 /** @var Revision $rev */
900 $rev = $page->doEditContent(
901 new WikitextContent( $text ),
903 )->value
['revision'];
905 $store = MediaWikiServices
::getInstance()->getRevisionStore();
906 $record = $store->newRevisionFromRow(
907 $this->revisionToRow( $rev ),
911 $this->assertRevisionRecordMatchesRevision( $rev, $record );
912 $this->assertSame( $text, $rev->getContent()->serialize() );
916 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
918 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
919 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
920 $this->overrideMwServices();
921 $page = $this->getTestPage();
922 $text = __METHOD__
. 'a-ä';
923 /** @var Revision $rev */
924 $rev = $page->doEditContent(
925 new WikitextContent( $text ),
927 )->value
['revision'];
929 $store = MediaWikiServices
::getInstance()->getRevisionStore();
930 $record = $store->newRevisionFromRow(
931 $this->revisionToRow( $rev ),
935 $this->assertRevisionRecordMatchesRevision( $rev, $record );
936 $this->assertSame( $text, $rev->getContent()->serialize() );
940 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
942 public function testNewRevisionFromRow_userEdit() {
943 $page = $this->getTestPage();
944 $text = __METHOD__
. 'b-ä';
945 /** @var Revision $rev */
946 $rev = $page->doEditContent(
947 new WikitextContent( $text ),
951 $this->getTestUser()->getUser()
952 )->value
['revision'];
954 $store = MediaWikiServices
::getInstance()->getRevisionStore();
955 $record = $store->newRevisionFromRow(
956 $this->revisionToRow( $rev ),
960 $this->assertRevisionRecordMatchesRevision( $rev, $record );
961 $this->assertSame( $text, $rev->getContent()->serialize() );
965 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
967 public function testNewRevisionFromArchiveRow() {
968 $store = MediaWikiServices
::getInstance()->getRevisionStore();
969 $title = Title
::newFromText( __METHOD__
);
970 $text = __METHOD__
. '-bä';
971 $page = WikiPage
::factory( $title );
972 /** @var Revision $orig */
973 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
975 $page->doDeleteArticle( __METHOD__
);
977 $db = wfGetDB( DB_MASTER
);
978 $arQuery = $store->getArchiveQueryInfo();
980 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
981 __METHOD__
, [], $arQuery['joins']
983 $this->assertTrue( is_object( $res ), 'query failed' );
985 $row = $res->fetchObject();
987 $record = $store->newRevisionFromArchiveRow( $row );
989 $this->assertRevisionRecordMatchesRevision( $orig, $record );
990 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
994 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
996 public function testNewRevisionFromArchiveRow_legacyEncoding() {
997 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
998 $this->overrideMwServices();
999 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1000 $title = Title
::newFromText( __METHOD__
);
1001 $text = __METHOD__
. '-bä';
1002 $page = WikiPage
::factory( $title );
1003 /** @var Revision $orig */
1004 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1005 ->value
['revision'];
1006 $page->doDeleteArticle( __METHOD__
);
1008 $db = wfGetDB( DB_MASTER
);
1009 $arQuery = $store->getArchiveQueryInfo();
1011 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1012 __METHOD__
, [], $arQuery['joins']
1014 $this->assertTrue( is_object( $res ), 'query failed' );
1016 $row = $res->fetchObject();
1018 $record = $store->newRevisionFromArchiveRow( $row );
1020 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1021 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
1025 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
1027 public function testInsertRevisionOn_archive() {
1028 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1029 $title = Title
::newFromText( __METHOD__
);
1031 $page = WikiPage
::factory( $title );
1032 /** @var Revision $origRev */
1033 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1034 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1035 ->value
['revision'];
1036 $orig = $origRev->getRevisionRecord();
1037 $page->doDeleteArticle( __METHOD__
);
1039 $db = wfGetDB( DB_MASTER
);
1040 $arQuery = $store->getArchiveQueryInfo();
1041 $row = $db->selectRow(
1042 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1043 __METHOD__
, [], $arQuery['joins']
1046 $record = $store->newRevisionFromArchiveRow( $row );
1048 $restored = $store->insertRevisionOn( $record, $db );
1049 $this->assertSame( $orig->getPageId(), $restored->getPageId() );
1050 $this->assertSame( $orig->getId(), $restored->getId() );
1051 $this->assertSame( $orig->getComment()->text
, $restored->getComment()->text
);
1053 $origMain = $orig->getSlot( 'main' );
1054 $restoredMain = $restored->getSlot( 'main' );
1056 $origMain->getOrigin(),
1057 $restoredMain->getOrigin()
1060 if ( $origMain->hasContentId() ) {
1062 $origMain->getContentId(),
1063 $restoredMain->getContentId()
1067 // NOTE: we didn't restore the page row, so we can't use RevisionStore::getRevisionById
1068 $this->assertSelect(
1071 [ 'rev_id' => $orig->getId() ],
1072 [ [ $orig->getId() ] ]
1077 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
1079 public function testLoadRevisionFromId() {
1080 $title = Title
::newFromText( __METHOD__
);
1081 $page = WikiPage
::factory( $title );
1082 /** @var Revision $rev */
1083 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1084 ->value
['revision'];
1086 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1087 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1088 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1092 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
1094 public function testLoadRevisionFromPageId() {
1095 $title = Title
::newFromText( __METHOD__
);
1096 $page = WikiPage
::factory( $title );
1097 /** @var Revision $rev */
1098 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1099 ->value
['revision'];
1101 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1102 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1103 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1107 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
1109 public function testLoadRevisionFromTitle() {
1110 $title = Title
::newFromText( __METHOD__
);
1111 $page = WikiPage
::factory( $title );
1112 /** @var Revision $rev */
1113 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1114 ->value
['revision'];
1116 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1117 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1118 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1122 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
1124 public function testLoadRevisionFromTimestamp() {
1125 $title = Title
::newFromText( __METHOD__
);
1126 $page = WikiPage
::factory( $title );
1127 /** @var Revision $revOne */
1128 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1129 ->value
['revision'];
1130 // Sleep to ensure different timestamps... )(evil)
1132 /** @var Revision $revTwo */
1133 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1134 ->value
['revision'];
1136 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1138 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1142 $store->loadRevisionFromTimestamp(
1143 wfGetDB( DB_MASTER
),
1145 $revOne->getTimestamp()
1150 $store->loadRevisionFromTimestamp(
1151 wfGetDB( DB_MASTER
),
1153 $revTwo->getTimestamp()
1159 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1161 public function testGetParentLengths() {
1162 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1163 /** @var Revision $revOne */
1164 $revOne = $page->doEditContent(
1165 new WikitextContent( __METHOD__
), __METHOD__
1166 )->value
['revision'];
1167 /** @var Revision $revTwo */
1168 $revTwo = $page->doEditContent(
1169 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1170 )->value
['revision'];
1172 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1175 $revOne->getId() => strlen( __METHOD__
),
1177 $store->listRevisionSizes(
1178 wfGetDB( DB_MASTER
),
1179 [ $revOne->getId() ]
1184 $revOne->getId() => strlen( __METHOD__
),
1185 $revTwo->getId() => strlen( __METHOD__
) +
1,
1187 $store->listRevisionSizes(
1188 wfGetDB( DB_MASTER
),
1189 [ $revOne->getId(), $revTwo->getId() ]
1195 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1197 public function testGetPreviousRevision() {
1198 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1199 /** @var Revision $revOne */
1200 $revOne = $page->doEditContent(
1201 new WikitextContent( __METHOD__
), __METHOD__
1202 )->value
['revision'];
1203 /** @var Revision $revTwo */
1204 $revTwo = $page->doEditContent(
1205 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1206 )->value
['revision'];
1208 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1210 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1214 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1219 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1221 public function testGetNextRevision() {
1222 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1223 /** @var Revision $revOne */
1224 $revOne = $page->doEditContent(
1225 new WikitextContent( __METHOD__
), __METHOD__
1226 )->value
['revision'];
1227 /** @var Revision $revTwo */
1228 $revTwo = $page->doEditContent(
1229 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1230 )->value
['revision'];
1232 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1235 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1238 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1243 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1245 public function testGetTimestampFromId_found() {
1246 $page = $this->getTestPage();
1247 /** @var Revision $rev */
1248 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1249 ->value
['revision'];
1251 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1252 $result = $store->getTimestampFromId(
1257 $this->assertSame( $rev->getTimestamp(), $result );
1261 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1263 public function testGetTimestampFromId_notFound() {
1264 $page = $this->getTestPage();
1265 /** @var Revision $rev */
1266 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1267 ->value
['revision'];
1269 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1270 $result = $store->getTimestampFromId(
1275 $this->assertFalse( $result );
1279 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1281 public function testCountRevisionsByPageId() {
1282 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1283 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1287 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1289 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1292 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1294 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1297 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1302 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1304 public function testCountRevisionsByTitle() {
1305 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1306 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1310 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1312 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1315 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1317 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1320 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1325 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1327 public function testUserWasLastToEdit_false() {
1328 $sysop = $this->getTestSysop()->getUser();
1329 $page = $this->getTestPage();
1330 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1332 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1333 $result = $store->userWasLastToEdit(
1334 wfGetDB( DB_MASTER
),
1339 $this->assertFalse( $result );
1343 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1345 public function testUserWasLastToEdit_true() {
1346 $startTime = wfTimestampNow();
1347 $sysop = $this->getTestSysop()->getUser();
1348 $page = $this->getTestPage();
1349 $page->doEditContent(
1350 new WikitextContent( __METHOD__
),
1357 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1358 $result = $store->userWasLastToEdit(
1359 wfGetDB( DB_MASTER
),
1364 $this->assertTrue( $result );
1368 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1370 public function testGetKnownCurrentRevision() {
1371 $page = $this->getTestPage();
1372 /** @var Revision $rev */
1373 $rev = $page->doEditContent(
1374 new WikitextContent( __METHOD__
. 'b' ),
1378 $this->getTestUser()->getUser()
1379 )->value
['revision'];
1381 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1382 $record = $store->getKnownCurrentRevision(
1387 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1390 public function provideNewMutableRevisionFromArray() {
1391 yield
'Basic array, content object' => [
1395 'timestamp' => '20171017114835',
1396 'user_text' => '111.0.1.2',
1398 'minor_edit' => false,
1402 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1403 'comment' => 'Goat Comment!',
1404 'content' => new WikitextContent( 'Some Content' ),
1407 yield
'Basic array, serialized text' => [
1411 'timestamp' => '20171017114835',
1412 'user_text' => '111.0.1.2',
1414 'minor_edit' => false,
1418 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1419 'comment' => 'Goat Comment!',
1420 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1423 yield
'Basic array, serialized text, utf-8 flags' => [
1427 'timestamp' => '20171017114835',
1428 'user_text' => '111.0.1.2',
1430 'minor_edit' => false,
1434 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1435 'comment' => 'Goat Comment!',
1436 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1440 yield
'Basic array, with title' => [
1442 'title' => Title
::newFromText( 'SomeText' ),
1443 'timestamp' => '20171017114835',
1444 'user_text' => '111.0.1.2',
1446 'minor_edit' => false,
1450 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1451 'comment' => 'Goat Comment!',
1452 'content' => new WikitextContent( 'Some Content' ),
1455 yield
'Basic array, no user field' => [
1459 'timestamp' => '20171017114835',
1460 'user_text' => '111.0.1.3',
1461 'minor_edit' => false,
1465 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1466 'comment' => 'Goat Comment!',
1467 'content' => new WikitextContent( 'Some Content' ),
1473 * @dataProvider provideNewMutableRevisionFromArray
1474 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1476 public function testNewMutableRevisionFromArray( array $array ) {
1477 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1479 // HACK: if $array['page'] is given, make sure that the page exists
1480 if ( isset( $array['page'] ) ) {
1481 $t = Title
::newFromID( $array['page'] );
1482 if ( !$t ||
!$t->exists() ) {
1483 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1484 $info = $this->insertPage( $t );
1485 $array['page'] = $info['id'];
1489 $result = $store->newMutableRevisionFromArray( $array );
1491 if ( isset( $array['id'] ) ) {
1492 $this->assertSame( $array['id'], $result->getId() );
1494 if ( isset( $array['page'] ) ) {
1495 $this->assertSame( $array['page'], $result->getPageId() );
1497 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1498 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1499 if ( isset( $array['user'] ) ) {
1500 $this->assertSame( $array['user'], $result->getUser()->getId() );
1502 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1503 $this->assertSame( $array['deleted'], $result->getVisibility() );
1504 $this->assertSame( $array['len'], $result->getSize() );
1505 $this->assertSame( $array['parent_id'], $result->getParentId() );
1506 $this->assertSame( $array['sha1'], $result->getSha1() );
1507 $this->assertSame( $array['comment'], $result->getComment()->text
);
1508 if ( isset( $array['content'] ) ) {
1509 foreach ( $array['content'] as $role => $content ) {
1511 $result->getContent( $role )->equals( $content )
1514 } elseif ( isset( $array['text'] ) ) {
1515 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1516 } elseif ( isset( $array['content_format'] ) ) {
1518 $array['content_format'],
1519 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1521 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1526 * @dataProvider provideNewMutableRevisionFromArray
1527 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1529 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1530 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1531 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1532 $blobStore = new SqlBlobStore( $lb, $cache );
1533 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1535 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1536 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1537 ->disableOriginalConstructor()
1539 $factory->expects( $this->any() )
1540 ->method( 'newBlobStore' )
1541 ->willReturn( $blobStore );
1542 $factory->expects( $this->any() )
1543 ->method( 'newSqlBlobStore' )
1544 ->willReturn( $blobStore );
1546 $this->setService( 'BlobStoreFactory', $factory );
1548 $this->testNewMutableRevisionFromArray( $array );
1551 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1562 if ( $returnTextIdField ) {
1563 $fields[] = 'rev_text_id';
1568 protected function getCommentQueryFields() {
1570 'rev_comment_text' => 'rev_comment',
1571 'rev_comment_data' => 'NULL',
1572 'rev_comment_cid' => 'NULL',
1576 protected function getActorQueryFields() {
1578 'rev_user' => 'rev_user',
1579 'rev_user_text' => 'rev_user_text',
1580 'rev_actor' => 'NULL',
1584 protected function getContentHandlerQueryFields() {
1586 'rev_content_format',
1587 'rev_content_model',
1591 abstract public function provideGetQueryInfo();
1594 * @dataProvider provideGetQueryInfo
1595 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1597 public function testGetQueryInfo( $options, $expected ) {
1598 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1600 $queryInfo = $store->getQueryInfo( $options );
1602 $this->assertArrayEqualsIgnoringIntKeyOrder(
1603 $expected['tables'],
1604 $queryInfo['tables']
1606 $this->assertArrayEqualsIgnoringIntKeyOrder(
1607 $expected['fields'],
1608 $queryInfo['fields']
1610 $this->assertArrayEqualsIgnoringIntKeyOrder(
1616 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1630 if ( $returnTextFields ) {
1631 $fields[] = 'ar_text_id';
1636 abstract public function provideGetArchiveQueryInfo();
1639 * @dataProvider provideGetArchiveQueryInfo
1640 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1642 public function testGetArchiveQueryInfo( $expected ) {
1643 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1645 $archiveQueryInfo = $store->getArchiveQueryInfo();
1647 $this->assertArrayEqualsIgnoringIntKeyOrder(
1648 $expected['tables'],
1649 $archiveQueryInfo['tables']
1652 $this->assertArrayEqualsIgnoringIntKeyOrder(
1653 $expected['fields'],
1654 $archiveQueryInfo['fields']
1657 $this->assertArrayEqualsIgnoringIntKeyOrder(
1659 $archiveQueryInfo['joins']
1663 abstract public function provideGetSlotsQueryInfo();
1666 * @dataProvider provideGetSlotsQueryInfo
1667 * @covers \MediaWiki\Storage\RevisionStore::getSlotsQueryInfo
1669 public function testGetSlotsQueryInfo( $options, $expected ) {
1670 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1672 $archiveQueryInfo = $store->getSlotsQueryInfo( $options );
1674 $this->assertArrayEqualsIgnoringIntKeyOrder(
1675 $expected['tables'],
1676 $archiveQueryInfo['tables']
1679 $this->assertArrayEqualsIgnoringIntKeyOrder(
1680 $expected['fields'],
1681 $archiveQueryInfo['fields']
1684 $this->assertArrayEqualsIgnoringIntKeyOrder(
1686 $archiveQueryInfo['joins']
1691 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1694 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1695 * split into a string key array and an int key array before assertions occur.
1697 * @param array $expected
1698 * @param array $actual
1700 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1701 $this->objectAssociativeSort( $expected );
1702 $this->objectAssociativeSort( $actual );
1704 // Separate the int key values from the string key values so that assertion failures are
1705 // easier to understand.
1706 $expectedIntKeyValues = [];
1707 $actualIntKeyValues = [];
1709 // Remove all int keys and re add them at the end after sorting by value
1710 // This will result in all int keys being in the same order with same ints at the end of
1712 foreach ( $expected as $key => $value ) {
1713 if ( is_int( $key ) ) {
1714 unset( $expected[$key] );
1715 $expectedIntKeyValues[] = $value;
1718 foreach ( $actual as $key => $value ) {
1719 if ( is_int( $key ) ) {
1720 unset( $actual[$key] );
1721 $actualIntKeyValues[] = $value;
1725 $this->assertArrayEquals( $expected, $actual, false, true );
1726 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );