Use the WebRequest::getCheck() shortcut where possible
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionDbTestBase.php
1 <?php
2 use MediaWiki\MediaWikiServices;
3 use MediaWiki\Revision\MutableRevisionRecord;
4 use MediaWiki\Revision\RevisionStore;
5 use MediaWiki\Revision\IncompleteRevisionException;
6 use MediaWiki\Revision\RevisionRecord;
7 use MediaWiki\Revision\SlotRecord;
8
9 /**
10 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
11 *
12 * @group Database
13 * @group medium
14 */
15 abstract class RevisionDbTestBase extends MediaWikiTestCase {
16
17 /**
18 * @var WikiPage $testPage
19 */
20 private $testPage;
21
22 public function __construct( $name = null, array $data = [], $dataName = '' ) {
23 parent::__construct( $name, $data, $dataName );
24
25 $this->tablesUsed = array_merge( $this->tablesUsed,
26 [
27 'page',
28 'revision',
29 'ip_changes',
30 'text',
31 'archive',
32
33 'recentchanges',
34 'logging',
35
36 'page_props',
37 'pagelinks',
38 'categorylinks',
39 'langlinks',
40 'externallinks',
41 'imagelinks',
42 'templatelinks',
43 'iwlinks'
44 ]
45 );
46 }
47
48 protected function addCoreDBData() {
49 // Blank out. This would fail with a modified schema, and we don't need it.
50 }
51
52 /**
53 * @return int
54 */
55 abstract protected function getMcrMigrationStage();
56
57 /**
58 * @return string[]
59 */
60 abstract protected function getMcrTablesToReset();
61
62 protected function setUp() {
63 $this->tablesUsed += $this->getMcrTablesToReset();
64
65 parent::setUp();
66
67 $this->mergeMwGlobalArrayValue(
68 'wgExtraNamespaces',
69 [
70 12312 => 'Dummy',
71 12313 => 'Dummy_talk',
72 ]
73 );
74
75 $this->mergeMwGlobalArrayValue(
76 'wgNamespaceContentModels',
77 [
78 12312 => DummyContentForTesting::MODEL_ID,
79 ]
80 );
81
82 $this->mergeMwGlobalArrayValue(
83 'wgContentHandlers',
84 [
85 DummyContentForTesting::MODEL_ID => 'DummyContentHandlerForTesting',
86 RevisionTestModifyableContent::MODEL_ID => 'RevisionTestModifyableContentHandler',
87 ]
88 );
89
90 $this->setMwGlobals( [
91 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
92 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
93 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_OLD,
94 ] );
95
96 $this->overrideMwServices();
97
98 if ( !$this->testPage ) {
99 /**
100 * We have to create a new page for each subclass as the page creation may result
101 * in different DB fields being filled based on configuration.
102 */
103 $this->testPage = $this->createPage( __CLASS__, __CLASS__ );
104 }
105 }
106
107 /**
108 * @param string $model
109 * @return Title
110 */
111 protected function getMockTitle() {
112 $mock = $this->getMockBuilder( Title::class )
113 ->disableOriginalConstructor()
114 ->getMock();
115 $mock->expects( $this->any() )
116 ->method( 'getNamespace' )
117 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
118 $mock->expects( $this->any() )
119 ->method( 'getPrefixedText' )
120 ->will( $this->returnValue( __CLASS__ ) );
121 $mock->expects( $this->any() )
122 ->method( 'getDBkey' )
123 ->will( $this->returnValue( __CLASS__ ) );
124 $mock->expects( $this->any() )
125 ->method( 'getArticleID' )
126 ->will( $this->returnValue( 23 ) );
127
128 return $mock;
129 }
130
131 abstract protected function getContentHandlerUseDB();
132
133 private function makeRevisionWithProps( $props = null ) {
134 if ( $props === null ) {
135 $props = [];
136 }
137
138 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
139 $props['text'] = 'Lorem Ipsum';
140 }
141
142 if ( !isset( $props['user_text'] ) ) {
143 $user = $this->getTestUser()->getUser();
144 $props['user_text'] = $user->getName();
145 $props['user'] = $user->getId();
146 }
147
148 if ( !isset( $props['user'] ) ) {
149 $props['user'] = 0;
150 }
151
152 if ( !isset( $props['comment'] ) ) {
153 $props['comment'] = 'just a test';
154 }
155
156 if ( !isset( $props['page'] ) ) {
157 $props['page'] = $this->testPage->getId();
158 }
159
160 if ( !isset( $props['content_model'] ) ) {
161 $props['content_model'] = CONTENT_MODEL_WIKITEXT;
162 }
163
164 $rev = new Revision( $props );
165
166 $dbw = wfGetDB( DB_MASTER );
167 $rev->insertOn( $dbw );
168
169 return $rev;
170 }
171
172 /**
173 * @param string $titleString
174 * @param string $text
175 * @param string|null $model
176 *
177 * @return WikiPage
178 */
179 private function createPage( $titleString, $text, $model = null ) {
180 if ( !preg_match( '/:/', $titleString ) &&
181 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
182 ) {
183 $ns = $this->getDefaultWikitextNS();
184 $titleString = MWNamespace::getCanonicalName( $ns ) . ':' . $titleString;
185 }
186
187 $title = Title::newFromText( $titleString );
188 $wikipage = new WikiPage( $title );
189
190 // Delete the article if it already exists
191 if ( $wikipage->exists() ) {
192 $wikipage->doDeleteArticle( "done" );
193 }
194
195 $content = ContentHandler::makeContent( $text, $title, $model );
196 $wikipage->doEditContent( $content, __METHOD__, EDIT_NEW );
197
198 return $wikipage;
199 }
200
201 private function assertRevEquals( Revision $orig, Revision $rev = null ) {
202 $this->assertNotNull( $rev, 'missing revision' );
203
204 $this->assertEquals( $orig->getId(), $rev->getId() );
205 $this->assertEquals( $orig->getPage(), $rev->getPage() );
206 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
207 $this->assertEquals( $orig->getUser(), $rev->getUser() );
208 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
209 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
210 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
211 }
212
213 /**
214 * @covers Revision::getRecentChange
215 */
216 public function testGetRecentChange() {
217 $rev = $this->testPage->getRevision();
218 $recentChange = $rev->getRecentChange();
219
220 // Make sure various attributes look right / the correct entry has been retrieved.
221 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
222 $this->assertEquals(
223 $rev->getTitle()->getNamespace(),
224 $recentChange->getAttribute( 'rc_namespace' )
225 );
226 $this->assertEquals(
227 $rev->getTitle()->getDBkey(),
228 $recentChange->getAttribute( 'rc_title' )
229 );
230 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
231 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
232 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
233 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
234 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
235 }
236
237 /**
238 * @covers Revision::insertOn
239 */
240 public function testInsertOn_success() {
241 $parentId = $this->testPage->getLatest();
242
243 // If an ExternalStore is set don't use it.
244 $this->setMwGlobals( 'wgDefaultExternalStore', false );
245
246 $rev = new Revision( [
247 'page' => $this->testPage->getId(),
248 'title' => $this->testPage->getTitle(),
249 'text' => 'Revision Text',
250 'comment' => 'Revision comment',
251 ] );
252
253 $revId = $rev->insertOn( wfGetDB( DB_MASTER ) );
254
255 $this->assertInternalType( 'integer', $revId );
256 $this->assertSame( $revId, $rev->getId() );
257
258 // getTextId() must be an int!
259 $this->assertInternalType( 'integer', $rev->getTextId() );
260
261 $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
262
263 // we currently only support storage in the text table
264 $textId = MediaWikiServices::getInstance()
265 ->getBlobStore()
266 ->getTextIdFromAddress( $mainSlot->getAddress() );
267
268 $this->assertSelect(
269 'text',
270 [ 'old_id', 'old_text' ],
271 "old_id = $textId",
272 [ [ strval( $textId ), 'Revision Text' ] ]
273 );
274 $this->assertSelect(
275 'revision',
276 [
277 'rev_id',
278 'rev_page',
279 'rev_minor_edit',
280 'rev_deleted',
281 'rev_len',
282 'rev_parent_id',
283 'rev_sha1',
284 ],
285 "rev_id = {$rev->getId()}",
286 [ [
287 strval( $rev->getId() ),
288 strval( $this->testPage->getId() ),
289 '0',
290 '0',
291 '13',
292 strval( $parentId ),
293 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
294 ] ]
295 );
296 }
297
298 public function provideInsertOn_exceptionOnIncomplete() {
299 $content = new TextContent( '' );
300 $user = User::newFromName( 'Foo' );
301
302 yield 'no parent' => [
303 [
304 'content' => $content,
305 'comment' => 'test',
306 'user' => $user,
307 ],
308 IncompleteRevisionException::class,
309 "rev_page field must not be 0!"
310 ];
311
312 yield 'no comment' => [
313 [
314 'content' => $content,
315 'page' => 7,
316 'user' => $user,
317 ],
318 IncompleteRevisionException::class,
319 "comment must not be NULL!"
320 ];
321
322 yield 'no content' => [
323 [
324 'comment' => 'test',
325 'page' => 7,
326 'user' => $user,
327 ],
328 IncompleteRevisionException::class,
329 "Uninitialized field: content_address" // XXX: message may change
330 ];
331 }
332
333 /**
334 * @dataProvider provideInsertOn_exceptionOnIncomplete
335 * @covers Revision::insertOn
336 */
337 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
338 // If an ExternalStore is set don't use it.
339 $this->setMwGlobals( 'wgDefaultExternalStore', false );
340 $this->setExpectedException( $expException, $expMessage );
341
342 $title = Title::newFromText( 'Nonexistant-' . __METHOD__ );
343 $rev = new Revision( $array, 0, $title );
344
345 $rev->insertOn( wfGetDB( DB_MASTER ) );
346 }
347
348 /**
349 * @covers Revision::newFromTitle
350 */
351 public function testNewFromTitle_withoutId() {
352 $latestRevId = $this->testPage->getLatest();
353
354 $rev = Revision::newFromTitle( $this->testPage->getTitle() );
355
356 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
357 $this->assertEquals( $latestRevId, $rev->getId() );
358 }
359
360 /**
361 * @covers Revision::newFromTitle
362 */
363 public function testNewFromTitle_withId() {
364 $latestRevId = $this->testPage->getLatest();
365
366 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId );
367
368 $this->assertTrue( $this->testPage->getTitle()->equals( $rev->getTitle() ) );
369 $this->assertEquals( $latestRevId, $rev->getId() );
370 }
371
372 /**
373 * @covers Revision::newFromTitle
374 */
375 public function testNewFromTitle_withBadId() {
376 $latestRevId = $this->testPage->getLatest();
377
378 $rev = Revision::newFromTitle( $this->testPage->getTitle(), $latestRevId + 1 );
379
380 $this->assertNull( $rev );
381 }
382
383 /**
384 * @covers Revision::newFromRow
385 */
386 public function testNewFromRow() {
387 $orig = $this->makeRevisionWithProps();
388
389 $dbr = wfGetDB( DB_REPLICA );
390 $revQuery = Revision::getQueryInfo();
391 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
392 __METHOD__, [], $revQuery['joins'] );
393 $this->assertTrue( is_object( $res ), 'query failed' );
394
395 $row = $res->fetchObject();
396 $res->free();
397
398 $rev = Revision::newFromRow( $row );
399
400 $this->assertRevEquals( $orig, $rev );
401 }
402
403 public function provideNewFromArchiveRow() {
404 yield [
405 function ( $f ) {
406 return $f;
407 },
408 ];
409 yield [
410 function ( $f ) {
411 return $f + [ 'ar_namespace', 'ar_title' ];
412 },
413 ];
414 yield [
415 function ( $f ) {
416 unset( $f['ar_text_id'] );
417 return $f;
418 },
419 ];
420 yield [
421 function ( $f ) {
422 unset( $f['ar_page_id'] );
423 return $f;
424 },
425 ];
426 yield [
427 function ( $f ) {
428 unset( $f['ar_parent_id'] );
429 return $f;
430 },
431 ];
432 yield [
433 function ( $f ) {
434 unset( $f['ar_rev_id'] );
435 return $f;
436 },
437 ];
438 yield [
439 function ( $f ) {
440 unset( $f['ar_sha1'] );
441 return $f;
442 },
443 ];
444 }
445
446 /**
447 * @dataProvider provideNewFromArchiveRow
448 * @covers Revision::newFromArchiveRow
449 */
450 public function testNewFromArchiveRow( $selectModifier ) {
451 $services = MediaWikiServices::getInstance();
452
453 $store = new RevisionStore(
454 $services->getDBLoadBalancer(),
455 $services->getService( '_SqlBlobStore' ),
456 $services->getMainWANObjectCache(),
457 $services->getCommentStore(),
458 $services->getContentModelStore(),
459 $services->getSlotRoleStore(),
460 $services->getSlotRoleRegistry(),
461 $this->getMcrMigrationStage(),
462 $services->getActorMigration()
463 );
464
465 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
466 $this->setService( 'RevisionStore', $store );
467
468 $page = $this->createPage(
469 'RevisionStorageTest_testNewFromArchiveRow',
470 'Lorem Ipsum',
471 CONTENT_MODEL_WIKITEXT
472 );
473 $orig = $page->getRevision();
474 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
475
476 $dbr = wfGetDB( DB_REPLICA );
477 $arQuery = Revision::getArchiveQueryInfo();
478 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
479 $res = $dbr->select(
480 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
481 __METHOD__, [], $arQuery['joins']
482 );
483 $this->assertTrue( is_object( $res ), 'query failed' );
484
485 $row = $res->fetchObject();
486 $res->free();
487
488 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
489 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
490 $rev = Revision::newFromArchiveRow( $row );
491
492 $this->assertRevEquals( $orig, $rev );
493 }
494
495 /**
496 * @covers Revision::newFromArchiveRow
497 */
498 public function testNewFromArchiveRowOverrides() {
499 $page = $this->createPage(
500 'RevisionStorageTest_testNewFromArchiveRow',
501 'Lorem Ipsum',
502 CONTENT_MODEL_WIKITEXT
503 );
504 $orig = $page->getRevision();
505 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
506
507 $dbr = wfGetDB( DB_REPLICA );
508 $arQuery = Revision::getArchiveQueryInfo();
509 $res = $dbr->select(
510 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
511 __METHOD__, [], $arQuery['joins']
512 );
513 $this->assertTrue( is_object( $res ), 'query failed' );
514
515 $row = $res->fetchObject();
516 $res->free();
517
518 $rev = Revision::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
519
520 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
521 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
522 }
523
524 /**
525 * @covers Revision::newFromId
526 */
527 public function testNewFromId() {
528 $orig = $this->testPage->getRevision();
529 $rev = Revision::newFromId( $orig->getId() );
530 $this->assertRevEquals( $orig, $rev );
531 }
532
533 /**
534 * @covers Revision::newFromPageId
535 */
536 public function testNewFromPageId() {
537 $rev = Revision::newFromPageId( $this->testPage->getId() );
538 $this->assertRevEquals(
539 $this->testPage->getRevision(),
540 $rev
541 );
542 }
543
544 /**
545 * @covers Revision::newFromPageId
546 */
547 public function testNewFromPageIdWithLatestId() {
548 $rev = Revision::newFromPageId(
549 $this->testPage->getId(),
550 $this->testPage->getLatest()
551 );
552 $this->assertRevEquals(
553 $this->testPage->getRevision(),
554 $rev
555 );
556 }
557
558 /**
559 * @covers Revision::newFromPageId
560 */
561 public function testNewFromPageIdWithNotLatestId() {
562 $content = new WikitextContent( __METHOD__ );
563 $this->testPage->doEditContent( $content, __METHOD__ );
564 $rev = Revision::newFromPageId(
565 $this->testPage->getId(),
566 $this->testPage->getRevision()->getPrevious()->getId()
567 );
568 $this->assertRevEquals(
569 $this->testPage->getRevision()->getPrevious(),
570 $rev
571 );
572 }
573
574 /**
575 * @covers Revision::getPage
576 */
577 public function testGetPage() {
578 $page = $this->testPage;
579
580 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
581 $rev = Revision::newFromId( $orig->getId() );
582
583 $this->assertEquals( $page->getId(), $rev->getPage() );
584 }
585
586 /**
587 * @covers Revision::isCurrent
588 */
589 public function testIsCurrent() {
590 $rev1 = $this->testPage->getRevision();
591
592 # @todo find out if this should be true
593 # $this->assertTrue( $rev1->isCurrent() );
594
595 $rev1x = Revision::newFromId( $rev1->getId() );
596 $this->assertTrue( $rev1x->isCurrent() );
597
598 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
599 $rev2 = $this->testPage->getRevision();
600
601 # @todo find out if this should be true
602 # $this->assertTrue( $rev2->isCurrent() );
603
604 $rev1x = Revision::newFromId( $rev1->getId() );
605 $this->assertFalse( $rev1x->isCurrent() );
606
607 $rev2x = Revision::newFromId( $rev2->getId() );
608 $this->assertTrue( $rev2x->isCurrent() );
609 }
610
611 /**
612 * @covers Revision::getPrevious
613 */
614 public function testGetPrevious() {
615 $oldestRevision = $this->testPage->getOldestRevision();
616 $latestRevision = $this->testPage->getLatest();
617
618 $this->assertNull( $oldestRevision->getPrevious() );
619
620 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
621 $newRevision = $this->testPage->getRevision();
622
623 $this->assertNotNull( $newRevision->getPrevious() );
624 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
625 }
626
627 /**
628 * @covers Revision::getNext
629 */
630 public function testGetNext() {
631 $rev1 = $this->testPage->getRevision();
632
633 $this->assertNull( $rev1->getNext() );
634
635 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
636 $rev2 = $this->testPage->getRevision();
637
638 $this->assertNotNull( $rev1->getNext() );
639 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
640 }
641
642 /**
643 * @covers Revision::newNullRevision
644 */
645 public function testNewNullRevision() {
646 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
647 $orig = $this->testPage->getRevision();
648
649 $dbw = wfGetDB( DB_MASTER );
650 $rev = Revision::newNullRevision( $dbw, $this->testPage->getId(), 'a null revision', false );
651
652 $this->assertNotEquals( $orig->getId(), $rev->getId(),
653 'new null revision should have a different id from the original revision' );
654 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
655 'new null revision should have the same text id as the original revision' );
656 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
657 'new null revision should have the same SHA1 as the original revision' );
658 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
659 'new null revision should have the same content as the original revision' );
660 $this->assertEquals( __METHOD__, $rev->getContent()->getText() );
661 }
662
663 /**
664 * @covers Revision::newNullRevision
665 */
666 public function testNewNullRevision_badPage() {
667 $dbw = wfGetDB( DB_MASTER );
668 $rev = Revision::newNullRevision( $dbw, -1, 'a null revision', false );
669
670 $this->assertNull( $rev );
671 }
672
673 /**
674 * @covers Revision::insertOn
675 */
676 public function testInsertOn() {
677 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
678
679 $orig = $this->makeRevisionWithProps( [
680 'user_text' => $ip
681 ] );
682
683 // Make sure the revision was copied to ip_changes
684 $dbr = wfGetDB( DB_REPLICA );
685 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
686 $row = $res->fetchObject();
687
688 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
689 $this->assertEquals(
690 $orig->getTimestamp(),
691 wfTimestamp( TS_MW, $row->ipc_rev_timestamp )
692 );
693 }
694
695 public static function provideUserWasLastToEdit() {
696 yield 'actually the last edit' => [ 3, true ];
697 yield 'not the current edit, but still by this user' => [ 2, true ];
698 yield 'edit by another user' => [ 1, false ];
699 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
700 }
701
702 /**
703 * @covers Revision::userWasLastToEdit
704 * @dataProvider provideUserWasLastToEdit
705 */
706 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
707 $userA = User::newFromName( "RevisionStorageTest_userA" );
708 $userB = User::newFromName( "RevisionStorageTest_userB" );
709
710 if ( $userA->getId() === 0 ) {
711 $userA = User::createNew( $userA->getName() );
712 }
713
714 if ( $userB->getId() === 0 ) {
715 $userB = User::createNew( $userB->getName() );
716 }
717
718 $ns = $this->getDefaultWikitextNS();
719
720 $dbw = wfGetDB( DB_MASTER );
721 $revisions = [];
722
723 // create revisions -----------------------------
724 $page = WikiPage::factory( Title::newFromText(
725 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
726 $page->insertOn( $dbw );
727
728 $revisions[0] = new Revision( [
729 'page' => $page->getId(),
730 // we need the title to determine the page's default content model
731 'title' => $page->getTitle(),
732 'timestamp' => '20120101000000',
733 'user' => $userA->getId(),
734 'text' => 'zero',
735 'content_model' => CONTENT_MODEL_WIKITEXT,
736 'comment' => 'edit zero'
737 ] );
738 $revisions[0]->insertOn( $dbw );
739
740 $revisions[1] = new Revision( [
741 'page' => $page->getId(),
742 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
743 'title' => $page->getTitle(),
744 'timestamp' => '20120101000100',
745 'user' => $userA->getId(),
746 'text' => 'one',
747 'content_model' => CONTENT_MODEL_WIKITEXT,
748 'comment' => 'edit one'
749 ] );
750 $revisions[1]->insertOn( $dbw );
751
752 $revisions[2] = new Revision( [
753 'page' => $page->getId(),
754 'title' => $page->getTitle(),
755 'timestamp' => '20120101000200',
756 'user' => $userB->getId(),
757 'text' => 'two',
758 'content_model' => CONTENT_MODEL_WIKITEXT,
759 'comment' => 'edit two'
760 ] );
761 $revisions[2]->insertOn( $dbw );
762
763 $revisions[3] = new Revision( [
764 'page' => $page->getId(),
765 'title' => $page->getTitle(),
766 'timestamp' => '20120101000300',
767 'user' => $userA->getId(),
768 'text' => 'three',
769 'content_model' => CONTENT_MODEL_WIKITEXT,
770 'comment' => 'edit three'
771 ] );
772 $revisions[3]->insertOn( $dbw );
773
774 $revisions[4] = new Revision( [
775 'page' => $page->getId(),
776 'title' => $page->getTitle(),
777 'timestamp' => '20120101000200',
778 'user' => $userA->getId(),
779 'text' => 'zero',
780 'content_model' => CONTENT_MODEL_WIKITEXT,
781 'comment' => 'edit four'
782 ] );
783 $revisions[4]->insertOn( $dbw );
784
785 // test it ---------------------------------
786 $since = $revisions[$sinceIdx]->getTimestamp();
787
788 $revQuery = Revision::getQueryInfo();
789 $allRows = iterator_to_array( $dbw->select(
790 $revQuery['tables'],
791 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
792 [
793 'rev_page' => $page->getId(),
794 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
795 ],
796 __METHOD__,
797 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
798 $revQuery['joins']
799 ) );
800
801 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
802
803 $this->assertEquals( $expectedLast, $wasLast );
804 }
805
806 /**
807 * @param string $text
808 * @param string $title
809 * @param string $model
810 * @param string $format
811 *
812 * @return Revision
813 */
814 private function newTestRevision( $text, $title = "Test",
815 $model = CONTENT_MODEL_WIKITEXT, $format = null
816 ) {
817 if ( is_string( $title ) ) {
818 $title = Title::newFromText( $title );
819 }
820
821 $content = ContentHandler::makeContent( $text, $title, $model, $format );
822
823 $rev = new Revision(
824 [
825 'id' => 42,
826 'page' => 23,
827 'title' => $title,
828
829 'content' => $content,
830 'length' => $content->getSize(),
831 'comment' => "testing",
832 'minor_edit' => false,
833
834 'content_format' => $format,
835 ]
836 );
837
838 return $rev;
839 }
840
841 public function provideGetContentModel() {
842 // NOTE: we expect the help namespace to always contain wikitext
843 return [
844 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
845 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
846 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
847 ];
848 }
849
850 /**
851 * @dataProvider provideGetContentModel
852 * @covers Revision::getContentModel
853 */
854 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
855 $rev = $this->newTestRevision( $text, $title, $model, $format );
856
857 $this->assertEquals( $expectedModel, $rev->getContentModel() );
858 }
859
860 public function provideGetContentFormat() {
861 // NOTE: we expect the help namespace to always contain wikitext
862 return [
863 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
864 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
865 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
866 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
867 ];
868 }
869
870 /**
871 * @dataProvider provideGetContentFormat
872 * @covers Revision::getContentFormat
873 */
874 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
875 $rev = $this->newTestRevision( $text, $title, $model, $format );
876
877 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
878 }
879
880 public function provideGetContentHandler() {
881 // NOTE: we expect the help namespace to always contain wikitext
882 return [
883 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler::class ],
884 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler::class ],
885 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting::class ],
886 ];
887 }
888
889 /**
890 * @dataProvider provideGetContentHandler
891 * @covers Revision::getContentHandler
892 */
893 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
894 $rev = $this->newTestRevision( $text, $title, $model, $format );
895
896 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
897 }
898
899 public function provideGetContent() {
900 // NOTE: we expect the help namespace to always contain wikitext
901 return [
902 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
903 [
904 serialize( 'hello world' ),
905 'Hello',
906 DummyContentForTesting::MODEL_ID,
907 null,
908 Revision::FOR_PUBLIC,
909 serialize( 'hello world' )
910 ],
911 [
912 serialize( 'hello world' ),
913 'Dummy:Hello',
914 null,
915 null,
916 Revision::FOR_PUBLIC,
917 serialize( 'hello world' )
918 ],
919 ];
920 }
921
922 /**
923 * @dataProvider provideGetContent
924 * @covers Revision::getContent
925 */
926 public function testGetContent( $text, $title, $model, $format,
927 $audience, $expectedSerialization
928 ) {
929 $rev = $this->newTestRevision( $text, $title, $model, $format );
930 $content = $rev->getContent( $audience );
931
932 $this->assertEquals(
933 $expectedSerialization,
934 is_null( $content ) ? null : $content->serialize( $format )
935 );
936 }
937
938 /**
939 * @covers Revision::getContent
940 */
941 public function testGetContent_failure() {
942 $rev = new Revision( [
943 'page' => $this->testPage->getId(),
944 'content_model' => $this->testPage->getContentModel(),
945 'id' => 123456789, // not in the test DB
946 ] );
947
948 Wikimedia\suppressWarnings(); // bad text_id will trigger a warning.
949
950 $this->assertNull( $rev->getContent(),
951 "getContent() should return null if the revision's text blob could not be loaded." );
952
953 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
954 $this->assertNull( $rev->getContent(),
955 "getContent() should return null if the revision's text blob could not be loaded." );
956
957 Wikimedia\restoreWarnings();
958 }
959
960 public function provideGetSize() {
961 return [
962 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
963 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
964 ];
965 }
966
967 /**
968 * @covers Revision::getSize
969 * @dataProvider provideGetSize
970 */
971 public function testGetSize( $text, $model, $expected_size ) {
972 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
973 $this->assertEquals( $expected_size, $rev->getSize() );
974 }
975
976 public function provideGetSha1() {
977 return [
978 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
979 [
980 serialize( "hello world." ),
981 DummyContentForTesting::MODEL_ID,
982 Revision::base36Sha1( serialize( "hello world." ) )
983 ],
984 ];
985 }
986
987 /**
988 * @covers Revision::getSha1
989 * @dataProvider provideGetSha1
990 */
991 public function testGetSha1( $text, $model, $expected_hash ) {
992 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
993 $this->assertEquals( $expected_hash, $rev->getSha1() );
994 }
995
996 /**
997 * Tests whether $rev->getContent() returns a clone when needed.
998 *
999 * @covers Revision::getContent
1000 */
1001 public function testGetContentClone() {
1002 $content = new RevisionTestModifyableContent( "foo" );
1003
1004 $rev = new Revision(
1005 [
1006 'id' => 42,
1007 'page' => 23,
1008 'title' => Title::newFromText( "testGetContentClone_dummy" ),
1009
1010 'content' => $content,
1011 'length' => $content->getSize(),
1012 'comment' => "testing",
1013 'minor_edit' => false,
1014 ]
1015 );
1016
1017 /** @var RevisionTestModifyableContent $content */
1018 $content = $rev->getContent( Revision::RAW );
1019 $content->setText( "bar" );
1020
1021 /** @var RevisionTestModifyableContent $content2 */
1022 $content2 = $rev->getContent( Revision::RAW );
1023 // content is mutable, expect clone
1024 $this->assertNotSame( $content, $content2, "expected a clone" );
1025 // clone should contain the original text
1026 $this->assertEquals( "foo", $content2->getText() );
1027
1028 $content2->setText( "bla bla" );
1029 // clones should be independent
1030 $this->assertEquals( "bar", $content->getText() );
1031 }
1032
1033 /**
1034 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1035 * @covers Revision::getContent
1036 */
1037 public function testGetContentUncloned() {
1038 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
1039 $content = $rev->getContent( Revision::RAW );
1040 $content2 = $rev->getContent( Revision::RAW );
1041
1042 // for immutable content like wikitext, this should be the same object
1043 $this->assertSame( $content, $content2 );
1044 }
1045
1046 /**
1047 * @covers Revision::loadFromId
1048 */
1049 public function testLoadFromId() {
1050 $rev = $this->testPage->getRevision();
1051 $this->hideDeprecated( 'Revision::loadFromId' );
1052 $this->assertRevEquals(
1053 $rev,
1054 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
1055 );
1056 }
1057
1058 /**
1059 * @covers Revision::loadFromPageId
1060 */
1061 public function testLoadFromPageId() {
1062 $this->assertRevEquals(
1063 $this->testPage->getRevision(),
1064 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
1065 );
1066 }
1067
1068 /**
1069 * @covers Revision::loadFromPageId
1070 */
1071 public function testLoadFromPageIdWithLatestRevId() {
1072 $this->assertRevEquals(
1073 $this->testPage->getRevision(),
1074 Revision::loadFromPageId(
1075 wfGetDB( DB_MASTER ),
1076 $this->testPage->getId(),
1077 $this->testPage->getLatest()
1078 )
1079 );
1080 }
1081
1082 /**
1083 * @covers Revision::loadFromPageId
1084 */
1085 public function testLoadFromPageIdWithNotLatestRevId() {
1086 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1087 $this->assertRevEquals(
1088 $this->testPage->getRevision()->getPrevious(),
1089 Revision::loadFromPageId(
1090 wfGetDB( DB_MASTER ),
1091 $this->testPage->getId(),
1092 $this->testPage->getRevision()->getPrevious()->getId()
1093 )
1094 );
1095 }
1096
1097 /**
1098 * @covers Revision::loadFromTitle
1099 */
1100 public function testLoadFromTitle() {
1101 $this->assertRevEquals(
1102 $this->testPage->getRevision(),
1103 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
1104 );
1105 }
1106
1107 /**
1108 * @covers Revision::loadFromTitle
1109 */
1110 public function testLoadFromTitleWithLatestRevId() {
1111 $this->assertRevEquals(
1112 $this->testPage->getRevision(),
1113 Revision::loadFromTitle(
1114 wfGetDB( DB_MASTER ),
1115 $this->testPage->getTitle(),
1116 $this->testPage->getLatest()
1117 )
1118 );
1119 }
1120
1121 /**
1122 * @covers Revision::loadFromTitle
1123 */
1124 public function testLoadFromTitleWithNotLatestRevId() {
1125 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1126 $this->assertRevEquals(
1127 $this->testPage->getRevision()->getPrevious(),
1128 Revision::loadFromTitle(
1129 wfGetDB( DB_MASTER ),
1130 $this->testPage->getTitle(),
1131 $this->testPage->getRevision()->getPrevious()->getId()
1132 )
1133 );
1134 }
1135
1136 /**
1137 * @covers Revision::loadFromTimestamp()
1138 */
1139 public function testLoadFromTimestamp() {
1140 $this->assertRevEquals(
1141 $this->testPage->getRevision(),
1142 Revision::loadFromTimestamp(
1143 wfGetDB( DB_MASTER ),
1144 $this->testPage->getTitle(),
1145 $this->testPage->getRevision()->getTimestamp()
1146 )
1147 );
1148 }
1149
1150 /**
1151 * @covers Revision::getParentLengths
1152 */
1153 public function testGetParentLengths_noRevIds() {
1154 $this->assertSame(
1155 [],
1156 Revision::getParentLengths(
1157 wfGetDB( DB_MASTER ),
1158 []
1159 )
1160 );
1161 }
1162
1163 /**
1164 * @covers Revision::getParentLengths
1165 */
1166 public function testGetParentLengths_oneRevId() {
1167 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1168 $textLength = strlen( $text );
1169
1170 $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
1171 $rev[1] = $this->testPage->getLatest();
1172
1173 $this->assertSame(
1174 [ $rev[1] => $textLength ],
1175 Revision::getParentLengths(
1176 wfGetDB( DB_MASTER ),
1177 [ $rev[1] ]
1178 )
1179 );
1180 }
1181
1182 /**
1183 * @covers Revision::getParentLengths
1184 */
1185 public function testGetParentLengths_multipleRevIds() {
1186 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1187 $textOneLength = strlen( $textOne );
1188 $textTwo = '831jr091jr092121j09rj1';
1189 $textTwoLength = strlen( $textTwo );
1190
1191 $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
1192 $rev[1] = $this->testPage->getLatest();
1193 $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
1194 $rev[2] = $this->testPage->getLatest();
1195
1196 $this->assertSame(
1197 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1198 Revision::getParentLengths(
1199 wfGetDB( DB_MASTER ),
1200 [ $rev[1], $rev[2] ]
1201 )
1202 );
1203 }
1204
1205 /**
1206 * @covers Revision::getTitle
1207 */
1208 public function testGetTitle_fromExistingRevision() {
1209 $this->assertTrue(
1210 $this->testPage->getTitle()->equals(
1211 $this->testPage->getRevision()->getTitle()
1212 )
1213 );
1214 }
1215
1216 /**
1217 * @covers Revision::getTitle
1218 */
1219 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1220 $rev = new Revision( [ 'id' => $this->testPage->getLatest() ] );
1221 $this->assertTrue(
1222 $this->testPage->getTitle()->equals(
1223 $rev->getTitle()
1224 )
1225 );
1226 }
1227
1228 /**
1229 * @covers Revision::isMinor
1230 */
1231 public function testIsMinor_true() {
1232 // Use a sysop to ensure we can mark edits as minor
1233 $sysop = $this->getTestSysop()->getUser();
1234
1235 $this->testPage->doEditContent(
1236 new WikitextContent( __METHOD__ ),
1237 __METHOD__,
1238 EDIT_MINOR,
1239 false,
1240 $sysop
1241 );
1242 $rev = $this->testPage->getRevision();
1243
1244 $this->assertSame( true, $rev->isMinor() );
1245 }
1246
1247 /**
1248 * @covers Revision::isMinor
1249 */
1250 public function testIsMinor_false() {
1251 $this->testPage->doEditContent(
1252 new WikitextContent( __METHOD__ ),
1253 __METHOD__,
1254 0
1255 );
1256 $rev = $this->testPage->getRevision();
1257
1258 $this->assertSame( false, $rev->isMinor() );
1259 }
1260
1261 /**
1262 * @covers Revision::getTimestamp
1263 */
1264 public function testGetTimestamp() {
1265 $testTimestamp = wfTimestampNow();
1266
1267 $this->testPage->doEditContent(
1268 new WikitextContent( __METHOD__ ),
1269 __METHOD__
1270 );
1271 $rev = $this->testPage->getRevision();
1272
1273 $this->assertInternalType( 'string', $rev->getTimestamp() );
1274 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1275 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1276 }
1277
1278 /**
1279 * @covers Revision::getUser
1280 * @covers Revision::getUserText
1281 */
1282 public function testGetUserAndText() {
1283 $sysop = $this->getTestSysop()->getUser();
1284
1285 $this->testPage->doEditContent(
1286 new WikitextContent( __METHOD__ ),
1287 __METHOD__,
1288 0,
1289 false,
1290 $sysop
1291 );
1292 $rev = $this->testPage->getRevision();
1293
1294 $this->assertSame( $sysop->getId(), $rev->getUser() );
1295 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1296 }
1297
1298 /**
1299 * @covers Revision::isDeleted
1300 */
1301 public function testIsDeleted_nothingDeleted() {
1302 $rev = $this->testPage->getRevision();
1303
1304 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
1305 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_COMMENT ) );
1306 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
1307 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_USER ) );
1308 }
1309
1310 /**
1311 * @covers Revision::getVisibility
1312 */
1313 public function testGetVisibility_nothingDeleted() {
1314 $rev = $this->testPage->getRevision();
1315
1316 $this->assertSame( 0, $rev->getVisibility() );
1317 }
1318
1319 /**
1320 * @covers Revision::getComment
1321 */
1322 public function testGetComment_notDeleted() {
1323 $expectedSummary = 'goatlicious summary';
1324
1325 $this->testPage->doEditContent(
1326 new WikitextContent( __METHOD__ ),
1327 $expectedSummary
1328 );
1329 $rev = $this->testPage->getRevision();
1330
1331 $this->assertSame( $expectedSummary, $rev->getComment() );
1332 }
1333
1334 /**
1335 * @covers Revision::isUnpatrolled
1336 */
1337 public function testIsUnpatrolled_returnsRecentChangesId() {
1338 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1339 $rev = $this->testPage->getRevision();
1340
1341 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1342 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1343 }
1344
1345 /**
1346 * @covers Revision::isUnpatrolled
1347 */
1348 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1349 // This assumes that sysops are auto patrolled
1350 $sysop = $this->getTestSysop()->getUser();
1351 $this->testPage->doEditContent(
1352 new WikitextContent( __METHOD__ ),
1353 __METHOD__,
1354 0,
1355 false,
1356 $sysop
1357 );
1358 $rev = $this->testPage->getRevision();
1359
1360 $this->assertSame( 0, $rev->isUnpatrolled() );
1361 }
1362
1363 /**
1364 * This is a simple blanket test for all simple content getters and is methods to provide some
1365 * coverage before the split of Revision into multiple classes for MCR work.
1366 * @covers Revision::getContent
1367 * @covers Revision::getSerializedData
1368 * @covers Revision::getContentModel
1369 * @covers Revision::getContentFormat
1370 * @covers Revision::getContentHandler
1371 */
1372 public function testSimpleContentGetters() {
1373 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1374 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1375
1376 $this->testPage->doEditContent(
1377 new WikitextContent( $expectedText ),
1378 $expectedSummary
1379 );
1380 $rev = $this->testPage->getRevision();
1381
1382 $this->assertSame( $expectedText, $rev->getContent()->getText() );
1383 $this->assertSame( $expectedText, $rev->getSerializedData() );
1384 $this->assertSame( $this->testPage->getContentModel(), $rev->getContentModel() );
1385 $this->assertSame( $this->testPage->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1386 $this->assertSame( $this->testPage->getContentHandler(), $rev->getContentHandler() );
1387 }
1388
1389 /**
1390 * @covers Revision::newKnownCurrent
1391 */
1392 public function testNewKnownCurrent() {
1393 // Setup the services
1394 $this->overrideMwServices();
1395 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1396 $this->setService( 'MainWANObjectCache', $cache );
1397 $db = wfGetDB( DB_MASTER );
1398
1399 // Get a fresh revision to use during testing
1400 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1401 $rev = $this->testPage->getRevision();
1402
1403 // Clear any previous cache for the revision during creation
1404 $key = $cache->makeGlobalKey(
1405 RevisionStore::ROW_CACHE_KEY,
1406 $db->getDomainID(),
1407 $rev->getPage(),
1408 $rev->getId()
1409 );
1410 $cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1411 $this->assertFalse( $cache->get( $key ) );
1412
1413 // Get the new revision and make sure it is in the cache and correct
1414 $newRev = Revision::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1415 $this->assertRevEquals( $rev, $newRev );
1416
1417 $cachedRow = $cache->get( $key );
1418 $this->assertNotFalse( $cachedRow );
1419 $this->assertEquals( $rev->getId(), $cachedRow->rev_id );
1420 }
1421
1422 public function testNewKnownCurrent_withPageId() {
1423 $db = wfGetDB( DB_MASTER );
1424
1425 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1426 $rev = $this->testPage->getRevision();
1427
1428 $pageId = $this->testPage->getId();
1429
1430 $newRev = Revision::newKnownCurrent( $db, $pageId, $rev->getId() );
1431 $this->assertRevEquals( $rev, $newRev );
1432 }
1433
1434 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1435 $db = wfGetDB( DB_MASTER );
1436
1437 $this->assertFalse( Revision::newKnownCurrent( $db, 0 ) );
1438 }
1439
1440 public function provideUserCanBitfield() {
1441 yield [ 0, 0, [], null, true ];
1442 // Bitfields match, user has no permissions
1443 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], null, false ];
1444 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], null, false ];
1445 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], null, false ];
1446 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], null, false ];
1447 // Bitfields match, user (admin) does have permissions
1448 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], null, true ];
1449 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], null, true ];
1450 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], null, true ];
1451 // Bitfields match, user (admin) does not have permissions
1452 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], null, false ];
1453 // Bitfields match, user (oversight) does have permissions
1454 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], null, true ];
1455 // Check permissions using the title
1456 yield [
1457 Revision::DELETED_TEXT,
1458 Revision::DELETED_TEXT,
1459 [ 'sysop' ],
1460 __METHOD__,
1461 true,
1462 ];
1463 yield [
1464 Revision::DELETED_TEXT,
1465 Revision::DELETED_TEXT,
1466 [],
1467 __METHOD__,
1468 false,
1469 ];
1470 }
1471
1472 /**
1473 * @dataProvider provideUserCanBitfield
1474 * @covers Revision::userCanBitfield
1475 */
1476 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1477 $title = Title::newFromText( $title );
1478
1479 $this->setMwGlobals(
1480 'wgGroupPermissions',
1481 [
1482 'sysop' => [
1483 'deletedtext' => true,
1484 'deletedhistory' => true,
1485 ],
1486 'oversight' => [
1487 'viewsuppressed' => true,
1488 'suppressrevision' => true,
1489 ],
1490 ]
1491 );
1492 $user = $this->getTestUser( $userGroups )->getUser();
1493
1494 $this->assertSame(
1495 $expected,
1496 Revision::userCanBitfield( $bitField, $field, $user, $title )
1497 );
1498
1499 // Fallback to $wgUser
1500 $this->setMwGlobals(
1501 'wgUser',
1502 $user
1503 );
1504 $this->assertSame(
1505 $expected,
1506 Revision::userCanBitfield( $bitField, $field, null, $title )
1507 );
1508 }
1509
1510 public function provideUserCan() {
1511 yield [ 0, 0, [], true ];
1512 // Bitfields match, user has no permissions
1513 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], false ];
1514 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], false ];
1515 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], false ];
1516 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], false ];
1517 // Bitfields match, user (admin) does have permissions
1518 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], true ];
1519 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], true ];
1520 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], true ];
1521 // Bitfields match, user (admin) does not have permissions
1522 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], false ];
1523 // Bitfields match, user (oversight) does have permissions
1524 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], true ];
1525 }
1526
1527 /**
1528 * @dataProvider provideUserCan
1529 * @covers Revision::userCan
1530 */
1531 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1532 $this->setMwGlobals(
1533 'wgGroupPermissions',
1534 [
1535 'sysop' => [
1536 'deletedtext' => true,
1537 'deletedhistory' => true,
1538 ],
1539 'oversight' => [
1540 'viewsuppressed' => true,
1541 'suppressrevision' => true,
1542 ],
1543 ]
1544 );
1545 $user = $this->getTestUser( $userGroups )->getUser();
1546 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage->getTitle() );
1547
1548 $this->assertSame(
1549 $expected,
1550 $revision->userCan( $field, $user )
1551 );
1552 }
1553
1554 public function provideGetTextId() {
1555 yield [ [], null ];
1556
1557 $slot = new SlotRecord( (object)[
1558 'slot_revision_id' => 42,
1559 'slot_content_id' => 1,
1560 'content_address' => 'tt:789',
1561 'model_name' => CONTENT_MODEL_WIKITEXT,
1562 'role_name' => SlotRecord::MAIN,
1563 'slot_origin' => 1,
1564 ], new WikitextContent( 'Test' ) );
1565
1566 $rec = new MutableRevisionRecord( $this->testPage->getTitle() );
1567 $rec->setId( 42 );
1568 $rec->setSlot( $slot );
1569
1570 yield [ $rec, 789 ];
1571 }
1572
1573 /**
1574 * @dataProvider provideGetTextId
1575 * @covers Revision::getTextId()
1576 */
1577 public function testGetTextId( $spec, $expected ) {
1578 $rev = new Revision( $spec, 0, $this->testPage->getTitle() );
1579 $this->assertSame( $expected, $rev->getTextId() );
1580 }
1581
1582 public function provideGetRevisionText() {
1583 yield [
1584 [ 'text' ]
1585 ];
1586 }
1587
1588 /**
1589 * @dataProvider provideGetRevisionText
1590 * @covers Revision::getRevisionText
1591 */
1592 public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
1593 $rev = $this->testPage->getRevisionRecord();
1594
1595 $queryInfo = Revision::getQueryInfo( $queryInfoOptions );
1596 $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ?? [] );
1597 $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ?? [] );
1598 $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ?? [] );
1599
1600 $conds = [ 'rev_id' => $rev->getId() ];
1601 $row = $this->db->selectRow(
1602 $queryInfo['tables'],
1603 $queryInfo['fields'],
1604 $conds,
1605 __METHOD__,
1606 [],
1607 $queryInfo['joins']
1608 );
1609
1610 $expected = $rev->getContent( SlotRecord::MAIN )->serialize();
1611
1612 $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
1613 $this->assertSame( $expected, Revision::getRevisionText( $row ) );
1614 }
1615
1616 }