Merge "Linker: Use Parser::guessSectionNameFromStrippedText()"
[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(
564 $orig->getTimestamp(),
565 wfTimestamp( TS_MW, $row->ipc_rev_timestamp )
566 );
567 }
568
569 public static function provideUserWasLastToEdit() {
570 yield 'actually the last edit' => [ 3, true ];
571 yield 'not the current edit, but still by this user' => [ 2, true ];
572 yield 'edit by another user' => [ 1, false ];
573 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
574 }
575
576 /**
577 * @dataProvider provideUserWasLastToEdit
578 */
579 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
580 $userA = User::newFromName( "RevisionStorageTest_userA" );
581 $userB = User::newFromName( "RevisionStorageTest_userB" );
582
583 if ( $userA->getId() === 0 ) {
584 $userA = User::createNew( $userA->getName() );
585 }
586
587 if ( $userB->getId() === 0 ) {
588 $userB = User::createNew( $userB->getName() );
589 }
590
591 $ns = $this->getDefaultWikitextNS();
592
593 $dbw = wfGetDB( DB_MASTER );
594 $revisions = [];
595
596 // create revisions -----------------------------
597 $page = WikiPage::factory( Title::newFromText(
598 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
599 $page->insertOn( $dbw );
600
601 $revisions[0] = new Revision( [
602 'page' => $page->getId(),
603 // we need the title to determine the page's default content model
604 'title' => $page->getTitle(),
605 'timestamp' => '20120101000000',
606 'user' => $userA->getId(),
607 'text' => 'zero',
608 'content_model' => CONTENT_MODEL_WIKITEXT,
609 'summary' => 'edit zero'
610 ] );
611 $revisions[0]->insertOn( $dbw );
612
613 $revisions[1] = new Revision( [
614 'page' => $page->getId(),
615 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
616 'title' => $page->getTitle(),
617 'timestamp' => '20120101000100',
618 'user' => $userA->getId(),
619 'text' => 'one',
620 'content_model' => CONTENT_MODEL_WIKITEXT,
621 'summary' => 'edit one'
622 ] );
623 $revisions[1]->insertOn( $dbw );
624
625 $revisions[2] = new Revision( [
626 'page' => $page->getId(),
627 'title' => $page->getTitle(),
628 'timestamp' => '20120101000200',
629 'user' => $userB->getId(),
630 'text' => 'two',
631 'content_model' => CONTENT_MODEL_WIKITEXT,
632 'summary' => 'edit two'
633 ] );
634 $revisions[2]->insertOn( $dbw );
635
636 $revisions[3] = new Revision( [
637 'page' => $page->getId(),
638 'title' => $page->getTitle(),
639 'timestamp' => '20120101000300',
640 'user' => $userA->getId(),
641 'text' => 'three',
642 'content_model' => CONTENT_MODEL_WIKITEXT,
643 'summary' => 'edit three'
644 ] );
645 $revisions[3]->insertOn( $dbw );
646
647 $revisions[4] = new Revision( [
648 'page' => $page->getId(),
649 'title' => $page->getTitle(),
650 'timestamp' => '20120101000200',
651 'user' => $userA->getId(),
652 'text' => 'zero',
653 'content_model' => CONTENT_MODEL_WIKITEXT,
654 'summary' => 'edit four'
655 ] );
656 $revisions[4]->insertOn( $dbw );
657
658 // test it ---------------------------------
659 $since = $revisions[$sinceIdx]->getTimestamp();
660
661 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
662
663 $this->assertEquals( $expectedLast, $wasLast );
664 }
665
666 /**
667 * @param string $text
668 * @param string $title
669 * @param string $model
670 * @param string $format
671 *
672 * @return Revision
673 */
674 private function newTestRevision( $text, $title = "Test",
675 $model = CONTENT_MODEL_WIKITEXT, $format = null
676 ) {
677 if ( is_string( $title ) ) {
678 $title = Title::newFromText( $title );
679 }
680
681 $content = ContentHandler::makeContent( $text, $title, $model, $format );
682
683 $rev = new Revision(
684 [
685 'id' => 42,
686 'page' => 23,
687 'title' => $title,
688
689 'content' => $content,
690 'length' => $content->getSize(),
691 'comment' => "testing",
692 'minor_edit' => false,
693
694 'content_format' => $format,
695 ]
696 );
697
698 return $rev;
699 }
700
701 public function provideGetContentModel() {
702 // NOTE: we expect the help namespace to always contain wikitext
703 return [
704 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
705 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
706 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
707 ];
708 }
709
710 /**
711 * @dataProvider provideGetContentModel
712 * @covers Revision::getContentModel
713 */
714 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
715 $rev = $this->newTestRevision( $text, $title, $model, $format );
716
717 $this->assertEquals( $expectedModel, $rev->getContentModel() );
718 }
719
720 public function provideGetContentFormat() {
721 // NOTE: we expect the help namespace to always contain wikitext
722 return [
723 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
724 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
725 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
726 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
727 ];
728 }
729
730 /**
731 * @dataProvider provideGetContentFormat
732 * @covers Revision::getContentFormat
733 */
734 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
735 $rev = $this->newTestRevision( $text, $title, $model, $format );
736
737 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
738 }
739
740 public function provideGetContentHandler() {
741 // NOTE: we expect the help namespace to always contain wikitext
742 return [
743 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
744 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
745 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
746 ];
747 }
748
749 /**
750 * @dataProvider provideGetContentHandler
751 * @covers Revision::getContentHandler
752 */
753 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
754 $rev = $this->newTestRevision( $text, $title, $model, $format );
755
756 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
757 }
758
759 public function provideGetContent() {
760 // NOTE: we expect the help namespace to always contain wikitext
761 return [
762 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
763 [
764 serialize( 'hello world' ),
765 'Hello',
766 DummyContentForTesting::MODEL_ID,
767 null,
768 Revision::FOR_PUBLIC,
769 serialize( 'hello world' )
770 ],
771 [
772 serialize( 'hello world' ),
773 'Dummy:Hello',
774 null,
775 null,
776 Revision::FOR_PUBLIC,
777 serialize( 'hello world' )
778 ],
779 ];
780 }
781
782 /**
783 * @dataProvider provideGetContent
784 * @covers Revision::getContent
785 */
786 public function testGetContent( $text, $title, $model, $format,
787 $audience, $expectedSerialization
788 ) {
789 $rev = $this->newTestRevision( $text, $title, $model, $format );
790 $content = $rev->getContent( $audience );
791
792 $this->assertEquals(
793 $expectedSerialization,
794 is_null( $content ) ? null : $content->serialize( $format )
795 );
796 }
797
798 /**
799 * @covers Revision::getContent
800 */
801 public function testGetContent_failure() {
802 $rev = new Revision( [
803 'page' => $this->testPage->getId(),
804 'content_model' => $this->testPage->getContentModel(),
805 'text_id' => 123456789, // not in the test DB
806 ] );
807
808 $this->assertNull( $rev->getContent(),
809 "getContent() should return null if the revision's text blob could not be loaded." );
810
811 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
812 $this->assertNull( $rev->getContent(),
813 "getContent() should return null if the revision's text blob could not be loaded." );
814 }
815
816 public function provideGetSize() {
817 return [
818 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
819 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
820 ];
821 }
822
823 /**
824 * @covers Revision::getSize
825 * @dataProvider provideGetSize
826 */
827 public function testGetSize( $text, $model, $expected_size ) {
828 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
829 $this->assertEquals( $expected_size, $rev->getSize() );
830 }
831
832 public function provideGetSha1() {
833 return [
834 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
835 [
836 serialize( "hello world." ),
837 DummyContentForTesting::MODEL_ID,
838 Revision::base36Sha1( serialize( "hello world." ) )
839 ],
840 ];
841 }
842
843 /**
844 * @covers Revision::getSha1
845 * @dataProvider provideGetSha1
846 */
847 public function testGetSha1( $text, $model, $expected_hash ) {
848 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
849 $this->assertEquals( $expected_hash, $rev->getSha1() );
850 }
851
852 /**
853 * Tests whether $rev->getContent() returns a clone when needed.
854 *
855 * @covers Revision::getContent
856 */
857 public function testGetContentClone() {
858 $content = new RevisionTestModifyableContent( "foo" );
859
860 $rev = new Revision(
861 [
862 'id' => 42,
863 'page' => 23,
864 'title' => Title::newFromText( "testGetContentClone_dummy" ),
865
866 'content' => $content,
867 'length' => $content->getSize(),
868 'comment' => "testing",
869 'minor_edit' => false,
870 ]
871 );
872
873 /** @var RevisionTestModifyableContent $content */
874 $content = $rev->getContent( Revision::RAW );
875 $content->setText( "bar" );
876
877 /** @var RevisionTestModifyableContent $content2 */
878 $content2 = $rev->getContent( Revision::RAW );
879 // content is mutable, expect clone
880 $this->assertNotSame( $content, $content2, "expected a clone" );
881 // clone should contain the original text
882 $this->assertEquals( "foo", $content2->getText() );
883
884 $content2->setText( "bla bla" );
885 // clones should be independent
886 $this->assertEquals( "bar", $content->getText() );
887 }
888
889 /**
890 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
891 * @covers Revision::getContent
892 */
893 public function testGetContentUncloned() {
894 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
895 $content = $rev->getContent( Revision::RAW );
896 $content2 = $rev->getContent( Revision::RAW );
897
898 // for immutable content like wikitext, this should be the same object
899 $this->assertSame( $content, $content2 );
900 }
901
902 /**
903 * @covers Revision::loadFromId
904 */
905 public function testLoadFromId() {
906 $rev = $this->testPage->getRevision();
907 $this->assertRevEquals(
908 $rev,
909 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
910 );
911 }
912
913 /**
914 * @covers Revision::loadFromPageId
915 */
916 public function testLoadFromPageId() {
917 $this->assertRevEquals(
918 $this->testPage->getRevision(),
919 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
920 );
921 }
922
923 /**
924 * @covers Revision::loadFromPageId
925 */
926 public function testLoadFromPageIdWithLatestRevId() {
927 $this->assertRevEquals(
928 $this->testPage->getRevision(),
929 Revision::loadFromPageId(
930 wfGetDB( DB_MASTER ),
931 $this->testPage->getId(),
932 $this->testPage->getLatest()
933 )
934 );
935 }
936
937 /**
938 * @covers Revision::loadFromPageId
939 */
940 public function testLoadFromPageIdWithNotLatestRevId() {
941 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
942 $this->assertRevEquals(
943 $this->testPage->getRevision()->getPrevious(),
944 Revision::loadFromPageId(
945 wfGetDB( DB_MASTER ),
946 $this->testPage->getId(),
947 $this->testPage->getRevision()->getPrevious()->getId()
948 )
949 );
950 }
951
952 /**
953 * @covers Revision::loadFromTitle
954 */
955 public function testLoadFromTitle() {
956 $this->assertRevEquals(
957 $this->testPage->getRevision(),
958 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
959 );
960 }
961
962 /**
963 * @covers Revision::loadFromTitle
964 */
965 public function testLoadFromTitleWithLatestRevId() {
966 $this->assertRevEquals(
967 $this->testPage->getRevision(),
968 Revision::loadFromTitle(
969 wfGetDB( DB_MASTER ),
970 $this->testPage->getTitle(),
971 $this->testPage->getLatest()
972 )
973 );
974 }
975
976 /**
977 * @covers Revision::loadFromTitle
978 */
979 public function testLoadFromTitleWithNotLatestRevId() {
980 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
981 $this->assertRevEquals(
982 $this->testPage->getRevision()->getPrevious(),
983 Revision::loadFromTitle(
984 wfGetDB( DB_MASTER ),
985 $this->testPage->getTitle(),
986 $this->testPage->getRevision()->getPrevious()->getId()
987 )
988 );
989 }
990
991 /**
992 * @covers Revision::loadFromTimestamp()
993 */
994 public function testLoadFromTimestamp() {
995 $this->assertRevEquals(
996 $this->testPage->getRevision(),
997 Revision::loadFromTimestamp(
998 wfGetDB( DB_MASTER ),
999 $this->testPage->getTitle(),
1000 $this->testPage->getRevision()->getTimestamp()
1001 )
1002 );
1003 }
1004
1005 /**
1006 * @covers Revision::getParentLengths
1007 */
1008 public function testGetParentLengths_noRevIds() {
1009 $this->assertSame(
1010 [],
1011 Revision::getParentLengths(
1012 wfGetDB( DB_MASTER ),
1013 []
1014 )
1015 );
1016 }
1017
1018 /**
1019 * @covers Revision::getParentLengths
1020 */
1021 public function testGetParentLengths_oneRevId() {
1022 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1023 $textLength = strlen( $text );
1024
1025 $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
1026 $rev[1] = $this->testPage->getLatest();
1027
1028 $this->assertSame(
1029 [ $rev[1] => strval( $textLength ) ],
1030 Revision::getParentLengths(
1031 wfGetDB( DB_MASTER ),
1032 [ $rev[1] ]
1033 )
1034 );
1035 }
1036
1037 /**
1038 * @covers Revision::getParentLengths
1039 */
1040 public function testGetParentLengths_multipleRevIds() {
1041 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1042 $textOneLength = strlen( $textOne );
1043 $textTwo = '831jr091jr092121j09rj1';
1044 $textTwoLength = strlen( $textTwo );
1045
1046 $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
1047 $rev[1] = $this->testPage->getLatest();
1048 $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
1049 $rev[2] = $this->testPage->getLatest();
1050
1051 $this->assertSame(
1052 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1053 Revision::getParentLengths(
1054 wfGetDB( DB_MASTER ),
1055 [ $rev[1], $rev[2] ]
1056 )
1057 );
1058 }
1059
1060 /**
1061 * @covers Revision::getTitle
1062 */
1063 public function testGetTitle_fromExistingRevision() {
1064 $this->assertTrue(
1065 $this->testPage->getTitle()->equals(
1066 $this->testPage->getRevision()->getTitle()
1067 )
1068 );
1069 }
1070
1071 /**
1072 * @covers Revision::getTitle
1073 */
1074 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1075 $rev = new Revision( [ 'id' => $this->testPage->getLatest() ] );
1076 $this->assertTrue(
1077 $this->testPage->getTitle()->equals(
1078 $rev->getTitle()
1079 )
1080 );
1081 }
1082
1083 /**
1084 * @covers Revision::getTitle
1085 */
1086 public function testGetTitle_forBadRevision() {
1087 $rev = new Revision( [] );
1088 $this->assertNull( $rev->getTitle() );
1089 }
1090
1091 /**
1092 * @covers Revision::isMinor
1093 */
1094 public function testIsMinor_true() {
1095 // Use a sysop to ensure we can mark edits as minor
1096 $sysop = $this->getTestSysop()->getUser();
1097
1098 $this->testPage->doEditContent(
1099 new WikitextContent( __METHOD__ ),
1100 __METHOD__,
1101 EDIT_MINOR,
1102 false,
1103 $sysop
1104 );
1105 $rev = $this->testPage->getRevision();
1106
1107 $this->assertSame( true, $rev->isMinor() );
1108 }
1109
1110 /**
1111 * @covers Revision::isMinor
1112 */
1113 public function testIsMinor_false() {
1114 $this->testPage->doEditContent(
1115 new WikitextContent( __METHOD__ ),
1116 __METHOD__,
1117 0
1118 );
1119 $rev = $this->testPage->getRevision();
1120
1121 $this->assertSame( false, $rev->isMinor() );
1122 }
1123
1124 /**
1125 * @covers Revision::getTimestamp
1126 */
1127 public function testGetTimestamp() {
1128 $testTimestamp = wfTimestampNow();
1129
1130 $this->testPage->doEditContent(
1131 new WikitextContent( __METHOD__ ),
1132 __METHOD__
1133 );
1134 $rev = $this->testPage->getRevision();
1135
1136 $this->assertInternalType( 'string', $rev->getTimestamp() );
1137 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1138 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1139 }
1140
1141 /**
1142 * @covers Revision::getUser
1143 * @covers Revision::getUserText
1144 */
1145 public function testGetUserAndText() {
1146 $sysop = $this->getTestSysop()->getUser();
1147
1148 $this->testPage->doEditContent(
1149 new WikitextContent( __METHOD__ ),
1150 __METHOD__,
1151 0,
1152 false,
1153 $sysop
1154 );
1155 $rev = $this->testPage->getRevision();
1156
1157 $this->assertSame( $sysop->getId(), $rev->getUser() );
1158 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1159 }
1160
1161 /**
1162 * @covers Revision::isDeleted
1163 */
1164 public function testIsDeleted_nothingDeleted() {
1165 $rev = $this->testPage->getRevision();
1166
1167 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
1168 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_COMMENT ) );
1169 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
1170 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_USER ) );
1171 }
1172
1173 /**
1174 * @covers Revision::getVisibility
1175 */
1176 public function testGetVisibility_nothingDeleted() {
1177 $rev = $this->testPage->getRevision();
1178
1179 $this->assertSame( 0, $rev->getVisibility() );
1180 }
1181
1182 /**
1183 * @covers Revision::getComment
1184 */
1185 public function testGetComment_notDeleted() {
1186 $expectedSummary = 'goatlicious summary';
1187
1188 $this->testPage->doEditContent(
1189 new WikitextContent( __METHOD__ ),
1190 $expectedSummary
1191 );
1192 $rev = $this->testPage->getRevision();
1193
1194 $this->assertSame( $expectedSummary, $rev->getComment() );
1195 }
1196
1197 /**
1198 * @covers Revision::isUnpatrolled
1199 */
1200 public function testIsUnpatrolled_returnsRecentChangesId() {
1201 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1202 $rev = $this->testPage->getRevision();
1203
1204 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1205 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1206 }
1207
1208 /**
1209 * @covers Revision::isUnpatrolled
1210 */
1211 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1212 // This assumes that sysops are auto patrolled
1213 $sysop = $this->getTestSysop()->getUser();
1214 $this->testPage->doEditContent(
1215 new WikitextContent( __METHOD__ ),
1216 __METHOD__,
1217 0,
1218 false,
1219 $sysop
1220 );
1221 $rev = $this->testPage->getRevision();
1222
1223 $this->assertSame( 0, $rev->isUnpatrolled() );
1224 }
1225
1226 /**
1227 * This is a simple blanket test for all simple content getters and is methods to provide some
1228 * coverage before the split of Revision into multiple classes for MCR work.
1229 * @covers Revision::getContent
1230 * @covers Revision::getSerializedData
1231 * @covers Revision::getContentModel
1232 * @covers Revision::getContentFormat
1233 * @covers Revision::getContentHandler
1234 */
1235 public function testSimpleContentGetters() {
1236 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1237 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1238
1239 $this->testPage->doEditContent(
1240 new WikitextContent( $expectedText ),
1241 $expectedSummary
1242 );
1243 $rev = $this->testPage->getRevision();
1244
1245 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1246 $this->assertSame( $expectedText, $rev->getSerializedData() );
1247 $this->assertSame( $this->testPage->getContentModel(), $rev->getContentModel() );
1248 $this->assertSame( $this->testPage->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1249 $this->assertSame( $this->testPage->getContentHandler(), $rev->getContentHandler() );
1250 }
1251
1252 /**
1253 * @covers Revision::newKnownCurrent
1254 */
1255 public function testNewKnownCurrent() {
1256 // Setup the services
1257 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1258 $this->setService( 'MainWANObjectCache', $cache );
1259 $db = wfGetDB( DB_MASTER );
1260
1261 // Get a fresh revision to use during testing
1262 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1263 $rev = $this->testPage->getRevision();
1264
1265 // Clear any previous cache for the revision during creation
1266 $key = $cache->makeGlobalKey( 'revision', $db->getDomainID(), $rev->getPage(), $rev->getId() );
1267 $cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1268 $this->assertFalse( $cache->get( $key ) );
1269
1270 // Get the new revision and make sure it is in the cache and correct
1271 $newRev = Revision::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1272 $this->assertRevEquals( $rev, $newRev );
1273 $this->assertRevEquals( $rev, $cache->get( $key ) );
1274 }
1275
1276 public function provideUserCanBitfield() {
1277 yield [ 0, 0, [], null, true ];
1278 // Bitfields match, user has no permissions
1279 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], null, false ];
1280 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], null, false ];
1281 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], null, false ];
1282 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], null, false ];
1283 // Bitfields match, user (admin) does have permissions
1284 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], null, true ];
1285 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], null, true ];
1286 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], null, true ];
1287 // Bitfields match, user (admin) does not have permissions
1288 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], null, false ];
1289 // Bitfields match, user (oversight) does have permissions
1290 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], null, true ];
1291 // Check permissions using the title
1292 yield [
1293 Revision::DELETED_TEXT,
1294 Revision::DELETED_TEXT,
1295 [ 'sysop' ],
1296 Title::newFromText( __METHOD__ ),
1297 true,
1298 ];
1299 yield [
1300 Revision::DELETED_TEXT,
1301 Revision::DELETED_TEXT,
1302 [],
1303 Title::newFromText( __METHOD__ ),
1304 false,
1305 ];
1306 }
1307
1308 /**
1309 * @dataProvider provideUserCanBitfield
1310 * @covers Revision::userCanBitfield
1311 */
1312 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1313 $this->setMwGlobals(
1314 'wgGroupPermissions',
1315 [
1316 'sysop' => [
1317 'deletedtext' => true,
1318 'deletedhistory' => true,
1319 ],
1320 'oversight' => [
1321 'viewsuppressed' => true,
1322 'suppressrevision' => true,
1323 ],
1324 ]
1325 );
1326 $user = $this->getTestUser( $userGroups )->getUser();
1327
1328 $this->assertSame(
1329 $expected,
1330 Revision::userCanBitfield( $bitField, $field, $user, $title )
1331 );
1332
1333 // Fallback to $wgUser
1334 $this->setMwGlobals(
1335 'wgUser',
1336 $user
1337 );
1338 $this->assertSame(
1339 $expected,
1340 Revision::userCanBitfield( $bitField, $field, null, $title )
1341 );
1342 }
1343
1344 public function provideUserCan() {
1345 yield [ 0, 0, [], true ];
1346 // Bitfields match, user has no permissions
1347 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], false ];
1348 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], false ];
1349 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], false ];
1350 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], false ];
1351 // Bitfields match, user (admin) does have permissions
1352 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], true ];
1353 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], true ];
1354 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], true ];
1355 // Bitfields match, user (admin) does not have permissions
1356 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], false ];
1357 // Bitfields match, user (oversight) does have permissions
1358 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], true ];
1359 }
1360
1361 /**
1362 * @dataProvider provideUserCan
1363 * @covers Revision::userCan
1364 */
1365 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1366 $this->setMwGlobals(
1367 'wgGroupPermissions',
1368 [
1369 'sysop' => [
1370 'deletedtext' => true,
1371 'deletedhistory' => true,
1372 ],
1373 'oversight' => [
1374 'viewsuppressed' => true,
1375 'suppressrevision' => true,
1376 ],
1377 ]
1378 );
1379 $user = $this->getTestUser( $userGroups )->getUser();
1380 $revision = new Revision( [ 'deleted' => $bitField ] );
1381
1382 $this->assertSame(
1383 $expected,
1384 $revision->userCan( $field, $user )
1385 );
1386 }
1387
1388 }