Merge "Special:Preferences: Expose `.mw-navigation-hint` on keyboard focus only"
[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 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
285 $this->overrideMwServices();
286 Wikimedia\suppressWarnings();
287 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
288 $this->assertSame( 77777777, $rev->getPage() );
289 Wikimedia\restoreWarnings();
290 }
291
292 public function provideGetId() {
293 yield [
294 [],
295 null
296 ];
297 yield [
298 [ 'id' => 998 ],
299 998
300 ];
301 }
302
303 /**
304 * @dataProvider provideGetId
305 * @covers Revision::getId
306 */
307 public function testGetId( $rowArray, $expectedId ) {
308 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
309 $this->assertEquals( $expectedId, $rev->getId() );
310 }
311
312 public function provideSetId() {
313 yield [ '123', 123 ];
314 yield [ 456, 456 ];
315 }
316
317 /**
318 * @dataProvider provideSetId
319 * @covers Revision::setId
320 */
321 public function testSetId( $input, $expected ) {
322 $rev = new Revision( [], 0, $this->getMockTitle() );
323 $rev->setId( $input );
324 $this->assertSame( $expected, $rev->getId() );
325 }
326
327 public function provideSetUserIdAndName() {
328 yield [ '123', 123, 'GOaT' ];
329 yield [ 456, 456, 'GOaT' ];
330 }
331
332 /**
333 * @dataProvider provideSetUserIdAndName
334 * @covers Revision::setUserIdAndName
335 */
336 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
337 $rev = new Revision( [], 0, $this->getMockTitle() );
338 $rev->setUserIdAndName( $inputId, $name );
339 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
340 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
341 }
342
343 public function provideGetParentId() {
344 yield [ [], null ];
345 yield [ [ 'parent_id' => '123' ], 123 ];
346 yield [ [ 'parent_id' => 456 ], 456 ];
347 }
348
349 /**
350 * @dataProvider provideGetParentId
351 * @covers Revision::getParentId()
352 */
353 public function testGetParentId( $rowArray, $expected ) {
354 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
355 $this->assertSame( $expected, $rev->getParentId() );
356 }
357
358 public function provideGetRevisionText() {
359 yield 'Generic test' => [
360 'This is a goat of revision text.',
361 (object)[
362 'old_flags' => '',
363 'old_text' => 'This is a goat of revision text.',
364 ],
365 ];
366 yield 'garbage in, garbage out' => [
367 false,
368 false,
369 ];
370 }
371
372 /**
373 * @covers Revision::getRevisionText
374 * @dataProvider provideGetRevisionText
375 */
376 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
377 $this->assertEquals(
378 $expected,
379 Revision::getRevisionText( $rowData, $prefix, $wiki ) );
380 }
381
382 public function provideGetRevisionTextWithZlibExtension() {
383 yield 'Generic gzip test' => [
384 'This is a small goat of revision text.',
385 (object)[
386 'old_flags' => 'gzip',
387 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
388 ],
389 ];
390 }
391
392 /**
393 * @covers Revision::getRevisionText
394 * @dataProvider provideGetRevisionTextWithZlibExtension
395 */
396 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
397 $this->checkPHPExtension( 'zlib' );
398 $this->testGetRevisionText( $expected, $rowData );
399 }
400
401 public function provideGetRevisionTextWithZlibExtension_badData() {
402 yield 'Generic gzip test' => [
403 'This is a small goat of revision text.',
404 (object)[
405 'old_flags' => 'gzip',
406 'old_text' => 'DEAD BEEF',
407 ],
408 ];
409 }
410
411 /**
412 * @covers Revision::getRevisionText
413 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
414 */
415 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
416 $this->checkPHPExtension( 'zlib' );
417 Wikimedia\suppressWarnings();
418 $this->assertFalse(
419 Revision::getRevisionText(
420 (object)$rowData
421 )
422 );
423 Wikimedia\suppressWarnings( true );
424 }
425
426 private function getWANObjectCache() {
427 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
428 }
429
430 /**
431 * @return SqlBlobStore
432 */
433 private function getBlobStore() {
434 /** @var LoadBalancer $lb */
435 $lb = $this->getMockBuilder( LoadBalancer::class )
436 ->disableOriginalConstructor()
437 ->getMock();
438
439 $cache = $this->getWANObjectCache();
440
441 $blobStore = new SqlBlobStore( $lb, $cache );
442 return $blobStore;
443 }
444
445 private function mockBlobStoreFactory( $blobStore ) {
446 /** @var LoadBalancer $lb */
447 $factory = $this->getMockBuilder( BlobStoreFactory::class )
448 ->disableOriginalConstructor()
449 ->getMock();
450 $factory->expects( $this->any() )
451 ->method( 'newBlobStore' )
452 ->willReturn( $blobStore );
453 $factory->expects( $this->any() )
454 ->method( 'newSqlBlobStore' )
455 ->willReturn( $blobStore );
456 return $factory;
457 }
458
459 /**
460 * @return RevisionStore
461 */
462 private function getRevisionStore() {
463 /** @var LoadBalancer $lb */
464 $lb = $this->getMockBuilder( LoadBalancer::class )
465 ->disableOriginalConstructor()
466 ->getMock();
467
468 $cache = $this->getWANObjectCache();
469
470 $blobStore = new RevisionStore(
471 $lb,
472 $this->getBlobStore(),
473 $cache,
474 MediaWikiServices::getInstance()->getCommentStore(),
475 MediaWikiServices::getInstance()->getContentModelStore(),
476 MediaWikiServices::getInstance()->getSlotRoleStore(),
477 MIGRATION_OLD,
478 MediaWikiServices::getInstance()->getActorMigration()
479 );
480 return $blobStore;
481 }
482
483 public function provideGetRevisionTextWithLegacyEncoding() {
484 yield 'Utf8Native' => [
485 "Wiki est l'\xc3\xa9cole superieur !",
486 'fr',
487 'iso-8859-1',
488 (object)[
489 'old_flags' => 'utf-8',
490 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
491 ]
492 ];
493 yield 'Utf8Legacy' => [
494 "Wiki est l'\xc3\xa9cole superieur !",
495 'fr',
496 'iso-8859-1',
497 (object)[
498 'old_flags' => '',
499 'old_text' => "Wiki est l'\xe9cole superieur !",
500 ]
501 ];
502 }
503
504 /**
505 * @covers Revision::getRevisionText
506 * @dataProvider provideGetRevisionTextWithLegacyEncoding
507 */
508 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
509 $blobStore = $this->getBlobStore();
510 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
511 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
512
513 $this->testGetRevisionText( $expected, $rowData );
514 }
515
516 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
517 /**
518 * WARNING!
519 * Do not set the external flag!
520 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
521 */
522 yield 'Utf8NativeGzip' => [
523 "Wiki est l'\xc3\xa9cole superieur !",
524 'fr',
525 'iso-8859-1',
526 (object)[
527 'old_flags' => 'gzip,utf-8',
528 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
529 ]
530 ];
531 yield 'Utf8LegacyGzip' => [
532 "Wiki est l'\xc3\xa9cole superieur !",
533 'fr',
534 'iso-8859-1',
535 (object)[
536 'old_flags' => 'gzip',
537 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
538 ]
539 ];
540 }
541
542 /**
543 * @covers Revision::getRevisionText
544 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
545 */
546 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
547 $this->checkPHPExtension( 'zlib' );
548
549 $blobStore = $this->getBlobStore();
550 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
551 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
552
553 $this->testGetRevisionText( $expected, $rowData );
554 }
555
556 /**
557 * @covers Revision::compressRevisionText
558 */
559 public function testCompressRevisionTextUtf8() {
560 $row = new stdClass;
561 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
562 $row->old_flags = Revision::compressRevisionText( $row->old_text );
563 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
564 "Flags should contain 'utf-8'" );
565 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
566 "Flags should not contain 'gzip'" );
567 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
568 $row->old_text, "Direct check" );
569 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
570 Revision::getRevisionText( $row ), "getRevisionText" );
571 }
572
573 /**
574 * @covers Revision::compressRevisionText
575 */
576 public function testCompressRevisionTextUtf8Gzip() {
577 $this->checkPHPExtension( 'zlib' );
578
579 $blobStore = $this->getBlobStore();
580 $blobStore->setCompressBlobs( true );
581 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
582
583 $row = new stdClass;
584 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
585 $row->old_flags = Revision::compressRevisionText( $row->old_text );
586 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
587 "Flags should contain 'utf-8'" );
588 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
589 "Flags should contain 'gzip'" );
590 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
591 gzinflate( $row->old_text ), "Direct check" );
592 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
593 Revision::getRevisionText( $row ), "getRevisionText" );
594 }
595
596 /**
597 * @covers Revision::loadFromTitle
598 */
599 public function testLoadFromTitle() {
600 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
601 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD );
602 $this->overrideMwServices();
603 $title = $this->getMockTitle();
604
605 $conditions = [
606 'rev_id=page_latest',
607 'page_namespace' => $title->getNamespace(),
608 'page_title' => $title->getDBkey()
609 ];
610
611 $row = (object)[
612 'rev_id' => '42',
613 'rev_page' => $title->getArticleID(),
614 'rev_timestamp' => '20171017114835',
615 'rev_user_text' => '127.0.0.1',
616 'rev_user' => '0',
617 'rev_minor_edit' => '0',
618 'rev_deleted' => '0',
619 'rev_len' => '46',
620 'rev_parent_id' => '1',
621 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
622 'rev_comment_text' => 'Goat Comment!',
623 'rev_comment_data' => null,
624 'rev_comment_cid' => null,
625 'rev_content_format' => 'GOATFORMAT',
626 'rev_content_model' => 'GOATMODEL',
627 ];
628
629 $domain = MediaWikiServices::getInstance()->getDBLoadBalancer()->getLocalDomainID();
630 $db = $this->getMock( IDatabase::class );
631 $db->expects( $this->any() )
632 ->method( 'getDomainId' )
633 ->will( $this->returnValue( $domain ) );
634 $db->expects( $this->once() )
635 ->method( 'selectRow' )
636 ->with(
637 $this->equalTo( [ 'revision', 'page', 'user' ] ),
638 // We don't really care about the fields are they come from the selectField methods
639 $this->isType( 'array' ),
640 $this->equalTo( $conditions ),
641 // Method name
642 $this->stringContains( 'fetchRevisionRowFromConds' ),
643 // We don't really care about the options here
644 $this->isType( 'array' ),
645 // We don't really care about the join conds are they come from the joinCond methods
646 $this->isType( 'array' )
647 )
648 ->willReturn( $row );
649
650 $revision = Revision::loadFromTitle( $db, $title );
651
652 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
653 $this->assertEquals( $row->rev_id, $revision->getId() );
654 $this->assertEquals( $row->rev_len, $revision->getSize() );
655 $this->assertEquals( $row->rev_sha1, $revision->getSha1() );
656 $this->assertEquals( $row->rev_parent_id, $revision->getParentId() );
657 $this->assertEquals( $row->rev_timestamp, $revision->getTimestamp() );
658 $this->assertEquals( $row->rev_comment_text, $revision->getComment() );
659 $this->assertEquals( $row->rev_user_text, $revision->getUserText() );
660 }
661
662 public function provideDecompressRevisionText() {
663 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
664 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
665 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
666 yield '(no legacy encoding), string in with gzip flag returns string' => [
667 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
668 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
669 ];
670 yield '(no legacy encoding), string in with object flag returns false' => [
671 // gzip string below generated with serialize( 'JOJO' )
672 false, "s:4:\"JOJO\";", [ 'object' ], false,
673 ];
674 yield '(no legacy encoding), serialized object in with object flag returns string' => [
675 false,
676 // Using a TitleValue object as it has a getText method (which is needed)
677 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
678 [ 'object' ],
679 'HHJJDDFF',
680 ];
681 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
682 false,
683 // Using a TitleValue object as it has a getText method (which is needed)
684 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
685 [ 'object', 'gzip' ],
686 '8219JJJ840',
687 ];
688 yield '(ISO-8859-1 encoding), string in string out' => [
689 'ISO-8859-1',
690 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
691 [],
692 '1®Àþ1',
693 ];
694 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
695 'ISO-8859-1',
696 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
697 [ 'gzip' ],
698 '4®Àþ4',
699 ];
700 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
701 'ISO-8859-1',
702 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
703 [ 'object' ],
704 '3®Àþ3',
705 ];
706 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
707 'ISO-8859-1',
708 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
709 [ 'gzip', 'object' ],
710 '2®Àþ2',
711 ];
712 }
713
714 /**
715 * @dataProvider provideDecompressRevisionText
716 * @covers Revision::decompressRevisionText
717 *
718 * @param bool $legacyEncoding
719 * @param mixed $text
720 * @param array $flags
721 * @param mixed $expected
722 */
723 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
724 $blobStore = $this->getBlobStore();
725 if ( $legacyEncoding ) {
726 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
727 }
728
729 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
730 $this->assertSame(
731 $expected,
732 Revision::decompressRevisionText( $text, $flags )
733 );
734 }
735
736 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
737 yield 'Just text' => [
738 (object)[ 'old_text' => 'SomeText' ],
739 'old_',
740 'SomeText'
741 ];
742 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
743 yield 'gzip text' => [
744 (object)[
745 'old_text' => "sttttr\002\022\000",
746 'old_flags' => 'gzip'
747 ],
748 'old_',
749 'AAAABBAAA'
750 ];
751 yield 'gzip text and different prefix' => [
752 (object)[
753 'jojo_text' => "sttttr\002\022\000",
754 'jojo_flags' => 'gzip'
755 ],
756 'jojo_',
757 'AAAABBAAA'
758 ];
759 }
760
761 /**
762 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
763 * @covers Revision::getRevisionText
764 */
765 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
766 $row,
767 $prefix,
768 $expected
769 ) {
770 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
771 }
772
773 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
774 yield 'Just some text' => [ 'someNonUrlText' ];
775 yield 'No second URL part' => [ 'someProtocol://' ];
776 }
777
778 /**
779 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
780 * @covers Revision::getRevisionText
781 */
782 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
783 $text
784 ) {
785 Wikimedia\suppressWarnings();
786 $this->assertFalse(
787 Revision::getRevisionText(
788 (object)[
789 'old_text' => $text,
790 'old_flags' => 'external',
791 ]
792 )
793 );
794 Wikimedia\suppressWarnings( true );
795 }
796
797 /**
798 * @covers Revision::getRevisionText
799 */
800 public function testGetRevisionText_external_noOldId() {
801 $this->setService(
802 'ExternalStoreFactory',
803 new ExternalStoreFactory( [ 'ForTesting' ] )
804 );
805 $this->assertSame(
806 'AAAABBAAA',
807 Revision::getRevisionText(
808 (object)[
809 'old_text' => 'ForTesting://cluster1/12345',
810 'old_flags' => 'external,gzip',
811 ]
812 )
813 );
814 }
815
816 /**
817 * @covers Revision::getRevisionText
818 */
819 public function testGetRevisionText_external_oldId() {
820 $cache = $this->getWANObjectCache();
821 $this->setService( 'MainWANObjectCache', $cache );
822
823 $this->setService(
824 'ExternalStoreFactory',
825 new ExternalStoreFactory( [ 'ForTesting' ] )
826 );
827
828 $lb = $this->getMockBuilder( LoadBalancer::class )
829 ->disableOriginalConstructor()
830 ->getMock();
831
832 $blobStore = new SqlBlobStore( $lb, $cache );
833 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
834
835 $this->assertSame(
836 'AAAABBAAA',
837 Revision::getRevisionText(
838 (object)[
839 'old_text' => 'ForTesting://cluster1/12345',
840 'old_flags' => 'external,gzip',
841 'old_id' => '7777',
842 ]
843 )
844 );
845
846 $cacheKey = $cache->makeGlobalKey(
847 'BlobStore',
848 'address',
849 $lb->getLocalDomainID(),
850 'tt:7777'
851 );
852 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
853 }
854
855 /**
856 * @covers Revision::getSize
857 */
858 public function testGetSize() {
859 $title = $this->getMockTitle();
860
861 $rec = new MutableRevisionRecord( $title );
862 $rev = new Revision( $rec, 0, $title );
863
864 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
865
866 $rec->setSize( 13 );
867 $this->assertSame( 13, $rev->getSize() );
868 }
869
870 /**
871 * @covers Revision::getSize
872 */
873 public function testGetSize_failure() {
874 $title = $this->getMockTitle();
875
876 $rec = $this->getMockBuilder( RevisionRecord::class )
877 ->disableOriginalConstructor()
878 ->getMock();
879
880 $rec->method( 'getSize' )
881 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
882
883 $rev = new Revision( $rec, 0, $title );
884 $this->assertNull( $rev->getSize() );
885 }
886
887 /**
888 * @covers Revision::getSha1
889 */
890 public function testGetSha1() {
891 $title = $this->getMockTitle();
892
893 $rec = new MutableRevisionRecord( $title );
894 $rev = new Revision( $rec, 0, $title );
895
896 $emptyHash = SlotRecord::base36Sha1( '' );
897 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
898
899 $rec->setSha1( 'deadbeef' );
900 $this->assertSame( 'deadbeef', $rev->getSha1() );
901 }
902
903 /**
904 * @covers Revision::getSha1
905 */
906 public function testGetSha1_failure() {
907 $title = $this->getMockTitle();
908
909 $rec = $this->getMockBuilder( RevisionRecord::class )
910 ->disableOriginalConstructor()
911 ->getMock();
912
913 $rec->method( 'getSha1' )
914 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
915
916 $rev = new Revision( $rec, 0, $title );
917 $this->assertNull( $rev->getSha1() );
918 }
919
920 /**
921 * @covers Revision::getContent
922 */
923 public function testGetContent() {
924 $title = $this->getMockTitle();
925
926 $rec = new MutableRevisionRecord( $title );
927 $rev = new Revision( $rec, 0, $title );
928
929 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
930
931 $content = new TextContent( 'Hello Kittens!' );
932 $rec->setContent( SlotRecord::MAIN, $content );
933 $this->assertSame( $content, $rev->getContent() );
934 }
935
936 /**
937 * @covers Revision::getContent
938 */
939 public function testGetContent_failure() {
940 $title = $this->getMockTitle();
941
942 $rec = $this->getMockBuilder( RevisionRecord::class )
943 ->disableOriginalConstructor()
944 ->getMock();
945
946 $rec->method( 'getContent' )
947 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
948
949 $rev = new Revision( $rec, 0, $title );
950 $this->assertNull( $rev->getContent() );
951 }
952
953 }