3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
9 use InvalidArgumentException
;
11 use MediaWiki\Linker\LinkTarget
;
12 use MediaWiki\MediaWikiServices
;
13 use MediaWiki\Revision\IncompleteRevisionException
;
14 use MediaWiki\Revision\MutableRevisionRecord
;
15 use MediaWiki\Revision\RevisionRecord
;
16 use MediaWiki\Revision\RevisionStore
;
17 use MediaWiki\Revision\SlotRecord
;
18 use MediaWiki\Storage\BlobStoreFactory
;
19 use MediaWiki\Storage\SqlBlobStore
;
20 use MediaWiki\User\UserIdentityValue
;
21 use MediaWikiTestCase
;
22 use PHPUnit_Framework_MockObject_MockObject
;
27 use Wikimedia\Rdbms\Database
;
28 use Wikimedia\Rdbms\DatabaseDomain
;
29 use Wikimedia\Rdbms\DatabaseSqlite
;
30 use Wikimedia\Rdbms\FakeResultWrapper
;
31 use Wikimedia\Rdbms\LoadBalancer
;
32 use Wikimedia\Rdbms\TransactionProfiler
;
38 * @group RevisionStore
40 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
45 private $testPageTitle;
55 abstract protected function getMcrMigrationStage();
60 protected function getContentHandlerUseDB() {
67 abstract protected function getMcrTablesToReset();
69 public function setUp() {
71 $this->tablesUsed
[] = 'archive';
72 $this->tablesUsed
[] = 'page';
73 $this->tablesUsed
[] = 'revision';
74 $this->tablesUsed
[] = 'comment';
76 $this->tablesUsed +
= $this->getMcrTablesToReset();
78 $this->setMwGlobals( [
79 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
80 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
81 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
82 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD
,
85 $this->overrideMwServices();
88 protected function addCoreDBData() {
89 // Blank out. This would fail with a modified schema, and we don't need it.
95 protected function getTestPageTitle() {
96 if ( $this->testPageTitle
) {
97 return $this->testPageTitle
;
100 $this->testPageTitle
= Title
::newFromText( 'UTPage-' . __CLASS__
);
101 return $this->testPageTitle
;
106 protected function getTestPage() {
107 if ( $this->testPage
) {
108 return $this->testPage
;
111 $title = $this->getTestPageTitle();
112 $this->testPage
= WikiPage
::factory( $title );
114 if ( !$this->testPage
->exists() ) {
115 // Make sure we don't write to the live db.
116 $this->ensureMockDatabaseConnection( wfGetDB( DB_MASTER
) );
118 $user = static::getTestSysop()->getUser();
120 $this->testPage
->doEditContent(
121 new WikitextContent( 'UTContent-' . __CLASS__
),
122 'UTPageSummary-' . __CLASS__
,
123 EDIT_NEW | EDIT_SUPPRESS_RC
,
129 return $this->testPage
;
133 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
135 private function getLoadBalancerMock( array $server ) {
136 $domain = new DatabaseDomain( $server['dbname'], null, $server['tablePrefix'] );
138 $lb = $this->getMockBuilder( LoadBalancer
::class )
139 ->setMethods( [ 'reallyOpenConnection' ] )
140 ->setConstructorArgs( [
141 [ 'servers' => [ $server ], 'localDomain' => $domain ]
145 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
146 function ( array $server, $dbNameOverride ) {
147 return $this->getDatabaseMock( $server );
155 * @return Database|PHPUnit_Framework_MockObject_MockObject
157 private function getDatabaseMock( array $params ) {
158 $db = $this->getMockBuilder( DatabaseSqlite
::class )
159 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
160 ->setConstructorArgs( [ $params ] )
163 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
164 $db->method( 'isOpen' )->willReturn( true );
169 public function provideDomainCheck() {
170 yield
[ false, 'test', '' ];
171 yield
[ 'test', 'test', '' ];
173 yield
[ false, 'test', 'foo_' ];
174 yield
[ 'test-foo_', 'test', 'foo_' ];
176 yield
[ false, 'dash-test', '' ];
177 yield
[ 'dash-test', 'dash-test', '' ];
179 yield
[ false, 'underscore_test', 'foo_' ];
180 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
184 * @dataProvider provideDomainCheck
185 * @covers \MediaWiki\Revision\RevisionStore::checkDatabaseWikiId
187 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
190 'wgDBname' => $dbName,
191 'wgDBprefix' => $dbPrefix,
195 $loadBalancer = $this->getLoadBalancerMock(
198 'dbDirectory' => '*dummy*',
200 'password' => 'test',
208 'trxProfiler' => new TransactionProfiler(),
209 'connLogger' => new \Psr\Log\
NullLogger(),
210 'queryLogger' => new \Psr\Log\
NullLogger(),
211 'errorLogger' => function () {
213 'deprecationLogger' => function () {
217 'tablePrefix' => $dbPrefix,
220 $db = $loadBalancer->getConnection( DB_REPLICA
);
222 /** @var SqlBlobStore $blobStore */
223 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
224 ->disableOriginalConstructor()
227 $store = new RevisionStore(
230 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
231 MediaWikiServices
::getInstance()->getCommentStore(),
232 MediaWikiServices
::getInstance()->getContentModelStore(),
233 MediaWikiServices
::getInstance()->getSlotRoleStore(),
234 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
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 protected 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 protected 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->assertArrayEquals( $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 protected 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 protected function assertRevisionCompleteness( RevisionRecord
$r ) {
307 $this->assertTrue( $r->hasSlot( SlotRecord
::MAIN
) );
308 $this->assertInstanceOf( SlotRecord
::class, $r->getSlot( SlotRecord
::MAIN
) );
309 $this->assertInstanceOf( Content
::class, $r->getContent( SlotRecord
::MAIN
) );
311 foreach ( $r->getSlotRoles() as $role ) {
312 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
316 protected 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( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
368 'comment' => $this->getRandomCommentStoreComment(),
369 'timestamp' => '20171117010101',
373 yield
'Detailed revision insertion' => [
375 'slot' => SlotRecord
::newUnsaved( SlotRecord
::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\Revision\RevisionStore::insertRevisionOn
394 * @covers \MediaWiki\Revision\RevisionStore::insertSlotRowOn
395 * @covers \MediaWiki\Revision\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\Revision\RevisionStore::insertRevisionOn
460 public function testInsertRevisionOn_blobAddressExists() {
461 $title = $this->getTestPageTitle();
463 'slot' => SlotRecord
::newUnsaved( SlotRecord
::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( SlotRecord
::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( SlotRecord
::MAIN
);
487 $secondMainSlot = $secondReturn->getSlot( SlotRecord
::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( SlotRecord
::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( SlotRecord
::MAIN
, new WikitextContent( 'Chicken' ) ),
528 'timestamp' => '20171117010101',
531 new IncompleteRevisionException( 'comment must not be NULL!' )
535 'slot' => SlotRecord
::newUnsaved( SlotRecord
::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\Revision\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\Revision\RevisionStore::newNullRevision
581 * @covers \MediaWiki\Revision\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->assertArrayEquals(
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\Revision\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\Revision\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\Revision\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\Revision\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\Revision\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( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
727 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
731 * @covers \MediaWiki\Revision\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( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
745 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
749 * @covers \MediaWiki\Revision\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( SlotRecord
::MAIN
)->getContent()->equals( $content ) );
763 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
767 * @covers \MediaWiki\Revision\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( SlotRecord
::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 protected 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(),
866 $record->getContent( SlotRecord
::MAIN
)->getDefaultFormat() );
867 $this->assertSame( $rev->getContentModel(), $record->getContent( SlotRecord
::MAIN
)->getModel() );
868 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
870 $revRec = $rev->getRevisionRecord();
871 $revMain = $revRec->getSlot( SlotRecord
::MAIN
);
872 $recMain = $record->getSlot( SlotRecord
::MAIN
);
874 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
875 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
876 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
878 if ( $revMain->hasOrigin() ) {
879 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
882 if ( $revMain->hasAddress() ) {
883 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
886 if ( $revMain->hasContentId() ) {
887 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
892 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
893 * @covers \MediaWiki\Revision\RevisionStore::getQueryInfo
895 public function testNewRevisionFromRow_getQueryInfo() {
896 $page = $this->getTestPage();
897 $text = __METHOD__
. 'a-ä';
898 /** @var Revision $rev */
899 $rev = $page->doEditContent(
900 new WikitextContent( $text ),
902 )->value
['revision'];
904 $store = MediaWikiServices
::getInstance()->getRevisionStore();
905 $info = $store->getQueryInfo();
906 $row = $this->db
->selectRow(
909 [ 'rev_id' => $rev->getId() ],
914 $record = $store->newRevisionFromRow(
919 $this->assertRevisionRecordMatchesRevision( $rev, $record );
920 $this->assertSame( $text, $rev->getContent()->serialize() );
924 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
926 public function testNewRevisionFromRow_anonEdit() {
927 $page = $this->getTestPage();
928 $text = __METHOD__
. 'a-ä';
929 /** @var Revision $rev */
930 $rev = $page->doEditContent(
931 new WikitextContent( $text ),
933 )->value
['revision'];
935 $store = MediaWikiServices
::getInstance()->getRevisionStore();
936 $record = $store->newRevisionFromRow(
937 $this->revisionToRow( $rev ),
941 $this->assertRevisionRecordMatchesRevision( $rev, $record );
942 $this->assertSame( $text, $rev->getContent()->serialize() );
946 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
948 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
949 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
950 $this->overrideMwServices();
951 $page = $this->getTestPage();
952 $text = __METHOD__
. 'a-ä';
953 /** @var Revision $rev */
954 $rev = $page->doEditContent(
955 new WikitextContent( $text ),
957 )->value
['revision'];
959 $store = MediaWikiServices
::getInstance()->getRevisionStore();
960 $record = $store->newRevisionFromRow(
961 $this->revisionToRow( $rev ),
965 $this->assertRevisionRecordMatchesRevision( $rev, $record );
966 $this->assertSame( $text, $rev->getContent()->serialize() );
970 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
972 public function testNewRevisionFromRow_userEdit() {
973 $page = $this->getTestPage();
974 $text = __METHOD__
. 'b-ä';
975 /** @var Revision $rev */
976 $rev = $page->doEditContent(
977 new WikitextContent( $text ),
981 $this->getTestUser()->getUser()
982 )->value
['revision'];
984 $store = MediaWikiServices
::getInstance()->getRevisionStore();
985 $record = $store->newRevisionFromRow(
986 $this->revisionToRow( $rev ),
990 $this->assertRevisionRecordMatchesRevision( $rev, $record );
991 $this->assertSame( $text, $rev->getContent()->serialize() );
995 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
996 * @covers \MediaWiki\Revision\RevisionStore::getArchiveQueryInfo
998 public function testNewRevisionFromArchiveRow_getArchiveQueryInfo() {
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( SlotRecord
::MAIN
)->serialize() );
1025 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1027 public function testNewRevisionFromArchiveRow_legacyEncoding() {
1028 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
1029 $this->overrideMwServices();
1030 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1031 $title = Title
::newFromText( __METHOD__
);
1032 $text = __METHOD__
. '-bä';
1033 $page = WikiPage
::factory( $title );
1034 /** @var Revision $orig */
1035 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
1036 ->value
['revision'];
1037 $page->doDeleteArticle( __METHOD__
);
1039 $db = wfGetDB( DB_MASTER
);
1040 $arQuery = $store->getArchiveQueryInfo();
1042 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1043 __METHOD__
, [], $arQuery['joins']
1045 $this->assertTrue( is_object( $res ), 'query failed' );
1047 $row = $res->fetchObject();
1049 $record = $store->newRevisionFromArchiveRow( $row );
1051 $this->assertRevisionRecordMatchesRevision( $orig, $record );
1052 $this->assertSame( $text, $record->getContent( SlotRecord
::MAIN
)->serialize() );
1056 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromArchiveRow
1058 public function testNewRevisionFromArchiveRow_no_user() {
1059 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1063 'ar_page_id' => '2',
1064 'ar_namespace' => '0',
1065 'ar_title' => 'Something',
1067 'ar_text_id' => '47',
1068 'ar_timestamp' => '20180528192356',
1069 'ar_minor_edit' => '0',
1070 'ar_deleted' => '0',
1072 'ar_parent_id' => '0',
1073 'ar_sha1' => 'deadbeef',
1074 'ar_comment_text' => 'whatever',
1075 'ar_comment_data' => null,
1076 'ar_comment_cid' => null,
1078 'ar_user_text' => '', // this is the important bit
1080 'ar_content_format' => null,
1081 'ar_content_model' => null,
1084 \Wikimedia\
suppressWarnings();
1085 $record = $store->newRevisionFromArchiveRow( $row );
1086 \Wikimedia\
suppressWarnings( true );
1088 $this->assertInstanceOf( RevisionRecord
::class, $record );
1089 $this->assertInstanceOf( UserIdentityValue
::class, $record->getUser() );
1090 $this->assertSame( 'Unknown user', $record->getUser()->getName() );
1094 * @covers \MediaWiki\Revision\RevisionStore::newRevisionFromRow
1096 public function testNewRevisionFromRow_no_user() {
1097 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1098 $title = Title
::newFromText( __METHOD__
);
1103 'page_namespace' => '0',
1104 'page_title' => $title->getText(),
1105 'rev_text_id' => '47',
1106 'rev_timestamp' => '20180528192356',
1107 'rev_minor_edit' => '0',
1108 'rev_deleted' => '0',
1110 'rev_parent_id' => '0',
1111 'rev_sha1' => 'deadbeef',
1112 'rev_comment_text' => 'whatever',
1113 'rev_comment_data' => null,
1114 'rev_comment_cid' => null,
1116 'rev_user_text' => '', // this is the important bit
1117 'rev_actor' => null,
1118 'rev_content_format' => null,
1119 'rev_content_model' => null,
1122 \Wikimedia\
suppressWarnings();
1123 $record = $store->newRevisionFromRow( $row, 0, $title );
1124 \Wikimedia\
suppressWarnings( true );
1126 $this->assertNotNull( $record );
1127 $this->assertNotNull( $record->getUser() );
1128 $this->assertNotEmpty( $record->getUser()->getName() );
1132 * @covers \MediaWiki\Revision\RevisionStore::insertRevisionOn
1134 public function testInsertRevisionOn_archive() {
1135 // This is a round trip test for deletion and undeletion of a
1136 // revision row via the archive table.
1138 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1139 $title = Title
::newFromText( __METHOD__
);
1141 $page = WikiPage
::factory( $title );
1142 /** @var Revision $origRev */
1143 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
1144 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
1145 ->value
['revision'];
1146 $orig = $origRev->getRevisionRecord();
1147 $page->doDeleteArticle( __METHOD__
);
1149 // re-create page, so we can later load revisions for it
1150 $page->doEditContent( new WikitextContent( 'Two' ), __METHOD__
);
1152 $db = wfGetDB( DB_MASTER
);
1153 $arQuery = $store->getArchiveQueryInfo();
1154 $row = $db->selectRow(
1155 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
1156 __METHOD__
, [], $arQuery['joins']
1159 $this->assertNotFalse( $row, 'query failed' );
1161 $record = $store->newRevisionFromArchiveRow(
1165 [ 'page_id' => $title->getArticleID() ]
1168 $restored = $store->insertRevisionOn( $record, $db );
1170 // is the new revision correct?
1171 $this->assertRevisionCompleteness( $restored );
1172 $this->assertRevisionRecordsEqual( $record, $restored );
1174 // does the new revision use the original slot?
1175 $recMain = $record->getSlot( SlotRecord
::MAIN
);
1176 $restMain = $restored->getSlot( SlotRecord
::MAIN
);
1177 $this->assertSame( $recMain->getAddress(), $restMain->getAddress() );
1178 $this->assertSame( $recMain->getContentId(), $restMain->getContentId() );
1179 $this->assertSame( $recMain->getOrigin(), $restMain->getOrigin() );
1180 $this->assertSame( 'Foo', $restMain->getContent()->serialize() );
1182 // can we load it from the store?
1183 $loaded = $store->getRevisionById( $restored->getId() );
1184 $this->assertNotNull( $loaded );
1185 $this->assertRevisionCompleteness( $loaded );
1186 $this->assertRevisionRecordsEqual( $restored, $loaded );
1188 // can we find it directly in the database?
1189 $this->assertRevisionExistsInDatabase( $restored );
1193 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromId
1195 public function testLoadRevisionFromId() {
1196 $title = Title
::newFromText( __METHOD__
);
1197 $page = WikiPage
::factory( $title );
1198 /** @var Revision $rev */
1199 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1200 ->value
['revision'];
1202 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1203 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
1204 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1208 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromPageId
1210 public function testLoadRevisionFromPageId() {
1211 $title = Title
::newFromText( __METHOD__
);
1212 $page = WikiPage
::factory( $title );
1213 /** @var Revision $rev */
1214 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1215 ->value
['revision'];
1217 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1218 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
1219 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1223 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTitle
1225 public function testLoadRevisionFromTitle() {
1226 $title = Title
::newFromText( __METHOD__
);
1227 $page = WikiPage
::factory( $title );
1228 /** @var Revision $rev */
1229 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1230 ->value
['revision'];
1232 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1233 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
1234 $this->assertRevisionRecordMatchesRevision( $rev, $result );
1238 * @covers \MediaWiki\Revision\RevisionStore::loadRevisionFromTimestamp
1240 public function testLoadRevisionFromTimestamp() {
1241 $title = Title
::newFromText( __METHOD__
);
1242 $page = WikiPage
::factory( $title );
1243 /** @var Revision $revOne */
1244 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1245 ->value
['revision'];
1246 // Sleep to ensure different timestamps... )(evil)
1248 /** @var Revision $revTwo */
1249 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1250 ->value
['revision'];
1252 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1254 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1258 $store->loadRevisionFromTimestamp(
1259 wfGetDB( DB_MASTER
),
1261 $revOne->getTimestamp()
1266 $store->loadRevisionFromTimestamp(
1267 wfGetDB( DB_MASTER
),
1269 $revTwo->getTimestamp()
1275 * @covers \MediaWiki\Revision\RevisionStore::listRevisionSizes
1277 public function testGetParentLengths() {
1278 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1279 /** @var Revision $revOne */
1280 $revOne = $page->doEditContent(
1281 new WikitextContent( __METHOD__
), __METHOD__
1282 )->value
['revision'];
1283 /** @var Revision $revTwo */
1284 $revTwo = $page->doEditContent(
1285 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1286 )->value
['revision'];
1288 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1291 $revOne->getId() => strlen( __METHOD__
),
1293 $store->listRevisionSizes(
1294 wfGetDB( DB_MASTER
),
1295 [ $revOne->getId() ]
1300 $revOne->getId() => strlen( __METHOD__
),
1301 $revTwo->getId() => strlen( __METHOD__
) +
1,
1303 $store->listRevisionSizes(
1304 wfGetDB( DB_MASTER
),
1305 [ $revOne->getId(), $revTwo->getId() ]
1311 * @covers \MediaWiki\Revision\RevisionStore::getPreviousRevision
1313 public function testGetPreviousRevision() {
1314 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1315 /** @var Revision $revOne */
1316 $revOne = $page->doEditContent(
1317 new WikitextContent( __METHOD__
), __METHOD__
1318 )->value
['revision'];
1319 /** @var Revision $revTwo */
1320 $revTwo = $page->doEditContent(
1321 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1322 )->value
['revision'];
1324 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1326 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1330 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1335 * @covers \MediaWiki\Revision\RevisionStore::getNextRevision
1337 public function testGetNextRevision() {
1338 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1339 /** @var Revision $revOne */
1340 $revOne = $page->doEditContent(
1341 new WikitextContent( __METHOD__
), __METHOD__
1342 )->value
['revision'];
1343 /** @var Revision $revTwo */
1344 $revTwo = $page->doEditContent(
1345 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1346 )->value
['revision'];
1348 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1351 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1354 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1359 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1361 public function testGetTimestampFromId_found() {
1362 $page = $this->getTestPage();
1363 /** @var Revision $rev */
1364 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1365 ->value
['revision'];
1367 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1368 $result = $store->getTimestampFromId(
1373 $this->assertSame( $rev->getTimestamp(), $result );
1377 * @covers \MediaWiki\Revision\RevisionStore::getTimestampFromId
1379 public function testGetTimestampFromId_notFound() {
1380 $page = $this->getTestPage();
1381 /** @var Revision $rev */
1382 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
1383 ->value
['revision'];
1385 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1386 $result = $store->getTimestampFromId(
1391 $this->assertFalse( $result );
1395 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByPageId
1397 public function testCountRevisionsByPageId() {
1398 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1399 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1403 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1405 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1408 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1410 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1413 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1418 * @covers \MediaWiki\Revision\RevisionStore::countRevisionsByTitle
1420 public function testCountRevisionsByTitle() {
1421 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1422 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1426 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1428 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1431 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1433 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1436 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1441 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1443 public function testUserWasLastToEdit_false() {
1444 $sysop = $this->getTestSysop()->getUser();
1445 $page = $this->getTestPage();
1446 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1448 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1449 $result = $store->userWasLastToEdit(
1450 wfGetDB( DB_MASTER
),
1455 $this->assertFalse( $result );
1459 * @covers \MediaWiki\Revision\RevisionStore::userWasLastToEdit
1461 public function testUserWasLastToEdit_true() {
1462 $startTime = wfTimestampNow();
1463 $sysop = $this->getTestSysop()->getUser();
1464 $page = $this->getTestPage();
1465 $page->doEditContent(
1466 new WikitextContent( __METHOD__
),
1473 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1474 $result = $store->userWasLastToEdit(
1475 wfGetDB( DB_MASTER
),
1480 $this->assertTrue( $result );
1484 * @covers \MediaWiki\Revision\RevisionStore::getKnownCurrentRevision
1486 public function testGetKnownCurrentRevision() {
1487 $page = $this->getTestPage();
1488 /** @var Revision $rev */
1489 $rev = $page->doEditContent(
1490 new WikitextContent( __METHOD__
. 'b' ),
1494 $this->getTestUser()->getUser()
1495 )->value
['revision'];
1497 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1498 $record = $store->getKnownCurrentRevision(
1503 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1506 public function provideNewMutableRevisionFromArray() {
1507 yield
'Basic array, content object' => [
1511 'timestamp' => '20171017114835',
1512 'user_text' => '111.0.1.2',
1514 'minor_edit' => false,
1518 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1519 'comment' => 'Goat Comment!',
1520 'content' => new WikitextContent( 'Some Content' ),
1523 yield
'Basic array, serialized text' => [
1527 'timestamp' => '20171017114835',
1528 'user_text' => '111.0.1.2',
1530 'minor_edit' => false,
1534 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1535 'comment' => 'Goat Comment!',
1536 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1539 yield
'Basic array, serialized text, utf-8 flags' => [
1543 'timestamp' => '20171017114835',
1544 'user_text' => '111.0.1.2',
1546 'minor_edit' => false,
1550 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1551 'comment' => 'Goat Comment!',
1552 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1556 yield
'Basic array, with title' => [
1558 'title' => Title
::newFromText( 'SomeText' ),
1559 'timestamp' => '20171017114835',
1560 'user_text' => '111.0.1.2',
1562 'minor_edit' => false,
1566 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1567 'comment' => 'Goat Comment!',
1568 'content' => new WikitextContent( 'Some Content' ),
1571 yield
'Basic array, no user field' => [
1575 'timestamp' => '20171017114835',
1576 'user_text' => '111.0.1.3',
1577 'minor_edit' => false,
1581 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1582 'comment' => 'Goat Comment!',
1583 'content' => new WikitextContent( 'Some Content' ),
1589 * @dataProvider provideNewMutableRevisionFromArray
1590 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1592 public function testNewMutableRevisionFromArray( array $array ) {
1593 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1595 // HACK: if $array['page'] is given, make sure that the page exists
1596 if ( isset( $array['page'] ) ) {
1597 $t = Title
::newFromID( $array['page'] );
1598 if ( !$t ||
!$t->exists() ) {
1599 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1600 $info = $this->insertPage( $t );
1601 $array['page'] = $info['id'];
1605 $result = $store->newMutableRevisionFromArray( $array );
1607 if ( isset( $array['id'] ) ) {
1608 $this->assertSame( $array['id'], $result->getId() );
1610 if ( isset( $array['page'] ) ) {
1611 $this->assertSame( $array['page'], $result->getPageId() );
1613 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1614 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1615 if ( isset( $array['user'] ) ) {
1616 $this->assertSame( $array['user'], $result->getUser()->getId() );
1618 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1619 $this->assertSame( $array['deleted'], $result->getVisibility() );
1620 $this->assertSame( $array['len'], $result->getSize() );
1621 $this->assertSame( $array['parent_id'], $result->getParentId() );
1622 $this->assertSame( $array['sha1'], $result->getSha1() );
1623 $this->assertSame( $array['comment'], $result->getComment()->text
);
1624 if ( isset( $array['content'] ) ) {
1625 foreach ( $array['content'] as $role => $content ) {
1627 $result->getContent( $role )->equals( $content )
1630 } elseif ( isset( $array['text'] ) ) {
1631 $this->assertSame( $array['text'],
1632 $result->getSlot( SlotRecord
::MAIN
)->getContent()->serialize() );
1633 } elseif ( isset( $array['content_format'] ) ) {
1635 $array['content_format'],
1636 $result->getSlot( SlotRecord
::MAIN
)->getContent()->getDefaultFormat()
1638 $this->assertSame( $array['content_model'], $result->getSlot( SlotRecord
::MAIN
)->getModel() );
1643 * @dataProvider provideNewMutableRevisionFromArray
1644 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
1646 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1647 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1648 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1649 $blobStore = new SqlBlobStore( $lb, $cache );
1650 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1652 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1653 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1654 ->disableOriginalConstructor()
1656 $factory->expects( $this->any() )
1657 ->method( 'newBlobStore' )
1658 ->willReturn( $blobStore );
1659 $factory->expects( $this->any() )
1660 ->method( 'newSqlBlobStore' )
1661 ->willReturn( $blobStore );
1663 $this->setService( 'BlobStoreFactory', $factory );
1665 $this->testNewMutableRevisionFromArray( $array );