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