Use consistent caching strategy in Revision storage classes
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Storage\BlobStoreFactory;
5 use MediaWiki\Storage\MutableRevisionRecord;
6 use MediaWiki\Storage\RevisionAccessException;
7 use MediaWiki\Storage\RevisionRecord;
8 use MediaWiki\Storage\RevisionStore;
9 use MediaWiki\Storage\SlotRecord;
10 use MediaWiki\Storage\SqlBlobStore;
11 use Wikimedia\Rdbms\IDatabase;
12 use Wikimedia\Rdbms\LoadBalancer;
13
14 /**
15 * Test cases in RevisionTest should not interact with the Database.
16 * For test cases that need Database interaction see RevisionDbTestBase.
17 */
18 class RevisionTest extends MediaWikiTestCase {
19
20 public function setUp() {
21 parent::setUp();
22 $this->setMwGlobals( 'wgMultiContentRevisionSchemaMigrationStage', MIGRATION_OLD );
23 }
24
25 public function provideConstructFromArray() {
26 yield 'with text' => [
27 [
28 'text' => 'hello world.',
29 'content_model' => CONTENT_MODEL_JAVASCRIPT
30 ],
31 ];
32 yield 'with content' => [
33 [
34 'content' => new JavaScriptContent( 'hellow world.' )
35 ],
36 ];
37 // FIXME: test with and without user ID, and with a user object.
38 // We can't prepare that here though, since we don't yet have a dummy DB
39 }
40
41 /**
42 * @param string $model
43 * @return Title
44 */
45 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT ) {
46 $mock = $this->getMockBuilder( Title::class )
47 ->disableOriginalConstructor()
48 ->getMock();
49 $mock->expects( $this->any() )
50 ->method( 'getNamespace' )
51 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
52 $mock->expects( $this->any() )
53 ->method( 'getPrefixedText' )
54 ->will( $this->returnValue( 'RevisionTest' ) );
55 $mock->expects( $this->any() )
56 ->method( 'getDBkey' )
57 ->will( $this->returnValue( 'RevisionTest' ) );
58 $mock->expects( $this->any() )
59 ->method( 'getArticleID' )
60 ->will( $this->returnValue( 23 ) );
61 $mock->expects( $this->any() )
62 ->method( 'getContentModel' )
63 ->will( $this->returnValue( $model ) );
64
65 return $mock;
66 }
67
68 /**
69 * @dataProvider provideConstructFromArray
70 * @covers Revision::__construct
71 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
72 */
73 public function testConstructFromArray( $rowArray ) {
74 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
75 $this->assertNotNull( $rev->getContent(), 'no content object available' );
76 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
77 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
78 }
79
80 /**
81 * @covers Revision::__construct
82 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
83 */
84 public function testConstructFromEmptyArray() {
85 $rev = new Revision( [], 0, $this->getMockTitle() );
86 $this->assertNull( $rev->getContent(), 'no content object should be available' );
87 }
88
89 /**
90 * @covers Revision::__construct
91 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
92 */
93 public function testConstructFromArrayWithBadPageId() {
94 Wikimedia\suppressWarnings();
95 $rev = new Revision( [ 'page' => 77777777 ] );
96 $this->assertSame( 77777777, $rev->getPage() );
97 Wikimedia\restoreWarnings();
98 }
99
100 public function provideConstructFromArray_userSetAsExpected() {
101 yield 'no user defaults to wgUser' => [
102 [
103 'content' => new JavaScriptContent( 'hello world.' ),
104 ],
105 null,
106 null,
107 ];
108 yield 'user text and id' => [
109 [
110 'content' => new JavaScriptContent( 'hello world.' ),
111 'user_text' => 'SomeTextUserName',
112 'user' => 99,
113
114 ],
115 99,
116 'SomeTextUserName',
117 ];
118 yield 'user text only' => [
119 [
120 'content' => new JavaScriptContent( 'hello world.' ),
121 'user_text' => '111.111.111.111',
122 ],
123 0,
124 '111.111.111.111',
125 ];
126 }
127
128 /**
129 * @dataProvider provideConstructFromArray_userSetAsExpected
130 * @covers Revision::__construct
131 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
132 *
133 * @param array $rowArray
134 * @param mixed $expectedUserId null to expect the current wgUser ID
135 * @param mixed $expectedUserName null to expect the current wgUser name
136 */
137 public function testConstructFromArray_userSetAsExpected(
138 array $rowArray,
139 $expectedUserId,
140 $expectedUserName
141 ) {
142 $testUser = $this->getTestUser()->getUser();
143 $this->setMwGlobals( 'wgUser', $testUser );
144 if ( $expectedUserId === null ) {
145 $expectedUserId = $testUser->getId();
146 }
147 if ( $expectedUserName === null ) {
148 $expectedUserName = $testUser->getName();
149 }
150
151 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
152 $this->assertEquals( $expectedUserId, $rev->getUser() );
153 $this->assertEquals( $expectedUserName, $rev->getUserText() );
154 }
155
156 public function provideConstructFromArrayThrowsExceptions() {
157 yield 'content and text_id both not empty' => [
158 [
159 'content' => new WikitextContent( 'GOAT' ),
160 'text_id' => 'someid',
161 ],
162 new MWException( 'Text already stored in external store (id someid),' )
163 ];
164 yield 'with bad content object (class)' => [
165 [ 'content' => new stdClass() ],
166 new MWException( 'content field must contain a Content object' )
167 ];
168 yield 'with bad content object (string)' => [
169 [ 'content' => 'ImAGoat' ],
170 new MWException( 'content field must contain a Content object' )
171 ];
172 yield 'bad row format' => [
173 'imastring, not a row',
174 new InvalidArgumentException(
175 '$row must be a row object, an associative array, or a RevisionRecord'
176 )
177 ];
178 }
179
180 /**
181 * @dataProvider provideConstructFromArrayThrowsExceptions
182 * @covers Revision::__construct
183 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
184 */
185 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
186 $this->setExpectedException(
187 get_class( $expectedException ),
188 $expectedException->getMessage(),
189 $expectedException->getCode()
190 );
191 new Revision( $rowArray, 0, $this->getMockTitle() );
192 }
193
194 /**
195 * @covers Revision::__construct
196 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
197 */
198 public function testConstructFromNothing() {
199 $this->setExpectedException(
200 InvalidArgumentException::class
201 );
202 new Revision( [] );
203 }
204
205 public function provideConstructFromRow() {
206 yield 'Full construction' => [
207 [
208 'rev_id' => '42',
209 'rev_page' => '23',
210 'rev_text_id' => '2',
211 'rev_timestamp' => '20171017114835',
212 'rev_user_text' => '127.0.0.1',
213 'rev_user' => '0',
214 'rev_minor_edit' => '0',
215 'rev_deleted' => '0',
216 'rev_len' => '46',
217 'rev_parent_id' => '1',
218 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
219 'rev_comment_text' => 'Goat Comment!',
220 'rev_comment_data' => null,
221 'rev_comment_cid' => null,
222 'rev_content_format' => 'GOATFORMAT',
223 'rev_content_model' => 'GOATMODEL',
224 ],
225 function ( RevisionTest $testCase, Revision $rev ) {
226 $testCase->assertSame( 42, $rev->getId() );
227 $testCase->assertSame( 23, $rev->getPage() );
228 $testCase->assertSame( 2, $rev->getTextId() );
229 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
230 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
231 $testCase->assertSame( 0, $rev->getUser() );
232 $testCase->assertSame( false, $rev->isMinor() );
233 $testCase->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
234 $testCase->assertSame( 46, $rev->getSize() );
235 $testCase->assertSame( 1, $rev->getParentId() );
236 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
237 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
238 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
239 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
240 }
241 ];
242 yield 'default field values' => [
243 [
244 'rev_id' => '42',
245 'rev_page' => '23',
246 'rev_text_id' => '2',
247 'rev_timestamp' => '20171017114835',
248 'rev_user_text' => '127.0.0.1',
249 'rev_user' => '0',
250 'rev_minor_edit' => '0',
251 'rev_deleted' => '0',
252 'rev_comment_text' => 'Goat Comment!',
253 'rev_comment_data' => null,
254 'rev_comment_cid' => null,
255 ],
256 function ( RevisionTest $testCase, Revision $rev ) {
257 // parent ID may be null
258 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
259
260 // given fields
261 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
262 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
263 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
264 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
265 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
266 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
267
268 // computed fields
269 $testCase->assertNotNull( $rev->getSize(), 'size' );
270 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
271
272 // NOTE: model and format will be detected based on the namespace of the (mock) title
273 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
274 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
275 }
276 ];
277 }
278
279 /**
280 * @dataProvider provideConstructFromRow
281 * @covers Revision::__construct
282 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
283 */
284 public function testConstructFromRow( array $arrayData, $assertions ) {
285 $data = 'Hello goat.'; // needs to match model and format
286
287 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
288 ->disableOriginalConstructor()
289 ->getMock();
290
291 $blobStore->method( 'getBlob' )
292 ->will( $this->returnValue( $data ) );
293
294 $blobStore->method( 'getTextIdFromAddress' )
295 ->will( $this->returnCallback(
296 function ( $address ) {
297 // Turn "tt:1234" into 12345.
298 // Note that this must be functional so we can test getTextId().
299 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
300 $parts = explode( ':', $address );
301 return (int)array_pop( $parts );
302 }
303 ) );
304
305 // Note override internal service, so RevisionStore uses it as well.
306 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
307
308 $row = (object)$arrayData;
309 $rev = new Revision( $row, 0, $this->getMockTitle() );
310 $assertions( $this, $rev );
311 }
312
313 /**
314 * @covers Revision::__construct
315 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
316 */
317 public function testConstructFromRowWithBadPageId() {
318 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
319 $this->overrideMwServices();
320 Wikimedia\suppressWarnings();
321 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
322 $this->assertSame( 77777777, $rev->getPage() );
323 Wikimedia\restoreWarnings();
324 }
325
326 public function provideGetRevisionText() {
327 yield 'Generic test' => [
328 'This is a goat of revision text.',
329 [
330 'old_flags' => '',
331 'old_text' => 'This is a goat of revision text.',
332 ],
333 ];
334 }
335
336 public function provideGetId() {
337 yield [
338 [],
339 null
340 ];
341 yield [
342 [ 'id' => 998 ],
343 998
344 ];
345 }
346
347 /**
348 * @dataProvider provideGetId
349 * @covers Revision::getId
350 */
351 public function testGetId( $rowArray, $expectedId ) {
352 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
353 $this->assertEquals( $expectedId, $rev->getId() );
354 }
355
356 public function provideSetId() {
357 yield [ '123', 123 ];
358 yield [ 456, 456 ];
359 }
360
361 /**
362 * @dataProvider provideSetId
363 * @covers Revision::setId
364 */
365 public function testSetId( $input, $expected ) {
366 $rev = new Revision( [], 0, $this->getMockTitle() );
367 $rev->setId( $input );
368 $this->assertSame( $expected, $rev->getId() );
369 }
370
371 public function provideSetUserIdAndName() {
372 yield [ '123', 123, 'GOaT' ];
373 yield [ 456, 456, 'GOaT' ];
374 }
375
376 /**
377 * @dataProvider provideSetUserIdAndName
378 * @covers Revision::setUserIdAndName
379 */
380 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
381 $rev = new Revision( [], 0, $this->getMockTitle() );
382 $rev->setUserIdAndName( $inputId, $name );
383 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
384 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
385 }
386
387 public function provideGetTextId() {
388 yield [ [], null ];
389 yield [ [ 'text_id' => '123' ], 123 ];
390 yield [ [ 'text_id' => 456 ], 456 ];
391 }
392
393 /**
394 * @dataProvider provideGetTextId
395 * @covers Revision::getTextId()
396 */
397 public function testGetTextId( $rowArray, $expected ) {
398 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
399 $this->assertSame( $expected, $rev->getTextId() );
400 }
401
402 public function provideGetParentId() {
403 yield [ [], null ];
404 yield [ [ 'parent_id' => '123' ], 123 ];
405 yield [ [ 'parent_id' => 456 ], 456 ];
406 }
407
408 /**
409 * @dataProvider provideGetParentId
410 * @covers Revision::getParentId()
411 */
412 public function testGetParentId( $rowArray, $expected ) {
413 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
414 $this->assertSame( $expected, $rev->getParentId() );
415 }
416
417 /**
418 * @covers Revision::getRevisionText
419 * @dataProvider provideGetRevisionText
420 */
421 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
422 $this->assertEquals(
423 $expected,
424 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
425 }
426
427 public function provideGetRevisionTextWithZlibExtension() {
428 yield 'Generic gzip test' => [
429 'This is a small goat of revision text.',
430 [
431 'old_flags' => 'gzip',
432 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
433 ],
434 ];
435 }
436
437 /**
438 * @covers Revision::getRevisionText
439 * @dataProvider provideGetRevisionTextWithZlibExtension
440 */
441 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
442 $this->checkPHPExtension( 'zlib' );
443 $this->testGetRevisionText( $expected, $rowData );
444 }
445
446 public function provideGetRevisionTextWithZlibExtension_badData() {
447 yield 'Generic gzip test' => [
448 'This is a small goat of revision text.',
449 [
450 'old_flags' => 'gzip',
451 'old_text' => 'DEAD BEEF',
452 ],
453 ];
454 }
455
456 /**
457 * @covers Revision::getRevisionText
458 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
459 */
460 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
461 $this->checkPHPExtension( 'zlib' );
462 Wikimedia\suppressWarnings();
463 $this->assertFalse(
464 Revision::getRevisionText(
465 (object)$rowData
466 )
467 );
468 Wikimedia\suppressWarnings( true );
469 }
470
471 private function getWANObjectCache() {
472 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
473 }
474
475 /**
476 * @return SqlBlobStore
477 */
478 private function getBlobStore() {
479 /** @var LoadBalancer $lb */
480 $lb = $this->getMockBuilder( LoadBalancer::class )
481 ->disableOriginalConstructor()
482 ->getMock();
483
484 $cache = $this->getWANObjectCache();
485
486 $blobStore = new SqlBlobStore( $lb, $cache );
487 return $blobStore;
488 }
489
490 private function mockBlobStoreFactory( $blobStore ) {
491 /** @var LoadBalancer $lb */
492 $factory = $this->getMockBuilder( BlobStoreFactory::class )
493 ->disableOriginalConstructor()
494 ->getMock();
495 $factory->expects( $this->any() )
496 ->method( 'newBlobStore' )
497 ->willReturn( $blobStore );
498 $factory->expects( $this->any() )
499 ->method( 'newSqlBlobStore' )
500 ->willReturn( $blobStore );
501 return $factory;
502 }
503
504 /**
505 * @return RevisionStore
506 */
507 private function getRevisionStore() {
508 /** @var LoadBalancer $lb */
509 $lb = $this->getMockBuilder( LoadBalancer::class )
510 ->disableOriginalConstructor()
511 ->getMock();
512
513 $cache = $this->getWANObjectCache();
514
515 $blobStore = new RevisionStore(
516 $lb,
517 $this->getBlobStore(),
518 $cache,
519 MediaWikiServices::getInstance()->getCommentStore(),
520 MediaWikiServices::getInstance()->getContentModelStore(),
521 MediaWikiServices::getInstance()->getSlotRoleStore(),
522 MIGRATION_OLD,
523 MediaWikiServices::getInstance()->getActorMigration()
524 );
525 return $blobStore;
526 }
527
528 public function provideGetRevisionTextWithLegacyEncoding() {
529 yield 'Utf8Native' => [
530 "Wiki est l'\xc3\xa9cole superieur !",
531 'fr',
532 'iso-8859-1',
533 [
534 'old_flags' => 'utf-8',
535 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
536 ]
537 ];
538 yield 'Utf8Legacy' => [
539 "Wiki est l'\xc3\xa9cole superieur !",
540 'fr',
541 'iso-8859-1',
542 [
543 'old_flags' => '',
544 'old_text' => "Wiki est l'\xe9cole superieur !",
545 ]
546 ];
547 }
548
549 /**
550 * @covers Revision::getRevisionText
551 * @dataProvider provideGetRevisionTextWithLegacyEncoding
552 */
553 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
554 $blobStore = $this->getBlobStore();
555 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
556 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
557
558 $this->testGetRevisionText( $expected, $rowData );
559 }
560
561 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
562 /**
563 * WARNING!
564 * Do not set the external flag!
565 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
566 */
567 yield 'Utf8NativeGzip' => [
568 "Wiki est l'\xc3\xa9cole superieur !",
569 'fr',
570 'iso-8859-1',
571 [
572 'old_flags' => 'gzip,utf-8',
573 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
574 ]
575 ];
576 yield 'Utf8LegacyGzip' => [
577 "Wiki est l'\xc3\xa9cole superieur !",
578 'fr',
579 'iso-8859-1',
580 [
581 'old_flags' => 'gzip',
582 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
583 ]
584 ];
585 }
586
587 /**
588 * @covers Revision::getRevisionText
589 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
590 */
591 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
592 $this->checkPHPExtension( 'zlib' );
593
594 $blobStore = $this->getBlobStore();
595 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
596 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
597
598 $this->testGetRevisionText( $expected, $rowData );
599 }
600
601 /**
602 * @covers Revision::compressRevisionText
603 */
604 public function testCompressRevisionTextUtf8() {
605 $row = new stdClass;
606 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
607 $row->old_flags = Revision::compressRevisionText( $row->old_text );
608 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
609 "Flags should contain 'utf-8'" );
610 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
611 "Flags should not contain 'gzip'" );
612 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
613 $row->old_text, "Direct check" );
614 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
615 Revision::getRevisionText( $row ), "getRevisionText" );
616 }
617
618 /**
619 * @covers Revision::compressRevisionText
620 */
621 public function testCompressRevisionTextUtf8Gzip() {
622 $this->checkPHPExtension( 'zlib' );
623
624 $blobStore = $this->getBlobStore();
625 $blobStore->setCompressBlobs( true );
626 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
627
628 $row = new stdClass;
629 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
630 $row->old_flags = Revision::compressRevisionText( $row->old_text );
631 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
632 "Flags should contain 'utf-8'" );
633 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
634 "Flags should contain 'gzip'" );
635 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
636 gzinflate( $row->old_text ), "Direct check" );
637 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
638 Revision::getRevisionText( $row ), "getRevisionText" );
639 }
640
641 /**
642 * @covers Revision::loadFromTitle
643 */
644 public function testLoadFromTitle() {
645 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
646 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
647 $this->overrideMwServices();
648 $title = $this->getMockTitle();
649
650 $conditions = [
651 'rev_id=page_latest',
652 'page_namespace' => $title->getNamespace(),
653 'page_title' => $title->getDBkey()
654 ];
655
656 $row = (object)[
657 'rev_id' => '42',
658 'rev_page' => $title->getArticleID(),
659 'rev_text_id' => '2',
660 'rev_timestamp' => '20171017114835',
661 'rev_user_text' => '127.0.0.1',
662 'rev_user' => '0',
663 'rev_minor_edit' => '0',
664 'rev_deleted' => '0',
665 'rev_len' => '46',
666 'rev_parent_id' => '1',
667 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
668 'rev_comment_text' => 'Goat Comment!',
669 'rev_comment_data' => null,
670 'rev_comment_cid' => null,
671 'rev_content_format' => 'GOATFORMAT',
672 'rev_content_model' => 'GOATMODEL',
673 ];
674
675 $db = $this->getMock( IDatabase::class );
676 $db->expects( $this->any() )
677 ->method( 'getDomainId' )
678 ->will( $this->returnValue( wfWikiID() ) );
679 $db->expects( $this->once() )
680 ->method( 'selectRow' )
681 ->with(
682 $this->equalTo( [ 'revision', 'page', 'user' ] ),
683 // We don't really care about the fields are they come from the selectField methods
684 $this->isType( 'array' ),
685 $this->equalTo( $conditions ),
686 // Method name
687 $this->stringContains( 'fetchRevisionRowFromConds' ),
688 // We don't really care about the options here
689 $this->isType( 'array' ),
690 // We don't really care about the join conds are they come from the joinCond methods
691 $this->isType( 'array' )
692 )
693 ->willReturn( $row );
694
695 $revision = Revision::loadFromTitle( $db, $title );
696
697 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
698 $this->assertEquals( $row->rev_id, $revision->getId() );
699 $this->assertEquals( $row->rev_len, $revision->getSize() );
700 $this->assertEquals( $row->rev_sha1, $revision->getSha1() );
701 $this->assertEquals( $row->rev_parent_id, $revision->getParentId() );
702 $this->assertEquals( $row->rev_timestamp, $revision->getTimestamp() );
703 $this->assertEquals( $row->rev_comment_text, $revision->getComment() );
704 $this->assertEquals( $row->rev_user_text, $revision->getUserText() );
705 }
706
707 public function provideDecompressRevisionText() {
708 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
709 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
710 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
711 yield '(no legacy encoding), string in with gzip flag returns string' => [
712 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
713 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
714 ];
715 yield '(no legacy encoding), string in with object flag returns false' => [
716 // gzip string below generated with serialize( 'JOJO' )
717 false, "s:4:\"JOJO\";", [ 'object' ], false,
718 ];
719 yield '(no legacy encoding), serialized object in with object flag returns string' => [
720 false,
721 // Using a TitleValue object as it has a getText method (which is needed)
722 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
723 [ 'object' ],
724 'HHJJDDFF',
725 ];
726 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
727 false,
728 // Using a TitleValue object as it has a getText method (which is needed)
729 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
730 [ 'object', 'gzip' ],
731 '8219JJJ840',
732 ];
733 yield '(ISO-8859-1 encoding), string in string out' => [
734 'ISO-8859-1',
735 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
736 [],
737 '1®Àþ1',
738 ];
739 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
740 'ISO-8859-1',
741 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
742 [ 'gzip' ],
743 '4®Àþ4',
744 ];
745 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
746 'ISO-8859-1',
747 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
748 [ 'object' ],
749 '3®Àþ3',
750 ];
751 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
752 'ISO-8859-1',
753 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
754 [ 'gzip', 'object' ],
755 '2®Àþ2',
756 ];
757 }
758
759 /**
760 * @dataProvider provideDecompressRevisionText
761 * @covers Revision::decompressRevisionText
762 *
763 * @param bool $legacyEncoding
764 * @param mixed $text
765 * @param array $flags
766 * @param mixed $expected
767 */
768 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
769 $blobStore = $this->getBlobStore();
770 if ( $legacyEncoding ) {
771 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
772 }
773
774 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
775 $this->assertSame(
776 $expected,
777 Revision::decompressRevisionText( $text, $flags )
778 );
779 }
780
781 /**
782 * @covers Revision::getRevisionText
783 */
784 public function testGetRevisionText_returnsFalseWhenNoTextField() {
785 $this->assertFalse( Revision::getRevisionText( new stdClass() ) );
786 }
787
788 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
789 yield 'Just text' => [
790 (object)[ 'old_text' => 'SomeText' ],
791 'old_',
792 'SomeText'
793 ];
794 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
795 yield 'gzip text' => [
796 (object)[
797 'old_text' => "sttttr\002\022\000",
798 'old_flags' => 'gzip'
799 ],
800 'old_',
801 'AAAABBAAA'
802 ];
803 yield 'gzip text and different prefix' => [
804 (object)[
805 'jojo_text' => "sttttr\002\022\000",
806 'jojo_flags' => 'gzip'
807 ],
808 'jojo_',
809 'AAAABBAAA'
810 ];
811 }
812
813 /**
814 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
815 * @covers Revision::getRevisionText
816 */
817 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
818 $row,
819 $prefix,
820 $expected
821 ) {
822 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
823 }
824
825 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
826 yield 'Just some text' => [ 'someNonUrlText' ];
827 yield 'No second URL part' => [ 'someProtocol://' ];
828 }
829
830 /**
831 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
832 * @covers Revision::getRevisionText
833 */
834 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
835 $text
836 ) {
837 Wikimedia\suppressWarnings();
838 $this->assertFalse(
839 Revision::getRevisionText(
840 (object)[
841 'old_text' => $text,
842 'old_flags' => 'external',
843 ]
844 )
845 );
846 Wikimedia\suppressWarnings( true );
847 }
848
849 /**
850 * @covers Revision::getRevisionText
851 */
852 public function testGetRevisionText_external_noOldId() {
853 $this->setService(
854 'ExternalStoreFactory',
855 new ExternalStoreFactory( [ 'ForTesting' ] )
856 );
857 $this->assertSame(
858 'AAAABBAAA',
859 Revision::getRevisionText(
860 (object)[
861 'old_text' => 'ForTesting://cluster1/12345',
862 'old_flags' => 'external,gzip',
863 ]
864 )
865 );
866 }
867
868 /**
869 * @covers Revision::getRevisionText
870 */
871 public function testGetRevisionText_external_oldId() {
872 $cache = $this->getWANObjectCache();
873 $this->setService( 'MainWANObjectCache', $cache );
874
875 $this->setService(
876 'ExternalStoreFactory',
877 new ExternalStoreFactory( [ 'ForTesting' ] )
878 );
879
880 $lb = $this->getMockBuilder( LoadBalancer::class )
881 ->disableOriginalConstructor()
882 ->getMock();
883
884 $blobStore = new SqlBlobStore( $lb, $cache );
885 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
886
887 $this->assertSame(
888 'AAAABBAAA',
889 Revision::getRevisionText(
890 (object)[
891 'old_text' => 'ForTesting://cluster1/12345',
892 'old_flags' => 'external,gzip',
893 'old_id' => '7777',
894 ]
895 )
896 );
897
898 $cacheKey = $cache->makeGlobalKey(
899 'BlobStore',
900 'address',
901 $lb->getLocalDomainID(),
902 'tt:7777'
903 );
904 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
905 }
906
907 /**
908 * @covers Revision::userJoinCond
909 */
910 public function testUserJoinCond() {
911 $this->hideDeprecated( 'Revision::userJoinCond' );
912 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
913 $this->overrideMwServices();
914 $this->assertEquals(
915 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
916 Revision::userJoinCond()
917 );
918 }
919
920 /**
921 * @covers Revision::pageJoinCond
922 */
923 public function testPageJoinCond() {
924 $this->hideDeprecated( 'Revision::pageJoinCond' );
925 $this->assertEquals(
926 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
927 Revision::pageJoinCond()
928 );
929 }
930
931 private function overrideCommentStoreAndActorMigration() {
932 $mockStore = $this->getMockBuilder( CommentStore::class )
933 ->disableOriginalConstructor()
934 ->getMock();
935 $mockStore->expects( $this->any() )
936 ->method( 'getFields' )
937 ->willReturn( [ 'commentstore' => 'fields' ] );
938 $mockStore->expects( $this->any() )
939 ->method( 'getJoin' )
940 ->willReturn( [
941 'tables' => [ 'commentstore' => 'table' ],
942 'fields' => [ 'commentstore' => 'field' ],
943 'joins' => [ 'commentstore' => 'join' ],
944 ] );
945 $this->setService( 'CommentStore', $mockStore );
946
947 $mockStore = $this->getMockBuilder( ActorMigration::class )
948 ->disableOriginalConstructor()
949 ->getMock();
950 $mockStore->expects( $this->any() )
951 ->method( 'getJoin' )
952 ->willReturnCallback( function ( $key ) {
953 $p = strtok( $key, '_' );
954 return [
955 'tables' => [ 'actormigration' => 'table' ],
956 'fields' => [
957 $p . '_user' => 'actormigration_user',
958 $p . '_user_text' => 'actormigration_user_text',
959 $p . '_actor' => 'actormigration_actor',
960 ],
961 'joins' => [ 'actormigration' => 'join' ],
962 ];
963 } );
964 $this->setService( 'ActorMigration', $mockStore );
965 }
966
967 public function provideSelectFields() {
968 yield [
969 true,
970 [
971 'rev_id',
972 'rev_page',
973 'rev_text_id',
974 'rev_timestamp',
975 'rev_user_text',
976 'rev_user',
977 'rev_actor' => 'NULL',
978 'rev_minor_edit',
979 'rev_deleted',
980 'rev_len',
981 'rev_parent_id',
982 'rev_sha1',
983 'commentstore' => 'fields',
984 'rev_content_format',
985 'rev_content_model',
986 ]
987 ];
988 yield [
989 false,
990 [
991 'rev_id',
992 'rev_page',
993 'rev_text_id',
994 'rev_timestamp',
995 'rev_user_text',
996 'rev_user',
997 'rev_actor' => 'NULL',
998 'rev_minor_edit',
999 'rev_deleted',
1000 'rev_len',
1001 'rev_parent_id',
1002 'rev_sha1',
1003 'commentstore' => 'fields',
1004 ]
1005 ];
1006 }
1007
1008 /**
1009 * @dataProvider provideSelectFields
1010 * @covers Revision::selectFields
1011 */
1012 public function testSelectFields( $contentHandlerUseDB, $expected ) {
1013 $this->hideDeprecated( 'Revision::selectFields' );
1014 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1015 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
1016 $this->overrideCommentStoreAndActorMigration();
1017 $this->assertEquals( $expected, Revision::selectFields() );
1018 }
1019
1020 public function provideSelectArchiveFields() {
1021 yield [
1022 true,
1023 [
1024 'ar_id',
1025 'ar_page_id',
1026 'ar_rev_id',
1027 'ar_text_id',
1028 'ar_timestamp',
1029 'ar_user_text',
1030 'ar_user',
1031 'ar_actor' => 'NULL',
1032 'ar_minor_edit',
1033 'ar_deleted',
1034 'ar_len',
1035 'ar_parent_id',
1036 'ar_sha1',
1037 'commentstore' => 'fields',
1038 'ar_content_format',
1039 'ar_content_model',
1040 ]
1041 ];
1042 yield [
1043 false,
1044 [
1045 'ar_id',
1046 'ar_page_id',
1047 'ar_rev_id',
1048 'ar_text_id',
1049 'ar_timestamp',
1050 'ar_user_text',
1051 'ar_user',
1052 'ar_actor' => 'NULL',
1053 'ar_minor_edit',
1054 'ar_deleted',
1055 'ar_len',
1056 'ar_parent_id',
1057 'ar_sha1',
1058 'commentstore' => 'fields',
1059 ]
1060 ];
1061 }
1062
1063 /**
1064 * @dataProvider provideSelectArchiveFields
1065 * @covers Revision::selectArchiveFields
1066 */
1067 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1068 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1069 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1070 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
1071 $this->overrideCommentStoreAndActorMigration();
1072 $this->assertEquals( $expected, Revision::selectArchiveFields() );
1073 }
1074
1075 /**
1076 * @covers Revision::selectTextFields
1077 */
1078 public function testSelectTextFields() {
1079 $this->hideDeprecated( 'Revision::selectTextFields' );
1080 $this->assertEquals(
1081 [
1082 'old_text',
1083 'old_flags',
1084 ],
1085 Revision::selectTextFields()
1086 );
1087 }
1088
1089 /**
1090 * @covers Revision::selectPageFields
1091 */
1092 public function testSelectPageFields() {
1093 $this->hideDeprecated( 'Revision::selectPageFields' );
1094 $this->assertEquals(
1095 [
1096 'page_namespace',
1097 'page_title',
1098 'page_id',
1099 'page_latest',
1100 'page_is_redirect',
1101 'page_len',
1102 ],
1103 Revision::selectPageFields()
1104 );
1105 }
1106
1107 /**
1108 * @covers Revision::selectUserFields
1109 */
1110 public function testSelectUserFields() {
1111 $this->hideDeprecated( 'Revision::selectUserFields' );
1112 $this->assertEquals(
1113 [
1114 'user_name',
1115 ],
1116 Revision::selectUserFields()
1117 );
1118 }
1119
1120 public function provideGetArchiveQueryInfo() {
1121 yield 'wgContentHandlerUseDB false' => [
1122 [
1123 'wgContentHandlerUseDB' => false,
1124 ],
1125 [
1126 'tables' => [
1127 'archive',
1128 'commentstore' => 'table',
1129 'actormigration' => 'table',
1130 ],
1131 'fields' => [
1132 'ar_id',
1133 'ar_page_id',
1134 'ar_namespace',
1135 'ar_title',
1136 'ar_rev_id',
1137 'ar_text_id',
1138 'ar_timestamp',
1139 'ar_minor_edit',
1140 'ar_deleted',
1141 'ar_len',
1142 'ar_parent_id',
1143 'ar_sha1',
1144 'commentstore' => 'field',
1145 'ar_user' => 'actormigration_user',
1146 'ar_user_text' => 'actormigration_user_text',
1147 'ar_actor' => 'actormigration_actor',
1148 ],
1149 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1150 ]
1151 ];
1152 yield 'wgContentHandlerUseDB true' => [
1153 [
1154 'wgContentHandlerUseDB' => true,
1155 ],
1156 [
1157 'tables' => [
1158 'archive',
1159 'commentstore' => 'table',
1160 'actormigration' => 'table',
1161 ],
1162 'fields' => [
1163 'ar_id',
1164 'ar_page_id',
1165 'ar_namespace',
1166 'ar_title',
1167 'ar_rev_id',
1168 'ar_text_id',
1169 'ar_timestamp',
1170 'ar_minor_edit',
1171 'ar_deleted',
1172 'ar_len',
1173 'ar_parent_id',
1174 'ar_sha1',
1175 'commentstore' => 'field',
1176 'ar_user' => 'actormigration_user',
1177 'ar_user_text' => 'actormigration_user_text',
1178 'ar_actor' => 'actormigration_actor',
1179 'ar_content_format',
1180 'ar_content_model',
1181 ],
1182 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1183 ]
1184 ];
1185 }
1186
1187 /**
1188 * @covers Revision::getArchiveQueryInfo
1189 * @dataProvider provideGetArchiveQueryInfo
1190 */
1191 public function testGetArchiveQueryInfo( $globals, $expected ) {
1192 $this->setMwGlobals( $globals );
1193 $this->overrideCommentStoreAndActorMigration();
1194
1195 $revisionStore = $this->getRevisionStore();
1196 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1197 $this->setService( 'RevisionStore', $revisionStore );
1198
1199 $queryInfo = Revision::getArchiveQueryInfo();
1200
1201 $this->assertArrayEqualsIgnoringIntKeyOrder(
1202 $expected['tables'],
1203 $queryInfo['tables']
1204 );
1205 $this->assertArrayEqualsIgnoringIntKeyOrder(
1206 $expected['fields'],
1207 $queryInfo['fields']
1208 );
1209 $this->assertArrayEqualsIgnoringIntKeyOrder(
1210 $expected['joins'],
1211 $queryInfo['joins']
1212 );
1213 }
1214
1215 /**
1216 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1217 * keys.
1218 *
1219 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1220 * split into a string key array and an int key array before assertions occur.
1221 *
1222 * @param array $expected
1223 * @param array $actual
1224 */
1225 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1226 $this->objectAssociativeSort( $expected );
1227 $this->objectAssociativeSort( $actual );
1228
1229 // Separate the int key values from the string key values so that assertion failures are
1230 // easier to understand.
1231 $expectedIntKeyValues = [];
1232 $actualIntKeyValues = [];
1233
1234 // Remove all int keys and re add them at the end after sorting by value
1235 // This will result in all int keys being in the same order with same ints at the end of
1236 // the array
1237 foreach ( $expected as $key => $value ) {
1238 if ( is_int( $key ) ) {
1239 unset( $expected[$key] );
1240 $expectedIntKeyValues[] = $value;
1241 }
1242 }
1243 foreach ( $actual as $key => $value ) {
1244 if ( is_int( $key ) ) {
1245 unset( $actual[$key] );
1246 $actualIntKeyValues[] = $value;
1247 }
1248 }
1249
1250 $this->assertArrayEquals( $expected, $actual, false, true );
1251 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );
1252 }
1253
1254 public function provideGetQueryInfo() {
1255 yield 'wgContentHandlerUseDB false, opts none' => [
1256 [
1257 'wgContentHandlerUseDB' => false,
1258 ],
1259 [],
1260 [
1261 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1262 'fields' => [
1263 'rev_id',
1264 'rev_page',
1265 'rev_text_id',
1266 'rev_timestamp',
1267 'rev_minor_edit',
1268 'rev_deleted',
1269 'rev_len',
1270 'rev_parent_id',
1271 'rev_sha1',
1272 'commentstore' => 'field',
1273 'rev_user' => 'actormigration_user',
1274 'rev_user_text' => 'actormigration_user_text',
1275 'rev_actor' => 'actormigration_actor',
1276 ],
1277 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1278 ],
1279 ];
1280 yield 'wgContentHandlerUseDB false, opts page' => [
1281 [
1282 'wgContentHandlerUseDB' => false,
1283 ],
1284 [ 'page' ],
1285 [
1286 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page' ],
1287 'fields' => [
1288 'rev_id',
1289 'rev_page',
1290 'rev_text_id',
1291 'rev_timestamp',
1292 'rev_minor_edit',
1293 'rev_deleted',
1294 'rev_len',
1295 'rev_parent_id',
1296 'rev_sha1',
1297 'commentstore' => 'field',
1298 'rev_user' => 'actormigration_user',
1299 'rev_user_text' => 'actormigration_user_text',
1300 'rev_actor' => 'actormigration_actor',
1301 'page_namespace',
1302 'page_title',
1303 'page_id',
1304 'page_latest',
1305 'page_is_redirect',
1306 'page_len',
1307 ],
1308 'joins' => [
1309 'page' => [
1310 'INNER JOIN',
1311 [ 'page_id = rev_page' ],
1312 ],
1313 'commentstore' => 'join',
1314 'actormigration' => 'join',
1315 ],
1316 ],
1317 ];
1318 yield 'wgContentHandlerUseDB false, opts user' => [
1319 [
1320 'wgContentHandlerUseDB' => false,
1321 ],
1322 [ 'user' ],
1323 [
1324 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'user' ],
1325 'fields' => [
1326 'rev_id',
1327 'rev_page',
1328 'rev_text_id',
1329 'rev_timestamp',
1330 'rev_minor_edit',
1331 'rev_deleted',
1332 'rev_len',
1333 'rev_parent_id',
1334 'rev_sha1',
1335 'commentstore' => 'field',
1336 'rev_user' => 'actormigration_user',
1337 'rev_user_text' => 'actormigration_user_text',
1338 'rev_actor' => 'actormigration_actor',
1339 'user_name',
1340 ],
1341 'joins' => [
1342 'user' => [
1343 'LEFT JOIN',
1344 [
1345 'actormigration_user != 0',
1346 'user_id = actormigration_user',
1347 ],
1348 ],
1349 'commentstore' => 'join',
1350 'actormigration' => 'join',
1351 ],
1352 ],
1353 ];
1354 yield 'wgContentHandlerUseDB false, opts text' => [
1355 [
1356 'wgContentHandlerUseDB' => false,
1357 ],
1358 [ 'text' ],
1359 [
1360 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'text' ],
1361 'fields' => [
1362 'rev_id',
1363 'rev_page',
1364 'rev_text_id',
1365 'rev_timestamp',
1366 'rev_minor_edit',
1367 'rev_deleted',
1368 'rev_len',
1369 'rev_parent_id',
1370 'rev_sha1',
1371 'commentstore' => 'field',
1372 'rev_user' => 'actormigration_user',
1373 'rev_user_text' => 'actormigration_user_text',
1374 'rev_actor' => 'actormigration_actor',
1375 'old_text',
1376 'old_flags',
1377 ],
1378 'joins' => [
1379 'text' => [
1380 'INNER JOIN',
1381 [ 'rev_text_id=old_id' ],
1382 ],
1383 'commentstore' => 'join',
1384 'actormigration' => 'join',
1385 ],
1386 ],
1387 ];
1388 yield 'wgContentHandlerUseDB false, opts 3' => [
1389 [
1390 'wgContentHandlerUseDB' => false,
1391 ],
1392 [ 'text', 'page', 'user' ],
1393 [
1394 'tables' => [
1395 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page', 'user', 'text'
1396 ],
1397 'fields' => [
1398 'rev_id',
1399 'rev_page',
1400 'rev_text_id',
1401 'rev_timestamp',
1402 'rev_minor_edit',
1403 'rev_deleted',
1404 'rev_len',
1405 'rev_parent_id',
1406 'rev_sha1',
1407 'commentstore' => 'field',
1408 'rev_user' => 'actormigration_user',
1409 'rev_user_text' => 'actormigration_user_text',
1410 'rev_actor' => 'actormigration_actor',
1411 'page_namespace',
1412 'page_title',
1413 'page_id',
1414 'page_latest',
1415 'page_is_redirect',
1416 'page_len',
1417 'user_name',
1418 'old_text',
1419 'old_flags',
1420 ],
1421 'joins' => [
1422 'page' => [
1423 'INNER JOIN',
1424 [ 'page_id = rev_page' ],
1425 ],
1426 'user' => [
1427 'LEFT JOIN',
1428 [
1429 'actormigration_user != 0',
1430 'user_id = actormigration_user',
1431 ],
1432 ],
1433 'text' => [
1434 'INNER JOIN',
1435 [ 'rev_text_id=old_id' ],
1436 ],
1437 'commentstore' => 'join',
1438 'actormigration' => 'join',
1439 ],
1440 ],
1441 ];
1442 yield 'wgContentHandlerUseDB true, opts none' => [
1443 [
1444 'wgContentHandlerUseDB' => true,
1445 ],
1446 [],
1447 [
1448 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1449 'fields' => [
1450 'rev_id',
1451 'rev_page',
1452 'rev_text_id',
1453 'rev_timestamp',
1454 'rev_minor_edit',
1455 'rev_deleted',
1456 'rev_len',
1457 'rev_parent_id',
1458 'rev_sha1',
1459 'commentstore' => 'field',
1460 'rev_user' => 'actormigration_user',
1461 'rev_user_text' => 'actormigration_user_text',
1462 'rev_actor' => 'actormigration_actor',
1463 'rev_content_format',
1464 'rev_content_model',
1465 ],
1466 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1467 ],
1468 ];
1469 }
1470
1471 /**
1472 * @covers Revision::getQueryInfo
1473 * @dataProvider provideGetQueryInfo
1474 */
1475 public function testGetQueryInfo( $globals, $options, $expected ) {
1476 $this->setMwGlobals( $globals );
1477 $this->overrideCommentStoreAndActorMigration();
1478
1479 $revisionStore = $this->getRevisionStore();
1480 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1481 $this->setService( 'RevisionStore', $revisionStore );
1482
1483 $queryInfo = Revision::getQueryInfo( $options );
1484
1485 $this->assertArrayEqualsIgnoringIntKeyOrder(
1486 $expected['tables'],
1487 $queryInfo['tables']
1488 );
1489 $this->assertArrayEqualsIgnoringIntKeyOrder(
1490 $expected['fields'],
1491 $queryInfo['fields']
1492 );
1493 $this->assertArrayEqualsIgnoringIntKeyOrder(
1494 $expected['joins'],
1495 $queryInfo['joins']
1496 );
1497 }
1498
1499 /**
1500 * @covers Revision::getSize
1501 */
1502 public function testGetSize() {
1503 $title = $this->getMockTitle();
1504
1505 $rec = new MutableRevisionRecord( $title );
1506 $rev = new Revision( $rec, 0, $title );
1507
1508 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1509
1510 $rec->setSize( 13 );
1511 $this->assertSame( 13, $rev->getSize() );
1512 }
1513
1514 /**
1515 * @covers Revision::getSize
1516 */
1517 public function testGetSize_failure() {
1518 $title = $this->getMockTitle();
1519
1520 $rec = $this->getMockBuilder( RevisionRecord::class )
1521 ->disableOriginalConstructor()
1522 ->getMock();
1523
1524 $rec->method( 'getSize' )
1525 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1526
1527 $rev = new Revision( $rec, 0, $title );
1528 $this->assertNull( $rev->getSize() );
1529 }
1530
1531 /**
1532 * @covers Revision::getSha1
1533 */
1534 public function testGetSha1() {
1535 $title = $this->getMockTitle();
1536
1537 $rec = new MutableRevisionRecord( $title );
1538 $rev = new Revision( $rec, 0, $title );
1539
1540 $emptyHash = SlotRecord::base36Sha1( '' );
1541 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1542
1543 $rec->setSha1( 'deadbeef' );
1544 $this->assertSame( 'deadbeef', $rev->getSha1() );
1545 }
1546
1547 /**
1548 * @covers Revision::getSha1
1549 */
1550 public function testGetSha1_failure() {
1551 $title = $this->getMockTitle();
1552
1553 $rec = $this->getMockBuilder( RevisionRecord::class )
1554 ->disableOriginalConstructor()
1555 ->getMock();
1556
1557 $rec->method( 'getSha1' )
1558 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1559
1560 $rev = new Revision( $rec, 0, $title );
1561 $this->assertNull( $rev->getSha1() );
1562 }
1563
1564 /**
1565 * @covers Revision::getContent
1566 */
1567 public function testGetContent() {
1568 $title = $this->getMockTitle();
1569
1570 $rec = new MutableRevisionRecord( $title );
1571 $rev = new Revision( $rec, 0, $title );
1572
1573 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1574
1575 $content = new TextContent( 'Hello Kittens!' );
1576 $rec->setContent( 'main', $content );
1577 $this->assertSame( $content, $rev->getContent() );
1578 }
1579
1580 /**
1581 * @covers Revision::getContent
1582 */
1583 public function testGetContent_failure() {
1584 $title = $this->getMockTitle();
1585
1586 $rec = $this->getMockBuilder( RevisionRecord::class )
1587 ->disableOriginalConstructor()
1588 ->getMock();
1589
1590 $rec->method( 'getContent' )
1591 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1592
1593 $rev = new Revision( $rec, 0, $title );
1594 $this->assertNull( $rev->getContent() );
1595 }
1596
1597 }