3 use MediaWiki\Storage\BlobStoreFactory
;
4 use MediaWiki\Storage\RevisionStore
;
5 use MediaWiki\Storage\SqlBlobStore
;
6 use Wikimedia\Rdbms\IDatabase
;
7 use Wikimedia\Rdbms\LoadBalancer
;
10 * Test cases in RevisionTest should not interact with the Database.
11 * For test cases that need Database interaction see RevisionDbTestBase.
13 class RevisionTest
extends MediaWikiTestCase
{
15 public function provideConstructFromArray() {
16 yield
'with text' => [
18 'text' => 'hello world.',
19 'content_model' => CONTENT_MODEL_JAVASCRIPT
22 yield
'with content' => [
24 'content' => new JavaScriptContent( 'hellow world.' )
27 // FIXME: test with and without user ID, and with a user object.
28 // We can't prepare that here though, since we don't yet have a dummy DB
32 * @param string $model
35 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
36 $mock = $this->getMockBuilder( Title
::class )
37 ->disableOriginalConstructor()
39 $mock->expects( $this->any() )
40 ->method( 'getNamespace' )
41 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
42 $mock->expects( $this->any() )
43 ->method( 'getPrefixedText' )
44 ->will( $this->returnValue( 'RevisionTest' ) );
45 $mock->expects( $this->any() )
46 ->method( 'getDBkey' )
47 ->will( $this->returnValue( 'RevisionTest' ) );
48 $mock->expects( $this->any() )
49 ->method( 'getArticleID' )
50 ->will( $this->returnValue( 23 ) );
51 $mock->expects( $this->any() )
52 ->method( 'getModel' )
53 ->will( $this->returnValue( $model ) );
59 * @dataProvider provideConstructFromArray
60 * @covers Revision::__construct
61 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
63 public function testConstructFromArray( $rowArray ) {
64 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
65 $this->assertNotNull( $rev->getContent(), 'no content object available' );
66 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
67 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
71 * @covers Revision::__construct
72 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
74 public function testConstructFromEmptyArray() {
75 $rev = new Revision( [], 0, $this->getMockTitle() );
76 $this->assertNull( $rev->getContent(), 'no content object should be available' );
80 * @covers Revision::__construct
81 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
83 public function testConstructFromArrayWithBadPageId() {
84 MediaWiki\
suppressWarnings();
85 $rev = new Revision( [ 'page' => 77777777 ] );
86 $this->assertSame( 77777777, $rev->getPage() );
87 MediaWiki\restoreWarnings
();
90 public function provideConstructFromArray_userSetAsExpected() {
91 yield
'no user defaults to wgUser' => [
93 'content' => new JavaScriptContent( 'hello world.' ),
98 yield
'user text and id' => [
100 'content' => new JavaScriptContent( 'hello world.' ),
101 'user_text' => 'SomeTextUserName',
108 yield
'user text only' => [
110 'content' => new JavaScriptContent( 'hello world.' ),
111 'user_text' => '111.111.111.111',
119 * @dataProvider provideConstructFromArray_userSetAsExpected
120 * @covers Revision::__construct
121 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
123 * @param array $rowArray
124 * @param mixed $expectedUserId null to expect the current wgUser ID
125 * @param mixed $expectedUserName null to expect the current wgUser name
127 public function testConstructFromArray_userSetAsExpected(
132 $testUser = $this->getTestUser()->getUser();
133 $this->setMwGlobals( 'wgUser', $testUser );
134 if ( $expectedUserId === null ) {
135 $expectedUserId = $testUser->getId();
137 if ( $expectedUserName === null ) {
138 $expectedUserName = $testUser->getName();
141 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
142 $this->assertEquals( $expectedUserId, $rev->getUser() );
143 $this->assertEquals( $expectedUserName, $rev->getUserText() );
146 public function provideConstructFromArrayThrowsExceptions() {
147 yield
'content and text_id both not empty' => [
149 'content' => new WikitextContent( 'GOAT' ),
150 'text_id' => 'someid',
152 new MWException( "Text already stored in external store (id someid), " .
153 "can't serialize content object" )
155 yield
'unknown user id and no user name' => [
157 'content' => new JavaScriptContent( 'hello world.' ),
160 new MWException( 'user_text not given, and unknown user ID 9989' )
162 yield
'with bad content object (class)' => [
163 [ 'content' => new stdClass() ],
164 new MWException( 'content field must contain a Content object.' )
166 yield
'with bad content object (string)' => [
167 [ 'content' => 'ImAGoat' ],
168 new MWException( 'content field must contain a Content object.' )
170 yield
'bad row format' => [
171 'imastring, not a row',
172 new InvalidArgumentException(
173 '$row must be a row object, an associative array, or a RevisionRecord'
179 * @dataProvider provideConstructFromArrayThrowsExceptions
180 * @covers Revision::__construct
181 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
183 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
184 $this->setExpectedException(
185 get_class( $expectedException ),
186 $expectedException->getMessage(),
187 $expectedException->getCode()
189 new Revision( $rowArray, 0, $this->getMockTitle() );
193 * @covers Revision::__construct
194 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
196 public function testConstructFromNothing() {
197 $this->setExpectedException(
198 InvalidArgumentException
::class
203 public function provideConstructFromRow() {
204 yield
'Full construction' => [
208 'rev_text_id' => '2',
209 'rev_timestamp' => '20171017114835',
210 'rev_user_text' => '127.0.0.1',
212 'rev_minor_edit' => '0',
213 'rev_deleted' => '0',
215 'rev_parent_id' => '1',
216 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
217 'rev_comment_text' => 'Goat Comment!',
218 'rev_comment_data' => null,
219 'rev_comment_cid' => null,
220 'rev_content_format' => 'GOATFORMAT',
221 'rev_content_model' => 'GOATMODEL',
223 function ( RevisionTest
$testCase, Revision
$rev ) {
224 $testCase->assertSame( 42, $rev->getId() );
225 $testCase->assertSame( 23, $rev->getPage() );
226 $testCase->assertSame( 2, $rev->getTextId() );
227 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
228 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
229 $testCase->assertSame( 0, $rev->getUser() );
230 $testCase->assertSame( false, $rev->isMinor() );
231 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
232 $testCase->assertSame( 46, $rev->getSize() );
233 $testCase->assertSame( 1, $rev->getParentId() );
234 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
235 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
236 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
237 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
240 yield
'default field values' => [
244 'rev_text_id' => '2',
245 'rev_timestamp' => '20171017114835',
246 'rev_user_text' => '127.0.0.1',
248 'rev_minor_edit' => '0',
249 'rev_deleted' => '0',
250 'rev_comment_text' => 'Goat Comment!',
251 'rev_comment_data' => null,
252 'rev_comment_cid' => null,
254 function ( RevisionTest
$testCase, Revision
$rev ) {
255 // parent ID may be null
256 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
259 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
260 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
261 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
262 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
263 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
264 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
267 $testCase->assertNotNull( $rev->getSize(), 'size' );
268 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
270 // NOTE: model and format will be detected based on the namespace of the (mock) title
271 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
272 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
278 * @dataProvider provideConstructFromRow
279 * @covers Revision::__construct
280 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
282 public function testConstructFromRow( array $arrayData, $assertions ) {
283 $data = 'Hello goat.'; // needs to match model and format
285 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
286 ->disableOriginalConstructor()
289 $blobStore->method( 'getBlob' )
290 ->will( $this->returnValue( $data ) );
292 $blobStore->method( 'getTextIdFromAddress' )
293 ->will( $this->returnCallback(
294 function ( $address ) {
295 // Turn "tt:1234" into 12345.
296 // Note that this must be functional so we can test getTextId().
297 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
298 $parts = explode( ':', $address );
299 return (int)array_pop( $parts );
303 // Note override internal service, so RevisionStore uses it as well.
304 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
306 $row = (object)$arrayData;
307 $rev = new Revision( $row, 0, $this->getMockTitle() );
308 $assertions( $this, $rev );
312 * @covers Revision::__construct
313 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
315 public function testConstructFromRowWithBadPageId() {
316 MediaWiki\
suppressWarnings();
317 $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
318 $this->assertSame( 77777777, $rev->getPage() );
319 MediaWiki\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( $lb, $this->getBlobStore(), $cache );
490 public function provideGetRevisionTextWithLegacyEncoding() {
491 yield
'Utf8Native' => [
492 "Wiki est l'\xc3\xa9cole superieur !",
496 'old_flags' => 'utf-8',
497 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
500 yield
'Utf8Legacy' => [
501 "Wiki est l'\xc3\xa9cole superieur !",
506 'old_text' => "Wiki est l'\xe9cole superieur !",
512 * @covers Revision::getRevisionText
513 * @dataProvider provideGetRevisionTextWithLegacyEncoding
515 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
516 $blobStore = $this->getBlobStore();
517 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
518 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
520 $this->testGetRevisionText( $expected, $rowData );
523 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
526 * Do not set the external flag!
527 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
529 yield
'Utf8NativeGzip' => [
530 "Wiki est l'\xc3\xa9cole superieur !",
534 'old_flags' => 'gzip,utf-8',
535 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
538 yield
'Utf8LegacyGzip' => [
539 "Wiki est l'\xc3\xa9cole superieur !",
543 'old_flags' => 'gzip',
544 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
550 * @covers Revision::getRevisionText
551 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
553 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
554 $this->checkPHPExtension( 'zlib' );
556 $blobStore = $this->getBlobStore();
557 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
558 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
560 $this->testGetRevisionText( $expected, $rowData );
564 * @covers Revision::compressRevisionText
566 public function testCompressRevisionTextUtf8() {
568 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
569 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
570 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
571 "Flags should contain 'utf-8'" );
572 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
573 "Flags should not contain 'gzip'" );
574 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
575 $row->old_text
, "Direct check" );
576 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
577 Revision
::getRevisionText( $row ), "getRevisionText" );
581 * @covers Revision::compressRevisionText
583 public function testCompressRevisionTextUtf8Gzip() {
584 $this->checkPHPExtension( 'zlib' );
586 $blobStore = $this->getBlobStore();
587 $blobStore->setCompressBlobs( true );
588 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
591 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
592 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
593 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
594 "Flags should contain 'utf-8'" );
595 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
596 "Flags should contain 'gzip'" );
597 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
598 gzinflate( $row->old_text
), "Direct check" );
599 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
600 Revision
::getRevisionText( $row ), "getRevisionText" );
604 * @covers Revision::loadFromTitle
606 public function testLoadFromTitle() {
607 $title = $this->getMockTitle();
610 'rev_id=page_latest',
611 'page_namespace' => $title->getNamespace(),
612 'page_title' => $title->getDBkey()
617 'rev_page' => $title->getArticleID(),
618 'rev_text_id' => '2',
619 'rev_timestamp' => '20171017114835',
620 'rev_user_text' => '127.0.0.1',
622 'rev_minor_edit' => '0',
623 'rev_deleted' => '0',
625 'rev_parent_id' => '1',
626 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
627 'rev_comment_text' => 'Goat Comment!',
628 'rev_comment_data' => null,
629 'rev_comment_cid' => null,
630 'rev_content_format' => 'GOATFORMAT',
631 'rev_content_model' => 'GOATMODEL',
634 $db = $this->getMock( IDatabase
::class );
635 $db->expects( $this->any() )
636 ->method( 'getDomainId' )
637 ->will( $this->returnValue( wfWikiID() ) );
638 $db->expects( $this->once() )
639 ->method( 'selectRow' )
641 $this->equalTo( [ 'revision', 'page', 'user' ] ),
642 // We don't really care about the fields are they come from the selectField methods
643 $this->isType( 'array' ),
644 $this->equalTo( $conditions ),
646 $this->stringContains( 'fetchRevisionRowFromConds' ),
647 // We don't really care about the options here
648 $this->isType( 'array' ),
649 // We don't really care about the join conds are they come from the joinCond methods
650 $this->isType( 'array' )
652 ->willReturn( $row );
654 $revision = Revision
::loadFromTitle( $db, $title );
656 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
657 $this->assertEquals( $row->rev_id
, $revision->getId() );
658 $this->assertEquals( $row->rev_len
, $revision->getSize() );
659 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
660 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
661 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
662 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
663 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
666 public function provideDecompressRevisionText() {
667 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
668 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
669 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
670 yield
'(no legacy encoding), string in with gzip flag returns string' => [
671 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
672 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
674 yield
'(no legacy encoding), string in with object flag returns false' => [
675 // gzip string below generated with serialize( 'JOJO' )
676 false, "s:4:\"JOJO\";", [ 'object' ], false,
678 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
680 // Using a TitleValue object as it has a getText method (which is needed)
681 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
685 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
687 // Using a TitleValue object as it has a getText method (which is needed)
688 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
689 [ 'object', 'gzip' ],
692 yield
'(ISO-8859-1 encoding), string in string out' => [
694 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
698 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
700 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
704 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
706 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
710 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
712 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
713 [ 'gzip', 'object' ],
719 * @dataProvider provideDecompressRevisionText
720 * @covers Revision::decompressRevisionText
722 * @param bool $legacyEncoding
724 * @param array $flags
725 * @param mixed $expected
727 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
728 $blobStore = $this->getBlobStore();
729 if ( $legacyEncoding ) {
730 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
733 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
736 Revision
::decompressRevisionText( $text, $flags )
741 * @covers Revision::getRevisionText
743 public function testGetRevisionText_returnsFalseWhenNoTextField() {
744 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
747 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
748 yield
'Just text' => [
749 (object)[ 'old_text' => 'SomeText' ],
753 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
754 yield
'gzip text' => [
756 'old_text' => "sttttr\002\022\000",
757 'old_flags' => 'gzip'
762 yield
'gzip text and different prefix' => [
764 'jojo_text' => "sttttr\002\022\000",
765 'jojo_flags' => 'gzip'
773 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
774 * @covers Revision::getRevisionText
776 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
781 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
784 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
785 yield
'Just some text' => [ 'someNonUrlText' ];
786 yield
'No second URL part' => [ 'someProtocol://' ];
790 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
791 * @covers Revision::getRevisionText
793 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
797 Revision
::getRevisionText(
800 'old_flags' => 'external',
807 * @covers Revision::getRevisionText
809 public function testGetRevisionText_external_noOldId() {
811 'ExternalStoreFactory',
812 new ExternalStoreFactory( [ 'ForTesting' ] )
816 Revision
::getRevisionText(
818 'old_text' => 'ForTesting://cluster1/12345',
819 'old_flags' => 'external,gzip',
826 * @covers Revision::getRevisionText
828 public function testGetRevisionText_external_oldId() {
829 $cache = $this->getWANObjectCache();
830 $this->setService( 'MainWANObjectCache', $cache );
833 'ExternalStoreFactory',
834 new ExternalStoreFactory( [ 'ForTesting' ] )
837 $lb = $this->getMockBuilder( LoadBalancer
::class )
838 ->disableOriginalConstructor()
841 $blobStore = new SqlBlobStore( $lb, $cache );
842 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
846 Revision
::getRevisionText(
848 'old_text' => 'ForTesting://cluster1/12345',
849 'old_flags' => 'external,gzip',
855 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
856 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
860 * @covers Revision::userJoinCond
862 public function testUserJoinCond() {
863 $this->hideDeprecated( 'Revision::userJoinCond' );
865 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
866 Revision
::userJoinCond()
871 * @covers Revision::pageJoinCond
873 public function testPageJoinCond() {
874 $this->hideDeprecated( 'Revision::pageJoinCond' );
876 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
877 Revision
::pageJoinCond()
881 public function provideSelectFields() {
896 'rev_comment_text' => 'rev_comment',
897 'rev_comment_data' => 'NULL',
898 'rev_comment_cid' => 'NULL',
899 'rev_content_format',
917 'rev_comment_text' => 'rev_comment',
918 'rev_comment_data' => 'NULL',
919 'rev_comment_cid' => 'NULL',
925 * @dataProvider provideSelectFields
926 * @covers Revision::selectFields
927 * @todo a true unit test would mock CommentStore
929 public function testSelectFields( $contentHandlerUseDB, $expected ) {
930 $this->hideDeprecated( 'Revision::selectFields' );
931 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
932 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
933 $this->assertEquals( $expected, Revision
::selectFields() );
936 public function provideSelectArchiveFields() {
953 'ar_comment_text' => 'ar_comment',
954 'ar_comment_data' => 'NULL',
955 'ar_comment_cid' => 'NULL',
976 'ar_comment_text' => 'ar_comment',
977 'ar_comment_data' => 'NULL',
978 'ar_comment_cid' => 'NULL',
984 * @dataProvider provideSelectArchiveFields
985 * @covers Revision::selectArchiveFields
986 * @todo a true unit test would mock CommentStore
988 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
989 $this->hideDeprecated( 'Revision::selectArchiveFields' );
990 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
991 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
992 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
996 * @covers Revision::selectTextFields
998 public function testSelectTextFields() {
999 $this->hideDeprecated( 'Revision::selectTextFields' );
1000 $this->assertEquals(
1005 Revision
::selectTextFields()
1010 * @covers Revision::selectPageFields
1012 public function testSelectPageFields() {
1013 $this->hideDeprecated( 'Revision::selectPageFields' );
1014 $this->assertEquals(
1023 Revision
::selectPageFields()
1028 * @covers Revision::selectUserFields
1030 public function testSelectUserFields() {
1031 $this->hideDeprecated( 'Revision::selectUserFields' );
1032 $this->assertEquals(
1036 Revision
::selectUserFields()
1040 public function provideGetArchiveQueryInfo() {
1041 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1043 'wgContentHandlerUseDB' => false,
1044 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1047 'tables' => [ 'archive' ],
1064 'ar_comment_text' => 'ar_comment',
1065 'ar_comment_data' => 'NULL',
1066 'ar_comment_cid' => 'NULL',
1071 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1073 'wgContentHandlerUseDB' => true,
1074 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1077 'tables' => [ 'archive' ],
1094 'ar_comment_text' => 'ar_comment',
1095 'ar_comment_data' => 'NULL',
1096 'ar_comment_cid' => 'NULL',
1097 'ar_content_format',
1103 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1105 'wgContentHandlerUseDB' => false,
1106 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1111 'comment_ar_comment' => 'comment',
1129 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1130 'ar_comment_data' => 'comment_ar_comment.comment_data',
1131 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1134 'comment_ar_comment' => [
1136 'comment_ar_comment.comment_id = ar_comment_id',
1141 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1143 'wgContentHandlerUseDB' => false,
1144 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1149 'comment_ar_comment' => 'comment',
1167 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1168 'ar_comment_data' => 'comment_ar_comment.comment_data',
1169 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1172 'comment_ar_comment' => [
1174 'comment_ar_comment.comment_id = ar_comment_id',
1179 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1181 'wgContentHandlerUseDB' => false,
1182 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1187 'comment_ar_comment' => 'comment',
1205 'ar_comment_text' => 'comment_ar_comment.comment_text',
1206 'ar_comment_data' => 'comment_ar_comment.comment_data',
1207 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1210 'comment_ar_comment' => [
1212 'comment_ar_comment.comment_id = ar_comment_id',
1220 * @covers Revision::getArchiveQueryInfo
1221 * @dataProvider provideGetArchiveQueryInfo
1223 public function testGetArchiveQueryInfo( $globals, $expected ) {
1224 $this->setMwGlobals( $globals );
1226 $revisionStore = $this->getRevisionStore();
1227 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1228 $this->setService( 'RevisionStore', $revisionStore );
1230 $this->assertEquals(
1232 Revision
::getArchiveQueryInfo()
1236 public function provideGetQueryInfo() {
1237 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1239 'wgContentHandlerUseDB' => false,
1240 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1244 'tables' => [ 'revision' ],
1257 'rev_comment_text' => 'rev_comment',
1258 'rev_comment_data' => 'NULL',
1259 'rev_comment_cid' => 'NULL',
1264 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1266 'wgContentHandlerUseDB' => false,
1267 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1271 'tables' => [ 'revision', 'page' ],
1284 'rev_comment_text' => 'rev_comment',
1285 'rev_comment_data' => 'NULL',
1286 'rev_comment_cid' => 'NULL',
1297 [ 'page_id = rev_page' ],
1302 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1304 'wgContentHandlerUseDB' => false,
1305 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1309 'tables' => [ 'revision', 'user' ],
1322 'rev_comment_text' => 'rev_comment',
1323 'rev_comment_data' => 'NULL',
1324 'rev_comment_cid' => 'NULL',
1332 'user_id = rev_user',
1338 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1340 'wgContentHandlerUseDB' => false,
1341 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1345 'tables' => [ 'revision', 'text' ],
1358 'rev_comment_text' => 'rev_comment',
1359 'rev_comment_data' => 'NULL',
1360 'rev_comment_cid' => 'NULL',
1367 [ 'rev_text_id=old_id' ],
1372 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1374 'wgContentHandlerUseDB' => false,
1375 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1377 [ 'text', 'page', 'user' ],
1379 'tables' => [ 'revision', 'page', 'user', 'text' ],
1392 'rev_comment_text' => 'rev_comment',
1393 'rev_comment_data' => 'NULL',
1394 'rev_comment_cid' => 'NULL',
1408 [ 'page_id = rev_page' ],
1414 'user_id = rev_user',
1419 [ 'rev_text_id=old_id' ],
1424 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1426 'wgContentHandlerUseDB' => true,
1427 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1431 'tables' => [ 'revision' ],
1444 'rev_comment_text' => 'rev_comment',
1445 'rev_comment_data' => 'NULL',
1446 'rev_comment_cid' => 'NULL',
1447 'rev_content_format',
1448 'rev_content_model',
1453 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1455 'wgContentHandlerUseDB' => false,
1456 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1462 'temp_rev_comment' => 'revision_comment_temp',
1463 'comment_rev_comment' => 'comment',
1477 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1478 'rev_comment_data' => 'comment_rev_comment.comment_data',
1479 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1482 'temp_rev_comment' => [
1484 'temp_rev_comment.revcomment_rev = rev_id',
1486 'comment_rev_comment' => [
1488 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1493 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1495 'wgContentHandlerUseDB' => false,
1496 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1502 'temp_rev_comment' => 'revision_comment_temp',
1503 'comment_rev_comment' => 'comment',
1517 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1518 'rev_comment_data' => 'comment_rev_comment.comment_data',
1519 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1522 'temp_rev_comment' => [
1524 'temp_rev_comment.revcomment_rev = rev_id',
1526 'comment_rev_comment' => [
1528 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1533 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1535 'wgContentHandlerUseDB' => false,
1536 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1542 'temp_rev_comment' => 'revision_comment_temp',
1543 'comment_rev_comment' => 'comment',
1557 'rev_comment_text' => 'comment_rev_comment.comment_text',
1558 'rev_comment_data' => 'comment_rev_comment.comment_data',
1559 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1562 'temp_rev_comment' => [
1564 'temp_rev_comment.revcomment_rev = rev_id',
1566 'comment_rev_comment' => [
1568 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1576 * @covers Revision::getQueryInfo
1577 * @dataProvider provideGetQueryInfo
1579 public function testGetQueryInfo( $globals, $options, $expected ) {
1580 $this->setMwGlobals( $globals );
1582 $revisionStore = $this->getRevisionStore();
1583 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1584 $this->setService( 'RevisionStore', $revisionStore );
1586 $this->assertEquals(
1588 Revision
::getQueryInfo( $options )