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 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
228 $this->overrideMwServices();
229 $store = $this->getRevisionStore();
230 $store->setContentHandlerUseDB( $contentHandlerUseDb );
231 $this->assertEquals( $expected, $store->getQueryInfo( $options ) );
234 private function getDefaultArchiveFields() {
255 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
257 public function testGetArchiveQueryInfo_contentHandlerDb() {
258 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
259 $this->overrideMwServices();
260 $store = $this->getRevisionStore();
261 $store->setContentHandlerUseDB( true );
267 'fields' => array_merge(
268 $this->getDefaultArchiveFields(),
270 'ar_comment_text' => 'ar_comment',
271 'ar_comment_data' => 'NULL',
272 'ar_comment_cid' => 'NULL',
279 $store->getArchiveQueryInfo()
284 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
286 public function testGetArchiveQueryInfo_noContentHandlerDb() {
287 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
288 $this->overrideMwServices();
289 $store = $this->getRevisionStore();
290 $store->setContentHandlerUseDB( false );
296 'fields' => array_merge(
297 $this->getDefaultArchiveFields(),
299 'ar_comment_text' => 'ar_comment',
300 'ar_comment_data' => 'NULL',
301 'ar_comment_cid' => 'NULL',
306 $store->getArchiveQueryInfo()
310 public function testGetTitle_successFromPageId() {
311 $mockLoadBalancer = $this->getMockLoadBalancer();
312 // Title calls wfGetDB() so we have to set the main service
313 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
315 $db = $this->getMockDatabase();
316 // Title calls wfGetDB() which uses a regular Connection
317 $mockLoadBalancer->expects( $this->atLeastOnce() )
318 ->method( 'getConnection' )
321 // First call to Title::newFromID, faking no result (db lag?)
322 $db->expects( $this->at( 0 ) )
323 ->method( 'selectRow' )
329 ->willReturn( (object)[
330 'page_namespace' => '1',
331 'page_title' => 'Food',
334 $store = $this->getRevisionStore( $mockLoadBalancer );
335 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
337 $this->assertSame( 1, $title->getNamespace() );
338 $this->assertSame( 'Food', $title->getDBkey() );
341 public function testGetTitle_successFromPageIdOnFallback() {
342 $mockLoadBalancer = $this->getMockLoadBalancer();
343 // Title calls wfGetDB() so we have to set the main service
344 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
346 $db = $this->getMockDatabase();
347 // Title calls wfGetDB() which uses a regular Connection
348 // Assert that the first call uses a REPLICA and the second falls back to master
349 $mockLoadBalancer->expects( $this->exactly( 2 ) )
350 ->method( 'getConnection' )
352 // RevisionStore getTitle uses a ConnectionRef
353 $mockLoadBalancer->expects( $this->atLeastOnce() )
354 ->method( 'getConnectionRef' )
357 // First call to Title::newFromID, faking no result (db lag?)
358 $db->expects( $this->at( 0 ) )
359 ->method( 'selectRow' )
365 ->willReturn( false );
367 // First select using rev_id, faking no result (db lag?)
368 $db->expects( $this->at( 1 ) )
369 ->method( 'selectRow' )
371 [ 'revision', 'page' ],
375 ->willReturn( false );
377 // Second call to Title::newFromID, no result
378 $db->expects( $this->at( 2 ) )
379 ->method( 'selectRow' )
385 ->willReturn( (object)[
386 'page_namespace' => '2',
387 'page_title' => 'Foodey',
390 $store = $this->getRevisionStore( $mockLoadBalancer );
391 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
393 $this->assertSame( 2, $title->getNamespace() );
394 $this->assertSame( 'Foodey', $title->getDBkey() );
397 public function testGetTitle_successFromRevId() {
398 $mockLoadBalancer = $this->getMockLoadBalancer();
399 // Title calls wfGetDB() so we have to set the main service
400 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
402 $db = $this->getMockDatabase();
403 // Title calls wfGetDB() which uses a regular Connection
404 $mockLoadBalancer->expects( $this->atLeastOnce() )
405 ->method( 'getConnection' )
407 // RevisionStore getTitle uses a ConnectionRef
408 $mockLoadBalancer->expects( $this->atLeastOnce() )
409 ->method( 'getConnectionRef' )
412 // First call to Title::newFromID, faking no result (db lag?)
413 $db->expects( $this->at( 0 ) )
414 ->method( 'selectRow' )
420 ->willReturn( false );
422 // First select using rev_id, faking no result (db lag?)
423 $db->expects( $this->at( 1 ) )
424 ->method( 'selectRow' )
426 [ 'revision', 'page' ],
430 ->willReturn( (object)[
431 'page_namespace' => '1',
432 'page_title' => 'Food2',
435 $store = $this->getRevisionStore( $mockLoadBalancer );
436 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
438 $this->assertSame( 1, $title->getNamespace() );
439 $this->assertSame( 'Food2', $title->getDBkey() );
442 public function testGetTitle_successFromRevIdOnFallback() {
443 $mockLoadBalancer = $this->getMockLoadBalancer();
444 // Title calls wfGetDB() so we have to set the main service
445 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
447 $db = $this->getMockDatabase();
448 // Title calls wfGetDB() which uses a regular Connection
449 // Assert that the first call uses a REPLICA and the second falls back to master
450 $mockLoadBalancer->expects( $this->exactly( 2 ) )
451 ->method( 'getConnection' )
453 // RevisionStore getTitle uses a ConnectionRef
454 $mockLoadBalancer->expects( $this->atLeastOnce() )
455 ->method( 'getConnectionRef' )
458 // First call to Title::newFromID, faking no result (db lag?)
459 $db->expects( $this->at( 0 ) )
460 ->method( 'selectRow' )
466 ->willReturn( false );
468 // First select using rev_id, faking no result (db lag?)
469 $db->expects( $this->at( 1 ) )
470 ->method( 'selectRow' )
472 [ 'revision', 'page' ],
476 ->willReturn( false );
478 // Second call to Title::newFromID, no result
479 $db->expects( $this->at( 2 ) )
480 ->method( 'selectRow' )
486 ->willReturn( false );
488 // Second select using rev_id, result
489 $db->expects( $this->at( 3 ) )
490 ->method( 'selectRow' )
492 [ 'revision', 'page' ],
496 ->willReturn( (object)[
497 'page_namespace' => '2',
498 'page_title' => 'Foodey',
501 $store = $this->getRevisionStore( $mockLoadBalancer );
502 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
504 $this->assertSame( 2, $title->getNamespace() );
505 $this->assertSame( 'Foodey', $title->getDBkey() );
509 * @covers \MediaWiki\Storage\RevisionStore::getTitle
511 public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() {
512 $mockLoadBalancer = $this->getMockLoadBalancer();
513 // Title calls wfGetDB() so we have to set the main service
514 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
516 $db = $this->getMockDatabase();
517 // Title calls wfGetDB() which uses a regular Connection
518 // Assert that the first call uses a REPLICA and the second falls back to master
520 // RevisionStore getTitle uses getConnectionRef
521 // Title::newFromID uses getConnection
522 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
523 $mockLoadBalancer->expects( $this->exactly( 2 ) )
525 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
526 static $callCounter = 0;
528 // The first call should be to a REPLICA, and the second a MASTER.
529 if ( $callCounter === 1 ) {
530 $this->assertSame( DB_REPLICA
, $masterOrReplica );
531 } elseif ( $callCounter === 2 ) {
532 $this->assertSame( DB_MASTER
, $masterOrReplica );
537 // First and third call to Title::newFromID, faking no result
538 foreach ( [ 0, 2 ] as $counter ) {
539 $db->expects( $this->at( $counter ) )
540 ->method( 'selectRow' )
546 ->willReturn( false );
549 foreach ( [ 1, 3 ] as $counter ) {
550 $db->expects( $this->at( $counter ) )
551 ->method( 'selectRow' )
553 [ 'revision', 'page' ],
557 ->willReturn( false );
560 $store = $this->getRevisionStore( $mockLoadBalancer );
562 $this->setExpectedException( RevisionAccessException
::class );
563 $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
566 public function provideNewRevisionFromRow_legacyEncoding_applied() {
567 yield
'windows-1252, old_flags is empty' => [
572 'old_text' => "S\xF6me Content",
577 yield
'windows-1252, old_flags is null' => [
582 'old_text' => "S\xF6me Content",
589 * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied
591 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
592 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
594 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
595 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
597 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
598 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $locale ) );
600 $store = new RevisionStore( wfGetLB(), $blobStore, $cache );
602 $record = $store->newRevisionFromRow(
603 $this->makeRow( $row ),
605 Title
::newFromText( __METHOD__
. '-UTPage' )
608 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
612 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
613 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
615 public function testNewRevisionFromRow_legacyEncoding_ignored() {
617 'old_flags' => 'utf-8',
618 'old_text' => 'Söme Content',
621 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
623 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
624 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
626 $store = new RevisionStore( wfGetLB(), $blobStore, $cache );
628 $record = $store->newRevisionFromRow(
629 $this->makeRow( $row ),
631 Title
::newFromText( __METHOD__
. '-UTPage' )
633 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
636 private function makeRow( array $array ) {
641 'rev_timestamp' => '20110101000000',
642 'rev_user_text' => 'Tester',
644 'rev_minor_edit' => 0,
647 'rev_parent_id' => 0,
648 'rev_sha1' => 'deadbeef',
649 'rev_comment_text' => 'Testing',
650 'rev_comment_data' => '{}',
651 'rev_comment_cid' => 111,
652 'rev_content_format' => CONTENT_FORMAT_TEXT
,
653 'rev_content_model' => CONTENT_MODEL_TEXT
,
654 'page_namespace' => 0,
655 'page_title' => 'TEST',
658 'page_is_redirect' => 0,
660 'user_name' => 'Tester',
662 'old_text' => 'Hello World',
663 'old_flags' => 'utf-8',