Add `actor` table and code to start using it
[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( 'getModel' )
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 private function getWANObjectCache() {
443 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
444 }
445
446 /**
447 * @return SqlBlobStore
448 */
449 private function getBlobStore() {
450 /** @var LoadBalancer $lb */
451 $lb = $this->getMockBuilder( LoadBalancer::class )
452 ->disableOriginalConstructor()
453 ->getMock();
454
455 $cache = $this->getWANObjectCache();
456
457 $blobStore = new SqlBlobStore( $lb, $cache );
458 return $blobStore;
459 }
460
461 private function mockBlobStoreFactory( $blobStore ) {
462 /** @var LoadBalancer $lb */
463 $factory = $this->getMockBuilder( BlobStoreFactory::class )
464 ->disableOriginalConstructor()
465 ->getMock();
466 $factory->expects( $this->any() )
467 ->method( 'newBlobStore' )
468 ->willReturn( $blobStore );
469 $factory->expects( $this->any() )
470 ->method( 'newSqlBlobStore' )
471 ->willReturn( $blobStore );
472 return $factory;
473 }
474
475 /**
476 * @return RevisionStore
477 */
478 private function getRevisionStore() {
479 /** @var LoadBalancer $lb */
480 $lb = $this->getMockBuilder( LoadBalancer::class )
481 ->disableOriginalConstructor()
482 ->getMock();
483
484 $cache = $this->getWANObjectCache();
485
486 $blobStore = new RevisionStore(
487 $lb,
488 $this->getBlobStore(),
489 $cache,
490 MediaWikiServices::getInstance()->getCommentStore(),
491 MediaWikiServices::getInstance()->getActorMigration()
492 );
493 return $blobStore;
494 }
495
496 public function provideGetRevisionTextWithLegacyEncoding() {
497 yield 'Utf8Native' => [
498 "Wiki est l'\xc3\xa9cole superieur !",
499 'fr',
500 'iso-8859-1',
501 [
502 'old_flags' => 'utf-8',
503 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
504 ]
505 ];
506 yield 'Utf8Legacy' => [
507 "Wiki est l'\xc3\xa9cole superieur !",
508 'fr',
509 'iso-8859-1',
510 [
511 'old_flags' => '',
512 'old_text' => "Wiki est l'\xe9cole superieur !",
513 ]
514 ];
515 }
516
517 /**
518 * @covers Revision::getRevisionText
519 * @dataProvider provideGetRevisionTextWithLegacyEncoding
520 */
521 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
522 $blobStore = $this->getBlobStore();
523 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
524 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
525
526 $this->testGetRevisionText( $expected, $rowData );
527 }
528
529 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
530 /**
531 * WARNING!
532 * Do not set the external flag!
533 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
534 */
535 yield 'Utf8NativeGzip' => [
536 "Wiki est l'\xc3\xa9cole superieur !",
537 'fr',
538 'iso-8859-1',
539 [
540 'old_flags' => 'gzip,utf-8',
541 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
542 ]
543 ];
544 yield 'Utf8LegacyGzip' => [
545 "Wiki est l'\xc3\xa9cole superieur !",
546 'fr',
547 'iso-8859-1',
548 [
549 'old_flags' => 'gzip',
550 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
551 ]
552 ];
553 }
554
555 /**
556 * @covers Revision::getRevisionText
557 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
558 */
559 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
560 $this->checkPHPExtension( 'zlib' );
561
562 $blobStore = $this->getBlobStore();
563 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
564 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
565
566 $this->testGetRevisionText( $expected, $rowData );
567 }
568
569 /**
570 * @covers Revision::compressRevisionText
571 */
572 public function testCompressRevisionTextUtf8() {
573 $row = new stdClass;
574 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
575 $row->old_flags = Revision::compressRevisionText( $row->old_text );
576 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
577 "Flags should contain 'utf-8'" );
578 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
579 "Flags should not contain 'gzip'" );
580 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
581 $row->old_text, "Direct check" );
582 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
583 Revision::getRevisionText( $row ), "getRevisionText" );
584 }
585
586 /**
587 * @covers Revision::compressRevisionText
588 */
589 public function testCompressRevisionTextUtf8Gzip() {
590 $this->checkPHPExtension( 'zlib' );
591
592 $blobStore = $this->getBlobStore();
593 $blobStore->setCompressBlobs( true );
594 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
595
596 $row = new stdClass;
597 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
598 $row->old_flags = Revision::compressRevisionText( $row->old_text );
599 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
600 "Flags should contain 'utf-8'" );
601 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
602 "Flags should contain 'gzip'" );
603 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
604 gzinflate( $row->old_text ), "Direct check" );
605 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
606 Revision::getRevisionText( $row ), "getRevisionText" );
607 }
608
609 /**
610 * @covers Revision::loadFromTitle
611 */
612 public function testLoadFromTitle() {
613 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
614 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
615 $this->overrideMwServices();
616 $title = $this->getMockTitle();
617
618 $conditions = [
619 'rev_id=page_latest',
620 'page_namespace' => $title->getNamespace(),
621 'page_title' => $title->getDBkey()
622 ];
623
624 $row = (object)[
625 'rev_id' => '42',
626 'rev_page' => $title->getArticleID(),
627 'rev_text_id' => '2',
628 'rev_timestamp' => '20171017114835',
629 'rev_user_text' => '127.0.0.1',
630 'rev_user' => '0',
631 'rev_minor_edit' => '0',
632 'rev_deleted' => '0',
633 'rev_len' => '46',
634 'rev_parent_id' => '1',
635 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
636 'rev_comment_text' => 'Goat Comment!',
637 'rev_comment_data' => null,
638 'rev_comment_cid' => null,
639 'rev_content_format' => 'GOATFORMAT',
640 'rev_content_model' => 'GOATMODEL',
641 ];
642
643 $db = $this->getMock( IDatabase::class );
644 $db->expects( $this->any() )
645 ->method( 'getDomainId' )
646 ->will( $this->returnValue( wfWikiID() ) );
647 $db->expects( $this->once() )
648 ->method( 'selectRow' )
649 ->with(
650 $this->equalTo( [ 'revision', 'page', 'user' ] ),
651 // We don't really care about the fields are they come from the selectField methods
652 $this->isType( 'array' ),
653 $this->equalTo( $conditions ),
654 // Method name
655 $this->stringContains( 'fetchRevisionRowFromConds' ),
656 // We don't really care about the options here
657 $this->isType( 'array' ),
658 // We don't really care about the join conds are they come from the joinCond methods
659 $this->isType( 'array' )
660 )
661 ->willReturn( $row );
662
663 $revision = Revision::loadFromTitle( $db, $title );
664
665 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
666 $this->assertEquals( $row->rev_id, $revision->getId() );
667 $this->assertEquals( $row->rev_len, $revision->getSize() );
668 $this->assertEquals( $row->rev_sha1, $revision->getSha1() );
669 $this->assertEquals( $row->rev_parent_id, $revision->getParentId() );
670 $this->assertEquals( $row->rev_timestamp, $revision->getTimestamp() );
671 $this->assertEquals( $row->rev_comment_text, $revision->getComment() );
672 $this->assertEquals( $row->rev_user_text, $revision->getUserText() );
673 }
674
675 public function provideDecompressRevisionText() {
676 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
677 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
678 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
679 yield '(no legacy encoding), string in with gzip flag returns string' => [
680 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
681 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
682 ];
683 yield '(no legacy encoding), string in with object flag returns false' => [
684 // gzip string below generated with serialize( 'JOJO' )
685 false, "s:4:\"JOJO\";", [ 'object' ], false,
686 ];
687 yield '(no legacy encoding), serialized object in with object flag returns string' => [
688 false,
689 // Using a TitleValue object as it has a getText method (which is needed)
690 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
691 [ 'object' ],
692 'HHJJDDFF',
693 ];
694 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
695 false,
696 // Using a TitleValue object as it has a getText method (which is needed)
697 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
698 [ 'object', 'gzip' ],
699 '8219JJJ840',
700 ];
701 yield '(ISO-8859-1 encoding), string in string out' => [
702 'ISO-8859-1',
703 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
704 [],
705 '1®Àþ1',
706 ];
707 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
708 'ISO-8859-1',
709 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
710 [ 'gzip' ],
711 '4®Àþ4',
712 ];
713 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
714 'ISO-8859-1',
715 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
716 [ 'object' ],
717 '3®Àþ3',
718 ];
719 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
720 'ISO-8859-1',
721 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
722 [ 'gzip', 'object' ],
723 '2®Àþ2',
724 ];
725 }
726
727 /**
728 * @dataProvider provideDecompressRevisionText
729 * @covers Revision::decompressRevisionText
730 *
731 * @param bool $legacyEncoding
732 * @param mixed $text
733 * @param array $flags
734 * @param mixed $expected
735 */
736 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
737 $blobStore = $this->getBlobStore();
738 if ( $legacyEncoding ) {
739 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
740 }
741
742 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
743 $this->assertSame(
744 $expected,
745 Revision::decompressRevisionText( $text, $flags )
746 );
747 }
748
749 /**
750 * @covers Revision::getRevisionText
751 */
752 public function testGetRevisionText_returnsFalseWhenNoTextField() {
753 $this->assertFalse( Revision::getRevisionText( new stdClass() ) );
754 }
755
756 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
757 yield 'Just text' => [
758 (object)[ 'old_text' => 'SomeText' ],
759 'old_',
760 'SomeText'
761 ];
762 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
763 yield 'gzip text' => [
764 (object)[
765 'old_text' => "sttttr\002\022\000",
766 'old_flags' => 'gzip'
767 ],
768 'old_',
769 'AAAABBAAA'
770 ];
771 yield 'gzip text and different prefix' => [
772 (object)[
773 'jojo_text' => "sttttr\002\022\000",
774 'jojo_flags' => 'gzip'
775 ],
776 'jojo_',
777 'AAAABBAAA'
778 ];
779 }
780
781 /**
782 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
783 * @covers Revision::getRevisionText
784 */
785 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
786 $row,
787 $prefix,
788 $expected
789 ) {
790 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
791 }
792
793 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
794 yield 'Just some text' => [ 'someNonUrlText' ];
795 yield 'No second URL part' => [ 'someProtocol://' ];
796 }
797
798 /**
799 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
800 * @covers Revision::getRevisionText
801 */
802 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
803 $text
804 ) {
805 $this->assertFalse(
806 Revision::getRevisionText(
807 (object)[
808 'old_text' => $text,
809 'old_flags' => 'external',
810 ]
811 )
812 );
813 }
814
815 /**
816 * @covers Revision::getRevisionText
817 */
818 public function testGetRevisionText_external_noOldId() {
819 $this->setService(
820 'ExternalStoreFactory',
821 new ExternalStoreFactory( [ 'ForTesting' ] )
822 );
823 $this->assertSame(
824 'AAAABBAAA',
825 Revision::getRevisionText(
826 (object)[
827 'old_text' => 'ForTesting://cluster1/12345',
828 'old_flags' => 'external,gzip',
829 ]
830 )
831 );
832 }
833
834 /**
835 * @covers Revision::getRevisionText
836 */
837 public function testGetRevisionText_external_oldId() {
838 $cache = $this->getWANObjectCache();
839 $this->setService( 'MainWANObjectCache', $cache );
840
841 $this->setService(
842 'ExternalStoreFactory',
843 new ExternalStoreFactory( [ 'ForTesting' ] )
844 );
845
846 $lb = $this->getMockBuilder( LoadBalancer::class )
847 ->disableOriginalConstructor()
848 ->getMock();
849
850 $blobStore = new SqlBlobStore( $lb, $cache );
851 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
852
853 $this->assertSame(
854 'AAAABBAAA',
855 Revision::getRevisionText(
856 (object)[
857 'old_text' => 'ForTesting://cluster1/12345',
858 'old_flags' => 'external,gzip',
859 'old_id' => '7777',
860 ]
861 )
862 );
863
864 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
865 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
866 }
867
868 /**
869 * @covers Revision::userJoinCond
870 */
871 public function testUserJoinCond() {
872 $this->hideDeprecated( 'Revision::userJoinCond' );
873 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
874 $this->overrideMwServices();
875 $this->assertEquals(
876 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
877 Revision::userJoinCond()
878 );
879 }
880
881 /**
882 * @covers Revision::pageJoinCond
883 */
884 public function testPageJoinCond() {
885 $this->hideDeprecated( 'Revision::pageJoinCond' );
886 $this->assertEquals(
887 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
888 Revision::pageJoinCond()
889 );
890 }
891
892 private function overrideCommentStoreAndActorMigration() {
893 $mockStore = $this->getMockBuilder( CommentStore::class )
894 ->disableOriginalConstructor()
895 ->getMock();
896 $mockStore->expects( $this->any() )
897 ->method( 'getFields' )
898 ->willReturn( [ 'commentstore' => 'fields' ] );
899 $mockStore->expects( $this->any() )
900 ->method( 'getJoin' )
901 ->willReturn( [
902 'tables' => [ 'commentstore' => 'table' ],
903 'fields' => [ 'commentstore' => 'field' ],
904 'joins' => [ 'commentstore' => 'join' ],
905 ] );
906 $this->setService( 'CommentStore', $mockStore );
907
908 $mockStore = $this->getMockBuilder( ActorMigration::class )
909 ->disableOriginalConstructor()
910 ->getMock();
911 $mockStore->expects( $this->any() )
912 ->method( 'getJoin' )
913 ->willReturnCallback( function ( $key ) {
914 $p = strtok( $key, '_' );
915 return [
916 'tables' => [ 'actormigration' => 'table' ],
917 'fields' => [
918 $p . '_user' => 'actormigration_user',
919 $p . '_user_text' => 'actormigration_user_text',
920 $p . '_actor' => 'actormigration_actor',
921 ],
922 'joins' => [ 'actormigration' => 'join' ],
923 ];
924 } );
925 $this->setService( 'ActorMigration', $mockStore );
926 }
927
928 public function provideSelectFields() {
929 yield [
930 true,
931 [
932 'rev_id',
933 'rev_page',
934 'rev_text_id',
935 'rev_timestamp',
936 'rev_user_text',
937 'rev_user',
938 'rev_actor' => 'NULL',
939 'rev_minor_edit',
940 'rev_deleted',
941 'rev_len',
942 'rev_parent_id',
943 'rev_sha1',
944 'commentstore' => 'fields',
945 'rev_content_format',
946 'rev_content_model',
947 ]
948 ];
949 yield [
950 false,
951 [
952 'rev_id',
953 'rev_page',
954 'rev_text_id',
955 'rev_timestamp',
956 'rev_user_text',
957 'rev_user',
958 'rev_actor' => 'NULL',
959 'rev_minor_edit',
960 'rev_deleted',
961 'rev_len',
962 'rev_parent_id',
963 'rev_sha1',
964 'commentstore' => 'fields',
965 ]
966 ];
967 }
968
969 /**
970 * @dataProvider provideSelectFields
971 * @covers Revision::selectFields
972 */
973 public function testSelectFields( $contentHandlerUseDB, $expected ) {
974 $this->hideDeprecated( 'Revision::selectFields' );
975 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
976 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
977 $this->overrideCommentStoreAndActorMigration();
978 $this->assertEquals( $expected, Revision::selectFields() );
979 }
980
981 public function provideSelectArchiveFields() {
982 yield [
983 true,
984 [
985 'ar_id',
986 'ar_page_id',
987 'ar_rev_id',
988 'ar_text',
989 'ar_text_id',
990 'ar_timestamp',
991 'ar_user_text',
992 'ar_user',
993 'ar_actor' => 'NULL',
994 'ar_minor_edit',
995 'ar_deleted',
996 'ar_len',
997 'ar_parent_id',
998 'ar_sha1',
999 'commentstore' => 'fields',
1000 'ar_content_format',
1001 'ar_content_model',
1002 ]
1003 ];
1004 yield [
1005 false,
1006 [
1007 'ar_id',
1008 'ar_page_id',
1009 'ar_rev_id',
1010 'ar_text',
1011 'ar_text_id',
1012 'ar_timestamp',
1013 'ar_user_text',
1014 'ar_user',
1015 'ar_actor' => 'NULL',
1016 'ar_minor_edit',
1017 'ar_deleted',
1018 'ar_len',
1019 'ar_parent_id',
1020 'ar_sha1',
1021 'commentstore' => 'fields',
1022 ]
1023 ];
1024 }
1025
1026 /**
1027 * @dataProvider provideSelectArchiveFields
1028 * @covers Revision::selectArchiveFields
1029 */
1030 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1031 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1032 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1033 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD );
1034 $this->overrideCommentStoreAndActorMigration();
1035 $this->assertEquals( $expected, Revision::selectArchiveFields() );
1036 }
1037
1038 /**
1039 * @covers Revision::selectTextFields
1040 */
1041 public function testSelectTextFields() {
1042 $this->hideDeprecated( 'Revision::selectTextFields' );
1043 $this->assertEquals(
1044 [
1045 'old_text',
1046 'old_flags',
1047 ],
1048 Revision::selectTextFields()
1049 );
1050 }
1051
1052 /**
1053 * @covers Revision::selectPageFields
1054 */
1055 public function testSelectPageFields() {
1056 $this->hideDeprecated( 'Revision::selectPageFields' );
1057 $this->assertEquals(
1058 [
1059 'page_namespace',
1060 'page_title',
1061 'page_id',
1062 'page_latest',
1063 'page_is_redirect',
1064 'page_len',
1065 ],
1066 Revision::selectPageFields()
1067 );
1068 }
1069
1070 /**
1071 * @covers Revision::selectUserFields
1072 */
1073 public function testSelectUserFields() {
1074 $this->hideDeprecated( 'Revision::selectUserFields' );
1075 $this->assertEquals(
1076 [
1077 'user_name',
1078 ],
1079 Revision::selectUserFields()
1080 );
1081 }
1082
1083 public function provideGetArchiveQueryInfo() {
1084 yield 'wgContentHandlerUseDB false' => [
1085 [
1086 'wgContentHandlerUseDB' => false,
1087 ],
1088 [
1089 'tables' => [
1090 'archive',
1091 'commentstore' => 'table',
1092 'actormigration' => 'table',
1093 ],
1094 'fields' => [
1095 'ar_id',
1096 'ar_page_id',
1097 'ar_namespace',
1098 'ar_title',
1099 'ar_rev_id',
1100 'ar_text',
1101 'ar_text_id',
1102 'ar_timestamp',
1103 'ar_minor_edit',
1104 'ar_deleted',
1105 'ar_len',
1106 'ar_parent_id',
1107 'ar_sha1',
1108 'commentstore' => 'field',
1109 'ar_user' => 'actormigration_user',
1110 'ar_user_text' => 'actormigration_user_text',
1111 'ar_actor' => 'actormigration_actor',
1112 ],
1113 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1114 ]
1115 ];
1116 yield 'wgContentHandlerUseDB true' => [
1117 [
1118 'wgContentHandlerUseDB' => true,
1119 ],
1120 [
1121 'tables' => [
1122 'archive',
1123 'commentstore' => 'table',
1124 'actormigration' => 'table',
1125 ],
1126 'fields' => [
1127 'ar_id',
1128 'ar_page_id',
1129 'ar_namespace',
1130 'ar_title',
1131 'ar_rev_id',
1132 'ar_text',
1133 'ar_text_id',
1134 'ar_timestamp',
1135 'ar_minor_edit',
1136 'ar_deleted',
1137 'ar_len',
1138 'ar_parent_id',
1139 'ar_sha1',
1140 'commentstore' => 'field',
1141 'ar_user' => 'actormigration_user',
1142 'ar_user_text' => 'actormigration_user_text',
1143 'ar_actor' => 'actormigration_actor',
1144 'ar_content_format',
1145 'ar_content_model',
1146 ],
1147 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1148 ]
1149 ];
1150 }
1151
1152 /**
1153 * @covers Revision::getArchiveQueryInfo
1154 * @dataProvider provideGetArchiveQueryInfo
1155 */
1156 public function testGetArchiveQueryInfo( $globals, $expected ) {
1157 $this->setMwGlobals( $globals );
1158 $this->overrideCommentStoreAndActorMigration();
1159
1160 $revisionStore = $this->getRevisionStore();
1161 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1162 $this->setService( 'RevisionStore', $revisionStore );
1163 $this->assertEquals(
1164 $expected,
1165 Revision::getArchiveQueryInfo()
1166 );
1167 }
1168
1169 public function provideGetQueryInfo() {
1170 yield 'wgContentHandlerUseDB false, opts none' => [
1171 [
1172 'wgContentHandlerUseDB' => false,
1173 ],
1174 [],
1175 [
1176 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1177 'fields' => [
1178 'rev_id',
1179 'rev_page',
1180 'rev_text_id',
1181 'rev_timestamp',
1182 'rev_minor_edit',
1183 'rev_deleted',
1184 'rev_len',
1185 'rev_parent_id',
1186 'rev_sha1',
1187 'commentstore' => 'field',
1188 'rev_user' => 'actormigration_user',
1189 'rev_user_text' => 'actormigration_user_text',
1190 'rev_actor' => 'actormigration_actor',
1191 ],
1192 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1193 ],
1194 ];
1195 yield 'wgContentHandlerUseDB false, opts page' => [
1196 [
1197 'wgContentHandlerUseDB' => false,
1198 ],
1199 [ 'page' ],
1200 [
1201 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page' ],
1202 'fields' => [
1203 'rev_id',
1204 'rev_page',
1205 'rev_text_id',
1206 'rev_timestamp',
1207 'rev_minor_edit',
1208 'rev_deleted',
1209 'rev_len',
1210 'rev_parent_id',
1211 'rev_sha1',
1212 'commentstore' => 'field',
1213 'rev_user' => 'actormigration_user',
1214 'rev_user_text' => 'actormigration_user_text',
1215 'rev_actor' => 'actormigration_actor',
1216 'page_namespace',
1217 'page_title',
1218 'page_id',
1219 'page_latest',
1220 'page_is_redirect',
1221 'page_len',
1222 ],
1223 'joins' => [
1224 'page' => [
1225 'INNER JOIN',
1226 [ 'page_id = rev_page' ],
1227 ],
1228 'commentstore' => 'join',
1229 'actormigration' => 'join',
1230 ],
1231 ],
1232 ];
1233 yield 'wgContentHandlerUseDB false, opts user' => [
1234 [
1235 'wgContentHandlerUseDB' => false,
1236 ],
1237 [ 'user' ],
1238 [
1239 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'user' ],
1240 'fields' => [
1241 'rev_id',
1242 'rev_page',
1243 'rev_text_id',
1244 'rev_timestamp',
1245 'rev_minor_edit',
1246 'rev_deleted',
1247 'rev_len',
1248 'rev_parent_id',
1249 'rev_sha1',
1250 'commentstore' => 'field',
1251 'rev_user' => 'actormigration_user',
1252 'rev_user_text' => 'actormigration_user_text',
1253 'rev_actor' => 'actormigration_actor',
1254 'user_name',
1255 ],
1256 'joins' => [
1257 'user' => [
1258 'LEFT JOIN',
1259 [
1260 'actormigration_user != 0',
1261 'user_id = actormigration_user',
1262 ],
1263 ],
1264 'commentstore' => 'join',
1265 'actormigration' => 'join',
1266 ],
1267 ],
1268 ];
1269 yield 'wgContentHandlerUseDB false, opts text' => [
1270 [
1271 'wgContentHandlerUseDB' => false,
1272 ],
1273 [ 'text' ],
1274 [
1275 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'text' ],
1276 'fields' => [
1277 'rev_id',
1278 'rev_page',
1279 'rev_text_id',
1280 'rev_timestamp',
1281 'rev_minor_edit',
1282 'rev_deleted',
1283 'rev_len',
1284 'rev_parent_id',
1285 'rev_sha1',
1286 'commentstore' => 'field',
1287 'rev_user' => 'actormigration_user',
1288 'rev_user_text' => 'actormigration_user_text',
1289 'rev_actor' => 'actormigration_actor',
1290 'old_text',
1291 'old_flags',
1292 ],
1293 'joins' => [
1294 'text' => [
1295 'INNER JOIN',
1296 [ 'rev_text_id=old_id' ],
1297 ],
1298 'commentstore' => 'join',
1299 'actormigration' => 'join',
1300 ],
1301 ],
1302 ];
1303 yield 'wgContentHandlerUseDB false, opts 3' => [
1304 [
1305 'wgContentHandlerUseDB' => false,
1306 ],
1307 [ 'text', 'page', 'user' ],
1308 [
1309 'tables' => [
1310 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page', 'user', 'text'
1311 ],
1312 'fields' => [
1313 'rev_id',
1314 'rev_page',
1315 'rev_text_id',
1316 'rev_timestamp',
1317 'rev_minor_edit',
1318 'rev_deleted',
1319 'rev_len',
1320 'rev_parent_id',
1321 'rev_sha1',
1322 'commentstore' => 'field',
1323 'rev_user' => 'actormigration_user',
1324 'rev_user_text' => 'actormigration_user_text',
1325 'rev_actor' => 'actormigration_actor',
1326 'page_namespace',
1327 'page_title',
1328 'page_id',
1329 'page_latest',
1330 'page_is_redirect',
1331 'page_len',
1332 'user_name',
1333 'old_text',
1334 'old_flags',
1335 ],
1336 'joins' => [
1337 'page' => [
1338 'INNER JOIN',
1339 [ 'page_id = rev_page' ],
1340 ],
1341 'user' => [
1342 'LEFT JOIN',
1343 [
1344 'actormigration_user != 0',
1345 'user_id = actormigration_user',
1346 ],
1347 ],
1348 'text' => [
1349 'INNER JOIN',
1350 [ 'rev_text_id=old_id' ],
1351 ],
1352 'commentstore' => 'join',
1353 'actormigration' => 'join',
1354 ],
1355 ],
1356 ];
1357 yield 'wgContentHandlerUseDB true, opts none' => [
1358 [
1359 'wgContentHandlerUseDB' => true,
1360 ],
1361 [],
1362 [
1363 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1364 'fields' => [
1365 'rev_id',
1366 'rev_page',
1367 'rev_text_id',
1368 'rev_timestamp',
1369 'rev_minor_edit',
1370 'rev_deleted',
1371 'rev_len',
1372 'rev_parent_id',
1373 'rev_sha1',
1374 'commentstore' => 'field',
1375 'rev_user' => 'actormigration_user',
1376 'rev_user_text' => 'actormigration_user_text',
1377 'rev_actor' => 'actormigration_actor',
1378 'rev_content_format',
1379 'rev_content_model',
1380 ],
1381 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1382 ],
1383 ];
1384 }
1385
1386 /**
1387 * @covers Revision::getQueryInfo
1388 * @dataProvider provideGetQueryInfo
1389 */
1390 public function testGetQueryInfo( $globals, $options, $expected ) {
1391 $this->setMwGlobals( $globals );
1392 $this->overrideCommentStoreAndActorMigration();
1393
1394 $revisionStore = $this->getRevisionStore();
1395 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1396 $this->setService( 'RevisionStore', $revisionStore );
1397
1398 $this->assertEquals(
1399 $expected,
1400 Revision::getQueryInfo( $options )
1401 );
1402 }
1403
1404 /**
1405 * @covers Revision::getSize
1406 */
1407 public function testGetSize() {
1408 $title = $this->getMockTitle();
1409
1410 $rec = new MutableRevisionRecord( $title );
1411 $rev = new Revision( $rec, 0, $title );
1412
1413 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1414
1415 $rec->setSize( 13 );
1416 $this->assertSame( 13, $rev->getSize() );
1417 }
1418
1419 /**
1420 * @covers Revision::getSize
1421 */
1422 public function testGetSize_failure() {
1423 $title = $this->getMockTitle();
1424
1425 $rec = $this->getMockBuilder( RevisionRecord::class )
1426 ->disableOriginalConstructor()
1427 ->getMock();
1428
1429 $rec->method( 'getSize' )
1430 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1431
1432 $rev = new Revision( $rec, 0, $title );
1433 $this->assertNull( $rev->getSize() );
1434 }
1435
1436 /**
1437 * @covers Revision::getSha1
1438 */
1439 public function testGetSha1() {
1440 $title = $this->getMockTitle();
1441
1442 $rec = new MutableRevisionRecord( $title );
1443 $rev = new Revision( $rec, 0, $title );
1444
1445 $emptyHash = SlotRecord::base36Sha1( '' );
1446 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1447
1448 $rec->setSha1( 'deadbeef' );
1449 $this->assertSame( 'deadbeef', $rev->getSha1() );
1450 }
1451
1452 /**
1453 * @covers Revision::getSha1
1454 */
1455 public function testGetSha1_failure() {
1456 $title = $this->getMockTitle();
1457
1458 $rec = $this->getMockBuilder( RevisionRecord::class )
1459 ->disableOriginalConstructor()
1460 ->getMock();
1461
1462 $rec->method( 'getSha1' )
1463 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1464
1465 $rev = new Revision( $rec, 0, $title );
1466 $this->assertNull( $rev->getSha1() );
1467 }
1468
1469 /**
1470 * @covers Revision::getContent
1471 */
1472 public function testGetContent() {
1473 $title = $this->getMockTitle();
1474
1475 $rec = new MutableRevisionRecord( $title );
1476 $rev = new Revision( $rec, 0, $title );
1477
1478 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1479
1480 $content = new TextContent( 'Hello Kittens!' );
1481 $rec->setContent( 'main', $content );
1482 $this->assertSame( $content, $rev->getContent() );
1483 }
1484
1485 /**
1486 * @covers Revision::getContent
1487 */
1488 public function testGetContent_failure() {
1489 $title = $this->getMockTitle();
1490
1491 $rec = $this->getMockBuilder( RevisionRecord::class )
1492 ->disableOriginalConstructor()
1493 ->getMock();
1494
1495 $rec->method( 'getContent' )
1496 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1497
1498 $rev = new Revision( $rec, 0, $title );
1499 $this->assertNull( $rev->getContent() );
1500 }
1501
1502 }