Merge "Reduce lag waiting time in CategoryMembershipUpdateJob critical section"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
6 * Test cases in RevisionTest should not interact with the Database.
7 * For test cases that need Database interaction see RevisionDbTestBase.
8 */
9 class RevisionTest extends MediaWikiTestCase {
10
11 public function provideConstructFromArray() {
12 yield 'with text' => [
13 [
14 'text' => 'hello world.',
15 'content_model' => CONTENT_MODEL_JAVASCRIPT
16 ],
17 ];
18 yield 'with content' => [
19 [
20 'content' => new JavaScriptContent( 'hellow world.' )
21 ],
22 ];
23 }
24
25 /**
26 * @dataProvider provideConstructFromArray
27 * @covers Revision::__construct
28 * @covers Revision::constructFromRowArray
29 */
30 public function testConstructFromArray( array $rowArray ) {
31 $rev = new Revision( $rowArray );
32 $this->assertNotNull( $rev->getContent(), 'no content object available' );
33 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
34 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() );
35 }
36
37 public function provideConstructFromArrayThrowsExceptions() {
38 yield 'content and text_id both not empty' => [
39 [
40 'content' => new WikitextContent( 'GOAT' ),
41 'text_id' => 'someid',
42 ],
43 new MWException( "Text already stored in external store (id someid), " .
44 "can't serialize content object" )
45 ];
46 yield 'with bad content object (class)' => [
47 [ 'content' => new stdClass() ],
48 new MWException( '`content` field must contain a Content object.' )
49 ];
50 yield 'with bad content object (string)' => [
51 [ 'content' => 'ImAGoat' ],
52 new MWException( '`content` field must contain a Content object.' )
53 ];
54 yield 'bad row format' => [
55 'imastring, not a row',
56 new MWException( 'Revision constructor passed invalid row format.' )
57 ];
58 }
59
60 /**
61 * @dataProvider provideConstructFromArrayThrowsExceptions
62 * @covers Revision::__construct
63 * @covers Revision::constructFromRowArray
64 */
65 public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
66 $this->setExpectedException(
67 get_class( $expectedException ),
68 $expectedException->getMessage(),
69 $expectedException->getCode()
70 );
71 new Revision( $rowArray );
72 }
73
74 public function provideConstructFromRow() {
75 yield 'Full construction' => [
76 [
77 'rev_id' => '2',
78 'rev_page' => '1',
79 'rev_text_id' => '2',
80 'rev_timestamp' => '20171017114835',
81 'rev_user_text' => '127.0.0.1',
82 'rev_user' => '0',
83 'rev_minor_edit' => '0',
84 'rev_deleted' => '0',
85 'rev_len' => '46',
86 'rev_parent_id' => '1',
87 'rev_sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z',
88 'rev_comment_text' => 'Goat Comment!',
89 'rev_comment_data' => null,
90 'rev_comment_cid' => null,
91 'rev_content_format' => 'GOATFORMAT',
92 'rev_content_model' => 'GOATMODEL',
93 ],
94 function ( RevisionTest $testCase, Revision $rev ) {
95 $testCase->assertSame( 2, $rev->getId() );
96 $testCase->assertSame( 1, $rev->getPage() );
97 $testCase->assertSame( 2, $rev->getTextId() );
98 $testCase->assertSame( '20171017114835', $rev->getTimestamp() );
99 $testCase->assertSame( '127.0.0.1', $rev->getUserText() );
100 $testCase->assertSame( 0, $rev->getUser() );
101 $testCase->assertSame( false, $rev->isMinor() );
102 $testCase->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
103 $testCase->assertSame( 46, $rev->getSize() );
104 $testCase->assertSame( 1, $rev->getParentId() );
105 $testCase->assertSame( 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', $rev->getSha1() );
106 $testCase->assertSame( 'Goat Comment!', $rev->getComment() );
107 $testCase->assertSame( 'GOATFORMAT', $rev->getContentFormat() );
108 $testCase->assertSame( 'GOATMODEL', $rev->getContentModel() );
109 }
110 ];
111 yield 'null fields' => [
112 [
113 'rev_id' => '2',
114 'rev_page' => '1',
115 'rev_text_id' => '2',
116 'rev_timestamp' => '20171017114835',
117 'rev_user_text' => '127.0.0.1',
118 'rev_user' => '0',
119 'rev_minor_edit' => '0',
120 'rev_deleted' => '0',
121 'rev_comment_text' => 'Goat Comment!',
122 'rev_comment_data' => null,
123 'rev_comment_cid' => null,
124 ],
125 function ( RevisionTest $testCase, Revision $rev ) {
126 $testCase->assertNull( $rev->getSize() );
127 $testCase->assertNull( $rev->getParentId() );
128 $testCase->assertNull( $rev->getSha1() );
129 $testCase->assertSame( 'text/x-wiki', $rev->getContentFormat() );
130 $testCase->assertSame( 'wikitext', $rev->getContentModel() );
131 }
132 ];
133 }
134
135 /**
136 * @dataProvider provideConstructFromRow
137 * @covers Revision::__construct
138 * @covers Revision::constructFromDbRowObject
139 */
140 public function testConstructFromRow( array $arrayData, $assertions ) {
141 $row = (object)$arrayData;
142 $rev = new Revision( $row );
143 $assertions( $this, $rev );
144 }
145
146 public function provideGetRevisionText() {
147 yield 'Generic test' => [
148 'This is a goat of revision text.',
149 [
150 'old_flags' => '',
151 'old_text' => 'This is a goat of revision text.',
152 ],
153 ];
154 }
155
156 public function provideGetId() {
157 yield [
158 [],
159 null
160 ];
161 yield [
162 [ 'id' => 998 ],
163 998
164 ];
165 }
166
167 /**
168 * @dataProvider provideGetId
169 * @covers Revision::getId
170 */
171 public function testGetId( $rowArray, $expectedId ) {
172 $rev = new Revision( $rowArray );
173 $this->assertEquals( $expectedId, $rev->getId() );
174 }
175
176 public function provideSetId() {
177 yield [ '123', 123 ];
178 yield [ 456, 456 ];
179 }
180
181 /**
182 * @dataProvider provideSetId
183 * @covers Revision::setId
184 */
185 public function testSetId( $input, $expected ) {
186 $rev = new Revision( [] );
187 $rev->setId( $input );
188 $this->assertSame( $expected, $rev->getId() );
189 }
190
191 public function provideSetUserIdAndName() {
192 yield [ '123', 123, 'GOaT' ];
193 yield [ 456, 456, 'GOaT' ];
194 }
195
196 /**
197 * @dataProvider provideSetUserIdAndName
198 * @covers Revision::setUserIdAndName
199 */
200 public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
201 $rev = new Revision( [] );
202 $rev->setUserIdAndName( $inputId, $name );
203 $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
204 $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
205 }
206
207 public function provideGetTextId() {
208 yield [ [], null ];
209 yield [ [ 'text_id' => '123' ], 123 ];
210 yield [ [ 'text_id' => 456 ], 456 ];
211 }
212
213 /**
214 * @dataProvider provideGetTextId
215 * @covers Revision::getTextId()
216 */
217 public function testGetTextId( $rowArray, $expected ) {
218 $rev = new Revision( $rowArray );
219 $this->assertSame( $expected, $rev->getTextId() );
220 }
221
222 public function provideGetParentId() {
223 yield [ [], null ];
224 yield [ [ 'parent_id' => '123' ], 123 ];
225 yield [ [ 'parent_id' => 456 ], 456 ];
226 }
227
228 /**
229 * @dataProvider provideGetParentId
230 * @covers Revision::getParentId()
231 */
232 public function testGetParentId( $rowArray, $expected ) {
233 $rev = new Revision( $rowArray );
234 $this->assertSame( $expected, $rev->getParentId() );
235 }
236
237 /**
238 * @covers Revision::getRevisionText
239 * @dataProvider provideGetRevisionText
240 */
241 public function testGetRevisionText( $expected, $rowData, $prefix = 'old_', $wiki = false ) {
242 $this->assertEquals(
243 $expected,
244 Revision::getRevisionText( (object)$rowData, $prefix, $wiki ) );
245 }
246
247 public function provideGetRevisionTextWithZlibExtension() {
248 yield 'Generic gzip test' => [
249 'This is a small goat of revision text.',
250 [
251 'old_flags' => 'gzip',
252 'old_text' => gzdeflate( 'This is a small goat of revision text.' ),
253 ],
254 ];
255 }
256
257 /**
258 * @covers Revision::getRevisionText
259 * @dataProvider provideGetRevisionTextWithZlibExtension
260 */
261 public function testGetRevisionWithZlibExtension( $expected, $rowData ) {
262 $this->checkPHPExtension( 'zlib' );
263 $this->testGetRevisionText( $expected, $rowData );
264 }
265
266 public function provideGetRevisionTextWithLegacyEncoding() {
267 yield 'Utf8Native' => [
268 "Wiki est l'\xc3\xa9cole superieur !",
269 'iso-8859-1',
270 [
271 'old_flags' => 'utf-8',
272 'old_text' => "Wiki est l'\xc3\xa9cole superieur !",
273 ]
274 ];
275 yield 'Utf8Legacy' => [
276 "Wiki est l'\xc3\xa9cole superieur !",
277 'iso-8859-1',
278 [
279 'old_flags' => '',
280 'old_text' => "Wiki est l'\xe9cole superieur !",
281 ]
282 ];
283 }
284
285 /**
286 * @covers Revision::getRevisionText
287 * @dataProvider provideGetRevisionTextWithLegacyEncoding
288 */
289 public function testGetRevisionWithLegacyEncoding( $expected, $encoding, $rowData ) {
290 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
291 $this->testGetRevisionText( $expected, $rowData );
292 }
293
294 public function provideGetRevisionTextWithGzipAndLegacyEncoding() {
295 /**
296 * WARNING!
297 * Do not set the external flag!
298 * Otherwise, getRevisionText will hit the live database (if ExternalStore is enabled)!
299 */
300 yield 'Utf8NativeGzip' => [
301 "Wiki est l'\xc3\xa9cole superieur !",
302 'iso-8859-1',
303 [
304 'old_flags' => 'gzip,utf-8',
305 'old_text' => gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ),
306 ]
307 ];
308 yield 'Utf8LegacyGzip' => [
309 "Wiki est l'\xc3\xa9cole superieur !",
310 'iso-8859-1',
311 [
312 'old_flags' => 'gzip',
313 'old_text' => gzdeflate( "Wiki est l'\xe9cole superieur !" ),
314 ]
315 ];
316 }
317
318 /**
319 * @covers Revision::getRevisionText
320 * @dataProvider provideGetRevisionTextWithGzipAndLegacyEncoding
321 */
322 public function testGetRevisionWithGzipAndLegacyEncoding( $expected, $encoding, $rowData ) {
323 $this->checkPHPExtension( 'zlib' );
324 $this->setMwGlobals( 'wgLegacyEncoding', $encoding );
325 $this->testGetRevisionText( $expected, $rowData );
326 }
327
328 /**
329 * @covers Revision::compressRevisionText
330 */
331 public function testCompressRevisionTextUtf8() {
332 $row = new stdClass;
333 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
334 $row->old_flags = Revision::compressRevisionText( $row->old_text );
335 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
336 "Flags should contain 'utf-8'" );
337 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ),
338 "Flags should not contain 'gzip'" );
339 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
340 $row->old_text, "Direct check" );
341 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
342 Revision::getRevisionText( $row ), "getRevisionText" );
343 }
344
345 /**
346 * @covers Revision::compressRevisionText
347 */
348 public function testCompressRevisionTextUtf8Gzip() {
349 $this->checkPHPExtension( 'zlib' );
350 $this->setMwGlobals( 'wgCompressRevisions', true );
351
352 $row = new stdClass;
353 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
354 $row->old_flags = Revision::compressRevisionText( $row->old_text );
355 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ),
356 "Flags should contain 'utf-8'" );
357 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ),
358 "Flags should contain 'gzip'" );
359 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
360 gzinflate( $row->old_text ), "Direct check" );
361 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !",
362 Revision::getRevisionText( $row ), "getRevisionText" );
363 }
364
365 public function provideFetchFromConds() {
366 yield [ 0, [] ];
367 yield [ Revision::READ_LOCKING, [ 'FOR UPDATE' ] ];
368 }
369
370 /**
371 * @dataProvider provideFetchFromConds
372 * @covers Revision::fetchFromConds
373 */
374 public function testFetchFromConds( $flags, array $options ) {
375 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
376 $conditions = [ 'conditionsArray' ];
377
378 $db = $this->getMock( IDatabase::class );
379 $db->expects( $this->once() )
380 ->method( 'selectRow' )
381 ->with(
382 $this->equalTo( [ 'revision', 'page', 'user' ] ),
383 // We don't really care about the fields are they come from the selectField methods
384 $this->isType( 'array' ),
385 $this->equalTo( $conditions ),
386 // Method name
387 $this->equalTo( 'Revision::fetchFromConds' ),
388 $this->equalTo( $options ),
389 // We don't really care about the join conds are they come from the joinCond methods
390 $this->isType( 'array' )
391 )
392 ->willReturn( 'RETURNVALUE' );
393
394 $wrapper = TestingAccessWrapper::newFromClass( Revision::class );
395 $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
396
397 $this->assertEquals( 'RETURNVALUE', $result );
398 }
399
400 public function provideDecompressRevisionText() {
401 yield '(no legacy encoding), false in false out' => [ false, false, [], false ];
402 yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ];
403 yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ];
404 yield '(no legacy encoding), string in with gzip flag returns string' => [
405 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
406 false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA',
407 ];
408 yield '(no legacy encoding), string in with object flag returns false' => [
409 // gzip string below generated with serialize( 'JOJO' )
410 false, "s:4:\"JOJO\";", [ 'object' ], false,
411 ];
412 yield '(no legacy encoding), serialized object in with object flag returns string' => [
413 false,
414 // Using a TitleValue object as it has a getText method (which is needed)
415 serialize( new TitleValue( 0, 'HHJJDDFF' ) ),
416 [ 'object' ],
417 'HHJJDDFF',
418 ];
419 yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [
420 false,
421 // Using a TitleValue object as it has a getText method (which is needed)
422 gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ),
423 [ 'object', 'gzip' ],
424 '8219JJJ840',
425 ];
426 yield '(ISO-8859-1 encoding), string in string out' => [
427 'ISO-8859-1',
428 iconv( 'utf8', 'ISO-8859-1', "1®Àþ1" ),
429 [],
430 '1®Àþ1',
431 ];
432 yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [
433 'ISO-8859-1',
434 gzdeflate( iconv( 'utf8', 'ISO-8859-1', "4®Àþ4" ) ),
435 [ 'gzip' ],
436 '4®Àþ4',
437 ];
438 yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [
439 'ISO-8859-1',
440 serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "3®Àþ3" ) ) ),
441 [ 'object' ],
442 '3®Àþ3',
443 ];
444 yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [
445 'ISO-8859-1',
446 gzdeflate( serialize( new TitleValue( 0, iconv( 'utf8', 'ISO-8859-1', "2®Àþ2" ) ) ) ),
447 [ 'gzip', 'object' ],
448 '2®Àþ2',
449 ];
450 }
451
452 /**
453 * @dataProvider provideDecompressRevisionText
454 * @covers Revision::decompressRevisionText
455 *
456 * @param bool $legacyEncoding
457 * @param mixed $text
458 * @param array $flags
459 * @param mixed $expected
460 */
461 public function testDecompressRevisionText( $legacyEncoding, $text, $flags, $expected ) {
462 $this->setMwGlobals( 'wgLegacyEncoding', $legacyEncoding );
463 $this->setMwGlobals( 'wgLanguageCode', 'en' );
464 $this->assertSame(
465 $expected,
466 Revision::decompressRevisionText( $text, $flags )
467 );
468 }
469
470 /**
471 * @covers Revision::getRevisionText
472 */
473 public function testGetRevisionText_returnsFalseWhenNoTextField() {
474 $this->assertFalse( Revision::getRevisionText( new stdClass() ) );
475 }
476
477 public function provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal() {
478 yield 'Just text' => [
479 (object)[ 'old_text' => 'SomeText' ],
480 'old_',
481 'SomeText'
482 ];
483 // gzip string below generated with gzdeflate( 'AAAABBAAA' )
484 yield 'gzip text' => [
485 (object)[
486 'old_text' => "sttttr\002\022\000",
487 'old_flags' => 'gzip'
488 ],
489 'old_',
490 'AAAABBAAA'
491 ];
492 yield 'gzip text and different prefix' => [
493 (object)[
494 'jojo_text' => "sttttr\002\022\000",
495 'jojo_flags' => 'gzip'
496 ],
497 'jojo_',
498 'AAAABBAAA'
499 ];
500 }
501
502 /**
503 * @dataProvider provideTestGetRevisionText_returnsDecompressedTextFieldWhenNotExternal
504 * @covers Revision::getRevisionText
505 */
506 public function testGetRevisionText_returnsDecompressedTextFieldWhenNotExternal(
507 $row,
508 $prefix,
509 $expected
510 ) {
511 $this->assertSame( $expected, Revision::getRevisionText( $row, $prefix ) );
512 }
513
514 public function provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts() {
515 yield 'Just some text' => [ 'someNonUrlText' ];
516 yield 'No second URL part' => [ 'someProtocol://' ];
517 }
518
519 /**
520 * @dataProvider provideTestGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts
521 * @covers Revision::getRevisionText
522 */
523 public function testGetRevisionText_external_returnsFalseWhenNotEnoughUrlParts(
524 $text
525 ) {
526 $this->assertFalse(
527 Revision::getRevisionText(
528 (object)[
529 'old_text' => $text,
530 'old_flags' => 'external',
531 ]
532 )
533 );
534 }
535
536 /**
537 * @covers Revision::getRevisionText
538 */
539 public function testGetRevisionText_external_noOldId() {
540 $this->setService(
541 'ExternalStoreFactory',
542 new ExternalStoreFactory( [ 'ForTesting' ] )
543 );
544 $this->assertSame(
545 'AAAABBAAA',
546 Revision::getRevisionText(
547 (object)[
548 'old_text' => 'ForTesting://cluster1/12345',
549 'old_flags' => 'external,gzip',
550 ]
551 )
552 );
553 }
554
555 /**
556 * @covers Revision::getRevisionText
557 */
558 public function testGetRevisionText_external_oldId() {
559 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
560 $this->setService( 'MainWANObjectCache', $cache );
561 $this->setService(
562 'ExternalStoreFactory',
563 new ExternalStoreFactory( [ 'ForTesting' ] )
564 );
565
566 $cacheKey = $cache->makeKey( 'revisiontext', 'textid', '7777' );
567
568 $this->assertSame(
569 'AAAABBAAA',
570 Revision::getRevisionText(
571 (object)[
572 'old_text' => 'ForTesting://cluster1/12345',
573 'old_flags' => 'external,gzip',
574 'old_id' => '7777',
575 ]
576 )
577 );
578 $this->assertSame( 'AAAABBAAA', $cache->get( $cacheKey ) );
579 }
580
581 /**
582 * @covers Revision::userJoinCond
583 */
584 public function testUserJoinCond() {
585 $this->hideDeprecated( 'Revision::userJoinCond' );
586 $this->assertEquals(
587 [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
588 Revision::userJoinCond()
589 );
590 }
591
592 /**
593 * @covers Revision::pageJoinCond
594 */
595 public function testPageJoinCond() {
596 $this->hideDeprecated( 'Revision::pageJoinCond' );
597 $this->assertEquals(
598 [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
599 Revision::pageJoinCond()
600 );
601 }
602
603 public function provideSelectFields() {
604 yield [
605 true,
606 [
607 'rev_id',
608 'rev_page',
609 'rev_text_id',
610 'rev_timestamp',
611 'rev_user_text',
612 'rev_user',
613 'rev_minor_edit',
614 'rev_deleted',
615 'rev_len',
616 'rev_parent_id',
617 'rev_sha1',
618 'rev_comment_text' => 'rev_comment',
619 'rev_comment_data' => 'NULL',
620 'rev_comment_cid' => 'NULL',
621 'rev_content_format',
622 'rev_content_model',
623 ]
624 ];
625 yield [
626 false,
627 [
628 'rev_id',
629 'rev_page',
630 'rev_text_id',
631 'rev_timestamp',
632 'rev_user_text',
633 'rev_user',
634 'rev_minor_edit',
635 'rev_deleted',
636 'rev_len',
637 'rev_parent_id',
638 'rev_sha1',
639 'rev_comment_text' => 'rev_comment',
640 'rev_comment_data' => 'NULL',
641 'rev_comment_cid' => 'NULL',
642 ]
643 ];
644 }
645
646 /**
647 * @dataProvider provideSelectFields
648 * @covers Revision::selectFields
649 * @todo a true unit test would mock CommentStore
650 */
651 public function testSelectFields( $contentHandlerUseDB, $expected ) {
652 $this->hideDeprecated( 'Revision::selectFields' );
653 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
654 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
655 $this->assertEquals( $expected, Revision::selectFields() );
656 }
657
658 public function provideSelectArchiveFields() {
659 yield [
660 true,
661 [
662 'ar_id',
663 'ar_page_id',
664 'ar_rev_id',
665 'ar_text',
666 'ar_text_id',
667 'ar_timestamp',
668 'ar_user_text',
669 'ar_user',
670 'ar_minor_edit',
671 'ar_deleted',
672 'ar_len',
673 'ar_parent_id',
674 'ar_sha1',
675 'ar_comment_text' => 'ar_comment',
676 'ar_comment_data' => 'NULL',
677 'ar_comment_cid' => 'NULL',
678 'ar_content_format',
679 'ar_content_model',
680 ]
681 ];
682 yield [
683 false,
684 [
685 'ar_id',
686 'ar_page_id',
687 'ar_rev_id',
688 'ar_text',
689 'ar_text_id',
690 'ar_timestamp',
691 'ar_user_text',
692 'ar_user',
693 'ar_minor_edit',
694 'ar_deleted',
695 'ar_len',
696 'ar_parent_id',
697 'ar_sha1',
698 'ar_comment_text' => 'ar_comment',
699 'ar_comment_data' => 'NULL',
700 'ar_comment_cid' => 'NULL',
701 ]
702 ];
703 }
704
705 /**
706 * @dataProvider provideSelectArchiveFields
707 * @covers Revision::selectArchiveFields
708 * @todo a true unit test would mock CommentStore
709 */
710 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
711 $this->hideDeprecated( 'Revision::selectArchiveFields' );
712 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
713 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
714 $this->assertEquals( $expected, Revision::selectArchiveFields() );
715 }
716
717 /**
718 * @covers Revision::selectTextFields
719 */
720 public function testSelectTextFields() {
721 $this->hideDeprecated( 'Revision::selectTextFields' );
722 $this->assertEquals(
723 [
724 'old_text',
725 'old_flags',
726 ],
727 Revision::selectTextFields()
728 );
729 }
730
731 /**
732 * @covers Revision::selectPageFields
733 */
734 public function testSelectPageFields() {
735 $this->hideDeprecated( 'Revision::selectPageFields' );
736 $this->assertEquals(
737 [
738 'page_namespace',
739 'page_title',
740 'page_id',
741 'page_latest',
742 'page_is_redirect',
743 'page_len',
744 ],
745 Revision::selectPageFields()
746 );
747 }
748
749 /**
750 * @covers Revision::selectUserFields
751 */
752 public function testSelectUserFields() {
753 $this->hideDeprecated( 'Revision::selectUserFields' );
754 $this->assertEquals(
755 [
756 'user_name',
757 ],
758 Revision::selectUserFields()
759 );
760 }
761
762 public function provideGetArchiveQueryInfo() {
763 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
764 [
765 'wgContentHandlerUseDB' => false,
766 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
767 ],
768 [
769 'tables' => [ 'archive' ],
770 'fields' => [
771 'ar_id',
772 'ar_page_id',
773 'ar_rev_id',
774 'ar_text',
775 'ar_text_id',
776 'ar_timestamp',
777 'ar_user_text',
778 'ar_user',
779 'ar_minor_edit',
780 'ar_deleted',
781 'ar_len',
782 'ar_parent_id',
783 'ar_sha1',
784 'ar_comment_text' => 'ar_comment',
785 'ar_comment_data' => 'NULL',
786 'ar_comment_cid' => 'NULL',
787 ],
788 'joins' => [],
789 ]
790 ];
791 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
792 [
793 'wgContentHandlerUseDB' => true,
794 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
795 ],
796 [
797 'tables' => [ 'archive' ],
798 'fields' => [
799 'ar_id',
800 'ar_page_id',
801 'ar_rev_id',
802 'ar_text',
803 'ar_text_id',
804 'ar_timestamp',
805 'ar_user_text',
806 'ar_user',
807 'ar_minor_edit',
808 'ar_deleted',
809 'ar_len',
810 'ar_parent_id',
811 'ar_sha1',
812 'ar_comment_text' => 'ar_comment',
813 'ar_comment_data' => 'NULL',
814 'ar_comment_cid' => 'NULL',
815 'ar_content_format',
816 'ar_content_model',
817 ],
818 'joins' => [],
819 ]
820 ];
821 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
822 [
823 'wgContentHandlerUseDB' => false,
824 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
825 ],
826 [
827 'tables' => [
828 'archive',
829 'comment_ar_comment' => 'comment',
830 ],
831 'fields' => [
832 'ar_id',
833 'ar_page_id',
834 'ar_rev_id',
835 'ar_text',
836 'ar_text_id',
837 'ar_timestamp',
838 'ar_user_text',
839 'ar_user',
840 'ar_minor_edit',
841 'ar_deleted',
842 'ar_len',
843 'ar_parent_id',
844 'ar_sha1',
845 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
846 'ar_comment_data' => 'comment_ar_comment.comment_data',
847 'ar_comment_cid' => 'comment_ar_comment.comment_id',
848 ],
849 'joins' => [
850 'comment_ar_comment' => [
851 'LEFT JOIN',
852 'comment_ar_comment.comment_id = ar_comment_id',
853 ],
854 ],
855 ]
856 ];
857 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
858 [
859 'wgContentHandlerUseDB' => false,
860 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
861 ],
862 [
863 'tables' => [
864 'archive',
865 'comment_ar_comment' => 'comment',
866 ],
867 'fields' => [
868 'ar_id',
869 'ar_page_id',
870 'ar_rev_id',
871 'ar_text',
872 'ar_text_id',
873 'ar_timestamp',
874 'ar_user_text',
875 'ar_user',
876 'ar_minor_edit',
877 'ar_deleted',
878 'ar_len',
879 'ar_parent_id',
880 'ar_sha1',
881 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
882 'ar_comment_data' => 'comment_ar_comment.comment_data',
883 'ar_comment_cid' => 'comment_ar_comment.comment_id',
884 ],
885 'joins' => [
886 'comment_ar_comment' => [
887 'LEFT JOIN',
888 'comment_ar_comment.comment_id = ar_comment_id',
889 ],
890 ],
891 ]
892 ];
893 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
894 [
895 'wgContentHandlerUseDB' => false,
896 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
897 ],
898 [
899 'tables' => [
900 'archive',
901 'comment_ar_comment' => 'comment',
902 ],
903 'fields' => [
904 'ar_id',
905 'ar_page_id',
906 'ar_rev_id',
907 'ar_text',
908 'ar_text_id',
909 'ar_timestamp',
910 'ar_user_text',
911 'ar_user',
912 'ar_minor_edit',
913 'ar_deleted',
914 'ar_len',
915 'ar_parent_id',
916 'ar_sha1',
917 'ar_comment_text' => 'comment_ar_comment.comment_text',
918 'ar_comment_data' => 'comment_ar_comment.comment_data',
919 'ar_comment_cid' => 'comment_ar_comment.comment_id',
920 ],
921 'joins' => [
922 'comment_ar_comment' => [
923 'JOIN',
924 'comment_ar_comment.comment_id = ar_comment_id',
925 ],
926 ],
927 ]
928 ];
929 }
930
931 /**
932 * @covers Revision::getArchiveQueryInfo
933 * @dataProvider provideGetArchiveQueryInfo
934 */
935 public function testGetArchiveQueryInfo( $globals, $expected ) {
936 $this->setMwGlobals( $globals );
937 $this->assertEquals(
938 $expected,
939 Revision::getArchiveQueryInfo()
940 );
941 }
942
943 public function provideGetQueryInfo() {
944 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
945 [
946 'wgContentHandlerUseDB' => false,
947 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
948 ],
949 [],
950 [
951 'tables' => [ 'revision' ],
952 'fields' => [
953 'rev_id',
954 'rev_page',
955 'rev_text_id',
956 'rev_timestamp',
957 'rev_user_text',
958 'rev_user',
959 'rev_minor_edit',
960 'rev_deleted',
961 'rev_len',
962 'rev_parent_id',
963 'rev_sha1',
964 'rev_comment_text' => 'rev_comment',
965 'rev_comment_data' => 'NULL',
966 'rev_comment_cid' => 'NULL',
967 ],
968 'joins' => [],
969 ],
970 ];
971 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
972 [
973 'wgContentHandlerUseDB' => false,
974 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
975 ],
976 [ 'page' ],
977 [
978 'tables' => [ 'revision', 'page' ],
979 'fields' => [
980 'rev_id',
981 'rev_page',
982 'rev_text_id',
983 'rev_timestamp',
984 'rev_user_text',
985 'rev_user',
986 'rev_minor_edit',
987 'rev_deleted',
988 'rev_len',
989 'rev_parent_id',
990 'rev_sha1',
991 'rev_comment_text' => 'rev_comment',
992 'rev_comment_data' => 'NULL',
993 'rev_comment_cid' => 'NULL',
994 'page_namespace',
995 'page_title',
996 'page_id',
997 'page_latest',
998 'page_is_redirect',
999 'page_len',
1000 ],
1001 'joins' => [
1002 'page' => [
1003 'INNER JOIN',
1004 [ 'page_id = rev_page' ],
1005 ],
1006 ],
1007 ],
1008 ];
1009 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1010 [
1011 'wgContentHandlerUseDB' => false,
1012 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1013 ],
1014 [ 'user' ],
1015 [
1016 'tables' => [ 'revision', 'user' ],
1017 'fields' => [
1018 'rev_id',
1019 'rev_page',
1020 'rev_text_id',
1021 'rev_timestamp',
1022 'rev_user_text',
1023 'rev_user',
1024 'rev_minor_edit',
1025 'rev_deleted',
1026 'rev_len',
1027 'rev_parent_id',
1028 'rev_sha1',
1029 'rev_comment_text' => 'rev_comment',
1030 'rev_comment_data' => 'NULL',
1031 'rev_comment_cid' => 'NULL',
1032 'user_name',
1033 ],
1034 'joins' => [
1035 'user' => [
1036 'LEFT JOIN',
1037 [
1038 'rev_user != 0',
1039 'user_id = rev_user',
1040 ],
1041 ],
1042 ],
1043 ],
1044 ];
1045 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1046 [
1047 'wgContentHandlerUseDB' => false,
1048 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1049 ],
1050 [ 'text' ],
1051 [
1052 'tables' => [ 'revision', 'text' ],
1053 'fields' => [
1054 'rev_id',
1055 'rev_page',
1056 'rev_text_id',
1057 'rev_timestamp',
1058 'rev_user_text',
1059 'rev_user',
1060 'rev_minor_edit',
1061 'rev_deleted',
1062 'rev_len',
1063 'rev_parent_id',
1064 'rev_sha1',
1065 'rev_comment_text' => 'rev_comment',
1066 'rev_comment_data' => 'NULL',
1067 'rev_comment_cid' => 'NULL',
1068 'old_text',
1069 'old_flags',
1070 ],
1071 'joins' => [
1072 'text' => [
1073 'INNER JOIN',
1074 [ 'rev_text_id=old_id' ],
1075 ],
1076 ],
1077 ],
1078 ];
1079 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1080 [
1081 'wgContentHandlerUseDB' => false,
1082 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1083 ],
1084 [ 'text', 'page', 'user' ],
1085 [
1086 'tables' => [ 'revision', 'page', 'user', 'text' ],
1087 'fields' => [
1088 'rev_id',
1089 'rev_page',
1090 'rev_text_id',
1091 'rev_timestamp',
1092 'rev_user_text',
1093 'rev_user',
1094 'rev_minor_edit',
1095 'rev_deleted',
1096 'rev_len',
1097 'rev_parent_id',
1098 'rev_sha1',
1099 'rev_comment_text' => 'rev_comment',
1100 'rev_comment_data' => 'NULL',
1101 'rev_comment_cid' => 'NULL',
1102 'page_namespace',
1103 'page_title',
1104 'page_id',
1105 'page_latest',
1106 'page_is_redirect',
1107 'page_len',
1108 'user_name',
1109 'old_text',
1110 'old_flags',
1111 ],
1112 'joins' => [
1113 'page' => [
1114 'INNER JOIN',
1115 [ 'page_id = rev_page' ],
1116 ],
1117 'user' => [
1118 'LEFT JOIN',
1119 [
1120 'rev_user != 0',
1121 'user_id = rev_user',
1122 ],
1123 ],
1124 'text' => [
1125 'INNER JOIN',
1126 [ 'rev_text_id=old_id' ],
1127 ],
1128 ],
1129 ],
1130 ];
1131 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1132 [
1133 'wgContentHandlerUseDB' => true,
1134 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1135 ],
1136 [],
1137 [
1138 'tables' => [ 'revision' ],
1139 'fields' => [
1140 'rev_id',
1141 'rev_page',
1142 'rev_text_id',
1143 'rev_timestamp',
1144 'rev_user_text',
1145 'rev_user',
1146 'rev_minor_edit',
1147 'rev_deleted',
1148 'rev_len',
1149 'rev_parent_id',
1150 'rev_sha1',
1151 'rev_comment_text' => 'rev_comment',
1152 'rev_comment_data' => 'NULL',
1153 'rev_comment_cid' => 'NULL',
1154 'rev_content_format',
1155 'rev_content_model',
1156 ],
1157 'joins' => [],
1158 ],
1159 ];
1160 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1161 [
1162 'wgContentHandlerUseDB' => false,
1163 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
1164 ],
1165 [],
1166 [
1167 'tables' => [
1168 'revision',
1169 'temp_rev_comment' => 'revision_comment_temp',
1170 'comment_rev_comment' => 'comment',
1171 ],
1172 'fields' => [
1173 'rev_id',
1174 'rev_page',
1175 'rev_text_id',
1176 'rev_timestamp',
1177 'rev_user_text',
1178 'rev_user',
1179 'rev_minor_edit',
1180 'rev_deleted',
1181 'rev_len',
1182 'rev_parent_id',
1183 'rev_sha1',
1184 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1185 'rev_comment_data' => 'comment_rev_comment.comment_data',
1186 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1187 ],
1188 'joins' => [
1189 'temp_rev_comment' => [
1190 'LEFT JOIN',
1191 'temp_rev_comment.revcomment_rev = rev_id',
1192 ],
1193 'comment_rev_comment' => [
1194 'LEFT JOIN',
1195 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1196 ],
1197 ],
1198 ],
1199 ];
1200 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1201 [
1202 'wgContentHandlerUseDB' => false,
1203 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
1204 ],
1205 [],
1206 [
1207 'tables' => [
1208 'revision',
1209 'temp_rev_comment' => 'revision_comment_temp',
1210 'comment_rev_comment' => 'comment',
1211 ],
1212 'fields' => [
1213 'rev_id',
1214 'rev_page',
1215 'rev_text_id',
1216 'rev_timestamp',
1217 'rev_user_text',
1218 'rev_user',
1219 'rev_minor_edit',
1220 'rev_deleted',
1221 'rev_len',
1222 'rev_parent_id',
1223 'rev_sha1',
1224 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1225 'rev_comment_data' => 'comment_rev_comment.comment_data',
1226 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1227 ],
1228 'joins' => [
1229 'temp_rev_comment' => [
1230 'LEFT JOIN',
1231 'temp_rev_comment.revcomment_rev = rev_id',
1232 ],
1233 'comment_rev_comment' => [
1234 'LEFT JOIN',
1235 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1236 ],
1237 ],
1238 ],
1239 ];
1240 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1241 [
1242 'wgContentHandlerUseDB' => false,
1243 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
1244 ],
1245 [],
1246 [
1247 'tables' => [
1248 'revision',
1249 'temp_rev_comment' => 'revision_comment_temp',
1250 'comment_rev_comment' => 'comment',
1251 ],
1252 'fields' => [
1253 'rev_id',
1254 'rev_page',
1255 'rev_text_id',
1256 'rev_timestamp',
1257 'rev_user_text',
1258 'rev_user',
1259 'rev_minor_edit',
1260 'rev_deleted',
1261 'rev_len',
1262 'rev_parent_id',
1263 'rev_sha1',
1264 'rev_comment_text' => 'comment_rev_comment.comment_text',
1265 'rev_comment_data' => 'comment_rev_comment.comment_data',
1266 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1267 ],
1268 'joins' => [
1269 'temp_rev_comment' => [
1270 'JOIN',
1271 'temp_rev_comment.revcomment_rev = rev_id',
1272 ],
1273 'comment_rev_comment' => [
1274 'JOIN',
1275 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1276 ],
1277 ],
1278 ],
1279 ];
1280 }
1281
1282 /**
1283 * @covers Revision::getQueryInfo
1284 * @dataProvider provideGetQueryInfo
1285 */
1286 public function testGetQueryInfo( $globals, $options, $expected ) {
1287 $this->setMwGlobals( $globals );
1288 $this->assertEquals(
1289 $expected,
1290 Revision::getQueryInfo( $options )
1291 );
1292 }
1293
1294 }