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