Merge "Handle failure to load content in Revision getSize, etc"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
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;
12
13 /**
14 * Test cases in RevisionTest should not interact with the Database.
15 * For test cases that need Database interaction see RevisionDbTestBase.
16 */
17 class RevisionTest extends MediaWikiTestCase {
18
19 public function provideConstructFromArray() {
20 yield 'with text' => [
21 [
22 'text' => 'hello world.',
23 'content_model' => CONTENT_MODEL_JAVASCRIPT
24 ],
25 ];
26 yield 'with content' => [
27 [
28 'content' => new JavaScriptContent( 'hellow world.' )
29 ],
30 ];
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
33 }
34
35 /**
36 * @param string $model
37 * @return Title
38 */
39 public function getMockTitle( $model = CONTENT_MODEL_WIKITEXT ) {
40 $mock = $this->getMockBuilder( Title::class )
41 ->disableOriginalConstructor()
42 ->getMock();
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 ) );
58
59 return $mock;
60 }
61
62 /**
63 * @dataProvider provideConstructFromArray
64 * @covers Revision::__construct
65 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
66 */
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() );
72 }
73
74 /**
75 * @covers Revision::__construct
76 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
77 */
78 public function testConstructFromEmptyArray() {
79 $rev = new Revision( [], 0, $this->getMockTitle() );
80 $this->assertNull( $rev->getContent(), 'no content object should be available' );
81 }
82
83 public function provideConstructFromArray_userSetAsExpected() {
84 yield 'no user defaults to wgUser' => [
85 [
86 'content' => new JavaScriptContent( 'hello world.' ),
87 ],
88 null,
89 null,
90 ];
91 yield 'user text and id' => [
92 [
93 'content' => new JavaScriptContent( 'hello world.' ),
94 'user_text' => 'SomeTextUserName',
95 'user' => 99,
96
97 ],
98 99,
99 'SomeTextUserName',
100 ];
101 yield 'user text only' => [
102 [
103 'content' => new JavaScriptContent( 'hello world.' ),
104 'user_text' => '111.111.111.111',
105 ],
106 0,
107 '111.111.111.111',
108 ];
109 }
110
111 /**
112 * @dataProvider provideConstructFromArray_userSetAsExpected
113 * @covers Revision::__construct
114 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
115 *
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
119 */
120 public function testConstructFromArray_userSetAsExpected(
121 array $rowArray,
122 $expectedUserId,
123 $expectedUserName
124 ) {
125 $testUser = $this->getTestUser()->getUser();
126 $this->setMwGlobals( 'wgUser', $testUser );
127 if ( $expectedUserId === null ) {
128 $expectedUserId = $testUser->getId();
129 }
130 if ( $expectedUserName === null ) {
131 $expectedUserName = $testUser->getName();
132 }
133
134 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
135 $this->assertEquals( $expectedUserId, $rev->getUser() );
136 $this->assertEquals( $expectedUserName, $rev->getUserText() );
137 }
138
139 public function provideConstructFromArrayThrowsExceptions() {
140 yield 'content and text_id both not empty' => [
141 [
142 'content' => new WikitextContent( 'GOAT' ),
143 'text_id' => 'someid',
144 ],
145 new MWException( "Text already stored in external store (id someid), " .
146 "can't serialize content object" )
147 ];
148 yield 'unknown user id and no user name' => [
149 [
150 'content' => new JavaScriptContent( 'hello world.' ),
151 'user' => 9989,
152 ],
153 new MWException( 'user_text not given, and unknown user ID 9989' )
154 ];
155 yield 'with bad content object (class)' => [
156 [ 'content' => new stdClass() ],
157 new MWException( 'content field must contain a Content object.' )
158 ];
159 yield 'with bad content object (string)' => [
160 [ 'content' => 'ImAGoat' ],
161 new MWException( 'content field must contain a Content object.' )
162 ];
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'
167 )
168 ];
169 }
170
171 /**
172 * @dataProvider provideConstructFromArrayThrowsExceptions
173 * @covers Revision::__construct
174 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
175 */
176 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
177 $this->setExpectedException(
178 get_class( $expectedException ),
179 $expectedException->getMessage(),
180 $expectedException->getCode()
181 );
182 new Revision( $rowArray, 0, $this->getMockTitle() );
183 }
184
185 /**
186 * @covers Revision::__construct
187 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
188 */
189 public function testConstructFromNothing() {
190 $this->setExpectedException(
191 InvalidArgumentException::class
192 );
193 new Revision( [] );
194 }
195
196 public function provideConstructFromRow() {
197 yield 'Full construction' => [
198 [
199 'rev_id' => '42',
200 'rev_page' => '23',
201 'rev_text_id' => '2',
202 'rev_timestamp' => '20171017114835',
203 'rev_user_text' => '127.0.0.1',
204 'rev_user' => '0',
205 'rev_minor_edit' => '0',
206 'rev_deleted' => '0',
207 'rev_len' => '46',
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',
215 ],
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() );
231 }
232 ];
233 yield 'default field values' => [
234 [
235 'rev_id' => '42',
236 'rev_page' => '23',
237 'rev_text_id' => '2',
238 'rev_timestamp' => '20171017114835',
239 'rev_user_text' => '127.0.0.1',
240 'rev_user' => '0',
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,
246 ],
247 function ( RevisionTest $testCase, Revision $rev ) {
248 // parent ID may be null
249 $testCase->assertSame( null, $rev->getParentId(), 'revision id' );
250
251 // given fields
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' );
258
259 // computed fields
260 $testCase->assertNotNull( $rev->getSize(), 'size' );
261 $testCase->assertNotNull( $rev->getSha1(), 'hash' );
262
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' );
266 }
267 ];
268 }
269
270 /**
271 * @dataProvider provideConstructFromRow
272 * @covers Revision::__construct
273 * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
274 */
275 public function testConstructFromRow( array $arrayData, $assertions ) {
276 $data = 'Hello goat.'; // needs to match model and format
277
278 $blobStore = $this->getMockBuilder( SqlBlobStore::class )
279 ->disableOriginalConstructor()
280 ->getMock();
281
282 $blobStore->method( 'getBlob' )
283 ->will( $this->returnValue( $data ) );
284
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 );
293 }
294 ) );
295
296 // Note override internal service, so RevisionStore uses it as well.
297 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
298
299 $row = (object)$arrayData;
300 $rev = new Revision( $row, 0, $this->getMockTitle() );
301 $assertions( $this, $rev );
302 }
303
304 public function provideGetRevisionText() {
305 yield 'Generic test' => [
306 'This is a goat of revision text.',
307 [
308 'old_flags' => '',
309 'old_text' => 'This is a goat of revision text.',
310 ],
311 ];
312 }
313
314 public function provideGetId() {
315 yield [
316 [],
317 null
318 ];
319 yield [
320 [ 'id' => 998 ],
321 998
322 ];
323 }
324
325 /**
326 * @dataProvider provideGetId
327 * @covers Revision::getId
328 */
329 public function testGetId( $rowArray, $expectedId ) {
330 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
331 $this->assertEquals( $expectedId, $rev->getId() );
332 }
333
334 public function provideSetId() {
335 yield [ '123', 123 ];
336 yield [ 456, 456 ];
337 }
338
339 /**
340 * @dataProvider provideSetId
341 * @covers Revision::setId
342 */
343 public function testSetId( $input, $expected ) {
344 $rev = new Revision( [], 0, $this->getMockTitle() );
345 $rev->setId( $input );
346 $this->assertSame( $expected, $rev->getId() );
347 }
348
349 public function provideSetUserIdAndName() {
350 yield [ '123', 123, 'GOaT' ];
351 yield [ 456, 456, 'GOaT' ];
352 }
353
354 /**
355 * @dataProvider provideSetUserIdAndName
356 * @covers Revision::setUserIdAndName
357 */
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 ) );
363 }
364
365 public function provideGetTextId() {
366 yield [ [], null ];
367 yield [ [ 'text_id' => '123' ], 123 ];
368 yield [ [ 'text_id' => 456 ], 456 ];
369 }
370
371 /**
372 * @dataProvider provideGetTextId
373 * @covers Revision::getTextId()
374 */
375 public function testGetTextId( $rowArray, $expected ) {
376 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
377 $this->assertSame( $expected, $rev->getTextId() );
378 }
379
380 public function provideGetParentId() {
381 yield [ [], null ];
382 yield [ [ 'parent_id' => '123' ], 123 ];
383 yield [ [ 'parent_id' => 456 ], 456 ];
384 }
385
386 /**
387 * @dataProvider provideGetParentId
388 * @covers Revision::getParentId()
389 */
390 public function testGetParentId( $rowArray, $expected ) {
391 $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
392 $this->assertSame( $expected, $rev->getParentId() );
393 }
394
395 /**
396 * @covers Revision::getRevisionText
397 * @dataProvider provideGetRevisionText
398 */
399 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
400 $this->assertEquals(
401 $expected,
402 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
403 }
404
405 public function provideGetRevisionTextWithZlibExtension() {
406 yield 'Generic gzip test' => [
407 'This is a small goat of revision text.',
408 [
409 'old_flags' => 'gzip',
410 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
411 ],
412 ];
413 }
414
415 /**
416 * @covers Revision::getRevisionText
417 * @dataProvider provideGetRevisionTextWithZlibExtension
418 */
419 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
420 $this->checkPHPExtension( 'zlib' );
421 $this->testGetRevisionText( $expected, $rowData );
422 }
423
424 private function getWANObjectCache() {
425 return new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
426 }
427
428 /**
429 * @return SqlBlobStore
430 */
431 private function getBlobStore() {
432 /** @var LoadBalancer $lb */
433 $lb = $this->getMockBuilder( LoadBalancer::class )
434 ->disableOriginalConstructor()
435 ->getMock();
436
437 $cache = $this->getWANObjectCache();
438
439 $blobStore = new SqlBlobStore( $lb, $cache );
440 return $blobStore;
441 }
442
443 private function mockBlobStoreFactory( $blobStore ) {
444 /** @var LoadBalancer $lb */
445 $factory = $this->getMockBuilder( BlobStoreFactory::class )
446 ->disableOriginalConstructor()
447 ->getMock();
448 $factory->expects( $this->any() )
449 ->method( 'newBlobStore' )
450 ->willReturn( $blobStore );
451 $factory->expects( $this->any() )
452 ->method( 'newSqlBlobStore' )
453 ->willReturn( $blobStore );
454 return $factory;
455 }
456
457 /**
458 * @return RevisionStore
459 */
460 private function getRevisionStore() {
461 /** @var LoadBalancer $lb */
462 $lb = $this->getMockBuilder( LoadBalancer::class )
463 ->disableOriginalConstructor()
464 ->getMock();
465
466 $cache = $this->getWANObjectCache();
467
468 $blobStore = new RevisionStore( $lb, $this->getBlobStore(), $cache );
469 return $blobStore;
470 }
471
472 public function provideGetRevisionTextWithLegacyEncoding() {
473 yield 'Utf8Native' => [
474 "Wiki est l'\xc3\xa9cole superieur !",
475 'fr',
476 'iso-8859-1',
477 [
478 'old_flags' => 'utf-8',
479 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
480 ]
481 ];
482 yield 'Utf8Legacy' => [
483 "Wiki est l'\xc3\xa9cole superieur !",
484 'fr',
485 'iso-8859-1',
486 [
487 'old_flags' => '',
488 'old_text' => "Wiki est l'\xe9cole superieur !",
489 ]
490 ];
491 }
492
493 /**
494 * @covers Revision::getRevisionText
495 * @dataProvider provideGetRevisionTextWithLegacyEncoding
496 */
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 ) );
501
502 $this->testGetRevisionText( $expected, $rowData );
503 }
504
505 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
506 /**
507 * WARNING!
508 * Do not set the external flag!
509 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
510 */
511 yield 'Utf8NativeGzip' => [
512 "Wiki est l'\xc3\xa9cole superieur !",
513 'fr',
514 'iso-8859-1',
515 [
516 'old_flags' => 'gzip,utf-8',
517 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
518 ]
519 ];
520 yield 'Utf8LegacyGzip' => [
521 "Wiki est l'\xc3\xa9cole superieur !",
522 'fr',
523 'iso-8859-1',
524 [
525 'old_flags' => 'gzip',
526 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
527 ]
528 ];
529 }
530
531 /**
532 * @covers Revision::getRevisionText
533 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
534 */
535 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
536 $this->checkPHPExtension( 'zlib' );
537
538 $blobStore = $this->getBlobStore();
539 $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
540 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
541
542 $this->testGetRevisionText( $expected, $rowData );
543 }
544
545 /**
546 * @covers Revision::compressRevisionText
547 */
548 public function testCompressRevisionTextUtf8() {
549 $row = new stdClass;
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" );
560 }
561
562 /**
563 * @covers Revision::compressRevisionText
564 */
565 public function testCompressRevisionTextUtf8Gzip() {
566 $this->checkPHPExtension( 'zlib' );
567
568 $blobStore = $this->getBlobStore();
569 $blobStore->setCompressBlobs( true );
570 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
571
572 $row = new stdClass;
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" );
583 }
584
585 /**
586 * @covers Revision::loadFromTitle
587 */
588 public function testLoadFromTitle() {
589 $title = $this->getMockTitle();
590
591 $conditions = [
592 'rev_id=page_latest',
593 'page_namespace' => $title->getNamespace(),
594 'page_title' => $title->getDBkey()
595 ];
596
597 $row = (object)[
598 'rev_id' => '42',
599 'rev_page' => $title->getArticleID(),
600 'rev_text_id' => '2',
601 'rev_timestamp' => '20171017114835',
602 'rev_user_text' => '127.0.0.1',
603 'rev_user' => '0',
604 'rev_minor_edit' => '0',
605 'rev_deleted' => '0',
606 'rev_len' => '46',
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',
614 ];
615
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' )
622 ->with(
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 ),
627 // Method name
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' )
633 )
634 ->willReturn( $row );
635
636 $revision = Revision::loadFromTitle( $db, $title );
637
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() );
646 }
647
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',
655 ];
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,
659 ];
660 yield '(no legacy encoding), serialized object in with object flag returns string' => [
661 false,
662 // Using a TitleValue object as it has a getText method (which is needed)
663 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
664 [ 'object' ],
665 'HHJJDDFF',
666 ];
667 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
668 false,
669 // Using a TitleValue object as it has a getText method (which is needed)
670 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
671 [ 'object', 'gzip' ],
672 '8219JJJ840',
673 ];
674 yield '(ISO-8859-1 encoding), string in string out' => [
675 'ISO-8859-1',
676 iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ),
677 [],
678 '1®Àþ1',
679 ];
680 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
681 'ISO-8859-1',
682 gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ),
683 [ 'gzip' ],
684 '4®Àþ4',
685 ];
686 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
687 'ISO-8859-1',
688 serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ),
689 [ 'object' ],
690 '3®Àþ3',
691 ];
692 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
693 'ISO-8859-1',
694 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
695 [ 'gzip', 'object' ],
696 '2®Àþ2',
697 ];
698 }
699
700 /**
701 * @dataProvider provideDecompressRevisionText
702 * @covers Revision::decompressRevisionText
703 *
704 * @param bool $legacyEncoding
705 * @param mixed $text
706 * @param array $flags
707 * @param mixed $expected
708 */
709 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
710 $blobStore = $this->getBlobStore();
711 if ( $legacyEncoding ) {
712 $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
713 }
714
715 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
716 $this->assertSame(
717 $expected,
718 Revision::decompressRevisionText( $text, $flags )
719 );
720 }
721
722 /**
723 * @covers Revision::getRevisionText
724 */
725 public function testGetRevisionText_returnsFalseWhenNoTextField() {
726 $this->assertFalse( Revision::getRevisionText( new stdClass() ) );
727 }
728
729 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
730 yield 'Just text' => [
731 (object)[ 'old_text' => 'SomeText' ],
732 'old_',
733 'SomeText'
734 ];
735 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
736 yield 'gzip text' => [
737 (object)[
738 'old_text' => "sttttr\002\022\000",
739 'old_flags' => 'gzip'
740 ],
741 'old_',
742 'AAAABBAAA'
743 ];
744 yield 'gzip text and different prefix' => [
745 (object)[
746 'jojo_text' => "sttttr\002\022\000",
747 'jojo_flags' => 'gzip'
748 ],
749 'jojo_',
750 'AAAABBAAA'
751 ];
752 }
753
754 /**
755 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
756 * @covers Revision::getRevisionText
757 */
758 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
759 $row,
760 $prefix,
761 $expected
762 ) {
763 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
764 }
765
766 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
767 yield 'Just some text' => [ 'someNonUrlText' ];
768 yield 'No second URL part' => [ 'someProtocol://' ];
769 }
770
771 /**
772 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
773 * @covers Revision::getRevisionText
774 */
775 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
776 $text
777 ) {
778 $this->assertFalse(
779 Revision::getRevisionText(
780 (object)[
781 'old_text' => $text,
782 'old_flags' => 'external',
783 ]
784 )
785 );
786 }
787
788 /**
789 * @covers Revision::getRevisionText
790 */
791 public function testGetRevisionText_external_noOldId() {
792 $this->setService(
793 'ExternalStoreFactory',
794 new ExternalStoreFactory( [ 'ForTesting' ] )
795 );
796 $this->assertSame(
797 'AAAABBAAA',
798 Revision::getRevisionText(
799 (object)[
800 'old_text' => 'ForTesting://cluster1/12345',
801 'old_flags' => 'external,gzip',
802 ]
803 )
804 );
805 }
806
807 /**
808 * @covers Revision::getRevisionText
809 */
810 public function testGetRevisionText_external_oldId() {
811 $cache = $this->getWANObjectCache();
812 $this->setService( 'MainWANObjectCache', $cache );
813
814 $this->setService(
815 'ExternalStoreFactory',
816 new ExternalStoreFactory( [ 'ForTesting' ] )
817 );
818
819 $lb = $this->getMockBuilder( LoadBalancer::class )
820 ->disableOriginalConstructor()
821 ->getMock();
822
823 $blobStore = new SqlBlobStore( $lb, $cache );
824 $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
825
826 $this->assertSame(
827 'AAAABBAAA',
828 Revision::getRevisionText(
829 (object)[
830 'old_text' => 'ForTesting://cluster1/12345',
831 'old_flags' => 'external,gzip',
832 'old_id' => '7777',
833 ]
834 )
835 );
836
837 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', 'tt:7777' );
838 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
839 }
840
841 /**
842 * @covers Revision::userJoinCond
843 */
844 public function testUserJoinCond() {
845 $this->hideDeprecated( 'Revision::userJoinCond' );
846 $this->assertEquals(
847 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
848 Revision::userJoinCond()
849 );
850 }
851
852 /**
853 * @covers Revision::pageJoinCond
854 */
855 public function testPageJoinCond() {
856 $this->hideDeprecated( 'Revision::pageJoinCond' );
857 $this->assertEquals(
858 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
859 Revision::pageJoinCond()
860 );
861 }
862
863 public function provideSelectFields() {
864 yield [
865 true,
866 [
867 'rev_id',
868 'rev_page',
869 'rev_text_id',
870 'rev_timestamp',
871 'rev_user_text',
872 'rev_user',
873 'rev_minor_edit',
874 'rev_deleted',
875 'rev_len',
876 'rev_parent_id',
877 'rev_sha1',
878 'rev_comment_text' => 'rev_comment',
879 'rev_comment_data' => 'NULL',
880 'rev_comment_cid' => 'NULL',
881 'rev_content_format',
882 'rev_content_model',
883 ]
884 ];
885 yield [
886 false,
887 [
888 'rev_id',
889 'rev_page',
890 'rev_text_id',
891 'rev_timestamp',
892 'rev_user_text',
893 'rev_user',
894 'rev_minor_edit',
895 'rev_deleted',
896 'rev_len',
897 'rev_parent_id',
898 'rev_sha1',
899 'rev_comment_text' => 'rev_comment',
900 'rev_comment_data' => 'NULL',
901 'rev_comment_cid' => 'NULL',
902 ]
903 ];
904 }
905
906 /**
907 * @dataProvider provideSelectFields
908 * @covers Revision::selectFields
909 * @todo a true unit test would mock CommentStore
910 */
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() );
916 }
917
918 public function provideSelectArchiveFields() {
919 yield [
920 true,
921 [
922 'ar_id',
923 'ar_page_id',
924 'ar_rev_id',
925 'ar_text',
926 'ar_text_id',
927 'ar_timestamp',
928 'ar_user_text',
929 'ar_user',
930 'ar_minor_edit',
931 'ar_deleted',
932 'ar_len',
933 'ar_parent_id',
934 'ar_sha1',
935 'ar_comment_text' => 'ar_comment',
936 'ar_comment_data' => 'NULL',
937 'ar_comment_cid' => 'NULL',
938 'ar_content_format',
939 'ar_content_model',
940 ]
941 ];
942 yield [
943 false,
944 [
945 'ar_id',
946 'ar_page_id',
947 'ar_rev_id',
948 'ar_text',
949 'ar_text_id',
950 'ar_timestamp',
951 'ar_user_text',
952 'ar_user',
953 'ar_minor_edit',
954 'ar_deleted',
955 'ar_len',
956 'ar_parent_id',
957 'ar_sha1',
958 'ar_comment_text' => 'ar_comment',
959 'ar_comment_data' => 'NULL',
960 'ar_comment_cid' => 'NULL',
961 ]
962 ];
963 }
964
965 /**
966 * @dataProvider provideSelectArchiveFields
967 * @covers Revision::selectArchiveFields
968 * @todo a true unit test would mock CommentStore
969 */
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() );
975 }
976
977 /**
978 * @covers Revision::selectTextFields
979 */
980 public function testSelectTextFields() {
981 $this->hideDeprecated( 'Revision::selectTextFields' );
982 $this->assertEquals(
983 [
984 'old_text',
985 'old_flags',
986 ],
987 Revision::selectTextFields()
988 );
989 }
990
991 /**
992 * @covers Revision::selectPageFields
993 */
994 public function testSelectPageFields() {
995 $this->hideDeprecated( 'Revision::selectPageFields' );
996 $this->assertEquals(
997 [
998 'page_namespace',
999 'page_title',
1000 'page_id',
1001 'page_latest',
1002 'page_is_redirect',
1003 'page_len',
1004 ],
1005 Revision::selectPageFields()
1006 );
1007 }
1008
1009 /**
1010 * @covers Revision::selectUserFields
1011 */
1012 public function testSelectUserFields() {
1013 $this->hideDeprecated( 'Revision::selectUserFields' );
1014 $this->assertEquals(
1015 [
1016 'user_name',
1017 ],
1018 Revision::selectUserFields()
1019 );
1020 }
1021
1022 public function provideGetArchiveQueryInfo() {
1023 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1024 [
1025 'wgContentHandlerUseDB' => false,
1026 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1027 ],
1028 [
1029 'tables' => [ 'archive' ],
1030 'fields' => [
1031 'ar_id',
1032 'ar_page_id',
1033 'ar_namespace',
1034 'ar_title',
1035 'ar_rev_id',
1036 'ar_text',
1037 'ar_text_id',
1038 'ar_timestamp',
1039 'ar_user_text',
1040 'ar_user',
1041 'ar_minor_edit',
1042 'ar_deleted',
1043 'ar_len',
1044 'ar_parent_id',
1045 'ar_sha1',
1046 'ar_comment_text' => 'ar_comment',
1047 'ar_comment_data' => 'NULL',
1048 'ar_comment_cid' => 'NULL',
1049 ],
1050 'joins' => [],
1051 ]
1052 ];
1053 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1054 [
1055 'wgContentHandlerUseDB' => true,
1056 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1057 ],
1058 [
1059 'tables' => [ 'archive' ],
1060 'fields' => [
1061 'ar_id',
1062 'ar_page_id',
1063 'ar_namespace',
1064 'ar_title',
1065 'ar_rev_id',
1066 'ar_text',
1067 'ar_text_id',
1068 'ar_timestamp',
1069 'ar_user_text',
1070 'ar_user',
1071 'ar_minor_edit',
1072 'ar_deleted',
1073 'ar_len',
1074 'ar_parent_id',
1075 'ar_sha1',
1076 'ar_comment_text' => 'ar_comment',
1077 'ar_comment_data' => 'NULL',
1078 'ar_comment_cid' => 'NULL',
1079 'ar_content_format',
1080 'ar_content_model',
1081 ],
1082 'joins' => [],
1083 ]
1084 ];
1085 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1086 [
1087 'wgContentHandlerUseDB' => false,
1088 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
1089 ],
1090 [
1091 'tables' => [
1092 'archive',
1093 'comment_ar_comment' => 'comment',
1094 ],
1095 'fields' => [
1096 'ar_id',
1097 'ar_page_id',
1098 'ar_namespace',
1099 'ar_title',
1100 'ar_rev_id',
1101 'ar_text',
1102 'ar_text_id',
1103 'ar_timestamp',
1104 'ar_user_text',
1105 'ar_user',
1106 'ar_minor_edit',
1107 'ar_deleted',
1108 'ar_len',
1109 'ar_parent_id',
1110 'ar_sha1',
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',
1114 ],
1115 'joins' => [
1116 'comment_ar_comment' => [
1117 'LEFT JOIN',
1118 'comment_ar_comment.comment_id = ar_comment_id',
1119 ],
1120 ],
1121 ]
1122 ];
1123 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1124 [
1125 'wgContentHandlerUseDB' => false,
1126 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
1127 ],
1128 [
1129 'tables' => [
1130 'archive',
1131 'comment_ar_comment' => 'comment',
1132 ],
1133 'fields' => [
1134 'ar_id',
1135 'ar_page_id',
1136 'ar_namespace',
1137 'ar_title',
1138 'ar_rev_id',
1139 'ar_text',
1140 'ar_text_id',
1141 'ar_timestamp',
1142 'ar_user_text',
1143 'ar_user',
1144 'ar_minor_edit',
1145 'ar_deleted',
1146 'ar_len',
1147 'ar_parent_id',
1148 'ar_sha1',
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',
1152 ],
1153 'joins' => [
1154 'comment_ar_comment' => [
1155 'LEFT JOIN',
1156 'comment_ar_comment.comment_id = ar_comment_id',
1157 ],
1158 ],
1159 ]
1160 ];
1161 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1162 [
1163 'wgContentHandlerUseDB' => false,
1164 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
1165 ],
1166 [
1167 'tables' => [
1168 'archive',
1169 'comment_ar_comment' => 'comment',
1170 ],
1171 'fields' => [
1172 'ar_id',
1173 'ar_page_id',
1174 'ar_namespace',
1175 'ar_title',
1176 'ar_rev_id',
1177 'ar_text',
1178 'ar_text_id',
1179 'ar_timestamp',
1180 'ar_user_text',
1181 'ar_user',
1182 'ar_minor_edit',
1183 'ar_deleted',
1184 'ar_len',
1185 'ar_parent_id',
1186 'ar_sha1',
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',
1190 ],
1191 'joins' => [
1192 'comment_ar_comment' => [
1193 'JOIN',
1194 'comment_ar_comment.comment_id = ar_comment_id',
1195 ],
1196 ],
1197 ]
1198 ];
1199 }
1200
1201 /**
1202 * @covers Revision::getArchiveQueryInfo
1203 * @dataProvider provideGetArchiveQueryInfo
1204 */
1205 public function testGetArchiveQueryInfo( $globals, $expected ) {
1206 $this->setMwGlobals( $globals );
1207
1208 $revisionStore = $this->getRevisionStore();
1209 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1210 $this->setService( 'RevisionStore', $revisionStore );
1211
1212 $this->assertEquals(
1213 $expected,
1214 Revision::getArchiveQueryInfo()
1215 );
1216 }
1217
1218 public function provideGetQueryInfo() {
1219 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1220 [
1221 'wgContentHandlerUseDB' => false,
1222 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1223 ],
1224 [],
1225 [
1226 'tables' => [ 'revision' ],
1227 'fields' => [
1228 'rev_id',
1229 'rev_page',
1230 'rev_text_id',
1231 'rev_timestamp',
1232 'rev_user_text',
1233 'rev_user',
1234 'rev_minor_edit',
1235 'rev_deleted',
1236 'rev_len',
1237 'rev_parent_id',
1238 'rev_sha1',
1239 'rev_comment_text' => 'rev_comment',
1240 'rev_comment_data' => 'NULL',
1241 'rev_comment_cid' => 'NULL',
1242 ],
1243 'joins' => [],
1244 ],
1245 ];
1246 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1247 [
1248 'wgContentHandlerUseDB' => false,
1249 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1250 ],
1251 [ 'page' ],
1252 [
1253 'tables' => [ 'revision', 'page' ],
1254 'fields' => [
1255 'rev_id',
1256 'rev_page',
1257 'rev_text_id',
1258 'rev_timestamp',
1259 'rev_user_text',
1260 'rev_user',
1261 'rev_minor_edit',
1262 'rev_deleted',
1263 'rev_len',
1264 'rev_parent_id',
1265 'rev_sha1',
1266 'rev_comment_text' => 'rev_comment',
1267 'rev_comment_data' => 'NULL',
1268 'rev_comment_cid' => 'NULL',
1269 'page_namespace',
1270 'page_title',
1271 'page_id',
1272 'page_latest',
1273 'page_is_redirect',
1274 'page_len',
1275 ],
1276 'joins' => [
1277 'page' => [
1278 'INNER JOIN',
1279 [ 'page_id = rev_page' ],
1280 ],
1281 ],
1282 ],
1283 ];
1284 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1285 [
1286 'wgContentHandlerUseDB' => false,
1287 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1288 ],
1289 [ 'user' ],
1290 [
1291 'tables' => [ 'revision', 'user' ],
1292 'fields' => [
1293 'rev_id',
1294 'rev_page',
1295 'rev_text_id',
1296 'rev_timestamp',
1297 'rev_user_text',
1298 'rev_user',
1299 'rev_minor_edit',
1300 'rev_deleted',
1301 'rev_len',
1302 'rev_parent_id',
1303 'rev_sha1',
1304 'rev_comment_text' => 'rev_comment',
1305 'rev_comment_data' => 'NULL',
1306 'rev_comment_cid' => 'NULL',
1307 'user_name',
1308 ],
1309 'joins' => [
1310 'user' => [
1311 'LEFT JOIN',
1312 [
1313 'rev_user != 0',
1314 'user_id = rev_user',
1315 ],
1316 ],
1317 ],
1318 ],
1319 ];
1320 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1321 [
1322 'wgContentHandlerUseDB' => false,
1323 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1324 ],
1325 [ 'text' ],
1326 [
1327 'tables' => [ 'revision', 'text' ],
1328 'fields' => [
1329 'rev_id',
1330 'rev_page',
1331 'rev_text_id',
1332 'rev_timestamp',
1333 'rev_user_text',
1334 'rev_user',
1335 'rev_minor_edit',
1336 'rev_deleted',
1337 'rev_len',
1338 'rev_parent_id',
1339 'rev_sha1',
1340 'rev_comment_text' => 'rev_comment',
1341 'rev_comment_data' => 'NULL',
1342 'rev_comment_cid' => 'NULL',
1343 'old_text',
1344 'old_flags',
1345 ],
1346 'joins' => [
1347 'text' => [
1348 'INNER JOIN',
1349 [ 'rev_text_id=old_id' ],
1350 ],
1351 ],
1352 ],
1353 ];
1354 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1355 [
1356 'wgContentHandlerUseDB' => false,
1357 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1358 ],
1359 [ 'text', 'page', 'user' ],
1360 [
1361 'tables' => [ 'revision', 'page', 'user', 'text' ],
1362 'fields' => [
1363 'rev_id',
1364 'rev_page',
1365 'rev_text_id',
1366 'rev_timestamp',
1367 'rev_user_text',
1368 'rev_user',
1369 'rev_minor_edit',
1370 'rev_deleted',
1371 'rev_len',
1372 'rev_parent_id',
1373 'rev_sha1',
1374 'rev_comment_text' => 'rev_comment',
1375 'rev_comment_data' => 'NULL',
1376 'rev_comment_cid' => 'NULL',
1377 'page_namespace',
1378 'page_title',
1379 'page_id',
1380 'page_latest',
1381 'page_is_redirect',
1382 'page_len',
1383 'user_name',
1384 'old_text',
1385 'old_flags',
1386 ],
1387 'joins' => [
1388 'page' => [
1389 'INNER JOIN',
1390 [ 'page_id = rev_page' ],
1391 ],
1392 'user' => [
1393 'LEFT JOIN',
1394 [
1395 'rev_user != 0',
1396 'user_id = rev_user',
1397 ],
1398 ],
1399 'text' => [
1400 'INNER JOIN',
1401 [ 'rev_text_id=old_id' ],
1402 ],
1403 ],
1404 ],
1405 ];
1406 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1407 [
1408 'wgContentHandlerUseDB' => true,
1409 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1410 ],
1411 [],
1412 [
1413 'tables' => [ 'revision' ],
1414 'fields' => [
1415 'rev_id',
1416 'rev_page',
1417 'rev_text_id',
1418 'rev_timestamp',
1419 'rev_user_text',
1420 'rev_user',
1421 'rev_minor_edit',
1422 'rev_deleted',
1423 'rev_len',
1424 'rev_parent_id',
1425 'rev_sha1',
1426 'rev_comment_text' => 'rev_comment',
1427 'rev_comment_data' => 'NULL',
1428 'rev_comment_cid' => 'NULL',
1429 'rev_content_format',
1430 'rev_content_model',
1431 ],
1432 'joins' => [],
1433 ],
1434 ];
1435 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1436 [
1437 'wgContentHandlerUseDB' => false,
1438 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
1439 ],
1440 [],
1441 [
1442 'tables' => [
1443 'revision',
1444 'temp_rev_comment' => 'revision_comment_temp',
1445 'comment_rev_comment' => 'comment',
1446 ],
1447 'fields' => [
1448 'rev_id',
1449 'rev_page',
1450 'rev_text_id',
1451 'rev_timestamp',
1452 'rev_user_text',
1453 'rev_user',
1454 'rev_minor_edit',
1455 'rev_deleted',
1456 'rev_len',
1457 'rev_parent_id',
1458 'rev_sha1',
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',
1462 ],
1463 'joins' => [
1464 'temp_rev_comment' => [
1465 'LEFT JOIN',
1466 'temp_rev_comment.revcomment_rev = rev_id',
1467 ],
1468 'comment_rev_comment' => [
1469 'LEFT JOIN',
1470 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1471 ],
1472 ],
1473 ],
1474 ];
1475 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1476 [
1477 'wgContentHandlerUseDB' => false,
1478 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
1479 ],
1480 [],
1481 [
1482 'tables' => [
1483 'revision',
1484 'temp_rev_comment' => 'revision_comment_temp',
1485 'comment_rev_comment' => 'comment',
1486 ],
1487 'fields' => [
1488 'rev_id',
1489 'rev_page',
1490 'rev_text_id',
1491 'rev_timestamp',
1492 'rev_user_text',
1493 'rev_user',
1494 'rev_minor_edit',
1495 'rev_deleted',
1496 'rev_len',
1497 'rev_parent_id',
1498 'rev_sha1',
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',
1502 ],
1503 'joins' => [
1504 'temp_rev_comment' => [
1505 'LEFT JOIN',
1506 'temp_rev_comment.revcomment_rev = rev_id',
1507 ],
1508 'comment_rev_comment' => [
1509 'LEFT JOIN',
1510 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1511 ],
1512 ],
1513 ],
1514 ];
1515 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1516 [
1517 'wgContentHandlerUseDB' => false,
1518 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
1519 ],
1520 [],
1521 [
1522 'tables' => [
1523 'revision',
1524 'temp_rev_comment' => 'revision_comment_temp',
1525 'comment_rev_comment' => 'comment',
1526 ],
1527 'fields' => [
1528 'rev_id',
1529 'rev_page',
1530 'rev_text_id',
1531 'rev_timestamp',
1532 'rev_user_text',
1533 'rev_user',
1534 'rev_minor_edit',
1535 'rev_deleted',
1536 'rev_len',
1537 'rev_parent_id',
1538 'rev_sha1',
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',
1542 ],
1543 'joins' => [
1544 'temp_rev_comment' => [
1545 'JOIN',
1546 'temp_rev_comment.revcomment_rev = rev_id',
1547 ],
1548 'comment_rev_comment' => [
1549 'JOIN',
1550 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1551 ],
1552 ],
1553 ],
1554 ];
1555 }
1556
1557 /**
1558 * @covers Revision::getQueryInfo
1559 * @dataProvider provideGetQueryInfo
1560 */
1561 public function testGetQueryInfo( $globals, $options, $expected ) {
1562 $this->setMwGlobals( $globals );
1563
1564 $revisionStore = $this->getRevisionStore();
1565 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1566 $this->setService( 'RevisionStore', $revisionStore );
1567
1568 $this->assertEquals(
1569 $expected,
1570 Revision::getQueryInfo( $options )
1571 );
1572 }
1573
1574 /**
1575 * @covers Revision::getSize
1576 */
1577 public function testGetSize() {
1578 $title = $this->getMockTitle();
1579
1580 $rec = new MutableRevisionRecord( $title );
1581 $rev = new Revision( $rec, 0, $title );
1582
1583 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1584
1585 $rec->setSize( 13 );
1586 $this->assertSame( 13, $rev->getSize() );
1587 }
1588
1589 /**
1590 * @covers Revision::getSize
1591 */
1592 public function testGetSize_failure() {
1593 $title = $this->getMockTitle();
1594
1595 $rec = $this->getMockBuilder( RevisionRecord::class )
1596 ->disableOriginalConstructor()
1597 ->getMock();
1598
1599 $rec->method( 'getSize' )
1600 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1601
1602 $rev = new Revision( $rec, 0, $title );
1603 $this->assertNull( $rev->getSize() );
1604 }
1605
1606 /**
1607 * @covers Revision::getSha1
1608 */
1609 public function testGetSha1() {
1610 $title = $this->getMockTitle();
1611
1612 $rec = new MutableRevisionRecord( $title );
1613 $rev = new Revision( $rec, 0, $title );
1614
1615 $emptyHash = SlotRecord::base36Sha1( '' );
1616 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1617
1618 $rec->setSha1( 'deadbeef' );
1619 $this->assertSame( 'deadbeef', $rev->getSha1() );
1620 }
1621
1622 /**
1623 * @covers Revision::getSha1
1624 */
1625 public function testGetSha1_failure() {
1626 $title = $this->getMockTitle();
1627
1628 $rec = $this->getMockBuilder( RevisionRecord::class )
1629 ->disableOriginalConstructor()
1630 ->getMock();
1631
1632 $rec->method( 'getSha1' )
1633 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1634
1635 $rev = new Revision( $rec, 0, $title );
1636 $this->assertNull( $rev->getSha1() );
1637 }
1638
1639 /**
1640 * @covers Revision::getContent
1641 */
1642 public function testGetContent() {
1643 $title = $this->getMockTitle();
1644
1645 $rec = new MutableRevisionRecord( $title );
1646 $rev = new Revision( $rec, 0, $title );
1647
1648 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1649
1650 $content = new TextContent( 'Hello Kittens!' );
1651 $rec->setContent( 'main', $content );
1652 $this->assertSame( $content, $rev->getContent() );
1653 }
1654
1655 /**
1656 * @covers Revision::getContent
1657 */
1658 public function testGetContent_failure() {
1659 $title = $this->getMockTitle();
1660
1661 $rec = $this->getMockBuilder( RevisionRecord::class )
1662 ->disableOriginalConstructor()
1663 ->getMock();
1664
1665 $rec->method( 'getContent' )
1666 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1667
1668 $rev = new Revision( $rec, 0, $title );
1669 $this->assertNull( $rev->getContent() );
1670 }
1671
1672 }