3 namespace MediaWiki\Tests\Storage
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Storage\RevisionAccessException
;
9 use MediaWiki\Storage\RevisionStore
;
10 use MediaWiki\Storage\SqlBlobStore
;
11 use MediaWikiTestCase
;
14 use Wikimedia\Rdbms\Database
;
15 use Wikimedia\Rdbms\LoadBalancer
;
17 class RevisionStoreTest
extends MediaWikiTestCase
{
20 * @param LoadBalancer $loadBalancer
21 * @param SqlBlobStore $blobStore
22 * @param WANObjectCache $WANObjectCache
24 * @return RevisionStore
26 private function getRevisionStore(
29 $WANObjectCache = null
31 return new RevisionStore(
32 $loadBalancer ?
$loadBalancer : $this->getMockLoadBalancer(),
33 $blobStore ?
$blobStore : $this->getMockSqlBlobStore(),
34 $WANObjectCache ?
$WANObjectCache : $this->getHashWANObjectCache(),
35 MediaWikiServices
::getInstance()->getCommentStore(),
36 MediaWikiServices
::getInstance()->getActorMigration()
41 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
43 private function getMockLoadBalancer() {
44 return $this->getMockBuilder( LoadBalancer
::class )
45 ->disableOriginalConstructor()->getMock();
49 * @return \PHPUnit_Framework_MockObject_MockObject|Database
51 private function getMockDatabase() {
52 return $this->getMockBuilder( Database
::class )
53 ->disableOriginalConstructor()->getMock();
57 * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
59 private function getMockSqlBlobStore() {
60 return $this->getMockBuilder( SqlBlobStore
::class )
61 ->disableOriginalConstructor()->getMock();
64 private function getHashWANObjectCache() {
65 return new WANObjectCache( [ 'cache' => new \
HashBagOStuff() ] );
69 * @covers \MediaWiki\Storage\RevisionStore::getContentHandlerUseDB
70 * @covers \MediaWiki\Storage\RevisionStore::setContentHandlerUseDB
72 public function testGetSetContentHandlerDb() {
73 $store = $this->getRevisionStore();
74 $this->assertTrue( $store->getContentHandlerUseDB() );
75 $store->setContentHandlerUseDB( false );
76 $this->assertFalse( $store->getContentHandlerUseDB() );
77 $store->setContentHandlerUseDB( true );
78 $this->assertTrue( $store->getContentHandlerUseDB() );
81 private function getDefaultQueryFields() {
95 private function getCommentQueryFields() {
97 'rev_comment_text' => 'rev_comment',
98 'rev_comment_data' => 'NULL',
99 'rev_comment_cid' => 'NULL',
103 private function getActorQueryFields() {
105 'rev_user' => 'rev_user',
106 'rev_user_text' => 'rev_user_text',
107 'rev_actor' => 'NULL',
111 private function getContentHandlerQueryFields() {
113 'rev_content_format',
118 public function provideGetQueryInfo() {
123 'tables' => [ 'revision' ],
124 'fields' => array_merge(
125 $this->getDefaultQueryFields(),
126 $this->getCommentQueryFields(),
127 $this->getActorQueryFields(),
128 $this->getContentHandlerQueryFields()
137 'tables' => [ 'revision' ],
138 'fields' => array_merge(
139 $this->getDefaultQueryFields(),
140 $this->getCommentQueryFields(),
141 $this->getActorQueryFields()
150 'tables' => [ 'revision', 'page' ],
151 'fields' => array_merge(
152 $this->getDefaultQueryFields(),
153 $this->getCommentQueryFields(),
154 $this->getActorQueryFields(),
165 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
173 'tables' => [ 'revision', 'user' ],
174 'fields' => array_merge(
175 $this->getDefaultQueryFields(),
176 $this->getCommentQueryFields(),
177 $this->getActorQueryFields(),
183 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
191 'tables' => [ 'revision', 'text' ],
192 'fields' => array_merge(
193 $this->getDefaultQueryFields(),
194 $this->getCommentQueryFields(),
195 $this->getActorQueryFields(),
202 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
208 [ 'page', 'user', 'text' ],
210 'tables' => [ 'revision', 'page', 'user', 'text' ],
211 'fields' => array_merge(
212 $this->getDefaultQueryFields(),
213 $this->getCommentQueryFields(),
214 $this->getActorQueryFields(),
215 $this->getContentHandlerQueryFields(),
229 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
230 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
231 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
238 * @dataProvider provideGetQueryInfo
239 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
241 public function testGetQueryInfo( $contentHandlerUseDb, $options, $expected ) {
242 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
243 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
244 $this->overrideMwServices();
245 $store = $this->getRevisionStore();
246 $store->setContentHandlerUseDB( $contentHandlerUseDb );
247 $this->assertEquals( $expected, $store->getQueryInfo( $options ) );
250 private function getDefaultArchiveFields() {
269 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
271 public function testGetArchiveQueryInfo_contentHandlerDb() {
272 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
273 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
274 $this->overrideMwServices();
275 $store = $this->getRevisionStore();
276 $store->setContentHandlerUseDB( true );
282 'fields' => array_merge(
283 $this->getDefaultArchiveFields(),
285 'ar_comment_text' => 'ar_comment',
286 'ar_comment_data' => 'NULL',
287 'ar_comment_cid' => 'NULL',
288 'ar_user_text' => 'ar_user_text',
289 'ar_user' => 'ar_user',
290 'ar_actor' => 'NULL',
297 $store->getArchiveQueryInfo()
302 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
304 public function testGetArchiveQueryInfo_noContentHandlerDb() {
305 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
306 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
307 $this->overrideMwServices();
308 $store = $this->getRevisionStore();
309 $store->setContentHandlerUseDB( false );
315 'fields' => array_merge(
316 $this->getDefaultArchiveFields(),
318 'ar_comment_text' => 'ar_comment',
319 'ar_comment_data' => 'NULL',
320 'ar_comment_cid' => 'NULL',
321 'ar_user_text' => 'ar_user_text',
322 'ar_user' => 'ar_user',
323 'ar_actor' => 'NULL',
328 $store->getArchiveQueryInfo()
332 public function testGetTitle_successFromPageId() {
333 $mockLoadBalancer = $this->getMockLoadBalancer();
334 // Title calls wfGetDB() so we have to set the main service
335 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
337 $db = $this->getMockDatabase();
338 // Title calls wfGetDB() which uses a regular Connection
339 $mockLoadBalancer->expects( $this->atLeastOnce() )
340 ->method( 'getConnection' )
343 // First call to Title::newFromID, faking no result (db lag?)
344 $db->expects( $this->at( 0 ) )
345 ->method( 'selectRow' )
351 ->willReturn( (object)[
352 'page_namespace' => '1',
353 'page_title' => 'Food',
356 $store = $this->getRevisionStore( $mockLoadBalancer );
357 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
359 $this->assertSame( 1, $title->getNamespace() );
360 $this->assertSame( 'Food', $title->getDBkey() );
363 public function testGetTitle_successFromPageIdOnFallback() {
364 $mockLoadBalancer = $this->getMockLoadBalancer();
365 // Title calls wfGetDB() so we have to set the main service
366 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
368 $db = $this->getMockDatabase();
369 // Title calls wfGetDB() which uses a regular Connection
370 // Assert that the first call uses a REPLICA and the second falls back to master
371 $mockLoadBalancer->expects( $this->exactly( 2 ) )
372 ->method( 'getConnection' )
374 // RevisionStore getTitle uses a ConnectionRef
375 $mockLoadBalancer->expects( $this->atLeastOnce() )
376 ->method( 'getConnectionRef' )
379 // First call to Title::newFromID, faking no result (db lag?)
380 $db->expects( $this->at( 0 ) )
381 ->method( 'selectRow' )
387 ->willReturn( false );
389 // First select using rev_id, faking no result (db lag?)
390 $db->expects( $this->at( 1 ) )
391 ->method( 'selectRow' )
393 [ 'revision', 'page' ],
397 ->willReturn( false );
399 // Second call to Title::newFromID, no result
400 $db->expects( $this->at( 2 ) )
401 ->method( 'selectRow' )
407 ->willReturn( (object)[
408 'page_namespace' => '2',
409 'page_title' => 'Foodey',
412 $store = $this->getRevisionStore( $mockLoadBalancer );
413 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
415 $this->assertSame( 2, $title->getNamespace() );
416 $this->assertSame( 'Foodey', $title->getDBkey() );
419 public function testGetTitle_successFromRevId() {
420 $mockLoadBalancer = $this->getMockLoadBalancer();
421 // Title calls wfGetDB() so we have to set the main service
422 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
424 $db = $this->getMockDatabase();
425 // Title calls wfGetDB() which uses a regular Connection
426 $mockLoadBalancer->expects( $this->atLeastOnce() )
427 ->method( 'getConnection' )
429 // RevisionStore getTitle uses a ConnectionRef
430 $mockLoadBalancer->expects( $this->atLeastOnce() )
431 ->method( 'getConnectionRef' )
434 // First call to Title::newFromID, faking no result (db lag?)
435 $db->expects( $this->at( 0 ) )
436 ->method( 'selectRow' )
442 ->willReturn( false );
444 // First select using rev_id, faking no result (db lag?)
445 $db->expects( $this->at( 1 ) )
446 ->method( 'selectRow' )
448 [ 'revision', 'page' ],
452 ->willReturn( (object)[
453 'page_namespace' => '1',
454 'page_title' => 'Food2',
457 $store = $this->getRevisionStore( $mockLoadBalancer );
458 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
460 $this->assertSame( 1, $title->getNamespace() );
461 $this->assertSame( 'Food2', $title->getDBkey() );
464 public function testGetTitle_successFromRevIdOnFallback() {
465 $mockLoadBalancer = $this->getMockLoadBalancer();
466 // Title calls wfGetDB() so we have to set the main service
467 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
469 $db = $this->getMockDatabase();
470 // Title calls wfGetDB() which uses a regular Connection
471 // Assert that the first call uses a REPLICA and the second falls back to master
472 $mockLoadBalancer->expects( $this->exactly( 2 ) )
473 ->method( 'getConnection' )
475 // RevisionStore getTitle uses a ConnectionRef
476 $mockLoadBalancer->expects( $this->atLeastOnce() )
477 ->method( 'getConnectionRef' )
480 // First call to Title::newFromID, faking no result (db lag?)
481 $db->expects( $this->at( 0 ) )
482 ->method( 'selectRow' )
488 ->willReturn( false );
490 // First select using rev_id, faking no result (db lag?)
491 $db->expects( $this->at( 1 ) )
492 ->method( 'selectRow' )
494 [ 'revision', 'page' ],
498 ->willReturn( false );
500 // Second call to Title::newFromID, no result
501 $db->expects( $this->at( 2 ) )
502 ->method( 'selectRow' )
508 ->willReturn( false );
510 // Second select using rev_id, result
511 $db->expects( $this->at( 3 ) )
512 ->method( 'selectRow' )
514 [ 'revision', 'page' ],
518 ->willReturn( (object)[
519 'page_namespace' => '2',
520 'page_title' => 'Foodey',
523 $store = $this->getRevisionStore( $mockLoadBalancer );
524 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
526 $this->assertSame( 2, $title->getNamespace() );
527 $this->assertSame( 'Foodey', $title->getDBkey() );
531 * @covers \MediaWiki\Storage\RevisionStore::getTitle
533 public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() {
534 $mockLoadBalancer = $this->getMockLoadBalancer();
535 // Title calls wfGetDB() so we have to set the main service
536 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
538 $db = $this->getMockDatabase();
539 // Title calls wfGetDB() which uses a regular Connection
540 // Assert that the first call uses a REPLICA and the second falls back to master
542 // RevisionStore getTitle uses getConnectionRef
543 // Title::newFromID uses getConnection
544 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
545 $mockLoadBalancer->expects( $this->exactly( 2 ) )
547 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
548 static $callCounter = 0;
550 // The first call should be to a REPLICA, and the second a MASTER.
551 if ( $callCounter === 1 ) {
552 $this->assertSame( DB_REPLICA
, $masterOrReplica );
553 } elseif ( $callCounter === 2 ) {
554 $this->assertSame( DB_MASTER
, $masterOrReplica );
559 // First and third call to Title::newFromID, faking no result
560 foreach ( [ 0, 2 ] as $counter ) {
561 $db->expects( $this->at( $counter ) )
562 ->method( 'selectRow' )
568 ->willReturn( false );
571 foreach ( [ 1, 3 ] as $counter ) {
572 $db->expects( $this->at( $counter ) )
573 ->method( 'selectRow' )
575 [ 'revision', 'page' ],
579 ->willReturn( false );
582 $store = $this->getRevisionStore( $mockLoadBalancer );
584 $this->setExpectedException( RevisionAccessException
::class );
585 $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
588 public function provideNewRevisionFromRow_legacyEncoding_applied() {
589 yield
'windows-1252, old_flags is empty' => [
594 'old_text' => "S\xF6me Content",
599 yield
'windows-1252, old_flags is null' => [
604 'old_text' => "S\xF6me Content",
611 * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied
613 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
614 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
616 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
617 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
619 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
620 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $locale ) );
622 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
624 $record = $store->newRevisionFromRow(
625 $this->makeRow( $row ),
627 Title
::newFromText( __METHOD__
. '-UTPage' )
630 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
634 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
635 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
637 public function testNewRevisionFromRow_legacyEncoding_ignored() {
639 'old_flags' => 'utf-8',
640 'old_text' => 'Söme Content',
643 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
645 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
646 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
648 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
650 $record = $store->newRevisionFromRow(
651 $this->makeRow( $row ),
653 Title
::newFromText( __METHOD__
. '-UTPage' )
655 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
658 private function makeRow( array $array ) {
663 'rev_timestamp' => '20110101000000',
664 'rev_user_text' => 'Tester',
666 'rev_minor_edit' => 0,
669 'rev_parent_id' => 0,
670 'rev_sha1' => 'deadbeef',
671 'rev_comment_text' => 'Testing',
672 'rev_comment_data' => '{}',
673 'rev_comment_cid' => 111,
674 'rev_content_format' => CONTENT_FORMAT_TEXT
,
675 'rev_content_model' => CONTENT_MODEL_TEXT
,
676 'page_namespace' => 0,
677 'page_title' => 'TEST',
680 'page_is_redirect' => 0,
682 'user_name' => 'Tester',
684 'old_text' => 'Hello World',
685 'old_flags' => 'utf-8',