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