48e00e0e2cc219fee2d65399ce84bdd92d110b83
[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::insertOn
156 */
157 public function testInsertOn_success() {
158 $parentId = $this->testPage->getLatest();
159
160 // If an ExternalStore is set don't use it.
161 $this->setMwGlobals( 'wgDefaultExternalStore', false );
162
163 $rev = new Revision( [
164 'page' => $this->testPage->getId(),
165 'title' => $this->testPage->getTitle(),
166 'text' => 'Revision Text',
167 'comment' => 'Revision comment',
168 ] );
169
170 $revId = $rev->insertOn( wfGetDB( DB_MASTER ) );
171
172 $this->assertInternalType( 'integer', $revId );
173 $this->assertInternalType( 'integer', $rev->getTextId() );
174 $this->assertSame( $revId, $rev->getId() );
175
176 $this->assertSelect(
177 'text',
178 [ 'old_id', 'old_text' ],
179 "old_id = {$rev->getTextId()}",
180 [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
181 );
182 $this->assertSelect(
183 'revision',
184 [
185 'rev_id',
186 'rev_page',
187 'rev_text_id',
188 'rev_user',
189 'rev_minor_edit',
190 'rev_deleted',
191 'rev_len',
192 'rev_parent_id',
193 'rev_sha1',
194 ],
195 "rev_id = {$rev->getId()}",
196 [ [
197 strval( $rev->getId() ),
198 strval( $this->testPage->getId() ),
199 strval( $rev->getTextId() ),
200 '0',
201 '0',
202 '0',
203 '13',
204 strval( $parentId ),
205 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
206 ] ]
207 );
208 }
209
210 /**
211 * @covers Revision::insertOn
212 */
213 public function testInsertOn_exceptionOnNoPage() {
214 // If an ExternalStore is set don't use it.
215 $this->setMwGlobals( 'wgDefaultExternalStore', false );
216 $this->setExpectedException(
217 MWException::class,
218 "Cannot insert revision: page ID must be nonzero"
219 );
220
221 $rev = new Revision( [] );
222
223 $rev->insertOn( wfGetDB( DB_MASTER ) );
224 }
225
226 /**
227 * @covers Revision::newFromTitle
228 */
229 public function testNewFromTitle_withoutId() {
230 $latestRevId = $this->testPage->getLatest();
231
232 $rev = Revision::newFromTitle( $this->testPage->getTitle() );
233
234 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
235 $this->assertEquals( $latestRevId, $rev->getId() );
236 }
237
238 /**
239 * @covers Revision::newFromTitle
240 */
241 public function testNewFromTitle_withId() {
242 $latestRevId = $this->testPage->getLatest();
243
244 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId );
245
246 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
247 $this->assertEquals( $latestRevId, $rev->getId() );
248 }
249
250 /**
251 * @covers Revision::newFromTitle
252 */
253 public function testNewFromTitle_withBadId() {
254 $latestRevId = $this->testPage->getLatest();
255
256 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId + 1 );
257
258 $this->assertNull( $rev );
259 }
260
261 /**
262 * @covers Revision::newFromRow
263 */
264 public function testNewFromRow() {
265 $orig = $this->makeRevisionWithProps();
266
267 $dbr = wfGetDB( DB_REPLICA );
268 $revQuery = Revision::getQueryInfo();
269 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
270 __METHOD__, [], $revQuery['joins'] );
271 $this->assertTrue( is_object( $res ), 'query failed' );
272
273 $row = $res->fetchObject();
274 $res->free();
275
276 $rev = Revision::newFromRow( $row );
277
278 $this->assertRevEquals( $orig, $rev );
279 }
280
281 public function provideNewFromArchiveRow() {
282 yield [
283 true,
284 function ( $f ) {
285 return $f;
286 },
287 ];
288 yield [
289 false,
290 function ( $f ) {
291 return $f;
292 },
293 ];
294 yield [
295 true,
296 function ( $f ) {
297 return $f + [ 'ar_namespace', 'ar_title' ];
298 },
299 ];
300 yield [
301 false,
302 function ( $f ) {
303 return $f + [ 'ar_namespace', 'ar_title' ];
304 },
305 ];
306 yield [
307 true,
308 function ( $f ) {
309 unset( $f['ar_text_id'] );
310 return $f;
311 },
312 ];
313 yield [
314 false,
315 function ( $f ) {
316 unset( $f['ar_text_id'] );
317 return $f;
318 },
319 ];
320 }
321
322 /**
323 * @dataProvider provideNewFromArchiveRow
324 * @covers Revision::newFromArchiveRow
325 */
326 public function testNewFromArchiveRow( $contentHandlerUseDB, $selectModifier ) {
327 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
328
329 $page = $this->createPage(
330 'RevisionStorageTest_testNewFromArchiveRow',
331 'Lorem Ipsum',
332 CONTENT_MODEL_WIKITEXT
333 );
334 $orig = $page->getRevision();
335 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
336
337 $dbr = wfGetDB( DB_REPLICA );
338 $arQuery = Revision::getArchiveQueryInfo();
339 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
340 $res = $dbr->select(
341 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
342 __METHOD__, [], $arQuery['joins']
343 );
344 $this->assertTrue( is_object( $res ), 'query failed' );
345
346 $row = $res->fetchObject();
347 $res->free();
348
349 $rev = Revision::newFromArchiveRow( $row );
350
351 $this->assertRevEquals( $orig, $rev );
352 }
353
354 /**
355 * @covers Revision::newFromArchiveRow
356 */
357 public function testNewFromArchiveRowOverrides() {
358 $page = $this->createPage(
359 'RevisionStorageTest_testNewFromArchiveRow',
360 'Lorem Ipsum',
361 CONTENT_MODEL_WIKITEXT
362 );
363 $orig = $page->getRevision();
364 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
365
366 $dbr = wfGetDB( DB_REPLICA );
367 $arQuery = Revision::getArchiveQueryInfo();
368 $res = $dbr->select(
369 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
370 __METHOD__, [], $arQuery['joins']
371 );
372 $this->assertTrue( is_object( $res ), 'query failed' );
373
374 $row = $res->fetchObject();
375 $res->free();
376
377 $rev = Revision::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
378
379 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
380 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
381 }
382
383 /**
384 * @covers Revision::newFromId
385 */
386 public function testNewFromId() {
387 $orig = $this->testPage->getRevision();
388 $rev = Revision::newFromId( $orig->getId() );
389 $this->assertRevEquals( $orig, $rev );
390 }
391
392 /**
393 * @covers Revision::newFromPageId
394 */
395 public function testNewFromPageId() {
396 $rev = Revision::newFromPageId( $this->testPage->getId() );
397 $this->assertRevEquals(
398 $this->testPage->getRevision(),
399 $rev
400 );
401 }
402
403 /**
404 * @covers Revision::newFromPageId
405 */
406 public function testNewFromPageIdWithLatestId() {
407 $rev = Revision::newFromPageId(
408 $this->testPage->getId(),
409 $this->testPage->getLatest()
410 );
411 $this->assertRevEquals(
412 $this->testPage->getRevision(),
413 $rev
414 );
415 }
416
417 /**
418 * @covers Revision::newFromPageId
419 */
420 public function testNewFromPageIdWithNotLatestId() {
421 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
422 $rev = Revision::newFromPageId(
423 $this->testPage->getId(),
424 $this->testPage->getRevision()->getPrevious()->getId()
425 );
426 $this->assertRevEquals(
427 $this->testPage->getRevision()->getPrevious(),
428 $rev
429 );
430 }
431
432 /**
433 * @covers Revision::fetchRevision
434 */
435 public function testFetchRevision() {
436 // Hidden process cache assertion below
437 $this->testPage->getRevision()->getId();
438
439 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
440 $id = $this->testPage->getRevision()->getId();
441
442 $res = Revision::fetchRevision( $this->testPage->getTitle() );
443
444 # note: order is unspecified
445 $rows = [];
446 while ( ( $row = $res->fetchObject() ) ) {
447 $rows[$row->rev_id] = $row;
448 }
449
450 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
451 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
452 }
453
454 /**
455 * @covers Revision::getPage
456 */
457 public function testGetPage() {
458 $page = $this->testPage;
459
460 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
461 $rev = Revision::newFromId( $orig->getId() );
462
463 $this->assertEquals( $page->getId(), $rev->getPage() );
464 }
465
466 /**
467 * @covers Revision::isCurrent
468 */
469 public function testIsCurrent() {
470 $rev1 = $this->testPage->getRevision();
471
472 # @todo find out if this should be true
473 # $this->assertTrue( $rev1->isCurrent() );
474
475 $rev1x = Revision::newFromId( $rev1->getId() );
476 $this->assertTrue( $rev1x->isCurrent() );
477
478 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
479 $rev2 = $this->testPage->getRevision();
480
481 # @todo find out if this should be true
482 # $this->assertTrue( $rev2->isCurrent() );
483
484 $rev1x = Revision::newFromId( $rev1->getId() );
485 $this->assertFalse( $rev1x->isCurrent() );
486
487 $rev2x = Revision::newFromId( $rev2->getId() );
488 $this->assertTrue( $rev2x->isCurrent() );
489 }
490
491 /**
492 * @covers Revision::getPrevious
493 */
494 public function testGetPrevious() {
495 $oldestRevision = $this->testPage->getOldestRevision();
496 $latestRevision = $this->testPage->getLatest();
497
498 $this->assertNull( $oldestRevision->getPrevious() );
499
500 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
501 $newRevision = $this->testPage->getRevision();
502
503 $this->assertNotNull( $newRevision->getPrevious() );
504 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
505 }
506
507 /**
508 * @covers Revision::getNext
509 */
510 public function testGetNext() {
511 $rev1 = $this->testPage->getRevision();
512
513 $this->assertNull( $rev1->getNext() );
514
515 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
516 $rev2 = $this->testPage->getRevision();
517
518 $this->assertNotNull( $rev1->getNext() );
519 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
520 }
521
522 /**
523 * @covers Revision::newNullRevision
524 */
525 public function testNewNullRevision() {
526 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
527 $orig = $this->testPage->getRevision();
528
529 $dbw = wfGetDB( DB_MASTER );
530 $rev = Revision::newNullRevision( $dbw, $this->testPage->getId(), 'a null revision', false );
531
532 $this->assertNotEquals( $orig->getId(), $rev->getId(),
533 'new null revision should have a different id from the original revision' );
534 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
535 'new null revision should have the same text id as the original revision' );
536 $this->assertEquals( __METHOD__, $rev->getContent()->getNativeData() );
537 }
538
539 /**
540 * @covers Revision::insertOn
541 */
542 public function testInsertOn() {
543 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
544
545 $orig = $this->makeRevisionWithProps( [
546 'user_text' => $ip
547 ] );
548
549 // Make sure the revision was copied to ip_changes
550 $dbr = wfGetDB( DB_REPLICA );
551 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
552 $row = $res->fetchObject();
553
554 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
555 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
556 }
557
558 public static function provideUserWasLastToEdit() {
559 yield 'actually the last edit' => [ 3, true ];
560 yield 'not the current edit, but still by this user' => [ 2, true ];
561 yield 'edit by another user' => [ 1, false ];
562 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
563 }
564
565 /**
566 * @dataProvider provideUserWasLastToEdit
567 */
568 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
569 $userA = User::newFromName( "RevisionStorageTest_userA" );
570 $userB = User::newFromName( "RevisionStorageTest_userB" );
571
572 if ( $userA->getId() === 0 ) {
573 $userA = User::createNew( $userA->getName() );
574 }
575
576 if ( $userB->getId() === 0 ) {
577 $userB = User::createNew( $userB->getName() );
578 }
579
580 $ns = $this->getDefaultWikitextNS();
581
582 $dbw = wfGetDB( DB_MASTER );
583 $revisions = [];
584
585 // create revisions -----------------------------
586 $page = WikiPage::factory( Title::newFromText(
587 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
588 $page->insertOn( $dbw );
589
590 $revisions[0] = new Revision( [
591 'page' => $page->getId(),
592 // we need the title to determine the page's default content model
593 'title' => $page->getTitle(),
594 'timestamp' => '20120101000000',
595 'user' => $userA->getId(),
596 'text' => 'zero',
597 'content_model' => CONTENT_MODEL_WIKITEXT,
598 'summary' => 'edit zero'
599 ] );
600 $revisions[0]->insertOn( $dbw );
601
602 $revisions[1] = new Revision( [
603 'page' => $page->getId(),
604 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
605 'title' => $page->getTitle(),
606 'timestamp' => '20120101000100',
607 'user' => $userA->getId(),
608 'text' => 'one',
609 'content_model' => CONTENT_MODEL_WIKITEXT,
610 'summary' => 'edit one'
611 ] );
612 $revisions[1]->insertOn( $dbw );
613
614 $revisions[2] = new Revision( [
615 'page' => $page->getId(),
616 'title' => $page->getTitle(),
617 'timestamp' => '20120101000200',
618 'user' => $userB->getId(),
619 'text' => 'two',
620 'content_model' => CONTENT_MODEL_WIKITEXT,
621 'summary' => 'edit two'
622 ] );
623 $revisions[2]->insertOn( $dbw );
624
625 $revisions[3] = new Revision( [
626 'page' => $page->getId(),
627 'title' => $page->getTitle(),
628 'timestamp' => '20120101000300',
629 'user' => $userA->getId(),
630 'text' => 'three',
631 'content_model' => CONTENT_MODEL_WIKITEXT,
632 'summary' => 'edit three'
633 ] );
634 $revisions[3]->insertOn( $dbw );
635
636 $revisions[4] = new Revision( [
637 'page' => $page->getId(),
638 'title' => $page->getTitle(),
639 'timestamp' => '20120101000200',
640 'user' => $userA->getId(),
641 'text' => 'zero',
642 'content_model' => CONTENT_MODEL_WIKITEXT,
643 'summary' => 'edit four'
644 ] );
645 $revisions[4]->insertOn( $dbw );
646
647 // test it ---------------------------------
648 $since = $revisions[$sinceIdx]->getTimestamp();
649
650 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
651
652 $this->assertEquals( $expectedLast, $wasLast );
653 }
654
655 /**
656 * @param string $text
657 * @param string $title
658 * @param string $model
659 * @param string $format
660 *
661 * @return Revision
662 */
663 private function newTestRevision( $text, $title = "Test",
664 $model = CONTENT_MODEL_WIKITEXT, $format = null
665 ) {
666 if ( is_string( $title ) ) {
667 $title = Title::newFromText( $title );
668 }
669
670 $content = ContentHandler::makeContent( $text, $title, $model, $format );
671
672 $rev = new Revision(
673 [
674 'id' => 42,
675 'page' => 23,
676 'title' => $title,
677
678 'content' => $content,
679 'length' => $content->getSize(),
680 'comment' => "testing",
681 'minor_edit' => false,
682
683 'content_format' => $format,
684 ]
685 );
686
687 return $rev;
688 }
689
690 public function provideGetContentModel() {
691 // NOTE: we expect the help namespace to always contain wikitext
692 return [
693 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
694 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
695 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
696 ];
697 }
698
699 /**
700 * @dataProvider provideGetContentModel
701 * @covers Revision::getContentModel
702 */
703 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
704 $rev = $this->newTestRevision( $text, $title, $model, $format );
705
706 $this->assertEquals( $expectedModel, $rev->getContentModel() );
707 }
708
709 public function provideGetContentFormat() {
710 // NOTE: we expect the help namespace to always contain wikitext
711 return [
712 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
713 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
714 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
715 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
716 ];
717 }
718
719 /**
720 * @dataProvider provideGetContentFormat
721 * @covers Revision::getContentFormat
722 */
723 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
724 $rev = $this->newTestRevision( $text, $title, $model, $format );
725
726 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
727 }
728
729 public function provideGetContentHandler() {
730 // NOTE: we expect the help namespace to always contain wikitext
731 return [
732 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
733 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
734 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
735 ];
736 }
737
738 /**
739 * @dataProvider provideGetContentHandler
740 * @covers Revision::getContentHandler
741 */
742 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
743 $rev = $this->newTestRevision( $text, $title, $model, $format );
744
745 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
746 }
747
748 public function provideGetContent() {
749 // NOTE: we expect the help namespace to always contain wikitext
750 return [
751 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
752 [
753 serialize( 'hello world' ),
754 'Hello',
755 DummyContentForTesting::MODEL_ID,
756 null,
757 Revision::FOR_PUBLIC,
758 serialize( 'hello world' )
759 ],
760 [
761 serialize( 'hello world' ),
762 'Dummy:Hello',
763 null,
764 null,
765 Revision::FOR_PUBLIC,
766 serialize( 'hello world' )
767 ],
768 ];
769 }
770
771 /**
772 * @dataProvider provideGetContent
773 * @covers Revision::getContent
774 */
775 public function testGetContent( $text, $title, $model, $format,
776 $audience, $expectedSerialization
777 ) {
778 $rev = $this->newTestRevision( $text, $title, $model, $format );
779 $content = $rev->getContent( $audience );
780
781 $this->assertEquals(
782 $expectedSerialization,
783 is_null( $content ) ? null : $content->serialize( $format )
784 );
785 }
786
787 /**
788 * @covers Revision::getContent
789 */
790 public function testGetContent_failure() {
791 $rev = new Revision( [
792 'page' => $this->testPage->getId(),
793 'content_model' => $this->testPage->getContentModel(),
794 'text_id' => 123456789, // not in the test DB
795 ] );
796
797 $this->assertNull( $rev->getContent(),
798 "getContent() should return null if the revision's text blob could not be loaded." );
799
800 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
801 $this->assertNull( $rev->getContent(),
802 "getContent() should return null if the revision's text blob could not be loaded." );
803 }
804
805 public function provideGetSize() {
806 return [
807 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
808 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
809 ];
810 }
811
812 /**
813 * @covers Revision::getSize
814 * @dataProvider provideGetSize
815 */
816 public function testGetSize( $text, $model, $expected_size ) {
817 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
818 $this->assertEquals( $expected_size, $rev->getSize() );
819 }
820
821 public function provideGetSha1() {
822 return [
823 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
824 [
825 serialize( "hello world." ),
826 DummyContentForTesting::MODEL_ID,
827 Revision::base36Sha1( serialize( "hello world." ) )
828 ],
829 ];
830 }
831
832 /**
833 * @covers Revision::getSha1
834 * @dataProvider provideGetSha1
835 */
836 public function testGetSha1( $text, $model, $expected_hash ) {
837 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
838 $this->assertEquals( $expected_hash, $rev->getSha1() );
839 }
840
841 /**
842 * Tests whether $rev->getContent() returns a clone when needed.
843 *
844 * @covers Revision::getContent
845 */
846 public function testGetContentClone() {
847 $content = new RevisionTestModifyableContent( "foo" );
848
849 $rev = new Revision(
850 [
851 'id' => 42,
852 'page' => 23,
853 'title' => Title::newFromText( "testGetContentClone_dummy" ),
854
855 'content' => $content,
856 'length' => $content->getSize(),
857 'comment' => "testing",
858 'minor_edit' => false,
859 ]
860 );
861
862 /** @var RevisionTestModifyableContent $content */
863 $content = $rev->getContent( Revision::RAW );
864 $content->setText( "bar" );
865
866 /** @var RevisionTestModifyableContent $content2 */
867 $content2 = $rev->getContent( Revision::RAW );
868 // content is mutable, expect clone
869 $this->assertNotSame( $content, $content2, "expected a clone" );
870 // clone should contain the original text
871 $this->assertEquals( "foo", $content2->getText() );
872
873 $content2->setText( "bla bla" );
874 // clones should be independent
875 $this->assertEquals( "bar", $content->getText() );
876 }
877
878 /**
879 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
880 * @covers Revision::getContent
881 */
882 public function testGetContentUncloned() {
883 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
884 $content = $rev->getContent( Revision::RAW );
885 $content2 = $rev->getContent( Revision::RAW );
886
887 // for immutable content like wikitext, this should be the same object
888 $this->assertSame( $content, $content2 );
889 }
890
891 /**
892 * @covers Revision::loadFromId
893 */
894 public function testLoadFromId() {
895 $rev = $this->testPage->getRevision();
896 $this->assertRevEquals(
897 $rev,
898 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
899 );
900 }
901
902 /**
903 * @covers Revision::loadFromPageId
904 */
905 public function testLoadFromPageId() {
906 $this->assertRevEquals(
907 $this->testPage->getRevision(),
908 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
909 );
910 }
911
912 /**
913 * @covers Revision::loadFromPageId
914 */
915 public function testLoadFromPageIdWithLatestRevId() {
916 $this->assertRevEquals(
917 $this->testPage->getRevision(),
918 Revision::loadFromPageId(
919 wfGetDB( DB_MASTER ),
920 $this->testPage->getId(),
921 $this->testPage->getLatest()
922 )
923 );
924 }
925
926 /**
927 * @covers Revision::loadFromPageId
928 */
929 public function testLoadFromPageIdWithNotLatestRevId() {
930 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
931 $this->assertRevEquals(
932 $this->testPage->getRevision()->getPrevious(),
933 Revision::loadFromPageId(
934 wfGetDB( DB_MASTER ),
935 $this->testPage->getId(),
936 $this->testPage->getRevision()->getPrevious()->getId()
937 )
938 );
939 }
940
941 /**
942 * @covers Revision::loadFromTitle
943 */
944 public function testLoadFromTitle() {
945 $this->assertRevEquals(
946 $this->testPage->getRevision(),
947 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
948 );
949 }
950
951 /**
952 * @covers Revision::loadFromTitle
953 */
954 public function testLoadFromTitleWithLatestRevId() {
955 $this->assertRevEquals(
956 $this->testPage->getRevision(),
957 Revision::loadFromTitle(
958 wfGetDB( DB_MASTER ),
959 $this->testPage->getTitle(),
960 $this->testPage->getLatest()
961 )
962 );
963 }
964
965 /**
966 * @covers Revision::loadFromTitle
967 */
968 public function testLoadFromTitleWithNotLatestRevId() {
969 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
970 $this->assertRevEquals(
971 $this->testPage->getRevision()->getPrevious(),
972 Revision::loadFromTitle(
973 wfGetDB( DB_MASTER ),
974 $this->testPage->getTitle(),
975 $this->testPage->getRevision()->getPrevious()->getId()
976 )
977 );
978 }
979
980 /**
981 * @covers Revision::loadFromTimestamp()
982 */
983 public function testLoadFromTimestamp() {
984 $this->assertRevEquals(
985 $this->testPage->getRevision(),
986 Revision::loadFromTimestamp(
987 wfGetDB( DB_MASTER ),
988 $this->testPage->getTitle(),
989 $this->testPage->getRevision()->getTimestamp()
990 )
991 );
992 }
993
994 /**
995 * @covers Revision::getParentLengths
996 */
997 public function testGetParentLengths_noRevIds() {
998 $this->assertSame(
999 [],
1000 Revision::getParentLengths(
1001 wfGetDB( DB_MASTER ),
1002 []
1003 )
1004 );
1005 }
1006
1007 /**
1008 * @covers Revision::getParentLengths
1009 */
1010 public function testGetParentLengths_oneRevId() {
1011 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1012 $textLength = strlen( $text );
1013
1014 $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
1015 $rev[1] = $this->testPage->getLatest();
1016
1017 $this->assertSame(
1018 [ $rev[1] => strval( $textLength ) ],
1019 Revision::getParentLengths(
1020 wfGetDB( DB_MASTER ),
1021 [ $rev[1] ]
1022 )
1023 );
1024 }
1025
1026 /**
1027 * @covers Revision::getParentLengths
1028 */
1029 public function testGetParentLengths_multipleRevIds() {
1030 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1031 $textOneLength = strlen( $textOne );
1032 $textTwo = '831jr091jr092121j09rj1';
1033 $textTwoLength = strlen( $textTwo );
1034
1035 $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
1036 $rev[1] = $this->testPage->getLatest();
1037 $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
1038 $rev[2] = $this->testPage->getLatest();
1039
1040 $this->assertSame(
1041 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1042 Revision::getParentLengths(
1043 wfGetDB( DB_MASTER ),
1044 [ $rev[1], $rev[2] ]
1045 )
1046 );
1047 }
1048
1049 }