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' => new \Psr\Log\
NullLogger(),
123 'tablePrefix' => $dbPrefix,
126 $db = $loadBalancer->getConnection( DB_REPLICA
);
128 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
129 ->disableOriginalConstructor()
132 $store = new RevisionStore(
135 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
136 MediaWikiServices
::getInstance()->getCommentStore(),
137 MediaWikiServices
::getInstance()->getActorMigration(),
141 $count = $store->countRevisionsByPageId( $db, 0 );
143 // Dummy check to make PhpUnit happy. We are really only interested in
144 // countRevisionsByPageId not failing due to the DB domain check.
145 $this->assertSame( 0, $count );
148 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
149 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
150 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
151 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
152 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
155 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
156 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
157 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
158 $this->assertEquals( $r1->getComment(), $r2->getComment() );
159 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
160 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
161 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
162 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
163 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
164 $this->assertEquals( $r1->getSize(), $r2->getSize() );
165 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
166 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
167 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
168 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
169 foreach ( $r1->getSlotRoles() as $role ) {
170 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
171 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
174 RevisionRecord
::DELETED_TEXT
,
175 RevisionRecord
::DELETED_COMMENT
,
176 RevisionRecord
::DELETED_USER
,
177 RevisionRecord
::DELETED_RESTRICTED
,
179 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
183 private function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
184 $this->assertSame( $s1->getRole(), $s2->getRole() );
185 $this->assertSame( $s1->getModel(), $s2->getModel() );
186 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
187 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
188 $this->assertSame( $s1->getSize(), $s2->getSize() );
189 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
191 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
192 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
195 private function assertRevisionCompleteness( RevisionRecord
$r ) {
196 foreach ( $r->getSlotRoles() as $role ) {
197 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
201 private function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
202 $this->assertTrue( $slot->hasAddress() );
203 $this->assertSame( $r->getId(), $slot->getRevision() );
207 * @param mixed[] $details
209 * @return RevisionRecord
211 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
212 // Convert some values that can't be provided by dataProviders
213 $page = WikiPage
::factory( $title );
214 if ( isset( $details['user'] ) && $details['user'] === true ) {
215 $details['user'] = $this->getTestUser()->getUser();
217 if ( isset( $details['page'] ) && $details['page'] === true ) {
218 $details['page'] = $page->getId();
220 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
221 $details['parent'] = $page->getLatest();
224 // Create the RevisionRecord with any available data
225 $rev = new MutableRevisionRecord( $title );
226 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
227 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
228 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
229 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
230 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
231 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
232 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
233 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
234 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
235 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
236 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
241 private function getRandomCommentStoreComment() {
242 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
245 public function provideInsertRevisionOn_successes() {
246 yield
'Bare minimum revision insertion' => [
247 Title
::newFromText( 'UTPage' ),
249 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
251 'comment' => $this->getRandomCommentStoreComment(),
252 'timestamp' => '20171117010101',
256 yield
'Detailed revision insertion' => [
257 Title
::newFromText( 'UTPage' ),
259 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
262 'comment' => $this->getRandomCommentStoreComment(),
263 'timestamp' => '20171117010101',
266 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
272 * @dataProvider provideInsertRevisionOn_successes
273 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
275 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
276 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
278 $store = MediaWikiServices
::getInstance()->getRevisionStore();
279 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
281 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
282 $this->assertRevisionRecordsEqual( $rev, $return );
283 $this->assertRevisionCompleteness( $return );
287 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
289 public function testInsertRevisionOn_blobAddressExists() {
290 $title = Title
::newFromText( 'UTPage' );
292 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
294 'comment' => $this->getRandomCommentStoreComment(),
295 'timestamp' => '20171117010101',
299 $store = MediaWikiServices
::getInstance()->getRevisionStore();
301 // Insert the first revision
302 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
303 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
304 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
305 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
307 // Insert a second revision inheriting the same blob address
308 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
309 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
310 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
311 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
312 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
314 // Assert that the same blob address has been used.
316 $firstReturn->getSlot( 'main' )->getAddress(),
317 $secondReturn->getSlot( 'main' )->getAddress()
319 // And that different revisions have been created.
320 $this->assertNotSame(
321 $firstReturn->getId(),
322 $secondReturn->getId()
326 public function provideInsertRevisionOn_failures() {
328 Title
::newFromText( 'UTPage' ),
330 'comment' => $this->getRandomCommentStoreComment(),
331 'timestamp' => '20171117010101',
334 new InvalidArgumentException( 'At least one slot needs to be defined!' )
336 yield
'slot that is not main slot' => [
337 Title
::newFromText( 'UTPage' ),
339 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
340 'comment' => $this->getRandomCommentStoreComment(),
341 'timestamp' => '20171117010101',
344 new InvalidArgumentException( 'Only the main slot is supported for now!' )
346 yield
'no timestamp' => [
347 Title
::newFromText( 'UTPage' ),
349 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
350 'comment' => $this->getRandomCommentStoreComment(),
353 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
355 yield
'no comment' => [
356 Title
::newFromText( 'UTPage' ),
358 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
359 'timestamp' => '20171117010101',
362 new IncompleteRevisionException( 'comment must not be NULL!' )
365 Title
::newFromText( 'UTPage' ),
367 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
368 'comment' => $this->getRandomCommentStoreComment(),
369 'timestamp' => '20171117010101',
371 new IncompleteRevisionException( 'user must not be NULL!' )
376 * @dataProvider provideInsertRevisionOn_failures
377 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
379 public function testInsertRevisionOn_failures(
381 array $revDetails = [],
382 Exception
$exception ) {
383 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
385 $store = MediaWikiServices
::getInstance()->getRevisionStore();
387 $this->setExpectedException(
388 get_class( $exception ),
389 $exception->getMessage(),
390 $exception->getCode()
392 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
395 public function provideNewNullRevision() {
397 Title
::newFromText( 'UTPage' ),
398 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
402 Title
::newFromText( 'UTPage' ),
403 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
409 * @dataProvider provideNewNullRevision
410 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
412 public function testNewNullRevision( Title
$title, $comment, $minor ) {
413 $store = MediaWikiServices
::getInstance()->getRevisionStore();
414 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
415 $record = $store->newNullRevision(
416 wfGetDB( DB_MASTER
),
423 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
424 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
425 $this->assertEquals( $comment, $record->getComment() );
426 $this->assertEquals( $minor, $record->isMinor() );
427 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
431 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
433 public function testNewNullRevision_nonExistingTitle() {
434 $store = MediaWikiServices
::getInstance()->getRevisionStore();
435 $record = $store->newNullRevision(
436 wfGetDB( DB_MASTER
),
437 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
438 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
440 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
442 $this->assertNull( $record );
446 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
448 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
449 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
450 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
451 /** @var Revision $rev */
452 $rev = $status->value
['revision'];
454 $store = MediaWikiServices
::getInstance()->getRevisionStore();
455 $revisionRecord = $store->getRevisionById( $rev->getId() );
456 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
458 $this->assertGreaterThan( 0, $result );
460 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
466 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
468 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
469 // This assumes that sysops are auto patrolled
470 $sysop = $this->getTestSysop()->getUser();
471 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
472 $status = $page->doEditContent(
473 new WikitextContent( __METHOD__
),
479 /** @var Revision $rev */
480 $rev = $status->value
['revision'];
482 $store = MediaWikiServices
::getInstance()->getRevisionStore();
483 $revisionRecord = $store->getRevisionById( $rev->getId() );
484 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
486 $this->assertSame( 0, $result );
490 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
492 public function testGetRecentChange() {
493 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
494 $content = new WikitextContent( __METHOD__
);
495 $status = $page->doEditContent( $content, __METHOD__
);
496 /** @var Revision $rev */
497 $rev = $status->value
['revision'];
499 $store = MediaWikiServices
::getInstance()->getRevisionStore();
500 $revRecord = $store->getRevisionById( $rev->getId() );
501 $recentChange = $store->getRecentChange( $revRecord );
503 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
504 $this->assertEquals( $rev->getRecentChange(), $recentChange );
508 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
510 public function testGetRevisionById() {
511 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
512 $content = new WikitextContent( __METHOD__
);
513 $status = $page->doEditContent( $content, __METHOD__
);
514 /** @var Revision $rev */
515 $rev = $status->value
['revision'];
517 $store = MediaWikiServices
::getInstance()->getRevisionStore();
518 $revRecord = $store->getRevisionById( $rev->getId() );
520 $this->assertSame( $rev->getId(), $revRecord->getId() );
521 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
522 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
526 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
528 public function testGetRevisionByTitle() {
529 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
530 $content = new WikitextContent( __METHOD__
);
531 $status = $page->doEditContent( $content, __METHOD__
);
532 /** @var Revision $rev */
533 $rev = $status->value
['revision'];
535 $store = MediaWikiServices
::getInstance()->getRevisionStore();
536 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
538 $this->assertSame( $rev->getId(), $revRecord->getId() );
539 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
540 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
544 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
546 public function testGetRevisionByPageId() {
547 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
548 $content = new WikitextContent( __METHOD__
);
549 $status = $page->doEditContent( $content, __METHOD__
);
550 /** @var Revision $rev */
551 $rev = $status->value
['revision'];
553 $store = MediaWikiServices
::getInstance()->getRevisionStore();
554 $revRecord = $store->getRevisionByPageId( $page->getId() );
556 $this->assertSame( $rev->getId(), $revRecord->getId() );
557 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
558 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
562 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
564 public function testGetRevisionByTimestamp() {
565 // Make sure there is 1 second between the last revision and the rev we create...
566 // Otherwise we might not get the correct revision and the test may fail...
569 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
570 $content = new WikitextContent( __METHOD__
);
571 $status = $page->doEditContent( $content, __METHOD__
);
572 /** @var Revision $rev */
573 $rev = $status->value
['revision'];
575 $store = MediaWikiServices
::getInstance()->getRevisionStore();
576 $revRecord = $store->getRevisionByTimestamp(
581 $this->assertSame( $rev->getId(), $revRecord->getId() );
582 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
583 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
586 private function revisionToRow( Revision
$rev ) {
587 $page = WikiPage
::factory( $rev->getTitle() );
590 'rev_id' => (string)$rev->getId(),
591 'rev_page' => (string)$rev->getPage(),
592 'rev_text_id' => (string)$rev->getTextId(),
593 'rev_timestamp' => (string)$rev->getTimestamp(),
594 'rev_user_text' => (string)$rev->getUserText(),
595 'rev_user' => (string)$rev->getUser(),
596 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
597 'rev_deleted' => (string)$rev->getVisibility(),
598 'rev_len' => (string)$rev->getSize(),
599 'rev_parent_id' => (string)$rev->getParentId(),
600 'rev_sha1' => (string)$rev->getSha1(),
601 'rev_comment_text' => $rev->getComment(),
602 'rev_comment_data' => null,
603 'rev_comment_cid' => null,
604 'rev_content_format' => $rev->getContentFormat(),
605 'rev_content_model' => $rev->getContentModel(),
606 'page_namespace' => (string)$page->getTitle()->getNamespace(),
607 'page_title' => $page->getTitle()->getDBkey(),
608 'page_id' => (string)$page->getId(),
609 'page_latest' => (string)$page->getLatest(),
610 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
611 'page_len' => (string)$page->getContent()->getSize(),
612 'user_name' => (string)$rev->getUserText(),
616 private function assertRevisionRecordMatchesRevision(
618 RevisionRecord
$record
620 $this->assertSame( $rev->getId(), $record->getId() );
621 $this->assertSame( $rev->getPage(), $record->getPageId() );
622 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
623 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
624 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
625 $this->assertSame( $rev->isMinor(), $record->isMinor() );
626 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
627 $this->assertSame( $rev->getSize(), $record->getSize() );
629 * @note As of MW 1.31, the database schema allows the parent ID to be
630 * NULL to indicate that it is unknown.
632 $expectedParent = $rev->getParentId();
633 if ( $expectedParent === null ) {
636 $this->assertSame( $expectedParent, $record->getParentId() );
637 $this->assertSame( $rev->getSha1(), $record->getSha1() );
638 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
639 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
640 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
641 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
645 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
646 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
648 public function testNewRevisionFromRow_anonEdit() {
649 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
650 $this->overrideMwServices();
652 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
653 $text = __METHOD__
. 'a-ä';
654 /** @var Revision $rev */
655 $rev = $page->doEditContent(
656 new WikitextContent( $text ),
658 )->value
['revision'];
660 $store = MediaWikiServices
::getInstance()->getRevisionStore();
661 $record = $store->newRevisionFromRow(
662 $this->revisionToRow( $rev ),
666 $this->assertRevisionRecordMatchesRevision( $rev, $record );
667 $this->assertSame( $text, $rev->getContent()->serialize() );
671 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
672 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
674 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
675 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
676 $this->overrideMwServices();
677 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
678 $text = __METHOD__
. 'a-ä';
679 /** @var Revision $rev */
680 $rev = $page->doEditContent(
681 new WikitextContent( $text ),
683 )->value
['revision'];
685 $store = MediaWikiServices
::getInstance()->getRevisionStore();
686 $record = $store->newRevisionFromRow(
687 $this->revisionToRow( $rev ),
691 $this->assertRevisionRecordMatchesRevision( $rev, $record );
692 $this->assertSame( $text, $rev->getContent()->serialize() );
696 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
697 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
699 public function testNewRevisionFromRow_userEdit() {
700 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
701 $this->overrideMwServices();
703 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
704 $text = __METHOD__
. 'b-ä';
705 /** @var Revision $rev */
706 $rev = $page->doEditContent(
707 new WikitextContent( $text ),
711 $this->getTestUser()->getUser()
712 )->value
['revision'];
714 $store = MediaWikiServices
::getInstance()->getRevisionStore();
715 $record = $store->newRevisionFromRow(
716 $this->revisionToRow( $rev ),
720 $this->assertRevisionRecordMatchesRevision( $rev, $record );
721 $this->assertSame( $text, $rev->getContent()->serialize() );
725 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
727 public function testNewRevisionFromArchiveRow() {
728 $store = MediaWikiServices
::getInstance()->getRevisionStore();
729 $title = Title
::newFromText( __METHOD__
);
730 $text = __METHOD__
. '-bä';
731 $page = WikiPage
::factory( $title );
732 /** @var Revision $orig */
733 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
735 $page->doDeleteArticle( __METHOD__
);
737 $db = wfGetDB( DB_MASTER
);
738 $arQuery = $store->getArchiveQueryInfo();
740 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
741 __METHOD__
, [], $arQuery['joins']
743 $this->assertTrue( is_object( $res ), 'query failed' );
745 $row = $res->fetchObject();
747 $record = $store->newRevisionFromArchiveRow( $row );
749 $this->assertRevisionRecordMatchesRevision( $orig, $record );
750 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
754 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
756 public function testNewRevisionFromArchiveRow_legacyEncoding() {
757 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
758 $this->overrideMwServices();
759 $store = MediaWikiServices
::getInstance()->getRevisionStore();
760 $title = Title
::newFromText( __METHOD__
);
761 $text = __METHOD__
. '-bä';
762 $page = WikiPage
::factory( $title );
763 /** @var Revision $orig */
764 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
766 $page->doDeleteArticle( __METHOD__
);
768 $db = wfGetDB( DB_MASTER
);
769 $arQuery = $store->getArchiveQueryInfo();
771 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
772 __METHOD__
, [], $arQuery['joins']
774 $this->assertTrue( is_object( $res ), 'query failed' );
776 $row = $res->fetchObject();
778 $record = $store->newRevisionFromArchiveRow( $row );
780 $this->assertRevisionRecordMatchesRevision( $orig, $record );
781 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
785 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
787 public function testLoadRevisionFromId() {
788 $title = Title
::newFromText( __METHOD__
);
789 $page = WikiPage
::factory( $title );
790 /** @var Revision $rev */
791 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
794 $store = MediaWikiServices
::getInstance()->getRevisionStore();
795 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
796 $this->assertRevisionRecordMatchesRevision( $rev, $result );
800 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
802 public function testLoadRevisionFromPageId() {
803 $title = Title
::newFromText( __METHOD__
);
804 $page = WikiPage
::factory( $title );
805 /** @var Revision $rev */
806 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
809 $store = MediaWikiServices
::getInstance()->getRevisionStore();
810 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
811 $this->assertRevisionRecordMatchesRevision( $rev, $result );
815 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
817 public function testLoadRevisionFromTitle() {
818 $title = Title
::newFromText( __METHOD__
);
819 $page = WikiPage
::factory( $title );
820 /** @var Revision $rev */
821 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
824 $store = MediaWikiServices
::getInstance()->getRevisionStore();
825 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
826 $this->assertRevisionRecordMatchesRevision( $rev, $result );
830 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
832 public function testLoadRevisionFromTimestamp() {
833 $title = Title
::newFromText( __METHOD__
);
834 $page = WikiPage
::factory( $title );
835 /** @var Revision $revOne */
836 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
838 // Sleep to ensure different timestamps... )(evil)
840 /** @var Revision $revTwo */
841 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
844 $store = MediaWikiServices
::getInstance()->getRevisionStore();
846 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
850 $store->loadRevisionFromTimestamp(
851 wfGetDB( DB_MASTER
),
853 $revOne->getTimestamp()
858 $store->loadRevisionFromTimestamp(
859 wfGetDB( DB_MASTER
),
861 $revTwo->getTimestamp()
867 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
869 public function testGetParentLengths() {
870 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
871 /** @var Revision $revOne */
872 $revOne = $page->doEditContent(
873 new WikitextContent( __METHOD__
), __METHOD__
874 )->value
['revision'];
875 /** @var Revision $revTwo */
876 $revTwo = $page->doEditContent(
877 new WikitextContent( __METHOD__
. '2' ), __METHOD__
878 )->value
['revision'];
880 $store = MediaWikiServices
::getInstance()->getRevisionStore();
883 $revOne->getId() => strlen( __METHOD__
),
885 $store->listRevisionSizes(
886 wfGetDB( DB_MASTER
),
892 $revOne->getId() => strlen( __METHOD__
),
893 $revTwo->getId() => strlen( __METHOD__
) +
1,
895 $store->listRevisionSizes(
896 wfGetDB( DB_MASTER
),
897 [ $revOne->getId(), $revTwo->getId() ]
903 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
905 public function testGetPreviousRevision() {
906 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
907 /** @var Revision $revOne */
908 $revOne = $page->doEditContent(
909 new WikitextContent( __METHOD__
), __METHOD__
910 )->value
['revision'];
911 /** @var Revision $revTwo */
912 $revTwo = $page->doEditContent(
913 new WikitextContent( __METHOD__
. '2' ), __METHOD__
914 )->value
['revision'];
916 $store = MediaWikiServices
::getInstance()->getRevisionStore();
918 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
922 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
927 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
929 public function testGetNextRevision() {
930 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
931 /** @var Revision $revOne */
932 $revOne = $page->doEditContent(
933 new WikitextContent( __METHOD__
), __METHOD__
934 )->value
['revision'];
935 /** @var Revision $revTwo */
936 $revTwo = $page->doEditContent(
937 new WikitextContent( __METHOD__
. '2' ), __METHOD__
938 )->value
['revision'];
940 $store = MediaWikiServices
::getInstance()->getRevisionStore();
943 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
946 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
951 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
953 public function testGetTimestampFromId_found() {
954 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
955 /** @var Revision $rev */
956 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
959 $store = MediaWikiServices
::getInstance()->getRevisionStore();
960 $result = $store->getTimestampFromId(
965 $this->assertSame( $rev->getTimestamp(), $result );
969 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
971 public function testGetTimestampFromId_notFound() {
972 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
973 /** @var Revision $rev */
974 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
977 $store = MediaWikiServices
::getInstance()->getRevisionStore();
978 $result = $store->getTimestampFromId(
983 $this->assertFalse( $result );
987 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
989 public function testCountRevisionsByPageId() {
990 $store = MediaWikiServices
::getInstance()->getRevisionStore();
991 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
995 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
997 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1000 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1002 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1005 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1010 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1012 public function testCountRevisionsByTitle() {
1013 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1014 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
1018 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1020 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1023 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1025 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1028 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1033 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1035 public function testUserWasLastToEdit_false() {
1036 $sysop = $this->getTestSysop()->getUser();
1037 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1038 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1040 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1041 $result = $store->userWasLastToEdit(
1042 wfGetDB( DB_MASTER
),
1047 $this->assertFalse( $result );
1051 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1053 public function testUserWasLastToEdit_true() {
1054 $startTime = wfTimestampNow();
1055 $sysop = $this->getTestSysop()->getUser();
1056 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1057 $page->doEditContent(
1058 new WikitextContent( __METHOD__
),
1065 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1066 $result = $store->userWasLastToEdit(
1067 wfGetDB( DB_MASTER
),
1072 $this->assertTrue( $result );
1076 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1078 public function testGetKnownCurrentRevision() {
1079 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1080 /** @var Revision $rev */
1081 $rev = $page->doEditContent(
1082 new WikitextContent( __METHOD__
. 'b' ),
1086 $this->getTestUser()->getUser()
1087 )->value
['revision'];
1089 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1090 $record = $store->getKnownCurrentRevision(
1095 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1098 public function provideNewMutableRevisionFromArray() {
1099 yield
'Basic array, with page & id' => [
1104 'timestamp' => '20171017114835',
1105 'user_text' => '111.0.1.2',
1107 'minor_edit' => false,
1111 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1112 'comment' => 'Goat Comment!',
1113 'content_format' => 'text/x-wiki',
1114 'content_model' => 'wikitext',
1117 yield
'Basic array, content object' => [
1121 'timestamp' => '20171017114835',
1122 'user_text' => '111.0.1.2',
1124 'minor_edit' => false,
1128 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1129 'comment' => 'Goat Comment!',
1130 'content' => new WikitextContent( 'Some Content' ),
1133 yield
'Basic array, serialized text' => [
1137 'timestamp' => '20171017114835',
1138 'user_text' => '111.0.1.2',
1140 'minor_edit' => false,
1144 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1145 'comment' => 'Goat Comment!',
1146 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1149 yield
'Basic array, serialized text, utf-8 flags' => [
1153 'timestamp' => '20171017114835',
1154 'user_text' => '111.0.1.2',
1156 'minor_edit' => false,
1160 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1161 'comment' => 'Goat Comment!',
1162 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1166 yield
'Basic array, with title' => [
1168 'title' => Title
::newFromText( 'SomeText' ),
1170 'timestamp' => '20171017114835',
1171 'user_text' => '111.0.1.2',
1173 'minor_edit' => false,
1177 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1178 'comment' => 'Goat Comment!',
1179 'content_format' => 'text/x-wiki',
1180 'content_model' => 'wikitext',
1183 yield
'Basic array, no user field' => [
1188 'timestamp' => '20171017114835',
1189 'user_text' => '111.0.1.3',
1190 'minor_edit' => false,
1194 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1195 'comment' => 'Goat Comment!',
1196 'content_format' => 'text/x-wiki',
1197 'content_model' => 'wikitext',
1203 * @dataProvider provideNewMutableRevisionFromArray
1204 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1206 public function testNewMutableRevisionFromArray( array $array ) {
1207 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1209 $result = $store->newMutableRevisionFromArray( $array );
1211 if ( isset( $array['id'] ) ) {
1212 $this->assertSame( $array['id'], $result->getId() );
1214 if ( isset( $array['page'] ) ) {
1215 $this->assertSame( $array['page'], $result->getPageId() );
1217 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1218 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1219 if ( isset( $array['user'] ) ) {
1220 $this->assertSame( $array['user'], $result->getUser()->getId() );
1222 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1223 $this->assertSame( $array['deleted'], $result->getVisibility() );
1224 $this->assertSame( $array['len'], $result->getSize() );
1225 $this->assertSame( $array['parent_id'], $result->getParentId() );
1226 $this->assertSame( $array['sha1'], $result->getSha1() );
1227 $this->assertSame( $array['comment'], $result->getComment()->text
);
1228 if ( isset( $array['content'] ) ) {
1230 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1232 } elseif ( isset( $array['text'] ) ) {
1233 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1236 $array['content_format'],
1237 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1239 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1244 * @dataProvider provideNewMutableRevisionFromArray
1245 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1247 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1248 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1249 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1250 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1252 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1253 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1254 ->disableOriginalConstructor()
1256 $factory->expects( $this->any() )
1257 ->method( 'newBlobStore' )
1258 ->willReturn( $blobStore );
1259 $factory->expects( $this->any() )
1260 ->method( 'newSqlBlobStore' )
1261 ->willReturn( $blobStore );
1263 $this->setService( 'BlobStoreFactory', $factory );
1265 $this->testNewMutableRevisionFromArray( $array );