3 use MediaWiki\Storage\BlobStoreFactory
;
4 use MediaWiki\Storage\MutableRevisionRecord
;
5 use MediaWiki\Storage\RevisionAccessException
;
6 use MediaWiki\Storage\RevisionRecord
;
7 use MediaWiki\Storage\RevisionStore
;
8 use MediaWiki\Storage\SlotRecord
;
9 use MediaWiki\Storage\SqlBlobStore
;
10 use Wikimedia\Rdbms\IDatabase
;
11 use Wikimedia\Rdbms\LoadBalancer
;
14 * Test cases in RevisionTest should not interact with the Database.
15 * For test cases that need Database interaction see RevisionDbTestBase.
17 class RevisionTest
extends MediaWikiTestCase
{
19 public function provideConstructFromArray() {
20 yield
'with text' => [
22 'text' => 'hello world.',
23 'content_model' => CONTENT_MODEL_JAVASCRIPT
26 yield
'with content' => [
28 'content' => new JavaScriptContent( 'hellow world.' )
31 // FIXME: test with and without user ID, and with a user object.
32 // We can't prepare that here though, since we don't yet have a dummy DB
36 * @param string $model
39 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
40 $mock = $this->getMockBuilder( Title
::class )
41 ->disableOriginalConstructor()
43 $mock->expects( $this->any() )
44 ->method( 'getNamespace' )
45 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
46 $mock->expects( $this->any() )
47 ->method( 'getPrefixedText' )
48 ->will( $this->returnValue( 'RevisionTest' ) );
49 $mock->expects( $this->any() )
50 ->method( 'getDBkey' )
51 ->will( $this->returnValue( 'RevisionTest' ) );
52 $mock->expects( $this->any() )
53 ->method( 'getArticleID' )
54 ->will( $this->returnValue( 23 ) );
55 $mock->expects( $this->any() )
56 ->method( 'getModel' )
57 ->will( $this->returnValue( $model ) );
63 * @dataProvider provideConstructFromArray
64 * @covers Revision::__construct
65 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
67 public function testConstructFromArray( $rowArray ) {
68 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
69 $this->assertNotNull( $rev->getContent(), 'no content object available' );
70 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
71 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
75 * @covers Revision::__construct
76 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
78 public function testConstructFromEmptyArray() {
79 $rev = new Revision( [], 0, $this->getMockTitle() );
80 $this->assertNull( $rev->getContent(), 'no content object should be available' );
84 * @covers Revision::__construct
85 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
87 public function testConstructFromArrayWithBadPageId() {
88 MediaWiki\
suppressWarnings();
89 $rev = new Revision( [ 'page' => 77777777 ] );
90 $this->assertSame( 77777777, $rev->getPage() );
91 MediaWiki\restoreWarnings
();
94 public function provideConstructFromArray_userSetAsExpected() {
95 yield
'no user defaults to wgUser' => [
97 'content' => new JavaScriptContent( 'hello world.' ),
102 yield
'user text and id' => [
104 'content' => new JavaScriptContent( 'hello world.' ),
105 'user_text' => 'SomeTextUserName',
112 yield
'user text only' => [
114 'content' => new JavaScriptContent( 'hello world.' ),
115 'user_text' => '111.111.111.111',
123 * @dataProvider provideConstructFromArray_userSetAsExpected
124 * @covers Revision::__construct
125 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
127 * @param array $rowArray
128 * @param mixed $expectedUserId null to expect the current wgUser ID
129 * @param mixed $expectedUserName null to expect the current wgUser name
131 public function testConstructFromArray_userSetAsExpected(
136 $testUser = $this->getTestUser()->getUser();
137 $this->setMwGlobals( 'wgUser', $testUser );
138 if ( $expectedUserId === null ) {
139 $expectedUserId = $testUser->getId();
141 if ( $expectedUserName === null ) {
142 $expectedUserName = $testUser->getName();
145 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
146 $this->assertEquals( $expectedUserId, $rev->getUser() );
147 $this->assertEquals( $expectedUserName, $rev->getUserText() );
150 public function provideConstructFromArrayThrowsExceptions() {
151 yield
'content and text_id both not empty' => [
153 'content' => new WikitextContent( 'GOAT' ),
154 'text_id' => 'someid',
156 new MWException( "Text already stored in external store (id someid), " .
157 "can't serialize content object" )
159 yield
'unknown user id and no user name' => [
161 'content' => new JavaScriptContent( 'hello world.' ),
164 new MWException( 'user_text not given, and unknown user ID 9989' )
166 yield
'with bad content object (class)' => [
167 [ 'content' => new stdClass() ],
168 new MWException( 'content field must contain a Content object.' )
170 yield
'with bad content object (string)' => [
171 [ 'content' => 'ImAGoat' ],
172 new MWException( 'content field must contain a Content object.' )
174 yield
'bad row format' => [
175 'imastring, not a row',
176 new InvalidArgumentException(
177 '$row must be a row object, an associative array, or a RevisionRecord'
183 * @dataProvider provideConstructFromArrayThrowsExceptions
184 * @covers Revision::__construct
185 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
187 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
188 $this->setExpectedException(
189 get_class( $expectedException ),
190 $expectedException->getMessage(),
191 $expectedException->getCode()
193 new Revision( $rowArray, 0, $this->getMockTitle() );
197 * @covers Revision::__construct
198 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
200 public function testConstructFromNothing() {
201 $this->setExpectedException(
202 InvalidArgumentException
::class
207 public function provideConstructFromRow() {
208 yield
'Full construction' => [
212 'rev_text_id' => '2',
213 'rev_timestamp' => '20171017114835',
214 'rev_user_text' => '127.0.0.1',
216 'rev_minor_edit' => '0',
217 'rev_deleted' => '0',
219 'rev_parent_id' => '1',
220 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
221 'rev_comment_text' => 'Goat Comment!',
222 'rev_comment_data' => null,
223 'rev_comment_cid' => null,
224 'rev_content_format' => 'GOATFORMAT',
225 'rev_content_model' => 'GOATMODEL',
227 function ( RevisionTest
$testCase, Revision
$rev ) {
228 $testCase->assertSame( 42, $rev->getId() );
229 $testCase->assertSame( 23, $rev->getPage() );
230 $testCase->assertSame( 2, $rev->getTextId() );
231 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
232 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
233 $testCase->assertSame( 0, $rev->getUser() );
234 $testCase->assertSame( false, $rev->isMinor() );
235 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
236 $testCase->assertSame( 46, $rev->getSize() );
237 $testCase->assertSame( 1, $rev->getParentId() );
238 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
239 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
240 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
241 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
244 yield
'default field values' => [
248 'rev_text_id' => '2',
249 'rev_timestamp' => '20171017114835',
250 'rev_user_text' => '127.0.0.1',
252 'rev_minor_edit' => '0',
253 'rev_deleted' => '0',
254 'rev_comment_text' => 'Goat Comment!',
255 'rev_comment_data' => null,
256 'rev_comment_cid' => null,
258 function ( RevisionTest
$testCase, Revision
$rev ) {
259 // parent ID may be null
260 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
263 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
264 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
265 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
266 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
267 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
268 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
271 $testCase->assertNotNull( $rev->getSize(), 'size' );
272 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
274 // NOTE: model and format will be detected based on the namespace of the (mock) title
275 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
276 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
282 * @dataProvider provideConstructFromRow
283 * @covers Revision::__construct
284 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
286 public function testConstructFromRow( array $arrayData, $assertions ) {
287 $data = 'Hello goat.'; // needs to match model and format
289 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
290 ->disableOriginalConstructor()
293 $blobStore->method( 'getBlob' )
294 ->will( $this->returnValue( $data ) );
296 $blobStore->method( 'getTextIdFromAddress' )
297 ->will( $this->returnCallback(
298 function ( $address ) {
299 // Turn "tt:1234" into 12345.
300 // Note that this must be functional so we can test getTextId().
301 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
302 $parts = explode( ':', $address );
303 return (int)array_pop( $parts );
307 // Note override internal service, so RevisionStore uses it as well.
308 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
310 $row = (object)$arrayData;
311 $rev = new Revision( $row, 0, $this->getMockTitle() );
312 $assertions( $this, $rev );
316 * @covers Revision::__construct
317 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
319 public function testConstructFromRowWithBadPageId() {
320 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
321 $this->overrideMwServices();
322 MediaWiki\
suppressWarnings();
323 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
324 $this->assertSame( 77777777, $rev->getPage() );
325 MediaWiki\restoreWarnings
();
328 public function provideGetRevisionText() {
329 yield
'Generic test' => [
330 'This is a goat of revision text.',
333 'old_text' => 'This is a goat of revision text.',
338 public function provideGetId() {
350 * @dataProvider provideGetId
351 * @covers Revision::getId
353 public function testGetId( $rowArray, $expectedId ) {
354 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
355 $this->assertEquals( $expectedId, $rev->getId() );
358 public function provideSetId() {
359 yield
[ '123', 123 ];
364 * @dataProvider provideSetId
365 * @covers Revision::setId
367 public function testSetId( $input, $expected ) {
368 $rev = new Revision( [], 0, $this->getMockTitle() );
369 $rev->setId( $input );
370 $this->assertSame( $expected, $rev->getId() );
373 public function provideSetUserIdAndName() {
374 yield
[ '123', 123, 'GOaT' ];
375 yield
[ 456, 456, 'GOaT' ];
379 * @dataProvider provideSetUserIdAndName
380 * @covers Revision::setUserIdAndName
382 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
383 $rev = new Revision( [], 0, $this->getMockTitle() );
384 $rev->setUserIdAndName( $inputId, $name );
385 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
386 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
389 public function provideGetTextId() {
391 yield
[ [ 'text_id' => '123' ], 123 ];
392 yield
[ [ 'text_id' => 456 ], 456 ];
396 * @dataProvider provideGetTextId
397 * @covers Revision::getTextId()
399 public function testGetTextId( $rowArray, $expected ) {
400 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
401 $this->assertSame( $expected, $rev->getTextId() );
404 public function provideGetParentId() {
406 yield
[ [ 'parent_id' => '123' ], 123 ];
407 yield
[ [ 'parent_id' => 456 ], 456 ];
411 * @dataProvider provideGetParentId
412 * @covers Revision::getParentId()
414 public function testGetParentId( $rowArray, $expected ) {
415 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
416 $this->assertSame( $expected, $rev->getParentId() );
420 * @covers Revision::getRevisionText
421 * @dataProvider provideGetRevisionText
423 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
426 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
429 public function provideGetRevisionTextWithZlibExtension() {
430 yield
'Generic gzip test' => [
431 'This is a small goat of revision text.',
433 'old_flags' => 'gzip',
434 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
440 * @covers Revision::getRevisionText
441 * @dataProvider provideGetRevisionTextWithZlibExtension
443 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
444 $this->checkPHPExtension( 'zlib' );
445 $this->testGetRevisionText( $expected, $rowData );
448 private function getWANObjectCache() {
449 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
453 * @return SqlBlobStore
455 private function getBlobStore() {
456 /** @var LoadBalancer $lb */
457 $lb = $this->getMockBuilder( LoadBalancer
::class )
458 ->disableOriginalConstructor()
461 $cache = $this->getWANObjectCache();
463 $blobStore = new SqlBlobStore( $lb, $cache );
467 private function mockBlobStoreFactory( $blobStore ) {
468 /** @var LoadBalancer $lb */
469 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
470 ->disableOriginalConstructor()
472 $factory->expects( $this->any() )
473 ->method( 'newBlobStore' )
474 ->willReturn( $blobStore );
475 $factory->expects( $this->any() )
476 ->method( 'newSqlBlobStore' )
477 ->willReturn( $blobStore );
482 * @return RevisionStore
484 private function getRevisionStore() {
485 /** @var LoadBalancer $lb */
486 $lb = $this->getMockBuilder( LoadBalancer
::class )
487 ->disableOriginalConstructor()
490 $cache = $this->getWANObjectCache();
492 $blobStore = new RevisionStore( $lb, $this->getBlobStore(), $cache );
496 public function provideGetRevisionTextWithLegacyEncoding() {
497 yield
'Utf8Native' => [
498 "Wiki est l'\xc3\xa9cole superieur !",
502 'old_flags' => 'utf-8',
503 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
506 yield
'Utf8Legacy' => [
507 "Wiki est l'\xc3\xa9cole superieur !",
512 'old_text' => "Wiki est l'\xe9cole superieur !",
518 * @covers Revision::getRevisionText
519 * @dataProvider provideGetRevisionTextWithLegacyEncoding
521 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
522 $blobStore = $this->getBlobStore();
523 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
524 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
526 $this->testGetRevisionText( $expected, $rowData );
529 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
532 * Do not set the external flag!
533 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
535 yield
'Utf8NativeGzip' => [
536 "Wiki est l'\xc3\xa9cole superieur !",
540 'old_flags' => 'gzip,utf-8',
541 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
544 yield
'Utf8LegacyGzip' => [
545 "Wiki est l'\xc3\xa9cole superieur !",
549 'old_flags' => 'gzip',
550 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
556 * @covers Revision::getRevisionText
557 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
559 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
560 $this->checkPHPExtension( 'zlib' );
562 $blobStore = $this->getBlobStore();
563 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
564 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
566 $this->testGetRevisionText( $expected, $rowData );
570 * @covers Revision::compressRevisionText
572 public function testCompressRevisionTextUtf8() {
574 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
575 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
576 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
577 "Flags should contain 'utf-8'" );
578 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
579 "Flags should not contain 'gzip'" );
580 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
581 $row->old_text
, "Direct check" );
582 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
583 Revision
::getRevisionText( $row ), "getRevisionText" );
587 * @covers Revision::compressRevisionText
589 public function testCompressRevisionTextUtf8Gzip() {
590 $this->checkPHPExtension( 'zlib' );
592 $blobStore = $this->getBlobStore();
593 $blobStore->setCompressBlobs( true );
594 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
597 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
598 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
599 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
600 "Flags should contain 'utf-8'" );
601 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
602 "Flags should contain 'gzip'" );
603 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
604 gzinflate( $row->old_text
), "Direct check" );
605 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
606 Revision
::getRevisionText( $row ), "getRevisionText" );
610 * @covers Revision::loadFromTitle
612 public function testLoadFromTitle() {
613 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
614 $this->overrideMwServices();
615 $title = $this->getMockTitle();
618 'rev_id=page_latest',
619 'page_namespace' => $title->getNamespace(),
620 'page_title' => $title->getDBkey()
625 'rev_page' => $title->getArticleID(),
626 'rev_text_id' => '2',
627 'rev_timestamp' => '20171017114835',
628 'rev_user_text' => '127.0.0.1',
630 'rev_minor_edit' => '0',
631 'rev_deleted' => '0',
633 'rev_parent_id' => '1',
634 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
635 'rev_comment_text' => 'Goat Comment!',
636 'rev_comment_data' => null,
637 'rev_comment_cid' => null,
638 'rev_content_format' => 'GOATFORMAT',
639 'rev_content_model' => 'GOATMODEL',
642 $db = $this->getMock( IDatabase
::class );
643 $db->expects( $this->any() )
644 ->method( 'getDomainId' )
645 ->will( $this->returnValue( wfWikiID() ) );
646 $db->expects( $this->once() )
647 ->method( 'selectRow' )
649 $this->equalTo( [ 'revision', 'page', 'user' ] ),
650 // We don't really care about the fields are they come from the selectField methods
651 $this->isType( 'array' ),
652 $this->equalTo( $conditions ),
654 $this->stringContains( 'fetchRevisionRowFromConds' ),
655 // We don't really care about the options here
656 $this->isType( 'array' ),
657 // We don't really care about the join conds are they come from the joinCond methods
658 $this->isType( 'array' )
660 ->willReturn( $row );
662 $revision = Revision
::loadFromTitle( $db, $title );
664 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
665 $this->assertEquals( $row->rev_id
, $revision->getId() );
666 $this->assertEquals( $row->rev_len
, $revision->getSize() );
667 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
668 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
669 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
670 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
671 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
674 public function provideDecompressRevisionText() {
675 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
676 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
677 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
678 yield
'(no legacy encoding), string in with gzip flag returns string' => [
679 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
680 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
682 yield
'(no legacy encoding), string in with object flag returns false' => [
683 // gzip string below generated with serialize( 'JOJO' )
684 false, "s:4:\"JOJO\";", [ 'object' ], false,
686 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
688 // Using a TitleValue object as it has a getText method (which is needed)
689 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
693 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
695 // Using a TitleValue object as it has a getText method (which is needed)
696 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
697 [ 'object', 'gzip' ],
700 yield
'(ISO-8859-1 encoding), string in string out' => [
702 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
706 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
708 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
712 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
714 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
718 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
720 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
721 [ 'gzip', 'object' ],
727 * @dataProvider provideDecompressRevisionText
728 * @covers Revision::decompressRevisionText
730 * @param bool $legacyEncoding
732 * @param array $flags
733 * @param mixed $expected
735 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
736 $blobStore = $this->getBlobStore();
737 if ( $legacyEncoding ) {
738 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
741 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
744 Revision
::decompressRevisionText( $text, $flags )
749 * @covers Revision::getRevisionText
751 public function testGetRevisionText_returnsFalseWhenNoTextField() {
752 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
755 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
756 yield
'Just text' => [
757 (object)[ 'old_text' => 'SomeText' ],
761 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
762 yield
'gzip text' => [
764 'old_text' => "sttttr\002\022\000",
765 'old_flags' => 'gzip'
770 yield
'gzip text and different prefix' => [
772 'jojo_text' => "sttttr\002\022\000",
773 'jojo_flags' => 'gzip'
781 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
782 * @covers Revision::getRevisionText
784 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
789 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
792 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
793 yield
'Just some text' => [ 'someNonUrlText' ];
794 yield
'No second URL part' => [ 'someProtocol://' ];
798 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
799 * @covers Revision::getRevisionText
801 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
805 Revision
::getRevisionText(
808 'old_flags' => 'external',
815 * @covers Revision::getRevisionText
817 public function testGetRevisionText_external_noOldId() {
819 'ExternalStoreFactory',
820 new ExternalStoreFactory( [ 'ForTesting' ] )
824 Revision
::getRevisionText(
826 'old_text' => 'ForTesting://cluster1/12345',
827 'old_flags' => 'external,gzip',
834 * @covers Revision::getRevisionText
836 public function testGetRevisionText_external_oldId() {
837 $cache = $this->getWANObjectCache();
838 $this->setService( 'MainWANObjectCache', $cache );
841 'ExternalStoreFactory',
842 new ExternalStoreFactory( [ 'ForTesting' ] )
845 $lb = $this->getMockBuilder( LoadBalancer
::class )
846 ->disableOriginalConstructor()
849 $blobStore = new SqlBlobStore( $lb, $cache );
850 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
854 Revision
::getRevisionText(
856 'old_text' => 'ForTesting://cluster1/12345',
857 'old_flags' => 'external,gzip',
863 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
864 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
868 * @covers Revision::userJoinCond
870 public function testUserJoinCond() {
871 $this->hideDeprecated( 'Revision::userJoinCond' );
873 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
874 Revision
::userJoinCond()
879 * @covers Revision::pageJoinCond
881 public function testPageJoinCond() {
882 $this->hideDeprecated( 'Revision::pageJoinCond' );
884 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
885 Revision
::pageJoinCond()
889 private function overrideCommentStore() {
890 $mockStore = $this->getMockBuilder( CommentStore
::class )
891 ->disableOriginalConstructor()
893 $mockStore->expects( $this->any() )
894 ->method( 'getFields' )
895 ->willReturn( [ 'commentstore' => 'fields' ] );
896 $mockStore->expects( $this->any() )
897 ->method( 'getJoin' )
899 'tables' => [ 'commentstore' => 'table' ],
900 'fields' => [ 'commentstore' => 'field' ],
901 'joins' => [ 'commentstore' => 'join' ],
904 $this->setService( 'CommentStore', $mockStore );
907 public function provideSelectFields() {
922 'commentstore' => 'fields',
923 'rev_content_format',
941 'commentstore' => 'fields',
947 * @dataProvider provideSelectFields
948 * @covers Revision::selectFields
950 public function testSelectFields( $contentHandlerUseDB, $expected ) {
951 $this->hideDeprecated( 'Revision::selectFields' );
952 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
953 $this->overrideCommentStore();
954 $this->assertEquals( $expected, Revision
::selectFields() );
957 public function provideSelectArchiveFields() {
974 'commentstore' => 'fields',
995 'commentstore' => 'fields',
1001 * @dataProvider provideSelectArchiveFields
1002 * @covers Revision::selectArchiveFields
1004 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1005 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1006 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1007 $this->overrideCommentStore();
1008 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1012 * @covers Revision::selectTextFields
1014 public function testSelectTextFields() {
1015 $this->hideDeprecated( 'Revision::selectTextFields' );
1016 $this->assertEquals(
1021 Revision
::selectTextFields()
1026 * @covers Revision::selectPageFields
1028 public function testSelectPageFields() {
1029 $this->hideDeprecated( 'Revision::selectPageFields' );
1030 $this->assertEquals(
1039 Revision
::selectPageFields()
1044 * @covers Revision::selectUserFields
1046 public function testSelectUserFields() {
1047 $this->hideDeprecated( 'Revision::selectUserFields' );
1048 $this->assertEquals(
1052 Revision
::selectUserFields()
1056 public function provideGetArchiveQueryInfo() {
1057 yield
'wgContentHandlerUseDB false' => [
1059 'wgContentHandlerUseDB' => false,
1064 'commentstore' => 'table',
1082 'commentstore' => 'field'
1084 'joins' => [ 'commentstore' => 'join' ],
1087 yield
'wgContentHandlerUseDB true' => [
1089 'wgContentHandlerUseDB' => true,
1094 'commentstore' => 'table',
1112 'commentstore' => 'field',
1113 'ar_content_format',
1116 'joins' => [ 'commentstore' => 'join' ],
1122 * @covers Revision::getArchiveQueryInfo
1123 * @dataProvider provideGetArchiveQueryInfo
1125 public function testGetArchiveQueryInfo( $globals, $expected ) {
1126 $this->setMwGlobals( $globals );
1127 $this->overrideCommentStore();
1129 $revisionStore = $this->getRevisionStore();
1130 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1131 $this->setService( 'RevisionStore', $revisionStore );
1132 $this->assertEquals(
1134 Revision
::getArchiveQueryInfo()
1138 public function provideGetQueryInfo() {
1139 yield
'wgContentHandlerUseDB false, opts none' => [
1141 'wgContentHandlerUseDB' => false,
1145 'tables' => [ 'revision', 'commentstore' => 'table' ],
1158 'commentstore' => 'field',
1160 'joins' => [ 'commentstore' => 'join' ],
1163 yield
'wgContentHandlerUseDB false, opts page' => [
1165 'wgContentHandlerUseDB' => false,
1169 'tables' => [ 'revision', 'commentstore' => 'table', 'page' ],
1182 'commentstore' => 'field',
1193 [ 'page_id = rev_page' ],
1195 'commentstore' => 'join',
1199 yield
'wgContentHandlerUseDB false, opts user' => [
1201 'wgContentHandlerUseDB' => false,
1205 'tables' => [ 'revision', 'commentstore' => 'table', 'user' ],
1218 'commentstore' => 'field',
1226 'user_id = rev_user',
1229 'commentstore' => 'join',
1233 yield
'wgContentHandlerUseDB false, opts text' => [
1235 'wgContentHandlerUseDB' => false,
1239 'tables' => [ 'revision', 'commentstore' => 'table', 'text' ],
1252 'commentstore' => 'field',
1259 [ 'rev_text_id=old_id' ],
1261 'commentstore' => 'join',
1265 yield
'wgContentHandlerUseDB false, opts 3' => [
1267 'wgContentHandlerUseDB' => false,
1269 [ 'text', 'page', 'user' ],
1271 'tables' => [ 'revision', 'commentstore' => 'table', 'page', 'user', 'text' ],
1284 'commentstore' => 'field',
1298 [ 'page_id = rev_page' ],
1304 'user_id = rev_user',
1309 [ 'rev_text_id=old_id' ],
1311 'commentstore' => 'join',
1315 yield
'wgContentHandlerUseDB true, opts none' => [
1317 'wgContentHandlerUseDB' => true,
1321 'tables' => [ 'revision', 'commentstore' => 'table' ],
1334 'commentstore' => 'field',
1335 'rev_content_format',
1336 'rev_content_model',
1338 'joins' => [ 'commentstore' => 'join' ],
1344 * @covers Revision::getQueryInfo
1345 * @dataProvider provideGetQueryInfo
1347 public function testGetQueryInfo( $globals, $options, $expected ) {
1348 $this->setMwGlobals( $globals );
1349 $this->overrideCommentStore();
1351 $revisionStore = $this->getRevisionStore();
1352 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1353 $this->setService( 'RevisionStore', $revisionStore );
1355 $this->assertEquals(
1357 Revision
::getQueryInfo( $options )
1362 * @covers Revision::getSize
1364 public function testGetSize() {
1365 $title = $this->getMockTitle();
1367 $rec = new MutableRevisionRecord( $title );
1368 $rev = new Revision( $rec, 0, $title );
1370 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1372 $rec->setSize( 13 );
1373 $this->assertSame( 13, $rev->getSize() );
1377 * @covers Revision::getSize
1379 public function testGetSize_failure() {
1380 $title = $this->getMockTitle();
1382 $rec = $this->getMockBuilder( RevisionRecord
::class )
1383 ->disableOriginalConstructor()
1386 $rec->method( 'getSize' )
1387 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1389 $rev = new Revision( $rec, 0, $title );
1390 $this->assertNull( $rev->getSize() );
1394 * @covers Revision::getSha1
1396 public function testGetSha1() {
1397 $title = $this->getMockTitle();
1399 $rec = new MutableRevisionRecord( $title );
1400 $rev = new Revision( $rec, 0, $title );
1402 $emptyHash = SlotRecord
::base36Sha1( '' );
1403 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1405 $rec->setSha1( 'deadbeef' );
1406 $this->assertSame( 'deadbeef', $rev->getSha1() );
1410 * @covers Revision::getSha1
1412 public function testGetSha1_failure() {
1413 $title = $this->getMockTitle();
1415 $rec = $this->getMockBuilder( RevisionRecord
::class )
1416 ->disableOriginalConstructor()
1419 $rec->method( 'getSha1' )
1420 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1422 $rev = new Revision( $rec, 0, $title );
1423 $this->assertNull( $rev->getSha1() );
1427 * @covers Revision::getContent
1429 public function testGetContent() {
1430 $title = $this->getMockTitle();
1432 $rec = new MutableRevisionRecord( $title );
1433 $rev = new Revision( $rec, 0, $title );
1435 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1437 $content = new TextContent( 'Hello Kittens!' );
1438 $rec->setContent( 'main', $content );
1439 $this->assertSame( $content, $rev->getContent() );
1443 * @covers Revision::getContent
1445 public function testGetContent_failure() {
1446 $title = $this->getMockTitle();
1448 $rec = $this->getMockBuilder( RevisionRecord
::class )
1449 ->disableOriginalConstructor()
1452 $rec->method( 'getContent' )
1453 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1455 $rev = new Revision( $rec, 0, $title );
1456 $this->assertNull( $rev->getContent() );