Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Revision\MutableRevisionRecord;
5 use MediaWiki\Revision\RevisionAccessException;
6 use MediaWiki\Revision\RevisionRecord;
7 use MediaWiki\Revision\RevisionStore;
8 use MediaWiki\Revision\SlotRecord;
9 use MediaWiki\Storage\BlobStoreFactory;
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(
23 'wgMultiContentRevisionSchemaMigrationStage',
24 SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
25 );
26 }
27
28 public function provideConstructFromArray() {
29 yield 'with text' => [
30 [
31 'text' => 'hello world.',
32 'content_model' => CONTENT_MODEL_JAVASCRIPT
33 ],
34 ];
35 yield 'with content' => [
36 [
37 'content' => new JavaScriptContent( 'hellow world.' )
38 ],
39 ];
40 // FIXME: test with and without user ID, and with a user object.
41 // We can't prepare that here though, since we don't yet have a dummy DB
42 }
43
44 /**
45 * @param string $model
46 * @return Title
47 */
48 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT ) {
49 $mock = $this->getMockBuilder( Title::class )
50 ->disableOriginalConstructor()
51 ->getMock();
52 $mock->expects( $this->any() )
53 ->method( 'getNamespace' )
54 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
55 $mock->expects( $this->any() )
56 ->method( 'getPrefixedText' )
57 ->will( $this->returnValue( 'RevisionTest' ) );
58 $mock->expects( $this->any() )
59 ->method( 'getDBkey' )
60 ->will( $this->returnValue( 'RevisionTest' ) );
61 $mock->expects( $this->any() )
62 ->method( 'getArticleID' )
63 ->will( $this->returnValue( 23 ) );
64 $mock->expects( $this->any() )
65 ->method( 'getContentModel' )
66 ->will( $this->returnValue( $model ) );
67
68 return $mock;
69 }
70
71 /**
72 * @dataProvider provideConstructFromArray
73 * @covers Revision::__construct
74 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
75 */
76 public function testConstructFromArray( $rowArray ) {
77 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
78 $this->assertNotNull( $rev->getContent(), 'no content object available' );
79 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
80 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
81 }
82
83 /**
84 * @covers Revision::__construct
85 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
86 */
87 public function testConstructFromEmptyArray() {
88 $rev = new Revision( [], 0, $this->getMockTitle() );
89 $this->assertNull( $rev->getContent(), 'no content object should be available' );
90 }
91
92 /**
93 * @covers Revision::__construct
94 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
95 */
96 public function testConstructFromArrayWithBadPageId() {
97 Wikimedia\suppressWarnings();
98 $rev = new Revision( [ 'page' => 77777777 ] );
99 $this->assertSame( 77777777, $rev->getPage() );
100 Wikimedia\restoreWarnings();
101 }
102
103 public function provideConstructFromArray_userSetAsExpected() {
104 yield 'no user defaults to wgUser' => [
105 [
106 'content' => new JavaScriptContent( 'hello world.' ),
107 ],
108 null,
109 null,
110 ];
111 yield 'user text and id' => [
112 [
113 'content' => new JavaScriptContent( 'hello world.' ),
114 'user_text' => 'SomeTextUserName',
115 'user' => 99,
116
117 ],
118 99,
119 'SomeTextUserName',
120 ];
121 yield 'user text only' => [
122 [
123 'content' => new JavaScriptContent( 'hello world.' ),
124 'user_text' => '111.111.111.111',
125 ],
126 0,
127 '111.111.111.111',
128 ];
129 }
130
131 /**
132 * @dataProvider provideConstructFromArray_userSetAsExpected
133 * @covers Revision::__construct
134 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
135 *
136 * @param array $rowArray
137 * @param mixed $expectedUserId null to expect the current wgUser ID
138 * @param mixed $expectedUserName null to expect the current wgUser name
139 */
140 public function testConstructFromArray_userSetAsExpected(
141 array $rowArray,
142 $expectedUserId,
143 $expectedUserName
144 ) {
145 $testUser = $this->getTestUser()->getUser();
146 $this->setMwGlobals( 'wgUser', $testUser );
147 if ( $expectedUserId === null ) {
148 $expectedUserId = $testUser->getId();
149 }
150 if ( $expectedUserName === null ) {
151 $expectedUserName = $testUser->getName();
152 }
153
154 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
155 $this->assertEquals( $expectedUserId, $rev->getUser() );
156 $this->assertEquals( $expectedUserName, $rev->getUserText() );
157 }
158
159 public function provideConstructFromArrayThrowsExceptions() {
160 yield 'content and text_id both not empty' => [
161 [
162 'content' => new WikitextContent( 'GOAT' ),
163 'text_id' => 'someid',
164 ],
165 new MWException( 'The text_id field is only available in the pre-MCR schema' )
166 ];
167
168 yield 'with bad content object (class)' => [
169 [ 'content' => new stdClass() ],
170 new MWException( 'content field must contain a Content object' )
171 ];
172 yield 'with bad content object (string)' => [
173 [ 'content' => 'ImAGoat' ],
174 new MWException( 'content field must contain a Content object' )
175 ];
176 yield 'bad row format' => [
177 'imastring, not a row',
178 new InvalidArgumentException(
179 '$row must be a row object, an associative array, or a RevisionRecord'
180 )
181 ];
182 }
183
184 /**
185 * @dataProvider provideConstructFromArrayThrowsExceptions
186 * @covers Revision::__construct
187 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
188 */
189 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
190 $this->setExpectedException(
191 get_class( $expectedException ),
192 $expectedException->getMessage(),
193 $expectedException->getCode()
194 );
195 new Revision( $rowArray, 0, $this->getMockTitle() );
196 }
197
198 /**
199 * @covers Revision::__construct
200 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
201 */
202 public function testConstructFromNothing() {
203 $this->setExpectedException(
204 InvalidArgumentException::class
205 );
206 new Revision( [] );
207 }
208
209 public function provideConstructFromRow() {
210 yield 'Full construction' => [
211 [
212 'rev_id' => '42',
213 'rev_page' => '23',
214 'rev_timestamp' => '20171017114835',
215 'rev_user_text' => '127.0.0.1',
216 'rev_user' => '0',
217 'rev_minor_edit' => '0',
218 'rev_deleted' => '0',
219 'rev_len' => '46',
220 'rev_parent_id' => '1',
221 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
222 'rev_comment_text' => 'Goat Comment!',
223 'rev_comment_data' => null,
224 'rev_comment_cid' => null,
225 ],
226 function ( RevisionTest $testCase, Revision $rev ) {
227 $testCase->assertSame( 42, $rev->getId() );
228 $testCase->assertSame( 23, $rev->getPage() );
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 }
239 ];
240 yield 'default field values' => [
241 [
242 'rev_id' => '42',
243 'rev_page' => '23',
244 'rev_timestamp' => '20171017114835',
245 'rev_user_text' => '127.0.0.1',
246 'rev_user' => '0',
247 'rev_minor_edit' => '0',
248 'rev_deleted' => '0',
249 'rev_comment_text' => 'Goat Comment!',
250 'rev_comment_data' => null,
251 'rev_comment_cid' => null,
252 ],
253 function ( RevisionTest $testCase, Revision $rev ) {
254 // parent ID may be null
255 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
256
257 // given fields
258 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
259 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
260 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
261 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
262 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
263 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
264 }
265 ];
266 }
267
268 /**
269 * @dataProvider provideConstructFromRow
270 * @covers Revision::__construct
271 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
272 */
273 public function testConstructFromRow( array $arrayData, callable $assertions ) {
274 $row = (object)$arrayData;
275 $rev = new Revision( $row, 0, $this->getMockTitle() );
276 $assertions( $this, $rev );
277 }
278
279 /**
280 * @covers Revision::__construct
281 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
282 */
283 public function testConstructFromRowWithBadPageId() {
284 Wikimedia\suppressWarnings();
285 $rev = new Revision( (object)[
286 'rev_page' => 77777777,
287 'rev_comment_text' => '',
288 'rev_comment_data' => null,
289 ] );
290 $this->assertSame( 77777777, $rev->getPage() );
291 Wikimedia\restoreWarnings();
292 }
293
294 public function provideGetId() {
295 yield [
296 [],
297 null
298 ];
299 yield [
300 [ 'id' => 998 ],
301 998
302 ];
303 }
304
305 /**
306 * @dataProvider provideGetId
307 * @covers Revision::getId
308 */
309 public function testGetId( $rowArray, $expectedId ) {
310 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
311 $this->assertEquals( $expectedId, $rev->getId() );
312 }
313
314 public function provideSetId() {
315 yield [ '123', 123 ];
316 yield [ 456, 456 ];
317 }
318
319 /**
320 * @dataProvider provideSetId
321 * @covers Revision::setId
322 */
323 public function testSetId( $input, $expected ) {
324 $rev = new Revision( [], 0, $this->getMockTitle() );
325 $rev->setId( $input );
326 $this->assertSame( $expected, $rev->getId() );
327 }
328
329 public function provideSetUserIdAndName() {
330 yield [ '123', 123, 'GOaT' ];
331 yield [ 456, 456, 'GOaT' ];
332 }
333
334 /**
335 * @dataProvider provideSetUserIdAndName
336 * @covers Revision::setUserIdAndName
337 */
338 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
339 $rev = new Revision( [], 0, $this->getMockTitle() );
340 $rev->setUserIdAndName( $inputId, $name );
341 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
342 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
343 }
344
345 public function provideGetParentId() {
346 yield [ [], null ];
347 yield [ [ 'parent_id' => '123' ], 123 ];
348 yield [ [ 'parent_id' => 456 ], 456 ];
349 }
350
351 /**
352 * @dataProvider provideGetParentId
353 * @covers Revision::getParentId()
354 */
355 public function testGetParentId( $rowArray, $expected ) {
356 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
357 $this->assertSame( $expected, $rev->getParentId() );
358 }
359
360 public function provideGetRevisionText() {
361 yield 'Generic test' => [
362 'This is a goat of revision text.',
363 (object)[
364 'old_flags' => '',
365 'old_text' => 'This is a goat of revision text.',
366 ],
367 ];
368 yield 'garbage in, garbage out' => [
369 false,
370 false,
371 ];
372 }
373
374 /**
375 * @covers Revision::getRevisionText
376 * @dataProvider provideGetRevisionText
377 */
378 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
379 $this->assertEquals(
380 $expected,
381 Revision::getRevisionText( $rowData, $prefix, $wiki ) );
382 }
383
384 public function provideGetRevisionTextWithZlibExtension() {
385 yield 'Generic gzip test' => [
386 'This is a small goat of revision text.',
387 (object)[
388 'old_flags' => 'gzip',
389 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
390 ],
391 ];
392 }
393
394 /**
395 * @covers Revision::getRevisionText
396 * @dataProvider provideGetRevisionTextWithZlibExtension
397 */
398 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
399 $this->checkPHPExtension( 'zlib' );
400 $this->testGetRevisionText( $expected, $rowData );
401 }
402
403 public function provideGetRevisionTextWithZlibExtension_badData() {
404 yield 'Generic gzip test' => [
405 'This is a small goat of revision text.',
406 (object)[
407 'old_flags' => 'gzip',
408 'old_text' => 'DEAD BEEF',
409 ],
410 ];
411 }
412
413 /**
414 * @covers Revision::getRevisionText
415 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
416 */
417 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
418 $this->checkPHPExtension( 'zlib' );
419 Wikimedia\suppressWarnings();
420 $this->assertFalse(
421 Revision::getRevisionText(
422 (object)$rowData
423 )
424 );
425 Wikimedia\suppressWarnings( true );
426 }
427
428 private function getWANObjectCache() {
429 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
430 }
431
432 /**
433 * @return SqlBlobStore
434 */
435 private function getBlobStore() {
436 /** @var LoadBalancer $lb */
437 $lb = $this->getMockBuilder( LoadBalancer::class )
438 ->disableOriginalConstructor()
439 ->getMock();
440 $access = MediaWikiServices::getInstance()->getExternalStoreAccess();
441 $cache = $this->getWANObjectCache();
442
443 $blobStore = new SqlBlobStore( $lb, $access, $cache );
444
445 return $blobStore;
446 }
447
448 private function mockBlobStoreFactory( $blobStore ) {
449 /** @var LoadBalancer $lb */
450 $factory = $this->getMockBuilder( BlobStoreFactory::class )
451 ->disableOriginalConstructor()
452 ->getMock();
453 $factory->expects( $this->any() )
454 ->method( 'newBlobStore' )
455 ->willReturn( $blobStore );
456 $factory->expects( $this->any() )
457 ->method( 'newSqlBlobStore' )
458 ->willReturn( $blobStore );
459 return $factory;
460 }
461
462 /**
463 * @return RevisionStore
464 */
465 private function getRevisionStore() {
466 /** @var LoadBalancer $lb */
467 $lb = $this->getMockBuilder( LoadBalancer::class )
468 ->disableOriginalConstructor()
469 ->getMock();
470
471 $cache = $this->getWANObjectCache();
472
473 $blobStore = new RevisionStore(
474 $lb,
475 $this->getBlobStore(),
476 $cache,
477 MediaWikiServices::getInstance()->getCommentStore(),
478 MediaWikiServices::getInstance()->getContentModelStore(),
479 MediaWikiServices::getInstance()->getSlotRoleStore(),
480 MediaWikiServices::getInstance()->getSlotRoleRegistry(),
481 MIGRATION_OLD,
482 MediaWikiServices::getInstance()->getActorMigration()
483 );
484 return $blobStore;
485 }
486
487 public function provideGetRevisionTextWithLegacyEncoding() {
488 yield 'Utf8Native' => [
489 "Wiki est l'\xc3\xa9cole superieur !",
490 'fr',
491 'iso-8859-1',
492 (object)[
493 'old_flags' => 'utf-8',
494 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
495 ]
496 ];
497 yield 'Utf8Legacy' => [
498 "Wiki est l'\xc3\xa9cole superieur !",
499 'fr',
500 'iso-8859-1',
501 (object)[
502 'old_flags' => '',
503 'old_text' => "Wiki est l'\xe9cole superieur !",
504 ]
505 ];
506 }
507
508 /**
509 * @covers Revision::getRevisionText
510 * @dataProvider provideGetRevisionTextWithLegacyEncoding
511 */
512 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
513 $blobStore = $this->getBlobStore();
514 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
515 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
516
517 $this->testGetRevisionText( $expected, $rowData );
518 }
519
520 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
521 /**
522 * WARNING!
523 * Do not set the external flag!
524 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
525 */
526 yield 'Utf8NativeGzip' => [
527 "Wiki est l'\xc3\xa9cole superieur !",
528 'fr',
529 'iso-8859-1',
530 (object)[
531 'old_flags' => 'gzip,utf-8',
532 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
533 ]
534 ];
535 yield 'Utf8LegacyGzip' => [
536 "Wiki est l'\xc3\xa9cole superieur !",
537 'fr',
538 'iso-8859-1',
539 (object)[
540 'old_flags' => 'gzip',
541 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
542 ]
543 ];
544 }
545
546 /**
547 * @covers Revision::getRevisionText
548 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
549 */
550 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
551 $this->checkPHPExtension( 'zlib' );
552
553 $blobStore = $this->getBlobStore();
554 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
555 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
556
557 $this->testGetRevisionText( $expected, $rowData );
558 }
559
560 /**
561 * @covers Revision::compressRevisionText
562 */
563 public function testCompressRevisionTextUtf8() {
564 $row = new stdClass;
565 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
566 $row->old_flags = Revision::compressRevisionText( $row->old_text );
567 $this->assertTrue( strpos( $row->old_flags, 'utf-8' ) !== false,
568 "Flags should contain 'utf-8'" );
569 $this->assertFalse( strpos( $row->old_flags, 'gzip' ) !== false,
570 "Flags should not contain 'gzip'" );
571 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
572 $row->old_text, "Direct check" );
573 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
574 Revision::getRevisionText( $row ), "getRevisionText" );
575 }
576
577 /**
578 * @covers Revision::compressRevisionText
579 */
580 public function testCompressRevisionTextUtf8Gzip() {
581 $this->checkPHPExtension( 'zlib' );
582
583 $blobStore = $this->getBlobStore();
584 $blobStore->setCompressBlobs( true );
585 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
586
587 $row = new stdClass;
588 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
589 $row->old_flags = Revision::compressRevisionText( $row->old_text );
590 $this->assertTrue( strpos( $row->old_flags, 'utf-8' ) !== false,
591 "Flags should contain 'utf-8'" );
592 $this->assertTrue( strpos( $row->old_flags, 'gzip' ) !== false,
593 "Flags should contain 'gzip'" );
594 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
595 gzinflate( $row->old_text ), "Direct check" );
596 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
597 Revision::getRevisionText( $row ), "getRevisionText" );
598 }
599
600 /**
601 * @covers Revision::loadFromTitle
602 */
603 public function testLoadFromTitle() {
604 $title = $this->getMockTitle();
605
606 $conditions = [
607 'rev_id=page_latest',
608 'page_namespace' => $title->getNamespace(),
609 'page_title' => $title->getDBkey()
610 ];
611
612 $row = (object)[
613 'rev_id' => '42',
614 'rev_page' => $title->getArticleID(),
615 'rev_timestamp' => '20171017114835',
616 'rev_user_text' => '127.0.0.1',
617 'rev_user' => '0',
618 'rev_minor_edit' => '0',
619 'rev_deleted' => '0',
620 'rev_len' => '46',
621 'rev_parent_id' => '1',
622 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
623 'rev_comment_text' => 'Goat Comment!',
624 'rev_comment_data' => null,
625 'rev_comment_cid' => null,
626 'rev_content_format' => 'GOATFORMAT',
627 'rev_content_model' => 'GOATMODEL',
628 ];
629
630 $domain = MediaWikiServices::getInstance()->getDBLoadBalancer()->getLocalDomainID();
631 $db = $this->getMock( IDatabase::class );
632 $db->expects( $this->any() )
633 ->method( 'getDomainId' )
634 ->will( $this->returnValue( $domain ) );
635 $db->expects( $this->once() )
636 ->method( 'selectRow' )
637 ->with(
638 $this->equalTo( [
639 'revision', 'page', 'user',
640 'temp_rev_comment' => 'revision_comment_temp', 'comment_rev_comment' => 'comment',
641 'temp_rev_user' => 'revision_actor_temp', 'actor_rev_user' => 'actor',
642 ] ),
643 // We don't really care about the fields are they come from the selectField methods
644 $this->isType( 'array' ),
645 $this->equalTo( $conditions ),
646 // Method name
647 $this->stringContains( 'fetchRevisionRowFromConds' ),
648 // We don't really care about the options here
649 $this->isType( 'array' ),
650 // We don't really care about the join conds are they come from the joinCond methods
651 $this->isType( 'array' )
652 )
653 ->willReturn( $row );
654
655 $revision = Revision::loadFromTitle( $db, $title );
656
657 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
658 $this->assertEquals( $row->rev_id, $revision->getId() );
659 $this->assertEquals( $row->rev_len, $revision->getSize() );
660 $this->assertEquals( $row->rev_sha1, $revision->getSha1() );
661 $this->assertEquals( $row->rev_parent_id, $revision->getParentId() );
662 $this->assertEquals( $row->rev_timestamp, $revision->getTimestamp() );
663 $this->assertEquals( $row->rev_comment_text, $revision->getComment() );
664 $this->assertEquals( $row->rev_user_text, $revision->getUserText() );
665 }
666
667 public function provideDecompressRevisionText() {
668 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
669 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
670 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
671 yield '(no legacy encoding), string in with gzip flag returns string' => [
672 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
673 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
674 ];
675 yield '(no legacy encoding), string in with object flag returns false' => [
676 // gzip string below generated with serialize( 'JOJO' )
677 false, "s:4:\"JOJO\";", [ 'object' ], false,
678 ];
679 yield '(no legacy encoding), serialized object in with object flag returns string' => [
680 false,
681 // Using a TitleValue object as it has a getText method (which is needed)
682 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
683 [ 'object' ],
684 'HHJJDDFF',
685 ];
686 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
687 false,
688 // Using a TitleValue object as it has a getText method (which is needed)
689 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
690 [ 'object', 'gzip' ],
691 '8219JJJ840',
692 ];
693 yield '(ISO-8859-1 encoding), string in string out' => [
694 'ISO-8859-1',
695 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
696 [],
697 '1®Àþ1',
698 ];
699 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
700 'ISO-8859-1',
701 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
702 [ 'gzip' ],
703 '4®Àþ4',
704 ];
705 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
706 'ISO-8859-1',
707 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
708 [ 'object' ],
709 '3®Àþ3',
710 ];
711 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
712 'ISO-8859-1',
713 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
714 [ 'gzip', 'object' ],
715 '2®Àþ2',
716 ];
717 }
718
719 /**
720 * @dataProvider provideDecompressRevisionText
721 * @covers Revision::decompressRevisionText
722 *
723 * @param bool $legacyEncoding
724 * @param mixed $text
725 * @param array $flags
726 * @param mixed $expected
727 */
728 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
729 $blobStore = $this->getBlobStore();
730 if ( $legacyEncoding ) {
731 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
732 }
733
734 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
735 $this->assertSame(
736 $expected,
737 Revision::decompressRevisionText( $text, $flags )
738 );
739 }
740
741 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
742 yield 'Just text' => [
743 (object)[ 'old_text' => 'SomeText' ],
744 'old_',
745 'SomeText'
746 ];
747 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
748 yield 'gzip text' => [
749 (object)[
750 'old_text' => "sttttr\002\022\000",
751 'old_flags' => 'gzip'
752 ],
753 'old_',
754 'AAAABBAAA'
755 ];
756 yield 'gzip text and different prefix' => [
757 (object)[
758 'jojo_text' => "sttttr\002\022\000",
759 'jojo_flags' => 'gzip'
760 ],
761 'jojo_',
762 'AAAABBAAA'
763 ];
764 }
765
766 /**
767 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
768 * @covers Revision::getRevisionText
769 */
770 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
771 $row,
772 $prefix,
773 $expected
774 ) {
775 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
776 }
777
778 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
779 yield 'Just some text' => [ 'someNonUrlText' ];
780 yield 'No second URL part' => [ 'someProtocol://' ];
781 }
782
783 /**
784 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
785 * @covers Revision::getRevisionText
786 */
787 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
788 $text
789 ) {
790 Wikimedia\suppressWarnings();
791 $this->assertFalse(
792 Revision::getRevisionText(
793 (object)[
794 'old_text' => $text,
795 'old_flags' => 'external',
796 ]
797 )
798 );
799 Wikimedia\suppressWarnings( true );
800 }
801
802 /**
803 * @covers Revision::getRevisionText
804 */
805 public function testGetRevisionText_external_noOldId() {
806 $this->setService(
807 'ExternalStoreFactory',
808 new ExternalStoreFactory( [ 'ForTesting' ], [ 'ForTesting://cluster1' ], 'test-id' )
809 );
810 $this->assertSame(
811 'AAAABBAAA',
812 Revision::getRevisionText(
813 (object)[
814 'old_text' => 'ForTesting://cluster1/12345',
815 'old_flags' => 'external,gzip',
816 ]
817 )
818 );
819 }
820
821 /**
822 * @covers Revision::getRevisionText
823 */
824 public function testGetRevisionText_external_oldId() {
825 $cache = $this->getWANObjectCache();
826 $this->setService( 'MainWANObjectCache', $cache );
827
828 $this->setService(
829 'ExternalStoreFactory',
830 new ExternalStoreFactory( [ 'ForTesting' ], [ 'ForTesting://cluster1' ], 'test-id' )
831 );
832
833 $lb = $this->getMockBuilder( LoadBalancer::class )
834 ->disableOriginalConstructor()
835 ->getMock();
836 $access = MediaWikiServices::getInstance()->getExternalStoreAccess();
837
838 $blobStore = new SqlBlobStore( $lb, $access, $cache );
839 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
840
841 $this->assertSame(
842 'AAAABBAAA',
843 Revision::getRevisionText(
844 (object)[
845 'old_text' => 'ForTesting://cluster1/12345',
846 'old_flags' => 'external,gzip',
847 'old_id' => '7777',
848 ]
849 )
850 );
851
852 $cacheKey = $cache->makeGlobalKey(
853 'SqlBlobStore-blob',
854 $lb->getLocalDomainID(),
855 'tt:7777'
856 );
857 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
858 }
859
860 /**
861 * @covers Revision::getSize
862 */
863 public function testGetSize() {
864 $title = $this->getMockTitle();
865
866 $rec = new MutableRevisionRecord( $title );
867 $rev = new Revision( $rec, 0, $title );
868
869 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
870
871 $rec->setSize( 13 );
872 $this->assertSame( 13, $rev->getSize() );
873 }
874
875 /**
876 * @covers Revision::getSize
877 */
878 public function testGetSize_failure() {
879 $title = $this->getMockTitle();
880
881 $rec = $this->getMockBuilder( RevisionRecord::class )
882 ->disableOriginalConstructor()
883 ->getMock();
884
885 $rec->method( 'getSize' )
886 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
887
888 $rev = new Revision( $rec, 0, $title );
889 $this->assertNull( $rev->getSize() );
890 }
891
892 /**
893 * @covers Revision::getSha1
894 */
895 public function testGetSha1() {
896 $title = $this->getMockTitle();
897
898 $rec = new MutableRevisionRecord( $title );
899 $rev = new Revision( $rec, 0, $title );
900
901 $emptyHash = SlotRecord::base36Sha1( '' );
902 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
903
904 $rec->setSha1( 'deadbeef' );
905 $this->assertSame( 'deadbeef', $rev->getSha1() );
906 }
907
908 /**
909 * @covers Revision::getSha1
910 */
911 public function testGetSha1_failure() {
912 $title = $this->getMockTitle();
913
914 $rec = $this->getMockBuilder( RevisionRecord::class )
915 ->disableOriginalConstructor()
916 ->getMock();
917
918 $rec->method( 'getSha1' )
919 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
920
921 $rev = new Revision( $rec, 0, $title );
922 $this->assertNull( $rev->getSha1() );
923 }
924
925 /**
926 * @covers Revision::getContent
927 */
928 public function testGetContent() {
929 $title = $this->getMockTitle();
930
931 $rec = new MutableRevisionRecord( $title );
932 $rev = new Revision( $rec, 0, $title );
933
934 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
935
936 $content = new TextContent( 'Hello Kittens!' );
937 $rec->setContent( SlotRecord::MAIN, $content );
938 $this->assertSame( $content, $rev->getContent() );
939 }
940
941 /**
942 * @covers Revision::getContent
943 */
944 public function testGetContent_failure() {
945 $title = $this->getMockTitle();
946
947 $rec = $this->getMockBuilder( RevisionRecord::class )
948 ->disableOriginalConstructor()
949 ->getMock();
950
951 $rec->method( 'getContent' )
952 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
953
954 $rev = new Revision( $rec, 0, $title );
955 $this->assertNull( $rev->getContent() );
956 }
957
958 }