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