Drop archive.ar_text and ar_flags
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / RevisionStoreTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use HashBagOStuff;
6 use Language;
7 use MediaWiki\MediaWikiServices;
8 use MediaWiki\Storage\RevisionAccessException;
9 use MediaWiki\Storage\RevisionStore;
10 use MediaWiki\Storage\SqlBlobStore;
11 use MediaWikiTestCase;
12 use Title;
13 use WANObjectCache;
14 use Wikimedia\Rdbms\Database;
15 use Wikimedia\Rdbms\LoadBalancer;
16
17 class RevisionStoreTest extends MediaWikiTestCase {
18
19 /**
20 * @param LoadBalancer $loadBalancer
21 * @param SqlBlobStore $blobStore
22 * @param WANObjectCache $WANObjectCache
23 *
24 * @return RevisionStore
25 */
26 private function getRevisionStore(
27 $loadBalancer = null,
28 $blobStore = null,
29 $WANObjectCache = null
30 ) {
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()
37 );
38 }
39
40 /**
41 * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer
42 */
43 private function getMockLoadBalancer() {
44 return $this->getMockBuilder( LoadBalancer::class )
45 ->disableOriginalConstructor()->getMock();
46 }
47
48 /**
49 * @return \PHPUnit_Framework_MockObject_MockObject|Database
50 */
51 private function getMockDatabase() {
52 return $this->getMockBuilder( Database::class )
53 ->disableOriginalConstructor()->getMock();
54 }
55
56 /**
57 * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
58 */
59 private function getMockSqlBlobStore() {
60 return $this->getMockBuilder( SqlBlobStore::class )
61 ->disableOriginalConstructor()->getMock();
62 }
63
64 private function getHashWANObjectCache() {
65 return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] );
66 }
67
68 /**
69 * @covers \MediaWiki\Storage\RevisionStore::getContentHandlerUseDB
70 * @covers \MediaWiki\Storage\RevisionStore::setContentHandlerUseDB
71 */
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() );
79 }
80
81 private function getDefaultQueryFields() {
82 return [
83 'rev_id',
84 'rev_page',
85 'rev_text_id',
86 'rev_timestamp',
87 'rev_minor_edit',
88 'rev_deleted',
89 'rev_len',
90 'rev_parent_id',
91 'rev_sha1',
92 ];
93 }
94
95 private function getCommentQueryFields() {
96 return [
97 'rev_comment_text' => 'rev_comment',
98 'rev_comment_data' => 'NULL',
99 'rev_comment_cid' => 'NULL',
100 ];
101 }
102
103 private function getActorQueryFields() {
104 return [
105 'rev_user' => 'rev_user',
106 'rev_user_text' => 'rev_user_text',
107 'rev_actor' => 'NULL',
108 ];
109 }
110
111 private function getContentHandlerQueryFields() {
112 return [
113 'rev_content_format',
114 'rev_content_model',
115 ];
116 }
117
118 public function provideGetQueryInfo() {
119 yield [
120 true,
121 [],
122 [
123 'tables' => [ 'revision' ],
124 'fields' => array_merge(
125 $this->getDefaultQueryFields(),
126 $this->getCommentQueryFields(),
127 $this->getActorQueryFields(),
128 $this->getContentHandlerQueryFields()
129 ),
130 'joins' => [],
131 ]
132 ];
133 yield [
134 false,
135 [],
136 [
137 'tables' => [ 'revision' ],
138 'fields' => array_merge(
139 $this->getDefaultQueryFields(),
140 $this->getCommentQueryFields(),
141 $this->getActorQueryFields()
142 ),
143 'joins' => [],
144 ]
145 ];
146 yield [
147 false,
148 [ 'page' ],
149 [
150 'tables' => [ 'revision', 'page' ],
151 'fields' => array_merge(
152 $this->getDefaultQueryFields(),
153 $this->getCommentQueryFields(),
154 $this->getActorQueryFields(),
155 [
156 'page_namespace',
157 'page_title',
158 'page_id',
159 'page_latest',
160 'page_is_redirect',
161 'page_len',
162 ]
163 ),
164 'joins' => [
165 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
166 ],
167 ]
168 ];
169 yield [
170 false,
171 [ 'user' ],
172 [
173 'tables' => [ 'revision', 'user' ],
174 'fields' => array_merge(
175 $this->getDefaultQueryFields(),
176 $this->getCommentQueryFields(),
177 $this->getActorQueryFields(),
178 [
179 'user_name',
180 ]
181 ),
182 'joins' => [
183 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
184 ],
185 ]
186 ];
187 yield [
188 false,
189 [ 'text' ],
190 [
191 'tables' => [ 'revision', 'text' ],
192 'fields' => array_merge(
193 $this->getDefaultQueryFields(),
194 $this->getCommentQueryFields(),
195 $this->getActorQueryFields(),
196 [
197 'old_text',
198 'old_flags',
199 ]
200 ),
201 'joins' => [
202 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ],
203 ],
204 ]
205 ];
206 yield [
207 true,
208 [ 'page', 'user', 'text' ],
209 [
210 'tables' => [ 'revision', 'page', 'user', 'text' ],
211 'fields' => array_merge(
212 $this->getDefaultQueryFields(),
213 $this->getCommentQueryFields(),
214 $this->getActorQueryFields(),
215 $this->getContentHandlerQueryFields(),
216 [
217 'page_namespace',
218 'page_title',
219 'page_id',
220 'page_latest',
221 'page_is_redirect',
222 'page_len',
223 'user_name',
224 'old_text',
225 'old_flags',
226 ]
227 ),
228 'joins' => [
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' ] ],
232 ],
233 ]
234 ];
235 }
236
237 /**
238 * @dataProvider provideGetQueryInfo
239 * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo
240 */
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 ) );
248 }
249
250 private function getDefaultArchiveFields() {
251 return [
252 'ar_id',
253 'ar_page_id',
254 'ar_namespace',
255 'ar_title',
256 'ar_rev_id',
257 'ar_text_id',
258 'ar_timestamp',
259 'ar_minor_edit',
260 'ar_deleted',
261 'ar_len',
262 'ar_parent_id',
263 'ar_sha1',
264 ];
265 }
266
267 /**
268 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
269 */
270 public function testGetArchiveQueryInfo_contentHandlerDb() {
271 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
272 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
273 $this->overrideMwServices();
274 $store = $this->getRevisionStore();
275 $store->setContentHandlerUseDB( true );
276 $this->assertEquals(
277 [
278 'tables' => [
279 'archive'
280 ],
281 'fields' => array_merge(
282 $this->getDefaultArchiveFields(),
283 [
284 'ar_comment_text' => 'ar_comment',
285 'ar_comment_data' => 'NULL',
286 'ar_comment_cid' => 'NULL',
287 'ar_user_text' => 'ar_user_text',
288 'ar_user' => 'ar_user',
289 'ar_actor' => 'NULL',
290 'ar_content_format',
291 'ar_content_model',
292 ]
293 ),
294 'joins' => [],
295 ],
296 $store->getArchiveQueryInfo()
297 );
298 }
299
300 /**
301 * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo
302 */
303 public function testGetArchiveQueryInfo_noContentHandlerDb() {
304 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
305 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
306 $this->overrideMwServices();
307 $store = $this->getRevisionStore();
308 $store->setContentHandlerUseDB( false );
309 $this->assertEquals(
310 [
311 'tables' => [
312 'archive'
313 ],
314 'fields' => array_merge(
315 $this->getDefaultArchiveFields(),
316 [
317 'ar_comment_text' => 'ar_comment',
318 'ar_comment_data' => 'NULL',
319 'ar_comment_cid' => 'NULL',
320 'ar_user_text' => 'ar_user_text',
321 'ar_user' => 'ar_user',
322 'ar_actor' => 'NULL',
323 ]
324 ),
325 'joins' => [],
326 ],
327 $store->getArchiveQueryInfo()
328 );
329 }
330
331 public function testGetTitle_successFromPageId() {
332 $mockLoadBalancer = $this->getMockLoadBalancer();
333 // Title calls wfGetDB() so we have to set the main service
334 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
335
336 $db = $this->getMockDatabase();
337 // Title calls wfGetDB() which uses a regular Connection
338 $mockLoadBalancer->expects( $this->atLeastOnce() )
339 ->method( 'getConnection' )
340 ->willReturn( $db );
341
342 // First call to Title::newFromID, faking no result (db lag?)
343 $db->expects( $this->at( 0 ) )
344 ->method( 'selectRow' )
345 ->with(
346 'page',
347 $this->anything(),
348 [ 'page_id' => 1 ]
349 )
350 ->willReturn( (object)[
351 'page_namespace' => '1',
352 'page_title' => 'Food',
353 ] );
354
355 $store = $this->getRevisionStore( $mockLoadBalancer );
356 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
357
358 $this->assertSame( 1, $title->getNamespace() );
359 $this->assertSame( 'Food', $title->getDBkey() );
360 }
361
362 public function testGetTitle_successFromPageIdOnFallback() {
363 $mockLoadBalancer = $this->getMockLoadBalancer();
364 // Title calls wfGetDB() so we have to set the main service
365 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
366
367 $db = $this->getMockDatabase();
368 // Title calls wfGetDB() which uses a regular Connection
369 // Assert that the first call uses a REPLICA and the second falls back to master
370 $mockLoadBalancer->expects( $this->exactly( 2 ) )
371 ->method( 'getConnection' )
372 ->willReturn( $db );
373 // RevisionStore getTitle uses a ConnectionRef
374 $mockLoadBalancer->expects( $this->atLeastOnce() )
375 ->method( 'getConnectionRef' )
376 ->willReturn( $db );
377
378 // First call to Title::newFromID, faking no result (db lag?)
379 $db->expects( $this->at( 0 ) )
380 ->method( 'selectRow' )
381 ->with(
382 'page',
383 $this->anything(),
384 [ 'page_id' => 1 ]
385 )
386 ->willReturn( false );
387
388 // First select using rev_id, faking no result (db lag?)
389 $db->expects( $this->at( 1 ) )
390 ->method( 'selectRow' )
391 ->with(
392 [ 'revision', 'page' ],
393 $this->anything(),
394 [ 'rev_id' => 2 ]
395 )
396 ->willReturn( false );
397
398 // Second call to Title::newFromID, no result
399 $db->expects( $this->at( 2 ) )
400 ->method( 'selectRow' )
401 ->with(
402 'page',
403 $this->anything(),
404 [ 'page_id' => 1 ]
405 )
406 ->willReturn( (object)[
407 'page_namespace' => '2',
408 'page_title' => 'Foodey',
409 ] );
410
411 $store = $this->getRevisionStore( $mockLoadBalancer );
412 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
413
414 $this->assertSame( 2, $title->getNamespace() );
415 $this->assertSame( 'Foodey', $title->getDBkey() );
416 }
417
418 public function testGetTitle_successFromRevId() {
419 $mockLoadBalancer = $this->getMockLoadBalancer();
420 // Title calls wfGetDB() so we have to set the main service
421 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
422
423 $db = $this->getMockDatabase();
424 // Title calls wfGetDB() which uses a regular Connection
425 $mockLoadBalancer->expects( $this->atLeastOnce() )
426 ->method( 'getConnection' )
427 ->willReturn( $db );
428 // RevisionStore getTitle uses a ConnectionRef
429 $mockLoadBalancer->expects( $this->atLeastOnce() )
430 ->method( 'getConnectionRef' )
431 ->willReturn( $db );
432
433 // First call to Title::newFromID, faking no result (db lag?)
434 $db->expects( $this->at( 0 ) )
435 ->method( 'selectRow' )
436 ->with(
437 'page',
438 $this->anything(),
439 [ 'page_id' => 1 ]
440 )
441 ->willReturn( false );
442
443 // First select using rev_id, faking no result (db lag?)
444 $db->expects( $this->at( 1 ) )
445 ->method( 'selectRow' )
446 ->with(
447 [ 'revision', 'page' ],
448 $this->anything(),
449 [ 'rev_id' => 2 ]
450 )
451 ->willReturn( (object)[
452 'page_namespace' => '1',
453 'page_title' => 'Food2',
454 ] );
455
456 $store = $this->getRevisionStore( $mockLoadBalancer );
457 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
458
459 $this->assertSame( 1, $title->getNamespace() );
460 $this->assertSame( 'Food2', $title->getDBkey() );
461 }
462
463 public function testGetTitle_successFromRevIdOnFallback() {
464 $mockLoadBalancer = $this->getMockLoadBalancer();
465 // Title calls wfGetDB() so we have to set the main service
466 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
467
468 $db = $this->getMockDatabase();
469 // Title calls wfGetDB() which uses a regular Connection
470 // Assert that the first call uses a REPLICA and the second falls back to master
471 $mockLoadBalancer->expects( $this->exactly( 2 ) )
472 ->method( 'getConnection' )
473 ->willReturn( $db );
474 // RevisionStore getTitle uses a ConnectionRef
475 $mockLoadBalancer->expects( $this->atLeastOnce() )
476 ->method( 'getConnectionRef' )
477 ->willReturn( $db );
478
479 // First call to Title::newFromID, faking no result (db lag?)
480 $db->expects( $this->at( 0 ) )
481 ->method( 'selectRow' )
482 ->with(
483 'page',
484 $this->anything(),
485 [ 'page_id' => 1 ]
486 )
487 ->willReturn( false );
488
489 // First select using rev_id, faking no result (db lag?)
490 $db->expects( $this->at( 1 ) )
491 ->method( 'selectRow' )
492 ->with(
493 [ 'revision', 'page' ],
494 $this->anything(),
495 [ 'rev_id' => 2 ]
496 )
497 ->willReturn( false );
498
499 // Second call to Title::newFromID, no result
500 $db->expects( $this->at( 2 ) )
501 ->method( 'selectRow' )
502 ->with(
503 'page',
504 $this->anything(),
505 [ 'page_id' => 1 ]
506 )
507 ->willReturn( false );
508
509 // Second select using rev_id, result
510 $db->expects( $this->at( 3 ) )
511 ->method( 'selectRow' )
512 ->with(
513 [ 'revision', 'page' ],
514 $this->anything(),
515 [ 'rev_id' => 2 ]
516 )
517 ->willReturn( (object)[
518 'page_namespace' => '2',
519 'page_title' => 'Foodey',
520 ] );
521
522 $store = $this->getRevisionStore( $mockLoadBalancer );
523 $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
524
525 $this->assertSame( 2, $title->getNamespace() );
526 $this->assertSame( 'Foodey', $title->getDBkey() );
527 }
528
529 /**
530 * @covers \MediaWiki\Storage\RevisionStore::getTitle
531 */
532 public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() {
533 $mockLoadBalancer = $this->getMockLoadBalancer();
534 // Title calls wfGetDB() so we have to set the main service
535 $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
536
537 $db = $this->getMockDatabase();
538 // Title calls wfGetDB() which uses a regular Connection
539 // Assert that the first call uses a REPLICA and the second falls back to master
540
541 // RevisionStore getTitle uses getConnectionRef
542 // Title::newFromID uses getConnection
543 foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
544 $mockLoadBalancer->expects( $this->exactly( 2 ) )
545 ->method( $method )
546 ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
547 static $callCounter = 0;
548 $callCounter++;
549 // The first call should be to a REPLICA, and the second a MASTER.
550 if ( $callCounter === 1 ) {
551 $this->assertSame( DB_REPLICA, $masterOrReplica );
552 } elseif ( $callCounter === 2 ) {
553 $this->assertSame( DB_MASTER, $masterOrReplica );
554 }
555 return $db;
556 } );
557 }
558 // First and third call to Title::newFromID, faking no result
559 foreach ( [ 0, 2 ] as $counter ) {
560 $db->expects( $this->at( $counter ) )
561 ->method( 'selectRow' )
562 ->with(
563 'page',
564 $this->anything(),
565 [ 'page_id' => 1 ]
566 )
567 ->willReturn( false );
568 }
569
570 foreach ( [ 1, 3 ] as $counter ) {
571 $db->expects( $this->at( $counter ) )
572 ->method( 'selectRow' )
573 ->with(
574 [ 'revision', 'page' ],
575 $this->anything(),
576 [ 'rev_id' => 2 ]
577 )
578 ->willReturn( false );
579 }
580
581 $store = $this->getRevisionStore( $mockLoadBalancer );
582
583 $this->setExpectedException( RevisionAccessException::class );
584 $store->getTitle( 1, 2, RevisionStore::READ_NORMAL );
585 }
586
587 public function provideNewRevisionFromRow_legacyEncoding_applied() {
588 yield 'windows-1252, old_flags is empty' => [
589 'windows-1252',
590 'en',
591 [
592 'old_flags' => '',
593 'old_text' => "S\xF6me Content",
594 ],
595 'Söme Content'
596 ];
597
598 yield 'windows-1252, old_flags is null' => [
599 'windows-1252',
600 'en',
601 [
602 'old_flags' => null,
603 'old_text' => "S\xF6me Content",
604 ],
605 'Söme Content'
606 ];
607 }
608
609 /**
610 * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied
611 *
612 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
613 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
614 */
615 public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) {
616 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
617
618 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
619 $blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) );
620
621 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
622
623 $record = $store->newRevisionFromRow(
624 $this->makeRow( $row ),
625 0,
626 Title::newFromText( __METHOD__ . '-UTPage' )
627 );
628
629 $this->assertSame( $text, $record->getContent( 'main' )->serialize() );
630 }
631
632 /**
633 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow
634 * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29
635 */
636 public function testNewRevisionFromRow_legacyEncoding_ignored() {
637 $row = [
638 'old_flags' => 'utf-8',
639 'old_text' => 'Söme Content',
640 ];
641
642 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
643
644 $blobStore = new SqlBlobStore( wfGetLB(), $cache );
645 $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
646
647 $store = $this->getRevisionStore( wfGetLB(), $blobStore, $cache );
648
649 $record = $store->newRevisionFromRow(
650 $this->makeRow( $row ),
651 0,
652 Title::newFromText( __METHOD__ . '-UTPage' )
653 );
654 $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() );
655 }
656
657 private function makeRow( array $array ) {
658 $row = $array + [
659 'rev_id' => 7,
660 'rev_page' => 5,
661 'rev_text_id' => 11,
662 'rev_timestamp' => '20110101000000',
663 'rev_user_text' => 'Tester',
664 'rev_user' => 17,
665 'rev_minor_edit' => 0,
666 'rev_deleted' => 0,
667 'rev_len' => 100,
668 'rev_parent_id' => 0,
669 'rev_sha1' => 'deadbeef',
670 'rev_comment_text' => 'Testing',
671 'rev_comment_data' => '{}',
672 'rev_comment_cid' => 111,
673 'rev_content_format' => CONTENT_FORMAT_TEXT,
674 'rev_content_model' => CONTENT_MODEL_TEXT,
675 'page_namespace' => 0,
676 'page_title' => 'TEST',
677 'page_id' => 5,
678 'page_latest' => 7,
679 'page_is_redirect' => 0,
680 'page_len' => 100,
681 'user_name' => 'Tester',
682 'old_is' => 13,
683 'old_text' => 'Hello World',
684 'old_flags' => 'utf-8',
685 ];
686
687 return (object)$row;
688 }
689
690 }