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_OLD
);
285 $this->overrideMwServices();
286 Wikimedia\
suppressWarnings();
287 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
288 $this->assertSame( 77777777, $rev->getPage() );
289 Wikimedia\restoreWarnings
();
292 public function provideGetId() {
304 * @dataProvider provideGetId
305 * @covers Revision::getId
307 public function testGetId( $rowArray, $expectedId ) {
308 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
309 $this->assertEquals( $expectedId, $rev->getId() );
312 public function provideSetId() {
313 yield
[ '123', 123 ];
318 * @dataProvider provideSetId
319 * @covers Revision::setId
321 public function testSetId( $input, $expected ) {
322 $rev = new Revision( [], 0, $this->getMockTitle() );
323 $rev->setId( $input );
324 $this->assertSame( $expected, $rev->getId() );
327 public function provideSetUserIdAndName() {
328 yield
[ '123', 123, 'GOaT' ];
329 yield
[ 456, 456, 'GOaT' ];
333 * @dataProvider provideSetUserIdAndName
334 * @covers Revision::setUserIdAndName
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
) );
343 public function provideGetParentId() {
345 yield
[ [ 'parent_id' => '123' ], 123 ];
346 yield
[ [ 'parent_id' => 456 ], 456 ];
350 * @dataProvider provideGetParentId
351 * @covers Revision::getParentId()
353 public function testGetParentId( $rowArray, $expected ) {
354 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
355 $this->assertSame( $expected, $rev->getParentId() );
358 public function provideGetRevisionText() {
359 yield
'Generic test' => [
360 'This is a goat of revision text.',
363 'old_text' => 'This is a goat of revision text.',
366 yield
'garbage in, garbage out' => [
373 * @covers Revision::getRevisionText
374 * @dataProvider provideGetRevisionText
376 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
379 Revision
::getRevisionText( $rowData, $prefix, $wiki ) );
382 public function provideGetRevisionTextWithZlibExtension() {
383 yield
'Generic gzip test' => [
384 'This is a small goat of revision text.',
386 'old_flags' => 'gzip',
387 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
393 * @covers Revision::getRevisionText
394 * @dataProvider provideGetRevisionTextWithZlibExtension
396 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
397 $this->checkPHPExtension( 'zlib' );
398 $this->testGetRevisionText( $expected, $rowData );
401 public function provideGetRevisionTextWithZlibExtension_badData() {
402 yield
'Generic gzip test' => [
403 'This is a small goat of revision text.',
405 'old_flags' => 'gzip',
406 'old_text' => 'DEAD BEEF',
412 * @covers Revision::getRevisionText
413 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
415 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
416 $this->checkPHPExtension( 'zlib' );
417 Wikimedia\
suppressWarnings();
419 Revision
::getRevisionText(
423 Wikimedia\
suppressWarnings( true );
426 private function getWANObjectCache() {
427 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
431 * @return SqlBlobStore
433 private function getBlobStore() {
434 /** @var LoadBalancer $lb */
435 $lb = $this->getMockBuilder( LoadBalancer
::class )
436 ->disableOriginalConstructor()
439 $cache = $this->getWANObjectCache();
441 $blobStore = new SqlBlobStore( $lb, $cache );
445 private function mockBlobStoreFactory( $blobStore ) {
446 /** @var LoadBalancer $lb */
447 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
448 ->disableOriginalConstructor()
450 $factory->expects( $this->any() )
451 ->method( 'newBlobStore' )
452 ->willReturn( $blobStore );
453 $factory->expects( $this->any() )
454 ->method( 'newSqlBlobStore' )
455 ->willReturn( $blobStore );
460 * @return RevisionStore
462 private function getRevisionStore() {
463 /** @var LoadBalancer $lb */
464 $lb = $this->getMockBuilder( LoadBalancer
::class )
465 ->disableOriginalConstructor()
468 $cache = $this->getWANObjectCache();
470 $blobStore = new RevisionStore(
472 $this->getBlobStore(),
474 MediaWikiServices
::getInstance()->getCommentStore(),
475 MediaWikiServices
::getInstance()->getContentModelStore(),
476 MediaWikiServices
::getInstance()->getSlotRoleStore(),
477 MediaWikiServices
::getInstance()->getSlotRoleRegistry(),
479 MediaWikiServices
::getInstance()->getActorMigration()
484 public function provideGetRevisionTextWithLegacyEncoding() {
485 yield
'Utf8Native' => [
486 "Wiki est l'\xc3\xa9cole superieur !",
490 'old_flags' => 'utf-8',
491 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
494 yield
'Utf8Legacy' => [
495 "Wiki est l'\xc3\xa9cole superieur !",
500 'old_text' => "Wiki est l'\xe9cole superieur !",
506 * @covers Revision::getRevisionText
507 * @dataProvider provideGetRevisionTextWithLegacyEncoding
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 ) );
514 $this->testGetRevisionText( $expected, $rowData );
517 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
520 * Do not set the external flag!
521 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
523 yield
'Utf8NativeGzip' => [
524 "Wiki est l'\xc3\xa9cole superieur !",
528 'old_flags' => 'gzip,utf-8',
529 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
532 yield
'Utf8LegacyGzip' => [
533 "Wiki est l'\xc3\xa9cole superieur !",
537 'old_flags' => 'gzip',
538 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
544 * @covers Revision::getRevisionText
545 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
547 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
548 $this->checkPHPExtension( 'zlib' );
550 $blobStore = $this->getBlobStore();
551 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
552 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
554 $this->testGetRevisionText( $expected, $rowData );
558 * @covers Revision::compressRevisionText
560 public function testCompressRevisionTextUtf8() {
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" );
575 * @covers Revision::compressRevisionText
577 public function testCompressRevisionTextUtf8Gzip() {
578 $this->checkPHPExtension( 'zlib' );
580 $blobStore = $this->getBlobStore();
581 $blobStore->setCompressBlobs( true );
582 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
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" );
598 * @covers Revision::loadFromTitle
600 public function testLoadFromTitle() {
601 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
602 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD
);
603 $this->overrideMwServices();
604 $title = $this->getMockTitle();
607 'rev_id=page_latest',
608 'page_namespace' => $title->getNamespace(),
609 'page_title' => $title->getDBkey()
614 'rev_page' => $title->getArticleID(),
615 'rev_timestamp' => '20171017114835',
616 'rev_user_text' => '127.0.0.1',
618 'rev_minor_edit' => '0',
619 'rev_deleted' => '0',
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',
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' )
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 ),
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' )
649 ->willReturn( $row );
651 $revision = Revision
::loadFromTitle( $db, $title );
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() );
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',
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,
675 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
677 // Using a TitleValue object as it has a getText method (which is needed)
678 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
682 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
684 // Using a TitleValue object as it has a getText method (which is needed)
685 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
686 [ 'object', 'gzip' ],
689 yield
'(ISO-8859-1 encoding), string in string out' => [
691 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
695 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
697 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
701 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
703 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
707 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
709 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
710 [ 'gzip', 'object' ],
716 * @dataProvider provideDecompressRevisionText
717 * @covers Revision::decompressRevisionText
719 * @param bool $legacyEncoding
721 * @param array $flags
722 * @param mixed $expected
724 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
725 $blobStore = $this->getBlobStore();
726 if ( $legacyEncoding ) {
727 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
730 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
733 Revision
::decompressRevisionText( $text, $flags )
737 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
738 yield
'Just text' => [
739 (object)[ 'old_text' => 'SomeText' ],
743 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
744 yield
'gzip text' => [
746 'old_text' => "sttttr\002\022\000",
747 'old_flags' => 'gzip'
752 yield
'gzip text and different prefix' => [
754 'jojo_text' => "sttttr\002\022\000",
755 'jojo_flags' => 'gzip'
763 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
764 * @covers Revision::getRevisionText
766 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
771 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
774 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
775 yield
'Just some text' => [ 'someNonUrlText' ];
776 yield
'No second URL part' => [ 'someProtocol://' ];
780 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
781 * @covers Revision::getRevisionText
783 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
786 Wikimedia\
suppressWarnings();
788 Revision
::getRevisionText(
791 'old_flags' => 'external',
795 Wikimedia\
suppressWarnings( true );
799 * @covers Revision::getRevisionText
801 public function testGetRevisionText_external_noOldId() {
803 'ExternalStoreFactory',
804 new ExternalStoreFactory( [ 'ForTesting' ] )
808 Revision
::getRevisionText(
810 'old_text' => 'ForTesting://cluster1/12345',
811 'old_flags' => 'external,gzip',
818 * @covers Revision::getRevisionText
820 public function testGetRevisionText_external_oldId() {
821 $cache = $this->getWANObjectCache();
822 $this->setService( 'MainWANObjectCache', $cache );
825 'ExternalStoreFactory',
826 new ExternalStoreFactory( [ 'ForTesting' ] )
829 $lb = $this->getMockBuilder( LoadBalancer
::class )
830 ->disableOriginalConstructor()
833 $blobStore = new SqlBlobStore( $lb, $cache );
834 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
838 Revision
::getRevisionText(
840 'old_text' => 'ForTesting://cluster1/12345',
841 'old_flags' => 'external,gzip',
847 $cacheKey = $cache->makeGlobalKey(
850 $lb->getLocalDomainID(),
853 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
857 * @covers Revision::getSize
859 public function testGetSize() {
860 $title = $this->getMockTitle();
862 $rec = new MutableRevisionRecord( $title );
863 $rev = new Revision( $rec, 0, $title );
865 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
868 $this->assertSame( 13, $rev->getSize() );
872 * @covers Revision::getSize
874 public function testGetSize_failure() {
875 $title = $this->getMockTitle();
877 $rec = $this->getMockBuilder( RevisionRecord
::class )
878 ->disableOriginalConstructor()
881 $rec->method( 'getSize' )
882 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
884 $rev = new Revision( $rec, 0, $title );
885 $this->assertNull( $rev->getSize() );
889 * @covers Revision::getSha1
891 public function testGetSha1() {
892 $title = $this->getMockTitle();
894 $rec = new MutableRevisionRecord( $title );
895 $rev = new Revision( $rec, 0, $title );
897 $emptyHash = SlotRecord
::base36Sha1( '' );
898 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
900 $rec->setSha1( 'deadbeef' );
901 $this->assertSame( 'deadbeef', $rev->getSha1() );
905 * @covers Revision::getSha1
907 public function testGetSha1_failure() {
908 $title = $this->getMockTitle();
910 $rec = $this->getMockBuilder( RevisionRecord
::class )
911 ->disableOriginalConstructor()
914 $rec->method( 'getSha1' )
915 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
917 $rev = new Revision( $rec, 0, $title );
918 $this->assertNull( $rev->getSha1() );
922 * @covers Revision::getContent
924 public function testGetContent() {
925 $title = $this->getMockTitle();
927 $rec = new MutableRevisionRecord( $title );
928 $rev = new Revision( $rec, 0, $title );
930 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
932 $content = new TextContent( 'Hello Kittens!' );
933 $rec->setContent( SlotRecord
::MAIN
, $content );
934 $this->assertSame( $content, $rev->getContent() );
938 * @covers Revision::getContent
940 public function testGetContent_failure() {
941 $title = $this->getMockTitle();
943 $rec = $this->getMockBuilder( RevisionRecord
::class )
944 ->disableOriginalConstructor()
947 $rec->method( 'getContent' )
948 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
950 $rev = new Revision( $rec, 0, $title );
951 $this->assertNull( $rev->getContent() );