Merge "Sanitizer::safeEncodeAttribute(): also encode ]"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionDbTestBase.php
1 <?php
2
3 /**
4 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
5 *
6 * @group Database
7 * @group medium
8 */
9 abstract class RevisionDbTestBase 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 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
71
72 MWNamespace::clearCaches();
73 // Reset namespace cache
74 $wgContLang->resetNamespaces();
75 if ( !$this->testPage ) {
76 /**
77 * We have to create a new page for each subclass as the page creation may result
78 * in different DB fields being filled based on configuration.
79 */
80 $this->testPage = $this->createPage( __CLASS__, __CLASS__ );
81 }
82 }
83
84 protected function tearDown() {
85 global $wgContLang;
86
87 parent::tearDown();
88
89 MWNamespace::clearCaches();
90 // Reset namespace cache
91 $wgContLang->resetNamespaces();
92 }
93
94 abstract protected function getContentHandlerUseDB();
95
96 private function makeRevisionWithProps( $props = null ) {
97 if ( $props === null ) {
98 $props = [];
99 }
100
101 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
102 $props['text'] = 'Lorem Ipsum';
103 }
104
105 if ( !isset( $props['comment'] ) ) {
106 $props['comment'] = 'just a test';
107 }
108
109 if ( !isset( $props['page'] ) ) {
110 $props['page'] = $this->testPage->getId();
111 }
112
113 $rev = new Revision( $props );
114
115 $dbw = wfGetDB( DB_MASTER );
116 $rev->insertOn( $dbw );
117
118 return $rev;
119 }
120
121 /**
122 * @param string $titleString
123 * @param string $text
124 * @param string|null $model
125 *
126 * @return WikiPage
127 */
128 private function createPage( $titleString, $text, $model = null ) {
129 if ( !preg_match( '/:/', $titleString ) &&
130 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
131 ) {
132 $ns = $this->getDefaultWikitextNS();
133 $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
134 }
135
136 $title = Title::newFromText( $titleString );
137 $wikipage = new WikiPage( $title );
138
139 // Delete the article if it already exists
140 if ( $wikipage->exists() ) {
141 $wikipage->doDeleteArticle( "done" );
142 }
143
144 $content = ContentHandler::makeContent( $text, $title, $model );
145 $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
146
147 return $wikipage;
148 }
149
150 private function assertRevEquals( Revision $orig, Revision $rev = null ) {
151 $this->assertNotNull( $rev, 'missing revision' );
152
153 $this->assertEquals( $orig->getId(), $rev->getId() );
154 $this->assertEquals( $orig->getPage(), $rev->getPage() );
155 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
156 $this->assertEquals( $orig->getUser(), $rev->getUser() );
157 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
158 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
159 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
160 }
161
162 /**
163 * @covers Revision::getRecentChange
164 */
165 public function testGetRecentChange() {
166 $rev = $this->testPage->getRevision();
167 $recentChange = $rev->getRecentChange();
168
169 // Make sure various attributes look right / the correct entry has been retrieved.
170 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
171 $this->assertEquals(
172 $rev->getTitle()->getNamespace(),
173 $recentChange->getAttribute( 'rc_namespace' )
174 );
175 $this->assertEquals(
176 $rev->getTitle()->getDBkey(),
177 $recentChange->getAttribute( 'rc_title' )
178 );
179 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
180 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
181 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
182 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
183 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
184 }
185
186 /**
187 * @covers Revision::insertOn
188 */
189 public function testInsertOn_success() {
190 $parentId = $this->testPage->getLatest();
191
192 // If an ExternalStore is set don't use it.
193 $this->setMwGlobals( 'wgDefaultExternalStore', false );
194
195 $rev = new Revision( [
196 'page' => $this->testPage->getId(),
197 'title' => $this->testPage->getTitle(),
198 'text' => 'Revision Text',
199 'comment' => 'Revision comment',
200 ] );
201
202 $revId = $rev->insertOn( wfGetDB( DB_MASTER ) );
203
204 $this->assertInternalType( 'integer', $revId );
205 $this->assertInternalType( 'integer', $rev->getTextId() );
206 $this->assertSame( $revId, $rev->getId() );
207
208 $this->assertSelect(
209 'text',
210 [ 'old_id', 'old_text' ],
211 "old_id = {$rev->getTextId()}",
212 [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
213 );
214 $this->assertSelect(
215 'revision',
216 [
217 'rev_id',
218 'rev_page',
219 'rev_text_id',
220 'rev_user',
221 'rev_minor_edit',
222 'rev_deleted',
223 'rev_len',
224 'rev_parent_id',
225 'rev_sha1',
226 ],
227 "rev_id = {$rev->getId()}",
228 [ [
229 strval( $rev->getId() ),
230 strval( $this->testPage->getId() ),
231 strval( $rev->getTextId() ),
232 '0',
233 '0',
234 '0',
235 '13',
236 strval( $parentId ),
237 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
238 ] ]
239 );
240 }
241
242 /**
243 * @covers Revision::insertOn
244 */
245 public function testInsertOn_exceptionOnNoPage() {
246 // If an ExternalStore is set don't use it.
247 $this->setMwGlobals( 'wgDefaultExternalStore', false );
248 $this->setExpectedException(
249 MWException::class,
250 "Cannot insert revision: page ID must be nonzero"
251 );
252
253 $rev = new Revision( [] );
254
255 $rev->insertOn( wfGetDB( DB_MASTER ) );
256 }
257
258 /**
259 * @covers Revision::newFromTitle
260 */
261 public function testNewFromTitle_withoutId() {
262 $latestRevId = $this->testPage->getLatest();
263
264 $rev = Revision::newFromTitle( $this->testPage->getTitle() );
265
266 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
267 $this->assertEquals( $latestRevId, $rev->getId() );
268 }
269
270 /**
271 * @covers Revision::newFromTitle
272 */
273 public function testNewFromTitle_withId() {
274 $latestRevId = $this->testPage->getLatest();
275
276 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId );
277
278 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
279 $this->assertEquals( $latestRevId, $rev->getId() );
280 }
281
282 /**
283 * @covers Revision::newFromTitle
284 */
285 public function testNewFromTitle_withBadId() {
286 $latestRevId = $this->testPage->getLatest();
287
288 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId + 1 );
289
290 $this->assertNull( $rev );
291 }
292
293 /**
294 * @covers Revision::newFromRow
295 */
296 public function testNewFromRow() {
297 $orig = $this->makeRevisionWithProps();
298
299 $dbr = wfGetDB( DB_REPLICA );
300 $revQuery = Revision::getQueryInfo();
301 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
302 __METHOD__, [], $revQuery['joins'] );
303 $this->assertTrue( is_object( $res ), 'query failed' );
304
305 $row = $res->fetchObject();
306 $res->free();
307
308 $rev = Revision::newFromRow( $row );
309
310 $this->assertRevEquals( $orig, $rev );
311 }
312
313 public function provideNewFromArchiveRow() {
314 yield [
315 function ( $f ) {
316 return $f;
317 },
318 ];
319 yield [
320 function ( $f ) {
321 return $f + [ 'ar_namespace', 'ar_title' ];
322 },
323 ];
324 yield [
325 function ( $f ) {
326 unset( $f['ar_text_id'] );
327 return $f;
328 },
329 ];
330 }
331
332 /**
333 * @dataProvider provideNewFromArchiveRow
334 * @covers Revision::newFromArchiveRow
335 */
336 public function testNewFromArchiveRow( $selectModifier ) {
337 $page = $this->createPage(
338 'RevisionStorageTest_testNewFromArchiveRow',
339 'Lorem Ipsum',
340 CONTENT_MODEL_WIKITEXT
341 );
342 $orig = $page->getRevision();
343 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
344
345 $dbr = wfGetDB( DB_REPLICA );
346 $arQuery = Revision::getArchiveQueryInfo();
347 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
348 $res = $dbr->select(
349 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
350 __METHOD__, [], $arQuery['joins']
351 );
352 $this->assertTrue( is_object( $res ), 'query failed' );
353
354 $row = $res->fetchObject();
355 $res->free();
356
357 $rev = Revision::newFromArchiveRow( $row );
358
359 $this->assertRevEquals( $orig, $rev );
360 }
361
362 /**
363 * @covers Revision::newFromArchiveRow
364 */
365 public function testNewFromArchiveRowOverrides() {
366 $page = $this->createPage(
367 'RevisionStorageTest_testNewFromArchiveRow',
368 'Lorem Ipsum',
369 CONTENT_MODEL_WIKITEXT
370 );
371 $orig = $page->getRevision();
372 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
373
374 $dbr = wfGetDB( DB_REPLICA );
375 $arQuery = Revision::getArchiveQueryInfo();
376 $res = $dbr->select(
377 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
378 __METHOD__, [], $arQuery['joins']
379 );
380 $this->assertTrue( is_object( $res ), 'query failed' );
381
382 $row = $res->fetchObject();
383 $res->free();
384
385 $rev = Revision::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
386
387 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
388 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
389 }
390
391 /**
392 * @covers Revision::newFromId
393 */
394 public function testNewFromId() {
395 $orig = $this->testPage->getRevision();
396 $rev = Revision::newFromId( $orig->getId() );
397 $this->assertRevEquals( $orig, $rev );
398 }
399
400 /**
401 * @covers Revision::newFromPageId
402 */
403 public function testNewFromPageId() {
404 $rev = Revision::newFromPageId( $this->testPage->getId() );
405 $this->assertRevEquals(
406 $this->testPage->getRevision(),
407 $rev
408 );
409 }
410
411 /**
412 * @covers Revision::newFromPageId
413 */
414 public function testNewFromPageIdWithLatestId() {
415 $rev = Revision::newFromPageId(
416 $this->testPage->getId(),
417 $this->testPage->getLatest()
418 );
419 $this->assertRevEquals(
420 $this->testPage->getRevision(),
421 $rev
422 );
423 }
424
425 /**
426 * @covers Revision::newFromPageId
427 */
428 public function testNewFromPageIdWithNotLatestId() {
429 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
430 $rev = Revision::newFromPageId(
431 $this->testPage->getId(),
432 $this->testPage->getRevision()->getPrevious()->getId()
433 );
434 $this->assertRevEquals(
435 $this->testPage->getRevision()->getPrevious(),
436 $rev
437 );
438 }
439
440 /**
441 * @covers Revision::fetchRevision
442 */
443 public function testFetchRevision() {
444 // Hidden process cache assertion below
445 $this->testPage->getRevision()->getId();
446
447 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
448 $id = $this->testPage->getRevision()->getId();
449
450 $res = Revision::fetchRevision( $this->testPage->getTitle() );
451
452 # note: order is unspecified
453 $rows = [];
454 while ( ( $row = $res->fetchObject() ) ) {
455 $rows[$row->rev_id] = $row;
456 }
457
458 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
459 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
460 }
461
462 /**
463 * @covers Revision::getPage
464 */
465 public function testGetPage() {
466 $page = $this->testPage;
467
468 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
469 $rev = Revision::newFromId( $orig->getId() );
470
471 $this->assertEquals( $page->getId(), $rev->getPage() );
472 }
473
474 /**
475 * @covers Revision::isCurrent
476 */
477 public function testIsCurrent() {
478 $rev1 = $this->testPage->getRevision();
479
480 # @todo find out if this should be true
481 # $this->assertTrue( $rev1->isCurrent() );
482
483 $rev1x = Revision::newFromId( $rev1->getId() );
484 $this->assertTrue( $rev1x->isCurrent() );
485
486 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
487 $rev2 = $this->testPage->getRevision();
488
489 # @todo find out if this should be true
490 # $this->assertTrue( $rev2->isCurrent() );
491
492 $rev1x = Revision::newFromId( $rev1->getId() );
493 $this->assertFalse( $rev1x->isCurrent() );
494
495 $rev2x = Revision::newFromId( $rev2->getId() );
496 $this->assertTrue( $rev2x->isCurrent() );
497 }
498
499 /**
500 * @covers Revision::getPrevious
501 */
502 public function testGetPrevious() {
503 $oldestRevision = $this->testPage->getOldestRevision();
504 $latestRevision = $this->testPage->getLatest();
505
506 $this->assertNull( $oldestRevision->getPrevious() );
507
508 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
509 $newRevision = $this->testPage->getRevision();
510
511 $this->assertNotNull( $newRevision->getPrevious() );
512 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
513 }
514
515 /**
516 * @covers Revision::getNext
517 */
518 public function testGetNext() {
519 $rev1 = $this->testPage->getRevision();
520
521 $this->assertNull( $rev1->getNext() );
522
523 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
524 $rev2 = $this->testPage->getRevision();
525
526 $this->assertNotNull( $rev1->getNext() );
527 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
528 }
529
530 /**
531 * @covers Revision::newNullRevision
532 */
533 public function testNewNullRevision() {
534 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
535 $orig = $this->testPage->getRevision();
536
537 $dbw = wfGetDB( DB_MASTER );
538 $rev = Revision::newNullRevision( $dbw, $this->testPage->getId(), 'a null revision', false );
539
540 $this->assertNotEquals( $orig->getId(), $rev->getId(),
541 'new null revision should have a different id from the original revision' );
542 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
543 'new null revision should have the same text id as the original revision' );
544 $this->assertEquals( __METHOD__, $rev->getContent()->getNativeData() );
545 }
546
547 /**
548 * @covers Revision::insertOn
549 */
550 public function testInsertOn() {
551 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
552
553 $orig = $this->makeRevisionWithProps( [
554 'user_text' => $ip
555 ] );
556
557 // Make sure the revision was copied to ip_changes
558 $dbr = wfGetDB( DB_REPLICA );
559 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
560 $row = $res->fetchObject();
561
562 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
563 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp );
564 }
565
566 public static function provideUserWasLastToEdit() {
567 yield 'actually the last edit' => [ 3, true ];
568 yield 'not the current edit, but still by this user' => [ 2, true ];
569 yield 'edit by another user' => [ 1, false ];
570 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
571 }
572
573 /**
574 * @dataProvider provideUserWasLastToEdit
575 */
576 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
577 $userA = User::newFromName( "RevisionStorageTest_userA" );
578 $userB = User::newFromName( "RevisionStorageTest_userB" );
579
580 if ( $userA->getId() === 0 ) {
581 $userA = User::createNew( $userA->getName() );
582 }
583
584 if ( $userB->getId() === 0 ) {
585 $userB = User::createNew( $userB->getName() );
586 }
587
588 $ns = $this->getDefaultWikitextNS();
589
590 $dbw = wfGetDB( DB_MASTER );
591 $revisions = [];
592
593 // create revisions -----------------------------
594 $page = WikiPage::factory( Title::newFromText(
595 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
596 $page->insertOn( $dbw );
597
598 $revisions[0] = new Revision( [
599 'page' => $page->getId(),
600 // we need the title to determine the page's default content model
601 'title' => $page->getTitle(),
602 'timestamp' => '20120101000000',
603 'user' => $userA->getId(),
604 'text' => 'zero',
605 'content_model' => CONTENT_MODEL_WIKITEXT,
606 'summary' => 'edit zero'
607 ] );
608 $revisions[0]->insertOn( $dbw );
609
610 $revisions[1] = new Revision( [
611 'page' => $page->getId(),
612 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
613 'title' => $page->getTitle(),
614 'timestamp' => '20120101000100',
615 'user' => $userA->getId(),
616 'text' => 'one',
617 'content_model' => CONTENT_MODEL_WIKITEXT,
618 'summary' => 'edit one'
619 ] );
620 $revisions[1]->insertOn( $dbw );
621
622 $revisions[2] = new Revision( [
623 'page' => $page->getId(),
624 'title' => $page->getTitle(),
625 'timestamp' => '20120101000200',
626 'user' => $userB->getId(),
627 'text' => 'two',
628 'content_model' => CONTENT_MODEL_WIKITEXT,
629 'summary' => 'edit two'
630 ] );
631 $revisions[2]->insertOn( $dbw );
632
633 $revisions[3] = new Revision( [
634 'page' => $page->getId(),
635 'title' => $page->getTitle(),
636 'timestamp' => '20120101000300',
637 'user' => $userA->getId(),
638 'text' => 'three',
639 'content_model' => CONTENT_MODEL_WIKITEXT,
640 'summary' => 'edit three'
641 ] );
642 $revisions[3]->insertOn( $dbw );
643
644 $revisions[4] = new Revision( [
645 'page' => $page->getId(),
646 'title' => $page->getTitle(),
647 'timestamp' => '20120101000200',
648 'user' => $userA->getId(),
649 'text' => 'zero',
650 'content_model' => CONTENT_MODEL_WIKITEXT,
651 'summary' => 'edit four'
652 ] );
653 $revisions[4]->insertOn( $dbw );
654
655 // test it ---------------------------------
656 $since = $revisions[$sinceIdx]->getTimestamp();
657
658 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
659
660 $this->assertEquals( $expectedLast, $wasLast );
661 }
662
663 /**
664 * @param string $text
665 * @param string $title
666 * @param string $model
667 * @param string $format
668 *
669 * @return Revision
670 */
671 private function newTestRevision( $text, $title = "Test",
672 $model = CONTENT_MODEL_WIKITEXT, $format = null
673 ) {
674 if ( is_string( $title ) ) {
675 $title = Title::newFromText( $title );
676 }
677
678 $content = ContentHandler::makeContent( $text, $title, $model, $format );
679
680 $rev = new Revision(
681 [
682 'id' => 42,
683 'page' => 23,
684 'title' => $title,
685
686 'content' => $content,
687 'length' => $content->getSize(),
688 'comment' => "testing",
689 'minor_edit' => false,
690
691 'content_format' => $format,
692 ]
693 );
694
695 return $rev;
696 }
697
698 public function provideGetContentModel() {
699 // NOTE: we expect the help namespace to always contain wikitext
700 return [
701 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
702 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
703 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
704 ];
705 }
706
707 /**
708 * @dataProvider provideGetContentModel
709 * @covers Revision::getContentModel
710 */
711 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
712 $rev = $this->newTestRevision( $text, $title, $model, $format );
713
714 $this->assertEquals( $expectedModel, $rev->getContentModel() );
715 }
716
717 public function provideGetContentFormat() {
718 // NOTE: we expect the help namespace to always contain wikitext
719 return [
720 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
721 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
722 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
723 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
724 ];
725 }
726
727 /**
728 * @dataProvider provideGetContentFormat
729 * @covers Revision::getContentFormat
730 */
731 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
732 $rev = $this->newTestRevision( $text, $title, $model, $format );
733
734 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
735 }
736
737 public function provideGetContentHandler() {
738 // NOTE: we expect the help namespace to always contain wikitext
739 return [
740 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
741 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
742 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
743 ];
744 }
745
746 /**
747 * @dataProvider provideGetContentHandler
748 * @covers Revision::getContentHandler
749 */
750 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
751 $rev = $this->newTestRevision( $text, $title, $model, $format );
752
753 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
754 }
755
756 public function provideGetContent() {
757 // NOTE: we expect the help namespace to always contain wikitext
758 return [
759 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
760 [
761 serialize( 'hello world' ),
762 'Hello',
763 DummyContentForTesting::MODEL_ID,
764 null,
765 Revision::FOR_PUBLIC,
766 serialize( 'hello world' )
767 ],
768 [
769 serialize( 'hello world' ),
770 'Dummy:Hello',
771 null,
772 null,
773 Revision::FOR_PUBLIC,
774 serialize( 'hello world' )
775 ],
776 ];
777 }
778
779 /**
780 * @dataProvider provideGetContent
781 * @covers Revision::getContent
782 */
783 public function testGetContent( $text, $title, $model, $format,
784 $audience, $expectedSerialization
785 ) {
786 $rev = $this->newTestRevision( $text, $title, $model, $format );
787 $content = $rev->getContent( $audience );
788
789 $this->assertEquals(
790 $expectedSerialization,
791 is_null( $content ) ? null : $content->serialize( $format )
792 );
793 }
794
795 /**
796 * @covers Revision::getContent
797 */
798 public function testGetContent_failure() {
799 $rev = new Revision( [
800 'page' => $this->testPage->getId(),
801 'content_model' => $this->testPage->getContentModel(),
802 'text_id' => 123456789, // not in the test DB
803 ] );
804
805 $this->assertNull( $rev->getContent(),
806 "getContent() should return null if the revision's text blob could not be loaded." );
807
808 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
809 $this->assertNull( $rev->getContent(),
810 "getContent() should return null if the revision's text blob could not be loaded." );
811 }
812
813 public function provideGetSize() {
814 return [
815 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
816 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
817 ];
818 }
819
820 /**
821 * @covers Revision::getSize
822 * @dataProvider provideGetSize
823 */
824 public function testGetSize( $text, $model, $expected_size ) {
825 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
826 $this->assertEquals( $expected_size, $rev->getSize() );
827 }
828
829 public function provideGetSha1() {
830 return [
831 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
832 [
833 serialize( "hello world." ),
834 DummyContentForTesting::MODEL_ID,
835 Revision::base36Sha1( serialize( "hello world." ) )
836 ],
837 ];
838 }
839
840 /**
841 * @covers Revision::getSha1
842 * @dataProvider provideGetSha1
843 */
844 public function testGetSha1( $text, $model, $expected_hash ) {
845 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
846 $this->assertEquals( $expected_hash, $rev->getSha1() );
847 }
848
849 /**
850 * Tests whether $rev->getContent() returns a clone when needed.
851 *
852 * @covers Revision::getContent
853 */
854 public function testGetContentClone() {
855 $content = new RevisionTestModifyableContent( "foo" );
856
857 $rev = new Revision(
858 [
859 'id' => 42,
860 'page' => 23,
861 'title' => Title::newFromText( "testGetContentClone_dummy" ),
862
863 'content' => $content,
864 'length' => $content->getSize(),
865 'comment' => "testing",
866 'minor_edit' => false,
867 ]
868 );
869
870 /** @var RevisionTestModifyableContent $content */
871 $content = $rev->getContent( Revision::RAW );
872 $content->setText( "bar" );
873
874 /** @var RevisionTestModifyableContent $content2 */
875 $content2 = $rev->getContent( Revision::RAW );
876 // content is mutable, expect clone
877 $this->assertNotSame( $content, $content2, "expected a clone" );
878 // clone should contain the original text
879 $this->assertEquals( "foo", $content2->getText() );
880
881 $content2->setText( "bla bla" );
882 // clones should be independent
883 $this->assertEquals( "bar", $content->getText() );
884 }
885
886 /**
887 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
888 * @covers Revision::getContent
889 */
890 public function testGetContentUncloned() {
891 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
892 $content = $rev->getContent( Revision::RAW );
893 $content2 = $rev->getContent( Revision::RAW );
894
895 // for immutable content like wikitext, this should be the same object
896 $this->assertSame( $content, $content2 );
897 }
898
899 /**
900 * @covers Revision::loadFromId
901 */
902 public function testLoadFromId() {
903 $rev = $this->testPage->getRevision();
904 $this->assertRevEquals(
905 $rev,
906 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
907 );
908 }
909
910 /**
911 * @covers Revision::loadFromPageId
912 */
913 public function testLoadFromPageId() {
914 $this->assertRevEquals(
915 $this->testPage->getRevision(),
916 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
917 );
918 }
919
920 /**
921 * @covers Revision::loadFromPageId
922 */
923 public function testLoadFromPageIdWithLatestRevId() {
924 $this->assertRevEquals(
925 $this->testPage->getRevision(),
926 Revision::loadFromPageId(
927 wfGetDB( DB_MASTER ),
928 $this->testPage->getId(),
929 $this->testPage->getLatest()
930 )
931 );
932 }
933
934 /**
935 * @covers Revision::loadFromPageId
936 */
937 public function testLoadFromPageIdWithNotLatestRevId() {
938 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
939 $this->assertRevEquals(
940 $this->testPage->getRevision()->getPrevious(),
941 Revision::loadFromPageId(
942 wfGetDB( DB_MASTER ),
943 $this->testPage->getId(),
944 $this->testPage->getRevision()->getPrevious()->getId()
945 )
946 );
947 }
948
949 /**
950 * @covers Revision::loadFromTitle
951 */
952 public function testLoadFromTitle() {
953 $this->assertRevEquals(
954 $this->testPage->getRevision(),
955 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
956 );
957 }
958
959 /**
960 * @covers Revision::loadFromTitle
961 */
962 public function testLoadFromTitleWithLatestRevId() {
963 $this->assertRevEquals(
964 $this->testPage->getRevision(),
965 Revision::loadFromTitle(
966 wfGetDB( DB_MASTER ),
967 $this->testPage->getTitle(),
968 $this->testPage->getLatest()
969 )
970 );
971 }
972
973 /**
974 * @covers Revision::loadFromTitle
975 */
976 public function testLoadFromTitleWithNotLatestRevId() {
977 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
978 $this->assertRevEquals(
979 $this->testPage->getRevision()->getPrevious(),
980 Revision::loadFromTitle(
981 wfGetDB( DB_MASTER ),
982 $this->testPage->getTitle(),
983 $this->testPage->getRevision()->getPrevious()->getId()
984 )
985 );
986 }
987
988 /**
989 * @covers Revision::loadFromTimestamp()
990 */
991 public function testLoadFromTimestamp() {
992 $this->assertRevEquals(
993 $this->testPage->getRevision(),
994 Revision::loadFromTimestamp(
995 wfGetDB( DB_MASTER ),
996 $this->testPage->getTitle(),
997 $this->testPage->getRevision()->getTimestamp()
998 )
999 );
1000 }
1001
1002 /**
1003 * @covers Revision::getParentLengths
1004 */
1005 public function testGetParentLengths_noRevIds() {
1006 $this->assertSame(
1007 [],
1008 Revision::getParentLengths(
1009 wfGetDB( DB_MASTER ),
1010 []
1011 )
1012 );
1013 }
1014
1015 /**
1016 * @covers Revision::getParentLengths
1017 */
1018 public function testGetParentLengths_oneRevId() {
1019 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1020 $textLength = strlen( $text );
1021
1022 $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
1023 $rev[1] = $this->testPage->getLatest();
1024
1025 $this->assertSame(
1026 [ $rev[1] => strval( $textLength ) ],
1027 Revision::getParentLengths(
1028 wfGetDB( DB_MASTER ),
1029 [ $rev[1] ]
1030 )
1031 );
1032 }
1033
1034 /**
1035 * @covers Revision::getParentLengths
1036 */
1037 public function testGetParentLengths_multipleRevIds() {
1038 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1039 $textOneLength = strlen( $textOne );
1040 $textTwo = '831jr091jr092121j09rj1';
1041 $textTwoLength = strlen( $textTwo );
1042
1043 $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
1044 $rev[1] = $this->testPage->getLatest();
1045 $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
1046 $rev[2] = $this->testPage->getLatest();
1047
1048 $this->assertSame(
1049 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1050 Revision::getParentLengths(
1051 wfGetDB( DB_MASTER ),
1052 [ $rev[1], $rev[2] ]
1053 )
1054 );
1055 }
1056
1057 /**
1058 * @covers Revision::getTitle
1059 */
1060 public function testGetTitle_fromExistingRevision() {
1061 $this->assertTrue(
1062 $this->testPage->getTitle()->equals(
1063 $this->testPage->getRevision()->getTitle()
1064 )
1065 );
1066 }
1067
1068 /**
1069 * @covers Revision::getTitle
1070 */
1071 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1072 $rev = new Revision( [ 'id' => $this->testPage->getLatest() ] );
1073 $this->assertTrue(
1074 $this->testPage->getTitle()->equals(
1075 $rev->getTitle()
1076 )
1077 );
1078 }
1079
1080 /**
1081 * @covers Revision::getTitle
1082 */
1083 public function testGetTitle_forBadRevision() {
1084 $rev = new Revision( [] );
1085 $this->assertNull( $rev->getTitle() );
1086 }
1087
1088 /**
1089 * @covers Revision::isMinor
1090 */
1091 public function testIsMinor_true() {
1092 // Use a sysop to ensure we can mark edits as minor
1093 $sysop = $this->getTestSysop()->getUser();
1094
1095 $this->testPage->doEditContent(
1096 new WikitextContent( __METHOD__ ),
1097 __METHOD__,
1098 EDIT_MINOR,
1099 false,
1100 $sysop
1101 );
1102 $rev = $this->testPage->getRevision();
1103
1104 $this->assertSame( true, $rev->isMinor() );
1105 }
1106
1107 /**
1108 * @covers Revision::isMinor
1109 */
1110 public function testIsMinor_false() {
1111 $this->testPage->doEditContent(
1112 new WikitextContent( __METHOD__ ),
1113 __METHOD__,
1114 0
1115 );
1116 $rev = $this->testPage->getRevision();
1117
1118 $this->assertSame( false, $rev->isMinor() );
1119 }
1120
1121 /**
1122 * @covers Revision::getTimestamp
1123 */
1124 public function testGetTimestamp() {
1125 $testTimestamp = wfTimestampNow();
1126
1127 $this->testPage->doEditContent(
1128 new WikitextContent( __METHOD__ ),
1129 __METHOD__
1130 );
1131 $rev = $this->testPage->getRevision();
1132
1133 $this->assertInternalType( 'string', $rev->getTimestamp() );
1134 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1135 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1136 }
1137
1138 /**
1139 * @covers Revision::getUser
1140 * @covers Revision::getUserText
1141 */
1142 public function testGetUserAndText() {
1143 $sysop = $this->getTestSysop()->getUser();
1144
1145 $this->testPage->doEditContent(
1146 new WikitextContent( __METHOD__ ),
1147 __METHOD__,
1148 0,
1149 false,
1150 $sysop
1151 );
1152 $rev = $this->testPage->getRevision();
1153
1154 $this->assertSame( $sysop->getId(), $rev->getUser() );
1155 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1156 }
1157
1158 /**
1159 * @covers Revision::isDeleted
1160 */
1161 public function testIsDeleted_nothingDeleted() {
1162 $rev = $this->testPage->getRevision();
1163
1164 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
1165 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_COMMENT ) );
1166 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
1167 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_USER ) );
1168 }
1169
1170 /**
1171 * @covers Revision::getVisibility
1172 */
1173 public function testGetVisibility_nothingDeleted() {
1174 $rev = $this->testPage->getRevision();
1175
1176 $this->assertSame( 0, $rev->getVisibility() );
1177 }
1178
1179 /**
1180 * @covers Revision::getComment
1181 */
1182 public function testGetComment_notDeleted() {
1183 $expectedSummary = 'goatlicious summary';
1184
1185 $this->testPage->doEditContent(
1186 new WikitextContent( __METHOD__ ),
1187 $expectedSummary
1188 );
1189 $rev = $this->testPage->getRevision();
1190
1191 $this->assertSame( $expectedSummary, $rev->getComment() );
1192 }
1193
1194 /**
1195 * @covers Revision::isUnpatrolled
1196 */
1197 public function testIsUnpatrolled_returnsRecentChangesId() {
1198 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1199 $rev = $this->testPage->getRevision();
1200
1201 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1202 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1203 }
1204
1205 /**
1206 * @covers Revision::isUnpatrolled
1207 */
1208 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1209 // This assumes that sysops are auto patrolled
1210 $sysop = $this->getTestSysop()->getUser();
1211 $this->testPage->doEditContent(
1212 new WikitextContent( __METHOD__ ),
1213 __METHOD__,
1214 0,
1215 false,
1216 $sysop
1217 );
1218 $rev = $this->testPage->getRevision();
1219
1220 $this->assertSame( 0, $rev->isUnpatrolled() );
1221 }
1222
1223 /**
1224 * This is a simple blanket test for all simple content getters and is methods to provide some
1225 * coverage before the split of Revision into multiple classes for MCR work.
1226 * @covers Revision::getContent
1227 * @covers Revision::getSerializedData
1228 * @covers Revision::getContentModel
1229 * @covers Revision::getContentFormat
1230 * @covers Revision::getContentHandler
1231 */
1232 public function testSimpleContentGetters() {
1233 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1234 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1235
1236 $this->testPage->doEditContent(
1237 new WikitextContent( $expectedText ),
1238 $expectedSummary
1239 );
1240 $rev = $this->testPage->getRevision();
1241
1242 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1243 $this->assertSame( $expectedText, $rev->getSerializedData() );
1244 $this->assertSame( $this->testPage->getContentModel(), $rev->getContentModel() );
1245 $this->assertSame( $this->testPage->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1246 $this->assertSame( $this->testPage->getContentHandler(), $rev->getContentHandler() );
1247 }
1248
1249 /**
1250 * @covers Revision::newKnownCurrent
1251 */
1252 public function testNewKnownCurrent() {
1253 // Setup the services
1254 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1255 $this->setService( 'MainWANObjectCache', $cache );
1256 $db = wfGetDB( DB_MASTER );
1257
1258 // Get a fresh revision to use during testing
1259 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1260 $rev = $this->testPage->getRevision();
1261
1262 // Clear any previous cache for the revision during creation
1263 $key = $cache->makeGlobalKey( 'revision', $db->getDomainID(), $rev->getPage(), $rev->getId() );
1264 $cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1265 $this->assertFalse( $cache->get( $key ) );
1266
1267 // Get the new revision and make sure it is in the cache and correct
1268 $newRev = Revision::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1269 $this->assertRevEquals( $rev, $newRev );
1270 $this->assertRevEquals( $rev, $cache->get( $key ) );
1271 }
1272
1273 public function provideUserCanBitfield() {
1274 yield [ 0, 0, [], null, true ];
1275 // Bitfields match, user has no permissions
1276 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], null, false ];
1277 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], null, false ];
1278 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], null, false ];
1279 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], null, false ];
1280 // Bitfields match, user (admin) does have permissions
1281 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], null, true ];
1282 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], null, true ];
1283 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], null, true ];
1284 // Bitfields match, user (admin) does not have permissions
1285 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], null, false ];
1286 // Bitfields match, user (oversight) does have permissions
1287 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], null, true ];
1288 // Check permissions using the title
1289 yield [
1290 Revision::DELETED_TEXT,
1291 Revision::DELETED_TEXT,
1292 [ 'sysop' ],
1293 Title::newFromText( __METHOD__ ),
1294 true,
1295 ];
1296 yield [
1297 Revision::DELETED_TEXT,
1298 Revision::DELETED_TEXT,
1299 [],
1300 Title::newFromText( __METHOD__ ),
1301 false,
1302 ];
1303 }
1304
1305 /**
1306 * @dataProvider provideUserCanBitfield
1307 * @covers Revision::userCanBitfield
1308 */
1309 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1310 $this->setMwGlobals(
1311 'wgGroupPermissions',
1312 [
1313 'sysop' => [
1314 'deletedtext' => true,
1315 'deletedhistory' => true,
1316 ],
1317 'oversight' => [
1318 'viewsuppressed' => true,
1319 'suppressrevision' => true,
1320 ],
1321 ]
1322 );
1323 $user = $this->getTestUser( $userGroups )->getUser();
1324
1325 $this->assertSame(
1326 $expected,
1327 Revision::userCanBitfield( $bitField, $field, $user, $title )
1328 );
1329
1330 // Fallback to $wgUser
1331 $this->setMwGlobals(
1332 'wgUser',
1333 $user
1334 );
1335 $this->assertSame(
1336 $expected,
1337 Revision::userCanBitfield( $bitField, $field, null, $title )
1338 );
1339 }
1340
1341 public function provideUserCan() {
1342 yield [ 0, 0, [], true ];
1343 // Bitfields match, user has no permissions
1344 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], false ];
1345 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], false ];
1346 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], false ];
1347 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], false ];
1348 // Bitfields match, user (admin) does have permissions
1349 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], true ];
1350 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], true ];
1351 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], true ];
1352 // Bitfields match, user (admin) does not have permissions
1353 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], false ];
1354 // Bitfields match, user (oversight) does have permissions
1355 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], true ];
1356 }
1357
1358 /**
1359 * @dataProvider provideUserCan
1360 * @covers Revision::userCan
1361 */
1362 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1363 $this->setMwGlobals(
1364 'wgGroupPermissions',
1365 [
1366 'sysop' => [
1367 'deletedtext' => true,
1368 'deletedhistory' => true,
1369 ],
1370 'oversight' => [
1371 'viewsuppressed' => true,
1372 'suppressrevision' => true,
1373 ],
1374 ]
1375 );
1376 $user = $this->getTestUser( $userGroups )->getUser();
1377 $revision = new Revision( [ 'deleted' => $bitField ] );
1378
1379 $this->assertSame(
1380 $expected,
1381 $revision->userCan( $field, $user )
1382 );
1383 }
1384
1385 }