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