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 public function provideGetRevisionTextWithZlibExtension_badData() {
443 yield
'Generic gzip test' => [
444 'This is a small goat of revision text.',
446 'old_flags' => 'gzip',
447 'old_text' => 'DEAD BEEF',
453 * @covers Revision::getRevisionText
454 * @dataProvider provideGetRevisionTextWithZlibExtension_badData
456 public function testGetRevisionWithZlibExtension_badData( $expected, $rowData ) {
457 $this->checkPHPExtension( 'zlib' );
458 Wikimedia\
suppressWarnings();
460 Revision
::getRevisionText(
464 Wikimedia\
suppressWarnings( true );
467 private function getWANObjectCache() {
468 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
472 * @return SqlBlobStore
474 private function getBlobStore() {
475 /** @var LoadBalancer $lb */
476 $lb = $this->getMockBuilder( LoadBalancer
::class )
477 ->disableOriginalConstructor()
480 $cache = $this->getWANObjectCache();
482 $blobStore = new SqlBlobStore( $lb, $cache );
486 private function mockBlobStoreFactory( $blobStore ) {
487 /** @var LoadBalancer $lb */
488 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
489 ->disableOriginalConstructor()
491 $factory->expects( $this->any() )
492 ->method( 'newBlobStore' )
493 ->willReturn( $blobStore );
494 $factory->expects( $this->any() )
495 ->method( 'newSqlBlobStore' )
496 ->willReturn( $blobStore );
501 * @return RevisionStore
503 private function getRevisionStore() {
504 /** @var LoadBalancer $lb */
505 $lb = $this->getMockBuilder( LoadBalancer
::class )
506 ->disableOriginalConstructor()
509 $cache = $this->getWANObjectCache();
511 $blobStore = new RevisionStore(
513 $this->getBlobStore(),
515 MediaWikiServices
::getInstance()->getCommentStore(),
516 MediaWikiServices
::getInstance()->getActorMigration()
521 public function provideGetRevisionTextWithLegacyEncoding() {
522 yield
'Utf8Native' => [
523 "Wiki est l'\xc3\xa9cole superieur !",
527 'old_flags' => 'utf-8',
528 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
531 yield
'Utf8Legacy' => [
532 "Wiki est l'\xc3\xa9cole superieur !",
537 'old_text' => "Wiki est l'\xe9cole superieur !",
543 * @covers Revision::getRevisionText
544 * @dataProvider provideGetRevisionTextWithLegacyEncoding
546 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
547 $blobStore = $this->getBlobStore();
548 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
549 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
551 $this->testGetRevisionText( $expected, $rowData );
554 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
557 * Do not set the external flag!
558 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
560 yield
'Utf8NativeGzip' => [
561 "Wiki est l'\xc3\xa9cole superieur !",
565 'old_flags' => 'gzip,utf-8',
566 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
569 yield
'Utf8LegacyGzip' => [
570 "Wiki est l'\xc3\xa9cole superieur !",
574 'old_flags' => 'gzip',
575 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
581 * @covers Revision::getRevisionText
582 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
584 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
585 $this->checkPHPExtension( 'zlib' );
587 $blobStore = $this->getBlobStore();
588 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
589 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
591 $this->testGetRevisionText( $expected, $rowData );
595 * @covers Revision::compressRevisionText
597 public function testCompressRevisionTextUtf8() {
599 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
600 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
601 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
602 "Flags should contain 'utf-8'" );
603 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
604 "Flags should not contain 'gzip'" );
605 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
606 $row->old_text
, "Direct check" );
607 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
608 Revision
::getRevisionText( $row ), "getRevisionText" );
612 * @covers Revision::compressRevisionText
614 public function testCompressRevisionTextUtf8Gzip() {
615 $this->checkPHPExtension( 'zlib' );
617 $blobStore = $this->getBlobStore();
618 $blobStore->setCompressBlobs( true );
619 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
622 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
623 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
624 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
625 "Flags should contain 'utf-8'" );
626 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
627 "Flags should contain 'gzip'" );
628 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
629 gzinflate( $row->old_text
), "Direct check" );
630 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
631 Revision
::getRevisionText( $row ), "getRevisionText" );
635 * @covers Revision::loadFromTitle
637 public function testLoadFromTitle() {
638 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
639 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
640 $this->overrideMwServices();
641 $title = $this->getMockTitle();
644 'rev_id=page_latest',
645 'page_namespace' => $title->getNamespace(),
646 'page_title' => $title->getDBkey()
651 'rev_page' => $title->getArticleID(),
652 'rev_text_id' => '2',
653 'rev_timestamp' => '20171017114835',
654 'rev_user_text' => '127.0.0.1',
656 'rev_minor_edit' => '0',
657 'rev_deleted' => '0',
659 'rev_parent_id' => '1',
660 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
661 'rev_comment_text' => 'Goat Comment!',
662 'rev_comment_data' => null,
663 'rev_comment_cid' => null,
664 'rev_content_format' => 'GOATFORMAT',
665 'rev_content_model' => 'GOATMODEL',
668 $db = $this->getMock( IDatabase
::class );
669 $db->expects( $this->any() )
670 ->method( 'getDomainId' )
671 ->will( $this->returnValue( wfWikiID() ) );
672 $db->expects( $this->once() )
673 ->method( 'selectRow' )
675 $this->equalTo( [ 'revision', 'page', 'user' ] ),
676 // We don't really care about the fields are they come from the selectField methods
677 $this->isType( 'array' ),
678 $this->equalTo( $conditions ),
680 $this->stringContains( 'fetchRevisionRowFromConds' ),
681 // We don't really care about the options here
682 $this->isType( 'array' ),
683 // We don't really care about the join conds are they come from the joinCond methods
684 $this->isType( 'array' )
686 ->willReturn( $row );
688 $revision = Revision
::loadFromTitle( $db, $title );
690 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
691 $this->assertEquals( $row->rev_id
, $revision->getId() );
692 $this->assertEquals( $row->rev_len
, $revision->getSize() );
693 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
694 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
695 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
696 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
697 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
700 public function provideDecompressRevisionText() {
701 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
702 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
703 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
704 yield
'(no legacy encoding), string in with gzip flag returns string' => [
705 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
706 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
708 yield
'(no legacy encoding), string in with object flag returns false' => [
709 // gzip string below generated with serialize( 'JOJO' )
710 false, "s:4:\"JOJO\";", [ 'object' ], false,
712 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
714 // Using a TitleValue object as it has a getText method (which is needed)
715 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
719 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
721 // Using a TitleValue object as it has a getText method (which is needed)
722 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
723 [ 'object', 'gzip' ],
726 yield
'(ISO-8859-1 encoding), string in string out' => [
728 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
732 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
734 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
738 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
740 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
744 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
746 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
747 [ 'gzip', 'object' ],
753 * @dataProvider provideDecompressRevisionText
754 * @covers Revision::decompressRevisionText
756 * @param bool $legacyEncoding
758 * @param array $flags
759 * @param mixed $expected
761 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
762 $blobStore = $this->getBlobStore();
763 if ( $legacyEncoding ) {
764 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
767 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
770 Revision
::decompressRevisionText( $text, $flags )
775 * @covers Revision::getRevisionText
777 public function testGetRevisionText_returnsFalseWhenNoTextField() {
778 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
781 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
782 yield
'Just text' => [
783 (object)[ 'old_text' => 'SomeText' ],
787 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
788 yield
'gzip text' => [
790 'old_text' => "sttttr\002\022\000",
791 'old_flags' => 'gzip'
796 yield
'gzip text and different prefix' => [
798 'jojo_text' => "sttttr\002\022\000",
799 'jojo_flags' => 'gzip'
807 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
808 * @covers Revision::getRevisionText
810 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
815 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
818 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
819 yield
'Just some text' => [ 'someNonUrlText' ];
820 yield
'No second URL part' => [ 'someProtocol://' ];
824 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
825 * @covers Revision::getRevisionText
827 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
830 Wikimedia\
suppressWarnings();
832 Revision
::getRevisionText(
835 'old_flags' => 'external',
839 Wikimedia\
suppressWarnings( true );
843 * @covers Revision::getRevisionText
845 public function testGetRevisionText_external_noOldId() {
847 'ExternalStoreFactory',
848 new ExternalStoreFactory( [ 'ForTesting' ] )
852 Revision
::getRevisionText(
854 'old_text' => 'ForTesting://cluster1/12345',
855 'old_flags' => 'external,gzip',
862 * @covers Revision::getRevisionText
864 public function testGetRevisionText_external_oldId() {
865 $cache = $this->getWANObjectCache();
866 $this->setService( 'MainWANObjectCache', $cache );
869 'ExternalStoreFactory',
870 new ExternalStoreFactory( [ 'ForTesting' ] )
873 $lb = $this->getMockBuilder( LoadBalancer
::class )
874 ->disableOriginalConstructor()
877 $blobStore = new SqlBlobStore( $lb, $cache );
878 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
882 Revision
::getRevisionText(
884 'old_text' => 'ForTesting://cluster1/12345',
885 'old_flags' => 'external,gzip',
891 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
892 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
896 * @covers Revision::userJoinCond
898 public function testUserJoinCond() {
899 $this->hideDeprecated( 'Revision::userJoinCond' );
900 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
901 $this->overrideMwServices();
903 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
904 Revision
::userJoinCond()
909 * @covers Revision::pageJoinCond
911 public function testPageJoinCond() {
912 $this->hideDeprecated( 'Revision::pageJoinCond' );
914 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
915 Revision
::pageJoinCond()
919 private function overrideCommentStoreAndActorMigration() {
920 $mockStore = $this->getMockBuilder( CommentStore
::class )
921 ->disableOriginalConstructor()
923 $mockStore->expects( $this->any() )
924 ->method( 'getFields' )
925 ->willReturn( [ 'commentstore' => 'fields' ] );
926 $mockStore->expects( $this->any() )
927 ->method( 'getJoin' )
929 'tables' => [ 'commentstore' => 'table' ],
930 'fields' => [ 'commentstore' => 'field' ],
931 'joins' => [ 'commentstore' => 'join' ],
933 $this->setService( 'CommentStore', $mockStore );
935 $mockStore = $this->getMockBuilder( ActorMigration
::class )
936 ->disableOriginalConstructor()
938 $mockStore->expects( $this->any() )
939 ->method( 'getJoin' )
940 ->willReturnCallback( function ( $key ) {
941 $p = strtok( $key, '_' );
943 'tables' => [ 'actormigration' => 'table' ],
945 $p . '_user' => 'actormigration_user',
946 $p . '_user_text' => 'actormigration_user_text',
947 $p . '_actor' => 'actormigration_actor',
949 'joins' => [ 'actormigration' => 'join' ],
952 $this->setService( 'ActorMigration', $mockStore );
955 public function provideSelectFields() {
965 'rev_actor' => 'NULL',
971 'commentstore' => 'fields',
972 'rev_content_format',
985 'rev_actor' => 'NULL',
991 'commentstore' => 'fields',
997 * @dataProvider provideSelectFields
998 * @covers Revision::selectFields
1000 public function testSelectFields( $contentHandlerUseDB, $expected ) {
1001 $this->hideDeprecated( 'Revision::selectFields' );
1002 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1003 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
1004 $this->overrideCommentStoreAndActorMigration();
1005 $this->assertEquals( $expected, Revision
::selectFields() );
1008 public function provideSelectArchiveFields() {
1019 'ar_actor' => 'NULL',
1025 'commentstore' => 'fields',
1026 'ar_content_format',
1040 'ar_actor' => 'NULL',
1046 'commentstore' => 'fields',
1052 * @dataProvider provideSelectArchiveFields
1053 * @covers Revision::selectArchiveFields
1055 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
1056 $this->hideDeprecated( 'Revision::selectArchiveFields' );
1057 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
1058 $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD
);
1059 $this->overrideCommentStoreAndActorMigration();
1060 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
1064 * @covers Revision::selectTextFields
1066 public function testSelectTextFields() {
1067 $this->hideDeprecated( 'Revision::selectTextFields' );
1068 $this->assertEquals(
1073 Revision
::selectTextFields()
1078 * @covers Revision::selectPageFields
1080 public function testSelectPageFields() {
1081 $this->hideDeprecated( 'Revision::selectPageFields' );
1082 $this->assertEquals(
1091 Revision
::selectPageFields()
1096 * @covers Revision::selectUserFields
1098 public function testSelectUserFields() {
1099 $this->hideDeprecated( 'Revision::selectUserFields' );
1100 $this->assertEquals(
1104 Revision
::selectUserFields()
1108 public function provideGetArchiveQueryInfo() {
1109 yield
'wgContentHandlerUseDB false' => [
1111 'wgContentHandlerUseDB' => false,
1116 'commentstore' => 'table',
1117 'actormigration' => 'table',
1132 'commentstore' => 'field',
1133 'ar_user' => 'actormigration_user',
1134 'ar_user_text' => 'actormigration_user_text',
1135 'ar_actor' => 'actormigration_actor',
1137 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1140 yield
'wgContentHandlerUseDB true' => [
1142 'wgContentHandlerUseDB' => true,
1147 'commentstore' => 'table',
1148 'actormigration' => 'table',
1163 'commentstore' => 'field',
1164 'ar_user' => 'actormigration_user',
1165 'ar_user_text' => 'actormigration_user_text',
1166 'ar_actor' => 'actormigration_actor',
1167 'ar_content_format',
1170 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1176 * @covers Revision::getArchiveQueryInfo
1177 * @dataProvider provideGetArchiveQueryInfo
1179 public function testGetArchiveQueryInfo( $globals, $expected ) {
1180 $this->setMwGlobals( $globals );
1181 $this->overrideCommentStoreAndActorMigration();
1183 $revisionStore = $this->getRevisionStore();
1184 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1185 $this->setService( 'RevisionStore', $revisionStore );
1186 $this->assertEquals(
1188 Revision
::getArchiveQueryInfo()
1192 public function provideGetQueryInfo() {
1193 yield
'wgContentHandlerUseDB false, opts none' => [
1195 'wgContentHandlerUseDB' => false,
1199 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1210 'commentstore' => 'field',
1211 'rev_user' => 'actormigration_user',
1212 'rev_user_text' => 'actormigration_user_text',
1213 'rev_actor' => 'actormigration_actor',
1215 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1218 yield
'wgContentHandlerUseDB false, opts page' => [
1220 'wgContentHandlerUseDB' => false,
1224 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page' ],
1235 'commentstore' => 'field',
1236 'rev_user' => 'actormigration_user',
1237 'rev_user_text' => 'actormigration_user_text',
1238 'rev_actor' => 'actormigration_actor',
1249 [ 'page_id = rev_page' ],
1251 'commentstore' => 'join',
1252 'actormigration' => 'join',
1256 yield
'wgContentHandlerUseDB false, opts user' => [
1258 'wgContentHandlerUseDB' => false,
1262 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'user' ],
1273 'commentstore' => 'field',
1274 'rev_user' => 'actormigration_user',
1275 'rev_user_text' => 'actormigration_user_text',
1276 'rev_actor' => 'actormigration_actor',
1283 'actormigration_user != 0',
1284 'user_id = actormigration_user',
1287 'commentstore' => 'join',
1288 'actormigration' => 'join',
1292 yield
'wgContentHandlerUseDB false, opts text' => [
1294 'wgContentHandlerUseDB' => false,
1298 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'text' ],
1309 'commentstore' => 'field',
1310 'rev_user' => 'actormigration_user',
1311 'rev_user_text' => 'actormigration_user_text',
1312 'rev_actor' => 'actormigration_actor',
1319 [ 'rev_text_id=old_id' ],
1321 'commentstore' => 'join',
1322 'actormigration' => 'join',
1326 yield
'wgContentHandlerUseDB false, opts 3' => [
1328 'wgContentHandlerUseDB' => false,
1330 [ 'text', 'page', 'user' ],
1333 'revision', 'commentstore' => 'table', 'actormigration' => 'table', 'page', 'user', 'text'
1345 'commentstore' => 'field',
1346 'rev_user' => 'actormigration_user',
1347 'rev_user_text' => 'actormigration_user_text',
1348 'rev_actor' => 'actormigration_actor',
1362 [ 'page_id = rev_page' ],
1367 'actormigration_user != 0',
1368 'user_id = actormigration_user',
1373 [ 'rev_text_id=old_id' ],
1375 'commentstore' => 'join',
1376 'actormigration' => 'join',
1380 yield
'wgContentHandlerUseDB true, opts none' => [
1382 'wgContentHandlerUseDB' => true,
1386 'tables' => [ 'revision', 'commentstore' => 'table', 'actormigration' => 'table' ],
1397 'commentstore' => 'field',
1398 'rev_user' => 'actormigration_user',
1399 'rev_user_text' => 'actormigration_user_text',
1400 'rev_actor' => 'actormigration_actor',
1401 'rev_content_format',
1402 'rev_content_model',
1404 'joins' => [ 'commentstore' => 'join', 'actormigration' => 'join' ],
1410 * @covers Revision::getQueryInfo
1411 * @dataProvider provideGetQueryInfo
1413 public function testGetQueryInfo( $globals, $options, $expected ) {
1414 $this->setMwGlobals( $globals );
1415 $this->overrideCommentStoreAndActorMigration();
1417 $revisionStore = $this->getRevisionStore();
1418 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1419 $this->setService( 'RevisionStore', $revisionStore );
1421 $this->assertEquals(
1423 Revision
::getQueryInfo( $options )
1428 * @covers Revision::getSize
1430 public function testGetSize() {
1431 $title = $this->getMockTitle();
1433 $rec = new MutableRevisionRecord( $title );
1434 $rev = new Revision( $rec, 0, $title );
1436 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1438 $rec->setSize( 13 );
1439 $this->assertSame( 13, $rev->getSize() );
1443 * @covers Revision::getSize
1445 public function testGetSize_failure() {
1446 $title = $this->getMockTitle();
1448 $rec = $this->getMockBuilder( RevisionRecord
::class )
1449 ->disableOriginalConstructor()
1452 $rec->method( 'getSize' )
1453 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1455 $rev = new Revision( $rec, 0, $title );
1456 $this->assertNull( $rev->getSize() );
1460 * @covers Revision::getSha1
1462 public function testGetSha1() {
1463 $title = $this->getMockTitle();
1465 $rec = new MutableRevisionRecord( $title );
1466 $rev = new Revision( $rec, 0, $title );
1468 $emptyHash = SlotRecord
::base36Sha1( '' );
1469 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1471 $rec->setSha1( 'deadbeef' );
1472 $this->assertSame( 'deadbeef', $rev->getSha1() );
1476 * @covers Revision::getSha1
1478 public function testGetSha1_failure() {
1479 $title = $this->getMockTitle();
1481 $rec = $this->getMockBuilder( RevisionRecord
::class )
1482 ->disableOriginalConstructor()
1485 $rec->method( 'getSha1' )
1486 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1488 $rev = new Revision( $rec, 0, $title );
1489 $this->assertNull( $rev->getSha1() );
1493 * @covers Revision::getContent
1495 public function testGetContent() {
1496 $title = $this->getMockTitle();
1498 $rec = new MutableRevisionRecord( $title );
1499 $rev = new Revision( $rec, 0, $title );
1501 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1503 $content = new TextContent( 'Hello Kittens!' );
1504 $rec->setContent( 'main', $content );
1505 $this->assertSame( $content, $rev->getContent() );
1509 * @covers Revision::getContent
1511 public function testGetContent_failure() {
1512 $title = $this->getMockTitle();
1514 $rec = $this->getMockBuilder( RevisionRecord
::class )
1515 ->disableOriginalConstructor()
1518 $rec->method( 'getContent' )
1519 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1521 $rev = new Revision( $rec, 0, $title );
1522 $this->assertNull( $rev->getContent() );