RevisionUnittest for select*Fields methods
[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 public function provideTrueFalse() {
229 yield [ true ];
230 yield [ false ];
231 }
232
233 /**
234 * @dataProvider provideTrueFalse
235 * @covers Revision::newFromArchiveRow
236 */
237 public function testNewFromArchiveRow( $contentHandlerUseDB ) {
238 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
239
240 $page = $this->createPage(
241 'RevisionStorageTest_testNewFromArchiveRow',
242 'Lorem Ipsum',
243 CONTENT_MODEL_WIKITEXT
244 );
245 $orig = $page->getRevision();
246 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
247
248 $dbr = wfGetDB( DB_REPLICA );
249 $res = $dbr->select(
250 'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
251 );
252 $this->assertTrue( is_object( $res ), 'query failed' );
253
254 $row = $res->fetchObject();
255 $res->free();
256
257 $rev = Revision::newFromArchiveRow( $row );
258
259 $this->assertRevEquals( $orig, $rev );
260 }
261
262 /**
263 * @covers Revision::newFromId
264 */
265 public function testNewFromId() {
266 $orig = $this->testPage->getRevision();
267 $rev = Revision::newFromId( $orig->getId() );
268 $this->assertRevEquals( $orig, $rev );
269 }
270
271 /**
272 * @covers Revision::newFromPageId
273 */
274 public function testNewFromPageId() {
275 $rev = Revision::newFromPageId( $this->testPage->getId() );
276 $this->assertRevEquals(
277 $this->testPage->getRevision(),
278 $rev
279 );
280 }
281
282 /**
283 * @covers Revision::newFromPageId
284 */
285 public function testNewFromPageIdWithLatestId() {
286 $rev = Revision::newFromPageId(
287 $this->testPage->getId(),
288 $this->testPage->getLatest()
289 );
290 $this->assertRevEquals(
291 $this->testPage->getRevision(),
292 $rev
293 );
294 }
295
296 /**
297 * @covers Revision::newFromPageId
298 */
299 public function testNewFromPageIdWithNotLatestId() {
300 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
301 $rev = Revision::newFromPageId(
302 $this->testPage->getId(),
303 $this->testPage->getRevision()->getPrevious()->getId()
304 );
305 $this->assertRevEquals(
306 $this->testPage->getRevision()->getPrevious(),
307 $rev
308 );
309 }
310
311 /**
312 * @covers Revision::fetchRevision
313 */
314 public function testFetchRevision() {
315 // Hidden process cache assertion below
316 $this->testPage->getRevision()->getId();
317
318 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
319 $id = $this->testPage->getRevision()->getId();
320
321 $res = Revision::fetchRevision( $this->testPage->getTitle() );
322
323 # note: order is unspecified
324 $rows = [];
325 while ( ( $row = $res->fetchObject() ) ) {
326 $rows[$row->rev_id] = $row;
327 }
328
329 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
330 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
331 }
332
333 /**
334 * @covers Revision::getPage
335 */
336 public function testGetPage() {
337 $page = $this->testPage;
338
339 $orig = $this->makeRevisionWithProps( [ '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 $rev1 = $this->testPage->getRevision();
350
351 # @todo find out if this should be true
352 # $this->assertTrue( $rev1->isCurrent() );
353
354 $rev1x = Revision::newFromId( $rev1->getId() );
355 $this->assertTrue( $rev1x->isCurrent() );
356
357 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
358 $rev2 = $this->testPage->getRevision();
359
360 # @todo find out if this should be true
361 # $this->assertTrue( $rev2->isCurrent() );
362
363 $rev1x = Revision::newFromId( $rev1->getId() );
364 $this->assertFalse( $rev1x->isCurrent() );
365
366 $rev2x = Revision::newFromId( $rev2->getId() );
367 $this->assertTrue( $rev2x->isCurrent() );
368 }
369
370 /**
371 * @covers Revision::getPrevious
372 */
373 public function testGetPrevious() {
374 $oldestRevision = $this->testPage->getOldestRevision();
375 $latestRevision = $this->testPage->getLatest();
376
377 $this->assertNull( $oldestRevision->getPrevious() );
378
379 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
380 $newRevision = $this->testPage->getRevision();
381
382 $this->assertNotNull( $newRevision->getPrevious() );
383 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
384 }
385
386 /**
387 * @covers Revision::getNext
388 */
389 public function testGetNext() {
390 $rev1 = $this->testPage->getRevision();
391
392 $this->assertNull( $rev1->getNext() );
393
394 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
395 $rev2 = $this->testPage->getRevision();
396
397 $this->assertNotNull( $rev1->getNext() );
398 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
399 }
400
401 /**
402 * @covers Revision::newNullRevision
403 */
404 public function testNewNullRevision() {
405 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
406 $orig = $this->testPage->getRevision();
407
408 $dbw = wfGetDB( DB_MASTER );
409 $rev = Revision::newNullRevision( $dbw, $this->testPage->getId(), 'a null revision', false );
410
411 $this->assertNotEquals( $orig->getId(), $rev->getId(),
412 'new null revision should have a different id from the original revision' );
413 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
414 'new null revision should have the same text id as the original revision' );
415 $this->assertEquals( __METHOD__, $rev->getContent()->getNativeData() );
416 }
417
418 /**
419 * @covers Revision::insertOn
420 */
421 public function testInsertOn() {
422 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
423
424 $orig = $this->makeRevisionWithProps( [
425 'user_text' => $ip
426 ] );
427
428 // Make sure the revision was copied to ip_changes
429 $dbr = wfGetDB( DB_REPLICA );
430 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
431 $row = $res->fetchObject();
432
433 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
434 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
435 }
436
437 public static function provideUserWasLastToEdit() {
438 yield 'actually the last edit' => [ 3, true ];
439 yield 'not the current edit, but still by this user' => [ 2, true ];
440 yield 'edit by another user' => [ 1, false ];
441 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
442 }
443
444 /**
445 * @dataProvider provideUserWasLastToEdit
446 */
447 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
448 $userA = User::newFromName( "RevisionStorageTest_userA" );
449 $userB = User::newFromName( "RevisionStorageTest_userB" );
450
451 if ( $userA->getId() === 0 ) {
452 $userA = User::createNew( $userA->getName() );
453 }
454
455 if ( $userB->getId() === 0 ) {
456 $userB = User::createNew( $userB->getName() );
457 }
458
459 $ns = $this->getDefaultWikitextNS();
460
461 $dbw = wfGetDB( DB_MASTER );
462 $revisions = [];
463
464 // create revisions -----------------------------
465 $page = WikiPage::factory( Title::newFromText(
466 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
467 $page->insertOn( $dbw );
468
469 $revisions[0] = new Revision( [
470 'page' => $page->getId(),
471 // we need the title to determine the page's default content model
472 'title' => $page->getTitle(),
473 'timestamp' => '20120101000000',
474 'user' => $userA->getId(),
475 'text' => 'zero',
476 'content_model' => CONTENT_MODEL_WIKITEXT,
477 'summary' => 'edit zero'
478 ] );
479 $revisions[0]->insertOn( $dbw );
480
481 $revisions[1] = new Revision( [
482 'page' => $page->getId(),
483 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
484 'title' => $page->getTitle(),
485 'timestamp' => '20120101000100',
486 'user' => $userA->getId(),
487 'text' => 'one',
488 'content_model' => CONTENT_MODEL_WIKITEXT,
489 'summary' => 'edit one'
490 ] );
491 $revisions[1]->insertOn( $dbw );
492
493 $revisions[2] = new Revision( [
494 'page' => $page->getId(),
495 'title' => $page->getTitle(),
496 'timestamp' => '20120101000200',
497 'user' => $userB->getId(),
498 'text' => 'two',
499 'content_model' => CONTENT_MODEL_WIKITEXT,
500 'summary' => 'edit two'
501 ] );
502 $revisions[2]->insertOn( $dbw );
503
504 $revisions[3] = new Revision( [
505 'page' => $page->getId(),
506 'title' => $page->getTitle(),
507 'timestamp' => '20120101000300',
508 'user' => $userA->getId(),
509 'text' => 'three',
510 'content_model' => CONTENT_MODEL_WIKITEXT,
511 'summary' => 'edit three'
512 ] );
513 $revisions[3]->insertOn( $dbw );
514
515 $revisions[4] = new Revision( [
516 'page' => $page->getId(),
517 'title' => $page->getTitle(),
518 'timestamp' => '20120101000200',
519 'user' => $userA->getId(),
520 'text' => 'zero',
521 'content_model' => CONTENT_MODEL_WIKITEXT,
522 'summary' => 'edit four'
523 ] );
524 $revisions[4]->insertOn( $dbw );
525
526 // test it ---------------------------------
527 $since = $revisions[$sinceIdx]->getTimestamp();
528
529 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
530
531 $this->assertEquals( $expectedLast, $wasLast );
532 }
533
534 /**
535 * @param string $text
536 * @param string $title
537 * @param string $model
538 * @param string $format
539 *
540 * @return Revision
541 */
542 private function newTestRevision( $text, $title = "Test",
543 $model = CONTENT_MODEL_WIKITEXT, $format = null
544 ) {
545 if ( is_string( $title ) ) {
546 $title = Title::newFromText( $title );
547 }
548
549 $content = ContentHandler::makeContent( $text, $title, $model, $format );
550
551 $rev = new Revision(
552 [
553 'id' => 42,
554 'page' => 23,
555 'title' => $title,
556
557 'content' => $content,
558 'length' => $content->getSize(),
559 'comment' => "testing",
560 'minor_edit' => false,
561
562 'content_format' => $format,
563 ]
564 );
565
566 return $rev;
567 }
568
569 public function provideGetContentModel() {
570 // NOTE: we expect the help namespace to always contain wikitext
571 return [
572 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
573 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
574 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
575 ];
576 }
577
578 /**
579 * @dataProvider provideGetContentModel
580 * @covers Revision::getContentModel
581 */
582 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
583 $rev = $this->newTestRevision( $text, $title, $model, $format );
584
585 $this->assertEquals( $expectedModel, $rev->getContentModel() );
586 }
587
588 public function provideGetContentFormat() {
589 // NOTE: we expect the help namespace to always contain wikitext
590 return [
591 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
592 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
593 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
594 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
595 ];
596 }
597
598 /**
599 * @dataProvider provideGetContentFormat
600 * @covers Revision::getContentFormat
601 */
602 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
603 $rev = $this->newTestRevision( $text, $title, $model, $format );
604
605 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
606 }
607
608 public function provideGetContentHandler() {
609 // NOTE: we expect the help namespace to always contain wikitext
610 return [
611 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
612 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
613 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
614 ];
615 }
616
617 /**
618 * @dataProvider provideGetContentHandler
619 * @covers Revision::getContentHandler
620 */
621 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
622 $rev = $this->newTestRevision( $text, $title, $model, $format );
623
624 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
625 }
626
627 public function provideGetContent() {
628 // NOTE: we expect the help namespace to always contain wikitext
629 return [
630 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
631 [
632 serialize( 'hello world' ),
633 'Hello',
634 DummyContentForTesting::MODEL_ID,
635 null,
636 Revision::FOR_PUBLIC,
637 serialize( 'hello world' )
638 ],
639 [
640 serialize( 'hello world' ),
641 'Dummy:Hello',
642 null,
643 null,
644 Revision::FOR_PUBLIC,
645 serialize( 'hello world' )
646 ],
647 ];
648 }
649
650 /**
651 * @dataProvider provideGetContent
652 * @covers Revision::getContent
653 */
654 public function testGetContent( $text, $title, $model, $format,
655 $audience, $expectedSerialization
656 ) {
657 $rev = $this->newTestRevision( $text, $title, $model, $format );
658 $content = $rev->getContent( $audience );
659
660 $this->assertEquals(
661 $expectedSerialization,
662 is_null( $content ) ? null : $content->serialize( $format )
663 );
664 }
665
666 /**
667 * @covers Revision::getContent
668 */
669 public function testGetContent_failure() {
670 $rev = new Revision( [
671 'page' => $this->testPage->getId(),
672 'content_model' => $this->testPage->getContentModel(),
673 'text_id' => 123456789, // not in the test DB
674 ] );
675
676 $this->assertNull( $rev->getContent(),
677 "getContent() should return null if the revision's text blob could not be loaded." );
678
679 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
680 $this->assertNull( $rev->getContent(),
681 "getContent() should return null if the revision's text blob could not be loaded." );
682 }
683
684 public function provideGetSize() {
685 return [
686 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
687 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
688 ];
689 }
690
691 /**
692 * @covers Revision::getSize
693 * @dataProvider provideGetSize
694 */
695 public function testGetSize( $text, $model, $expected_size ) {
696 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
697 $this->assertEquals( $expected_size, $rev->getSize() );
698 }
699
700 public function provideGetSha1() {
701 return [
702 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
703 [
704 serialize( "hello world." ),
705 DummyContentForTesting::MODEL_ID,
706 Revision::base36Sha1( serialize( "hello world." ) )
707 ],
708 ];
709 }
710
711 /**
712 * @covers Revision::getSha1
713 * @dataProvider provideGetSha1
714 */
715 public function testGetSha1( $text, $model, $expected_hash ) {
716 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
717 $this->assertEquals( $expected_hash, $rev->getSha1() );
718 }
719
720 /**
721 * Tests whether $rev->getContent() returns a clone when needed.
722 *
723 * @covers Revision::getContent
724 */
725 public function testGetContentClone() {
726 $content = new RevisionTestModifyableContent( "foo" );
727
728 $rev = new Revision(
729 [
730 'id' => 42,
731 'page' => 23,
732 'title' => Title::newFromText( "testGetContentClone_dummy" ),
733
734 'content' => $content,
735 'length' => $content->getSize(),
736 'comment' => "testing",
737 'minor_edit' => false,
738 ]
739 );
740
741 /** @var RevisionTestModifyableContent $content */
742 $content = $rev->getContent( Revision::RAW );
743 $content->setText( "bar" );
744
745 /** @var RevisionTestModifyableContent $content2 */
746 $content2 = $rev->getContent( Revision::RAW );
747 // content is mutable, expect clone
748 $this->assertNotSame( $content, $content2, "expected a clone" );
749 // clone should contain the original text
750 $this->assertEquals( "foo", $content2->getText() );
751
752 $content2->setText( "bla bla" );
753 // clones should be independent
754 $this->assertEquals( "bar", $content->getText() );
755 }
756
757 /**
758 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
759 * @covers Revision::getContent
760 */
761 public function testGetContentUncloned() {
762 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
763 $content = $rev->getContent( Revision::RAW );
764 $content2 = $rev->getContent( Revision::RAW );
765
766 // for immutable content like wikitext, this should be the same object
767 $this->assertSame( $content, $content2 );
768 }
769
770 /**
771 * @covers Revision::loadFromId
772 */
773 public function testLoadFromId() {
774 $rev = $this->testPage->getRevision();
775 $this->assertRevEquals(
776 $rev,
777 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
778 );
779 }
780
781 /**
782 * @covers Revision::loadFromPageId
783 */
784 public function testLoadFromPageId() {
785 $this->assertRevEquals(
786 $this->testPage->getRevision(),
787 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
788 );
789 }
790
791 /**
792 * @covers Revision::loadFromPageId
793 */
794 public function testLoadFromPageIdWithLatestRevId() {
795 $this->assertRevEquals(
796 $this->testPage->getRevision(),
797 Revision::loadFromPageId(
798 wfGetDB( DB_MASTER ),
799 $this->testPage->getId(),
800 $this->testPage->getLatest()
801 )
802 );
803 }
804
805 /**
806 * @covers Revision::loadFromPageId
807 */
808 public function testLoadFromPageIdWithNotLatestRevId() {
809 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
810 $this->assertRevEquals(
811 $this->testPage->getRevision()->getPrevious(),
812 Revision::loadFromPageId(
813 wfGetDB( DB_MASTER ),
814 $this->testPage->getId(),
815 $this->testPage->getRevision()->getPrevious()->getId()
816 )
817 );
818 }
819
820 /**
821 * @covers Revision::loadFromTitle
822 */
823 public function testLoadFromTitle() {
824 $this->assertRevEquals(
825 $this->testPage->getRevision(),
826 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
827 );
828 }
829
830 /**
831 * @covers Revision::loadFromTitle
832 */
833 public function testLoadFromTitleWithLatestRevId() {
834 $this->assertRevEquals(
835 $this->testPage->getRevision(),
836 Revision::loadFromTitle(
837 wfGetDB( DB_MASTER ),
838 $this->testPage->getTitle(),
839 $this->testPage->getLatest()
840 )
841 );
842 }
843
844 /**
845 * @covers Revision::loadFromTitle
846 */
847 public function testLoadFromTitleWithNotLatestRevId() {
848 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
849 $this->assertRevEquals(
850 $this->testPage->getRevision()->getPrevious(),
851 Revision::loadFromTitle(
852 wfGetDB( DB_MASTER ),
853 $this->testPage->getTitle(),
854 $this->testPage->getRevision()->getPrevious()->getId()
855 )
856 );
857 }
858
859 /**
860 * @covers Revision::loadFromTimestamp()
861 */
862 public function testLoadFromTimestamp() {
863 $this->assertRevEquals(
864 $this->testPage->getRevision(),
865 Revision::loadFromTimestamp(
866 wfGetDB( DB_MASTER ),
867 $this->testPage->getTitle(),
868 $this->testPage->getRevision()->getTimestamp()
869 )
870 );
871 }
872
873 }