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