3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Storage\BlobStoreFactory
;
5 use MediaWiki\Storage\MutableRevisionRecord
;
6 use MediaWiki\Storage\RevisionAccessException
;
7 use MediaWiki\Storage\RevisionRecord
;
8 use MediaWiki\Storage\RevisionStore
;
9 use MediaWiki\Storage\SlotRecord
;
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 provideConstructFromArray() {
21 yield
'with text' => [
23 'text' => 'hello world.',
24 'content_model' => CONTENT_MODEL_JAVASCRIPT
27 yield
'with content' => [
29 'content' => new JavaScriptContent( 'hellow world.' )
32 // FIXME: test with and without user ID, and with a user object.
33 // We can't prepare that here though, since we don't yet have a dummy DB
37 * @param string $model
40 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
41 $mock = $this->getMockBuilder( Title
::class )
42 ->disableOriginalConstructor()
44 $mock->expects( $this->any() )
45 ->method( 'getNamespace' )
46 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
47 $mock->expects( $this->any() )
48 ->method( 'getPrefixedText' )
49 ->will( $this->returnValue( 'RevisionTest' ) );
50 $mock->expects( $this->any() )
51 ->method( 'getDBkey' )
52 ->will( $this->returnValue( 'RevisionTest' ) );
53 $mock->expects( $this->any() )
54 ->method( 'getArticleID' )
55 ->will( $this->returnValue( 23 ) );
56 $mock->expects( $this->any() )
57 ->method( 'getContentModel' )
58 ->will( $this->returnValue( $model ) );
64 * @dataProvider provideConstructFromArray
65 * @covers Revision::__construct
66 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
68 public function testConstructFromArray( $rowArray ) {
69 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
70 $this->assertNotNull( $rev->getContent(), 'no content object available' );
71 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
72 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
76 * @covers Revision::__construct
77 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
79 public function testConstructFromEmptyArray() {
80 $rev = new Revision( [], 0, $this->getMockTitle() );
81 $this->assertNull( $rev->getContent(), 'no content object should be available' );
85 * @covers Revision::__construct
86 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
88 public function testConstructFromArrayWithBadPageId() {
89 Wikimedia\
suppressWarnings();
90 $rev = new Revision( [ 'page' => 77777777 ] );
91 $this->assertSame( 77777777, $rev->getPage() );
92 Wikimedia\restoreWarnings
();
95 public function provideConstructFromArray_userSetAsExpected() {
96 yield
'no user defaults to wgUser' => [
98 'content' => new JavaScriptContent( 'hello world.' ),
103 yield
'user text and id' => [
105 'content' => new JavaScriptContent( 'hello world.' ),
106 'user_text' => 'SomeTextUserName',
113 yield
'user text only' => [
115 'content' => new JavaScriptContent( 'hello world.' ),
116 'user_text' => '111.111.111.111',
124 * @dataProvider provideConstructFromArray_userSetAsExpected
125 * @covers Revision::__construct
126 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
128 * @param array $rowArray
129 * @param mixed $expectedUserId null to expect the current wgUser ID
130 * @param mixed $expectedUserName null to expect the current wgUser name
132 public function testConstructFromArray_userSetAsExpected(
137 $testUser = $this->getTestUser()->getUser();
138 $this->setMwGlobals( 'wgUser', $testUser );
139 if ( $expectedUserId === null ) {
140 $expectedUserId = $testUser->getId();
142 if ( $expectedUserName === null ) {
143 $expectedUserName = $testUser->getName();
146 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
147 $this->assertEquals( $expectedUserId, $rev->getUser() );
148 $this->assertEquals( $expectedUserName, $rev->getUserText() );
151 public function provideConstructFromArrayThrowsExceptions() {
152 yield
'content and text_id both not empty' => [
154 'content' => new WikitextContent( 'GOAT' ),
155 'text_id' => 'someid',
157 new MWException( "Text already stored in external store (id someid), " .
158 "can't serialize content object" )
160 yield
'with bad content object (class)' => [
161 [ 'content' => new stdClass() ],
162 new MWException( 'content field must contain a Content object.' )
164 yield
'with bad content object (string)' => [
165 [ 'content' => 'ImAGoat' ],
166 new MWException( 'content field must contain a Content object.' )
168 yield
'bad row format' => [
169 'imastring, not a row',
170 new InvalidArgumentException(
171 '$row must be a row object, an associative array, or a RevisionRecord'
177 * @dataProvider provideConstructFromArrayThrowsExceptions
178 * @covers Revision::__construct
179 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
181 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
182 $this->setExpectedException(
183 get_class( $expectedException ),
184 $expectedException->getMessage(),
185 $expectedException->getCode()
187 new Revision( $rowArray, 0, $this->getMockTitle() );
191 * @covers Revision::__construct
192 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
194 public function testConstructFromNothing() {
195 $this->setExpectedException(
196 InvalidArgumentException
::class
201 public function provideConstructFromRow() {
202 yield
'Full construction' => [
206 'rev_text_id' => '2',
207 'rev_timestamp' => '20171017114835',
208 'rev_user_text' => '127.0.0.1',
210 'rev_minor_edit' => '0',
211 'rev_deleted' => '0',
213 'rev_parent_id' => '1',
214 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
215 'rev_comment_text' => 'Goat Comment!',
216 'rev_comment_data' => null,
217 'rev_comment_cid' => null,
218 'rev_content_format' => 'GOATFORMAT',
219 'rev_content_model' => 'GOATMODEL',
221 function ( RevisionTest
$testCase, Revision
$rev ) {
222 $testCase->assertSame( 42, $rev->getId() );
223 $testCase->assertSame( 23, $rev->getPage() );
224 $testCase->assertSame( 2, $rev->getTextId() );
225 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
226 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
227 $testCase->assertSame( 0, $rev->getUser() );
228 $testCase->assertSame( false, $rev->isMinor() );
229 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
230 $testCase->assertSame( 46, $rev->getSize() );
231 $testCase->assertSame( 1, $rev->getParentId() );
232 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
233 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
234 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
235 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
238 yield
'default field values' => [
242 'rev_text_id' => '2',
243 'rev_timestamp' => '20171017114835',
244 'rev_user_text' => '127.0.0.1',
246 'rev_minor_edit' => '0',
247 'rev_deleted' => '0',
248 'rev_comment_text' => 'Goat Comment!',
249 'rev_comment_data' => null,
250 'rev_comment_cid' => null,
252 function ( RevisionTest
$testCase, Revision
$rev ) {
253 // parent ID may be null
254 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
257 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
258 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
259 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
260 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
261 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
262 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
265 $testCase->assertNotNull( $rev->getSize(), 'size' );
266 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
268 // NOTE: model and format will be detected based on the namespace of the (mock) title
269 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
270 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
276 * @dataProvider provideConstructFromRow
277 * @covers Revision::__construct
278 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
280 public function testConstructFromRow( array $arrayData, $assertions ) {
281 $data = 'Hello goat.'; // needs to match model and format
283 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
284 ->disableOriginalConstructor()
287 $blobStore->method( 'getBlob' )
288 ->will( $this->returnValue( $data ) );
290 $blobStore->method( 'getTextIdFromAddress' )
291 ->will( $this->returnCallback(
292 function ( $address ) {
293 // Turn "tt:1234" into 12345.
294 // Note that this must be functional so we can test getTextId().
295 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
296 $parts = explode( ':', $address );
297 return (int)array_pop( $parts );
301 // Note override internal service, so RevisionStore uses it as well.
302 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
304 $row = (object)$arrayData;
305 $rev = new Revision( $row, 0, $this->getMockTitle() );
306 $assertions( $this, $rev );
310 * @covers Revision::__construct
311 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
313 public function testConstructFromRowWithBadPageId() {
314 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
315 $this->overrideMwServices();
316 Wikimedia\
suppressWarnings();
317 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
318 $this->assertSame( 77777777, $rev->getPage() );
319 Wikimedia\restoreWarnings
();
322 public function provideGetRevisionText() {
323 yield
'Generic test' => [
324 'This is a goat of revision text.',
327 'old_text' => 'This is a goat of revision text.',
332 public function provideGetId() {
344 * @dataProvider provideGetId
345 * @covers Revision::getId
347 public function testGetId( $rowArray, $expectedId ) {
348 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
349 $this->assertEquals( $expectedId, $rev->getId() );
352 public function provideSetId() {
353 yield
[ '123', 123 ];
358 * @dataProvider provideSetId
359 * @covers Revision::setId
361 public function testSetId( $input, $expected ) {
362 $rev = new Revision( [], 0, $this->getMockTitle() );
363 $rev->setId( $input );
364 $this->assertSame( $expected, $rev->getId() );
367 public function provideSetUserIdAndName() {
368 yield
[ '123', 123, 'GOaT' ];
369 yield
[ 456, 456, 'GOaT' ];
373 * @dataProvider provideSetUserIdAndName
374 * @covers Revision::setUserIdAndName
376 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
377 $rev = new Revision( [], 0, $this->getMockTitle() );
378 $rev->setUserIdAndName( $inputId, $name );
379 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
380 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
383 public function provideGetTextId() {
385 yield
[ [ 'text_id' => '123' ], 123 ];
386 yield
[ [ 'text_id' => 456 ], 456 ];
390 * @dataProvider provideGetTextId
391 * @covers Revision::getTextId()
393 public function testGetTextId( $rowArray, $expected ) {
394 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
395 $this->assertSame( $expected, $rev->getTextId() );
398 public function provideGetParentId() {
400 yield
[ [ 'parent_id' => '123' ], 123 ];
401 yield
[ [ 'parent_id' => 456 ], 456 ];
405 * @dataProvider provideGetParentId
406 * @covers Revision::getParentId()
408 public function testGetParentId( $rowArray, $expected ) {
409 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
410 $this->assertSame( $expected, $rev->getParentId() );
414 * @covers Revision::getRevisionText
415 * @dataProvider provideGetRevisionText
417 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
420 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
423 public function provideGetRevisionTextWithZlibExtension() {
424 yield
'Generic gzip test' => [
425 'This is a small goat of revision text.',
427 'old_flags' => 'gzip',
428 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
434 * @covers Revision::getRevisionText
435 * @dataProvider provideGetRevisionTextWithZlibExtension
437 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
438 $this->checkPHPExtension( 'zlib' );
439 $this->testGetRevisionText( $expected, $rowData );
442 private function getWANObjectCache() {
443 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
447 * @return SqlBlobStore
449 private function getBlobStore() {
450 /** @var LoadBalancer $lb */
451 $lb = $this->getMockBuilder( LoadBalancer
::class )
452 ->disableOriginalConstructor()
455 $cache = $this->getWANObjectCache();
457 $blobStore = new SqlBlobStore( $lb, $cache );
461 private function mockBlobStoreFactory( $blobStore ) {
462 /** @var LoadBalancer $lb */
463 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
464 ->disableOriginalConstructor()
466 $factory->expects( $this->any() )
467 ->method( 'newBlobStore' )
468 ->willReturn( $blobStore );
469 $factory->expects( $this->any() )
470 ->method( 'newSqlBlobStore' )
471 ->willReturn( $blobStore );
476 * @return RevisionStore
478 private function getRevisionStore() {
479 /** @var LoadBalancer $lb */
480 $lb = $this->getMockBuilder( LoadBalancer
::class )
481 ->disableOriginalConstructor()
484 $cache = $this->getWANObjectCache();
486 $blobStore = new RevisionStore(
488 $this->getBlobStore(),
490 MediaWikiServices
::getInstance()->getCommentStore(),
491 MediaWikiServices
::getInstance()->getActorMigration()
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->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
615 $this->overrideMwServices();
616 $title = $this->getMockTitle();
619 'rev_id=page_latest',
620 'page_namespace' => $title->getNamespace(),
621 'page_title' => $title->getDBkey()
626 'rev_page' => $title->getArticleID(),
627 'rev_text_id' => '2',
628 'rev_timestamp' => '20171017114835',
629 'rev_user_text' => '127.0.0.1',
631 'rev_minor_edit' => '0',
632 'rev_deleted' => '0',
634 'rev_parent_id' => '1',
635 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
636 'rev_comment_text' => 'Goat Comment!',
637 'rev_comment_data' => null,
638 'rev_comment_cid' => null,
639 'rev_content_format' => 'GOATFORMAT',
640 'rev_content_model' => 'GOATMODEL',
643 $db = $this->getMock( IDatabase
::class );
644 $db->expects( $this->any() )
645 ->method( 'getDomainId' )
646 ->will( $this->returnValue( wfWikiID() ) );
647 $db->expects( $this->once() )
648 ->method( 'selectRow' )
650 $this->equalTo( [ 'revision', 'page', 'user' ] ),
651 // We don't really care about the fields are they come from the selectField methods
652 $this->isType( 'array' ),
653 $this->equalTo( $conditions ),
655 $this->stringContains( 'fetchRevisionRowFromConds' ),
656 // We don't really care about the options here
657 $this->isType( 'array' ),
658 // We don't really care about the join conds are they come from the joinCond methods
659 $this->isType( 'array' )
661 ->willReturn( $row );
663 $revision = Revision
::loadFromTitle( $db, $title );
665 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
666 $this->assertEquals( $row->rev_id
, $revision->getId() );
667 $this->assertEquals( $row->rev_len
, $revision->getSize() );
668 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
669 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
670 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
671 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
672 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
675 public function provideDecompressRevisionText() {
676 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
677 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
678 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
679 yield
'(no legacy encoding), string in with gzip flag returns string' => [
680 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
681 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
683 yield
'(no legacy encoding), string in with object flag returns false' => [
684 // gzip string below generated with serialize( 'JOJO' )
685 false, "s:4:\"JOJO\";", [ 'object' ], false,
687 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
689 // Using a TitleValue object as it has a getText method (which is needed)
690 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
694 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
696 // Using a TitleValue object as it has a getText method (which is needed)
697 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
698 [ 'object', 'gzip' ],
701 yield
'(ISO-8859-1 encoding), string in string out' => [
703 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
707 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
709 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
713 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
715 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
719 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
721 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
722 [ 'gzip', 'object' ],
728 * @dataProvider provideDecompressRevisionText
729 * @covers Revision::decompressRevisionText
731 * @param bool $legacyEncoding
733 * @param array $flags
734 * @param mixed $expected
736 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
737 $blobStore = $this->getBlobStore();
738 if ( $legacyEncoding ) {
739 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
742 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
745 Revision
::decompressRevisionText( $text, $flags )
750 * @covers Revision::getRevisionText
752 public function testGetRevisionText_returnsFalseWhenNoTextField() {
753 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
756 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
757 yield
'Just text' => [
758 (object)[ 'old_text' => 'SomeText' ],
762 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
763 yield
'gzip text' => [
765 'old_text' => "sttttr\002\022\000",
766 'old_flags' => 'gzip'
771 yield
'gzip text and different prefix' => [
773 'jojo_text' => "sttttr\002\022\000",
774 'jojo_flags' => 'gzip'
782 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
783 * @covers Revision::getRevisionText
785 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
790 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
793 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
794 yield
'Just some text' => [ 'someNonUrlText' ];
795 yield
'No second URL part' => [ 'someProtocol://' ];
799 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
800 * @covers Revision::getRevisionText
802 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
806 Revision
::getRevisionText(
809 'old_flags' => 'external',
816 * @covers Revision::getRevisionText
818 public function testGetRevisionText_external_noOldId() {
820 'ExternalStoreFactory',
821 new ExternalStoreFactory( [ 'ForTesting' ] )
825 Revision
::getRevisionText(
827 'old_text' => 'ForTesting://cluster1/12345',
828 'old_flags' => 'external,gzip',
835 * @covers Revision::getRevisionText
837 public function testGetRevisionText_external_oldId() {
838 $cache = $this->getWANObjectCache();
839 $this->setService( 'MainWANObjectCache', $cache );
842 'ExternalStoreFactory',
843 new ExternalStoreFactory( [ 'ForTesting' ] )
846 $lb = $this->getMockBuilder( LoadBalancer
::class )
847 ->disableOriginalConstructor()
850 $blobStore = new SqlBlobStore( $lb, $cache );
851 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
855 Revision
::getRevisionText(
857 'old_text' => 'ForTesting://cluster1/12345',
858 'old_flags' => 'external,gzip',
864 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
865 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
869 * @covers Revision::userJoinCond
871 public function testUserJoinCond() {
872 $this->hideDeprecated( 'Revision::userJoinCond' );
873 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
874 $this->overrideMwServices();
876 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
877 Revision
::userJoinCond()
882 * @covers Revision::pageJoinCond
884 public function testPageJoinCond() {
885 $this->hideDeprecated( 'Revision::pageJoinCond' );
887 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
888 Revision
::pageJoinCond()
892 private function overrideCommentStoreAndActorMigration() {
893 $mockStore = $this->getMockBuilder( CommentStore
::class )
894 ->disableOriginalConstructor()
896 $mockStore->expects( $this->any() )
897 ->method( 'getFields' )
898 ->willReturn( [ 'commentstore' => 'fields' ] );
899 $mockStore->expects( $this->any() )
900 ->method( 'getJoin' )
902 'tables' => [ 'commentstore' => 'table' ],
903 'fields' => [ 'commentstore' => 'field' ],
904 'joins' => [ 'commentstore' => 'join' ],
906 $this->setService( 'CommentStore', $mockStore );
908 $mockStore = $this->getMockBuilder( ActorMigration
::class )
909 ->disableOriginalConstructor()
911 $mockStore->expects( $this->any() )
912 ->method( 'getJoin' )
913 ->willReturnCallback( function ( $key ) {
914 $p = strtok( $key, '_' );
916 'tables' => [ 'actormigration' => 'table' ],
918 $p . '_user' => 'actormigration_user',
919 $p . '_user_text' => 'actormigration_user_text',
920 $p . '_actor' => 'actormigration_actor',
922 'joins' => [ 'actormigration' => 'join' ],
925 $this->setService( 'ActorMigration', $mockStore );
928 public function provideSelectFields() {
938 'rev_actor' => 'NULL',
944 'commentstore' => 'fields',
945 'rev_content_format',
958 'rev_actor' => 'NULL',
964 'commentstore' => 'fields',
970 * @dataProvider provideSelectFields
971 * @covers Revision::selectFields
973 public function testSelectFields( $contentHandlerUseDB, $expected ) {
974 $this->hideDeprecated( 'Revision::selectFields' );
975 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
976 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
977 $this->overrideCommentStoreAndActorMigration();
978 $this->assertEquals( $expected, Revision
::selectFields() );
981 public function provideSelectArchiveFields() {
992 'ar_actor' => 'NULL',
998 'commentstore' => 'fields',
1013 'ar_actor' => 'NULL',
1019 'commentstore' => 'fields',
1025 * @dataProvider provideSelectArchiveFields
1026 * @covers Revision::selectArchiveFields
1028 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1029 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1030 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1031 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
1032 $this->overrideCommentStoreAndActorMigration();
1033 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1037 * @covers Revision::selectTextFields
1039 public function testSelectTextFields() {
1040 $this->hideDeprecated( 'Revision::selectTextFields' );
1041 $this->assertEquals(
1046 Revision
::selectTextFields()
1051 * @covers Revision::selectPageFields
1053 public function testSelectPageFields() {
1054 $this->hideDeprecated( 'Revision::selectPageFields' );
1055 $this->assertEquals(
1064 Revision
::selectPageFields()
1069 * @covers Revision::selectUserFields
1071 public function testSelectUserFields() {
1072 $this->hideDeprecated( 'Revision::selectUserFields' );
1073 $this->assertEquals(
1077 Revision
::selectUserFields()
1081 public function provideGetArchiveQueryInfo() {
1082 yield
'wgContentHandlerUseDB false' => [
1084 'wgContentHandlerUseDB' => false,
1089 'commentstore' => 'table',
1090 'actormigration' => 'table',
1105 'commentstore' => 'field',
1106 'ar_user' => 'actormigration_user',
1107 'ar_user_text' => 'actormigration_user_text',
1108 'ar_actor' => 'actormigration_actor',
1110 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1113 yield
'wgContentHandlerUseDB true' => [
1115 'wgContentHandlerUseDB' => true,
1120 'commentstore' => 'table',
1121 'actormigration' => 'table',
1136 'commentstore' => 'field',
1137 'ar_user' => 'actormigration_user',
1138 'ar_user_text' => 'actormigration_user_text',
1139 'ar_actor' => 'actormigration_actor',
1140 'ar_content_format',
1143 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1149 * @covers Revision::getArchiveQueryInfo
1150 * @dataProvider provideGetArchiveQueryInfo
1152 public function testGetArchiveQueryInfo( $globals, $expected ) {
1153 $this->setMwGlobals( $globals );
1154 $this->overrideCommentStoreAndActorMigration();
1156 $revisionStore = $this->getRevisionStore();
1157 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1158 $this->setService( 'RevisionStore', $revisionStore );
1159 $this->assertEquals(
1161 Revision
::getArchiveQueryInfo()
1165 public function provideGetQueryInfo() {
1166 yield
'wgContentHandlerUseDB false, opts none' => [
1168 'wgContentHandlerUseDB' => false,
1172 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1183 'commentstore' => 'field',
1184 'rev_user' => 'actormigration_user',
1185 'rev_user_text' => 'actormigration_user_text',
1186 'rev_actor' => 'actormigration_actor',
1188 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1191 yield
'wgContentHandlerUseDB false, opts page' => [
1193 'wgContentHandlerUseDB' => false,
1197 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page' ],
1208 'commentstore' => 'field',
1209 'rev_user' => 'actormigration_user',
1210 'rev_user_text' => 'actormigration_user_text',
1211 'rev_actor' => 'actormigration_actor',
1222 [ 'page_id = rev_page' ],
1224 'commentstore' => 'join',
1225 'actormigration' => 'join',
1229 yield
'wgContentHandlerUseDB false, opts user' => [
1231 'wgContentHandlerUseDB' => false,
1235 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'user' ],
1246 'commentstore' => 'field',
1247 'rev_user' => 'actormigration_user',
1248 'rev_user_text' => 'actormigration_user_text',
1249 'rev_actor' => 'actormigration_actor',
1256 'actormigration_user != 0',
1257 'user_id = actormigration_user',
1260 'commentstore' => 'join',
1261 'actormigration' => 'join',
1265 yield
'wgContentHandlerUseDB false, opts text' => [
1267 'wgContentHandlerUseDB' => false,
1271 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'text' ],
1282 'commentstore' => 'field',
1283 'rev_user' => 'actormigration_user',
1284 'rev_user_text' => 'actormigration_user_text',
1285 'rev_actor' => 'actormigration_actor',
1292 [ 'rev_text_id=old_id' ],
1294 'commentstore' => 'join',
1295 'actormigration' => 'join',
1299 yield
'wgContentHandlerUseDB false, opts 3' => [
1301 'wgContentHandlerUseDB' => false,
1303 [ 'text', 'page', 'user' ],
1306 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page', 'user', 'text'
1318 'commentstore' => 'field',
1319 'rev_user' => 'actormigration_user',
1320 'rev_user_text' => 'actormigration_user_text',
1321 'rev_actor' => 'actormigration_actor',
1335 [ 'page_id = rev_page' ],
1340 'actormigration_user != 0',
1341 'user_id = actormigration_user',
1346 [ 'rev_text_id=old_id' ],
1348 'commentstore' => 'join',
1349 'actormigration' => 'join',
1353 yield
'wgContentHandlerUseDB true, opts none' => [
1355 'wgContentHandlerUseDB' => true,
1359 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1370 'commentstore' => 'field',
1371 'rev_user' => 'actormigration_user',
1372 'rev_user_text' => 'actormigration_user_text',
1373 'rev_actor' => 'actormigration_actor',
1374 'rev_content_format',
1375 'rev_content_model',
1377 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1383 * @covers Revision::getQueryInfo
1384 * @dataProvider provideGetQueryInfo
1386 public function testGetQueryInfo( $globals, $options, $expected ) {
1387 $this->setMwGlobals( $globals );
1388 $this->overrideCommentStoreAndActorMigration();
1390 $revisionStore = $this->getRevisionStore();
1391 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1392 $this->setService( 'RevisionStore', $revisionStore );
1394 $this->assertEquals(
1396 Revision
::getQueryInfo( $options )
1401 * @covers Revision::getSize
1403 public function testGetSize() {
1404 $title = $this->getMockTitle();
1406 $rec = new MutableRevisionRecord( $title );
1407 $rev = new Revision( $rec, 0, $title );
1409 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1411 $rec->setSize( 13 );
1412 $this->assertSame( 13, $rev->getSize() );
1416 * @covers Revision::getSize
1418 public function testGetSize_failure() {
1419 $title = $this->getMockTitle();
1421 $rec = $this->getMockBuilder( RevisionRecord
::class )
1422 ->disableOriginalConstructor()
1425 $rec->method( 'getSize' )
1426 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1428 $rev = new Revision( $rec, 0, $title );
1429 $this->assertNull( $rev->getSize() );
1433 * @covers Revision::getSha1
1435 public function testGetSha1() {
1436 $title = $this->getMockTitle();
1438 $rec = new MutableRevisionRecord( $title );
1439 $rev = new Revision( $rec, 0, $title );
1441 $emptyHash = SlotRecord
::base36Sha1( '' );
1442 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1444 $rec->setSha1( 'deadbeef' );
1445 $this->assertSame( 'deadbeef', $rev->getSha1() );
1449 * @covers Revision::getSha1
1451 public function testGetSha1_failure() {
1452 $title = $this->getMockTitle();
1454 $rec = $this->getMockBuilder( RevisionRecord
::class )
1455 ->disableOriginalConstructor()
1458 $rec->method( 'getSha1' )
1459 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1461 $rev = new Revision( $rec, 0, $title );
1462 $this->assertNull( $rev->getSha1() );
1466 * @covers Revision::getContent
1468 public function testGetContent() {
1469 $title = $this->getMockTitle();
1471 $rec = new MutableRevisionRecord( $title );
1472 $rev = new Revision( $rec, 0, $title );
1474 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1476 $content = new TextContent( 'Hello Kittens!' );
1477 $rec->setContent( 'main', $content );
1478 $this->assertSame( $content, $rev->getContent() );
1482 * @covers Revision::getContent
1484 public function testGetContent_failure() {
1485 $title = $this->getMockTitle();
1487 $rec = $this->getMockBuilder( RevisionRecord
::class )
1488 ->disableOriginalConstructor()
1491 $rec->method( 'getContent' )
1492 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1494 $rev = new Revision( $rec, 0, $title );
1495 $this->assertNull( $rev->getContent() );