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