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() ] ),
139 $count = $store->countRevisionsByPageId( $db, 0 );
141 // Dummy check to make PhpUnit happy. We are really only interested in
142 // countRevisionsByPageId not failing due to the DB domain check.
143 $this->assertSame( 0, $count );
146 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
147 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
148 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
149 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
150 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
153 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
154 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
155 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
156 $this->assertEquals( $r1->getComment(), $r2->getComment() );
157 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
158 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
159 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
160 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
161 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
162 $this->assertEquals( $r1->getSize(), $r2->getSize() );
163 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
164 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
165 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
166 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
167 foreach ( $r1->getSlotRoles() as $role ) {
168 $this->assertEquals( $r1->getSlot( $role ), $r2->getSlot( $role ) );
169 $this->assertEquals( $r1->getContent( $role ), $r2->getContent( $role ) );
172 RevisionRecord
::DELETED_TEXT
,
173 RevisionRecord
::DELETED_COMMENT
,
174 RevisionRecord
::DELETED_USER
,
175 RevisionRecord
::DELETED_RESTRICTED
,
177 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
182 * @param mixed[] $details
184 * @return RevisionRecord
186 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
187 // Convert some values that can't be provided by dataProviders
188 $page = WikiPage
::factory( $title );
189 if ( isset( $details['user'] ) && $details['user'] === true ) {
190 $details['user'] = $this->getTestUser()->getUser();
192 if ( isset( $details['page'] ) && $details['page'] === true ) {
193 $details['page'] = $page->getId();
195 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
196 $details['parent'] = $page->getLatest();
199 // Create the RevisionRecord with any available data
200 $rev = new MutableRevisionRecord( $title );
201 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
202 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
203 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
204 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
205 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
206 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
207 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
208 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
209 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
210 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
211 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
216 private function getRandomCommentStoreComment() {
217 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
220 public function provideInsertRevisionOn_successes() {
221 yield
'Bare minimum revision insertion' => [
222 Title
::newFromText( 'UTPage' ),
224 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
226 'comment' => $this->getRandomCommentStoreComment(),
227 'timestamp' => '20171117010101',
231 yield
'Detailed revision insertion' => [
232 Title
::newFromText( 'UTPage' ),
234 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
237 'comment' => $this->getRandomCommentStoreComment(),
238 'timestamp' => '20171117010101',
241 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
247 * @dataProvider provideInsertRevisionOn_successes
248 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
250 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
251 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
253 $store = MediaWikiServices
::getInstance()->getRevisionStore();
254 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
256 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
257 $this->assertRevisionRecordsEqual( $rev, $return );
261 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
263 public function testInsertRevisionOn_blobAddressExists() {
264 $title = Title
::newFromText( 'UTPage' );
266 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
268 'comment' => $this->getRandomCommentStoreComment(),
269 'timestamp' => '20171117010101',
273 $store = MediaWikiServices
::getInstance()->getRevisionStore();
275 // Insert the first revision
276 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
277 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
278 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
279 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
281 // Insert a second revision inheriting the same blob address
282 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
283 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
284 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
285 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
286 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
288 // Assert that the same blob address has been used.
290 $firstReturn->getSlot( 'main' )->getAddress(),
291 $secondReturn->getSlot( 'main' )->getAddress()
293 // And that different revisions have been created.
294 $this->assertNotSame(
295 $firstReturn->getId(),
296 $secondReturn->getId()
300 public function provideInsertRevisionOn_failures() {
302 Title
::newFromText( 'UTPage' ),
304 'comment' => $this->getRandomCommentStoreComment(),
305 'timestamp' => '20171117010101',
308 new InvalidArgumentException( 'At least one slot needs to be defined!' )
310 yield
'slot that is not main slot' => [
311 Title
::newFromText( 'UTPage' ),
313 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
314 'comment' => $this->getRandomCommentStoreComment(),
315 'timestamp' => '20171117010101',
318 new InvalidArgumentException( 'Only the main slot is supported for now!' )
320 yield
'no timestamp' => [
321 Title
::newFromText( 'UTPage' ),
323 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
324 'comment' => $this->getRandomCommentStoreComment(),
327 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
329 yield
'no comment' => [
330 Title
::newFromText( 'UTPage' ),
332 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
333 'timestamp' => '20171117010101',
336 new IncompleteRevisionException( 'comment must not be NULL!' )
339 Title
::newFromText( 'UTPage' ),
341 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
342 'comment' => $this->getRandomCommentStoreComment(),
343 'timestamp' => '20171117010101',
345 new IncompleteRevisionException( 'user must not be NULL!' )
350 * @dataProvider provideInsertRevisionOn_failures
351 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
353 public function testInsertRevisionOn_failures(
355 array $revDetails = [],
356 Exception
$exception ) {
357 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
359 $store = MediaWikiServices
::getInstance()->getRevisionStore();
361 $this->setExpectedException(
362 get_class( $exception ),
363 $exception->getMessage(),
364 $exception->getCode()
366 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
369 public function provideNewNullRevision() {
371 Title
::newFromText( 'UTPage' ),
372 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
376 Title
::newFromText( 'UTPage' ),
377 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
383 * @dataProvider provideNewNullRevision
384 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
386 public function testNewNullRevision( Title
$title, $comment, $minor ) {
387 $store = MediaWikiServices
::getInstance()->getRevisionStore();
388 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
389 $record = $store->newNullRevision(
390 wfGetDB( DB_MASTER
),
397 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
398 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
399 $this->assertEquals( $comment, $record->getComment() );
400 $this->assertEquals( $minor, $record->isMinor() );
401 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
405 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
407 public function testNewNullRevision_nonExistingTitle() {
408 $store = MediaWikiServices
::getInstance()->getRevisionStore();
409 $record = $store->newNullRevision(
410 wfGetDB( DB_MASTER
),
411 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
412 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
414 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
416 $this->assertNull( $record );
420 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
422 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
423 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
424 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
425 /** @var Revision $rev */
426 $rev = $status->value
['revision'];
428 $store = MediaWikiServices
::getInstance()->getRevisionStore();
429 $revisionRecord = $store->getRevisionById( $rev->getId() );
430 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
432 $this->assertGreaterThan( 0, $result );
434 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
440 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
442 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
443 // This assumes that sysops are auto patrolled
444 $sysop = $this->getTestSysop()->getUser();
445 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
446 $status = $page->doEditContent(
447 new WikitextContent( __METHOD__
),
453 /** @var Revision $rev */
454 $rev = $status->value
['revision'];
456 $store = MediaWikiServices
::getInstance()->getRevisionStore();
457 $revisionRecord = $store->getRevisionById( $rev->getId() );
458 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
460 $this->assertSame( 0, $result );
464 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
466 public function testGetRecentChange() {
467 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
468 $content = new WikitextContent( __METHOD__
);
469 $status = $page->doEditContent( $content, __METHOD__
);
470 /** @var Revision $rev */
471 $rev = $status->value
['revision'];
473 $store = MediaWikiServices
::getInstance()->getRevisionStore();
474 $revRecord = $store->getRevisionById( $rev->getId() );
475 $recentChange = $store->getRecentChange( $revRecord );
477 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
478 $this->assertEquals( $rev->getRecentChange(), $recentChange );
482 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
484 public function testGetRevisionById() {
485 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
486 $content = new WikitextContent( __METHOD__
);
487 $status = $page->doEditContent( $content, __METHOD__
);
488 /** @var Revision $rev */
489 $rev = $status->value
['revision'];
491 $store = MediaWikiServices
::getInstance()->getRevisionStore();
492 $revRecord = $store->getRevisionById( $rev->getId() );
494 $this->assertSame( $rev->getId(), $revRecord->getId() );
495 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
496 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
500 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
502 public function testGetRevisionByTitle() {
503 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
504 $content = new WikitextContent( __METHOD__
);
505 $status = $page->doEditContent( $content, __METHOD__
);
506 /** @var Revision $rev */
507 $rev = $status->value
['revision'];
509 $store = MediaWikiServices
::getInstance()->getRevisionStore();
510 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
512 $this->assertSame( $rev->getId(), $revRecord->getId() );
513 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
514 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
518 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
520 public function testGetRevisionByPageId() {
521 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
522 $content = new WikitextContent( __METHOD__
);
523 $status = $page->doEditContent( $content, __METHOD__
);
524 /** @var Revision $rev */
525 $rev = $status->value
['revision'];
527 $store = MediaWikiServices
::getInstance()->getRevisionStore();
528 $revRecord = $store->getRevisionByPageId( $page->getId() );
530 $this->assertSame( $rev->getId(), $revRecord->getId() );
531 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
532 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
536 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
538 public function testGetRevisionByTimestamp() {
539 // Make sure there is 1 second between the last revision and the rev we create...
540 // Otherwise we might not get the correct revision and the test may fail...
543 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
544 $content = new WikitextContent( __METHOD__
);
545 $status = $page->doEditContent( $content, __METHOD__
);
546 /** @var Revision $rev */
547 $rev = $status->value
['revision'];
549 $store = MediaWikiServices
::getInstance()->getRevisionStore();
550 $revRecord = $store->getRevisionByTimestamp(
555 $this->assertSame( $rev->getId(), $revRecord->getId() );
556 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
557 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
560 private function revisionToRow( Revision
$rev ) {
561 $page = WikiPage
::factory( $rev->getTitle() );
564 'rev_id' => (string)$rev->getId(),
565 'rev_page' => (string)$rev->getPage(),
566 'rev_text_id' => (string)$rev->getTextId(),
567 'rev_timestamp' => (string)$rev->getTimestamp(),
568 'rev_user_text' => (string)$rev->getUserText(),
569 'rev_user' => (string)$rev->getUser(),
570 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
571 'rev_deleted' => (string)$rev->getVisibility(),
572 'rev_len' => (string)$rev->getSize(),
573 'rev_parent_id' => (string)$rev->getParentId(),
574 'rev_sha1' => (string)$rev->getSha1(),
575 'rev_comment_text' => $rev->getComment(),
576 'rev_comment_data' => null,
577 'rev_comment_cid' => null,
578 'rev_content_format' => $rev->getContentFormat(),
579 'rev_content_model' => $rev->getContentModel(),
580 'page_namespace' => (string)$page->getTitle()->getNamespace(),
581 'page_title' => $page->getTitle()->getDBkey(),
582 'page_id' => (string)$page->getId(),
583 'page_latest' => (string)$page->getLatest(),
584 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
585 'page_len' => (string)$page->getContent()->getSize(),
586 'user_name' => (string)$rev->getUserText(),
590 private function assertRevisionRecordMatchesRevision(
592 RevisionRecord
$record
594 $this->assertSame( $rev->getId(), $record->getId() );
595 $this->assertSame( $rev->getPage(), $record->getPageId() );
596 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
597 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
598 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
599 $this->assertSame( $rev->isMinor(), $record->isMinor() );
600 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
601 $this->assertSame( $rev->getSize(), $record->getSize() );
603 * @note As of MW 1.31, the database schema allows the parent ID to be
604 * NULL to indicate that it is unknown.
606 $expectedParent = $rev->getParentId();
607 if ( $expectedParent === null ) {
610 $this->assertSame( $expectedParent, $record->getParentId() );
611 $this->assertSame( $rev->getSha1(), $record->getSha1() );
612 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
613 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
614 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
615 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
619 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
620 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
622 public function testNewRevisionFromRow_anonEdit() {
623 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
624 $text = __METHOD__
. 'a-ä';
625 /** @var Revision $rev */
626 $rev = $page->doEditContent(
627 new WikitextContent( $text ),
629 )->value
['revision'];
631 $store = MediaWikiServices
::getInstance()->getRevisionStore();
632 $record = $store->newRevisionFromRow(
633 $this->revisionToRow( $rev ),
637 $this->assertRevisionRecordMatchesRevision( $rev, $record );
638 $this->assertSame( $text, $rev->getContent()->serialize() );
642 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
643 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
645 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
646 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
647 $this->overrideMwServices();
648 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
649 $text = __METHOD__
. 'a-ä';
650 /** @var Revision $rev */
651 $rev = $page->doEditContent(
652 new WikitextContent( $text ),
654 )->value
['revision'];
656 $store = MediaWikiServices
::getInstance()->getRevisionStore();
657 $record = $store->newRevisionFromRow(
658 $this->revisionToRow( $rev ),
662 $this->assertRevisionRecordMatchesRevision( $rev, $record );
663 $this->assertSame( $text, $rev->getContent()->serialize() );
667 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
668 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
670 public function testNewRevisionFromRow_userEdit() {
671 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
672 $text = __METHOD__
. 'b-ä';
673 /** @var Revision $rev */
674 $rev = $page->doEditContent(
675 new WikitextContent( $text ),
679 $this->getTestUser()->getUser()
680 )->value
['revision'];
682 $store = MediaWikiServices
::getInstance()->getRevisionStore();
683 $record = $store->newRevisionFromRow(
684 $this->revisionToRow( $rev ),
688 $this->assertRevisionRecordMatchesRevision( $rev, $record );
689 $this->assertSame( $text, $rev->getContent()->serialize() );
693 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
695 public function testNewRevisionFromArchiveRow() {
696 $store = MediaWikiServices
::getInstance()->getRevisionStore();
697 $title = Title
::newFromText( __METHOD__
);
698 $text = __METHOD__
. '-bä';
699 $page = WikiPage
::factory( $title );
700 /** @var Revision $orig */
701 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
703 $page->doDeleteArticle( __METHOD__
);
705 $db = wfGetDB( DB_MASTER
);
706 $arQuery = $store->getArchiveQueryInfo();
708 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
709 __METHOD__
, [], $arQuery['joins']
711 $this->assertTrue( is_object( $res ), 'query failed' );
713 $row = $res->fetchObject();
715 $record = $store->newRevisionFromArchiveRow( $row );
717 $this->assertRevisionRecordMatchesRevision( $orig, $record );
718 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
722 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
724 public function testNewRevisionFromArchiveRow_legacyEncoding() {
725 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
726 $this->overrideMwServices();
727 $store = MediaWikiServices
::getInstance()->getRevisionStore();
728 $title = Title
::newFromText( __METHOD__
);
729 $text = __METHOD__
. '-bä';
730 $page = WikiPage
::factory( $title );
731 /** @var Revision $orig */
732 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
734 $page->doDeleteArticle( __METHOD__
);
736 $db = wfGetDB( DB_MASTER
);
737 $arQuery = $store->getArchiveQueryInfo();
739 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
740 __METHOD__
, [], $arQuery['joins']
742 $this->assertTrue( is_object( $res ), 'query failed' );
744 $row = $res->fetchObject();
746 $record = $store->newRevisionFromArchiveRow( $row );
748 $this->assertRevisionRecordMatchesRevision( $orig, $record );
749 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
753 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
755 public function testLoadRevisionFromId() {
756 $title = Title
::newFromText( __METHOD__
);
757 $page = WikiPage
::factory( $title );
758 /** @var Revision $rev */
759 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
762 $store = MediaWikiServices
::getInstance()->getRevisionStore();
763 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
764 $this->assertRevisionRecordMatchesRevision( $rev, $result );
768 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
770 public function testLoadRevisionFromPageId() {
771 $title = Title
::newFromText( __METHOD__
);
772 $page = WikiPage
::factory( $title );
773 /** @var Revision $rev */
774 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
777 $store = MediaWikiServices
::getInstance()->getRevisionStore();
778 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
779 $this->assertRevisionRecordMatchesRevision( $rev, $result );
783 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
785 public function testLoadRevisionFromTitle() {
786 $title = Title
::newFromText( __METHOD__
);
787 $page = WikiPage
::factory( $title );
788 /** @var Revision $rev */
789 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
792 $store = MediaWikiServices
::getInstance()->getRevisionStore();
793 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
794 $this->assertRevisionRecordMatchesRevision( $rev, $result );
798 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
800 public function testLoadRevisionFromTimestamp() {
801 $title = Title
::newFromText( __METHOD__
);
802 $page = WikiPage
::factory( $title );
803 /** @var Revision $revOne */
804 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
806 // Sleep to ensure different timestamps... )(evil)
808 /** @var Revision $revTwo */
809 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
812 $store = MediaWikiServices
::getInstance()->getRevisionStore();
814 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
818 $store->loadRevisionFromTimestamp(
819 wfGetDB( DB_MASTER
),
821 $revOne->getTimestamp()
826 $store->loadRevisionFromTimestamp(
827 wfGetDB( DB_MASTER
),
829 $revTwo->getTimestamp()
835 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
837 public function testGetParentLengths() {
838 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
839 /** @var Revision $revOne */
840 $revOne = $page->doEditContent(
841 new WikitextContent( __METHOD__
), __METHOD__
842 )->value
['revision'];
843 /** @var Revision $revTwo */
844 $revTwo = $page->doEditContent(
845 new WikitextContent( __METHOD__
. '2' ), __METHOD__
846 )->value
['revision'];
848 $store = MediaWikiServices
::getInstance()->getRevisionStore();
851 $revOne->getId() => strlen( __METHOD__
),
853 $store->listRevisionSizes(
854 wfGetDB( DB_MASTER
),
860 $revOne->getId() => strlen( __METHOD__
),
861 $revTwo->getId() => strlen( __METHOD__
) +
1,
863 $store->listRevisionSizes(
864 wfGetDB( DB_MASTER
),
865 [ $revOne->getId(), $revTwo->getId() ]
871 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
873 public function testGetPreviousRevision() {
874 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
875 /** @var Revision $revOne */
876 $revOne = $page->doEditContent(
877 new WikitextContent( __METHOD__
), __METHOD__
878 )->value
['revision'];
879 /** @var Revision $revTwo */
880 $revTwo = $page->doEditContent(
881 new WikitextContent( __METHOD__
. '2' ), __METHOD__
882 )->value
['revision'];
884 $store = MediaWikiServices
::getInstance()->getRevisionStore();
886 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
890 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
895 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
897 public function testGetNextRevision() {
898 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
899 /** @var Revision $revOne */
900 $revOne = $page->doEditContent(
901 new WikitextContent( __METHOD__
), __METHOD__
902 )->value
['revision'];
903 /** @var Revision $revTwo */
904 $revTwo = $page->doEditContent(
905 new WikitextContent( __METHOD__
. '2' ), __METHOD__
906 )->value
['revision'];
908 $store = MediaWikiServices
::getInstance()->getRevisionStore();
911 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
914 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
919 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
921 public function testGetTimestampFromId_found() {
922 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
923 /** @var Revision $rev */
924 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
927 $store = MediaWikiServices
::getInstance()->getRevisionStore();
928 $result = $store->getTimestampFromId(
933 $this->assertSame( $rev->getTimestamp(), $result );
937 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
939 public function testGetTimestampFromId_notFound() {
940 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
941 /** @var Revision $rev */
942 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
945 $store = MediaWikiServices
::getInstance()->getRevisionStore();
946 $result = $store->getTimestampFromId(
951 $this->assertFalse( $result );
955 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
957 public function testCountRevisionsByPageId() {
958 $store = MediaWikiServices
::getInstance()->getRevisionStore();
959 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
963 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
965 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
968 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
970 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
973 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
978 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
980 public function testCountRevisionsByTitle() {
981 $store = MediaWikiServices
::getInstance()->getRevisionStore();
982 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
986 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
988 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
991 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
993 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
996 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1001 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1003 public function testUserWasLastToEdit_false() {
1004 $sysop = $this->getTestSysop()->getUser();
1005 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1006 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1008 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1009 $result = $store->userWasLastToEdit(
1010 wfGetDB( DB_MASTER
),
1015 $this->assertFalse( $result );
1019 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1021 public function testUserWasLastToEdit_true() {
1022 $startTime = wfTimestampNow();
1023 $sysop = $this->getTestSysop()->getUser();
1024 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1025 $page->doEditContent(
1026 new WikitextContent( __METHOD__
),
1033 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1034 $result = $store->userWasLastToEdit(
1035 wfGetDB( DB_MASTER
),
1040 $this->assertTrue( $result );
1044 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1046 public function testGetKnownCurrentRevision() {
1047 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1048 /** @var Revision $rev */
1049 $rev = $page->doEditContent(
1050 new WikitextContent( __METHOD__
. 'b' ),
1054 $this->getTestUser()->getUser()
1055 )->value
['revision'];
1057 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1058 $record = $store->getKnownCurrentRevision(
1063 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1066 public function provideNewMutableRevisionFromArray() {
1067 yield
'Basic array, with page & id' => [
1072 'timestamp' => '20171017114835',
1073 'user_text' => '111.0.1.2',
1075 'minor_edit' => false,
1079 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1080 'comment' => 'Goat Comment!',
1081 'content_format' => 'text/x-wiki',
1082 'content_model' => 'wikitext',
1085 yield
'Basic array, content object' => [
1089 'timestamp' => '20171017114835',
1090 'user_text' => '111.0.1.2',
1092 'minor_edit' => false,
1096 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1097 'comment' => 'Goat Comment!',
1098 'content' => new WikitextContent( 'Some Content' ),
1101 yield
'Basic array, serialized text' => [
1105 'timestamp' => '20171017114835',
1106 'user_text' => '111.0.1.2',
1108 'minor_edit' => false,
1112 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1113 'comment' => 'Goat Comment!',
1114 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1117 yield
'Basic array, serialized text, utf-8 flags' => [
1121 'timestamp' => '20171017114835',
1122 'user_text' => '111.0.1.2',
1124 'minor_edit' => false,
1128 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1129 'comment' => 'Goat Comment!',
1130 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1134 yield
'Basic array, with title' => [
1136 'title' => Title
::newFromText( 'SomeText' ),
1138 'timestamp' => '20171017114835',
1139 'user_text' => '111.0.1.2',
1141 'minor_edit' => false,
1145 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1146 'comment' => 'Goat Comment!',
1147 'content_format' => 'text/x-wiki',
1148 'content_model' => 'wikitext',
1151 yield
'Basic array, no user field' => [
1156 'timestamp' => '20171017114835',
1157 'user_text' => '111.0.1.3',
1158 'minor_edit' => false,
1162 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1163 'comment' => 'Goat Comment!',
1164 'content_format' => 'text/x-wiki',
1165 'content_model' => 'wikitext',
1171 * @dataProvider provideNewMutableRevisionFromArray
1172 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1174 public function testNewMutableRevisionFromArray( array $array ) {
1175 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1177 $result = $store->newMutableRevisionFromArray( $array );
1179 if ( isset( $array['id'] ) ) {
1180 $this->assertSame( $array['id'], $result->getId() );
1182 if ( isset( $array['page'] ) ) {
1183 $this->assertSame( $array['page'], $result->getPageId() );
1185 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1186 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1187 if ( isset( $array['user'] ) ) {
1188 $this->assertSame( $array['user'], $result->getUser()->getId() );
1190 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1191 $this->assertSame( $array['deleted'], $result->getVisibility() );
1192 $this->assertSame( $array['len'], $result->getSize() );
1193 $this->assertSame( $array['parent_id'], $result->getParentId() );
1194 $this->assertSame( $array['sha1'], $result->getSha1() );
1195 $this->assertSame( $array['comment'], $result->getComment()->text
);
1196 if ( isset( $array['content'] ) ) {
1198 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1200 } elseif ( isset( $array['text'] ) ) {
1201 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1204 $array['content_format'],
1205 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1207 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1212 * @dataProvider provideNewMutableRevisionFromArray
1213 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1215 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1216 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1217 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1218 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1220 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1221 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1222 ->disableOriginalConstructor()
1224 $factory->expects( $this->any() )
1225 ->method( 'newBlobStore' )
1226 ->willReturn( $blobStore );
1227 $factory->expects( $this->any() )
1228 ->method( 'newSqlBlobStore' )
1229 ->willReturn( $blobStore );
1231 $this->setService( 'BlobStoreFactory', $factory );
1233 $this->testNewMutableRevisionFromArray( $array );