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 MediaWiki\
suppressWarnings();
321 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
322 $this->assertSame( 77777777, $rev->getPage() );
323 MediaWiki\restoreWarnings
();
326 public function provideGetRevisionText() {
327 yield
'Generic test' => [
328 'This is a goat of revision text.',
331 'old_text' => 'This is a goat of revision text.',
336 public function provideGetId() {
348 * @dataProvider provideGetId
349 * @covers Revision::getId
351 public function testGetId( $rowArray, $expectedId ) {
352 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
353 $this->assertEquals( $expectedId, $rev->getId() );
356 public function provideSetId() {
357 yield
[ '123', 123 ];
362 * @dataProvider provideSetId
363 * @covers Revision::setId
365 public function testSetId( $input, $expected ) {
366 $rev = new Revision( [], 0, $this->getMockTitle() );
367 $rev->setId( $input );
368 $this->assertSame( $expected, $rev->getId() );
371 public function provideSetUserIdAndName() {
372 yield
[ '123', 123, 'GOaT' ];
373 yield
[ 456, 456, 'GOaT' ];
377 * @dataProvider provideSetUserIdAndName
378 * @covers Revision::setUserIdAndName
380 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
381 $rev = new Revision( [], 0, $this->getMockTitle() );
382 $rev->setUserIdAndName( $inputId, $name );
383 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
384 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
387 public function provideGetTextId() {
389 yield
[ [ 'text_id' => '123' ], 123 ];
390 yield
[ [ 'text_id' => 456 ], 456 ];
394 * @dataProvider provideGetTextId
395 * @covers Revision::getTextId()
397 public function testGetTextId( $rowArray, $expected ) {
398 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
399 $this->assertSame( $expected, $rev->getTextId() );
402 public function provideGetParentId() {
404 yield
[ [ 'parent_id' => '123' ], 123 ];
405 yield
[ [ 'parent_id' => 456 ], 456 ];
409 * @dataProvider provideGetParentId
410 * @covers Revision::getParentId()
412 public function testGetParentId( $rowArray, $expected ) {
413 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
414 $this->assertSame( $expected, $rev->getParentId() );
418 * @covers Revision::getRevisionText
419 * @dataProvider provideGetRevisionText
421 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
424 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
427 public function provideGetRevisionTextWithZlibExtension() {
428 yield
'Generic gzip test' => [
429 'This is a small goat of revision text.',
431 'old_flags' => 'gzip',
432 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
438 * @covers Revision::getRevisionText
439 * @dataProvider provideGetRevisionTextWithZlibExtension
441 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
442 $this->checkPHPExtension( 'zlib' );
443 $this->testGetRevisionText( $expected, $rowData );
446 private function getWANObjectCache() {
447 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
451 * @return SqlBlobStore
453 private function getBlobStore() {
454 /** @var LoadBalancer $lb */
455 $lb = $this->getMockBuilder( LoadBalancer
::class )
456 ->disableOriginalConstructor()
459 $cache = $this->getWANObjectCache();
461 $blobStore = new SqlBlobStore( $lb, $cache );
465 private function mockBlobStoreFactory( $blobStore ) {
466 /** @var LoadBalancer $lb */
467 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
468 ->disableOriginalConstructor()
470 $factory->expects( $this->any() )
471 ->method( 'newBlobStore' )
472 ->willReturn( $blobStore );
473 $factory->expects( $this->any() )
474 ->method( 'newSqlBlobStore' )
475 ->willReturn( $blobStore );
480 * @return RevisionStore
482 private function getRevisionStore() {
483 /** @var LoadBalancer $lb */
484 $lb = $this->getMockBuilder( LoadBalancer
::class )
485 ->disableOriginalConstructor()
488 $cache = $this->getWANObjectCache();
490 $blobStore = new RevisionStore( $lb, $this->getBlobStore(), $cache );
494 public function provideGetRevisionTextWithLegacyEncoding() {
495 yield
'Utf8Native' => [
496 "Wiki est l'\xc3\xa9cole superieur !",
500 'old_flags' => 'utf-8',
501 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
504 yield
'Utf8Legacy' => [
505 "Wiki est l'\xc3\xa9cole superieur !",
510 'old_text' => "Wiki est l'\xe9cole superieur !",
516 * @covers Revision::getRevisionText
517 * @dataProvider provideGetRevisionTextWithLegacyEncoding
519 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
520 $blobStore = $this->getBlobStore();
521 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
522 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
524 $this->testGetRevisionText( $expected, $rowData );
527 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
530 * Do not set the external flag!
531 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
533 yield
'Utf8NativeGzip' => [
534 "Wiki est l'\xc3\xa9cole superieur !",
538 'old_flags' => 'gzip,utf-8',
539 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
542 yield
'Utf8LegacyGzip' => [
543 "Wiki est l'\xc3\xa9cole superieur !",
547 'old_flags' => 'gzip',
548 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
554 * @covers Revision::getRevisionText
555 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
557 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
558 $this->checkPHPExtension( 'zlib' );
560 $blobStore = $this->getBlobStore();
561 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
562 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
564 $this->testGetRevisionText( $expected, $rowData );
568 * @covers Revision::compressRevisionText
570 public function testCompressRevisionTextUtf8() {
572 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
573 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
574 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
575 "Flags should contain 'utf-8'" );
576 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
577 "Flags should not contain 'gzip'" );
578 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
579 $row->old_text
, "Direct check" );
580 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
581 Revision
::getRevisionText( $row ), "getRevisionText" );
585 * @covers Revision::compressRevisionText
587 public function testCompressRevisionTextUtf8Gzip() {
588 $this->checkPHPExtension( 'zlib' );
590 $blobStore = $this->getBlobStore();
591 $blobStore->setCompressBlobs( true );
592 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
595 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
596 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
597 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
598 "Flags should contain 'utf-8'" );
599 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
600 "Flags should contain 'gzip'" );
601 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
602 gzinflate( $row->old_text
), "Direct check" );
603 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
604 Revision
::getRevisionText( $row ), "getRevisionText" );
608 * @covers Revision::loadFromTitle
610 public function testLoadFromTitle() {
611 $title = $this->getMockTitle();
614 'rev_id=page_latest',
615 'page_namespace' => $title->getNamespace(),
616 'page_title' => $title->getDBkey()
621 'rev_page' => $title->getArticleID(),
622 'rev_text_id' => '2',
623 'rev_timestamp' => '20171017114835',
624 'rev_user_text' => '127.0.0.1',
626 'rev_minor_edit' => '0',
627 'rev_deleted' => '0',
629 'rev_parent_id' => '1',
630 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
631 'rev_comment_text' => 'Goat Comment!',
632 'rev_comment_data' => null,
633 'rev_comment_cid' => null,
634 'rev_content_format' => 'GOATFORMAT',
635 'rev_content_model' => 'GOATMODEL',
638 $db = $this->getMock( IDatabase
::class );
639 $db->expects( $this->any() )
640 ->method( 'getDomainId' )
641 ->will( $this->returnValue( wfWikiID() ) );
642 $db->expects( $this->once() )
643 ->method( 'selectRow' )
645 $this->equalTo( [ 'revision', 'page', 'user' ] ),
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 )
745 * @covers Revision::getRevisionText
747 public function testGetRevisionText_returnsFalseWhenNoTextField() {
748 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
751 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
752 yield
'Just text' => [
753 (object)[ 'old_text' => 'SomeText' ],
757 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
758 yield
'gzip text' => [
760 'old_text' => "sttttr\002\022\000",
761 'old_flags' => 'gzip'
766 yield
'gzip text and different prefix' => [
768 'jojo_text' => "sttttr\002\022\000",
769 'jojo_flags' => 'gzip'
777 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
778 * @covers Revision::getRevisionText
780 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
785 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
788 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
789 yield
'Just some text' => [ 'someNonUrlText' ];
790 yield
'No second URL part' => [ 'someProtocol://' ];
794 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
795 * @covers Revision::getRevisionText
797 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
801 Revision
::getRevisionText(
804 'old_flags' => 'external',
811 * @covers Revision::getRevisionText
813 public function testGetRevisionText_external_noOldId() {
815 'ExternalStoreFactory',
816 new ExternalStoreFactory( [ 'ForTesting' ] )
820 Revision
::getRevisionText(
822 'old_text' => 'ForTesting://cluster1/12345',
823 'old_flags' => 'external,gzip',
830 * @covers Revision::getRevisionText
832 public function testGetRevisionText_external_oldId() {
833 $cache = $this->getWANObjectCache();
834 $this->setService( 'MainWANObjectCache', $cache );
837 'ExternalStoreFactory',
838 new ExternalStoreFactory( [ 'ForTesting' ] )
841 $lb = $this->getMockBuilder( LoadBalancer
::class )
842 ->disableOriginalConstructor()
845 $blobStore = new SqlBlobStore( $lb, $cache );
846 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
850 Revision
::getRevisionText(
852 'old_text' => 'ForTesting://cluster1/12345',
853 'old_flags' => 'external,gzip',
859 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
860 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
864 * @covers Revision::userJoinCond
866 public function testUserJoinCond() {
867 $this->hideDeprecated( 'Revision::userJoinCond' );
869 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
870 Revision
::userJoinCond()
875 * @covers Revision::pageJoinCond
877 public function testPageJoinCond() {
878 $this->hideDeprecated( 'Revision::pageJoinCond' );
880 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
881 Revision
::pageJoinCond()
885 public function provideSelectFields() {
900 'rev_comment_text' => 'rev_comment',
901 'rev_comment_data' => 'NULL',
902 'rev_comment_cid' => 'NULL',
903 'rev_content_format',
921 'rev_comment_text' => 'rev_comment',
922 'rev_comment_data' => 'NULL',
923 'rev_comment_cid' => 'NULL',
929 * @dataProvider provideSelectFields
930 * @covers Revision::selectFields
931 * @todo a true unit test would mock CommentStore
933 public function testSelectFields( $contentHandlerUseDB, $expected ) {
934 $this->hideDeprecated( 'Revision::selectFields' );
935 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
936 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
937 $this->assertEquals( $expected, Revision
::selectFields() );
940 public function provideSelectArchiveFields() {
957 'ar_comment_text' => 'ar_comment',
958 'ar_comment_data' => 'NULL',
959 'ar_comment_cid' => 'NULL',
980 'ar_comment_text' => 'ar_comment',
981 'ar_comment_data' => 'NULL',
982 'ar_comment_cid' => 'NULL',
988 * @dataProvider provideSelectArchiveFields
989 * @covers Revision::selectArchiveFields
990 * @todo a true unit test would mock CommentStore
992 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
993 $this->hideDeprecated( 'Revision::selectArchiveFields' );
994 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
995 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
996 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1000 * @covers Revision::selectTextFields
1002 public function testSelectTextFields() {
1003 $this->hideDeprecated( 'Revision::selectTextFields' );
1004 $this->assertEquals(
1009 Revision
::selectTextFields()
1014 * @covers Revision::selectPageFields
1016 public function testSelectPageFields() {
1017 $this->hideDeprecated( 'Revision::selectPageFields' );
1018 $this->assertEquals(
1027 Revision
::selectPageFields()
1032 * @covers Revision::selectUserFields
1034 public function testSelectUserFields() {
1035 $this->hideDeprecated( 'Revision::selectUserFields' );
1036 $this->assertEquals(
1040 Revision
::selectUserFields()
1044 public function provideGetArchiveQueryInfo() {
1045 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1047 'wgContentHandlerUseDB' => false,
1048 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1051 'tables' => [ 'archive' ],
1068 'ar_comment_text' => 'ar_comment',
1069 'ar_comment_data' => 'NULL',
1070 'ar_comment_cid' => 'NULL',
1075 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1077 'wgContentHandlerUseDB' => true,
1078 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1081 'tables' => [ 'archive' ],
1098 'ar_comment_text' => 'ar_comment',
1099 'ar_comment_data' => 'NULL',
1100 'ar_comment_cid' => 'NULL',
1101 'ar_content_format',
1107 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1109 'wgContentHandlerUseDB' => false,
1110 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1115 'comment_ar_comment' => 'comment',
1133 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1134 'ar_comment_data' => 'comment_ar_comment.comment_data',
1135 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1138 'comment_ar_comment' => [
1140 'comment_ar_comment.comment_id = ar_comment_id',
1145 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1147 'wgContentHandlerUseDB' => false,
1148 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1153 'comment_ar_comment' => 'comment',
1171 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1172 'ar_comment_data' => 'comment_ar_comment.comment_data',
1173 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1176 'comment_ar_comment' => [
1178 'comment_ar_comment.comment_id = ar_comment_id',
1183 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1185 'wgContentHandlerUseDB' => false,
1186 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1191 'comment_ar_comment' => 'comment',
1209 'ar_comment_text' => 'comment_ar_comment.comment_text',
1210 'ar_comment_data' => 'comment_ar_comment.comment_data',
1211 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1214 'comment_ar_comment' => [
1216 'comment_ar_comment.comment_id = ar_comment_id',
1224 * @covers Revision::getArchiveQueryInfo
1225 * @dataProvider provideGetArchiveQueryInfo
1227 public function testGetArchiveQueryInfo( $globals, $expected ) {
1228 $this->setMwGlobals( $globals );
1230 $revisionStore = $this->getRevisionStore();
1231 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1232 $this->setService( 'RevisionStore', $revisionStore );
1234 $this->assertEquals(
1236 Revision
::getArchiveQueryInfo()
1240 public function provideGetQueryInfo() {
1241 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1243 'wgContentHandlerUseDB' => false,
1244 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1248 'tables' => [ 'revision' ],
1261 'rev_comment_text' => 'rev_comment',
1262 'rev_comment_data' => 'NULL',
1263 'rev_comment_cid' => 'NULL',
1268 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1270 'wgContentHandlerUseDB' => false,
1271 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1275 'tables' => [ 'revision', 'page' ],
1288 'rev_comment_text' => 'rev_comment',
1289 'rev_comment_data' => 'NULL',
1290 'rev_comment_cid' => 'NULL',
1301 [ 'page_id = rev_page' ],
1306 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1308 'wgContentHandlerUseDB' => false,
1309 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1313 'tables' => [ 'revision', 'user' ],
1326 'rev_comment_text' => 'rev_comment',
1327 'rev_comment_data' => 'NULL',
1328 'rev_comment_cid' => 'NULL',
1336 'user_id = rev_user',
1342 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1344 'wgContentHandlerUseDB' => false,
1345 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1349 'tables' => [ 'revision', 'text' ],
1362 'rev_comment_text' => 'rev_comment',
1363 'rev_comment_data' => 'NULL',
1364 'rev_comment_cid' => 'NULL',
1371 [ 'rev_text_id=old_id' ],
1376 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1378 'wgContentHandlerUseDB' => false,
1379 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1381 [ 'text', 'page', 'user' ],
1383 'tables' => [ 'revision', 'page', 'user', 'text' ],
1396 'rev_comment_text' => 'rev_comment',
1397 'rev_comment_data' => 'NULL',
1398 'rev_comment_cid' => 'NULL',
1412 [ 'page_id = rev_page' ],
1418 'user_id = rev_user',
1423 [ 'rev_text_id=old_id' ],
1428 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1430 'wgContentHandlerUseDB' => true,
1431 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1435 'tables' => [ 'revision' ],
1448 'rev_comment_text' => 'rev_comment',
1449 'rev_comment_data' => 'NULL',
1450 'rev_comment_cid' => 'NULL',
1451 'rev_content_format',
1452 'rev_content_model',
1457 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1459 'wgContentHandlerUseDB' => false,
1460 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1466 'temp_rev_comment' => 'revision_comment_temp',
1467 'comment_rev_comment' => 'comment',
1481 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1482 'rev_comment_data' => 'comment_rev_comment.comment_data',
1483 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1486 'temp_rev_comment' => [
1488 'temp_rev_comment.revcomment_rev = rev_id',
1490 'comment_rev_comment' => [
1492 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1497 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1499 'wgContentHandlerUseDB' => false,
1500 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1506 'temp_rev_comment' => 'revision_comment_temp',
1507 'comment_rev_comment' => 'comment',
1521 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1522 'rev_comment_data' => 'comment_rev_comment.comment_data',
1523 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1526 'temp_rev_comment' => [
1528 'temp_rev_comment.revcomment_rev = rev_id',
1530 'comment_rev_comment' => [
1532 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1537 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1539 'wgContentHandlerUseDB' => false,
1540 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1546 'temp_rev_comment' => 'revision_comment_temp',
1547 'comment_rev_comment' => 'comment',
1561 'rev_comment_text' => 'comment_rev_comment.comment_text',
1562 'rev_comment_data' => 'comment_rev_comment.comment_data',
1563 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1566 'temp_rev_comment' => [
1568 'temp_rev_comment.revcomment_rev = rev_id',
1570 'comment_rev_comment' => [
1572 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1580 * @covers Revision::getQueryInfo
1581 * @dataProvider provideGetQueryInfo
1583 public function testGetQueryInfo( $globals, $options, $expected ) {
1584 $this->setMwGlobals( $globals );
1586 $revisionStore = $this->getRevisionStore();
1587 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1588 $this->setService( 'RevisionStore', $revisionStore );
1590 $this->assertEquals(
1592 Revision
::getQueryInfo( $options )
1597 * @covers Revision::getSize
1599 public function testGetSize() {
1600 $title = $this->getMockTitle();
1602 $rec = new MutableRevisionRecord( $title );
1603 $rev = new Revision( $rec, 0, $title );
1605 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1607 $rec->setSize( 13 );
1608 $this->assertSame( 13, $rev->getSize() );
1612 * @covers Revision::getSize
1614 public function testGetSize_failure() {
1615 $title = $this->getMockTitle();
1617 $rec = $this->getMockBuilder( RevisionRecord
::class )
1618 ->disableOriginalConstructor()
1621 $rec->method( 'getSize' )
1622 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1624 $rev = new Revision( $rec, 0, $title );
1625 $this->assertNull( $rev->getSize() );
1629 * @covers Revision::getSha1
1631 public function testGetSha1() {
1632 $title = $this->getMockTitle();
1634 $rec = new MutableRevisionRecord( $title );
1635 $rev = new Revision( $rec, 0, $title );
1637 $emptyHash = SlotRecord
::base36Sha1( '' );
1638 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1640 $rec->setSha1( 'deadbeef' );
1641 $this->assertSame( 'deadbeef', $rev->getSha1() );
1645 * @covers Revision::getSha1
1647 public function testGetSha1_failure() {
1648 $title = $this->getMockTitle();
1650 $rec = $this->getMockBuilder( RevisionRecord
::class )
1651 ->disableOriginalConstructor()
1654 $rec->method( 'getSha1' )
1655 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1657 $rev = new Revision( $rec, 0, $title );
1658 $this->assertNull( $rev->getSha1() );
1662 * @covers Revision::getContent
1664 public function testGetContent() {
1665 $title = $this->getMockTitle();
1667 $rec = new MutableRevisionRecord( $title );
1668 $rev = new Revision( $rec, 0, $title );
1670 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1672 $content = new TextContent( 'Hello Kittens!' );
1673 $rec->setContent( 'main', $content );
1674 $this->assertSame( $content, $rev->getContent() );
1678 * @covers Revision::getContent
1680 public function testGetContent_failure() {
1681 $title = $this->getMockTitle();
1683 $rec = $this->getMockBuilder( RevisionRecord
::class )
1684 ->disableOriginalConstructor()
1687 $rec->method( 'getContent' )
1688 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1690 $rev = new Revision( $rec, 0, $title );
1691 $this->assertNull( $rev->getContent() );