3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
8 use InvalidArgumentException
;
10 use MediaWiki\Linker\LinkTarget
;
11 use MediaWiki\MediaWikiServices
;
12 use MediaWiki\Storage\BlobStoreFactory
;
13 use MediaWiki\Storage\IncompleteRevisionException
;
14 use MediaWiki\Storage\MutableRevisionRecord
;
15 use MediaWiki\Storage\RevisionRecord
;
16 use MediaWiki\Storage\RevisionStore
;
17 use MediaWiki\Storage\SlotRecord
;
18 use MediaWiki\Storage\SqlBlobStore
;
19 use MediaWikiTestCase
;
24 use Wikimedia\Rdbms\Database
;
25 use Wikimedia\Rdbms\DatabaseSqlite
;
26 use Wikimedia\Rdbms\FakeResultWrapper
;
27 use Wikimedia\Rdbms\LoadBalancer
;
28 use Wikimedia\Rdbms\TransactionProfiler
;
35 class RevisionStoreDbTest
extends MediaWikiTestCase
{
37 public function setUp() {
39 $this->tablesUsed
[] = 'archive';
40 $this->tablesUsed
[] = 'page';
41 $this->tablesUsed
[] = 'revision';
42 $this->tablesUsed
[] = 'comment';
46 * @return LoadBalancer
48 private function getLoadBalancerMock( array $server ) {
49 $lb = $this->getMockBuilder( LoadBalancer
::class )
50 ->setMethods( [ 'reallyOpenConnection' ] )
51 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
54 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
55 function ( array $server, $dbNameOverride ) {
56 return $this->getDatabaseMock( $server );
66 private function getDatabaseMock( array $params ) {
67 $db = $this->getMockBuilder( DatabaseSqlite
::class )
68 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
69 ->setConstructorArgs( [ $params ] )
72 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
73 $db->method( 'isOpen' )->willReturn( true );
78 public function provideDomainCheck() {
79 yield
[ false, 'test', '' ];
80 yield
[ 'test', 'test', '' ];
82 yield
[ false, 'test', 'foo_' ];
83 yield
[ 'test-foo_', 'test', 'foo_' ];
85 yield
[ false, 'dash-test', '' ];
86 yield
[ 'dash-test', 'dash-test', '' ];
88 yield
[ false, 'underscore_test', 'foo_' ];
89 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
93 * @dataProvider provideDomainCheck
94 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
96 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
99 'wgDBname' => $dbName,
100 'wgDBprefix' => $dbPrefix,
104 $loadBalancer = $this->getLoadBalancerMock(
107 'dbDirectory' => '*dummy*',
109 'password' => 'test',
117 'trxProfiler' => new TransactionProfiler(),
118 'connLogger' => new \Psr\Log\
NullLogger(),
119 'queryLogger' => new \Psr\Log\
NullLogger(),
120 'errorLogger' => function () {
122 'deprecationLogger' => function () {
126 'tablePrefix' => $dbPrefix,
129 $db = $loadBalancer->getConnection( DB_REPLICA
);
131 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
132 ->disableOriginalConstructor()
135 $store = new RevisionStore(
138 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
139 MediaWikiServices
::getInstance()->getCommentStore(),
140 MediaWikiServices
::getInstance()->getActorMigration(),
144 $count = $store->countRevisionsByPageId( $db, 0 );
146 // Dummy check to make PhpUnit happy. We are really only interested in
147 // countRevisionsByPageId not failing due to the DB domain check.
148 $this->assertSame( 0, $count );
151 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
152 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
153 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
154 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
155 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
158 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
159 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
160 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
161 $this->assertEquals( $r1->getComment(), $r2->getComment() );
162 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
163 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
164 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
165 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
166 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
167 $this->assertEquals( $r1->getSize(), $r2->getSize() );
168 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
169 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
170 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
171 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
172 foreach ( $r1->getSlotRoles() as $role ) {
173 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
174 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
177 RevisionRecord
::DELETED_TEXT
,
178 RevisionRecord
::DELETED_COMMENT
,
179 RevisionRecord
::DELETED_USER
,
180 RevisionRecord
::DELETED_RESTRICTED
,
182 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
186 private function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
187 $this->assertSame( $s1->getRole(), $s2->getRole() );
188 $this->assertSame( $s1->getModel(), $s2->getModel() );
189 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
190 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
191 $this->assertSame( $s1->getSize(), $s2->getSize() );
192 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
194 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
195 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
198 private function assertRevisionCompleteness( RevisionRecord
$r ) {
199 foreach ( $r->getSlotRoles() as $role ) {
200 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
204 private function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
205 $this->assertTrue( $slot->hasAddress() );
206 $this->assertSame( $r->getId(), $slot->getRevision() );
210 * @param mixed[] $details
212 * @return RevisionRecord
214 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
215 // Convert some values that can't be provided by dataProviders
216 $page = WikiPage
::factory( $title );
217 if ( isset( $details['user'] ) && $details['user'] === true ) {
218 $details['user'] = $this->getTestUser()->getUser();
220 if ( isset( $details['page'] ) && $details['page'] === true ) {
221 $details['page'] = $page->getId();
223 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
224 $details['parent'] = $page->getLatest();
227 // Create the RevisionRecord with any available data
228 $rev = new MutableRevisionRecord( $title );
229 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
230 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
231 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
232 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
233 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
234 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
235 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
236 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
237 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
238 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
239 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
244 private function getRandomCommentStoreComment() {
245 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
248 public function provideInsertRevisionOn_successes() {
249 yield
'Bare minimum revision insertion' => [
250 Title
::newFromText( 'UTPage' ),
252 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
254 'comment' => $this->getRandomCommentStoreComment(),
255 'timestamp' => '20171117010101',
259 yield
'Detailed revision insertion' => [
260 Title
::newFromText( 'UTPage' ),
262 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
265 'comment' => $this->getRandomCommentStoreComment(),
266 'timestamp' => '20171117010101',
269 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
275 * @dataProvider provideInsertRevisionOn_successes
276 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
278 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
279 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
281 $store = MediaWikiServices
::getInstance()->getRevisionStore();
282 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
284 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
285 $this->assertRevisionRecordsEqual( $rev, $return );
286 $this->assertRevisionCompleteness( $return );
290 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
292 public function testInsertRevisionOn_blobAddressExists() {
293 $title = Title
::newFromText( 'UTPage' );
295 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
297 'comment' => $this->getRandomCommentStoreComment(),
298 'timestamp' => '20171117010101',
302 $store = MediaWikiServices
::getInstance()->getRevisionStore();
304 // Insert the first revision
305 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
306 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
307 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
308 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
310 // Insert a second revision inheriting the same blob address
311 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
312 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
313 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
314 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
315 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
317 // Assert that the same blob address has been used.
319 $firstReturn->getSlot( 'main' )->getAddress(),
320 $secondReturn->getSlot( 'main' )->getAddress()
322 // And that different revisions have been created.
323 $this->assertNotSame(
324 $firstReturn->getId(),
325 $secondReturn->getId()
329 public function provideInsertRevisionOn_failures() {
331 Title
::newFromText( 'UTPage' ),
333 'comment' => $this->getRandomCommentStoreComment(),
334 'timestamp' => '20171117010101',
337 new InvalidArgumentException( 'At least one slot needs to be defined!' )
339 yield
'slot that is not main slot' => [
340 Title
::newFromText( 'UTPage' ),
342 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
343 'comment' => $this->getRandomCommentStoreComment(),
344 'timestamp' => '20171117010101',
347 new InvalidArgumentException( 'Only the main slot is supported for now!' )
349 yield
'no timestamp' => [
350 Title
::newFromText( 'UTPage' ),
352 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
353 'comment' => $this->getRandomCommentStoreComment(),
356 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
358 yield
'no comment' => [
359 Title
::newFromText( 'UTPage' ),
361 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
362 'timestamp' => '20171117010101',
365 new IncompleteRevisionException( 'comment must not be NULL!' )
368 Title
::newFromText( 'UTPage' ),
370 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
371 'comment' => $this->getRandomCommentStoreComment(),
372 'timestamp' => '20171117010101',
374 new IncompleteRevisionException( 'user must not be NULL!' )
379 * @dataProvider provideInsertRevisionOn_failures
380 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
382 public function testInsertRevisionOn_failures(
384 array $revDetails = [],
385 Exception
$exception ) {
386 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
388 $store = MediaWikiServices
::getInstance()->getRevisionStore();
390 $this->setExpectedException(
391 get_class( $exception ),
392 $exception->getMessage(),
393 $exception->getCode()
395 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
398 public function provideNewNullRevision() {
400 Title
::newFromText( 'UTPage' ),
401 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
405 Title
::newFromText( 'UTPage' ),
406 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
412 * @dataProvider provideNewNullRevision
413 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
415 public function testNewNullRevision( Title
$title, $comment, $minor ) {
416 $store = MediaWikiServices
::getInstance()->getRevisionStore();
417 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
419 $parent = $store->getRevisionByTitle( $title );
420 $record = $store->newNullRevision(
421 wfGetDB( DB_MASTER
),
428 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
429 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
430 $this->assertEquals( $comment, $record->getComment() );
431 $this->assertEquals( $minor, $record->isMinor() );
432 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
433 $this->assertEquals( $parent->getId(), $record->getParentId() );
435 $parentSlot = $parent->getSlot( 'main' );
436 $slot = $record->getSlot( 'main' );
438 $this->assertTrue( $slot->isInherited(), 'isInherited' );
439 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
440 $this->assertSame( $parentSlot->getAddress(), $slot->getAddress(), 'getAddress' );
444 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
446 public function testNewNullRevision_nonExistingTitle() {
447 $store = MediaWikiServices
::getInstance()->getRevisionStore();
448 $record = $store->newNullRevision(
449 wfGetDB( DB_MASTER
),
450 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
451 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
453 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
455 $this->assertNull( $record );
459 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
461 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
462 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
463 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
464 /** @var Revision $rev */
465 $rev = $status->value
['revision'];
467 $store = MediaWikiServices
::getInstance()->getRevisionStore();
468 $revisionRecord = $store->getRevisionById( $rev->getId() );
469 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
471 $this->assertGreaterThan( 0, $result );
473 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
479 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
481 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
482 // This assumes that sysops are auto patrolled
483 $sysop = $this->getTestSysop()->getUser();
484 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
485 $status = $page->doEditContent(
486 new WikitextContent( __METHOD__
),
492 /** @var Revision $rev */
493 $rev = $status->value
['revision'];
495 $store = MediaWikiServices
::getInstance()->getRevisionStore();
496 $revisionRecord = $store->getRevisionById( $rev->getId() );
497 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
499 $this->assertSame( 0, $result );
503 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
505 public function testGetRecentChange() {
506 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
507 $content = new WikitextContent( __METHOD__
);
508 $status = $page->doEditContent( $content, __METHOD__
);
509 /** @var Revision $rev */
510 $rev = $status->value
['revision'];
512 $store = MediaWikiServices
::getInstance()->getRevisionStore();
513 $revRecord = $store->getRevisionById( $rev->getId() );
514 $recentChange = $store->getRecentChange( $revRecord );
516 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
517 $this->assertEquals( $rev->getRecentChange(), $recentChange );
521 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
523 public function testGetRevisionById() {
524 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
525 $content = new WikitextContent( __METHOD__
);
526 $status = $page->doEditContent( $content, __METHOD__
);
527 /** @var Revision $rev */
528 $rev = $status->value
['revision'];
530 $store = MediaWikiServices
::getInstance()->getRevisionStore();
531 $revRecord = $store->getRevisionById( $rev->getId() );
533 $this->assertSame( $rev->getId(), $revRecord->getId() );
534 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
535 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
539 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
541 public function testGetRevisionByTitle() {
542 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
543 $content = new WikitextContent( __METHOD__
);
544 $status = $page->doEditContent( $content, __METHOD__
);
545 /** @var Revision $rev */
546 $rev = $status->value
['revision'];
548 $store = MediaWikiServices
::getInstance()->getRevisionStore();
549 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
551 $this->assertSame( $rev->getId(), $revRecord->getId() );
552 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
553 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
557 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
559 public function testGetRevisionByPageId() {
560 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
561 $content = new WikitextContent( __METHOD__
);
562 $status = $page->doEditContent( $content, __METHOD__
);
563 /** @var Revision $rev */
564 $rev = $status->value
['revision'];
566 $store = MediaWikiServices
::getInstance()->getRevisionStore();
567 $revRecord = $store->getRevisionByPageId( $page->getId() );
569 $this->assertSame( $rev->getId(), $revRecord->getId() );
570 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
571 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
575 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
577 public function testGetRevisionByTimestamp() {
578 // Make sure there is 1 second between the last revision and the rev we create...
579 // Otherwise we might not get the correct revision and the test may fail...
582 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
583 $content = new WikitextContent( __METHOD__
);
584 $status = $page->doEditContent( $content, __METHOD__
);
585 /** @var Revision $rev */
586 $rev = $status->value
['revision'];
588 $store = MediaWikiServices
::getInstance()->getRevisionStore();
589 $revRecord = $store->getRevisionByTimestamp(
594 $this->assertSame( $rev->getId(), $revRecord->getId() );
595 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
596 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
599 private function revisionToRow( Revision
$rev ) {
600 $page = WikiPage
::factory( $rev->getTitle() );
603 'rev_id' => (string)$rev->getId(),
604 'rev_page' => (string)$rev->getPage(),
605 'rev_text_id' => (string)$rev->getTextId(),
606 'rev_timestamp' => (string)$rev->getTimestamp(),
607 'rev_user_text' => (string)$rev->getUserText(),
608 'rev_user' => (string)$rev->getUser(),
609 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
610 'rev_deleted' => (string)$rev->getVisibility(),
611 'rev_len' => (string)$rev->getSize(),
612 'rev_parent_id' => (string)$rev->getParentId(),
613 'rev_sha1' => (string)$rev->getSha1(),
614 'rev_comment_text' => $rev->getComment(),
615 'rev_comment_data' => null,
616 'rev_comment_cid' => null,
617 'rev_content_format' => $rev->getContentFormat(),
618 'rev_content_model' => $rev->getContentModel(),
619 'page_namespace' => (string)$page->getTitle()->getNamespace(),
620 'page_title' => $page->getTitle()->getDBkey(),
621 'page_id' => (string)$page->getId(),
622 'page_latest' => (string)$page->getLatest(),
623 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
624 'page_len' => (string)$page->getContent()->getSize(),
625 'user_name' => (string)$rev->getUserText(),
629 private function assertRevisionRecordMatchesRevision(
631 RevisionRecord
$record
633 $this->assertSame( $rev->getId(), $record->getId() );
634 $this->assertSame( $rev->getPage(), $record->getPageId() );
635 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
636 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
637 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
638 $this->assertSame( $rev->isMinor(), $record->isMinor() );
639 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
640 $this->assertSame( $rev->getSize(), $record->getSize() );
642 * @note As of MW 1.31, the database schema allows the parent ID to be
643 * NULL to indicate that it is unknown.
645 $expectedParent = $rev->getParentId();
646 if ( $expectedParent === null ) {
649 $this->assertSame( $expectedParent, $record->getParentId() );
650 $this->assertSame( $rev->getSha1(), $record->getSha1() );
651 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
652 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
653 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
654 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
658 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
659 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
661 public function testNewRevisionFromRow_anonEdit() {
662 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
663 $this->overrideMwServices();
665 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
666 $text = __METHOD__
. 'a-ä';
667 /** @var Revision $rev */
668 $rev = $page->doEditContent(
669 new WikitextContent( $text ),
671 )->value
['revision'];
673 $store = MediaWikiServices
::getInstance()->getRevisionStore();
674 $record = $store->newRevisionFromRow(
675 $this->revisionToRow( $rev ),
679 $this->assertRevisionRecordMatchesRevision( $rev, $record );
680 $this->assertSame( $text, $rev->getContent()->serialize() );
684 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
685 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
687 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
688 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
689 $this->overrideMwServices();
690 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
691 $text = __METHOD__
. 'a-ä';
692 /** @var Revision $rev */
693 $rev = $page->doEditContent(
694 new WikitextContent( $text ),
696 )->value
['revision'];
698 $store = MediaWikiServices
::getInstance()->getRevisionStore();
699 $record = $store->newRevisionFromRow(
700 $this->revisionToRow( $rev ),
704 $this->assertRevisionRecordMatchesRevision( $rev, $record );
705 $this->assertSame( $text, $rev->getContent()->serialize() );
709 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
710 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
712 public function testNewRevisionFromRow_userEdit() {
713 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
714 $this->overrideMwServices();
716 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
717 $text = __METHOD__
. 'b-ä';
718 /** @var Revision $rev */
719 $rev = $page->doEditContent(
720 new WikitextContent( $text ),
724 $this->getTestUser()->getUser()
725 )->value
['revision'];
727 $store = MediaWikiServices
::getInstance()->getRevisionStore();
728 $record = $store->newRevisionFromRow(
729 $this->revisionToRow( $rev ),
733 $this->assertRevisionRecordMatchesRevision( $rev, $record );
734 $this->assertSame( $text, $rev->getContent()->serialize() );
738 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
740 public function testNewRevisionFromArchiveRow() {
741 $store = MediaWikiServices
::getInstance()->getRevisionStore();
742 $title = Title
::newFromText( __METHOD__
);
743 $text = __METHOD__
. '-bä';
744 $page = WikiPage
::factory( $title );
745 /** @var Revision $orig */
746 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
748 $page->doDeleteArticle( __METHOD__
);
750 $db = wfGetDB( DB_MASTER
);
751 $arQuery = $store->getArchiveQueryInfo();
753 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
754 __METHOD__
, [], $arQuery['joins']
756 $this->assertTrue( is_object( $res ), 'query failed' );
758 $row = $res->fetchObject();
760 $record = $store->newRevisionFromArchiveRow( $row );
762 $this->assertRevisionRecordMatchesRevision( $orig, $record );
763 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
767 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
769 public function testNewRevisionFromArchiveRow_legacyEncoding() {
770 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
771 $this->overrideMwServices();
772 $store = MediaWikiServices
::getInstance()->getRevisionStore();
773 $title = Title
::newFromText( __METHOD__
);
774 $text = __METHOD__
. '-bä';
775 $page = WikiPage
::factory( $title );
776 /** @var Revision $orig */
777 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
779 $page->doDeleteArticle( __METHOD__
);
781 $db = wfGetDB( DB_MASTER
);
782 $arQuery = $store->getArchiveQueryInfo();
784 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
785 __METHOD__
, [], $arQuery['joins']
787 $this->assertTrue( is_object( $res ), 'query failed' );
789 $row = $res->fetchObject();
791 $record = $store->newRevisionFromArchiveRow( $row );
793 $this->assertRevisionRecordMatchesRevision( $orig, $record );
794 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
798 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
800 public function testLoadRevisionFromId() {
801 $title = Title
::newFromText( __METHOD__
);
802 $page = WikiPage
::factory( $title );
803 /** @var Revision $rev */
804 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
807 $store = MediaWikiServices
::getInstance()->getRevisionStore();
808 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
809 $this->assertRevisionRecordMatchesRevision( $rev, $result );
813 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
815 public function testLoadRevisionFromPageId() {
816 $title = Title
::newFromText( __METHOD__
);
817 $page = WikiPage
::factory( $title );
818 /** @var Revision $rev */
819 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
822 $store = MediaWikiServices
::getInstance()->getRevisionStore();
823 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
824 $this->assertRevisionRecordMatchesRevision( $rev, $result );
828 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
830 public function testLoadRevisionFromTitle() {
831 $title = Title
::newFromText( __METHOD__
);
832 $page = WikiPage
::factory( $title );
833 /** @var Revision $rev */
834 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
837 $store = MediaWikiServices
::getInstance()->getRevisionStore();
838 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
839 $this->assertRevisionRecordMatchesRevision( $rev, $result );
843 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
845 public function testLoadRevisionFromTimestamp() {
846 $title = Title
::newFromText( __METHOD__
);
847 $page = WikiPage
::factory( $title );
848 /** @var Revision $revOne */
849 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
851 // Sleep to ensure different timestamps... )(evil)
853 /** @var Revision $revTwo */
854 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
857 $store = MediaWikiServices
::getInstance()->getRevisionStore();
859 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
863 $store->loadRevisionFromTimestamp(
864 wfGetDB( DB_MASTER
),
866 $revOne->getTimestamp()
871 $store->loadRevisionFromTimestamp(
872 wfGetDB( DB_MASTER
),
874 $revTwo->getTimestamp()
880 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
882 public function testGetParentLengths() {
883 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
884 /** @var Revision $revOne */
885 $revOne = $page->doEditContent(
886 new WikitextContent( __METHOD__
), __METHOD__
887 )->value
['revision'];
888 /** @var Revision $revTwo */
889 $revTwo = $page->doEditContent(
890 new WikitextContent( __METHOD__
. '2' ), __METHOD__
891 )->value
['revision'];
893 $store = MediaWikiServices
::getInstance()->getRevisionStore();
896 $revOne->getId() => strlen( __METHOD__
),
898 $store->listRevisionSizes(
899 wfGetDB( DB_MASTER
),
905 $revOne->getId() => strlen( __METHOD__
),
906 $revTwo->getId() => strlen( __METHOD__
) +
1,
908 $store->listRevisionSizes(
909 wfGetDB( DB_MASTER
),
910 [ $revOne->getId(), $revTwo->getId() ]
916 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
918 public function testGetPreviousRevision() {
919 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
920 /** @var Revision $revOne */
921 $revOne = $page->doEditContent(
922 new WikitextContent( __METHOD__
), __METHOD__
923 )->value
['revision'];
924 /** @var Revision $revTwo */
925 $revTwo = $page->doEditContent(
926 new WikitextContent( __METHOD__
. '2' ), __METHOD__
927 )->value
['revision'];
929 $store = MediaWikiServices
::getInstance()->getRevisionStore();
931 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
935 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
940 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
942 public function testGetNextRevision() {
943 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
944 /** @var Revision $revOne */
945 $revOne = $page->doEditContent(
946 new WikitextContent( __METHOD__
), __METHOD__
947 )->value
['revision'];
948 /** @var Revision $revTwo */
949 $revTwo = $page->doEditContent(
950 new WikitextContent( __METHOD__
. '2' ), __METHOD__
951 )->value
['revision'];
953 $store = MediaWikiServices
::getInstance()->getRevisionStore();
956 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
959 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
964 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
966 public function testGetTimestampFromId_found() {
967 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
968 /** @var Revision $rev */
969 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
972 $store = MediaWikiServices
::getInstance()->getRevisionStore();
973 $result = $store->getTimestampFromId(
978 $this->assertSame( $rev->getTimestamp(), $result );
982 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
984 public function testGetTimestampFromId_notFound() {
985 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
986 /** @var Revision $rev */
987 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
990 $store = MediaWikiServices
::getInstance()->getRevisionStore();
991 $result = $store->getTimestampFromId(
996 $this->assertFalse( $result );
1000 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1002 public function testCountRevisionsByPageId() {
1003 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1004 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1008 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1010 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1013 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1015 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1018 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1023 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1025 public function testCountRevisionsByTitle() {
1026 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1027 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1031 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1033 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1036 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1038 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1041 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1046 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1048 public function testUserWasLastToEdit_false() {
1049 $sysop = $this->getTestSysop()->getUser();
1050 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1051 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1053 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1054 $result = $store->userWasLastToEdit(
1055 wfGetDB( DB_MASTER
),
1060 $this->assertFalse( $result );
1064 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1066 public function testUserWasLastToEdit_true() {
1067 $startTime = wfTimestampNow();
1068 $sysop = $this->getTestSysop()->getUser();
1069 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1070 $page->doEditContent(
1071 new WikitextContent( __METHOD__
),
1078 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1079 $result = $store->userWasLastToEdit(
1080 wfGetDB( DB_MASTER
),
1085 $this->assertTrue( $result );
1089 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1091 public function testGetKnownCurrentRevision() {
1092 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1093 /** @var Revision $rev */
1094 $rev = $page->doEditContent(
1095 new WikitextContent( __METHOD__
. 'b' ),
1099 $this->getTestUser()->getUser()
1100 )->value
['revision'];
1102 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1103 $record = $store->getKnownCurrentRevision(
1108 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1111 public function provideNewMutableRevisionFromArray() {
1112 yield
'Basic array, with page & id' => [
1117 'timestamp' => '20171017114835',
1118 'user_text' => '111.0.1.2',
1120 'minor_edit' => false,
1124 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1125 'comment' => 'Goat Comment!',
1126 'content_format' => 'text/x-wiki',
1127 'content_model' => 'wikitext',
1130 yield
'Basic array, content object' => [
1134 'timestamp' => '20171017114835',
1135 'user_text' => '111.0.1.2',
1137 'minor_edit' => false,
1141 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1142 'comment' => 'Goat Comment!',
1143 'content' => new WikitextContent( 'Some Content' ),
1146 yield
'Basic array, serialized text' => [
1150 'timestamp' => '20171017114835',
1151 'user_text' => '111.0.1.2',
1153 'minor_edit' => false,
1157 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1158 'comment' => 'Goat Comment!',
1159 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1162 yield
'Basic array, serialized text, utf-8 flags' => [
1166 'timestamp' => '20171017114835',
1167 'user_text' => '111.0.1.2',
1169 'minor_edit' => false,
1173 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1174 'comment' => 'Goat Comment!',
1175 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1179 yield
'Basic array, with title' => [
1181 'title' => Title
::newFromText( 'SomeText' ),
1183 'timestamp' => '20171017114835',
1184 'user_text' => '111.0.1.2',
1186 'minor_edit' => false,
1190 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1191 'comment' => 'Goat Comment!',
1192 'content_format' => 'text/x-wiki',
1193 'content_model' => 'wikitext',
1196 yield
'Basic array, no user field' => [
1201 'timestamp' => '20171017114835',
1202 'user_text' => '111.0.1.3',
1203 'minor_edit' => false,
1207 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1208 'comment' => 'Goat Comment!',
1209 'content_format' => 'text/x-wiki',
1210 'content_model' => 'wikitext',
1216 * @dataProvider provideNewMutableRevisionFromArray
1217 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1219 public function testNewMutableRevisionFromArray( array $array ) {
1220 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1222 $result = $store->newMutableRevisionFromArray( $array );
1224 if ( isset( $array['id'] ) ) {
1225 $this->assertSame( $array['id'], $result->getId() );
1227 if ( isset( $array['page'] ) ) {
1228 $this->assertSame( $array['page'], $result->getPageId() );
1230 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1231 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1232 if ( isset( $array['user'] ) ) {
1233 $this->assertSame( $array['user'], $result->getUser()->getId() );
1235 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1236 $this->assertSame( $array['deleted'], $result->getVisibility() );
1237 $this->assertSame( $array['len'], $result->getSize() );
1238 $this->assertSame( $array['parent_id'], $result->getParentId() );
1239 $this->assertSame( $array['sha1'], $result->getSha1() );
1240 $this->assertSame( $array['comment'], $result->getComment()->text
);
1241 if ( isset( $array['content'] ) ) {
1243 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1245 } elseif ( isset( $array['text'] ) ) {
1246 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1249 $array['content_format'],
1250 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1252 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1257 * @dataProvider provideNewMutableRevisionFromArray
1258 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1260 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1261 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1262 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1263 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1265 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1266 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1267 ->disableOriginalConstructor()
1269 $factory->expects( $this->any() )
1270 ->method( 'newBlobStore' )
1271 ->willReturn( $blobStore );
1272 $factory->expects( $this->any() )
1273 ->method( 'newSqlBlobStore' )
1274 ->willReturn( $blobStore );
1276 $this->setService( 'BlobStoreFactory', $factory );
1278 $this->testNewMutableRevisionFromArray( $array );