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