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