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\DatabaseDomain
;
26 use Wikimedia\Rdbms\DatabaseSqlite
;
27 use Wikimedia\Rdbms\FakeResultWrapper
;
28 use Wikimedia\Rdbms\LoadBalancer
;
29 use Wikimedia\Rdbms\TransactionProfiler
;
36 class RevisionStoreDbTest
extends MediaWikiTestCase
{
39 * @return LoadBalancer
41 private function getLoadBalancerMock( array $server ) {
42 $lb = $this->getMockBuilder( LoadBalancer
::class )
43 ->setMethods( [ 'reallyOpenConnection' ] )
44 ->setConstructorArgs( [ [
45 'servers' => [ $server ],
46 'localDomain' => new DatabaseDomain(
47 $server['dbname'], null, $server['tablePrefix']
52 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
53 function ( array $server, $dbNameOverride ) {
54 return $this->getDatabaseMock( $server );
64 private function getDatabaseMock( array $params ) {
65 $db = $this->getMockBuilder( DatabaseSqlite
::class )
66 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
67 ->setConstructorArgs( [ $params ] )
70 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
71 $db->method( 'isOpen' )->willReturn( true );
76 public function provideDomainCheck() {
77 yield
[ false, 'test', '' ];
78 yield
[ 'test', 'test', '' ];
80 yield
[ false, 'test', 'foo_' ];
81 yield
[ 'test-foo_', 'test', 'foo_' ];
83 yield
[ false, 'dash-test', '' ];
84 yield
[ 'dash-test', 'dash-test', '' ];
86 yield
[ false, 'underscore_test', 'foo_' ];
87 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
91 * @dataProvider provideDomainCheck
92 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
94 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
97 'wgDBname' => $dbName,
98 'wgDBprefix' => $dbPrefix,
102 $loadBalancer = $this->getLoadBalancerMock(
105 'dbDirectory' => '*dummy*',
107 'password' => 'test',
115 'trxProfiler' => new TransactionProfiler(),
116 'connLogger' => new \Psr\Log\
NullLogger(),
117 'queryLogger' => new \Psr\Log\
NullLogger(),
118 'errorLogger' => new \Psr\Log\
NullLogger(),
121 'tablePrefix' => $dbPrefix,
124 $db = $loadBalancer->getConnection( DB_REPLICA
);
126 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
127 ->disableOriginalConstructor()
130 $store = new RevisionStore(
133 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
137 $count = $store->countRevisionsByPageId( $db, 0 );
139 // Dummy check to make PhpUnit happy. We are really only interested in
140 // countRevisionsByPageId not failing due to the DB domain check.
141 $this->assertSame( 0, $count );
144 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
145 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
146 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
147 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
148 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
151 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
152 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
153 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
154 $this->assertEquals( $r1->getComment(), $r2->getComment() );
155 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
156 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
157 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
158 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
159 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
160 $this->assertEquals( $r1->getSize(), $r2->getSize() );
161 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
162 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
163 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
164 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
165 foreach ( $r1->getSlotRoles() as $role ) {
166 $this->assertEquals( $r1->getSlot( $role ), $r2->getSlot( $role ) );
167 $this->assertEquals( $r1->getContent( $role ), $r2->getContent( $role ) );
170 RevisionRecord
::DELETED_TEXT
,
171 RevisionRecord
::DELETED_COMMENT
,
172 RevisionRecord
::DELETED_USER
,
173 RevisionRecord
::DELETED_RESTRICTED
,
175 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
180 * @param mixed[] $details
182 * @return RevisionRecord
184 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
185 // Convert some values that can't be provided by dataProviders
186 $page = WikiPage
::factory( $title );
187 if ( isset( $details['user'] ) && $details['user'] === true ) {
188 $details['user'] = $this->getTestUser()->getUser();
190 if ( isset( $details['page'] ) && $details['page'] === true ) {
191 $details['page'] = $page->getId();
193 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
194 $details['parent'] = $page->getLatest();
197 // Create the RevisionRecord with any available data
198 $rev = new MutableRevisionRecord( $title );
199 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
200 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
201 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
202 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
203 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
204 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
205 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
206 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
207 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
208 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
209 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
214 private function getRandomCommentStoreComment() {
215 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
218 public function provideInsertRevisionOn_successes() {
219 yield
'Bare minimum revision insertion' => [
220 Title
::newFromText( 'UTPage' ),
222 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
224 'comment' => $this->getRandomCommentStoreComment(),
225 'timestamp' => '20171117010101',
229 yield
'Detailed revision insertion' => [
230 Title
::newFromText( 'UTPage' ),
232 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
235 'comment' => $this->getRandomCommentStoreComment(),
236 'timestamp' => '20171117010101',
239 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
245 * @dataProvider provideInsertRevisionOn_successes
246 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
248 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
249 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
251 $store = MediaWikiServices
::getInstance()->getRevisionStore();
252 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
254 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
255 $this->assertRevisionRecordsEqual( $rev, $return );
259 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
261 public function testInsertRevisionOn_blobAddressExists() {
262 $title = Title
::newFromText( 'UTPage' );
264 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
266 'comment' => $this->getRandomCommentStoreComment(),
267 'timestamp' => '20171117010101',
271 $store = MediaWikiServices
::getInstance()->getRevisionStore();
273 // Insert the first revision
274 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
275 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
276 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
277 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
279 // Insert a second revision inheriting the same blob address
280 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
281 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
282 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
283 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
284 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
286 // Assert that the same blob address has been used.
288 $firstReturn->getSlot( 'main' )->getAddress(),
289 $secondReturn->getSlot( 'main' )->getAddress()
291 // And that different revisions have been created.
292 $this->assertNotSame(
293 $firstReturn->getId(),
294 $secondReturn->getId()
298 public function provideInsertRevisionOn_failures() {
300 Title
::newFromText( 'UTPage' ),
302 'comment' => $this->getRandomCommentStoreComment(),
303 'timestamp' => '20171117010101',
306 new InvalidArgumentException( 'At least one slot needs to be defined!' )
308 yield
'slot that is not main slot' => [
309 Title
::newFromText( 'UTPage' ),
311 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
312 'comment' => $this->getRandomCommentStoreComment(),
313 'timestamp' => '20171117010101',
316 new InvalidArgumentException( 'Only the main slot is supported for now!' )
318 yield
'no timestamp' => [
319 Title
::newFromText( 'UTPage' ),
321 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
322 'comment' => $this->getRandomCommentStoreComment(),
325 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
327 yield
'no comment' => [
328 Title
::newFromText( 'UTPage' ),
330 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
331 'timestamp' => '20171117010101',
334 new IncompleteRevisionException( 'comment must not be NULL!' )
337 Title
::newFromText( 'UTPage' ),
339 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
340 'comment' => $this->getRandomCommentStoreComment(),
341 'timestamp' => '20171117010101',
343 new IncompleteRevisionException( 'user must not be NULL!' )
348 * @dataProvider provideInsertRevisionOn_failures
349 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
351 public function testInsertRevisionOn_failures(
353 array $revDetails = [],
354 Exception
$exception ) {
355 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
357 $store = MediaWikiServices
::getInstance()->getRevisionStore();
359 $this->setExpectedException(
360 get_class( $exception ),
361 $exception->getMessage(),
362 $exception->getCode()
364 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
367 public function provideNewNullRevision() {
369 Title
::newFromText( 'UTPage' ),
370 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
374 Title
::newFromText( 'UTPage' ),
375 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
381 * @dataProvider provideNewNullRevision
382 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
384 public function testNewNullRevision( Title
$title, $comment, $minor ) {
385 $store = MediaWikiServices
::getInstance()->getRevisionStore();
386 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
387 $record = $store->newNullRevision(
388 wfGetDB( DB_MASTER
),
395 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
396 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
397 $this->assertEquals( $comment, $record->getComment() );
398 $this->assertEquals( $minor, $record->isMinor() );
399 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
403 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
405 public function testNewNullRevision_nonExistingTitle() {
406 $store = MediaWikiServices
::getInstance()->getRevisionStore();
407 $record = $store->newNullRevision(
408 wfGetDB( DB_MASTER
),
409 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
410 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
412 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
414 $this->assertNull( $record );
418 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
420 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
421 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
422 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
423 /** @var Revision $rev */
424 $rev = $status->value
['revision'];
426 $store = MediaWikiServices
::getInstance()->getRevisionStore();
427 $revisionRecord = $store->getRevisionById( $rev->getId() );
428 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
430 $this->assertGreaterThan( 0, $result );
432 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
438 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
440 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
441 // This assumes that sysops are auto patrolled
442 $sysop = $this->getTestSysop()->getUser();
443 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
444 $status = $page->doEditContent(
445 new WikitextContent( __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->assertSame( 0, $result );
462 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
464 public function testGetRecentChange() {
465 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
466 $content = new WikitextContent( __METHOD__
);
467 $status = $page->doEditContent( $content, __METHOD__
);
468 /** @var Revision $rev */
469 $rev = $status->value
['revision'];
471 $store = MediaWikiServices
::getInstance()->getRevisionStore();
472 $revRecord = $store->getRevisionById( $rev->getId() );
473 $recentChange = $store->getRecentChange( $revRecord );
475 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
476 $this->assertEquals( $rev->getRecentChange(), $recentChange );
480 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
482 public function testGetRevisionById() {
483 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
484 $content = new WikitextContent( __METHOD__
);
485 $status = $page->doEditContent( $content, __METHOD__
);
486 /** @var Revision $rev */
487 $rev = $status->value
['revision'];
489 $store = MediaWikiServices
::getInstance()->getRevisionStore();
490 $revRecord = $store->getRevisionById( $rev->getId() );
492 $this->assertSame( $rev->getId(), $revRecord->getId() );
493 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
494 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
498 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
500 public function testGetRevisionByTitle() {
501 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
502 $content = new WikitextContent( __METHOD__
);
503 $status = $page->doEditContent( $content, __METHOD__
);
504 /** @var Revision $rev */
505 $rev = $status->value
['revision'];
507 $store = MediaWikiServices
::getInstance()->getRevisionStore();
508 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
510 $this->assertSame( $rev->getId(), $revRecord->getId() );
511 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
512 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
516 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
518 public function testGetRevisionByPageId() {
519 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
520 $content = new WikitextContent( __METHOD__
);
521 $status = $page->doEditContent( $content, __METHOD__
);
522 /** @var Revision $rev */
523 $rev = $status->value
['revision'];
525 $store = MediaWikiServices
::getInstance()->getRevisionStore();
526 $revRecord = $store->getRevisionByPageId( $page->getId() );
528 $this->assertSame( $rev->getId(), $revRecord->getId() );
529 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
530 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
534 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
536 public function testGetRevisionByTimestamp() {
537 // Make sure there is 1 second between the last revision and the rev we create...
538 // Otherwise we might not get the correct revision and the test may fail...
541 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
542 $content = new WikitextContent( __METHOD__
);
543 $status = $page->doEditContent( $content, __METHOD__
);
544 /** @var Revision $rev */
545 $rev = $status->value
['revision'];
547 $store = MediaWikiServices
::getInstance()->getRevisionStore();
548 $revRecord = $store->getRevisionByTimestamp(
553 $this->assertSame( $rev->getId(), $revRecord->getId() );
554 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
555 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
558 private function revisionToRow( Revision
$rev ) {
559 $page = WikiPage
::factory( $rev->getTitle() );
562 'rev_id' => (string)$rev->getId(),
563 'rev_page' => (string)$rev->getPage(),
564 'rev_text_id' => (string)$rev->getTextId(),
565 'rev_timestamp' => (string)$rev->getTimestamp(),
566 'rev_user_text' => (string)$rev->getUserText(),
567 'rev_user' => (string)$rev->getUser(),
568 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
569 'rev_deleted' => (string)$rev->getVisibility(),
570 'rev_len' => (string)$rev->getSize(),
571 'rev_parent_id' => (string)$rev->getParentId(),
572 'rev_sha1' => (string)$rev->getSha1(),
573 'rev_comment_text' => $rev->getComment(),
574 'rev_comment_data' => null,
575 'rev_comment_cid' => null,
576 'rev_content_format' => $rev->getContentFormat(),
577 'rev_content_model' => $rev->getContentModel(),
578 'page_namespace' => (string)$page->getTitle()->getNamespace(),
579 'page_title' => $page->getTitle()->getDBkey(),
580 'page_id' => (string)$page->getId(),
581 'page_latest' => (string)$page->getLatest(),
582 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
583 'page_len' => (string)$page->getContent()->getSize(),
584 'user_name' => (string)$rev->getUserText(),
588 private function assertRevisionRecordMatchesRevision(
590 RevisionRecord
$record
592 $this->assertSame( $rev->getId(), $record->getId() );
593 $this->assertSame( $rev->getPage(), $record->getPageId() );
594 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
595 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
596 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
597 $this->assertSame( $rev->isMinor(), $record->isMinor() );
598 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
599 $this->assertSame( $rev->getSize(), $record->getSize() );
601 * @note As of MW 1.31, the database schema allows the parent ID to be
602 * NULL to indicate that it is unknown.
604 $expectedParent = $rev->getParentId();
605 if ( $expectedParent === null ) {
608 $this->assertSame( $expectedParent, $record->getParentId() );
609 $this->assertSame( $rev->getSha1(), $record->getSha1() );
610 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
611 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
612 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
613 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
617 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
618 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
620 public function testNewRevisionFromRow_anonEdit() {
621 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
622 $text = __METHOD__
. 'a-ä';
623 /** @var Revision $rev */
624 $rev = $page->doEditContent(
625 new WikitextContent( $text ),
627 )->value
['revision'];
629 $store = MediaWikiServices
::getInstance()->getRevisionStore();
630 $record = $store->newRevisionFromRow(
631 $this->revisionToRow( $rev ),
635 $this->assertRevisionRecordMatchesRevision( $rev, $record );
636 $this->assertSame( $text, $rev->getContent()->serialize() );
640 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
641 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
643 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
644 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
645 $this->overrideMwServices();
646 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
647 $text = __METHOD__
. 'a-ä';
648 /** @var Revision $rev */
649 $rev = $page->doEditContent(
650 new WikitextContent( $text ),
652 )->value
['revision'];
654 $store = MediaWikiServices
::getInstance()->getRevisionStore();
655 $record = $store->newRevisionFromRow(
656 $this->revisionToRow( $rev ),
660 $this->assertRevisionRecordMatchesRevision( $rev, $record );
661 $this->assertSame( $text, $rev->getContent()->serialize() );
665 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
666 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
668 public function testNewRevisionFromRow_userEdit() {
669 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
670 $text = __METHOD__
. 'b-ä';
671 /** @var Revision $rev */
672 $rev = $page->doEditContent(
673 new WikitextContent( $text ),
677 $this->getTestUser()->getUser()
678 )->value
['revision'];
680 $store = MediaWikiServices
::getInstance()->getRevisionStore();
681 $record = $store->newRevisionFromRow(
682 $this->revisionToRow( $rev ),
686 $this->assertRevisionRecordMatchesRevision( $rev, $record );
687 $this->assertSame( $text, $rev->getContent()->serialize() );
691 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
693 public function testNewRevisionFromArchiveRow() {
694 $store = MediaWikiServices
::getInstance()->getRevisionStore();
695 $title = Title
::newFromText( __METHOD__
);
696 $text = __METHOD__
. '-bä';
697 $page = WikiPage
::factory( $title );
698 /** @var Revision $orig */
699 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
701 $page->doDeleteArticle( __METHOD__
);
703 $db = wfGetDB( DB_MASTER
);
704 $arQuery = $store->getArchiveQueryInfo();
706 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
707 __METHOD__
, [], $arQuery['joins']
709 $this->assertTrue( is_object( $res ), 'query failed' );
711 $row = $res->fetchObject();
713 $record = $store->newRevisionFromArchiveRow( $row );
715 $this->assertRevisionRecordMatchesRevision( $orig, $record );
716 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
720 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
722 public function testNewRevisionFromArchiveRow_legacyEncoding() {
723 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
724 $this->overrideMwServices();
725 $store = MediaWikiServices
::getInstance()->getRevisionStore();
726 $title = Title
::newFromText( __METHOD__
);
727 $text = __METHOD__
. '-bä';
728 $page = WikiPage
::factory( $title );
729 /** @var Revision $orig */
730 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
732 $page->doDeleteArticle( __METHOD__
);
734 $db = wfGetDB( DB_MASTER
);
735 $arQuery = $store->getArchiveQueryInfo();
737 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
738 __METHOD__
, [], $arQuery['joins']
740 $this->assertTrue( is_object( $res ), 'query failed' );
742 $row = $res->fetchObject();
744 $record = $store->newRevisionFromArchiveRow( $row );
746 $this->assertRevisionRecordMatchesRevision( $orig, $record );
747 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
751 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
753 public function testLoadRevisionFromId() {
754 $title = Title
::newFromText( __METHOD__
);
755 $page = WikiPage
::factory( $title );
756 /** @var Revision $rev */
757 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
760 $store = MediaWikiServices
::getInstance()->getRevisionStore();
761 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
762 $this->assertRevisionRecordMatchesRevision( $rev, $result );
766 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
768 public function testLoadRevisionFromPageId() {
769 $title = Title
::newFromText( __METHOD__
);
770 $page = WikiPage
::factory( $title );
771 /** @var Revision $rev */
772 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
775 $store = MediaWikiServices
::getInstance()->getRevisionStore();
776 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
777 $this->assertRevisionRecordMatchesRevision( $rev, $result );
781 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
783 public function testLoadRevisionFromTitle() {
784 $title = Title
::newFromText( __METHOD__
);
785 $page = WikiPage
::factory( $title );
786 /** @var Revision $rev */
787 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
790 $store = MediaWikiServices
::getInstance()->getRevisionStore();
791 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
792 $this->assertRevisionRecordMatchesRevision( $rev, $result );
796 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
798 public function testLoadRevisionFromTimestamp() {
799 $title = Title
::newFromText( __METHOD__
);
800 $page = WikiPage
::factory( $title );
801 /** @var Revision $revOne */
802 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
804 // Sleep to ensure different timestamps... )(evil)
806 /** @var Revision $revTwo */
807 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
810 $store = MediaWikiServices
::getInstance()->getRevisionStore();
812 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
816 $store->loadRevisionFromTimestamp(
817 wfGetDB( DB_MASTER
),
819 $revOne->getTimestamp()
824 $store->loadRevisionFromTimestamp(
825 wfGetDB( DB_MASTER
),
827 $revTwo->getTimestamp()
833 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
835 public function testGetParentLengths() {
836 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
837 /** @var Revision $revOne */
838 $revOne = $page->doEditContent(
839 new WikitextContent( __METHOD__
), __METHOD__
840 )->value
['revision'];
841 /** @var Revision $revTwo */
842 $revTwo = $page->doEditContent(
843 new WikitextContent( __METHOD__
. '2' ), __METHOD__
844 )->value
['revision'];
846 $store = MediaWikiServices
::getInstance()->getRevisionStore();
849 $revOne->getId() => strlen( __METHOD__
),
851 $store->listRevisionSizes(
852 wfGetDB( DB_MASTER
),
858 $revOne->getId() => strlen( __METHOD__
),
859 $revTwo->getId() => strlen( __METHOD__
) +
1,
861 $store->listRevisionSizes(
862 wfGetDB( DB_MASTER
),
863 [ $revOne->getId(), $revTwo->getId() ]
869 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
871 public function testGetPreviousRevision() {
872 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
873 /** @var Revision $revOne */
874 $revOne = $page->doEditContent(
875 new WikitextContent( __METHOD__
), __METHOD__
876 )->value
['revision'];
877 /** @var Revision $revTwo */
878 $revTwo = $page->doEditContent(
879 new WikitextContent( __METHOD__
. '2' ), __METHOD__
880 )->value
['revision'];
882 $store = MediaWikiServices
::getInstance()->getRevisionStore();
884 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
888 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
893 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
895 public function testGetNextRevision() {
896 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
897 /** @var Revision $revOne */
898 $revOne = $page->doEditContent(
899 new WikitextContent( __METHOD__
), __METHOD__
900 )->value
['revision'];
901 /** @var Revision $revTwo */
902 $revTwo = $page->doEditContent(
903 new WikitextContent( __METHOD__
. '2' ), __METHOD__
904 )->value
['revision'];
906 $store = MediaWikiServices
::getInstance()->getRevisionStore();
909 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
912 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
917 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
919 public function testGetTimestampFromId_found() {
920 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
921 /** @var Revision $rev */
922 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
925 $store = MediaWikiServices
::getInstance()->getRevisionStore();
926 $result = $store->getTimestampFromId(
931 $this->assertSame( $rev->getTimestamp(), $result );
935 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
937 public function testGetTimestampFromId_notFound() {
938 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
939 /** @var Revision $rev */
940 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
943 $store = MediaWikiServices
::getInstance()->getRevisionStore();
944 $result = $store->getTimestampFromId(
949 $this->assertFalse( $result );
953 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
955 public function testCountRevisionsByPageId() {
956 $store = MediaWikiServices
::getInstance()->getRevisionStore();
957 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
961 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
963 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
966 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
968 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
971 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
976 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
978 public function testCountRevisionsByTitle() {
979 $store = MediaWikiServices
::getInstance()->getRevisionStore();
980 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
984 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
986 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
989 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
991 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
994 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
999 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1001 public function testUserWasLastToEdit_false() {
1002 $sysop = $this->getTestSysop()->getUser();
1003 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1004 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1006 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1007 $result = $store->userWasLastToEdit(
1008 wfGetDB( DB_MASTER
),
1013 $this->assertFalse( $result );
1017 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1019 public function testUserWasLastToEdit_true() {
1020 $startTime = wfTimestampNow();
1021 $sysop = $this->getTestSysop()->getUser();
1022 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1023 $page->doEditContent(
1024 new WikitextContent( __METHOD__
),
1031 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1032 $result = $store->userWasLastToEdit(
1033 wfGetDB( DB_MASTER
),
1038 $this->assertTrue( $result );
1042 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1044 public function testGetKnownCurrentRevision() {
1045 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1046 /** @var Revision $rev */
1047 $rev = $page->doEditContent(
1048 new WikitextContent( __METHOD__
. 'b' ),
1052 $this->getTestUser()->getUser()
1053 )->value
['revision'];
1055 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1056 $record = $store->getKnownCurrentRevision(
1061 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1064 public function provideNewMutableRevisionFromArray() {
1065 yield
'Basic array, with page & id' => [
1070 'timestamp' => '20171017114835',
1071 'user_text' => '111.0.1.2',
1073 'minor_edit' => false,
1077 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1078 'comment' => 'Goat Comment!',
1079 'content_format' => 'text/x-wiki',
1080 'content_model' => 'wikitext',
1083 yield
'Basic array, content object' => [
1087 'timestamp' => '20171017114835',
1088 'user_text' => '111.0.1.2',
1090 'minor_edit' => false,
1094 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1095 'comment' => 'Goat Comment!',
1096 'content' => new WikitextContent( 'Some Content' ),
1099 yield
'Basic array, serialized text' => [
1103 'timestamp' => '20171017114835',
1104 'user_text' => '111.0.1.2',
1106 'minor_edit' => false,
1110 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1111 'comment' => 'Goat Comment!',
1112 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1115 yield
'Basic array, serialized text, utf-8 flags' => [
1119 'timestamp' => '20171017114835',
1120 'user_text' => '111.0.1.2',
1122 'minor_edit' => false,
1126 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1127 'comment' => 'Goat Comment!',
1128 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1132 yield
'Basic array, with title' => [
1134 'title' => Title
::newFromText( 'SomeText' ),
1136 'timestamp' => '20171017114835',
1137 'user_text' => '111.0.1.2',
1139 'minor_edit' => false,
1143 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1144 'comment' => 'Goat Comment!',
1145 'content_format' => 'text/x-wiki',
1146 'content_model' => 'wikitext',
1149 yield
'Basic array, no user field' => [
1154 'timestamp' => '20171017114835',
1155 'user_text' => '111.0.1.3',
1156 'minor_edit' => false,
1160 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1161 'comment' => 'Goat Comment!',
1162 'content_format' => 'text/x-wiki',
1163 'content_model' => 'wikitext',
1169 * @dataProvider provideNewMutableRevisionFromArray
1170 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1172 public function testNewMutableRevisionFromArray( array $array ) {
1173 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1175 $result = $store->newMutableRevisionFromArray( $array );
1177 if ( isset( $array['id'] ) ) {
1178 $this->assertSame( $array['id'], $result->getId() );
1180 if ( isset( $array['page'] ) ) {
1181 $this->assertSame( $array['page'], $result->getPageId() );
1183 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1184 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1185 if ( isset( $array['user'] ) ) {
1186 $this->assertSame( $array['user'], $result->getUser()->getId() );
1188 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1189 $this->assertSame( $array['deleted'], $result->getVisibility() );
1190 $this->assertSame( $array['len'], $result->getSize() );
1191 $this->assertSame( $array['parent_id'], $result->getParentId() );
1192 $this->assertSame( $array['sha1'], $result->getSha1() );
1193 $this->assertSame( $array['comment'], $result->getComment()->text
);
1194 if ( isset( $array['content'] ) ) {
1196 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1198 } elseif ( isset( $array['text'] ) ) {
1199 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1202 $array['content_format'],
1203 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1205 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1210 * @dataProvider provideNewMutableRevisionFromArray
1211 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1213 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1214 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1215 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1216 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1218 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1219 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1220 ->disableOriginalConstructor()
1222 $factory->expects( $this->any() )
1223 ->method( 'newBlobStore' )
1224 ->willReturn( $blobStore );
1225 $factory->expects( $this->any() )
1226 ->method( 'newSqlBlobStore' )
1227 ->willReturn( $blobStore );
1229 $this->setService( 'BlobStoreFactory', $factory );
1231 $this->testNewMutableRevisionFromArray( $array );