3 namespace MediaWiki\Tests\Storage
;
7 use MediaWiki\Storage\RevisionAccessException
;
8 use MediaWiki\Storage\RevisionStore
;
9 use MediaWiki\Storage\SqlBlobStore
;
10 use MediaWikiTestCase
;
13 use Wikimedia\Rdbms\Database
;
14 use Wikimedia\Rdbms\LoadBalancer
;
16 class RevisionStoreTest
extends MediaWikiTestCase
{
19 * @param LoadBalancer $loadBalancer
20 * @param SqlBlobStore $blobStore
21 * @param WANObjectCache $WANObjectCache
23 * @return RevisionStore
25 private function getRevisionStore(
28 $WANObjectCache = null
30 return new RevisionStore(
31 $loadBalancer ?
$loadBalancer : $this->getMockLoadBalancer(),
32 $blobStore ?
$blobStore : $this->getMockSqlBlobStore(),
33 $WANObjectCache ?
$WANObjectCache : $this->getHashWANObjectCache()
38 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
40 private function getMockLoadBalancer() {
41 return $this->getMockBuilder( LoadBalancer
::class )
42 ->disableOriginalConstructor()->getMock();
46 * @return \PHPUnit_Framework_MockObject_MockObject|Database
48 private function getMockDatabase() {
49 return $this->getMockBuilder( Database
::class )
50 ->disableOriginalConstructor()->getMock();
54 * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
56 private function getMockSqlBlobStore() {
57 return $this->getMockBuilder( SqlBlobStore
::class )
58 ->disableOriginalConstructor()->getMock();
61 private function getHashWANObjectCache() {
62 return new WANObjectCache( [ 'cache' => new \
HashBagOStuff() ] );
66 * @covers \MediaWiki\Storage\RevisionStore::getContentHandlerUseDB
67 * @covers \MediaWiki\Storage\RevisionStore::setContentHandlerUseDB
69 public function testGetSetContentHandlerDb() {
70 $store = $this->getRevisionStore();
71 $this->assertTrue( $store->getContentHandlerUseDB() );
72 $store->setContentHandlerUseDB( false );
73 $this->assertFalse( $store->getContentHandlerUseDB() );
74 $store->setContentHandlerUseDB( true );
75 $this->assertTrue( $store->getContentHandlerUseDB() );
78 private function getDefaultQueryFields() {
94 private function getCommentQueryFields() {
96 'rev_comment_text' => 'rev_comment',
97 'rev_comment_data' => 'NULL',
98 'rev_comment_cid' => 'NULL',
102 private function getContentHandlerQueryFields() {
104 'rev_content_format',
109 public function provideGetQueryInfo() {
114 'tables' => [ 'revision' ],
115 'fields' => array_merge(
116 $this->getDefaultQueryFields(),
117 $this->getCommentQueryFields(),
118 $this->getContentHandlerQueryFields()
127 'tables' => [ 'revision' ],
128 'fields' => array_merge(
129 $this->getDefaultQueryFields(),
130 $this->getCommentQueryFields()
139 'tables' => [ 'revision', 'page' ],
140 'fields' => array_merge(
141 $this->getDefaultQueryFields(),
142 $this->getCommentQueryFields(),
153 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
161 'tables' => [ 'revision', 'user' ],
162 'fields' => array_merge(
163 $this->getDefaultQueryFields(),
164 $this->getCommentQueryFields(),
170 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
178 'tables' => [ 'revision', 'text' ],
179 'fields' => array_merge(
180 $this->getDefaultQueryFields(),
181 $this->getCommentQueryFields(),
188 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
194 [ 'page', 'user', 'text' ],
196 'tables' => [ 'revision', 'page', 'user', 'text' ],
197 'fields' => array_merge(
198 $this->getDefaultQueryFields(),
199 $this->getCommentQueryFields(),
200 $this->getContentHandlerQueryFields(),
214 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
215 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
216 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
223 * @dataProvider provideGetQueryInfo
224 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
226 public function testGetQueryInfo( $contentHandlerUseDb, $options, $expected ) {
227 $store = $this->getRevisionStore();
228 $store->setContentHandlerUseDB( $contentHandlerUseDb );
229 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
230 $this->assertEquals( $expected, $store->getQueryInfo( $options ) );
233 private function getDefaultArchiveFields() {
254 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
256 public function testGetArchiveQueryInfo_contentHandlerDb() {
257 $store = $this->getRevisionStore();
258 $store->setContentHandlerUseDB( true );
259 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
265 'fields' => array_merge(
266 $this->getDefaultArchiveFields(),
268 'ar_comment_text' => 'ar_comment',
269 'ar_comment_data' => 'NULL',
270 'ar_comment_cid' => 'NULL',
277 $store->getArchiveQueryInfo()
282 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
284 public function testGetArchiveQueryInfo_noContentHandlerDb() {
285 $store = $this->getRevisionStore();
286 $store->setContentHandlerUseDB( false );
287 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
293 'fields' => array_merge(
294 $this->getDefaultArchiveFields(),
296 'ar_comment_text' => 'ar_comment',
297 'ar_comment_data' => 'NULL',
298 'ar_comment_cid' => 'NULL',
303 $store->getArchiveQueryInfo()
307 public function testGetTitle_successFromPageId() {
308 $mockLoadBalancer = $this->getMockLoadBalancer();
309 // Title calls wfGetDB() so we have to set the main service
310 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
312 $db = $this->getMockDatabase();
313 // Title calls wfGetDB() which uses a regular Connection
314 $mockLoadBalancer->expects( $this->atLeastOnce() )
315 ->method( 'getConnection' )
318 // First call to Title::newFromID, faking no result (db lag?)
319 $db->expects( $this->at( 0 ) )
320 ->method( 'selectRow' )
326 ->willReturn( (object)[
327 'page_namespace' => '1',
328 'page_title' => 'Food',
331 $store = $this->getRevisionStore( $mockLoadBalancer );
332 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
334 $this->assertSame( 1, $title->getNamespace() );
335 $this->assertSame( 'Food', $title->getDBkey() );
338 public function testGetTitle_successFromPageIdOnFallback() {
339 $mockLoadBalancer = $this->getMockLoadBalancer();
340 // Title calls wfGetDB() so we have to set the main service
341 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
343 $db = $this->getMockDatabase();
344 // Title calls wfGetDB() which uses a regular Connection
345 // Assert that the first call uses a REPLICA and the second falls back to master
346 $mockLoadBalancer->expects( $this->exactly( 2 ) )
347 ->method( 'getConnection' )
349 // RevisionStore getTitle uses a ConnectionRef
350 $mockLoadBalancer->expects( $this->atLeastOnce() )
351 ->method( 'getConnectionRef' )
354 // First call to Title::newFromID, faking no result (db lag?)
355 $db->expects( $this->at( 0 ) )
356 ->method( 'selectRow' )
362 ->willReturn( false );
364 // First select using rev_id, faking no result (db lag?)
365 $db->expects( $this->at( 1 ) )
366 ->method( 'selectRow' )
368 [ 'revision', 'page' ],
372 ->willReturn( false );
374 // Second call to Title::newFromID, no result
375 $db->expects( $this->at( 2 ) )
376 ->method( 'selectRow' )
382 ->willReturn( (object)[
383 'page_namespace' => '2',
384 'page_title' => 'Foodey',
387 $store = $this->getRevisionStore( $mockLoadBalancer );
388 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
390 $this->assertSame( 2, $title->getNamespace() );
391 $this->assertSame( 'Foodey', $title->getDBkey() );
394 public function testGetTitle_successFromRevId() {
395 $mockLoadBalancer = $this->getMockLoadBalancer();
396 // Title calls wfGetDB() so we have to set the main service
397 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
399 $db = $this->getMockDatabase();
400 // Title calls wfGetDB() which uses a regular Connection
401 $mockLoadBalancer->expects( $this->atLeastOnce() )
402 ->method( 'getConnection' )
404 // RevisionStore getTitle uses a ConnectionRef
405 $mockLoadBalancer->expects( $this->atLeastOnce() )
406 ->method( 'getConnectionRef' )
409 // First call to Title::newFromID, faking no result (db lag?)
410 $db->expects( $this->at( 0 ) )
411 ->method( 'selectRow' )
417 ->willReturn( false );
419 // First select using rev_id, faking no result (db lag?)
420 $db->expects( $this->at( 1 ) )
421 ->method( 'selectRow' )
423 [ 'revision', 'page' ],
427 ->willReturn( (object)[
428 'page_namespace' => '1',
429 'page_title' => 'Food2',
432 $store = $this->getRevisionStore( $mockLoadBalancer );
433 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
435 $this->assertSame( 1, $title->getNamespace() );
436 $this->assertSame( 'Food2', $title->getDBkey() );
439 public function testGetTitle_successFromRevIdOnFallback() {
440 $mockLoadBalancer = $this->getMockLoadBalancer();
441 // Title calls wfGetDB() so we have to set the main service
442 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
444 $db = $this->getMockDatabase();
445 // Title calls wfGetDB() which uses a regular Connection
446 // Assert that the first call uses a REPLICA and the second falls back to master
447 $mockLoadBalancer->expects( $this->exactly( 2 ) )
448 ->method( 'getConnection' )
450 // RevisionStore getTitle uses a ConnectionRef
451 $mockLoadBalancer->expects( $this->atLeastOnce() )
452 ->method( 'getConnectionRef' )
455 // First call to Title::newFromID, faking no result (db lag?)
456 $db->expects( $this->at( 0 ) )
457 ->method( 'selectRow' )
463 ->willReturn( false );
465 // First select using rev_id, faking no result (db lag?)
466 $db->expects( $this->at( 1 ) )
467 ->method( 'selectRow' )
469 [ 'revision', 'page' ],
473 ->willReturn( false );
475 // Second call to Title::newFromID, no result
476 $db->expects( $this->at( 2 ) )
477 ->method( 'selectRow' )
483 ->willReturn( false );
485 // Second select using rev_id, result
486 $db->expects( $this->at( 3 ) )
487 ->method( 'selectRow' )
489 [ 'revision', 'page' ],
493 ->willReturn( (object)[
494 'page_namespace' => '2',
495 'page_title' => 'Foodey',
498 $store = $this->getRevisionStore( $mockLoadBalancer );
499 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
501 $this->assertSame( 2, $title->getNamespace() );
502 $this->assertSame( 'Foodey', $title->getDBkey() );
506 * @covers \MediaWiki\Storage\RevisionStore::getTitle
508 public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() {
509 $mockLoadBalancer = $this->getMockLoadBalancer();
510 // Title calls wfGetDB() so we have to set the main service
511 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
513 $db = $this->getMockDatabase();
514 // Title calls wfGetDB() which uses a regular Connection
515 // Assert that the first call uses a REPLICA and the second falls back to master
517 // RevisionStore getTitle uses getConnectionRef
518 // Title::newFromID uses getConnection
519 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
520 $mockLoadBalancer->expects( $this->exactly( 2 ) )
522 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
523 static $callCounter = 0;
525 // The first call should be to a REPLICA, and the second a MASTER.
526 if ( $callCounter === 1 ) {
527 $this->assertSame( DB_REPLICA
, $masterOrReplica );
528 } elseif ( $callCounter === 2 ) {
529 $this->assertSame( DB_MASTER
, $masterOrReplica );
534 // First and third call to Title::newFromID, faking no result
535 foreach ( [ 0, 2 ] as $counter ) {
536 $db->expects( $this->at( $counter ) )
537 ->method( 'selectRow' )
543 ->willReturn( false );
546 foreach ( [ 1, 3 ] as $counter ) {
547 $db->expects( $this->at( $counter ) )
548 ->method( 'selectRow' )
550 [ 'revision', 'page' ],
554 ->willReturn( false );
557 $store = $this->getRevisionStore( $mockLoadBalancer );
559 $this->setExpectedException( RevisionAccessException
::class );
560 $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
563 public function provideNewRevisionFromRow_legacyEncoding_applied() {
564 yield
'windows-1252, old_flags is empty' => [
569 'old_text' => "S\xF6me Content",
574 yield
'windows-1252, old_flags is null' => [
579 'old_text' => "S\xF6me Content",
586 * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied
588 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
589 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
591 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
592 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
594 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
595 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $locale ) );
597 $store = new RevisionStore( wfGetLB(), $blobStore, $cache );
599 $record = $store->newRevisionFromRow(
600 $this->makeRow( $row ),
602 Title
::newFromText( __METHOD__
. '-UTPage' )
605 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
609 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
610 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
612 public function testNewRevisionFromRow_legacyEncoding_ignored() {
614 'old_flags' => 'utf-8',
615 'old_text' => 'Söme Content',
618 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
620 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
621 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
623 $store = new RevisionStore( wfGetLB(), $blobStore, $cache );
625 $record = $store->newRevisionFromRow(
626 $this->makeRow( $row ),
628 Title
::newFromText( __METHOD__
. '-UTPage' )
630 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
633 private function makeRow( array $array ) {
638 'rev_timestamp' => '20110101000000',
639 'rev_user_text' => 'Tester',
641 'rev_minor_edit' => 0,
644 'rev_parent_id' => 0,
645 'rev_sha1' => 'deadbeef',
646 'rev_comment_text' => 'Testing',
647 'rev_comment_data' => '{}',
648 'rev_comment_cid' => 111,
649 'rev_content_format' => CONTENT_FORMAT_TEXT
,
650 'rev_content_model' => CONTENT_MODEL_TEXT
,
651 'page_namespace' => 0,
652 'page_title' => 'TEST',
655 'page_is_redirect' => 0,
657 'user_name' => 'Tester',
659 'old_text' => 'Hello World',
660 'old_flags' => 'utf-8',