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