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(),
140 $count = $store->countRevisionsByPageId( $db, 0 );
142 // Dummy check to make PhpUnit happy. We are really only interested in
143 // countRevisionsByPageId not failing due to the DB domain check.
144 $this->assertSame( 0, $count );
147 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
148 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
149 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
150 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
151 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
154 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
155 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
156 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
157 $this->assertEquals( $r1->getComment(), $r2->getComment() );
158 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
159 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
160 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
161 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
162 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
163 $this->assertEquals( $r1->getSize(), $r2->getSize() );
164 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
165 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
166 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
167 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
168 foreach ( $r1->getSlotRoles() as $role ) {
169 $this->assertEquals( $r1->getSlot( $role ), $r2->getSlot( $role ) );
170 $this->assertEquals( $r1->getContent( $role ), $r2->getContent( $role ) );
173 RevisionRecord
::DELETED_TEXT
,
174 RevisionRecord
::DELETED_COMMENT
,
175 RevisionRecord
::DELETED_USER
,
176 RevisionRecord
::DELETED_RESTRICTED
,
178 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
183 * @param mixed[] $details
185 * @return RevisionRecord
187 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
188 // Convert some values that can't be provided by dataProviders
189 $page = WikiPage
::factory( $title );
190 if ( isset( $details['user'] ) && $details['user'] === true ) {
191 $details['user'] = $this->getTestUser()->getUser();
193 if ( isset( $details['page'] ) && $details['page'] === true ) {
194 $details['page'] = $page->getId();
196 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
197 $details['parent'] = $page->getLatest();
200 // Create the RevisionRecord with any available data
201 $rev = new MutableRevisionRecord( $title );
202 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
203 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
204 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
205 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
206 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
207 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
208 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
209 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
210 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
211 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
212 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
217 private function getRandomCommentStoreComment() {
218 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
221 public function provideInsertRevisionOn_successes() {
222 yield
'Bare minimum revision insertion' => [
223 Title
::newFromText( 'UTPage' ),
225 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
227 'comment' => $this->getRandomCommentStoreComment(),
228 'timestamp' => '20171117010101',
232 yield
'Detailed revision insertion' => [
233 Title
::newFromText( 'UTPage' ),
235 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
238 'comment' => $this->getRandomCommentStoreComment(),
239 'timestamp' => '20171117010101',
242 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
248 * @dataProvider provideInsertRevisionOn_successes
249 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
251 public function testInsertRevisionOn_successes( Title
$title, array $revDetails = [] ) {
252 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
254 $store = MediaWikiServices
::getInstance()->getRevisionStore();
255 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
257 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
258 $this->assertRevisionRecordsEqual( $rev, $return );
262 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
264 public function testInsertRevisionOn_blobAddressExists() {
265 $title = Title
::newFromText( 'UTPage' );
267 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
269 'comment' => $this->getRandomCommentStoreComment(),
270 'timestamp' => '20171117010101',
274 $store = MediaWikiServices
::getInstance()->getRevisionStore();
276 // Insert the first revision
277 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
278 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
279 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
280 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
282 // Insert a second revision inheriting the same blob address
283 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
284 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
285 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
286 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
287 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
289 // Assert that the same blob address has been used.
291 $firstReturn->getSlot( 'main' )->getAddress(),
292 $secondReturn->getSlot( 'main' )->getAddress()
294 // And that different revisions have been created.
295 $this->assertNotSame(
296 $firstReturn->getId(),
297 $secondReturn->getId()
301 public function provideInsertRevisionOn_failures() {
303 Title
::newFromText( 'UTPage' ),
305 'comment' => $this->getRandomCommentStoreComment(),
306 'timestamp' => '20171117010101',
309 new InvalidArgumentException( 'At least one slot needs to be defined!' )
311 yield
'slot that is not main slot' => [
312 Title
::newFromText( 'UTPage' ),
314 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
315 'comment' => $this->getRandomCommentStoreComment(),
316 'timestamp' => '20171117010101',
319 new InvalidArgumentException( 'Only the main slot is supported for now!' )
321 yield
'no timestamp' => [
322 Title
::newFromText( 'UTPage' ),
324 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
325 'comment' => $this->getRandomCommentStoreComment(),
328 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
330 yield
'no comment' => [
331 Title
::newFromText( 'UTPage' ),
333 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
334 'timestamp' => '20171117010101',
337 new IncompleteRevisionException( 'comment must not be NULL!' )
340 Title
::newFromText( 'UTPage' ),
342 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
343 'comment' => $this->getRandomCommentStoreComment(),
344 'timestamp' => '20171117010101',
346 new IncompleteRevisionException( 'user must not be NULL!' )
351 * @dataProvider provideInsertRevisionOn_failures
352 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
354 public function testInsertRevisionOn_failures(
356 array $revDetails = [],
357 Exception
$exception ) {
358 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
360 $store = MediaWikiServices
::getInstance()->getRevisionStore();
362 $this->setExpectedException(
363 get_class( $exception ),
364 $exception->getMessage(),
365 $exception->getCode()
367 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
370 public function provideNewNullRevision() {
372 Title
::newFromText( 'UTPage' ),
373 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
377 Title
::newFromText( 'UTPage' ),
378 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
384 * @dataProvider provideNewNullRevision
385 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
387 public function testNewNullRevision( Title
$title, $comment, $minor ) {
388 $store = MediaWikiServices
::getInstance()->getRevisionStore();
389 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
390 $record = $store->newNullRevision(
391 wfGetDB( DB_MASTER
),
398 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
399 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
400 $this->assertEquals( $comment, $record->getComment() );
401 $this->assertEquals( $minor, $record->isMinor() );
402 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
406 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
408 public function testNewNullRevision_nonExistingTitle() {
409 $store = MediaWikiServices
::getInstance()->getRevisionStore();
410 $record = $store->newNullRevision(
411 wfGetDB( DB_MASTER
),
412 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
413 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
415 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
417 $this->assertNull( $record );
421 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
423 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
424 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
425 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
426 /** @var Revision $rev */
427 $rev = $status->value
['revision'];
429 $store = MediaWikiServices
::getInstance()->getRevisionStore();
430 $revisionRecord = $store->getRevisionById( $rev->getId() );
431 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
433 $this->assertGreaterThan( 0, $result );
435 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
441 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
443 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
444 // This assumes that sysops are auto patrolled
445 $sysop = $this->getTestSysop()->getUser();
446 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
447 $status = $page->doEditContent(
448 new WikitextContent( __METHOD__
),
454 /** @var Revision $rev */
455 $rev = $status->value
['revision'];
457 $store = MediaWikiServices
::getInstance()->getRevisionStore();
458 $revisionRecord = $store->getRevisionById( $rev->getId() );
459 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
461 $this->assertSame( 0, $result );
465 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
467 public function testGetRecentChange() {
468 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
469 $content = new WikitextContent( __METHOD__
);
470 $status = $page->doEditContent( $content, __METHOD__
);
471 /** @var Revision $rev */
472 $rev = $status->value
['revision'];
474 $store = MediaWikiServices
::getInstance()->getRevisionStore();
475 $revRecord = $store->getRevisionById( $rev->getId() );
476 $recentChange = $store->getRecentChange( $revRecord );
478 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
479 $this->assertEquals( $rev->getRecentChange(), $recentChange );
483 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
485 public function testGetRevisionById() {
486 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
487 $content = new WikitextContent( __METHOD__
);
488 $status = $page->doEditContent( $content, __METHOD__
);
489 /** @var Revision $rev */
490 $rev = $status->value
['revision'];
492 $store = MediaWikiServices
::getInstance()->getRevisionStore();
493 $revRecord = $store->getRevisionById( $rev->getId() );
495 $this->assertSame( $rev->getId(), $revRecord->getId() );
496 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
497 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
501 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
503 public function testGetRevisionByTitle() {
504 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
505 $content = new WikitextContent( __METHOD__
);
506 $status = $page->doEditContent( $content, __METHOD__
);
507 /** @var Revision $rev */
508 $rev = $status->value
['revision'];
510 $store = MediaWikiServices
::getInstance()->getRevisionStore();
511 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
513 $this->assertSame( $rev->getId(), $revRecord->getId() );
514 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
515 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
519 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
521 public function testGetRevisionByPageId() {
522 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
523 $content = new WikitextContent( __METHOD__
);
524 $status = $page->doEditContent( $content, __METHOD__
);
525 /** @var Revision $rev */
526 $rev = $status->value
['revision'];
528 $store = MediaWikiServices
::getInstance()->getRevisionStore();
529 $revRecord = $store->getRevisionByPageId( $page->getId() );
531 $this->assertSame( $rev->getId(), $revRecord->getId() );
532 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
533 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
537 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
539 public function testGetRevisionByTimestamp() {
540 // Make sure there is 1 second between the last revision and the rev we create...
541 // Otherwise we might not get the correct revision and the test may fail...
544 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
545 $content = new WikitextContent( __METHOD__
);
546 $status = $page->doEditContent( $content, __METHOD__
);
547 /** @var Revision $rev */
548 $rev = $status->value
['revision'];
550 $store = MediaWikiServices
::getInstance()->getRevisionStore();
551 $revRecord = $store->getRevisionByTimestamp(
556 $this->assertSame( $rev->getId(), $revRecord->getId() );
557 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
558 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
561 private function revisionToRow( Revision
$rev ) {
562 $page = WikiPage
::factory( $rev->getTitle() );
565 'rev_id' => (string)$rev->getId(),
566 'rev_page' => (string)$rev->getPage(),
567 'rev_text_id' => (string)$rev->getTextId(),
568 'rev_timestamp' => (string)$rev->getTimestamp(),
569 'rev_user_text' => (string)$rev->getUserText(),
570 'rev_user' => (string)$rev->getUser(),
571 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
572 'rev_deleted' => (string)$rev->getVisibility(),
573 'rev_len' => (string)$rev->getSize(),
574 'rev_parent_id' => (string)$rev->getParentId(),
575 'rev_sha1' => (string)$rev->getSha1(),
576 'rev_comment_text' => $rev->getComment(),
577 'rev_comment_data' => null,
578 'rev_comment_cid' => null,
579 'rev_content_format' => $rev->getContentFormat(),
580 'rev_content_model' => $rev->getContentModel(),
581 'page_namespace' => (string)$page->getTitle()->getNamespace(),
582 'page_title' => $page->getTitle()->getDBkey(),
583 'page_id' => (string)$page->getId(),
584 'page_latest' => (string)$page->getLatest(),
585 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
586 'page_len' => (string)$page->getContent()->getSize(),
587 'user_name' => (string)$rev->getUserText(),
591 private function assertRevisionRecordMatchesRevision(
593 RevisionRecord
$record
595 $this->assertSame( $rev->getId(), $record->getId() );
596 $this->assertSame( $rev->getPage(), $record->getPageId() );
597 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
598 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
599 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
600 $this->assertSame( $rev->isMinor(), $record->isMinor() );
601 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
602 $this->assertSame( $rev->getSize(), $record->getSize() );
604 * @note As of MW 1.31, the database schema allows the parent ID to be
605 * NULL to indicate that it is unknown.
607 $expectedParent = $rev->getParentId();
608 if ( $expectedParent === null ) {
611 $this->assertSame( $expectedParent, $record->getParentId() );
612 $this->assertSame( $rev->getSha1(), $record->getSha1() );
613 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
614 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
615 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
616 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
620 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
621 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
623 public function testNewRevisionFromRow_anonEdit() {
624 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
625 $text = __METHOD__
. 'a-ä';
626 /** @var Revision $rev */
627 $rev = $page->doEditContent(
628 new WikitextContent( $text ),
630 )->value
['revision'];
632 $store = MediaWikiServices
::getInstance()->getRevisionStore();
633 $record = $store->newRevisionFromRow(
634 $this->revisionToRow( $rev ),
638 $this->assertRevisionRecordMatchesRevision( $rev, $record );
639 $this->assertSame( $text, $rev->getContent()->serialize() );
643 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
644 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
646 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
647 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
648 $this->overrideMwServices();
649 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
650 $text = __METHOD__
. 'a-ä';
651 /** @var Revision $rev */
652 $rev = $page->doEditContent(
653 new WikitextContent( $text ),
655 )->value
['revision'];
657 $store = MediaWikiServices
::getInstance()->getRevisionStore();
658 $record = $store->newRevisionFromRow(
659 $this->revisionToRow( $rev ),
663 $this->assertRevisionRecordMatchesRevision( $rev, $record );
664 $this->assertSame( $text, $rev->getContent()->serialize() );
668 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
669 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
671 public function testNewRevisionFromRow_userEdit() {
672 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
673 $text = __METHOD__
. 'b-ä';
674 /** @var Revision $rev */
675 $rev = $page->doEditContent(
676 new WikitextContent( $text ),
680 $this->getTestUser()->getUser()
681 )->value
['revision'];
683 $store = MediaWikiServices
::getInstance()->getRevisionStore();
684 $record = $store->newRevisionFromRow(
685 $this->revisionToRow( $rev ),
689 $this->assertRevisionRecordMatchesRevision( $rev, $record );
690 $this->assertSame( $text, $rev->getContent()->serialize() );
694 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
696 public function testNewRevisionFromArchiveRow() {
697 $store = MediaWikiServices
::getInstance()->getRevisionStore();
698 $title = Title
::newFromText( __METHOD__
);
699 $text = __METHOD__
. '-bä';
700 $page = WikiPage
::factory( $title );
701 /** @var Revision $orig */
702 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
704 $page->doDeleteArticle( __METHOD__
);
706 $db = wfGetDB( DB_MASTER
);
707 $arQuery = $store->getArchiveQueryInfo();
709 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
710 __METHOD__
, [], $arQuery['joins']
712 $this->assertTrue( is_object( $res ), 'query failed' );
714 $row = $res->fetchObject();
716 $record = $store->newRevisionFromArchiveRow( $row );
718 $this->assertRevisionRecordMatchesRevision( $orig, $record );
719 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
723 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
725 public function testNewRevisionFromArchiveRow_legacyEncoding() {
726 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
727 $this->overrideMwServices();
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::loadRevisionFromId
756 public function testLoadRevisionFromId() {
757 $title = Title
::newFromText( __METHOD__
);
758 $page = WikiPage
::factory( $title );
759 /** @var Revision $rev */
760 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
763 $store = MediaWikiServices
::getInstance()->getRevisionStore();
764 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
765 $this->assertRevisionRecordMatchesRevision( $rev, $result );
769 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
771 public function testLoadRevisionFromPageId() {
772 $title = Title
::newFromText( __METHOD__
);
773 $page = WikiPage
::factory( $title );
774 /** @var Revision $rev */
775 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
778 $store = MediaWikiServices
::getInstance()->getRevisionStore();
779 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
780 $this->assertRevisionRecordMatchesRevision( $rev, $result );
784 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
786 public function testLoadRevisionFromTitle() {
787 $title = Title
::newFromText( __METHOD__
);
788 $page = WikiPage
::factory( $title );
789 /** @var Revision $rev */
790 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
793 $store = MediaWikiServices
::getInstance()->getRevisionStore();
794 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
795 $this->assertRevisionRecordMatchesRevision( $rev, $result );
799 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
801 public function testLoadRevisionFromTimestamp() {
802 $title = Title
::newFromText( __METHOD__
);
803 $page = WikiPage
::factory( $title );
804 /** @var Revision $revOne */
805 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
807 // Sleep to ensure different timestamps... )(evil)
809 /** @var Revision $revTwo */
810 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
813 $store = MediaWikiServices
::getInstance()->getRevisionStore();
815 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
819 $store->loadRevisionFromTimestamp(
820 wfGetDB( DB_MASTER
),
822 $revOne->getTimestamp()
827 $store->loadRevisionFromTimestamp(
828 wfGetDB( DB_MASTER
),
830 $revTwo->getTimestamp()
836 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
838 public function testGetParentLengths() {
839 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
840 /** @var Revision $revOne */
841 $revOne = $page->doEditContent(
842 new WikitextContent( __METHOD__
), __METHOD__
843 )->value
['revision'];
844 /** @var Revision $revTwo */
845 $revTwo = $page->doEditContent(
846 new WikitextContent( __METHOD__
. '2' ), __METHOD__
847 )->value
['revision'];
849 $store = MediaWikiServices
::getInstance()->getRevisionStore();
852 $revOne->getId() => strlen( __METHOD__
),
854 $store->listRevisionSizes(
855 wfGetDB( DB_MASTER
),
861 $revOne->getId() => strlen( __METHOD__
),
862 $revTwo->getId() => strlen( __METHOD__
) +
1,
864 $store->listRevisionSizes(
865 wfGetDB( DB_MASTER
),
866 [ $revOne->getId(), $revTwo->getId() ]
872 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
874 public function testGetPreviousRevision() {
875 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
876 /** @var Revision $revOne */
877 $revOne = $page->doEditContent(
878 new WikitextContent( __METHOD__
), __METHOD__
879 )->value
['revision'];
880 /** @var Revision $revTwo */
881 $revTwo = $page->doEditContent(
882 new WikitextContent( __METHOD__
. '2' ), __METHOD__
883 )->value
['revision'];
885 $store = MediaWikiServices
::getInstance()->getRevisionStore();
887 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
891 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
896 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
898 public function testGetNextRevision() {
899 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
900 /** @var Revision $revOne */
901 $revOne = $page->doEditContent(
902 new WikitextContent( __METHOD__
), __METHOD__
903 )->value
['revision'];
904 /** @var Revision $revTwo */
905 $revTwo = $page->doEditContent(
906 new WikitextContent( __METHOD__
. '2' ), __METHOD__
907 )->value
['revision'];
909 $store = MediaWikiServices
::getInstance()->getRevisionStore();
912 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
915 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
920 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
922 public function testGetTimestampFromId_found() {
923 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
924 /** @var Revision $rev */
925 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
928 $store = MediaWikiServices
::getInstance()->getRevisionStore();
929 $result = $store->getTimestampFromId(
934 $this->assertSame( $rev->getTimestamp(), $result );
938 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
940 public function testGetTimestampFromId_notFound() {
941 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
942 /** @var Revision $rev */
943 $rev = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
946 $store = MediaWikiServices
::getInstance()->getRevisionStore();
947 $result = $store->getTimestampFromId(
952 $this->assertFalse( $result );
956 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
958 public function testCountRevisionsByPageId() {
959 $store = MediaWikiServices
::getInstance()->getRevisionStore();
960 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
964 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
966 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
969 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
971 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
974 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
979 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
981 public function testCountRevisionsByTitle() {
982 $store = MediaWikiServices
::getInstance()->getRevisionStore();
983 $page = WikiPage
::factory( Title
::newFromText( __METHOD__
) );
987 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
989 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
992 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
994 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
997 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1002 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1004 public function testUserWasLastToEdit_false() {
1005 $sysop = $this->getTestSysop()->getUser();
1006 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1007 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1009 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1010 $result = $store->userWasLastToEdit(
1011 wfGetDB( DB_MASTER
),
1016 $this->assertFalse( $result );
1020 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1022 public function testUserWasLastToEdit_true() {
1023 $startTime = wfTimestampNow();
1024 $sysop = $this->getTestSysop()->getUser();
1025 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1026 $page->doEditContent(
1027 new WikitextContent( __METHOD__
),
1034 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1035 $result = $store->userWasLastToEdit(
1036 wfGetDB( DB_MASTER
),
1041 $this->assertTrue( $result );
1045 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1047 public function testGetKnownCurrentRevision() {
1048 $page = WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
1049 /** @var Revision $rev */
1050 $rev = $page->doEditContent(
1051 new WikitextContent( __METHOD__
. 'b' ),
1055 $this->getTestUser()->getUser()
1056 )->value
['revision'];
1058 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1059 $record = $store->getKnownCurrentRevision(
1064 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1067 public function provideNewMutableRevisionFromArray() {
1068 yield
'Basic array, with page & id' => [
1073 'timestamp' => '20171017114835',
1074 'user_text' => '111.0.1.2',
1076 'minor_edit' => false,
1080 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1081 'comment' => 'Goat Comment!',
1082 'content_format' => 'text/x-wiki',
1083 'content_model' => 'wikitext',
1086 yield
'Basic array, content object' => [
1090 'timestamp' => '20171017114835',
1091 'user_text' => '111.0.1.2',
1093 'minor_edit' => false,
1097 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1098 'comment' => 'Goat Comment!',
1099 'content' => new WikitextContent( 'Some Content' ),
1102 yield
'Basic array, serialized text' => [
1106 'timestamp' => '20171017114835',
1107 'user_text' => '111.0.1.2',
1109 'minor_edit' => false,
1113 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1114 'comment' => 'Goat Comment!',
1115 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1118 yield
'Basic array, serialized text, utf-8 flags' => [
1122 'timestamp' => '20171017114835',
1123 'user_text' => '111.0.1.2',
1125 'minor_edit' => false,
1129 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1130 'comment' => 'Goat Comment!',
1131 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1135 yield
'Basic array, with title' => [
1137 'title' => Title
::newFromText( 'SomeText' ),
1139 'timestamp' => '20171017114835',
1140 'user_text' => '111.0.1.2',
1142 'minor_edit' => false,
1146 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1147 'comment' => 'Goat Comment!',
1148 'content_format' => 'text/x-wiki',
1149 'content_model' => 'wikitext',
1152 yield
'Basic array, no user field' => [
1157 'timestamp' => '20171017114835',
1158 'user_text' => '111.0.1.3',
1159 'minor_edit' => false,
1163 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1164 'comment' => 'Goat Comment!',
1165 'content_format' => 'text/x-wiki',
1166 'content_model' => 'wikitext',
1172 * @dataProvider provideNewMutableRevisionFromArray
1173 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1175 public function testNewMutableRevisionFromArray( array $array ) {
1176 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1178 $result = $store->newMutableRevisionFromArray( $array );
1180 if ( isset( $array['id'] ) ) {
1181 $this->assertSame( $array['id'], $result->getId() );
1183 if ( isset( $array['page'] ) ) {
1184 $this->assertSame( $array['page'], $result->getPageId() );
1186 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1187 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1188 if ( isset( $array['user'] ) ) {
1189 $this->assertSame( $array['user'], $result->getUser()->getId() );
1191 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1192 $this->assertSame( $array['deleted'], $result->getVisibility() );
1193 $this->assertSame( $array['len'], $result->getSize() );
1194 $this->assertSame( $array['parent_id'], $result->getParentId() );
1195 $this->assertSame( $array['sha1'], $result->getSha1() );
1196 $this->assertSame( $array['comment'], $result->getComment()->text
);
1197 if ( isset( $array['content'] ) ) {
1199 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1201 } elseif ( isset( $array['text'] ) ) {
1202 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1205 $array['content_format'],
1206 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1208 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1213 * @dataProvider provideNewMutableRevisionFromArray
1214 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1216 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1217 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1218 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
1219 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1221 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1222 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1223 ->disableOriginalConstructor()
1225 $factory->expects( $this->any() )
1226 ->method( 'newBlobStore' )
1227 ->willReturn( $blobStore );
1228 $factory->expects( $this->any() )
1229 ->method( 'newSqlBlobStore' )
1230 ->willReturn( $blobStore );
1232 $this->setService( 'BlobStoreFactory', $factory );
1234 $this->testNewMutableRevisionFromArray( $array );