Split Revision tests into Unit & Integration classes
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionIntegrationTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 * @group Database
6 *
7 * @group medium
8 */
9 class RevisionIntegrationTest extends MediaWikiTestCase {
10
11 /**
12 * @var WikiPage $the_page
13 */
14 private $the_page;
15
16 public function __construct( $name = null, array $data = [], $dataName = '' ) {
17 parent::__construct( $name, $data, $dataName );
18
19 $this->tablesUsed = array_merge( $this->tablesUsed,
20 [
21 'page',
22 'revision',
23 'ip_changes',
24 'text',
25 'archive',
26
27 'recentchanges',
28 'logging',
29
30 'page_props',
31 'pagelinks',
32 'categorylinks',
33 'langlinks',
34 'externallinks',
35 'imagelinks',
36 'templatelinks',
37 'iwlinks'
38 ]
39 );
40 }
41
42 protected function setUp() {
43 global $wgContLang;
44
45 parent::setUp();
46
47 $this->mergeMwGlobalArrayValue(
48 'wgExtraNamespaces',
49 [
50 12312 => 'Dummy',
51 12313 => 'Dummy_talk',
52 ]
53 );
54
55 $this->mergeMwGlobalArrayValue(
56 'wgNamespaceContentModels',
57 [
58 12312 => DummyContentForTesting::MODEL_ID,
59 ]
60 );
61
62 $this->mergeMwGlobalArrayValue(
63 'wgContentHandlers',
64 [
65 DummyContentForTesting::MODEL_ID => 'DummyContentHandlerForTesting',
66 RevisionTestModifyableContent::MODEL_ID => 'RevisionTestModifyableContentHandler',
67 ]
68 );
69
70 MWNamespace::clearCaches();
71 // Reset namespace cache
72 $wgContLang->resetNamespaces();
73 if ( !$this->the_page ) {
74 $this->the_page = $this->createPage(
75 'RevisionStorageTest_the_page',
76 "just a dummy page",
77 CONTENT_MODEL_WIKITEXT
78 );
79 }
80 }
81
82 protected function tearDown() {
83 global $wgContLang;
84
85 parent::tearDown();
86
87 MWNamespace::clearCaches();
88 // Reset namespace cache
89 $wgContLang->resetNamespaces();
90 }
91
92 protected function makeRevision( $props = null ) {
93 if ( $props === null ) {
94 $props = [];
95 }
96
97 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
98 $props['text'] = 'Lorem Ipsum';
99 }
100
101 if ( !isset( $props['comment'] ) ) {
102 $props['comment'] = 'just a test';
103 }
104
105 if ( !isset( $props['page'] ) ) {
106 $props['page'] = $this->the_page->getId();
107 }
108
109 $rev = new Revision( $props );
110
111 $dbw = wfGetDB( DB_MASTER );
112 $rev->insertOn( $dbw );
113
114 return $rev;
115 }
116
117 /**
118 * @param string $titleString
119 * @param string $text
120 * @param string|null $model
121 *
122 * @return WikiPage
123 */
124 protected function createPage( $titleString, $text, $model = null ) {
125 if ( !preg_match( '/:/', $titleString ) &&
126 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
127 ) {
128 $ns = $this->getDefaultWikitextNS();
129 $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
130 }
131
132 $title = Title::newFromText( $titleString );
133 $wikipage = new WikiPage( $title );
134
135 // Delete the article if it already exists
136 if ( $wikipage->exists() ) {
137 $wikipage->doDeleteArticle( "done" );
138 }
139
140 $content = ContentHandler::makeContent( $text, $title, $model );
141 $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
142
143 return $wikipage;
144 }
145
146 protected function assertRevEquals( Revision $orig, Revision $rev = null ) {
147 $this->assertNotNull( $rev, 'missing revision' );
148
149 $this->assertEquals( $orig->getId(), $rev->getId() );
150 $this->assertEquals( $orig->getPage(), $rev->getPage() );
151 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
152 $this->assertEquals( $orig->getUser(), $rev->getUser() );
153 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
154 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
155 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
156 }
157
158 /**
159 * @covers Revision::__construct
160 */
161 public function testConstructFromRow() {
162 $orig = $this->makeRevision();
163
164 $dbr = wfGetDB( DB_REPLICA );
165 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
166 $this->assertTrue( is_object( $res ), 'query failed' );
167
168 $row = $res->fetchObject();
169 $res->free();
170
171 $rev = new Revision( $row );
172
173 $this->assertRevEquals( $orig, $rev );
174 }
175
176 /**
177 * @covers Revision::newFromTitle
178 */
179 public function testNewFromTitle_withoutId() {
180 $page = $this->createPage(
181 __METHOD__,
182 'GOAT',
183 CONTENT_MODEL_WIKITEXT
184 );
185 $latestRevId = $page->getLatest();
186
187 $rev = Revision::newFromTitle( $page->getTitle() );
188
189 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
190 $this->assertEquals( $latestRevId, $rev->getId() );
191 }
192
193 /**
194 * @covers Revision::newFromTitle
195 */
196 public function testNewFromTitle_withId() {
197 $page = $this->createPage(
198 __METHOD__,
199 'GOAT',
200 CONTENT_MODEL_WIKITEXT
201 );
202 $latestRevId = $page->getLatest();
203
204 $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId );
205
206 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
207 $this->assertEquals( $latestRevId, $rev->getId() );
208 }
209
210 /**
211 * @covers Revision::newFromTitle
212 */
213 public function testNewFromTitle_withBadId() {
214 $page = $this->createPage(
215 __METHOD__,
216 'GOAT',
217 CONTENT_MODEL_WIKITEXT
218 );
219 $latestRevId = $page->getLatest();
220
221 $rev = Revision::newFromTitle( $page->getTitle(), $latestRevId + 1 );
222
223 $this->assertNull( $rev );
224 }
225
226 /**
227 * @covers Revision::newFromRow
228 */
229 public function testNewFromRow() {
230 $orig = $this->makeRevision();
231
232 $dbr = wfGetDB( DB_REPLICA );
233 $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
234 $this->assertTrue( is_object( $res ), 'query failed' );
235
236 $row = $res->fetchObject();
237 $res->free();
238
239 $rev = Revision::newFromRow( $row );
240
241 $this->assertRevEquals( $orig, $rev );
242 }
243
244 /**
245 * @covers Revision::newFromArchiveRow
246 */
247 public function testNewFromArchiveRow() {
248 $page = $this->createPage(
249 'RevisionStorageTest_testNewFromArchiveRow',
250 'Lorem Ipsum',
251 CONTENT_MODEL_WIKITEXT
252 );
253 $orig = $page->getRevision();
254 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
255
256 $dbr = wfGetDB( DB_REPLICA );
257 $res = $dbr->select(
258 'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
259 );
260 $this->assertTrue( is_object( $res ), 'query failed' );
261
262 $row = $res->fetchObject();
263 $res->free();
264
265 $rev = Revision::newFromArchiveRow( $row );
266
267 $this->assertRevEquals( $orig, $rev );
268 }
269
270 /**
271 * @covers Revision::newFromId
272 */
273 public function testNewFromId() {
274 $orig = $this->makeRevision();
275
276 $rev = Revision::newFromId( $orig->getId() );
277
278 $this->assertRevEquals( $orig, $rev );
279 }
280
281 /**
282 * @covers Revision::fetchRevision
283 */
284 public function testFetchRevision() {
285 $page = $this->createPage(
286 'RevisionStorageTest_testFetchRevision',
287 'one',
288 CONTENT_MODEL_WIKITEXT
289 );
290
291 // Hidden process cache assertion below
292 $page->getRevision()->getId();
293
294 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
295 $id = $page->getRevision()->getId();
296
297 $res = Revision::fetchRevision( $page->getTitle() );
298
299 # note: order is unspecified
300 $rows = [];
301 while ( ( $row = $res->fetchObject() ) ) {
302 $rows[$row->rev_id] = $row;
303 }
304
305 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
306 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
307 }
308
309 /**
310 * @covers Revision::selectFields
311 */
312 public function testSelectFields() {
313 global $wgContentHandlerUseDB;
314
315 $fields = Revision::selectFields();
316
317 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
318 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
319 $this->assertTrue(
320 in_array( 'rev_timestamp', $fields ),
321 'missing rev_timestamp in list of fields'
322 );
323 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
324
325 if ( $wgContentHandlerUseDB ) {
326 $this->assertTrue( in_array( 'rev_content_model', $fields ),
327 'missing rev_content_model in list of fields' );
328 $this->assertTrue( in_array( 'rev_content_format', $fields ),
329 'missing rev_content_format in list of fields' );
330 }
331 }
332
333 /**
334 * @covers Revision::getPage
335 */
336 public function testGetPage() {
337 $page = $this->the_page;
338
339 $orig = $this->makeRevision( [ 'page' => $page->getId() ] );
340 $rev = Revision::newFromId( $orig->getId() );
341
342 $this->assertEquals( $page->getId(), $rev->getPage() );
343 }
344
345 /**
346 * @covers Revision::isCurrent
347 */
348 public function testIsCurrent() {
349 $page = $this->createPage(
350 'RevisionStorageTest_testIsCurrent',
351 'Lorem Ipsum',
352 CONTENT_MODEL_WIKITEXT
353 );
354 $rev1 = $page->getRevision();
355
356 # @todo find out if this should be true
357 # $this->assertTrue( $rev1->isCurrent() );
358
359 $rev1x = Revision::newFromId( $rev1->getId() );
360 $this->assertTrue( $rev1x->isCurrent() );
361
362 $page->doEditContent(
363 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
364 'second rev'
365 );
366 $rev2 = $page->getRevision();
367
368 # @todo find out if this should be true
369 # $this->assertTrue( $rev2->isCurrent() );
370
371 $rev1x = Revision::newFromId( $rev1->getId() );
372 $this->assertFalse( $rev1x->isCurrent() );
373
374 $rev2x = Revision::newFromId( $rev2->getId() );
375 $this->assertTrue( $rev2x->isCurrent() );
376 }
377
378 /**
379 * @covers Revision::getPrevious
380 */
381 public function testGetPrevious() {
382 $page = $this->createPage(
383 'RevisionStorageTest_testGetPrevious',
384 'Lorem Ipsum testGetPrevious',
385 CONTENT_MODEL_WIKITEXT
386 );
387 $rev1 = $page->getRevision();
388
389 $this->assertNull( $rev1->getPrevious() );
390
391 $page->doEditContent(
392 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
393 'second rev testGetPrevious' );
394 $rev2 = $page->getRevision();
395
396 $this->assertNotNull( $rev2->getPrevious() );
397 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
398 }
399
400 /**
401 * @covers Revision::getNext
402 */
403 public function testGetNext() {
404 $page = $this->createPage(
405 'RevisionStorageTest_testGetNext',
406 'Lorem Ipsum testGetNext',
407 CONTENT_MODEL_WIKITEXT
408 );
409 $rev1 = $page->getRevision();
410
411 $this->assertNull( $rev1->getNext() );
412
413 $page->doEditContent(
414 ContentHandler::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
415 'second rev testGetNext'
416 );
417 $rev2 = $page->getRevision();
418
419 $this->assertNotNull( $rev1->getNext() );
420 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
421 }
422
423 /**
424 * @covers Revision::newNullRevision
425 */
426 public function testNewNullRevision() {
427 $page = $this->createPage(
428 'RevisionStorageTest_testNewNullRevision',
429 'some testing text',
430 CONTENT_MODEL_WIKITEXT
431 );
432 $orig = $page->getRevision();
433
434 $dbw = wfGetDB( DB_MASTER );
435 $rev = Revision::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
436
437 $this->assertNotEquals( $orig->getId(), $rev->getId(),
438 'new null revision shold have a different id from the original revision' );
439 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
440 'new null revision shold have the same text id as the original revision' );
441 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
442 }
443
444 /**
445 * @covers Revision::insertOn
446 */
447 public function testInsertOn() {
448 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
449
450 $orig = $this->makeRevision( [
451 'user_text' => $ip
452 ] );
453
454 // Make sure the revision was copied to ip_changes
455 $dbr = wfGetDB( DB_REPLICA );
456 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
457 $row = $res->fetchObject();
458
459 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
460 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
461 }
462
463 public static function provideUserWasLastToEdit() {
464 yield 'actually the last edit' => [ 3, true ];
465 yield 'not the current edit, but still by this user' => [ 2, true ];
466 yield 'edit by another user' => [ 1, false ];
467 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
468 }
469
470 /**
471 * @dataProvider provideUserWasLastToEdit
472 */
473 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
474 $userA = User::newFromName( "RevisionStorageTest_userA" );
475 $userB = User::newFromName( "RevisionStorageTest_userB" );
476
477 if ( $userA->getId() === 0 ) {
478 $userA = User::createNew( $userA->getName() );
479 }
480
481 if ( $userB->getId() === 0 ) {
482 $userB = User::createNew( $userB->getName() );
483 }
484
485 $ns = $this->getDefaultWikitextNS();
486
487 $dbw = wfGetDB( DB_MASTER );
488 $revisions = [];
489
490 // create revisions -----------------------------
491 $page = WikiPage::factory( Title::newFromText(
492 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
493 $page->insertOn( $dbw );
494
495 $revisions[0] = new Revision( [
496 'page' => $page->getId(),
497 // we need the title to determine the page's default content model
498 'title' => $page->getTitle(),
499 'timestamp' => '20120101000000',
500 'user' => $userA->getId(),
501 'text' => 'zero',
502 'content_model' => CONTENT_MODEL_WIKITEXT,
503 'summary' => 'edit zero'
504 ] );
505 $revisions[0]->insertOn( $dbw );
506
507 $revisions[1] = new Revision( [
508 'page' => $page->getId(),
509 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
510 'title' => $page->getTitle(),
511 'timestamp' => '20120101000100',
512 'user' => $userA->getId(),
513 'text' => 'one',
514 'content_model' => CONTENT_MODEL_WIKITEXT,
515 'summary' => 'edit one'
516 ] );
517 $revisions[1]->insertOn( $dbw );
518
519 $revisions[2] = new Revision( [
520 'page' => $page->getId(),
521 'title' => $page->getTitle(),
522 'timestamp' => '20120101000200',
523 'user' => $userB->getId(),
524 'text' => 'two',
525 'content_model' => CONTENT_MODEL_WIKITEXT,
526 'summary' => 'edit two'
527 ] );
528 $revisions[2]->insertOn( $dbw );
529
530 $revisions[3] = new Revision( [
531 'page' => $page->getId(),
532 'title' => $page->getTitle(),
533 'timestamp' => '20120101000300',
534 'user' => $userA->getId(),
535 'text' => 'three',
536 'content_model' => CONTENT_MODEL_WIKITEXT,
537 'summary' => 'edit three'
538 ] );
539 $revisions[3]->insertOn( $dbw );
540
541 $revisions[4] = new Revision( [
542 'page' => $page->getId(),
543 'title' => $page->getTitle(),
544 'timestamp' => '20120101000200',
545 'user' => $userA->getId(),
546 'text' => 'zero',
547 'content_model' => CONTENT_MODEL_WIKITEXT,
548 'summary' => 'edit four'
549 ] );
550 $revisions[4]->insertOn( $dbw );
551
552 // test it ---------------------------------
553 $since = $revisions[$sinceIdx]->getTimestamp();
554
555 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
556
557 $this->assertEquals( $expectedLast, $wasLast );
558 }
559
560 /**
561 * @param string $text
562 * @param string $title
563 * @param string $model
564 * @param string $format
565 *
566 * @return Revision
567 */
568 private function newTestRevision( $text, $title = "Test",
569 $model = CONTENT_MODEL_WIKITEXT, $format = null
570 ) {
571 if ( is_string( $title ) ) {
572 $title = Title::newFromText( $title );
573 }
574
575 $content = ContentHandler::makeContent( $text, $title, $model, $format );
576
577 $rev = new Revision(
578 [
579 'id' => 42,
580 'page' => 23,
581 'title' => $title,
582
583 'content' => $content,
584 'length' => $content->getSize(),
585 'comment' => "testing",
586 'minor_edit' => false,
587
588 'content_format' => $format,
589 ]
590 );
591
592 return $rev;
593 }
594
595 public function provideGetContentModel() {
596 // NOTE: we expect the help namespace to always contain wikitext
597 return [
598 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
599 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
600 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
601 ];
602 }
603
604 /**
605 * @dataProvider provideGetContentModel
606 * @covers Revision::getContentModel
607 */
608 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
609 $rev = $this->newTestRevision( $text, $title, $model, $format );
610
611 $this->assertEquals( $expectedModel, $rev->getContentModel() );
612 }
613
614 public function provideGetContentFormat() {
615 // NOTE: we expect the help namespace to always contain wikitext
616 return [
617 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
618 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
619 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
620 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
621 ];
622 }
623
624 /**
625 * @dataProvider provideGetContentFormat
626 * @covers Revision::getContentFormat
627 */
628 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
629 $rev = $this->newTestRevision( $text, $title, $model, $format );
630
631 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
632 }
633
634 public function provideGetContentHandler() {
635 // NOTE: we expect the help namespace to always contain wikitext
636 return [
637 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
638 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
639 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
640 ];
641 }
642
643 /**
644 * @dataProvider provideGetContentHandler
645 * @covers Revision::getContentHandler
646 */
647 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
648 $rev = $this->newTestRevision( $text, $title, $model, $format );
649
650 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
651 }
652
653 public function provideGetContent() {
654 // NOTE: we expect the help namespace to always contain wikitext
655 return [
656 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
657 [
658 serialize( 'hello world' ),
659 'Hello',
660 DummyContentForTesting::MODEL_ID,
661 null,
662 Revision::FOR_PUBLIC,
663 serialize( 'hello world' )
664 ],
665 [
666 serialize( 'hello world' ),
667 'Dummy:Hello',
668 null,
669 null,
670 Revision::FOR_PUBLIC,
671 serialize( 'hello world' )
672 ],
673 ];
674 }
675
676 /**
677 * @dataProvider provideGetContent
678 * @covers Revision::getContent
679 */
680 public function testGetContent( $text, $title, $model, $format,
681 $audience, $expectedSerialization
682 ) {
683 $rev = $this->newTestRevision( $text, $title, $model, $format );
684 $content = $rev->getContent( $audience );
685
686 $this->assertEquals(
687 $expectedSerialization,
688 is_null( $content ) ? null : $content->serialize( $format )
689 );
690 }
691
692 /**
693 * @covers Revision::getContent
694 */
695 public function testGetContent_failure() {
696 $rev = new Revision( [
697 'page' => $this->the_page->getId(),
698 'content_model' => $this->the_page->getContentModel(),
699 'text_id' => 123456789, // not in the test DB
700 ] );
701
702 $this->assertNull( $rev->getContent(),
703 "getContent() should return null if the revision's text blob could not be loaded." );
704
705 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
706 $this->assertNull( $rev->getContent(),
707 "getContent() should return null if the revision's text blob could not be loaded." );
708 }
709
710 public function provideGetSize() {
711 return [
712 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
713 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
714 ];
715 }
716
717 /**
718 * @covers Revision::getSize
719 * @dataProvider provideGetSize
720 */
721 public function testGetSize( $text, $model, $expected_size ) {
722 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
723 $this->assertEquals( $expected_size, $rev->getSize() );
724 }
725
726 public function provideGetSha1() {
727 return [
728 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
729 [
730 serialize( "hello world." ),
731 DummyContentForTesting::MODEL_ID,
732 Revision::base36Sha1( serialize( "hello world." ) )
733 ],
734 ];
735 }
736
737 /**
738 * @covers Revision::getSha1
739 * @dataProvider provideGetSha1
740 */
741 public function testGetSha1( $text, $model, $expected_hash ) {
742 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
743 $this->assertEquals( $expected_hash, $rev->getSha1() );
744 }
745
746 /**
747 * Tests whether $rev->getContent() returns a clone when needed.
748 *
749 * @covers Revision::getContent
750 */
751 public function testGetContentClone() {
752 $content = new RevisionTestModifyableContent( "foo" );
753
754 $rev = new Revision(
755 [
756 'id' => 42,
757 'page' => 23,
758 'title' => Title::newFromText( "testGetContentClone_dummy" ),
759
760 'content' => $content,
761 'length' => $content->getSize(),
762 'comment' => "testing",
763 'minor_edit' => false,
764 ]
765 );
766
767 /** @var RevisionTestModifyableContent $content */
768 $content = $rev->getContent( Revision::RAW );
769 $content->setText( "bar" );
770
771 /** @var RevisionTestModifyableContent $content2 */
772 $content2 = $rev->getContent( Revision::RAW );
773 // content is mutable, expect clone
774 $this->assertNotSame( $content, $content2, "expected a clone" );
775 // clone should contain the original text
776 $this->assertEquals( "foo", $content2->getText() );
777
778 $content2->setText( "bla bla" );
779 // clones should be independent
780 $this->assertEquals( "bar", $content->getText() );
781 }
782
783 /**
784 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
785 * @covers Revision::getContent
786 */
787 public function testGetContentUncloned() {
788 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
789 $content = $rev->getContent( Revision::RAW );
790 $content2 = $rev->getContent( Revision::RAW );
791
792 // for immutable content like wikitext, this should be the same object
793 $this->assertSame( $content, $content2 );
794 }
795
796 }