4518e15ecf04ea71f116b014d00256ad2bbe5bce
[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->overrideMwServices();
938 $this->assertEquals( $expected, Revision::selectFields() );
939 }
940
941 public function provideSelectArchiveFields() {
942 yield [
943 true,
944 [
945 'ar_id',
946 'ar_page_id',
947 'ar_rev_id',
948 'ar_text',
949 'ar_text_id',
950 'ar_timestamp',
951 'ar_user_text',
952 'ar_user',
953 'ar_minor_edit',
954 'ar_deleted',
955 'ar_len',
956 'ar_parent_id',
957 'ar_sha1',
958 'ar_comment_text' => 'ar_comment',
959 'ar_comment_data' => 'NULL',
960 'ar_comment_cid' => 'NULL',
961 'ar_content_format',
962 'ar_content_model',
963 ]
964 ];
965 yield [
966 false,
967 [
968 'ar_id',
969 'ar_page_id',
970 'ar_rev_id',
971 'ar_text',
972 'ar_text_id',
973 'ar_timestamp',
974 'ar_user_text',
975 'ar_user',
976 'ar_minor_edit',
977 'ar_deleted',
978 'ar_len',
979 'ar_parent_id',
980 'ar_sha1',
981 'ar_comment_text' => 'ar_comment',
982 'ar_comment_data' => 'NULL',
983 'ar_comment_cid' => 'NULL',
984 ]
985 ];
986 }
987
988 /**
989 * @dataProvider provideSelectArchiveFields
990 * @covers Revision::selectArchiveFields
991 * @todo a true unit test would mock CommentStore
992 */
993 public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
994 $this->hideDeprecated( 'Revision::selectArchiveFields' );
995 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
996 $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD );
997 $this->overrideMwServices();
998 $this->assertEquals( $expected, Revision::selectArchiveFields() );
999 }
1000
1001 /**
1002 * @covers Revision::selectTextFields
1003 */
1004 public function testSelectTextFields() {
1005 $this->hideDeprecated( 'Revision::selectTextFields' );
1006 $this->assertEquals(
1007 [
1008 'old_text',
1009 'old_flags',
1010 ],
1011 Revision::selectTextFields()
1012 );
1013 }
1014
1015 /**
1016 * @covers Revision::selectPageFields
1017 */
1018 public function testSelectPageFields() {
1019 $this->hideDeprecated( 'Revision::selectPageFields' );
1020 $this->assertEquals(
1021 [
1022 'page_namespace',
1023 'page_title',
1024 'page_id',
1025 'page_latest',
1026 'page_is_redirect',
1027 'page_len',
1028 ],
1029 Revision::selectPageFields()
1030 );
1031 }
1032
1033 /**
1034 * @covers Revision::selectUserFields
1035 */
1036 public function testSelectUserFields() {
1037 $this->hideDeprecated( 'Revision::selectUserFields' );
1038 $this->assertEquals(
1039 [
1040 'user_name',
1041 ],
1042 Revision::selectUserFields()
1043 );
1044 }
1045
1046 public function provideGetArchiveQueryInfo() {
1047 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD' => [
1048 [
1049 'wgContentHandlerUseDB' => false,
1050 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1051 ],
1052 [
1053 'tables' => [ 'archive' ],
1054 'fields' => [
1055 'ar_id',
1056 'ar_page_id',
1057 'ar_namespace',
1058 'ar_title',
1059 'ar_rev_id',
1060 'ar_text',
1061 'ar_text_id',
1062 'ar_timestamp',
1063 'ar_user_text',
1064 'ar_user',
1065 'ar_minor_edit',
1066 'ar_deleted',
1067 'ar_len',
1068 'ar_parent_id',
1069 'ar_sha1',
1070 'ar_comment_text' => 'ar_comment',
1071 'ar_comment_data' => 'NULL',
1072 'ar_comment_cid' => 'NULL',
1073 ],
1074 'joins' => [],
1075 ]
1076 ];
1077 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD' => [
1078 [
1079 'wgContentHandlerUseDB' => true,
1080 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1081 ],
1082 [
1083 'tables' => [ 'archive' ],
1084 'fields' => [
1085 'ar_id',
1086 'ar_page_id',
1087 'ar_namespace',
1088 'ar_title',
1089 'ar_rev_id',
1090 'ar_text',
1091 'ar_text_id',
1092 'ar_timestamp',
1093 'ar_user_text',
1094 'ar_user',
1095 'ar_minor_edit',
1096 'ar_deleted',
1097 'ar_len',
1098 'ar_parent_id',
1099 'ar_sha1',
1100 'ar_comment_text' => 'ar_comment',
1101 'ar_comment_data' => 'NULL',
1102 'ar_comment_cid' => 'NULL',
1103 'ar_content_format',
1104 'ar_content_model',
1105 ],
1106 'joins' => [],
1107 ]
1108 ];
1109 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH' => [
1110 [
1111 'wgContentHandlerUseDB' => false,
1112 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
1113 ],
1114 [
1115 'tables' => [
1116 'archive',
1117 'comment_ar_comment' => 'comment',
1118 ],
1119 'fields' => [
1120 'ar_id',
1121 'ar_page_id',
1122 'ar_namespace',
1123 'ar_title',
1124 'ar_rev_id',
1125 'ar_text',
1126 'ar_text_id',
1127 'ar_timestamp',
1128 'ar_user_text',
1129 'ar_user',
1130 'ar_minor_edit',
1131 'ar_deleted',
1132 'ar_len',
1133 'ar_parent_id',
1134 'ar_sha1',
1135 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1136 'ar_comment_data' => 'comment_ar_comment.comment_data',
1137 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1138 ],
1139 'joins' => [
1140 'comment_ar_comment' => [
1141 'LEFT JOIN',
1142 'comment_ar_comment.comment_id = ar_comment_id',
1143 ],
1144 ],
1145 ]
1146 ];
1147 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW' => [
1148 [
1149 'wgContentHandlerUseDB' => false,
1150 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
1151 ],
1152 [
1153 'tables' => [
1154 'archive',
1155 'comment_ar_comment' => 'comment',
1156 ],
1157 'fields' => [
1158 'ar_id',
1159 'ar_page_id',
1160 'ar_namespace',
1161 'ar_title',
1162 'ar_rev_id',
1163 'ar_text',
1164 'ar_text_id',
1165 'ar_timestamp',
1166 'ar_user_text',
1167 'ar_user',
1168 'ar_minor_edit',
1169 'ar_deleted',
1170 'ar_len',
1171 'ar_parent_id',
1172 'ar_sha1',
1173 'ar_comment_text' => 'COALESCE( comment_ar_comment.comment_text, ar_comment )',
1174 'ar_comment_data' => 'comment_ar_comment.comment_data',
1175 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1176 ],
1177 'joins' => [
1178 'comment_ar_comment' => [
1179 'LEFT JOIN',
1180 'comment_ar_comment.comment_id = ar_comment_id',
1181 ],
1182 ],
1183 ]
1184 ];
1185 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW' => [
1186 [
1187 'wgContentHandlerUseDB' => false,
1188 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
1189 ],
1190 [
1191 'tables' => [
1192 'archive',
1193 'comment_ar_comment' => 'comment',
1194 ],
1195 'fields' => [
1196 'ar_id',
1197 'ar_page_id',
1198 'ar_namespace',
1199 'ar_title',
1200 'ar_rev_id',
1201 'ar_text',
1202 'ar_text_id',
1203 'ar_timestamp',
1204 'ar_user_text',
1205 'ar_user',
1206 'ar_minor_edit',
1207 'ar_deleted',
1208 'ar_len',
1209 'ar_parent_id',
1210 'ar_sha1',
1211 'ar_comment_text' => 'comment_ar_comment.comment_text',
1212 'ar_comment_data' => 'comment_ar_comment.comment_data',
1213 'ar_comment_cid' => 'comment_ar_comment.comment_id',
1214 ],
1215 'joins' => [
1216 'comment_ar_comment' => [
1217 'JOIN',
1218 'comment_ar_comment.comment_id = ar_comment_id',
1219 ],
1220 ],
1221 ]
1222 ];
1223 }
1224
1225 /**
1226 * @covers Revision::getArchiveQueryInfo
1227 * @dataProvider provideGetArchiveQueryInfo
1228 */
1229 public function testGetArchiveQueryInfo( $globals, $expected ) {
1230 $this->setMwGlobals( $globals );
1231
1232 $revisionStore = $this->getRevisionStore();
1233 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1234 $this->setService( 'RevisionStore', $revisionStore );
1235
1236 $this->assertEquals(
1237 $expected,
1238 Revision::getArchiveQueryInfo()
1239 );
1240 }
1241
1242 public function provideGetQueryInfo() {
1243 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1244 [
1245 'wgContentHandlerUseDB' => false,
1246 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1247 ],
1248 [],
1249 [
1250 'tables' => [ 'revision' ],
1251 'fields' => [
1252 'rev_id',
1253 'rev_page',
1254 'rev_text_id',
1255 'rev_timestamp',
1256 'rev_user_text',
1257 'rev_user',
1258 'rev_minor_edit',
1259 'rev_deleted',
1260 'rev_len',
1261 'rev_parent_id',
1262 'rev_sha1',
1263 'rev_comment_text' => 'rev_comment',
1264 'rev_comment_data' => 'NULL',
1265 'rev_comment_cid' => 'NULL',
1266 ],
1267 'joins' => [],
1268 ],
1269 ];
1270 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts page' => [
1271 [
1272 'wgContentHandlerUseDB' => false,
1273 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1274 ],
1275 [ 'page' ],
1276 [
1277 'tables' => [ 'revision', 'page' ],
1278 'fields' => [
1279 'rev_id',
1280 'rev_page',
1281 'rev_text_id',
1282 'rev_timestamp',
1283 'rev_user_text',
1284 'rev_user',
1285 'rev_minor_edit',
1286 'rev_deleted',
1287 'rev_len',
1288 'rev_parent_id',
1289 'rev_sha1',
1290 'rev_comment_text' => 'rev_comment',
1291 'rev_comment_data' => 'NULL',
1292 'rev_comment_cid' => 'NULL',
1293 'page_namespace',
1294 'page_title',
1295 'page_id',
1296 'page_latest',
1297 'page_is_redirect',
1298 'page_len',
1299 ],
1300 'joins' => [
1301 'page' => [
1302 'INNER JOIN',
1303 [ 'page_id = rev_page' ],
1304 ],
1305 ],
1306 ],
1307 ];
1308 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts user' => [
1309 [
1310 'wgContentHandlerUseDB' => false,
1311 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1312 ],
1313 [ 'user' ],
1314 [
1315 'tables' => [ 'revision', 'user' ],
1316 'fields' => [
1317 'rev_id',
1318 'rev_page',
1319 'rev_text_id',
1320 'rev_timestamp',
1321 'rev_user_text',
1322 'rev_user',
1323 'rev_minor_edit',
1324 'rev_deleted',
1325 'rev_len',
1326 'rev_parent_id',
1327 'rev_sha1',
1328 'rev_comment_text' => 'rev_comment',
1329 'rev_comment_data' => 'NULL',
1330 'rev_comment_cid' => 'NULL',
1331 'user_name',
1332 ],
1333 'joins' => [
1334 'user' => [
1335 'LEFT JOIN',
1336 [
1337 'rev_user != 0',
1338 'user_id = rev_user',
1339 ],
1340 ],
1341 ],
1342 ],
1343 ];
1344 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts text' => [
1345 [
1346 'wgContentHandlerUseDB' => false,
1347 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1348 ],
1349 [ 'text' ],
1350 [
1351 'tables' => [ 'revision', 'text' ],
1352 'fields' => [
1353 'rev_id',
1354 'rev_page',
1355 'rev_text_id',
1356 'rev_timestamp',
1357 'rev_user_text',
1358 'rev_user',
1359 'rev_minor_edit',
1360 'rev_deleted',
1361 'rev_len',
1362 'rev_parent_id',
1363 'rev_sha1',
1364 'rev_comment_text' => 'rev_comment',
1365 'rev_comment_data' => 'NULL',
1366 'rev_comment_cid' => 'NULL',
1367 'old_text',
1368 'old_flags',
1369 ],
1370 'joins' => [
1371 'text' => [
1372 'INNER JOIN',
1373 [ 'rev_text_id=old_id' ],
1374 ],
1375 ],
1376 ],
1377 ];
1378 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage OLD, opts 3' => [
1379 [
1380 'wgContentHandlerUseDB' => false,
1381 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1382 ],
1383 [ 'text', 'page', 'user' ],
1384 [
1385 'tables' => [ 'revision', 'page', 'user', 'text' ],
1386 'fields' => [
1387 'rev_id',
1388 'rev_page',
1389 'rev_text_id',
1390 'rev_timestamp',
1391 'rev_user_text',
1392 'rev_user',
1393 'rev_minor_edit',
1394 'rev_deleted',
1395 'rev_len',
1396 'rev_parent_id',
1397 'rev_sha1',
1398 'rev_comment_text' => 'rev_comment',
1399 'rev_comment_data' => 'NULL',
1400 'rev_comment_cid' => 'NULL',
1401 'page_namespace',
1402 'page_title',
1403 'page_id',
1404 'page_latest',
1405 'page_is_redirect',
1406 'page_len',
1407 'user_name',
1408 'old_text',
1409 'old_flags',
1410 ],
1411 'joins' => [
1412 'page' => [
1413 'INNER JOIN',
1414 [ 'page_id = rev_page' ],
1415 ],
1416 'user' => [
1417 'LEFT JOIN',
1418 [
1419 'rev_user != 0',
1420 'user_id = rev_user',
1421 ],
1422 ],
1423 'text' => [
1424 'INNER JOIN',
1425 [ 'rev_text_id=old_id' ],
1426 ],
1427 ],
1428 ],
1429 ];
1430 yield 'wgContentHandlerUseDB true, wgCommentTableSchemaMigrationStage OLD, opts none' => [
1431 [
1432 'wgContentHandlerUseDB' => true,
1433 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD,
1434 ],
1435 [],
1436 [
1437 'tables' => [ 'revision' ],
1438 'fields' => [
1439 'rev_id',
1440 'rev_page',
1441 'rev_text_id',
1442 'rev_timestamp',
1443 'rev_user_text',
1444 'rev_user',
1445 'rev_minor_edit',
1446 'rev_deleted',
1447 'rev_len',
1448 'rev_parent_id',
1449 'rev_sha1',
1450 'rev_comment_text' => 'rev_comment',
1451 'rev_comment_data' => 'NULL',
1452 'rev_comment_cid' => 'NULL',
1453 'rev_content_format',
1454 'rev_content_model',
1455 ],
1456 'joins' => [],
1457 ],
1458 ];
1459 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_BOTH, opts none' => [
1460 [
1461 'wgContentHandlerUseDB' => false,
1462 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH,
1463 ],
1464 [],
1465 [
1466 'tables' => [
1467 'revision',
1468 'temp_rev_comment' => 'revision_comment_temp',
1469 'comment_rev_comment' => 'comment',
1470 ],
1471 'fields' => [
1472 'rev_id',
1473 'rev_page',
1474 'rev_text_id',
1475 'rev_timestamp',
1476 'rev_user_text',
1477 'rev_user',
1478 'rev_minor_edit',
1479 'rev_deleted',
1480 'rev_len',
1481 'rev_parent_id',
1482 'rev_sha1',
1483 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1484 'rev_comment_data' => 'comment_rev_comment.comment_data',
1485 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1486 ],
1487 'joins' => [
1488 'temp_rev_comment' => [
1489 'LEFT JOIN',
1490 'temp_rev_comment.revcomment_rev = rev_id',
1491 ],
1492 'comment_rev_comment' => [
1493 'LEFT JOIN',
1494 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1495 ],
1496 ],
1497 ],
1498 ];
1499 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage WRITE_NEW, opts none' => [
1500 [
1501 'wgContentHandlerUseDB' => false,
1502 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW,
1503 ],
1504 [],
1505 [
1506 'tables' => [
1507 'revision',
1508 'temp_rev_comment' => 'revision_comment_temp',
1509 'comment_rev_comment' => 'comment',
1510 ],
1511 'fields' => [
1512 'rev_id',
1513 'rev_page',
1514 'rev_text_id',
1515 'rev_timestamp',
1516 'rev_user_text',
1517 'rev_user',
1518 'rev_minor_edit',
1519 'rev_deleted',
1520 'rev_len',
1521 'rev_parent_id',
1522 'rev_sha1',
1523 'rev_comment_text' => 'COALESCE( comment_rev_comment.comment_text, rev_comment )',
1524 'rev_comment_data' => 'comment_rev_comment.comment_data',
1525 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1526 ],
1527 'joins' => [
1528 'temp_rev_comment' => [
1529 'LEFT JOIN',
1530 'temp_rev_comment.revcomment_rev = rev_id',
1531 ],
1532 'comment_rev_comment' => [
1533 'LEFT JOIN',
1534 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1535 ],
1536 ],
1537 ],
1538 ];
1539 yield 'wgContentHandlerUseDB false, wgCommentTableSchemaMigrationStage NEW, opts none' => [
1540 [
1541 'wgContentHandlerUseDB' => false,
1542 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW,
1543 ],
1544 [],
1545 [
1546 'tables' => [
1547 'revision',
1548 'temp_rev_comment' => 'revision_comment_temp',
1549 'comment_rev_comment' => 'comment',
1550 ],
1551 'fields' => [
1552 'rev_id',
1553 'rev_page',
1554 'rev_text_id',
1555 'rev_timestamp',
1556 'rev_user_text',
1557 'rev_user',
1558 'rev_minor_edit',
1559 'rev_deleted',
1560 'rev_len',
1561 'rev_parent_id',
1562 'rev_sha1',
1563 'rev_comment_text' => 'comment_rev_comment.comment_text',
1564 'rev_comment_data' => 'comment_rev_comment.comment_data',
1565 'rev_comment_cid' => 'comment_rev_comment.comment_id',
1566 ],
1567 'joins' => [
1568 'temp_rev_comment' => [
1569 'JOIN',
1570 'temp_rev_comment.revcomment_rev = rev_id',
1571 ],
1572 'comment_rev_comment' => [
1573 'JOIN',
1574 'comment_rev_comment.comment_id = temp_rev_comment.revcomment_comment_id',
1575 ],
1576 ],
1577 ],
1578 ];
1579 }
1580
1581 /**
1582 * @covers Revision::getQueryInfo
1583 * @dataProvider provideGetQueryInfo
1584 */
1585 public function testGetQueryInfo( $globals, $options, $expected ) {
1586 $this->setMwGlobals( $globals );
1587
1588 $revisionStore = $this->getRevisionStore();
1589 $revisionStore->setContentHandlerUseDB( $globals['wgContentHandlerUseDB'] );
1590 $this->setService( 'RevisionStore', $revisionStore );
1591
1592 $this->assertEquals(
1593 $expected,
1594 Revision::getQueryInfo( $options )
1595 );
1596 }
1597
1598 /**
1599 * @covers Revision::getSize
1600 */
1601 public function testGetSize() {
1602 $title = $this->getMockTitle();
1603
1604 $rec = new MutableRevisionRecord( $title );
1605 $rev = new Revision( $rec, 0, $title );
1606
1607 $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
1608
1609 $rec->setSize( 13 );
1610 $this->assertSame( 13, $rev->getSize() );
1611 }
1612
1613 /**
1614 * @covers Revision::getSize
1615 */
1616 public function testGetSize_failure() {
1617 $title = $this->getMockTitle();
1618
1619 $rec = $this->getMockBuilder( RevisionRecord::class )
1620 ->disableOriginalConstructor()
1621 ->getMock();
1622
1623 $rec->method( 'getSize' )
1624 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1625
1626 $rev = new Revision( $rec, 0, $title );
1627 $this->assertNull( $rev->getSize() );
1628 }
1629
1630 /**
1631 * @covers Revision::getSha1
1632 */
1633 public function testGetSha1() {
1634 $title = $this->getMockTitle();
1635
1636 $rec = new MutableRevisionRecord( $title );
1637 $rev = new Revision( $rec, 0, $title );
1638
1639 $emptyHash = SlotRecord::base36Sha1( '' );
1640 $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
1641
1642 $rec->setSha1( 'deadbeef' );
1643 $this->assertSame( 'deadbeef', $rev->getSha1() );
1644 }
1645
1646 /**
1647 * @covers Revision::getSha1
1648 */
1649 public function testGetSha1_failure() {
1650 $title = $this->getMockTitle();
1651
1652 $rec = $this->getMockBuilder( RevisionRecord::class )
1653 ->disableOriginalConstructor()
1654 ->getMock();
1655
1656 $rec->method( 'getSha1' )
1657 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1658
1659 $rev = new Revision( $rec, 0, $title );
1660 $this->assertNull( $rev->getSha1() );
1661 }
1662
1663 /**
1664 * @covers Revision::getContent
1665 */
1666 public function testGetContent() {
1667 $title = $this->getMockTitle();
1668
1669 $rec = new MutableRevisionRecord( $title );
1670 $rev = new Revision( $rec, 0, $title );
1671
1672 $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
1673
1674 $content = new TextContent( 'Hello Kittens!' );
1675 $rec->setContent( 'main', $content );
1676 $this->assertSame( $content, $rev->getContent() );
1677 }
1678
1679 /**
1680 * @covers Revision::getContent
1681 */
1682 public function testGetContent_failure() {
1683 $title = $this->getMockTitle();
1684
1685 $rec = $this->getMockBuilder( RevisionRecord::class )
1686 ->disableOriginalConstructor()
1687 ->getMock();
1688
1689 $rec->method( 'getContent' )
1690 ->willThrowException( new RevisionAccessException( 'Oops!' ) );
1691
1692 $rev = new Revision( $rec, 0, $title );
1693 $this->assertNull( $rev->getContent() );
1694 }
1695
1696 }