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