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 public function testGetTitle_successFromPageId() {
251 $mockLoadBalancer = $this->getMockLoadBalancer();
252 // Title calls wfGetDB() so we have to set the main service
253 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
255 $db = $this->getMockDatabase();
256 // Title calls wfGetDB() which uses a regular Connection
257 $mockLoadBalancer->expects( $this->atLeastOnce() )
258 ->method( 'getConnection' )
261 // First call to Title::newFromID, faking no result (db lag?)
262 $db->expects( $this->at( 0 ) )
263 ->method( 'selectRow' )
269 ->willReturn( (object)[
270 'page_namespace' => '1',
271 'page_title' => 'Food',
274 $store = $this->getRevisionStore( $mockLoadBalancer );
275 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
277 $this->assertSame( 1, $title->getNamespace() );
278 $this->assertSame( 'Food', $title->getDBkey() );
281 public function testGetTitle_successFromPageIdOnFallback() {
282 $mockLoadBalancer = $this->getMockLoadBalancer();
283 // Title calls wfGetDB() so we have to set the main service
284 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
286 $db = $this->getMockDatabase();
287 // Title calls wfGetDB() which uses a regular Connection
288 // Assert that the first call uses a REPLICA and the second falls back to master
289 $mockLoadBalancer->expects( $this->exactly( 2 ) )
290 ->method( 'getConnection' )
292 // RevisionStore getTitle uses a ConnectionRef
293 $mockLoadBalancer->expects( $this->atLeastOnce() )
294 ->method( 'getConnectionRef' )
297 // First call to Title::newFromID, faking no result (db lag?)
298 $db->expects( $this->at( 0 ) )
299 ->method( 'selectRow' )
305 ->willReturn( false );
307 // First select using rev_id, faking no result (db lag?)
308 $db->expects( $this->at( 1 ) )
309 ->method( 'selectRow' )
311 [ 'revision', 'page' ],
315 ->willReturn( false );
317 // Second call to Title::newFromID, no result
318 $db->expects( $this->at( 2 ) )
319 ->method( 'selectRow' )
325 ->willReturn( (object)[
326 'page_namespace' => '2',
327 'page_title' => 'Foodey',
330 $store = $this->getRevisionStore( $mockLoadBalancer );
331 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
333 $this->assertSame( 2, $title->getNamespace() );
334 $this->assertSame( 'Foodey', $title->getDBkey() );
337 public function testGetTitle_successFromRevId() {
338 $mockLoadBalancer = $this->getMockLoadBalancer();
339 // Title calls wfGetDB() so we have to set the main service
340 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
342 $db = $this->getMockDatabase();
343 // Title calls wfGetDB() which uses a regular Connection
344 $mockLoadBalancer->expects( $this->atLeastOnce() )
345 ->method( 'getConnection' )
347 // RevisionStore getTitle uses a ConnectionRef
348 $mockLoadBalancer->expects( $this->atLeastOnce() )
349 ->method( 'getConnectionRef' )
352 // First call to Title::newFromID, faking no result (db lag?)
353 $db->expects( $this->at( 0 ) )
354 ->method( 'selectRow' )
360 ->willReturn( false );
362 // First select using rev_id, faking no result (db lag?)
363 $db->expects( $this->at( 1 ) )
364 ->method( 'selectRow' )
366 [ 'revision', 'page' ],
370 ->willReturn( (object)[
371 'page_namespace' => '1',
372 'page_title' => 'Food2',
375 $store = $this->getRevisionStore( $mockLoadBalancer );
376 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
378 $this->assertSame( 1, $title->getNamespace() );
379 $this->assertSame( 'Food2', $title->getDBkey() );
382 public function testGetTitle_successFromRevIdOnFallback() {
383 $mockLoadBalancer = $this->getMockLoadBalancer();
384 // Title calls wfGetDB() so we have to set the main service
385 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
387 $db = $this->getMockDatabase();
388 // Title calls wfGetDB() which uses a regular Connection
389 // Assert that the first call uses a REPLICA and the second falls back to master
390 $mockLoadBalancer->expects( $this->exactly( 2 ) )
391 ->method( 'getConnection' )
393 // RevisionStore getTitle uses a ConnectionRef
394 $mockLoadBalancer->expects( $this->atLeastOnce() )
395 ->method( 'getConnectionRef' )
398 // First call to Title::newFromID, faking no result (db lag?)
399 $db->expects( $this->at( 0 ) )
400 ->method( 'selectRow' )
406 ->willReturn( false );
408 // First select using rev_id, faking no result (db lag?)
409 $db->expects( $this->at( 1 ) )
410 ->method( 'selectRow' )
412 [ 'revision', 'page' ],
416 ->willReturn( false );
418 // Second call to Title::newFromID, no result
419 $db->expects( $this->at( 2 ) )
420 ->method( 'selectRow' )
426 ->willReturn( false );
428 // Second select using rev_id, result
429 $db->expects( $this->at( 3 ) )
430 ->method( 'selectRow' )
432 [ 'revision', 'page' ],
436 ->willReturn( (object)[
437 'page_namespace' => '2',
438 'page_title' => 'Foodey',
441 $store = $this->getRevisionStore( $mockLoadBalancer );
442 $title = $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
444 $this->assertSame( 2, $title->getNamespace() );
445 $this->assertSame( 'Foodey', $title->getDBkey() );
449 * @covers \MediaWiki\Storage\RevisionStore::getTitle
451 public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() {
452 $mockLoadBalancer = $this->getMockLoadBalancer();
453 // Title calls wfGetDB() so we have to set the main service
454 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
456 $db = $this->getMockDatabase();
457 // Title calls wfGetDB() which uses a regular Connection
458 // Assert that the first call uses a REPLICA and the second falls back to master
460 // RevisionStore getTitle uses getConnectionRef
461 // Title::newFromID uses getConnection
462 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
463 $mockLoadBalancer->expects( $this->exactly( 2 ) )
465 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
466 static $callCounter = 0;
468 // The first call should be to a REPLICA, and the second a MASTER.
469 if ( $callCounter === 1 ) {
470 $this->assertSame( DB_REPLICA
, $masterOrReplica );
471 } elseif ( $callCounter === 2 ) {
472 $this->assertSame( DB_MASTER
, $masterOrReplica );
477 // First and third call to Title::newFromID, faking no result
478 foreach ( [ 0, 2 ] as $counter ) {
479 $db->expects( $this->at( $counter ) )
480 ->method( 'selectRow' )
486 ->willReturn( false );
489 foreach ( [ 1, 3 ] as $counter ) {
490 $db->expects( $this->at( $counter ) )
491 ->method( 'selectRow' )
493 [ 'revision', 'page' ],
497 ->willReturn( false );
500 $store = $this->getRevisionStore( $mockLoadBalancer );
502 $this->setExpectedException( RevisionAccessException
::class );
503 $store->getTitle( 1, 2, RevisionStore
::READ_NORMAL
);
506 public function provideNewRevisionFromRow_legacyEncoding_applied() {
507 yield
'windows-1252, old_flags is empty' => [
512 'old_text' => "S\xF6me Content",
517 yield
'windows-1252, old_flags is null' => [
522 'old_text' => "S\xF6me Content",
529 * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied
531 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
532 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
534 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
535 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
536 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
538 $blobStore = new SqlBlobStore( $lb, $cache );
539 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $locale ) );
541 $store = $this->getRevisionStore( $lb, $blobStore, $cache );
543 $record = $store->newRevisionFromRow(
544 $this->makeRow( $row ),
546 Title
::newFromText( __METHOD__
. '-UTPage' )
549 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
553 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
554 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
556 public function testNewRevisionFromRow_legacyEncoding_ignored() {
558 'old_flags' => 'utf-8',
559 'old_text' => 'Söme Content',
562 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
563 $lb = MediaWikiServices
::getInstance()->getDBLoadBalancer();
565 $blobStore = new SqlBlobStore( $lb, $cache );
566 $blobStore->setLegacyEncoding( 'windows-1252', Language
::factory( 'en' ) );
568 $store = $this->getRevisionStore( $lb, $blobStore, $cache );
570 $record = $store->newRevisionFromRow(
571 $this->makeRow( $row ),
573 Title
::newFromText( __METHOD__
. '-UTPage' )
575 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
578 private function makeRow( array $array ) {
583 'rev_timestamp' => '20110101000000',
584 'rev_user_text' => 'Tester',
586 'rev_minor_edit' => 0,
589 'rev_parent_id' => 0,
590 'rev_sha1' => 'deadbeef',
591 'rev_comment_text' => 'Testing',
592 'rev_comment_data' => '{}',
593 'rev_comment_cid' => 111,
594 'rev_content_format' => CONTENT_FORMAT_TEXT
,
595 'rev_content_model' => CONTENT_MODEL_TEXT
,
596 'page_namespace' => 0,
597 'page_title' => 'TEST',
600 'page_is_redirect' => 0,
602 'user_name' => 'Tester',
604 'old_text' => 'Hello World',
605 'old_flags' => 'utf-8',