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