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