3 use MediaWiki\Storage\BlobStoreFactory
;
4 use MediaWiki\Storage\MutableRevisionRecord
;
5 use MediaWiki\Storage\RevisionAccessException
;
6 use MediaWiki\Storage\RevisionRecord
;
7 use MediaWiki\Storage\RevisionStore
;
8 use MediaWiki\Storage\SlotRecord
;
9 use MediaWiki\Storage\SqlBlobStore
;
10 use Wikimedia\Rdbms\IDatabase
;
11 use Wikimedia\Rdbms\LoadBalancer
;
14 * Test cases in RevisionTest should not interact with the Database.
15 * For test cases that need Database interaction see RevisionDbTestBase.
17 class RevisionTest
extends MediaWikiTestCase
{
19 public function provideConstructFromArray() {
20 yield
'with text' => [
22 'text' => 'hello world.',
23 'content_model' => CONTENT_MODEL_JAVASCRIPT
26 yield
'with content' => [
28 'content' => new JavaScriptContent( 'hellow world.' )
31 // FIXME: test with and without user ID, and with a user object.
32 // We can't prepare that here though, since we don't yet have a dummy DB
36 * @param string $model
39 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT
) {
40 $mock = $this->getMockBuilder( Title
::class )
41 ->disableOriginalConstructor()
43 $mock->expects( $this->any() )
44 ->method( 'getNamespace' )
45 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
46 $mock->expects( $this->any() )
47 ->method( 'getPrefixedText' )
48 ->will( $this->returnValue( 'RevisionTest' ) );
49 $mock->expects( $this->any() )
50 ->method( 'getDBkey' )
51 ->will( $this->returnValue( 'RevisionTest' ) );
52 $mock->expects( $this->any() )
53 ->method( 'getArticleID' )
54 ->will( $this->returnValue( 23 ) );
55 $mock->expects( $this->any() )
56 ->method( 'getModel' )
57 ->will( $this->returnValue( $model ) );
63 * @dataProvider provideConstructFromArray
64 * @covers Revision::__construct
65 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
67 public function testConstructFromArray( $rowArray ) {
68 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
69 $this->assertNotNull( $rev->getContent(), 'no content object available' );
70 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContent()->getModel() );
71 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT
, $rev->getContentModel() );
75 * @covers Revision::__construct
76 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
78 public function testConstructFromEmptyArray() {
79 $rev = new Revision( [], 0, $this->getMockTitle() );
80 $this->assertNull( $rev->getContent(), 'no content object should be available' );
83 public function provideConstructFromArray_userSetAsExpected() {
84 yield
'no user defaults to wgUser' => [
86 'content' => new JavaScriptContent( 'hello world.' ),
91 yield
'user text and id' => [
93 'content' => new JavaScriptContent( 'hello world.' ),
94 'user_text' => 'SomeTextUserName',
101 yield
'user text only' => [
103 'content' => new JavaScriptContent( 'hello world.' ),
104 'user_text' => '111.111.111.111',
112 * @dataProvider provideConstructFromArray_userSetAsExpected
113 * @covers Revision::__construct
114 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
116 * @param array $rowArray
117 * @param mixed $expectedUserId null to expect the current wgUser ID
118 * @param mixed $expectedUserName null to expect the current wgUser name
120 public function testConstructFromArray_userSetAsExpected(
125 $testUser = $this->getTestUser()->getUser();
126 $this->setMwGlobals( 'wgUser', $testUser );
127 if ( $expectedUserId === null ) {
128 $expectedUserId = $testUser->getId();
130 if ( $expectedUserName === null ) {
131 $expectedUserName = $testUser->getName();
134 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
135 $this->assertEquals( $expectedUserId, $rev->getUser() );
136 $this->assertEquals( $expectedUserName, $rev->getUserText() );
139 public function provideConstructFromArrayThrowsExceptions() {
140 yield
'content and text_id both not empty' => [
142 'content' => new WikitextContent( 'GOAT' ),
143 'text_id' => 'someid',
145 new MWException( "Text already stored in external store (id someid), " .
146 "can't serialize content object" )
148 yield
'unknown user id and no user name' => [
150 'content' => new JavaScriptContent( 'hello world.' ),
153 new MWException( 'user_text not given, and unknown user ID 9989' )
155 yield
'with bad content object (class)' => [
156 [ 'content' => new stdClass() ],
157 new MWException( 'content field must contain a Content object.' )
159 yield
'with bad content object (string)' => [
160 [ 'content' => 'ImAGoat' ],
161 new MWException( 'content field must contain a Content object.' )
163 yield
'bad row format' => [
164 'imastring, not a row',
165 new InvalidArgumentException(
166 '$row must be a row object, an associative array, or a RevisionRecord'
172 * @dataProvider provideConstructFromArrayThrowsExceptions
173 * @covers Revision::__construct
174 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
176 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception
$expectedException ) {
177 $this->setExpectedException(
178 get_class( $expectedException ),
179 $expectedException->getMessage(),
180 $expectedException->getCode()
182 new Revision( $rowArray, 0, $this->getMockTitle() );
186 * @covers Revision::__construct
187 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
189 public function testConstructFromNothing() {
190 $this->setExpectedException(
191 InvalidArgumentException
::class
196 public function provideConstructFromRow() {
197 yield
'Full construction' => [
201 'rev_text_id' => '2',
202 'rev_timestamp' => '20171017114835',
203 'rev_user_text' => '127.0.0.1',
205 'rev_minor_edit' => '0',
206 'rev_deleted' => '0',
208 'rev_parent_id' => '1',
209 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
210 'rev_comment_text' => 'Goat Comment!',
211 'rev_comment_data' => null,
212 'rev_comment_cid' => null,
213 'rev_content_format' => 'GOATFORMAT',
214 'rev_content_model' => 'GOATMODEL',
216 function ( RevisionTest
$testCase, Revision
$rev ) {
217 $testCase->assertSame( 42, $rev->getId() );
218 $testCase->assertSame( 23, $rev->getPage() );
219 $testCase->assertSame( 2, $rev->getTextId() );
220 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
221 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
222 $testCase->assertSame( 0, $rev->getUser() );
223 $testCase->assertSame( false, $rev->isMinor() );
224 $testCase->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
225 $testCase->assertSame( 46, $rev->getSize() );
226 $testCase->assertSame( 1, $rev->getParentId() );
227 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
228 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
229 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
230 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
233 yield
'default field values' => [
237 'rev_text_id' => '2',
238 'rev_timestamp' => '20171017114835',
239 'rev_user_text' => '127.0.0.1',
241 'rev_minor_edit' => '0',
242 'rev_deleted' => '0',
243 'rev_comment_text' => 'Goat Comment!',
244 'rev_comment_data' => null,
245 'rev_comment_cid' => null,
247 function ( RevisionTest
$testCase, Revision
$rev ) {
248 // parent ID may be null
249 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
252 $testCase->assertSame( $rev->getTimestamp(), '20171017114835', 'timestamp' );
253 $testCase->assertSame( $rev->getUserText(), '127.0.0.1', 'user name' );
254 $testCase->assertSame( $rev->getUser(), 0, 'user id' );
255 $testCase->assertSame( $rev->getComment(), 'Goat Comment!' );
256 $testCase->assertSame( false, $rev->isMinor(), 'minor edit' );
257 $testCase->assertSame( 0, $rev->getVisibility(), 'visibility flags' );
260 $testCase->assertNotNull( $rev->getSize(), 'size' );
261 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
263 // NOTE: model and format will be detected based on the namespace of the (mock) title
264 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat(), 'format' );
265 $testCase->assertSame( 'wikitext', $rev->getContentModel(), 'model' );
271 * @dataProvider provideConstructFromRow
272 * @covers Revision::__construct
273 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
275 public function testConstructFromRow( array $arrayData, $assertions ) {
276 $data = 'Hello goat.'; // needs to match model and format
278 $blobStore = $this->getMockBuilder( SqlBlobStore
::class )
279 ->disableOriginalConstructor()
282 $blobStore->method( 'getBlob' )
283 ->will( $this->returnValue( $data ) );
285 $blobStore->method( 'getTextIdFromAddress' )
286 ->will( $this->returnCallback(
287 function ( $address ) {
288 // Turn "tt:1234" into 12345.
289 // Note that this must be functional so we can test getTextId().
290 // Ideally, we'd un-mock getTextIdFromAddress and use its actual implementation.
291 $parts = explode( ':', $address );
292 return (int)array_pop( $parts );
296 // Note override internal service, so RevisionStore uses it as well.
297 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
299 $row = (object)$arrayData;
300 $rev = new Revision( $row, 0, $this->getMockTitle() );
301 $assertions( $this, $rev );
304 public function provideGetRevisionText() {
305 yield
'Generic test' => [
306 'This is a goat of revision text.',
309 'old_text' => 'This is a goat of revision text.',
314 public function provideGetId() {
326 * @dataProvider provideGetId
327 * @covers Revision::getId
329 public function testGetId( $rowArray, $expectedId ) {
330 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
331 $this->assertEquals( $expectedId, $rev->getId() );
334 public function provideSetId() {
335 yield
[ '123', 123 ];
340 * @dataProvider provideSetId
341 * @covers Revision::setId
343 public function testSetId( $input, $expected ) {
344 $rev = new Revision( [], 0, $this->getMockTitle() );
345 $rev->setId( $input );
346 $this->assertSame( $expected, $rev->getId() );
349 public function provideSetUserIdAndName() {
350 yield
[ '123', 123, 'GOaT' ];
351 yield
[ 456, 456, 'GOaT' ];
355 * @dataProvider provideSetUserIdAndName
356 * @covers Revision::setUserIdAndName
358 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
359 $rev = new Revision( [], 0, $this->getMockTitle() );
360 $rev->setUserIdAndName( $inputId, $name );
361 $this->assertSame( $expectedId, $rev->getUser( Revision
::RAW
) );
362 $this->assertEquals( $name, $rev->getUserText( Revision
::RAW
) );
365 public function provideGetTextId() {
367 yield
[ [ 'text_id' => '123' ], 123 ];
368 yield
[ [ 'text_id' => 456 ], 456 ];
372 * @dataProvider provideGetTextId
373 * @covers Revision::getTextId()
375 public function testGetTextId( $rowArray, $expected ) {
376 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
377 $this->assertSame( $expected, $rev->getTextId() );
380 public function provideGetParentId() {
382 yield
[ [ 'parent_id' => '123' ], 123 ];
383 yield
[ [ 'parent_id' => 456 ], 456 ];
387 * @dataProvider provideGetParentId
388 * @covers Revision::getParentId()
390 public function testGetParentId( $rowArray, $expected ) {
391 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
392 $this->assertSame( $expected, $rev->getParentId() );
396 * @covers Revision::getRevisionText
397 * @dataProvider provideGetRevisionText
399 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
402 Revision
::getRevisionText( (object)$rowData, $prefix, $wiki ) );
405 public function provideGetRevisionTextWithZlibExtension() {
406 yield
'Generic gzip test' => [
407 'This is a small goat of revision text.',
409 'old_flags' => 'gzip',
410 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
416 * @covers Revision::getRevisionText
417 * @dataProvider provideGetRevisionTextWithZlibExtension
419 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
420 $this->checkPHPExtension( 'zlib' );
421 $this->testGetRevisionText( $expected, $rowData );
424 private function getWANObjectCache() {
425 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
429 * @return SqlBlobStore
431 private function getBlobStore() {
432 /** @var LoadBalancer $lb */
433 $lb = $this->getMockBuilder( LoadBalancer
::class )
434 ->disableOriginalConstructor()
437 $cache = $this->getWANObjectCache();
439 $blobStore = new SqlBlobStore( $lb, $cache );
443 private function mockBlobStoreFactory( $blobStore ) {
444 /** @var LoadBalancer $lb */
445 $factory = $this->getMockBuilder( BlobStoreFactory
::class )
446 ->disableOriginalConstructor()
448 $factory->expects( $this->any() )
449 ->method( 'newBlobStore' )
450 ->willReturn( $blobStore );
451 $factory->expects( $this->any() )
452 ->method( 'newSqlBlobStore' )
453 ->willReturn( $blobStore );
458 * @return RevisionStore
460 private function getRevisionStore() {
461 /** @var LoadBalancer $lb */
462 $lb = $this->getMockBuilder( LoadBalancer
::class )
463 ->disableOriginalConstructor()
466 $cache = $this->getWANObjectCache();
468 $blobStore = new RevisionStore( $lb, $this->getBlobStore(), $cache );
472 public function provideGetRevisionTextWithLegacyEncoding() {
473 yield
'Utf8Native' => [
474 "Wiki est l'\xc3\xa9cole superieur !",
478 'old_flags' => 'utf-8',
479 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
482 yield
'Utf8Legacy' => [
483 "Wiki est l'\xc3\xa9cole superieur !",
488 'old_text' => "Wiki est l'\xe9cole superieur !",
494 * @covers Revision::getRevisionText
495 * @dataProvider provideGetRevisionTextWithLegacyEncoding
497 public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
498 $blobStore = $this->getBlobStore();
499 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
500 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
502 $this->testGetRevisionText( $expected, $rowData );
505 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
508 * Do not set the external flag!
509 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
511 yield
'Utf8NativeGzip' => [
512 "Wiki est l'\xc3\xa9cole superieur !",
516 'old_flags' => 'gzip,utf-8',
517 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
520 yield
'Utf8LegacyGzip' => [
521 "Wiki est l'\xc3\xa9cole superieur !",
525 'old_flags' => 'gzip',
526 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
532 * @covers Revision::getRevisionText
533 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
535 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
536 $this->checkPHPExtension( 'zlib' );
538 $blobStore = $this->getBlobStore();
539 $blobStore->setLegacyEncoding( $encoding, Language
::factory( $lang ) );
540 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
542 $this->testGetRevisionText( $expected, $rowData );
546 * @covers Revision::compressRevisionText
548 public function testCompressRevisionTextUtf8() {
550 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
551 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
552 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
553 "Flags should contain 'utf-8'" );
554 $this->assertFalse( false !== strpos( $row->old_flags
, 'gzip' ),
555 "Flags should not contain 'gzip'" );
556 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
557 $row->old_text
, "Direct check" );
558 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
559 Revision
::getRevisionText( $row ), "getRevisionText" );
563 * @covers Revision::compressRevisionText
565 public function testCompressRevisionTextUtf8Gzip() {
566 $this->checkPHPExtension( 'zlib' );
568 $blobStore = $this->getBlobStore();
569 $blobStore->setCompressBlobs( true );
570 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
573 $row->old_text
= "Wiki est l'\xc3\xa9cole superieur !";
574 $row->old_flags
= Revision
::compressRevisionText( $row->old_text
);
575 $this->assertTrue( false !== strpos( $row->old_flags
, 'utf-8' ),
576 "Flags should contain 'utf-8'" );
577 $this->assertTrue( false !== strpos( $row->old_flags
, 'gzip' ),
578 "Flags should contain 'gzip'" );
579 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
580 gzinflate( $row->old_text
), "Direct check" );
581 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
582 Revision
::getRevisionText( $row ), "getRevisionText" );
586 * @covers Revision::loadFromTitle
588 public function testLoadFromTitle() {
589 $title = $this->getMockTitle();
592 'rev_id=page_latest',
593 'page_namespace' => $title->getNamespace(),
594 'page_title' => $title->getDBkey()
599 'rev_page' => $title->getArticleID(),
600 'rev_text_id' => '2',
601 'rev_timestamp' => '20171017114835',
602 'rev_user_text' => '127.0.0.1',
604 'rev_minor_edit' => '0',
605 'rev_deleted' => '0',
607 'rev_parent_id' => '1',
608 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
609 'rev_comment_text' => 'Goat Comment!',
610 'rev_comment_data' => null,
611 'rev_comment_cid' => null,
612 'rev_content_format' => 'GOATFORMAT',
613 'rev_content_model' => 'GOATMODEL',
616 $db = $this->getMock( IDatabase
::class );
617 $db->expects( $this->any() )
618 ->method( 'getDomainId' )
619 ->will( $this->returnValue( wfWikiID() ) );
620 $db->expects( $this->once() )
621 ->method( 'selectRow' )
623 $this->equalTo( [ 'revision', 'page', 'user' ] ),
624 // We don't really care about the fields are they come from the selectField methods
625 $this->isType( 'array' ),
626 $this->equalTo( $conditions ),
628 $this->stringContains( 'fetchRevisionRowFromConds' ),
629 // We don't really care about the options here
630 $this->isType( 'array' ),
631 // We don't really care about the join conds are they come from the joinCond methods
632 $this->isType( 'array' )
634 ->willReturn( $row );
636 $revision = Revision
::loadFromTitle( $db, $title );
638 $this->assertEquals( $title->getArticleID(), $revision->getTitle()->getArticleID() );
639 $this->assertEquals( $row->rev_id
, $revision->getId() );
640 $this->assertEquals( $row->rev_len
, $revision->getSize() );
641 $this->assertEquals( $row->rev_sha1
, $revision->getSha1() );
642 $this->assertEquals( $row->rev_parent_id
, $revision->getParentId() );
643 $this->assertEquals( $row->rev_timestamp
, $revision->getTimestamp() );
644 $this->assertEquals( $row->rev_comment_text
, $revision->getComment() );
645 $this->assertEquals( $row->rev_user_text
, $revision->getUserText() );
648 public function provideDecompressRevisionText() {
649 yield
'(no legacy encoding), false in false out' => [ false, false, [], false ];
650 yield
'(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
651 yield
'(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
652 yield
'(no legacy encoding), string in with gzip flag returns string' => [
653 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
654 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
656 yield
'(no legacy encoding), string in with object flag returns false' => [
657 // gzip string below generated with serialize( 'JOJO' )
658 false, "s:4:\"JOJO\";", [ 'object' ], false,
660 yield
'(no legacy encoding), serialized object in with object flag returns string' => [
662 // Using a TitleValue object as it has a getText method (which is needed)
663 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
667 yield
'(no legacy encoding), serialized object in with object & gzip flag returns string' => [
669 // Using a TitleValue object as it has a getText method (which is needed)
670 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
671 [ 'object', 'gzip' ],
674 yield
'(ISO-8859-1 encoding), string in string out' => [
676 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
680 yield
'(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
682 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
686 yield
'(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
688 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
692 yield
'(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
694 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
695 [ 'gzip', 'object' ],
701 * @dataProvider provideDecompressRevisionText
702 * @covers Revision::decompressRevisionText
704 * @param bool $legacyEncoding
706 * @param array $flags
707 * @param mixed $expected
709 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
710 $blobStore = $this->getBlobStore();
711 if ( $legacyEncoding ) {
712 $blobStore->setLegacyEncoding( $legacyEncoding, Language
::factory( 'en' ) );
715 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
718 Revision
::decompressRevisionText( $text, $flags )
723 * @covers Revision::getRevisionText
725 public function testGetRevisionText_returnsFalseWhenNoTextField() {
726 $this->assertFalse( Revision
::getRevisionText( new stdClass() ) );
729 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
730 yield
'Just text' => [
731 (object)[ 'old_text' => 'SomeText' ],
735 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
736 yield
'gzip text' => [
738 'old_text' => "sttttr\002\022\000",
739 'old_flags' => 'gzip'
744 yield
'gzip text and different prefix' => [
746 'jojo_text' => "sttttr\002\022\000",
747 'jojo_flags' => 'gzip'
755 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
756 * @covers Revision::getRevisionText
758 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
763 $this->assertSame( $expected, Revision
::getRevisionText( $row, $prefix ) );
766 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
767 yield
'Just some text' => [ 'someNonUrlText' ];
768 yield
'No second URL part' => [ 'someProtocol://' ];
772 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
773 * @covers Revision::getRevisionText
775 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
779 Revision
::getRevisionText(
782 'old_flags' => 'external',
789 * @covers Revision::getRevisionText
791 public function testGetRevisionText_external_noOldId() {
793 'ExternalStoreFactory',
794 new ExternalStoreFactory( [ 'ForTesting' ] )
798 Revision
::getRevisionText(
800 'old_text' => 'ForTesting://cluster1/12345',
801 'old_flags' => 'external,gzip',
808 * @covers Revision::getRevisionText
810 public function testGetRevisionText_external_oldId() {
811 $cache = $this->getWANObjectCache();
812 $this->setService( 'MainWANObjectCache', $cache );
815 'ExternalStoreFactory',
816 new ExternalStoreFactory( [ 'ForTesting' ] )
819 $lb = $this->getMockBuilder( LoadBalancer
::class )
820 ->disableOriginalConstructor()
823 $blobStore = new SqlBlobStore( $lb, $cache );
824 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
828 Revision
::getRevisionText(
830 'old_text' => 'ForTesting://cluster1/12345',
831 'old_flags' => 'external,gzip',
837 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
838 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
842 * @covers Revision::userJoinCond
844 public function testUserJoinCond() {
845 $this->hideDeprecated( 'Revision::userJoinCond' );
847 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
848 Revision
::userJoinCond()
853 * @covers Revision::pageJoinCond
855 public function testPageJoinCond() {
856 $this->hideDeprecated( 'Revision::pageJoinCond' );
858 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
859 Revision
::pageJoinCond()
863 public function provideSelectFields() {
878 'rev_comment_text' => 'rev_comment',
879 'rev_comment_data' => 'NULL',
880 'rev_comment_cid' => 'NULL',
881 'rev_content_format',
899 'rev_comment_text' => 'rev_comment',
900 'rev_comment_data' => 'NULL',
901 'rev_comment_cid' => 'NULL',
907 * @dataProvider provideSelectFields
908 * @covers Revision::selectFields
909 * @todo a true unit test would mock CommentStore
911 public function testSelectFields( $contentHandlerUseDB, $expected ) {
912 $this->hideDeprecated( 'Revision::selectFields' );
913 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
914 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
915 $this->assertEquals( $expected, Revision
::selectFields() );
918 public function provideSelectArchiveFields() {
935 'ar_comment_text' => 'ar_comment',
936 'ar_comment_data' => 'NULL',
937 'ar_comment_cid' => 'NULL',
958 'ar_comment_text' => 'ar_comment',
959 'ar_comment_data' => 'NULL',
960 'ar_comment_cid' => 'NULL',
966 * @dataProvider provideSelectArchiveFields
967 * @covers Revision::selectArchiveFields
968 * @todo a true unit test would mock CommentStore
970 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
971 $this->hideDeprecated( 'Revision::selectArchiveFields' );
972 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
973 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD
);
974 $this->assertEquals( $expected, Revision
::selectArchiveFields() );
978 * @covers Revision::selectTextFields
980 public function testSelectTextFields() {
981 $this->hideDeprecated( 'Revision::selectTextFields' );
987 Revision
::selectTextFields()
992 * @covers Revision::selectPageFields
994 public function testSelectPageFields() {
995 $this->hideDeprecated( 'Revision::selectPageFields' );
1005 Revision
::selectPageFields()
1010 * @covers Revision::selectUserFields
1012 public function testSelectUserFields() {
1013 $this->hideDeprecated( 'Revision::selectUserFields' );
1014 $this->assertEquals(
1018 Revision
::selectUserFields()
1022 public function provideGetArchiveQueryInfo() {
1023 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1025 'wgContentHandlerUseDB' => false,
1026 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1029 'tables' => [ 'archive' ],
1046 'ar_comment_text' => 'ar_comment',
1047 'ar_comment_data' => 'NULL',
1048 'ar_comment_cid' => 'NULL',
1053 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1055 'wgContentHandlerUseDB' => true,
1056 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1059 'tables' => [ 'archive' ],
1076 'ar_comment_text' => 'ar_comment',
1077 'ar_comment_data' => 'NULL',
1078 'ar_comment_cid' => 'NULL',
1079 'ar_content_format',
1085 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1087 'wgContentHandlerUseDB' => false,
1088 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1093 'comment_ar_comment' => 'comment',
1111 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1112 'ar_comment_data' => 'comment_ar_comment.comment_data',
1113 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1116 'comment_ar_comment' => [
1118 'comment_ar_comment.comment_id = ar_comment_id',
1123 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1125 'wgContentHandlerUseDB' => false,
1126 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1131 'comment_ar_comment' => 'comment',
1149 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1150 'ar_comment_data' => 'comment_ar_comment.comment_data',
1151 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1154 'comment_ar_comment' => [
1156 'comment_ar_comment.comment_id = ar_comment_id',
1161 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1163 'wgContentHandlerUseDB' => false,
1164 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1169 'comment_ar_comment' => 'comment',
1187 'ar_comment_text' => 'comment_ar_comment.comment_text',
1188 'ar_comment_data' => 'comment_ar_comment.comment_data',
1189 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1192 'comment_ar_comment' => [
1194 'comment_ar_comment.comment_id = ar_comment_id',
1202 * @covers Revision::getArchiveQueryInfo
1203 * @dataProvider provideGetArchiveQueryInfo
1205 public function testGetArchiveQueryInfo( $globals, $expected ) {
1206 $this->setMwGlobals( $globals );
1208 $revisionStore = $this->getRevisionStore();
1209 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1210 $this->setService( 'RevisionStore', $revisionStore );
1212 $this->assertEquals(
1214 Revision
::getArchiveQueryInfo()
1218 public function provideGetQueryInfo() {
1219 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1221 'wgContentHandlerUseDB' => false,
1222 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1226 'tables' => [ 'revision' ],
1239 'rev_comment_text' => 'rev_comment',
1240 'rev_comment_data' => 'NULL',
1241 'rev_comment_cid' => 'NULL',
1246 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1248 'wgContentHandlerUseDB' => false,
1249 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1253 'tables' => [ 'revision', 'page' ],
1266 'rev_comment_text' => 'rev_comment',
1267 'rev_comment_data' => 'NULL',
1268 'rev_comment_cid' => 'NULL',
1279 [ 'page_id = rev_page' ],
1284 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1286 'wgContentHandlerUseDB' => false,
1287 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1291 'tables' => [ 'revision', 'user' ],
1304 'rev_comment_text' => 'rev_comment',
1305 'rev_comment_data' => 'NULL',
1306 'rev_comment_cid' => 'NULL',
1314 'user_id = rev_user',
1320 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1322 'wgContentHandlerUseDB' => false,
1323 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1327 'tables' => [ 'revision', 'text' ],
1340 'rev_comment_text' => 'rev_comment',
1341 'rev_comment_data' => 'NULL',
1342 'rev_comment_cid' => 'NULL',
1349 [ 'rev_text_id=old_id' ],
1354 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1356 'wgContentHandlerUseDB' => false,
1357 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1359 [ 'text', 'page', 'user' ],
1361 'tables' => [ 'revision', 'page', 'user', 'text' ],
1374 'rev_comment_text' => 'rev_comment',
1375 'rev_comment_data' => 'NULL',
1376 'rev_comment_cid' => 'NULL',
1390 [ 'page_id = rev_page' ],
1396 'user_id = rev_user',
1401 [ 'rev_text_id=old_id' ],
1406 yield
'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1408 'wgContentHandlerUseDB' => true,
1409 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD
,
1413 'tables' => [ 'revision' ],
1426 'rev_comment_text' => 'rev_comment',
1427 'rev_comment_data' => 'NULL',
1428 'rev_comment_cid' => 'NULL',
1429 'rev_content_format',
1430 'rev_content_model',
1435 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1437 'wgContentHandlerUseDB' => false,
1438 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH
,
1444 'temp_rev_comment' => 'revision_comment_temp',
1445 'comment_rev_comment' => 'comment',
1459 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1460 'rev_comment_data' => 'comment_rev_comment.comment_data',
1461 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1464 'temp_rev_comment' => [
1466 'temp_rev_comment.revcomment_rev = rev_id',
1468 'comment_rev_comment' => [
1470 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1475 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1477 'wgContentHandlerUseDB' => false,
1478 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW
,
1484 'temp_rev_comment' => 'revision_comment_temp',
1485 'comment_rev_comment' => 'comment',
1499 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1500 'rev_comment_data' => 'comment_rev_comment.comment_data',
1501 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1504 'temp_rev_comment' => [
1506 'temp_rev_comment.revcomment_rev = rev_id',
1508 'comment_rev_comment' => [
1510 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1515 yield
'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1517 'wgContentHandlerUseDB' => false,
1518 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW
,
1524 'temp_rev_comment' => 'revision_comment_temp',
1525 'comment_rev_comment' => 'comment',
1539 'rev_comment_text' => 'comment_rev_comment.comment_text',
1540 'rev_comment_data' => 'comment_rev_comment.comment_data',
1541 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1544 'temp_rev_comment' => [
1546 'temp_rev_comment.revcomment_rev = rev_id',
1548 'comment_rev_comment' => [
1550 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1558 * @covers Revision::getQueryInfo
1559 * @dataProvider provideGetQueryInfo
1561 public function testGetQueryInfo( $globals, $options, $expected ) {
1562 $this->setMwGlobals( $globals );
1564 $revisionStore = $this->getRevisionStore();
1565 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1566 $this->setService( 'RevisionStore', $revisionStore );
1568 $this->assertEquals(
1570 Revision
::getQueryInfo( $options )
1575 * @covers Revision::getSize
1577 public function testGetSize() {
1578 $title = $this->getMockTitle();
1580 $rec = new MutableRevisionRecord( $title );
1581 $rev = new Revision( $rec, 0, $title );
1583 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1585 $rec->setSize( 13 );
1586 $this->assertSame( 13, $rev->getSize() );
1590 * @covers Revision::getSize
1592 public function testGetSize_failure() {
1593 $title = $this->getMockTitle();
1595 $rec = $this->getMockBuilder( RevisionRecord
::class )
1596 ->disableOriginalConstructor()
1599 $rec->method( 'getSize' )
1600 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1602 $rev = new Revision( $rec, 0, $title );
1603 $this->assertNull( $rev->getSize() );
1607 * @covers Revision::getSha1
1609 public function testGetSha1() {
1610 $title = $this->getMockTitle();
1612 $rec = new MutableRevisionRecord( $title );
1613 $rev = new Revision( $rec, 0, $title );
1615 $emptyHash = SlotRecord
::base36Sha1( '' );
1616 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1618 $rec->setSha1( 'deadbeef' );
1619 $this->assertSame( 'deadbeef', $rev->getSha1() );
1623 * @covers Revision::getSha1
1625 public function testGetSha1_failure() {
1626 $title = $this->getMockTitle();
1628 $rec = $this->getMockBuilder( RevisionRecord
::class )
1629 ->disableOriginalConstructor()
1632 $rec->method( 'getSha1' )
1633 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1635 $rev = new Revision( $rec, 0, $title );
1636 $this->assertNull( $rev->getSha1() );
1640 * @covers Revision::getContent
1642 public function testGetContent() {
1643 $title = $this->getMockTitle();
1645 $rec = new MutableRevisionRecord( $title );
1646 $rev = new Revision( $rec, 0, $title );
1648 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1650 $content = new TextContent( 'Hello Kittens!' );
1651 $rec->setContent( 'main', $content );
1652 $this->assertSame( $content, $rev->getContent() );
1656 * @covers Revision::getContent
1658 public function testGetContent_failure() {
1659 $title = $this->getMockTitle();
1661 $rec = $this->getMockBuilder( RevisionRecord
::class )
1662 ->disableOriginalConstructor()
1665 $rec->method( 'getContent' )
1666 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1668 $rev = new Revision( $rec, 0, $title );
1669 $this->assertNull( $rev->getContent() );