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
;
15 * Test cases in RevisionTest should not interact with the Database.
16 * For test cases that need Database interaction see RevisionDbTestBase.
18 class RevisionTest
extends MediaWikiTestCase
{
20 public function setUp() {
23 'wgMultiContentRevisionSchemaMigrationStage',
24 SCHEMA_COMPAT_WRITE_BOTH | SCHEMA_COMPAT_READ_NEW
28 public function provideConstructFromArray() {
29 yield
'with text' => [
31 'text' => 'hello world.',
32 'content_model' => CONTENT_MODEL_JAVASCRIPT
35 yield
'with content' => [
37 'content' => new JavaScriptContent( 'hellow world.' )
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
45 * @param string $model
48 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
49 $mock = $this->getMockBuilder( Title
::class )
50 ->disableOriginalConstructor()
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 ) );
72 * @dataProvider provideConstructFromArray
73 * @covers Revision::__construct
74 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
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() );
84 * @covers Revision::__construct
85 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
87 public function testConstructFromEmptyArray() {
88 $rev = new Revision( [], 0, $this->getMockTitle() );
89 $this->assertNull( $rev->getContent(), 'no content object should be available' );
93 * @covers Revision::__construct
94 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
96 public function testConstructFromArrayWithBadPageId() {
97 Wikimedia\
suppressWarnings();
98 $rev = new Revision( [ 'page' => 77777777 ] );
99 $this->assertSame( 77777777, $rev->getPage() );
100 Wikimedia\restoreWarnings
();
103 public function provideConstructFromArray_userSetAsExpected() {
104 yield
'no user defaults to wgUser' => [
106 'content' => new JavaScriptContent( 'hello world.' ),
111 yield
'user text and id' => [
113 'content' => new JavaScriptContent( 'hello world.' ),
114 'user_text' => 'SomeTextUserName',
121 yield
'user text only' => [
123 'content' => new JavaScriptContent( 'hello world.' ),
124 'user_text' => '111.111.111.111',
132 * @dataProvider provideConstructFromArray_userSetAsExpected
133 * @covers Revision::__construct
134 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
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
140 public function testConstructFromArray_userSetAsExpected(
145 $testUser = $this->getTestUser()->getUser();
146 $this->setMwGlobals( 'wgUser', $testUser );
147 if ( $expectedUserId === null ) {
148 $expectedUserId = $testUser->getId();
150 if ( $expectedUserName === null ) {
151 $expectedUserName = $testUser->getName();
154 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
155 $this->assertEquals( $expectedUserId, $rev->getUser() );
156 $this->assertEquals( $expectedUserName, $rev->getUserText() );
159 public function provideConstructFromArrayThrowsExceptions() {
160 yield
'content and text_id both not empty' => [
162 'content' => new WikitextContent( 'GOAT' ),
163 'text_id' => 'someid',
165 new MWException( 'The text_id field is only available in the pre-MCR schema' )
168 yield
'with bad content object (class)' => [
169 [ 'content' => new stdClass() ],
170 new MWException( 'content field must contain a Content object' )
172 yield
'with bad content object (string)' => [
173 [ 'content' => 'ImAGoat' ],
174 new MWException( 'content field must contain a Content object' )
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'
185 * @dataProvider provideConstructFromArrayThrowsExceptions
186 * @covers Revision::__construct
187 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
189 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
190 $this->setExpectedException(
191 get_class( $expectedException ),
192 $expectedException->getMessage(),
193 $expectedException->getCode()
195 new Revision( $rowArray, 0, $this->getMockTitle() );
199 * @covers Revision::__construct
200 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
202 public function testConstructFromNothing() {
203 $this->setExpectedException(
204 InvalidArgumentException
::class
209 public function provideConstructFromRow() {
210 yield
'Full construction' => [
214 'rev_timestamp' => '20171017114835',
215 'rev_user_text' => '127.0.0.1',
217 'rev_minor_edit' => '0',
218 'rev_deleted' => '0',
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,
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() );
240 yield
'default field values' => [
244 'rev_timestamp' => '20171017114835',
245 'rev_user_text' => '127.0.0.1',
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,
253 function ( RevisionTest
$testCase, Revision
$rev ) {
254 // parent ID may be null
255 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
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' );
269 * @dataProvider provideConstructFromRow
270 * @covers Revision::__construct
271 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
273 public function testConstructFromRow( array $arrayData, callable
$assertions ) {
274 $row = (object)$arrayData;
275 $rev = new Revision( $row, 0, $this->getMockTitle() );
276 $assertions( $this, $rev );
280 * @covers Revision::__construct
281 * @covers \MediaWiki\Revision\RevisionStore::newMutableRevisionFromArray
283 public function testConstructFromRowWithBadPageId() {
284 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_NEW
);
285 $this->overrideMwServices();
286 Wikimedia\
suppressWarnings();
287 $rev = new Revision( (object)[
288 'rev_page' => 77777777,
289 'rev_comment_text' => '',
290 'rev_comment_data' => null,
292 $this->assertSame( 77777777, $rev->getPage() );
293 Wikimedia\restoreWarnings
();
296 public function provideGetId() {
308 * @dataProvider provideGetId
309 * @covers Revision::getId
311 public function testGetId( $rowArray, $expectedId ) {
312 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
313 $this->assertEquals( $expectedId, $rev->getId() );
316 public function provideSetId() {
317 yield
[ '123', 123 ];
322 * @dataProvider provideSetId
323 * @covers Revision::setId
325 public function testSetId( $input, $expected ) {
326 $rev = new Revision( [], 0, $this->getMockTitle() );
327 $rev->setId( $input );
328 $this->assertSame( $expected, $rev->getId() );
331 public function provideSetUserIdAndName() {
332 yield
[ '123', 123, 'GOaT' ];
333 yield
[ 456, 456, 'GOaT' ];
337 * @dataProvider provideSetUserIdAndName
338 * @covers Revision::setUserIdAndName
340 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
341 $rev = new Revision( [], 0, $this->getMockTitle() );
342 $rev->setUserIdAndName( $inputId, $name );
343 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
344 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
347 public function provideGetParentId() {
349 yield
[ [ 'parent_id' => '123' ], 123 ];
350 yield
[ [ 'parent_id' => 456 ], 456 ];
354 * @dataProvider provideGetParentId
355 * @covers Revision::getParentId()
357 public function testGetParentId( $rowArray, $expected ) {
358 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
359 $this->assertSame( $expected, $rev->getParentId() );
362 public function provideGetRevisionText() {
363 yield
'Generic test' => [
364 'This is a goat of revision text.',
367 'old_text' => 'This is a goat of revision text.',
370 yield
'garbage in, garbage out' => [
377 * @covers Revision::getRevisionText
378 * @dataProvider provideGetRevisionText
380 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
383 Revision
::getRevisionText( $rowData, $prefix, $wiki ) );
386 public function provideGetRevisionTextWithZlibExtension() {
387 yield
'Generic gzip test' => [
388 'This is a small goat of revision text.',
390 'old_flags' => 'gzip',
391 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
397 * @covers Revision::getRevisionText
398 * @dataProvider provideGetRevisionTextWithZlibExtension
400 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
401 $this->checkPHPExtension( 'zlib' );
402 $this->testGetRevisionText( $expected, $rowData );
405 public function provideGetRevisionTextWithZlibExtension_badData() {
406 yield
'Generic gzip test' => [
407 'This is a small goat of revision text.',
409 'old_flags' => 'gzip',
410 'old_text' => 'DEAD BEEF',
416 * @covers Revision::getRevisionText
417 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
419 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
420 $this->checkPHPExtension( 'zlib' );
421 Wikimedia\
suppressWarnings();
423 Revision
::getRevisionText(
427 Wikimedia\
suppressWarnings( true );
430 private function getWANObjectCache() {
431 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
435 * @return SqlBlobStore
437 private function getBlobStore() {
438 /** @var LoadBalancer $lb */
439 $lb = $this->getMockBuilder( LoadBalancer
::class )
440 ->disableOriginalConstructor()
443 $cache = $this->getWANObjectCache();
445 $blobStore = new SqlBlobStore( $lb, $cache );
449 private function mockBlobStoreFactory( $blobStore ) {
450 /** @var LoadBalancer $lb */
451 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
452 ->disableOriginalConstructor()
454 $factory->expects( $this->any() )
455 ->method( 'newBlobStore' )
456 ->willReturn( $blobStore );
457 $factory->expects( $this->any() )
458 ->method( 'newSqlBlobStore' )
459 ->willReturn( $blobStore );
464 * @return RevisionStore
466 private function getRevisionStore() {
467 /** @var LoadBalancer $lb */
468 $lb = $this->getMockBuilder( LoadBalancer
::class )
469 ->disableOriginalConstructor()
472 $cache = $this->getWANObjectCache();
474 $blobStore = new RevisionStore(
476 $this->getBlobStore(),
478 MediaWikiServices
::getInstance()->getCommentStore(),
479 MediaWikiServices
::getInstance()->getContentModelStore(),
480 MediaWikiServices
::getInstance()->getSlotRoleStore(),
481 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
483 MediaWikiServices
::getInstance()->getActorMigration()
488 public function provideGetRevisionTextWithLegacyEncoding() {
489 yield
'Utf8Native' => [
490 "Wiki est l'\xc3\xa9cole superieur !",
494 'old_flags' => 'utf-8',
495 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
498 yield
'Utf8Legacy' => [
499 "Wiki est l'\xc3\xa9cole superieur !",
504 'old_text' => "Wiki est l'\xe9cole superieur !",
510 * @covers Revision::getRevisionText
511 * @dataProvider provideGetRevisionTextWithLegacyEncoding
513 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
514 $blobStore = $this->getBlobStore();
515 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
516 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
518 $this->testGetRevisionText( $expected, $rowData );
521 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
524 * Do not set the external flag!
525 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
527 yield
'Utf8NativeGzip' => [
528 "Wiki est l'\xc3\xa9cole superieur !",
532 'old_flags' => 'gzip,utf-8',
533 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
536 yield
'Utf8LegacyGzip' => [
537 "Wiki est l'\xc3\xa9cole superieur !",
541 'old_flags' => 'gzip',
542 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
548 * @covers Revision::getRevisionText
549 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
551 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
552 $this->checkPHPExtension( 'zlib' );
554 $blobStore = $this->getBlobStore();
555 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
556 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
558 $this->testGetRevisionText( $expected, $rowData );
562 * @covers Revision::compressRevisionText
564 public function testCompressRevisionTextUtf8() {
566 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
567 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
568 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
569 "Flags should contain 'utf-8'" );
570 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
571 "Flags should not contain 'gzip'" );
572 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
573 $row->old_text
, "Direct check" );
574 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
575 Revision
::getRevisionText( $row ), "getRevisionText" );
579 * @covers Revision::compressRevisionText
581 public function testCompressRevisionTextUtf8Gzip() {
582 $this->checkPHPExtension( 'zlib' );
584 $blobStore = $this->getBlobStore();
585 $blobStore->setCompressBlobs( true );
586 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
589 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
590 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
591 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
592 "Flags should contain 'utf-8'" );
593 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
594 "Flags should contain 'gzip'" );
595 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
596 gzinflate( $row->old_text
), "Direct check" );
597 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
598 Revision
::getRevisionText( $row ), "getRevisionText" );
602 * @covers Revision::loadFromTitle
604 public function testLoadFromTitle() {
605 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_NEW
);
606 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD
);
607 $this->overrideMwServices();
608 $title = $this->getMockTitle();
611 'rev_id=page_latest',
612 'page_namespace' => $title->getNamespace(),
613 'page_title' => $title->getDBkey()
618 'rev_page' => $title->getArticleID(),
619 'rev_timestamp' => '20171017114835',
620 'rev_user_text' => '127.0.0.1',
622 'rev_minor_edit' => '0',
623 'rev_deleted' => '0',
625 'rev_parent_id' => '1',
626 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
627 'rev_comment_text' => 'Goat Comment!',
628 'rev_comment_data' => null,
629 'rev_comment_cid' => null,
630 'rev_content_format' => 'GOATFORMAT',
631 'rev_content_model' => 'GOATMODEL',
634 $domain = MediaWikiServices
::getInstance()->getDBLoadBalancer()->getLocalDomainID();
635 $db = $this->getMock( IDatabase
::class );
636 $db->expects( $this->any() )
637 ->method( 'getDomainId' )
638 ->will( $this->returnValue( $domain ) );
639 $db->expects( $this->once() )
640 ->method( 'selectRow' )
643 'revision', 'page', 'user',
644 'temp_rev_comment' => 'revision_comment_temp', 'comment_rev_comment' => 'comment',
646 // We don't really care about the fields are they come from the selectField methods
647 $this->isType( 'array' ),
648 $this->equalTo( $conditions ),
650 $this->stringContains( 'fetchRevisionRowFromConds' ),
651 // We don't really care about the options here
652 $this->isType( 'array' ),
653 // We don't really care about the join conds are they come from the joinCond methods
654 $this->isType( 'array' )
656 ->willReturn( $row );
658 $revision = Revision
::loadFromTitle( $db, $title );
660 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
661 $this->assertEquals( $row->rev_id
, $revision->getId() );
662 $this->assertEquals( $row->rev_len
, $revision->getSize() );
663 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
664 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
665 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
666 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
667 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
670 public function provideDecompressRevisionText() {
671 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
672 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
673 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
674 yield
'(no legacy encoding), string in with gzip flag returns string' => [
675 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
676 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
678 yield
'(no legacy encoding), string in with object flag returns false' => [
679 // gzip string below generated with serialize( 'JOJO' )
680 false, "s:4:\"JOJO\";", [ 'object' ], false,
682 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
684 // Using a TitleValue object as it has a getText method (which is needed)
685 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
689 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
691 // Using a TitleValue object as it has a getText method (which is needed)
692 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
693 [ 'object', 'gzip' ],
696 yield
'(ISO-8859-1 encoding), string in string out' => [
698 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
702 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
704 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
708 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
710 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
714 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
716 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
717 [ 'gzip', 'object' ],
723 * @dataProvider provideDecompressRevisionText
724 * @covers Revision::decompressRevisionText
726 * @param bool $legacyEncoding
728 * @param array $flags
729 * @param mixed $expected
731 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
732 $blobStore = $this->getBlobStore();
733 if ( $legacyEncoding ) {
734 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
737 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
740 Revision
::decompressRevisionText( $text, $flags )
744 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
745 yield
'Just text' => [
746 (object)[ 'old_text' => 'SomeText' ],
750 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
751 yield
'gzip text' => [
753 'old_text' => "sttttr\002\022\000",
754 'old_flags' => 'gzip'
759 yield
'gzip text and different prefix' => [
761 'jojo_text' => "sttttr\002\022\000",
762 'jojo_flags' => 'gzip'
770 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
771 * @covers Revision::getRevisionText
773 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
778 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
781 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
782 yield
'Just some text' => [ 'someNonUrlText' ];
783 yield
'No second URL part' => [ 'someProtocol://' ];
787 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
788 * @covers Revision::getRevisionText
790 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
793 Wikimedia\
suppressWarnings();
795 Revision
::getRevisionText(
798 'old_flags' => 'external',
802 Wikimedia\
suppressWarnings( true );
806 * @covers Revision::getRevisionText
808 public function testGetRevisionText_external_noOldId() {
810 'ExternalStoreFactory',
811 new ExternalStoreFactory( [ 'ForTesting' ] )
815 Revision
::getRevisionText(
817 'old_text' => 'ForTesting://cluster1/12345',
818 'old_flags' => 'external,gzip',
825 * @covers Revision::getRevisionText
827 public function testGetRevisionText_external_oldId() {
828 $cache = $this->getWANObjectCache();
829 $this->setService( 'MainWANObjectCache', $cache );
832 'ExternalStoreFactory',
833 new ExternalStoreFactory( [ 'ForTesting' ] )
836 $lb = $this->getMockBuilder( LoadBalancer
::class )
837 ->disableOriginalConstructor()
840 $blobStore = new SqlBlobStore( $lb, $cache );
841 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
845 Revision
::getRevisionText(
847 'old_text' => 'ForTesting://cluster1/12345',
848 'old_flags' => 'external,gzip',
854 $cacheKey = $cache->makeGlobalKey(
857 $lb->getLocalDomainID(),
860 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
864 * @covers Revision::getSize
866 public function testGetSize() {
867 $title = $this->getMockTitle();
869 $rec = new MutableRevisionRecord( $title );
870 $rev = new Revision( $rec, 0, $title );
872 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
875 $this->assertSame( 13, $rev->getSize() );
879 * @covers Revision::getSize
881 public function testGetSize_failure() {
882 $title = $this->getMockTitle();
884 $rec = $this->getMockBuilder( RevisionRecord
::class )
885 ->disableOriginalConstructor()
888 $rec->method( 'getSize' )
889 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
891 $rev = new Revision( $rec, 0, $title );
892 $this->assertNull( $rev->getSize() );
896 * @covers Revision::getSha1
898 public function testGetSha1() {
899 $title = $this->getMockTitle();
901 $rec = new MutableRevisionRecord( $title );
902 $rev = new Revision( $rec, 0, $title );
904 $emptyHash = SlotRecord
::base36Sha1( '' );
905 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
907 $rec->setSha1( 'deadbeef' );
908 $this->assertSame( 'deadbeef', $rev->getSha1() );
912 * @covers Revision::getSha1
914 public function testGetSha1_failure() {
915 $title = $this->getMockTitle();
917 $rec = $this->getMockBuilder( RevisionRecord
::class )
918 ->disableOriginalConstructor()
921 $rec->method( 'getSha1' )
922 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
924 $rev = new Revision( $rec, 0, $title );
925 $this->assertNull( $rev->getSha1() );
929 * @covers Revision::getContent
931 public function testGetContent() {
932 $title = $this->getMockTitle();
934 $rec = new MutableRevisionRecord( $title );
935 $rev = new Revision( $rec, 0, $title );
937 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
939 $content = new TextContent( 'Hello Kittens!' );
940 $rec->setContent( SlotRecord
::MAIN
, $content );
941 $this->assertSame( $content, $rev->getContent() );
945 * @covers Revision::getContent
947 public function testGetContent_failure() {
948 $title = $this->getMockTitle();
950 $rec = $this->getMockBuilder( RevisionRecord
::class )
951 ->disableOriginalConstructor()
954 $rec->method( 'getContent' )
955 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
957 $rev = new Revision( $rec, 0, $title );
958 $this->assertNull( $rev->getContent() );