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(),
478 MediaWikiServices
::getInstance()->getActorMigration()
483 public function provideGetRevisionTextWithLegacyEncoding() {
484 yield
'Utf8Native' => [
485 "Wiki est l'\xc3\xa9cole superieur !",
489 'old_flags' => 'utf-8',
490 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
493 yield
'Utf8Legacy' => [
494 "Wiki est l'\xc3\xa9cole superieur !",
499 'old_text' => "Wiki est l'\xe9cole superieur !",
505 * @covers Revision::getRevisionText
506 * @dataProvider provideGetRevisionTextWithLegacyEncoding
508 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
509 $blobStore = $this->getBlobStore();
510 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
511 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
513 $this->testGetRevisionText( $expected, $rowData );
516 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
519 * Do not set the external flag!
520 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
522 yield
'Utf8NativeGzip' => [
523 "Wiki est l'\xc3\xa9cole superieur !",
527 'old_flags' => 'gzip,utf-8',
528 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
531 yield
'Utf8LegacyGzip' => [
532 "Wiki est l'\xc3\xa9cole superieur !",
536 'old_flags' => 'gzip',
537 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
543 * @covers Revision::getRevisionText
544 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
546 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
547 $this->checkPHPExtension( 'zlib' );
549 $blobStore = $this->getBlobStore();
550 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
551 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
553 $this->testGetRevisionText( $expected, $rowData );
557 * @covers Revision::compressRevisionText
559 public function testCompressRevisionTextUtf8() {
561 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
562 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
563 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
564 "Flags should contain 'utf-8'" );
565 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
566 "Flags should not contain 'gzip'" );
567 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
568 $row->old_text
, "Direct check" );
569 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
570 Revision
::getRevisionText( $row ), "getRevisionText" );
574 * @covers Revision::compressRevisionText
576 public function testCompressRevisionTextUtf8Gzip() {
577 $this->checkPHPExtension( 'zlib' );
579 $blobStore = $this->getBlobStore();
580 $blobStore->setCompressBlobs( true );
581 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
584 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
585 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
586 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
587 "Flags should contain 'utf-8'" );
588 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
589 "Flags should contain 'gzip'" );
590 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
591 gzinflate( $row->old_text
), "Direct check" );
592 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
593 Revision
::getRevisionText( $row ), "getRevisionText" );
597 * @covers Revision::loadFromTitle
599 public function testLoadFromTitle() {
600 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
601 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', SCHEMA_COMPAT_OLD
);
602 $this->overrideMwServices();
603 $title = $this->getMockTitle();
606 'rev_id=page_latest',
607 'page_namespace' => $title->getNamespace(),
608 'page_title' => $title->getDBkey()
613 'rev_page' => $title->getArticleID(),
614 'rev_timestamp' => '20171017114835',
615 'rev_user_text' => '127.0.0.1',
617 'rev_minor_edit' => '0',
618 'rev_deleted' => '0',
620 'rev_parent_id' => '1',
621 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
622 'rev_comment_text' => 'Goat Comment!',
623 'rev_comment_data' => null,
624 'rev_comment_cid' => null,
625 'rev_content_format' => 'GOATFORMAT',
626 'rev_content_model' => 'GOATMODEL',
629 $domain = MediaWikiServices
::getInstance()->getDBLoadBalancer()->getLocalDomainID();
630 $db = $this->getMock( IDatabase
::class );
631 $db->expects( $this->any() )
632 ->method( 'getDomainId' )
633 ->will( $this->returnValue( $domain ) );
634 $db->expects( $this->once() )
635 ->method( 'selectRow' )
637 $this->equalTo( [ 'revision', 'page', 'user' ] ),
638 // We don't really care about the fields are they come from the selectField methods
639 $this->isType( 'array' ),
640 $this->equalTo( $conditions ),
642 $this->stringContains( 'fetchRevisionRowFromConds' ),
643 // We don't really care about the options here
644 $this->isType( 'array' ),
645 // We don't really care about the join conds are they come from the joinCond methods
646 $this->isType( 'array' )
648 ->willReturn( $row );
650 $revision = Revision
::loadFromTitle( $db, $title );
652 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
653 $this->assertEquals( $row->rev_id
, $revision->getId() );
654 $this->assertEquals( $row->rev_len
, $revision->getSize() );
655 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
656 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
657 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
658 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
659 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
662 public function provideDecompressRevisionText() {
663 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
664 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
665 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
666 yield
'(no legacy encoding), string in with gzip flag returns string' => [
667 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
668 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
670 yield
'(no legacy encoding), string in with object flag returns false' => [
671 // gzip string below generated with serialize( 'JOJO' )
672 false, "s:4:\"JOJO\";", [ 'object' ], false,
674 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
676 // Using a TitleValue object as it has a getText method (which is needed)
677 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
681 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
683 // Using a TitleValue object as it has a getText method (which is needed)
684 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
685 [ 'object', 'gzip' ],
688 yield
'(ISO-8859-1 encoding), string in string out' => [
690 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
694 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
696 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
700 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
702 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
706 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
708 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
709 [ 'gzip', 'object' ],
715 * @dataProvider provideDecompressRevisionText
716 * @covers Revision::decompressRevisionText
718 * @param bool $legacyEncoding
720 * @param array $flags
721 * @param mixed $expected
723 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
724 $blobStore = $this->getBlobStore();
725 if ( $legacyEncoding ) {
726 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
729 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
732 Revision
::decompressRevisionText( $text, $flags )
736 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
737 yield
'Just text' => [
738 (object)[ 'old_text' => 'SomeText' ],
742 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
743 yield
'gzip text' => [
745 'old_text' => "sttttr\002\022\000",
746 'old_flags' => 'gzip'
751 yield
'gzip text and different prefix' => [
753 'jojo_text' => "sttttr\002\022\000",
754 'jojo_flags' => 'gzip'
762 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
763 * @covers Revision::getRevisionText
765 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
770 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
773 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
774 yield
'Just some text' => [ 'someNonUrlText' ];
775 yield
'No second URL part' => [ 'someProtocol://' ];
779 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
780 * @covers Revision::getRevisionText
782 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
785 Wikimedia\
suppressWarnings();
787 Revision
::getRevisionText(
790 'old_flags' => 'external',
794 Wikimedia\
suppressWarnings( true );
798 * @covers Revision::getRevisionText
800 public function testGetRevisionText_external_noOldId() {
802 'ExternalStoreFactory',
803 new ExternalStoreFactory( [ 'ForTesting' ] )
807 Revision
::getRevisionText(
809 'old_text' => 'ForTesting://cluster1/12345',
810 'old_flags' => 'external,gzip',
817 * @covers Revision::getRevisionText
819 public function testGetRevisionText_external_oldId() {
820 $cache = $this->getWANObjectCache();
821 $this->setService( 'MainWANObjectCache', $cache );
824 'ExternalStoreFactory',
825 new ExternalStoreFactory( [ 'ForTesting' ] )
828 $lb = $this->getMockBuilder( LoadBalancer
::class )
829 ->disableOriginalConstructor()
832 $blobStore = new SqlBlobStore( $lb, $cache );
833 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
837 Revision
::getRevisionText(
839 'old_text' => 'ForTesting://cluster1/12345',
840 'old_flags' => 'external,gzip',
846 $cacheKey = $cache->makeGlobalKey(
849 $lb->getLocalDomainID(),
852 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
856 * @covers Revision::getSize
858 public function testGetSize() {
859 $title = $this->getMockTitle();
861 $rec = new MutableRevisionRecord( $title );
862 $rev = new Revision( $rec, 0, $title );
864 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
867 $this->assertSame( 13, $rev->getSize() );
871 * @covers Revision::getSize
873 public function testGetSize_failure() {
874 $title = $this->getMockTitle();
876 $rec = $this->getMockBuilder( RevisionRecord
::class )
877 ->disableOriginalConstructor()
880 $rec->method( 'getSize' )
881 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
883 $rev = new Revision( $rec, 0, $title );
884 $this->assertNull( $rev->getSize() );
888 * @covers Revision::getSha1
890 public function testGetSha1() {
891 $title = $this->getMockTitle();
893 $rec = new MutableRevisionRecord( $title );
894 $rev = new Revision( $rec, 0, $title );
896 $emptyHash = SlotRecord
::base36Sha1( '' );
897 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
899 $rec->setSha1( 'deadbeef' );
900 $this->assertSame( 'deadbeef', $rev->getSha1() );
904 * @covers Revision::getSha1
906 public function testGetSha1_failure() {
907 $title = $this->getMockTitle();
909 $rec = $this->getMockBuilder( RevisionRecord
::class )
910 ->disableOriginalConstructor()
913 $rec->method( 'getSha1' )
914 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
916 $rev = new Revision( $rec, 0, $title );
917 $this->assertNull( $rev->getSha1() );
921 * @covers Revision::getContent
923 public function testGetContent() {
924 $title = $this->getMockTitle();
926 $rec = new MutableRevisionRecord( $title );
927 $rev = new Revision( $rec, 0, $title );
929 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
931 $content = new TextContent( 'Hello Kittens!' );
932 $rec->setContent( SlotRecord
::MAIN
, $content );
933 $this->assertSame( $content, $rev->getContent() );
937 * @covers Revision::getContent
939 public function testGetContent_failure() {
940 $title = $this->getMockTitle();
942 $rec = $this->getMockBuilder( RevisionRecord
::class )
943 ->disableOriginalConstructor()
946 $rec->method( 'getContent' )
947 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
949 $rev = new Revision( $rec, 0, $title );
950 $this->assertNull( $rev->getContent() );