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