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
;
20 use PHPUnit_Framework_MockObject_MockObject
;
25 use Wikimedia\Rdbms\Database
;
26 use Wikimedia\Rdbms\DatabaseSqlite
;
27 use Wikimedia\Rdbms\FakeResultWrapper
;
28 use Wikimedia\Rdbms\LoadBalancer
;
29 use Wikimedia\Rdbms\TransactionProfiler
;
35 * @group RevisionStore
37 abstract class RevisionStoreDbTestBase
extends MediaWikiTestCase
{
42 abstract protected function getMcrMigrationStage();
47 protected function getContentHandlerUseDB() {
54 abstract protected function getMcrTablesToReset();
56 public function needsDB() {
60 public function setUp() {
62 $this->tablesUsed
[] = 'archive';
63 $this->tablesUsed
[] = 'page';
64 $this->tablesUsed
[] = 'revision';
65 $this->tablesUsed
[] = 'comment';
67 $this->tablesUsed +
= $this->getMcrTablesToReset();
70 'wgMultiContentRevisionSchemaMigrationStage',
71 $this->getMcrMigrationStage()
75 'wgContentHandlerUseDB',
76 $this->getContentHandlerUseDB()
79 $this->overrideMwServices();
82 protected function addCoreDBData() {
83 // Blank out. This would fail with a modified schema, and we don't need it.
87 * @return LoadBalancer|PHPUnit_Framework_MockObject_MockObject
89 private function getLoadBalancerMock( array $server ) {
90 $lb = $this->getMockBuilder( LoadBalancer
::class )
91 ->setMethods( [ 'reallyOpenConnection' ] )
92 ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] )
95 $lb->method( 'reallyOpenConnection' )->willReturnCallback(
96 function ( array $server, $dbNameOverride ) {
97 return $this->getDatabaseMock( $server );
105 * @return Database|PHPUnit_Framework_MockObject_MockObject
107 private function getDatabaseMock( array $params ) {
108 $db = $this->getMockBuilder( DatabaseSqlite
::class )
109 ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] )
110 ->setConstructorArgs( [ $params ] )
113 $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
114 $db->method( 'isOpen' )->willReturn( true );
119 public function provideDomainCheck() {
120 yield
[ false, 'test', '' ];
121 yield
[ 'test', 'test', '' ];
123 yield
[ false, 'test', 'foo_' ];
124 yield
[ 'test-foo_', 'test', 'foo_' ];
126 yield
[ false, 'dash-test', '' ];
127 yield
[ 'dash-test', 'dash-test', '' ];
129 yield
[ false, 'underscore_test', 'foo_' ];
130 yield
[ 'underscore_test-foo_', 'underscore_test', 'foo_' ];
134 * @dataProvider provideDomainCheck
135 * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId
137 public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) {
140 'wgDBname' => $dbName,
141 'wgDBprefix' => $dbPrefix,
145 $loadBalancer = $this->getLoadBalancerMock(
148 'dbDirectory' => '*dummy*',
150 'password' => 'test',
158 'trxProfiler' => new TransactionProfiler(),
159 'connLogger' => new \Psr\Log\
NullLogger(),
160 'queryLogger' => new \Psr\Log\
NullLogger(),
161 'errorLogger' => function () {
163 'deprecationLogger' => function () {
167 'tablePrefix' => $dbPrefix,
170 $db = $loadBalancer->getConnection( DB_REPLICA
);
172 /** @var SqlBlobStore $blobStore */
173 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
174 ->disableOriginalConstructor()
177 $store = new RevisionStore(
180 new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ),
181 MediaWikiServices
::getInstance()->getCommentStore(),
182 MediaWikiServices
::getInstance()->getContentModelStore(),
183 MediaWikiServices
::getInstance()->getSlotRoleStore(),
184 $this->getMcrMigrationStage(),
185 MediaWikiServices
::getInstance()->getActorMigration(),
189 $count = $store->countRevisionsByPageId( $db, 0 );
191 // Dummy check to make PhpUnit happy. We are really only interested in
192 // countRevisionsByPageId not failing due to the DB domain check.
193 $this->assertSame( 0, $count );
196 private function assertLinkTargetsEqual( LinkTarget
$l1, LinkTarget
$l2 ) {
197 $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() );
198 $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() );
199 $this->assertEquals( $l1->getFragment(), $l2->getFragment() );
200 $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() );
203 private function assertRevisionRecordsEqual( RevisionRecord
$r1, RevisionRecord
$r2 ) {
204 $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() );
205 $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() );
206 $this->assertEquals( $r1->getComment(), $r2->getComment() );
207 $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() );
208 $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() );
209 $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() );
210 $this->assertEquals( $r1->getSha1(), $r2->getSha1() );
211 $this->assertEquals( $r1->getParentId(), $r2->getParentId() );
212 $this->assertEquals( $r1->getSize(), $r2->getSize() );
213 $this->assertEquals( $r1->getPageId(), $r2->getPageId() );
214 $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() );
215 $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() );
216 $this->assertEquals( $r1->isMinor(), $r2->isMinor() );
217 foreach ( $r1->getSlotRoles() as $role ) {
218 $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) );
219 $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) );
222 RevisionRecord
::DELETED_TEXT
,
223 RevisionRecord
::DELETED_COMMENT
,
224 RevisionRecord
::DELETED_USER
,
225 RevisionRecord
::DELETED_RESTRICTED
,
227 $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) );
231 private function assertSlotRecordsEqual( SlotRecord
$s1, SlotRecord
$s2 ) {
232 $this->assertSame( $s1->getRole(), $s2->getRole() );
233 $this->assertSame( $s1->getModel(), $s2->getModel() );
234 $this->assertSame( $s1->getFormat(), $s2->getFormat() );
235 $this->assertSame( $s1->getSha1(), $s2->getSha1() );
236 $this->assertSame( $s1->getSize(), $s2->getSize() );
237 $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) );
239 $s1->hasRevision() ?
$this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null;
240 $s1->hasAddress() ?
$this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null;
243 private function assertRevisionCompleteness( RevisionRecord
$r ) {
244 foreach ( $r->getSlotRoles() as $role ) {
245 $this->assertSlotCompleteness( $r, $r->getSlot( $role ) );
249 private function assertSlotCompleteness( RevisionRecord
$r, SlotRecord
$slot ) {
250 $this->assertTrue( $slot->hasAddress() );
251 $this->assertSame( $r->getId(), $slot->getRevision() );
255 * @param mixed[] $details
257 * @return RevisionRecord
259 private function getRevisionRecordFromDetailsArray( $title, $details = [] ) {
260 // Convert some values that can't be provided by dataProviders
261 $page = WikiPage
::factory( $title );
262 if ( isset( $details['user'] ) && $details['user'] === true ) {
263 $details['user'] = $this->getTestUser()->getUser();
265 if ( isset( $details['page'] ) && $details['page'] === true ) {
266 $details['page'] = $page->getId();
268 if ( isset( $details['parent'] ) && $details['parent'] === true ) {
269 $details['parent'] = $page->getLatest();
272 // Create the RevisionRecord with any available data
273 $rev = new MutableRevisionRecord( $title );
274 isset( $details['slot'] ) ?
$rev->setSlot( $details['slot'] ) : null;
275 isset( $details['parent'] ) ?
$rev->setParentId( $details['parent'] ) : null;
276 isset( $details['page'] ) ?
$rev->setPageId( $details['page'] ) : null;
277 isset( $details['size'] ) ?
$rev->setSize( $details['size'] ) : null;
278 isset( $details['sha1'] ) ?
$rev->setSha1( $details['sha1'] ) : null;
279 isset( $details['comment'] ) ?
$rev->setComment( $details['comment'] ) : null;
280 isset( $details['timestamp'] ) ?
$rev->setTimestamp( $details['timestamp'] ) : null;
281 isset( $details['minor'] ) ?
$rev->setMinorEdit( $details['minor'] ) : null;
282 isset( $details['user'] ) ?
$rev->setUser( $details['user'] ) : null;
283 isset( $details['visibility'] ) ?
$rev->setVisibility( $details['visibility'] ) : null;
284 isset( $details['id'] ) ?
$rev->setId( $details['id'] ) : null;
289 public function provideInsertRevisionOn_successes() {
290 yield
'Bare minimum revision insertion' => [
291 Title
::newFromText( 'UTPage' ),
293 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
295 'comment' => $this->getRandomCommentStoreComment(),
296 'timestamp' => '20171117010101',
300 yield
'Detailed revision insertion' => [
301 Title
::newFromText( 'UTPage' ),
303 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
306 'comment' => $this->getRandomCommentStoreComment(),
307 'timestamp' => '20171117010101',
310 'visibility' => RevisionRecord
::DELETED_RESTRICTED
,
315 private function getRandomCommentStoreComment() {
316 return CommentStoreComment
::newUnsavedComment( __METHOD__
. '.' . rand( 0, 1000 ) );
320 * @dataProvider provideInsertRevisionOn_successes
321 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
322 * @covers \MediaWiki\Storage\RevisionStore::insertSlotRowOn
323 * @covers \MediaWiki\Storage\RevisionStore::insertContentRowOn
325 public function testInsertRevisionOn_successes(
327 array $revDetails = []
329 $this->getExistingTestPage( $title );
330 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
332 $this->overrideMwServices();
333 $store = MediaWikiServices
::getInstance()->getRevisionStore();
334 $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
336 $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() );
337 $this->assertRevisionRecordsEqual( $rev, $return );
338 $this->assertRevisionCompleteness( $return );
339 $this->assertRevisionExistsInDatabase( $return );
342 protected function assertRevisionExistsInDatabase( RevisionRecord
$rev ) {
344 'revision', [ 'count(*)' ], [ 'rev_id' => $rev->getId() ], [ [ '1' ] ]
349 * @param SlotRecord $a
350 * @param SlotRecord $b
352 protected function assertSameSlotContent( SlotRecord
$a, SlotRecord
$b ) {
353 // Assert that the same blob address has been used.
354 $this->assertSame( $a->getAddress(), $b->getAddress() );
358 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
360 public function testInsertRevisionOn_blobAddressExists() {
361 $page = $this->getExistingTestPage();
362 $title = $page->getTitle();
364 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
366 'comment' => $this->getRandomCommentStoreComment(),
367 'timestamp' => '20171117010101',
371 $this->overrideMwServices();
372 $store = MediaWikiServices
::getInstance()->getRevisionStore();
374 // Insert the first revision
375 $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
376 $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER
) );
377 $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() );
378 $this->assertRevisionRecordsEqual( $revOne, $firstReturn );
380 // Insert a second revision inheriting the same blob address
381 $revDetails['slot'] = SlotRecord
::newInherited( $firstReturn->getSlot( 'main' ) );
382 $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
383 $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER
) );
384 $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() );
385 $this->assertRevisionRecordsEqual( $revTwo, $secondReturn );
387 $firstMainSlot = $firstReturn->getSlot( 'main' );
388 $secondMainSlot = $secondReturn->getSlot( 'main' );
390 $this->assertSameSlotContent( $firstMainSlot, $secondMainSlot );
392 // And that different revisions have been created.
393 $this->assertNotSame( $firstReturn->getId(), $secondReturn->getId() );
395 // Make sure the slot rows reference the correct revision
396 $this->assertSame( $firstReturn->getId(), $firstMainSlot->getRevision() );
397 $this->assertSame( $secondReturn->getId(), $secondMainSlot->getRevision() );
400 public function provideInsertRevisionOn_failures() {
402 Title
::newFromText( 'UTPage' ),
404 'comment' => $this->getRandomCommentStoreComment(),
405 'timestamp' => '20171117010101',
408 new InvalidArgumentException( 'At least one slot needs to be defined!' )
410 yield
'slot that is not main slot' => [
411 Title
::newFromText( 'UTPage' ),
413 'slot' => SlotRecord
::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ),
414 'comment' => $this->getRandomCommentStoreComment(),
415 'timestamp' => '20171117010101',
418 new InvalidArgumentException( 'Only the main slot is supported for now!' )
420 yield
'no timestamp' => [
421 Title
::newFromText( 'UTPage' ),
423 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
424 'comment' => $this->getRandomCommentStoreComment(),
427 new IncompleteRevisionException( 'timestamp field must not be NULL!' )
429 yield
'no comment' => [
430 Title
::newFromText( 'UTPage' ),
432 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
433 'timestamp' => '20171117010101',
436 new IncompleteRevisionException( 'comment must not be NULL!' )
439 Title
::newFromText( 'UTPage' ),
441 'slot' => SlotRecord
::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ),
442 'comment' => $this->getRandomCommentStoreComment(),
443 'timestamp' => '20171117010101',
445 new IncompleteRevisionException( 'user must not be NULL!' )
450 * @dataProvider provideInsertRevisionOn_failures
451 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
453 public function testInsertRevisionOn_failures(
455 array $revDetails = [],
458 $page = $this->getExistingTestPage( $title );
460 $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails );
462 $store = MediaWikiServices
::getInstance()->getRevisionStore();
464 $this->setExpectedException(
465 get_class( $exception ),
466 $exception->getMessage(),
467 $exception->getCode()
469 $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER
) );
472 public function provideNewNullRevision() {
474 Title
::newFromText( __METHOD__
),
475 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment1' ),
479 Title
::newFromText( __METHOD__
),
480 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment2', [ 'a' => 1 ] ),
486 * @dataProvider provideNewNullRevision
487 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
488 * @covers \MediaWiki\Storage\RevisionStore::findSlotContentId
490 public function testNewNullRevision( Title
$title, $comment, $minor ) {
491 $this->overrideMwServices();
493 $page = $this->getExistingTestPage( $title );
494 $rev = $page->getRevision();
496 $store = MediaWikiServices
::getInstance()->getRevisionStore();
497 $user = TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser();
499 $parent = $store->getRevisionById( $rev->getId() );
500 $record = $store->newNullRevision(
501 wfGetDB( DB_MASTER
),
508 $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() );
509 $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() );
510 $this->assertEquals( $comment, $record->getComment() );
511 $this->assertEquals( $minor, $record->isMinor() );
512 $this->assertEquals( $user->getName(), $record->getUser()->getName() );
513 $this->assertEquals( $parent->getId(), $record->getParentId() );
515 $parentSlot = $parent->getSlot( 'main' );
516 $slot = $record->getSlot( 'main' );
518 $this->assertTrue( $slot->isInherited(), 'isInherited' );
519 $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' );
520 $this->assertSameSlotContent( $parentSlot, $slot );
524 * @covers \MediaWiki\Storage\RevisionStore::newNullRevision
526 public function testNewNullRevision_nonExistingTitle() {
527 $store = MediaWikiServices
::getInstance()->getRevisionStore();
528 $record = $store->newNullRevision(
529 wfGetDB( DB_MASTER
),
530 Title
::newFromText( __METHOD__
. '.iDontExist!' ),
531 CommentStoreComment
::newUnsavedComment( __METHOD__
. ' comment' ),
533 TestUserRegistry
::getMutableTestUser( __METHOD__
)->getUser()
535 $this->assertNull( $record );
539 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
541 public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() {
542 $page = $this->getExistingTestPage();
543 $status = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
544 /** @var Revision $rev */
545 $rev = $status->value
['revision'];
547 $store = MediaWikiServices
::getInstance()->getRevisionStore();
548 $revisionRecord = $store->getRevisionById( $rev->getId() );
549 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
551 $this->assertGreaterThan( 0, $result );
553 $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ),
559 * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled
561 public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() {
562 // This assumes that sysops are auto patrolled
563 $sysop = $this->getTestSysop()->getUser();
564 $page = $this->getExistingTestPage();
565 $status = $page->doEditContent(
566 new WikitextContent( __METHOD__
),
572 /** @var Revision $rev */
573 $rev = $status->value
['revision'];
575 $store = MediaWikiServices
::getInstance()->getRevisionStore();
576 $revisionRecord = $store->getRevisionById( $rev->getId() );
577 $result = $store->getRcIdIfUnpatrolled( $revisionRecord );
579 $this->assertSame( 0, $result );
583 * @covers \MediaWiki\Storage\RevisionStore::getRecentChange
585 public function testGetRecentChange() {
586 $page = $this->getExistingTestPage();
587 $content = new WikitextContent( __METHOD__
);
588 $status = $page->doEditContent( $content, __METHOD__
);
589 /** @var Revision $rev */
590 $rev = $status->value
['revision'];
592 $store = MediaWikiServices
::getInstance()->getRevisionStore();
593 $revRecord = $store->getRevisionById( $rev->getId() );
594 $recentChange = $store->getRecentChange( $revRecord );
596 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
597 $this->assertEquals( $rev->getRecentChange(), $recentChange );
601 * @covers \MediaWiki\Storage\RevisionStore::getRevisionById
603 public function testGetRevisionById() {
604 $page = $this->getExistingTestPage();
605 $content = new WikitextContent( __METHOD__
);
606 $status = $page->doEditContent( $content, __METHOD__
);
607 /** @var Revision $rev */
608 $rev = $status->value
['revision'];
610 $store = MediaWikiServices
::getInstance()->getRevisionStore();
611 $revRecord = $store->getRevisionById( $rev->getId() );
613 $this->assertSame( $rev->getId(), $revRecord->getId() );
614 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
615 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
619 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle
621 public function testGetRevisionByTitle() {
622 $page = $this->getExistingTestPage();
623 $content = new WikitextContent( __METHOD__
);
624 $status = $page->doEditContent( $content, __METHOD__
);
625 /** @var Revision $rev */
626 $rev = $status->value
['revision'];
628 $store = MediaWikiServices
::getInstance()->getRevisionStore();
629 $revRecord = $store->getRevisionByTitle( $page->getTitle() );
631 $this->assertSame( $rev->getId(), $revRecord->getId() );
632 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
633 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
637 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId
639 public function testGetRevisionByPageId() {
640 $page = $this->getExistingTestPage();
641 $content = new WikitextContent( __METHOD__
);
642 $status = $page->doEditContent( $content, __METHOD__
);
643 /** @var Revision $rev */
644 $rev = $status->value
['revision'];
646 $store = MediaWikiServices
::getInstance()->getRevisionStore();
647 $revRecord = $store->getRevisionByPageId( $page->getId() );
649 $this->assertSame( $rev->getId(), $revRecord->getId() );
650 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
651 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
655 * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp
657 public function testGetRevisionByTimestamp() {
658 // Make sure there is 1 second between the last revision and the rev we create...
659 // Otherwise we might not get the correct revision and the test may fail...
661 $page = $this->getExistingTestPage();
663 $content = new WikitextContent( __METHOD__
);
664 $status = $page->doEditContent( $content, __METHOD__
);
665 /** @var Revision $rev */
666 $rev = $status->value
['revision'];
668 $store = MediaWikiServices
::getInstance()->getRevisionStore();
669 $revRecord = $store->getRevisionByTimestamp(
674 $this->assertSame( $rev->getId(), $revRecord->getId() );
675 $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) );
676 $this->assertSame( __METHOD__
, $revRecord->getComment()->text
);
679 protected function revisionToRow( Revision
$rev ) {
680 $page = WikiPage
::factory( $rev->getTitle() );
683 'rev_id' => (string)$rev->getId(),
684 'rev_page' => (string)$rev->getPage(),
685 'rev_text_id' => (string)$rev->getTextId(),
686 'rev_timestamp' => $this->db
->timestamp( $rev->getTimestamp() ),
687 'rev_user_text' => (string)$rev->getUserText(),
688 'rev_user' => (string)$rev->getUser(),
689 'rev_minor_edit' => $rev->isMinor() ?
'1' : '0',
690 'rev_deleted' => (string)$rev->getVisibility(),
691 'rev_len' => (string)$rev->getSize(),
692 'rev_parent_id' => (string)$rev->getParentId(),
693 'rev_sha1' => (string)$rev->getSha1(),
694 'rev_comment_text' => $rev->getComment(),
695 'rev_comment_data' => null,
696 'rev_comment_cid' => null,
697 'rev_content_format' => $rev->getContentFormat(),
698 'rev_content_model' => $rev->getContentModel(),
699 'page_namespace' => (string)$page->getTitle()->getNamespace(),
700 'page_title' => $page->getTitle()->getDBkey(),
701 'page_id' => (string)$page->getId(),
702 'page_latest' => (string)$page->getLatest(),
703 'page_is_redirect' => $page->isRedirect() ?
'1' : '0',
704 'page_len' => (string)$page->getContent()->getSize(),
705 'user_name' => (string)$rev->getUserText(),
709 private function assertRevisionRecordMatchesRevision(
711 RevisionRecord
$record
713 $this->assertSame( $rev->getId(), $record->getId() );
714 $this->assertSame( $rev->getPage(), $record->getPageId() );
715 $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() );
716 $this->assertSame( $rev->getUserText(), $record->getUser()->getName() );
717 $this->assertSame( $rev->getUser(), $record->getUser()->getId() );
718 $this->assertSame( $rev->isMinor(), $record->isMinor() );
719 $this->assertSame( $rev->getVisibility(), $record->getVisibility() );
720 $this->assertSame( $rev->getSize(), $record->getSize() );
722 * @note As of MW 1.31, the database schema allows the parent ID to be
723 * NULL to indicate that it is unknown.
725 $expectedParent = $rev->getParentId();
726 if ( $expectedParent === null ) {
729 $this->assertSame( $expectedParent, $record->getParentId() );
730 $this->assertSame( $rev->getSha1(), $record->getSha1() );
731 $this->assertSame( $rev->getComment(), $record->getComment()->text
);
732 $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() );
733 $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() );
734 $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() );
736 $revRec = $rev->getRevisionRecord();
737 $revMain = $revRec->getSlot( 'main' );
738 $recMain = $record->getSlot( 'main' );
740 $this->assertSame( $revMain->hasOrigin(), $recMain->hasOrigin(), 'hasOrigin' );
741 $this->assertSame( $revMain->hasAddress(), $recMain->hasAddress(), 'hasAddress' );
742 $this->assertSame( $revMain->hasContentId(), $recMain->hasContentId(), 'hasContentId' );
744 if ( $revMain->hasOrigin() ) {
745 $this->assertSame( $revMain->getOrigin(), $recMain->getOrigin(), 'getOrigin' );
748 if ( $revMain->hasAddress() ) {
749 $this->assertSame( $revMain->getAddress(), $recMain->getAddress(), 'getAddress' );
752 if ( $revMain->hasContentId() ) {
753 $this->assertSame( $revMain->getContentId(), $recMain->getContentId(), 'getContentId' );
758 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
759 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
761 public function testNewRevisionFromRow_anonEdit() {
762 $page = $this->getExistingTestPage();
764 $text = __METHOD__
. 'a-ä';
765 /** @var Revision $rev */
766 $rev = $page->doEditContent(
767 new WikitextContent( $text ),
769 )->value
['revision'];
771 $store = MediaWikiServices
::getInstance()->getRevisionStore();
772 $record = $store->newRevisionFromRow(
773 $this->revisionToRow( $rev ),
777 $this->assertRevisionRecordMatchesRevision( $rev, $record );
778 $this->assertSame( $text, $rev->getContent()->serialize() );
782 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
783 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
785 public function testNewRevisionFromRow_anonEdit_legacyEncoding() {
786 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
787 $this->overrideMwServices();
788 $page = $this->getExistingTestPage();
790 $text = __METHOD__
. 'a-ä';
791 /** @var Revision $rev */
792 $rev = $page->doEditContent(
793 new WikitextContent( $text ),
795 )->value
['revision'];
797 $store = MediaWikiServices
::getInstance()->getRevisionStore();
798 $record = $store->newRevisionFromRow(
799 $this->revisionToRow( $rev ),
803 $this->assertRevisionRecordMatchesRevision( $rev, $record );
804 $this->assertSame( $text, $rev->getContent()->serialize() );
808 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
809 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
811 public function testNewRevisionFromRow_userEdit() {
812 $page = $this->getExistingTestPage();
814 $text = __METHOD__
. 'b-ä';
815 /** @var Revision $rev */
816 $rev = $page->doEditContent(
817 new WikitextContent( $text ),
821 $this->getTestUser()->getUser()
822 )->value
['revision'];
824 $store = MediaWikiServices
::getInstance()->getRevisionStore();
825 $record = $store->newRevisionFromRow(
826 $this->revisionToRow( $rev ),
830 $this->assertRevisionRecordMatchesRevision( $rev, $record );
831 $this->assertSame( $text, $rev->getContent()->serialize() );
835 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
837 public function testNewRevisionFromArchiveRow() {
838 $store = MediaWikiServices
::getInstance()->getRevisionStore();
839 $title = Title
::newFromText( __METHOD__
);
840 $text = __METHOD__
. '-bä';
841 $page = $this->getExistingTestPage( $title );
842 /** @var Revision $orig */
843 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
845 $page->doDeleteArticle( __METHOD__
);
847 $db = wfGetDB( DB_MASTER
);
848 $arQuery = $store->getArchiveQueryInfo();
850 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
851 __METHOD__
, [], $arQuery['joins']
853 $this->assertTrue( is_object( $res ), 'query failed' );
855 $row = $res->fetchObject();
857 $record = $store->newRevisionFromArchiveRow( $row );
859 $this->assertRevisionRecordMatchesRevision( $orig, $record );
860 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
864 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow
866 public function testNewRevisionFromArchiveRow_legacyEncoding() {
867 $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' );
868 $this->overrideMwServices();
869 $store = MediaWikiServices
::getInstance()->getRevisionStore();
870 $title = Title
::newFromText( __METHOD__
);
871 $text = __METHOD__
. '-bä';
872 $page = $this->getExistingTestPage( $title );
873 /** @var Revision $orig */
874 $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__
)
876 $page->doDeleteArticle( __METHOD__
);
878 $db = wfGetDB( DB_MASTER
);
879 $arQuery = $store->getArchiveQueryInfo();
881 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
882 __METHOD__
, [], $arQuery['joins']
884 $this->assertTrue( is_object( $res ), 'query failed' );
886 $row = $res->fetchObject();
888 $record = $store->newRevisionFromArchiveRow( $row );
890 $this->assertRevisionRecordMatchesRevision( $orig, $record );
891 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
895 * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn
897 public function testInsertRevisionOn_archive() {
898 $store = MediaWikiServices
::getInstance()->getRevisionStore();
899 $title = Title
::newFromText( __METHOD__
);
901 $page = WikiPage
::factory( $title );
902 /** @var Revision $origRev */
903 $page->doEditContent( new WikitextContent( "First" ), __METHOD__
. '-first' );
904 $origRev = $page->doEditContent( new WikitextContent( "Foo" ), __METHOD__
)
906 $orig = $origRev->getRevisionRecord();
907 $page->doDeleteArticle( __METHOD__
);
909 $db = wfGetDB( DB_MASTER
);
910 $arQuery = $store->getArchiveQueryInfo();
911 $row = $db->selectRow(
912 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
913 __METHOD__
, [], $arQuery['joins']
916 $record = $store->newRevisionFromArchiveRow( $row );
918 $restored = $store->insertRevisionOn( $record, $db );
919 $this->assertSame( $orig->getPageId(), $restored->getPageId() );
920 $this->assertSame( $orig->getId(), $restored->getId() );
921 $this->assertSame( $orig->getComment()->text
, $restored->getComment()->text
);
923 $origMain = $orig->getSlot( 'main' );
924 $restoredMain = $restored->getSlot( 'main' );
926 $origMain->getOrigin(),
927 $restoredMain->getOrigin()
930 if ( $origMain->hasContentId() ) {
932 $origMain->getContentId(),
933 $restoredMain->getContentId()
937 // NOTE: we didn't restore the page row, so we can't use RevisionStore::getRevisionById
941 [ 'rev_id' => $orig->getId() ],
942 [ [ $orig->getId() ] ]
947 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId
949 public function testLoadRevisionFromId() {
950 $title = Title
::newFromText( __METHOD__
);
951 $page = $this->getExistingTestPage( $title );
952 $rev = $page->getRevision();
954 $store = MediaWikiServices
::getInstance()->getRevisionStore();
955 $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER
), $rev->getId() );
956 $this->assertRevisionRecordMatchesRevision( $rev, $result );
960 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId
962 public function testLoadRevisionFromPageId() {
963 $title = Title
::newFromText( __METHOD__
);
964 $page = $this->getExistingTestPage( $title );
965 $rev = $page->getRevision();
967 $store = MediaWikiServices
::getInstance()->getRevisionStore();
968 $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER
), $page->getId() );
969 $this->assertRevisionRecordMatchesRevision( $rev, $result );
973 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle
975 public function testLoadRevisionFromTitle() {
976 $title = Title
::newFromText( __METHOD__
);
977 $page = $this->getExistingTestPage( $title );
978 $rev = $page->getRevision();
980 $store = MediaWikiServices
::getInstance()->getRevisionStore();
981 $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER
), $title );
982 $this->assertRevisionRecordMatchesRevision( $rev, $result );
986 * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp
988 public function testLoadRevisionFromTimestamp() {
989 $page = $this->getNonexistingTestPage( __METHOD__
);
990 $title = $page->getTitle();
991 /** @var Revision $revOne */
992 $revOne = $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
)
994 // Sleep to ensure different timestamps... )(evil)
996 /** @var Revision $revTwo */
997 $revTwo = $page->doEditContent( new WikitextContent( __METHOD__
. 'a' ), '' )
1000 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1002 $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER
), $title, '20150101010101' )
1006 $store->loadRevisionFromTimestamp(
1007 wfGetDB( DB_MASTER
),
1009 $revOne->getTimestamp()
1014 $store->loadRevisionFromTimestamp(
1015 wfGetDB( DB_MASTER
),
1017 $revTwo->getTimestamp()
1023 * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes
1025 public function testGetParentLengths() {
1026 $page = $this->getNonexistingTestPage( __METHOD__
);
1027 /** @var Revision $revOne */
1028 $revOne = $page->doEditContent(
1029 new WikitextContent( __METHOD__
), __METHOD__
1030 )->value
['revision'];
1031 /** @var Revision $revTwo */
1032 $revTwo = $page->doEditContent(
1033 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1034 )->value
['revision'];
1036 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1039 $revOne->getId() => strlen( __METHOD__
),
1041 $store->listRevisionSizes(
1042 wfGetDB( DB_MASTER
),
1043 [ $revOne->getId() ]
1048 $revOne->getId() => strlen( __METHOD__
),
1049 $revTwo->getId() => strlen( __METHOD__
) +
1,
1051 $store->listRevisionSizes(
1052 wfGetDB( DB_MASTER
),
1053 [ $revOne->getId(), $revTwo->getId() ]
1059 * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision
1061 public function testGetPreviousRevision() {
1062 $page = $this->getNonexistingTestPage( __METHOD__
);
1063 /** @var Revision $revOne */
1064 $revOne = $page->doEditContent(
1065 new WikitextContent( __METHOD__
), __METHOD__
1066 )->value
['revision'];
1067 /** @var Revision $revTwo */
1068 $revTwo = $page->doEditContent(
1069 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1070 )->value
['revision'];
1072 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1074 $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) )
1078 $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId()
1083 * @covers \MediaWiki\Storage\RevisionStore::getNextRevision
1085 public function testGetNextRevision() {
1086 $page = $this->getNonexistingTestPage( __METHOD__
);
1087 /** @var Revision $revOne */
1088 $revOne = $page->doEditContent(
1089 new WikitextContent( __METHOD__
), __METHOD__
1090 )->value
['revision'];
1091 /** @var Revision $revTwo */
1092 $revTwo = $page->doEditContent(
1093 new WikitextContent( __METHOD__
. '2' ), __METHOD__
1094 )->value
['revision'];
1096 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1099 $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId()
1102 $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) )
1107 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1109 public function testGetTimestampFromId_found() {
1110 $page = $this->getExistingTestPage();
1111 $rev = $page->getRevision();
1113 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1114 $result = $store->getTimestampFromId(
1119 $this->assertSame( $rev->getTimestamp(), $result );
1123 * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId
1125 public function testGetTimestampFromId_notFound() {
1126 $page = $this->getExistingTestPage();
1127 $rev = $page->getRevision();
1129 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1130 $result = $store->getTimestampFromId(
1135 $this->assertFalse( $result );
1139 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId
1141 public function testCountRevisionsByPageId() {
1142 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1143 $page = $this->getNonexistingTestPage( __METHOD__
);
1147 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1149 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1152 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1154 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1157 $store->countRevisionsByPageId( wfGetDB( DB_MASTER
), $page->getId() )
1162 * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle
1164 public function testCountRevisionsByTitle() {
1165 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1166 $page = $this->getNonexistingTestPage( __METHOD__
);
1170 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1172 $page->doEditContent( new WikitextContent( 'a' ), 'a' );
1175 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1177 $page->doEditContent( new WikitextContent( 'b' ), 'b' );
1180 $store->countRevisionsByTitle( wfGetDB( DB_MASTER
), $page->getTitle() )
1185 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1187 public function testUserWasLastToEdit_false() {
1188 $sysop = $this->getTestSysop()->getUser();
1189 $page = $this->getExistingTestPage();
1190 $page->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1192 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1193 $result = $store->userWasLastToEdit(
1194 wfGetDB( DB_MASTER
),
1199 $this->assertFalse( $result );
1203 * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit
1205 public function testUserWasLastToEdit_true() {
1206 $startTime = wfTimestampNow();
1207 $sysop = $this->getTestSysop()->getUser();
1208 $page = $this->getExistingTestPage();
1209 $page->doEditContent(
1210 new WikitextContent( __METHOD__
),
1217 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1218 $result = $store->userWasLastToEdit(
1219 wfGetDB( DB_MASTER
),
1224 $this->assertTrue( $result );
1228 * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision
1230 public function testGetKnownCurrentRevision() {
1231 $page = $this->getExistingTestPage();
1232 /** @var Revision $rev */
1233 $rev = $page->doEditContent(
1234 new WikitextContent( __METHOD__
. 'b' ),
1238 $this->getTestUser()->getUser()
1239 )->value
['revision'];
1241 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1242 $record = $store->getKnownCurrentRevision(
1247 $this->assertRevisionRecordMatchesRevision( $rev, $record );
1250 public function provideNewMutableRevisionFromArray() {
1251 yield
'Basic array, with page & id' => [
1256 'timestamp' => '20171017114835',
1257 'user_text' => '111.0.1.2',
1259 'minor_edit' => false,
1263 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1264 'comment' => 'Goat Comment!',
1265 'content_format' => 'text/x-wiki',
1266 'content_model' => 'wikitext',
1269 yield
'Basic array, content object' => [
1273 'timestamp' => '20171017114835',
1274 'user_text' => '111.0.1.2',
1276 'minor_edit' => false,
1280 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1281 'comment' => 'Goat Comment!',
1282 'content' => new WikitextContent( 'Some Content' ),
1285 yield
'Basic array, serialized text' => [
1289 'timestamp' => '20171017114835',
1290 'user_text' => '111.0.1.2',
1292 'minor_edit' => false,
1296 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1297 'comment' => 'Goat Comment!',
1298 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1301 yield
'Basic array, serialized text, utf-8 flags' => [
1305 'timestamp' => '20171017114835',
1306 'user_text' => '111.0.1.2',
1308 'minor_edit' => false,
1312 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1313 'comment' => 'Goat Comment!',
1314 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(),
1318 yield
'Basic array, with title' => [
1320 'title' => Title
::newFromText( 'SomeText' ),
1322 'timestamp' => '20171017114835',
1323 'user_text' => '111.0.1.2',
1325 'minor_edit' => false,
1329 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1330 'comment' => 'Goat Comment!',
1331 'content_format' => 'text/x-wiki',
1332 'content_model' => 'wikitext',
1335 yield
'Basic array, no user field' => [
1340 'timestamp' => '20171017114835',
1341 'user_text' => '111.0.1.3',
1342 'minor_edit' => false,
1346 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
1347 'comment' => 'Goat Comment!',
1348 'content_format' => 'text/x-wiki',
1349 'content_model' => 'wikitext',
1355 * @dataProvider provideNewMutableRevisionFromArray
1356 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1358 public function testNewMutableRevisionFromArray( array $array ) {
1359 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1361 // HACK: if $array['page'] is given, make sure that the page exists
1362 if ( isset( $array['page'] ) ) {
1363 $t = Title
::newFromID( $array['page'] );
1364 if ( !$t ||
!$t->exists() ) {
1365 $t = Title
::makeTitle( NS_MAIN
, __METHOD__
);
1366 $info = $this->insertPage( $t );
1367 $array['page'] = $info['id'];
1371 $result = $store->newMutableRevisionFromArray( $array );
1373 if ( isset( $array['id'] ) ) {
1374 $this->assertSame( $array['id'], $result->getId() );
1376 if ( isset( $array['page'] ) ) {
1377 $this->assertSame( $array['page'], $result->getPageId() );
1379 $this->assertSame( $array['timestamp'], $result->getTimestamp() );
1380 $this->assertSame( $array['user_text'], $result->getUser()->getName() );
1381 if ( isset( $array['user'] ) ) {
1382 $this->assertSame( $array['user'], $result->getUser()->getId() );
1384 $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() );
1385 $this->assertSame( $array['deleted'], $result->getVisibility() );
1386 $this->assertSame( $array['len'], $result->getSize() );
1387 $this->assertSame( $array['parent_id'], $result->getParentId() );
1388 $this->assertSame( $array['sha1'], $result->getSha1() );
1389 $this->assertSame( $array['comment'], $result->getComment()->text
);
1390 if ( isset( $array['content'] ) ) {
1392 $result->getSlot( 'main' )->getContent()->equals( $array['content'] )
1394 } elseif ( isset( $array['text'] ) ) {
1395 $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() );
1398 $array['content_format'],
1399 $result->getSlot( 'main' )->getContent()->getDefaultFormat()
1401 $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() );
1406 * @dataProvider provideNewMutableRevisionFromArray
1407 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
1409 public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) {
1410 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1411 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
1412 $blobStore = new SqlBlobStore( $lb, $cache );
1413 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
1415 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
1416 ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] )
1417 ->disableOriginalConstructor()
1419 $factory->expects( $this->any() )
1420 ->method( 'newBlobStore' )
1421 ->willReturn( $blobStore );
1422 $factory->expects( $this->any() )
1423 ->method( 'newSqlBlobStore' )
1424 ->willReturn( $blobStore );
1426 $this->setService( 'BlobStoreFactory', $factory );
1428 $this->testNewMutableRevisionFromArray( $array );
1431 protected function getDefaultQueryFields( $returnTextIdField = true ) {
1442 if ( $returnTextIdField ) {
1443 $fields[] = 'rev_text_id';
1448 protected function getCommentQueryFields() {
1450 'rev_comment_text' => 'rev_comment',
1451 'rev_comment_data' => 'NULL',
1452 'rev_comment_cid' => 'NULL',
1456 protected function getActorQueryFields() {
1458 'rev_user' => 'rev_user',
1459 'rev_user_text' => 'rev_user_text',
1460 'rev_actor' => 'NULL',
1464 protected function getContentHandlerQueryFields() {
1466 'rev_content_format',
1467 'rev_content_model',
1471 abstract public function provideGetQueryInfo();
1474 * @dataProvider provideGetQueryInfo
1475 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
1477 public function testGetQueryInfo( $options, $expected ) {
1478 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1480 $queryInfo = $store->getQueryInfo( $options );
1482 $this->assertArrayEqualsIgnoringIntKeyOrder(
1483 $expected['tables'],
1484 $queryInfo['tables']
1486 $this->assertArrayEqualsIgnoringIntKeyOrder(
1487 $expected['fields'],
1488 $queryInfo['fields']
1490 $this->assertArrayEqualsIgnoringIntKeyOrder(
1496 protected function getDefaultArchiveFields( $returnTextFields = true ) {
1510 if ( $returnTextFields ) {
1511 $fields[] = 'ar_text_id';
1516 abstract public function provideGetArchiveQueryInfo();
1519 * @dataProvider provideGetArchiveQueryInfo
1520 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
1522 public function testGetArchiveQueryInfo( $expected ) {
1523 $store = MediaWikiServices
::getInstance()->getRevisionStore();
1525 $archiveQueryInfo = $store->getArchiveQueryInfo();
1527 $this->assertArrayEqualsIgnoringIntKeyOrder(
1528 $expected['tables'],
1529 $archiveQueryInfo['tables']
1532 $this->assertArrayEqualsIgnoringIntKeyOrder(
1533 $expected['fields'],
1534 $archiveQueryInfo['fields']
1537 $this->assertArrayEqualsIgnoringIntKeyOrder(
1539 $archiveQueryInfo['joins']
1544 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1547 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1548 * split into a string key array and an int key array before assertions occur.
1550 * @param array $expected
1551 * @param array $actual
1553 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1554 $this->objectAssociativeSort( $expected );
1555 $this->objectAssociativeSort( $actual );
1557 // Separate the int key values from the string key values so that assertion failures are
1558 // easier to understand.
1559 $expectedIntKeyValues = [];
1560 $actualIntKeyValues = [];
1562 // Remove all int keys and re add them at the end after sorting by value
1563 // This will result in all int keys being in the same order with same ints at the end of
1565 foreach ( $expected as $key => $value ) {
1566 if ( is_int( $key ) ) {
1567 unset( $expected[$key] );
1568 $expectedIntKeyValues[] = $value;
1571 foreach ( $actual as $key => $value ) {
1572 if ( is_int( $key ) ) {
1573 unset( $actual[$key] );
1574 $actualIntKeyValues[] = $value;
1578 $this->assertArrayEquals( $expected, $actual, false, true );
1579 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );