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 setUp() {
22 $this->setMwGlobals( 'wgMultiContentRevisionSchemaMigrationStage', MIGRATION_OLD
);
25 public function provideConstructFromArray() {
26 yield
'with text' => [
28 'text' => 'hello world.',
29 'content_model' => CONTENT_MODEL_JAVASCRIPT
32 yield
'with content' => [
34 'content' => new JavaScriptContent( 'hellow world.' )
37 // FIXME: test with and without user ID, and with a user object.
38 // We can't prepare that here though, since we don't yet have a dummy DB
42 * @param string $model
45 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
46 $mock = $this->getMockBuilder( Title
::class )
47 ->disableOriginalConstructor()
49 $mock->expects( $this->any() )
50 ->method( 'getNamespace' )
51 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
52 $mock->expects( $this->any() )
53 ->method( 'getPrefixedText' )
54 ->will( $this->returnValue( 'RevisionTest' ) );
55 $mock->expects( $this->any() )
56 ->method( 'getDBkey' )
57 ->will( $this->returnValue( 'RevisionTest' ) );
58 $mock->expects( $this->any() )
59 ->method( 'getArticleID' )
60 ->will( $this->returnValue( 23 ) );
61 $mock->expects( $this->any() )
62 ->method( 'getContentModel' )
63 ->will( $this->returnValue( $model ) );
69 * @dataProvider provideConstructFromArray
70 * @covers Revision::__construct
71 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
73 public function testConstructFromArray( $rowArray ) {
74 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
75 $this->assertNotNull( $rev->getContent(), 'no content object available' );
76 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
77 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
81 * @covers Revision::__construct
82 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
84 public function testConstructFromEmptyArray() {
85 $rev = new Revision( [], 0, $this->getMockTitle() );
86 $this->assertNull( $rev->getContent(), 'no content object should be available' );
90 * @covers Revision::__construct
91 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
93 public function testConstructFromArrayWithBadPageId() {
94 Wikimedia\
suppressWarnings();
95 $rev = new Revision( [ 'page' => 77777777 ] );
96 $this->assertSame( 77777777, $rev->getPage() );
97 Wikimedia\restoreWarnings
();
100 public function provideConstructFromArray_userSetAsExpected() {
101 yield
'no user defaults to wgUser' => [
103 'content' => new JavaScriptContent( 'hello world.' ),
108 yield
'user text and id' => [
110 'content' => new JavaScriptContent( 'hello world.' ),
111 'user_text' => 'SomeTextUserName',
118 yield
'user text only' => [
120 'content' => new JavaScriptContent( 'hello world.' ),
121 'user_text' => '111.111.111.111',
129 * @dataProvider provideConstructFromArray_userSetAsExpected
130 * @covers Revision::__construct
131 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
133 * @param array $rowArray
134 * @param mixed $expectedUserId null to expect the current wgUser ID
135 * @param mixed $expectedUserName null to expect the current wgUser name
137 public function testConstructFromArray_userSetAsExpected(
142 $testUser = $this->getTestUser()->getUser();
143 $this->setMwGlobals( 'wgUser', $testUser );
144 if ( $expectedUserId === null ) {
145 $expectedUserId = $testUser->getId();
147 if ( $expectedUserName === null ) {
148 $expectedUserName = $testUser->getName();
151 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
152 $this->assertEquals( $expectedUserId, $rev->getUser() );
153 $this->assertEquals( $expectedUserName, $rev->getUserText() );
156 public function provideConstructFromArrayThrowsExceptions() {
157 yield
'content and text_id both not empty' => [
159 'content' => new WikitextContent( 'GOAT' ),
160 'text_id' => 'someid',
162 new MWException( 'Text already stored in external store (id someid),' )
164 yield
'with bad content object (class)' => [
165 [ 'content' => new stdClass() ],
166 new MWException( 'content field must contain a Content object' )
168 yield
'with bad content object (string)' => [
169 [ 'content' => 'ImAGoat' ],
170 new MWException( 'content field must contain a Content object' )
172 yield
'bad row format' => [
173 'imastring, not a row',
174 new InvalidArgumentException(
175 '$row must be a row object, an associative array, or a RevisionRecord'
181 * @dataProvider provideConstructFromArrayThrowsExceptions
182 * @covers Revision::__construct
183 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
185 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
186 $this->setExpectedException(
187 get_class( $expectedException ),
188 $expectedException->getMessage(),
189 $expectedException->getCode()
191 new Revision( $rowArray, 0, $this->getMockTitle() );
195 * @covers Revision::__construct
196 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
198 public function testConstructFromNothing() {
199 $this->setExpectedException(
200 InvalidArgumentException
::class
205 public function provideConstructFromRow() {
206 yield
'Full construction' => [
210 'rev_text_id' => '2',
211 'rev_timestamp' => '20171017114835',
212 'rev_user_text' => '127.0.0.1',
214 'rev_minor_edit' => '0',
215 'rev_deleted' => '0',
217 'rev_parent_id' => '1',
218 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
219 'rev_comment_text' => 'Goat Comment!',
220 'rev_comment_data' => null,
221 'rev_comment_cid' => null,
222 'rev_content_format' => 'GOATFORMAT',
223 'rev_content_model' => 'GOATMODEL',
225 function ( RevisionTest
$testCase, Revision
$rev ) {
226 $testCase->assertSame( 42, $rev->getId() );
227 $testCase->assertSame( 23, $rev->getPage() );
228 $testCase->assertSame( 2, $rev->getTextId() );
229 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
230 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
231 $testCase->assertSame( 0, $rev->getUser() );
232 $testCase->assertSame( false, $rev->isMinor() );
233 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
234 $testCase->assertSame( 46, $rev->getSize() );
235 $testCase->assertSame( 1, $rev->getParentId() );
236 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
237 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
238 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
239 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
242 yield
'default field values' => [
246 'rev_text_id' => '2',
247 'rev_timestamp' => '20171017114835',
248 'rev_user_text' => '127.0.0.1',
250 'rev_minor_edit' => '0',
251 'rev_deleted' => '0',
252 'rev_comment_text' => 'Goat Comment!',
253 'rev_comment_data' => null,
254 'rev_comment_cid' => null,
256 function ( RevisionTest
$testCase, Revision
$rev ) {
257 // parent ID may be null
258 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
261 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
262 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
263 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
264 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
265 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
266 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
269 $testCase->assertNotNull( $rev->getSize(), 'size' );
270 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
272 // NOTE: model and format will be detected based on the namespace of the (mock) title
273 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
274 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
280 * @dataProvider provideConstructFromRow
281 * @covers Revision::__construct
282 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
284 public function testConstructFromRow( array $arrayData, $assertions ) {
285 $data = 'Hello goat.'; // needs to match model and format
287 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
288 ->disableOriginalConstructor()
291 $blobStore->method( 'getBlob' )
292 ->will( $this->returnValue( $data ) );
294 $blobStore->method( 'getTextIdFromAddress' )
295 ->will( $this->returnCallback(
296 function ( $address ) {
297 // Turn "tt:1234" into 12345.
298 // Note that this must be functional so we can test getTextId().
299 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
300 $parts = explode( ':', $address );
301 return (int)array_pop( $parts );
305 // Note override internal service, so RevisionStore uses it as well.
306 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
308 $row = (object)$arrayData;
309 $rev = new Revision( $row, 0, $this->getMockTitle() );
310 $assertions( $this, $rev );
314 * @covers Revision::__construct
315 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
317 public function testConstructFromRowWithBadPageId() {
318 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
319 $this->overrideMwServices();
320 Wikimedia\
suppressWarnings();
321 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
322 $this->assertSame( 77777777, $rev->getPage() );
323 Wikimedia\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 public function provideGetRevisionTextWithZlibExtension_badData() {
447 yield
'Generic gzip test' => [
448 'This is a small goat of revision text.',
450 'old_flags' => 'gzip',
451 'old_text' => 'DEAD BEEF',
457 * @covers Revision::getRevisionText
458 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
460 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
461 $this->checkPHPExtension( 'zlib' );
462 Wikimedia\
suppressWarnings();
464 Revision
::getRevisionText(
468 Wikimedia\
suppressWarnings( true );
471 private function getWANObjectCache() {
472 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
476 * @return SqlBlobStore
478 private function getBlobStore() {
479 /** @var LoadBalancer $lb */
480 $lb = $this->getMockBuilder( LoadBalancer
::class )
481 ->disableOriginalConstructor()
484 $cache = $this->getWANObjectCache();
486 $blobStore = new SqlBlobStore( $lb, $cache );
490 private function mockBlobStoreFactory( $blobStore ) {
491 /** @var LoadBalancer $lb */
492 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
493 ->disableOriginalConstructor()
495 $factory->expects( $this->any() )
496 ->method( 'newBlobStore' )
497 ->willReturn( $blobStore );
498 $factory->expects( $this->any() )
499 ->method( 'newSqlBlobStore' )
500 ->willReturn( $blobStore );
505 * @return RevisionStore
507 private function getRevisionStore() {
508 /** @var LoadBalancer $lb */
509 $lb = $this->getMockBuilder( LoadBalancer
::class )
510 ->disableOriginalConstructor()
513 $cache = $this->getWANObjectCache();
515 $blobStore = new RevisionStore(
517 $this->getBlobStore(),
519 MediaWikiServices
::getInstance()->getCommentStore(),
520 MediaWikiServices
::getInstance()->getContentModelStore(),
521 MediaWikiServices
::getInstance()->getSlotRoleStore(),
523 MediaWikiServices
::getInstance()->getActorMigration()
528 public function provideGetRevisionTextWithLegacyEncoding() {
529 yield
'Utf8Native' => [
530 "Wiki est l'\xc3\xa9cole superieur !",
534 'old_flags' => 'utf-8',
535 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
538 yield
'Utf8Legacy' => [
539 "Wiki est l'\xc3\xa9cole superieur !",
544 'old_text' => "Wiki est l'\xe9cole superieur !",
550 * @covers Revision::getRevisionText
551 * @dataProvider provideGetRevisionTextWithLegacyEncoding
553 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
554 $blobStore = $this->getBlobStore();
555 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
556 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
558 $this->testGetRevisionText( $expected, $rowData );
561 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
564 * Do not set the external flag!
565 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
567 yield
'Utf8NativeGzip' => [
568 "Wiki est l'\xc3\xa9cole superieur !",
572 'old_flags' => 'gzip,utf-8',
573 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
576 yield
'Utf8LegacyGzip' => [
577 "Wiki est l'\xc3\xa9cole superieur !",
581 'old_flags' => 'gzip',
582 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
588 * @covers Revision::getRevisionText
589 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
591 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
592 $this->checkPHPExtension( 'zlib' );
594 $blobStore = $this->getBlobStore();
595 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
596 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
598 $this->testGetRevisionText( $expected, $rowData );
602 * @covers Revision::compressRevisionText
604 public function testCompressRevisionTextUtf8() {
606 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
607 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
608 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
609 "Flags should contain 'utf-8'" );
610 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
611 "Flags should not contain 'gzip'" );
612 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
613 $row->old_text
, "Direct check" );
614 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
615 Revision
::getRevisionText( $row ), "getRevisionText" );
619 * @covers Revision::compressRevisionText
621 public function testCompressRevisionTextUtf8Gzip() {
622 $this->checkPHPExtension( 'zlib' );
624 $blobStore = $this->getBlobStore();
625 $blobStore->setCompressBlobs( true );
626 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
629 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
630 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
631 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
632 "Flags should contain 'utf-8'" );
633 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
634 "Flags should contain 'gzip'" );
635 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
636 gzinflate( $row->old_text
), "Direct check" );
637 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
638 Revision
::getRevisionText( $row ), "getRevisionText" );
642 * @covers Revision::loadFromTitle
644 public function testLoadFromTitle() {
645 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
646 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
647 $this->overrideMwServices();
648 $title = $this->getMockTitle();
651 'rev_id=page_latest',
652 'page_namespace' => $title->getNamespace(),
653 'page_title' => $title->getDBkey()
658 'rev_page' => $title->getArticleID(),
659 'rev_text_id' => '2',
660 'rev_timestamp' => '20171017114835',
661 'rev_user_text' => '127.0.0.1',
663 'rev_minor_edit' => '0',
664 'rev_deleted' => '0',
666 'rev_parent_id' => '1',
667 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
668 'rev_comment_text' => 'Goat Comment!',
669 'rev_comment_data' => null,
670 'rev_comment_cid' => null,
671 'rev_content_format' => 'GOATFORMAT',
672 'rev_content_model' => 'GOATMODEL',
675 $db = $this->getMock( IDatabase
::class );
676 $db->expects( $this->any() )
677 ->method( 'getDomainId' )
678 ->will( $this->returnValue( wfWikiID() ) );
679 $db->expects( $this->once() )
680 ->method( 'selectRow' )
682 $this->equalTo( [ 'revision', 'page', 'user' ] ),
683 // We don't really care about the fields are they come from the selectField methods
684 $this->isType( 'array' ),
685 $this->equalTo( $conditions ),
687 $this->stringContains( 'fetchRevisionRowFromConds' ),
688 // We don't really care about the options here
689 $this->isType( 'array' ),
690 // We don't really care about the join conds are they come from the joinCond methods
691 $this->isType( 'array' )
693 ->willReturn( $row );
695 $revision = Revision
::loadFromTitle( $db, $title );
697 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
698 $this->assertEquals( $row->rev_id
, $revision->getId() );
699 $this->assertEquals( $row->rev_len
, $revision->getSize() );
700 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
701 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
702 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
703 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
704 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
707 public function provideDecompressRevisionText() {
708 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
709 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
710 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
711 yield
'(no legacy encoding), string in with gzip flag returns string' => [
712 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
713 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
715 yield
'(no legacy encoding), string in with object flag returns false' => [
716 // gzip string below generated with serialize( 'JOJO' )
717 false, "s:4:\"JOJO\";", [ 'object' ], false,
719 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
721 // Using a TitleValue object as it has a getText method (which is needed)
722 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
726 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
728 // Using a TitleValue object as it has a getText method (which is needed)
729 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
730 [ 'object', 'gzip' ],
733 yield
'(ISO-8859-1 encoding), string in string out' => [
735 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
739 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
741 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
745 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
747 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
751 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
753 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
754 [ 'gzip', 'object' ],
760 * @dataProvider provideDecompressRevisionText
761 * @covers Revision::decompressRevisionText
763 * @param bool $legacyEncoding
765 * @param array $flags
766 * @param mixed $expected
768 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
769 $blobStore = $this->getBlobStore();
770 if ( $legacyEncoding ) {
771 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
774 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
777 Revision
::decompressRevisionText( $text, $flags )
782 * @covers Revision::getRevisionText
784 public function testGetRevisionText_returnsFalseWhenNoTextField() {
785 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
788 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
789 yield
'Just text' => [
790 (object)[ 'old_text' => 'SomeText' ],
794 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
795 yield
'gzip text' => [
797 'old_text' => "sttttr\002\022\000",
798 'old_flags' => 'gzip'
803 yield
'gzip text and different prefix' => [
805 'jojo_text' => "sttttr\002\022\000",
806 'jojo_flags' => 'gzip'
814 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
815 * @covers Revision::getRevisionText
817 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
822 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
825 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
826 yield
'Just some text' => [ 'someNonUrlText' ];
827 yield
'No second URL part' => [ 'someProtocol://' ];
831 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
832 * @covers Revision::getRevisionText
834 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
837 Wikimedia\
suppressWarnings();
839 Revision
::getRevisionText(
842 'old_flags' => 'external',
846 Wikimedia\
suppressWarnings( true );
850 * @covers Revision::getRevisionText
852 public function testGetRevisionText_external_noOldId() {
854 'ExternalStoreFactory',
855 new ExternalStoreFactory( [ 'ForTesting' ] )
859 Revision
::getRevisionText(
861 'old_text' => 'ForTesting://cluster1/12345',
862 'old_flags' => 'external,gzip',
869 * @covers Revision::getRevisionText
871 public function testGetRevisionText_external_oldId() {
872 $cache = $this->getWANObjectCache();
873 $this->setService( 'MainWANObjectCache', $cache );
876 'ExternalStoreFactory',
877 new ExternalStoreFactory( [ 'ForTesting' ] )
880 $lb = $this->getMockBuilder( LoadBalancer
::class )
881 ->disableOriginalConstructor()
884 $blobStore = new SqlBlobStore( $lb, $cache );
885 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
889 Revision
::getRevisionText(
891 'old_text' => 'ForTesting://cluster1/12345',
892 'old_flags' => 'external,gzip',
898 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
899 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
903 * @covers Revision::userJoinCond
905 public function testUserJoinCond() {
906 $this->hideDeprecated( 'Revision::userJoinCond' );
907 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
908 $this->overrideMwServices();
910 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
911 Revision
::userJoinCond()
916 * @covers Revision::pageJoinCond
918 public function testPageJoinCond() {
919 $this->hideDeprecated( 'Revision::pageJoinCond' );
921 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
922 Revision
::pageJoinCond()
926 private function overrideCommentStoreAndActorMigration() {
927 $mockStore = $this->getMockBuilder( CommentStore
::class )
928 ->disableOriginalConstructor()
930 $mockStore->expects( $this->any() )
931 ->method( 'getFields' )
932 ->willReturn( [ 'commentstore' => 'fields' ] );
933 $mockStore->expects( $this->any() )
934 ->method( 'getJoin' )
936 'tables' => [ 'commentstore' => 'table' ],
937 'fields' => [ 'commentstore' => 'field' ],
938 'joins' => [ 'commentstore' => 'join' ],
940 $this->setService( 'CommentStore', $mockStore );
942 $mockStore = $this->getMockBuilder( ActorMigration
::class )
943 ->disableOriginalConstructor()
945 $mockStore->expects( $this->any() )
946 ->method( 'getJoin' )
947 ->willReturnCallback( function ( $key ) {
948 $p = strtok( $key, '_' );
950 'tables' => [ 'actormigration' => 'table' ],
952 $p . '_user' => 'actormigration_user',
953 $p . '_user_text' => 'actormigration_user_text',
954 $p . '_actor' => 'actormigration_actor',
956 'joins' => [ 'actormigration' => 'join' ],
959 $this->setService( 'ActorMigration', $mockStore );
962 public function provideSelectFields() {
972 'rev_actor' => 'NULL',
978 'commentstore' => 'fields',
979 'rev_content_format',
992 'rev_actor' => 'NULL',
998 'commentstore' => 'fields',
1004 * @dataProvider provideSelectFields
1005 * @covers Revision::selectFields
1007 public function testSelectFields( $contentHandlerUseDB, $expected ) {
1008 $this->hideDeprecated( 'Revision::selectFields' );
1009 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1010 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
1011 $this->overrideCommentStoreAndActorMigration();
1012 $this->assertEquals( $expected, Revision
::selectFields() );
1015 public function provideSelectArchiveFields() {
1026 'ar_actor' => 'NULL',
1032 'commentstore' => 'fields',
1033 'ar_content_format',
1047 'ar_actor' => 'NULL',
1053 'commentstore' => 'fields',
1059 * @dataProvider provideSelectArchiveFields
1060 * @covers Revision::selectArchiveFields
1062 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1063 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1064 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1065 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
1066 $this->overrideCommentStoreAndActorMigration();
1067 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1071 * @covers Revision::selectTextFields
1073 public function testSelectTextFields() {
1074 $this->hideDeprecated( 'Revision::selectTextFields' );
1075 $this->assertEquals(
1080 Revision
::selectTextFields()
1085 * @covers Revision::selectPageFields
1087 public function testSelectPageFields() {
1088 $this->hideDeprecated( 'Revision::selectPageFields' );
1089 $this->assertEquals(
1098 Revision
::selectPageFields()
1103 * @covers Revision::selectUserFields
1105 public function testSelectUserFields() {
1106 $this->hideDeprecated( 'Revision::selectUserFields' );
1107 $this->assertEquals(
1111 Revision
::selectUserFields()
1115 public function provideGetArchiveQueryInfo() {
1116 yield
'wgContentHandlerUseDB false' => [
1118 'wgContentHandlerUseDB' => false,
1123 'commentstore' => 'table',
1124 'actormigration' => 'table',
1139 'commentstore' => 'field',
1140 'ar_user' => 'actormigration_user',
1141 'ar_user_text' => 'actormigration_user_text',
1142 'ar_actor' => 'actormigration_actor',
1144 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1147 yield
'wgContentHandlerUseDB true' => [
1149 'wgContentHandlerUseDB' => true,
1154 'commentstore' => 'table',
1155 'actormigration' => 'table',
1170 'commentstore' => 'field',
1171 'ar_user' => 'actormigration_user',
1172 'ar_user_text' => 'actormigration_user_text',
1173 'ar_actor' => 'actormigration_actor',
1174 'ar_content_format',
1177 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1183 * @covers Revision::getArchiveQueryInfo
1184 * @dataProvider provideGetArchiveQueryInfo
1186 public function testGetArchiveQueryInfo( $globals, $expected ) {
1187 $this->setMwGlobals( $globals );
1188 $this->overrideCommentStoreAndActorMigration();
1190 $revisionStore = $this->getRevisionStore();
1191 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1192 $this->setService( 'RevisionStore', $revisionStore );
1194 $queryInfo = Revision
::getArchiveQueryInfo();
1196 $this->assertArrayEqualsIgnoringIntKeyOrder(
1197 $expected['tables'],
1198 $queryInfo['tables']
1200 $this->assertArrayEqualsIgnoringIntKeyOrder(
1201 $expected['fields'],
1202 $queryInfo['fields']
1204 $this->assertArrayEqualsIgnoringIntKeyOrder(
1211 * Assert that the two arrays passed are equal, ignoring the order of the values that integer
1214 * Note: Failures of this assertion can be slightly confusing as the arrays are actually
1215 * split into a string key array and an int key array before assertions occur.
1217 * @param array $expected
1218 * @param array $actual
1220 private function assertArrayEqualsIgnoringIntKeyOrder( array $expected, array $actual ) {
1221 $this->objectAssociativeSort( $expected );
1222 $this->objectAssociativeSort( $actual );
1224 // Separate the int key values from the string key values so that assertion failures are
1225 // easier to understand.
1226 $expectedIntKeyValues = [];
1227 $actualIntKeyValues = [];
1229 // Remove all int keys and re add them at the end after sorting by value
1230 // This will result in all int keys being in the same order with same ints at the end of
1232 foreach ( $expected as $key => $value ) {
1233 if ( is_int( $key ) ) {
1234 unset( $expected[$key] );
1235 $expectedIntKeyValues[] = $value;
1238 foreach ( $actual as $key => $value ) {
1239 if ( is_int( $key ) ) {
1240 unset( $actual[$key] );
1241 $actualIntKeyValues[] = $value;
1245 $this->assertArrayEquals( $expected, $actual, false, true );
1246 $this->assertArrayEquals( $expectedIntKeyValues, $actualIntKeyValues, false, true );
1249 public function provideGetQueryInfo() {
1250 yield
'wgContentHandlerUseDB false, opts none' => [
1252 'wgContentHandlerUseDB' => false,
1256 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1267 'commentstore' => 'field',
1268 'rev_user' => 'actormigration_user',
1269 'rev_user_text' => 'actormigration_user_text',
1270 'rev_actor' => 'actormigration_actor',
1272 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1275 yield
'wgContentHandlerUseDB false, opts page' => [
1277 'wgContentHandlerUseDB' => false,
1281 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page' ],
1292 'commentstore' => 'field',
1293 'rev_user' => 'actormigration_user',
1294 'rev_user_text' => 'actormigration_user_text',
1295 'rev_actor' => 'actormigration_actor',
1306 [ 'page_id = rev_page' ],
1308 'commentstore' => 'join',
1309 'actormigration' => 'join',
1313 yield
'wgContentHandlerUseDB false, opts user' => [
1315 'wgContentHandlerUseDB' => false,
1319 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'user' ],
1330 'commentstore' => 'field',
1331 'rev_user' => 'actormigration_user',
1332 'rev_user_text' => 'actormigration_user_text',
1333 'rev_actor' => 'actormigration_actor',
1340 'actormigration_user != 0',
1341 'user_id = actormigration_user',
1344 'commentstore' => 'join',
1345 'actormigration' => 'join',
1349 yield
'wgContentHandlerUseDB false, opts text' => [
1351 'wgContentHandlerUseDB' => false,
1355 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'text' ],
1366 'commentstore' => 'field',
1367 'rev_user' => 'actormigration_user',
1368 'rev_user_text' => 'actormigration_user_text',
1369 'rev_actor' => 'actormigration_actor',
1376 [ 'rev_text_id=old_id' ],
1378 'commentstore' => 'join',
1379 'actormigration' => 'join',
1383 yield
'wgContentHandlerUseDB false, opts 3' => [
1385 'wgContentHandlerUseDB' => false,
1387 [ 'text', 'page', 'user' ],
1390 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page', 'user', 'text'
1402 'commentstore' => 'field',
1403 'rev_user' => 'actormigration_user',
1404 'rev_user_text' => 'actormigration_user_text',
1405 'rev_actor' => 'actormigration_actor',
1419 [ 'page_id = rev_page' ],
1424 'actormigration_user != 0',
1425 'user_id = actormigration_user',
1430 [ 'rev_text_id=old_id' ],
1432 'commentstore' => 'join',
1433 'actormigration' => 'join',
1437 yield
'wgContentHandlerUseDB true, opts none' => [
1439 'wgContentHandlerUseDB' => true,
1443 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1454 'commentstore' => 'field',
1455 'rev_user' => 'actormigration_user',
1456 'rev_user_text' => 'actormigration_user_text',
1457 'rev_actor' => 'actormigration_actor',
1458 'rev_content_format',
1459 'rev_content_model',
1461 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1467 * @covers Revision::getQueryInfo
1468 * @dataProvider provideGetQueryInfo
1470 public function testGetQueryInfo( $globals, $options, $expected ) {
1471 $this->setMwGlobals( $globals );
1472 $this->overrideCommentStoreAndActorMigration();
1474 $revisionStore = $this->getRevisionStore();
1475 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1476 $this->setService( 'RevisionStore', $revisionStore );
1478 $queryInfo = Revision
::getQueryInfo( $options );
1480 $this->assertArrayEqualsIgnoringIntKeyOrder(
1481 $expected['tables'],
1482 $queryInfo['tables']
1484 $this->assertArrayEqualsIgnoringIntKeyOrder(
1485 $expected['fields'],
1486 $queryInfo['fields']
1488 $this->assertArrayEqualsIgnoringIntKeyOrder(
1495 * @covers Revision::getSize
1497 public function testGetSize() {
1498 $title = $this->getMockTitle();
1500 $rec = new MutableRevisionRecord( $title );
1501 $rev = new Revision( $rec, 0, $title );
1503 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1505 $rec->setSize( 13 );
1506 $this->assertSame( 13, $rev->getSize() );
1510 * @covers Revision::getSize
1512 public function testGetSize_failure() {
1513 $title = $this->getMockTitle();
1515 $rec = $this->getMockBuilder( RevisionRecord
::class )
1516 ->disableOriginalConstructor()
1519 $rec->method( 'getSize' )
1520 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1522 $rev = new Revision( $rec, 0, $title );
1523 $this->assertNull( $rev->getSize() );
1527 * @covers Revision::getSha1
1529 public function testGetSha1() {
1530 $title = $this->getMockTitle();
1532 $rec = new MutableRevisionRecord( $title );
1533 $rev = new Revision( $rec, 0, $title );
1535 $emptyHash = SlotRecord
::base36Sha1( '' );
1536 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1538 $rec->setSha1( 'deadbeef' );
1539 $this->assertSame( 'deadbeef', $rev->getSha1() );
1543 * @covers Revision::getSha1
1545 public function testGetSha1_failure() {
1546 $title = $this->getMockTitle();
1548 $rec = $this->getMockBuilder( RevisionRecord
::class )
1549 ->disableOriginalConstructor()
1552 $rec->method( 'getSha1' )
1553 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1555 $rev = new Revision( $rec, 0, $title );
1556 $this->assertNull( $rev->getSha1() );
1560 * @covers Revision::getContent
1562 public function testGetContent() {
1563 $title = $this->getMockTitle();
1565 $rec = new MutableRevisionRecord( $title );
1566 $rev = new Revision( $rec, 0, $title );
1568 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1570 $content = new TextContent( 'Hello Kittens!' );
1571 $rec->setContent( 'main', $content );
1572 $this->assertSame( $content, $rev->getContent() );
1576 * @covers Revision::getContent
1578 public function testGetContent_failure() {
1579 $title = $this->getMockTitle();
1581 $rec = $this->getMockBuilder( RevisionRecord
::class )
1582 ->disableOriginalConstructor()
1585 $rec->method( 'getContent' )
1586 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1588 $rev = new Revision( $rec, 0, $title );
1589 $this->assertNull( $rev->getContent() );