[MCR] Introduce SlotRoleHandler and SlotRoleRegistry
[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 MediaWikiServices::getInstance()->getSlotRoleRegistry(),
478 MIGRATION_OLD,
479 MediaWikiServices::getInstance()->getActorMigration()
480 );
481 return $blobStore;
482 }
483
484 public function provideGetRevisionTextWithLegacyEncoding() {
485 yield 'Utf8Native' => [
486 "Wiki est l'\xc3\xa9cole superieur !",
487 'fr',
488 'iso-8859-1',
489 (object)[
490 'old_flags' => 'utf-8',
491 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
492 ]
493 ];
494 yield 'Utf8Legacy' => [
495 "Wiki est l'\xc3\xa9cole superieur !",
496 'fr',
497 'iso-8859-1',
498 (object)[
499 'old_flags' => '',
500 'old_text' => "Wiki est l'\xe9cole superieur !",
501 ]
502 ];
503 }
504
505 /**
506 * @covers Revision::getRevisionText
507 * @dataProvider provideGetRevisionTextWithLegacyEncoding
508 */
509 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
510 $blobStore = $this->getBlobStore();
511 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
512 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
513
514 $this->testGetRevisionText( $expected, $rowData );
515 }
516
517 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
518 /**
519 * WARNING!
520 * Do not set the external flag!
521 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
522 */
523 yield 'Utf8NativeGzip' => [
524 "Wiki est l'\xc3\xa9cole superieur !",
525 'fr',
526 'iso-8859-1',
527 (object)[
528 'old_flags' => 'gzip,utf-8',
529 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
530 ]
531 ];
532 yield 'Utf8LegacyGzip' => [
533 "Wiki est l'\xc3\xa9cole superieur !",
534 'fr',
535 'iso-8859-1',
536 (object)[
537 'old_flags' => 'gzip',
538 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
539 ]
540 ];
541 }
542
543 /**
544 * @covers Revision::getRevisionText
545 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
546 */
547 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
548 $this->checkPHPExtension( 'zlib' );
549
550 $blobStore = $this->getBlobStore();
551 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
552 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
553
554 $this->testGetRevisionText( $expected, $rowData );
555 }
556
557 /**
558 * @covers Revision::compressRevisionText
559 */
560 public function testCompressRevisionTextUtf8() {
561 $row = new stdClass;
562 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
563 $row->old_flags = Revision::compressRevisionText( $row->old_text );
564 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
565 "Flags should contain 'utf-8'" );
566 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
567 "Flags should not contain 'gzip'" );
568 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
569 $row->old_text, "Direct check" );
570 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
571 Revision::getRevisionText( $row ), "getRevisionText" );
572 }
573
574 /**
575 * @covers Revision::compressRevisionText
576 */
577 public function testCompressRevisionTextUtf8Gzip() {
578 $this->checkPHPExtension( 'zlib' );
579
580 $blobStore = $this->getBlobStore();
581 $blobStore->setCompressBlobs( true );
582 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
583
584 $row = new stdClass;
585 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
586 $row->old_flags = Revision::compressRevisionText( $row->old_text );
587 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
588 "Flags should contain 'utf-8'" );
589 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
590 "Flags should contain 'gzip'" );
591 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
592 gzinflate( $row->old_text ), "Direct check" );
593 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
594 Revision::getRevisionText( $row ), "getRevisionText" );
595 }
596
597 /**
598 * @covers Revision::loadFromTitle
599 */
600 public function testLoadFromTitle() {
601 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
602 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD );
603 $this->overrideMwServices();
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( [ 'revision', 'page', 'user' ] ),
639 // We don't really care about the fields are they come from the selectField methods
640 $this->isType( 'array' ),
641 $this->equalTo( $conditions ),
642 // Method name
643 $this->stringContains( 'fetchRevisionRowFromConds' ),
644 // We don't really care about the options here
645 $this->isType( 'array' ),
646 // We don't really care about the join conds are they come from the joinCond methods
647 $this->isType( 'array' )
648 )
649 ->willReturn( $row );
650
651 $revision = Revision::loadFromTitle( $db, $title );
652
653 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
654 $this->assertEquals( $row->rev_id, $revision->getId() );
655 $this->assertEquals( $row->rev_len, $revision->getSize() );
656 $this->assertEquals( $row->rev_sha1, $revision->getSha1() );
657 $this->assertEquals( $row->rev_parent_id, $revision->getParentId() );
658 $this->assertEquals( $row->rev_timestamp, $revision->getTimestamp() );
659 $this->assertEquals( $row->rev_comment_text, $revision->getComment() );
660 $this->assertEquals( $row->rev_user_text, $revision->getUserText() );
661 }
662
663 public function provideDecompressRevisionText() {
664 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
665 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
666 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
667 yield '(no legacy encoding), string in with gzip flag returns string' => [
668 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
669 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
670 ];
671 yield '(no legacy encoding), string in with object flag returns false' => [
672 // gzip string below generated with serialize( 'JOJO' )
673 false, "s:4:\"JOJO\";", [ 'object' ], false,
674 ];
675 yield '(no legacy encoding), serialized object in with object flag returns string' => [
676 false,
677 // Using a TitleValue object as it has a getText method (which is needed)
678 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
679 [ 'object' ],
680 'HHJJDDFF',
681 ];
682 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
683 false,
684 // Using a TitleValue object as it has a getText method (which is needed)
685 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
686 [ 'object', 'gzip' ],
687 '8219JJJ840',
688 ];
689 yield '(ISO-8859-1 encoding), string in string out' => [
690 'ISO-8859-1',
691 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
692 [],
693 '1®Àþ1',
694 ];
695 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
696 'ISO-8859-1',
697 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
698 [ 'gzip' ],
699 '4®Àþ4',
700 ];
701 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
702 'ISO-8859-1',
703 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
704 [ 'object' ],
705 '3®Àþ3',
706 ];
707 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
708 'ISO-8859-1',
709 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
710 [ 'gzip', 'object' ],
711 '2®Àþ2',
712 ];
713 }
714
715 /**
716 * @dataProvider provideDecompressRevisionText
717 * @covers Revision::decompressRevisionText
718 *
719 * @param bool $legacyEncoding
720 * @param mixed $text
721 * @param array $flags
722 * @param mixed $expected
723 */
724 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
725 $blobStore = $this->getBlobStore();
726 if ( $legacyEncoding ) {
727 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
728 }
729
730 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
731 $this->assertSame(
732 $expected,
733 Revision::decompressRevisionText( $text, $flags )
734 );
735 }
736
737 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
738 yield 'Just text' => [
739 (object)[ 'old_text' => 'SomeText' ],
740 'old_',
741 'SomeText'
742 ];
743 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
744 yield 'gzip text' => [
745 (object)[
746 'old_text' => "sttttr\002\022\000",
747 'old_flags' => 'gzip'
748 ],
749 'old_',
750 'AAAABBAAA'
751 ];
752 yield 'gzip text and different prefix' => [
753 (object)[
754 'jojo_text' => "sttttr\002\022\000",
755 'jojo_flags' => 'gzip'
756 ],
757 'jojo_',
758 'AAAABBAAA'
759 ];
760 }
761
762 /**
763 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
764 * @covers Revision::getRevisionText
765 */
766 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
767 $row,
768 $prefix,
769 $expected
770 ) {
771 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
772 }
773
774 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
775 yield 'Just some text' => [ 'someNonUrlText' ];
776 yield 'No second URL part' => [ 'someProtocol://' ];
777 }
778
779 /**
780 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
781 * @covers Revision::getRevisionText
782 */
783 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
784 $text
785 ) {
786 Wikimedia\suppressWarnings();
787 $this->assertFalse(
788 Revision::getRevisionText(
789 (object)[
790 'old_text' => $text,
791 'old_flags' => 'external',
792 ]
793 )
794 );
795 Wikimedia\suppressWarnings( true );
796 }
797
798 /**
799 * @covers Revision::getRevisionText
800 */
801 public function testGetRevisionText_external_noOldId() {
802 $this->setService(
803 'ExternalStoreFactory',
804 new ExternalStoreFactory( [ 'ForTesting' ] )
805 );
806 $this->assertSame(
807 'AAAABBAAA',
808 Revision::getRevisionText(
809 (object)[
810 'old_text' => 'ForTesting://cluster1/12345',
811 'old_flags' => 'external,gzip',
812 ]
813 )
814 );
815 }
816
817 /**
818 * @covers Revision::getRevisionText
819 */
820 public function testGetRevisionText_external_oldId() {
821 $cache = $this->getWANObjectCache();
822 $this->setService( 'MainWANObjectCache', $cache );
823
824 $this->setService(
825 'ExternalStoreFactory',
826 new ExternalStoreFactory( [ 'ForTesting' ] )
827 );
828
829 $lb = $this->getMockBuilder( LoadBalancer::class )
830 ->disableOriginalConstructor()
831 ->getMock();
832
833 $blobStore = new SqlBlobStore( $lb, $cache );
834 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
835
836 $this->assertSame(
837 'AAAABBAAA',
838 Revision::getRevisionText(
839 (object)[
840 'old_text' => 'ForTesting://cluster1/12345',
841 'old_flags' => 'external,gzip',
842 'old_id' => '7777',
843 ]
844 )
845 );
846
847 $cacheKey = $cache->makeGlobalKey(
848 'BlobStore',
849 'address',
850 $lb->getLocalDomainID(),
851 'tt:7777'
852 );
853 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
854 }
855
856 /**
857 * @covers Revision::getSize
858 */
859 public function testGetSize() {
860 $title = $this->getMockTitle();
861
862 $rec = new MutableRevisionRecord( $title );
863 $rev = new Revision( $rec, 0, $title );
864
865 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
866
867 $rec->setSize( 13 );
868 $this->assertSame( 13, $rev->getSize() );
869 }
870
871 /**
872 * @covers Revision::getSize
873 */
874 public function testGetSize_failure() {
875 $title = $this->getMockTitle();
876
877 $rec = $this->getMockBuilder( RevisionRecord::class )
878 ->disableOriginalConstructor()
879 ->getMock();
880
881 $rec->method( 'getSize' )
882 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
883
884 $rev = new Revision( $rec, 0, $title );
885 $this->assertNull( $rev->getSize() );
886 }
887
888 /**
889 * @covers Revision::getSha1
890 */
891 public function testGetSha1() {
892 $title = $this->getMockTitle();
893
894 $rec = new MutableRevisionRecord( $title );
895 $rev = new Revision( $rec, 0, $title );
896
897 $emptyHash = SlotRecord::base36Sha1( '' );
898 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
899
900 $rec->setSha1( 'deadbeef' );
901 $this->assertSame( 'deadbeef', $rev->getSha1() );
902 }
903
904 /**
905 * @covers Revision::getSha1
906 */
907 public function testGetSha1_failure() {
908 $title = $this->getMockTitle();
909
910 $rec = $this->getMockBuilder( RevisionRecord::class )
911 ->disableOriginalConstructor()
912 ->getMock();
913
914 $rec->method( 'getSha1' )
915 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
916
917 $rev = new Revision( $rec, 0, $title );
918 $this->assertNull( $rev->getSha1() );
919 }
920
921 /**
922 * @covers Revision::getContent
923 */
924 public function testGetContent() {
925 $title = $this->getMockTitle();
926
927 $rec = new MutableRevisionRecord( $title );
928 $rev = new Revision( $rec, 0, $title );
929
930 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
931
932 $content = new TextContent( 'Hello Kittens!' );
933 $rec->setContent( SlotRecord::MAIN, $content );
934 $this->assertSame( $content, $rev->getContent() );
935 }
936
937 /**
938 * @covers Revision::getContent
939 */
940 public function testGetContent_failure() {
941 $title = $this->getMockTitle();
942
943 $rec = $this->getMockBuilder( RevisionRecord::class )
944 ->disableOriginalConstructor()
945 ->getMock();
946
947 $rec->method( 'getContent' )
948 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
949
950 $rev = new Revision( $rec, 0, $title );
951 $this->assertNull( $rev->getContent() );
952 }
953
954 }