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