Follow-up 8697ba8: No need for two dependencies on the same module
[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 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_NEW,
96 ] );
97
98 if ( !$this->testPage ) {
99 /**
100 * We have to create a new page for each subclass as the page creation may result
101 * in different DB fields being filled based on configuration.
102 */
103 $this->testPage = $this->createPage( __CLASS__, __CLASS__ );
104 }
105 }
106
107 /**
108 * @param string $model
109 * @return Title
110 */
111 protected function getMockTitle() {
112 $mock = $this->getMockBuilder( Title::class )
113 ->disableOriginalConstructor()
114 ->getMock();
115 $mock->expects( $this->any() )
116 ->method( 'getNamespace' )
117 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
118 $mock->expects( $this->any() )
119 ->method( 'getPrefixedText' )
120 ->will( $this->returnValue( __CLASS__ ) );
121 $mock->expects( $this->any() )
122 ->method( 'getDBkey' )
123 ->will( $this->returnValue( __CLASS__ ) );
124 $mock->expects( $this->any() )
125 ->method( 'getArticleID' )
126 ->will( $this->returnValue( 23 ) );
127
128 return $mock;
129 }
130
131 abstract protected function getContentHandlerUseDB();
132
133 private function makeRevisionWithProps( $props = null ) {
134 if ( $props === null ) {
135 $props = [];
136 }
137
138 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
139 $props['text'] = 'Lorem Ipsum';
140 }
141
142 if ( !isset( $props['user_text'] ) ) {
143 $user = $this->getTestUser()->getUser();
144 $props['user_text'] = $user->getName();
145 $props['user'] = $user->getId();
146 }
147
148 if ( !isset( $props['user'] ) ) {
149 $props['user'] = 0;
150 }
151
152 if ( !isset( $props['comment'] ) ) {
153 $props['comment'] = 'just a test';
154 }
155
156 if ( !isset( $props['page'] ) ) {
157 $props['page'] = $this->testPage->getId();
158 }
159
160 if ( !isset( $props['content_model'] ) ) {
161 $props['content_model'] = CONTENT_MODEL_WIKITEXT;
162 }
163
164 $rev = new Revision( $props );
165
166 $dbw = wfGetDB( DB_MASTER );
167 $rev->insertOn( $dbw );
168
169 return $rev;
170 }
171
172 /**
173 * @param string $titleString
174 * @param string $text
175 * @param string|null $model
176 *
177 * @return WikiPage
178 */
179 private function createPage( $titleString, $text, $model = null ) {
180 if ( !preg_match( '/:/', $titleString ) &&
181 ( $model === null || $model === CONTENT_MODEL_WIKITEXT )
182 ) {
183 $ns = $this->getDefaultWikitextNS();
184 $titleString = MediaWikiServices::getInstance()->getNamespaceInfo()->
185 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::getPage
577 */
578 public function testGetPage() {
579 $page = $this->testPage;
580
581 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
582 $rev = Revision::newFromId( $orig->getId() );
583
584 $this->assertEquals( $page->getId(), $rev->getPage() );
585 }
586
587 /**
588 * @covers Revision::isCurrent
589 */
590 public function testIsCurrent() {
591 $rev1 = $this->testPage->getRevision();
592
593 # @todo find out if this should be true
594 # $this->assertTrue( $rev1->isCurrent() );
595
596 $rev1x = Revision::newFromId( $rev1->getId() );
597 $this->assertTrue( $rev1x->isCurrent() );
598
599 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
600 $rev2 = $this->testPage->getRevision();
601
602 # @todo find out if this should be true
603 # $this->assertTrue( $rev2->isCurrent() );
604
605 $rev1x = Revision::newFromId( $rev1->getId() );
606 $this->assertFalse( $rev1x->isCurrent() );
607
608 $rev2x = Revision::newFromId( $rev2->getId() );
609 $this->assertTrue( $rev2x->isCurrent() );
610 }
611
612 /**
613 * @covers Revision::getPrevious
614 */
615 public function testGetPrevious() {
616 $oldestRevision = $this->testPage->getOldestRevision();
617 $latestRevision = $this->testPage->getLatest();
618
619 $this->assertNull( $oldestRevision->getPrevious() );
620
621 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
622 $newRevision = $this->testPage->getRevision();
623
624 $this->assertNotNull( $newRevision->getPrevious() );
625 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
626 }
627
628 /**
629 * @covers Title::getPreviousRevisionID
630 * @covers Title::getRelativeRevisionID
631 * @covers MediaWiki\Revision\RevisionStore::getPreviousRevision
632 * @covers MediaWiki\Revision\RevisionStore::getRelativeRevision
633 */
634 public function testTitleGetPreviousRevisionID() {
635 $oldestId = $this->testPage->getOldestRevision()->getId();
636 $latestId = $this->testPage->getLatest();
637
638 $title = $this->testPage->getTitle();
639
640 $this->assertFalse( $title->getPreviousRevisionID( $oldestId ) );
641
642 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
643 $newId = $this->testPage->getRevision()->getId();
644
645 $this->assertEquals( $latestId, $title->getPreviousRevisionID( $newId ) );
646 }
647
648 /**
649 * @covers Title::getPreviousRevisionID
650 * @covers Title::getRelativeRevisionID
651 */
652 public function testTitleGetPreviousRevisionID_invalid() {
653 $this->assertFalse( $this->testPage->getTitle()->getPreviousRevisionID( 123456789 ) );
654 }
655
656 /**
657 * @covers Revision::getNext
658 */
659 public function testGetNext() {
660 $rev1 = $this->testPage->getRevision();
661
662 $this->assertNull( $rev1->getNext() );
663
664 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
665 $rev2 = $this->testPage->getRevision();
666
667 $this->assertNotNull( $rev1->getNext() );
668 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
669 }
670
671 /**
672 * @covers Title::getNextRevisionID
673 * @covers Title::getRelativeRevisionID
674 * @covers MediaWiki\Revision\RevisionStore::getNextRevision
675 * @covers MediaWiki\Revision\RevisionStore::getRelativeRevision
676 */
677 public function testTitleGetNextRevisionID() {
678 $title = $this->testPage->getTitle();
679
680 $origId = $this->testPage->getLatest();
681
682 $this->assertFalse( $title->getNextRevisionID( $origId ) );
683
684 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
685 $newId = $this->testPage->getLatest();
686
687 $this->assertSame( $this->testPage->getLatest(), $title->getNextRevisionID( $origId ) );
688 }
689
690 /**
691 * @covers Title::getNextRevisionID
692 * @covers Title::getRelativeRevisionID
693 */
694 public function testTitleGetNextRevisionID_invalid() {
695 $this->assertFalse( $this->testPage->getTitle()->getNextRevisionID( 123456789 ) );
696 }
697
698 /**
699 * @covers Revision::newNullRevision
700 */
701 public function testNewNullRevision() {
702 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
703 $orig = $this->testPage->getRevision();
704
705 $dbw = wfGetDB( DB_MASTER );
706 $rev = Revision::newNullRevision( $dbw, $this->testPage->getId(), 'a null revision', false );
707
708 $this->assertNotEquals( $orig->getId(), $rev->getId(),
709 'new null revision should have a different id from the original revision' );
710 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
711 'new null revision should have the same text id as the original revision' );
712 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
713 'new null revision should have the same SHA1 as the original revision' );
714 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
715 'new null revision should have the same content as the original revision' );
716 $this->assertEquals( __METHOD__, $rev->getContent()->getText() );
717 }
718
719 /**
720 * @covers Revision::newNullRevision
721 */
722 public function testNewNullRevision_badPage() {
723 $dbw = wfGetDB( DB_MASTER );
724 $rev = Revision::newNullRevision( $dbw, -1, 'a null revision', false );
725
726 $this->assertNull( $rev );
727 }
728
729 /**
730 * @covers Revision::insertOn
731 */
732 public function testInsertOn() {
733 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
734
735 $orig = $this->makeRevisionWithProps( [
736 'user_text' => $ip
737 ] );
738
739 // Make sure the revision was copied to ip_changes
740 $dbr = wfGetDB( DB_REPLICA );
741 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
742 $row = $res->fetchObject();
743
744 $this->assertEquals( IP::toHex( $ip ), $row->ipc_hex );
745 $this->assertEquals(
746 $orig->getTimestamp(),
747 wfTimestamp( TS_MW, $row->ipc_rev_timestamp )
748 );
749 }
750
751 public static function provideUserWasLastToEdit() {
752 yield 'actually the last edit' => [ 3, true ];
753 yield 'not the current edit, but still by this user' => [ 2, true ];
754 yield 'edit by another user' => [ 1, false ];
755 yield 'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
756 }
757
758 /**
759 * @covers Revision::userWasLastToEdit
760 * @dataProvider provideUserWasLastToEdit
761 */
762 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
763 $userA = User::newFromName( "RevisionStorageTest_userA" );
764 $userB = User::newFromName( "RevisionStorageTest_userB" );
765
766 if ( $userA->getId() === 0 ) {
767 $userA = User::createNew( $userA->getName() );
768 }
769
770 if ( $userB->getId() === 0 ) {
771 $userB = User::createNew( $userB->getName() );
772 }
773
774 $ns = $this->getDefaultWikitextNS();
775
776 $dbw = wfGetDB( DB_MASTER );
777 $revisions = [];
778
779 // create revisions -----------------------------
780 $page = WikiPage::factory( Title::newFromText(
781 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
782 $page->insertOn( $dbw );
783
784 $revisions[0] = new Revision( [
785 'page' => $page->getId(),
786 // we need the title to determine the page's default content model
787 'title' => $page->getTitle(),
788 'timestamp' => '20120101000000',
789 'user' => $userA->getId(),
790 'text' => 'zero',
791 'content_model' => CONTENT_MODEL_WIKITEXT,
792 'comment' => 'edit zero'
793 ] );
794 $revisions[0]->insertOn( $dbw );
795
796 $revisions[1] = new Revision( [
797 'page' => $page->getId(),
798 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
799 'title' => $page->getTitle(),
800 'timestamp' => '20120101000100',
801 'user' => $userA->getId(),
802 'text' => 'one',
803 'content_model' => CONTENT_MODEL_WIKITEXT,
804 'comment' => 'edit one'
805 ] );
806 $revisions[1]->insertOn( $dbw );
807
808 $revisions[2] = new Revision( [
809 'page' => $page->getId(),
810 'title' => $page->getTitle(),
811 'timestamp' => '20120101000200',
812 'user' => $userB->getId(),
813 'text' => 'two',
814 'content_model' => CONTENT_MODEL_WIKITEXT,
815 'comment' => 'edit two'
816 ] );
817 $revisions[2]->insertOn( $dbw );
818
819 $revisions[3] = new Revision( [
820 'page' => $page->getId(),
821 'title' => $page->getTitle(),
822 'timestamp' => '20120101000300',
823 'user' => $userA->getId(),
824 'text' => 'three',
825 'content_model' => CONTENT_MODEL_WIKITEXT,
826 'comment' => 'edit three'
827 ] );
828 $revisions[3]->insertOn( $dbw );
829
830 $revisions[4] = new Revision( [
831 'page' => $page->getId(),
832 'title' => $page->getTitle(),
833 'timestamp' => '20120101000200',
834 'user' => $userA->getId(),
835 'text' => 'zero',
836 'content_model' => CONTENT_MODEL_WIKITEXT,
837 'comment' => 'edit four'
838 ] );
839 $revisions[4]->insertOn( $dbw );
840
841 // test it ---------------------------------
842 $since = $revisions[$sinceIdx]->getTimestamp();
843
844 $revQuery = Revision::getQueryInfo();
845 $allRows = iterator_to_array( $dbw->select(
846 $revQuery['tables'],
847 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
848 [
849 'rev_page' => $page->getId(),
850 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
851 ],
852 __METHOD__,
853 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
854 $revQuery['joins']
855 ) );
856
857 $wasLast = Revision::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
858
859 $this->assertEquals( $expectedLast, $wasLast );
860 }
861
862 /**
863 * @param string $text
864 * @param string $title
865 * @param string $model
866 * @param string $format
867 *
868 * @return Revision
869 */
870 private function newTestRevision( $text, $title = "Test",
871 $model = CONTENT_MODEL_WIKITEXT, $format = null
872 ) {
873 if ( is_string( $title ) ) {
874 $title = Title::newFromText( $title );
875 }
876
877 $content = ContentHandler::makeContent( $text, $title, $model, $format );
878
879 $rev = new Revision(
880 [
881 'id' => 42,
882 'page' => 23,
883 'title' => $title,
884
885 'content' => $content,
886 'length' => $content->getSize(),
887 'comment' => "testing",
888 'minor_edit' => false,
889
890 'content_format' => $format,
891 ]
892 );
893
894 return $rev;
895 }
896
897 public function provideGetContentModel() {
898 // NOTE: we expect the help namespace to always contain wikitext
899 return [
900 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ],
901 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ],
902 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
903 ];
904 }
905
906 /**
907 * @dataProvider provideGetContentModel
908 * @covers Revision::getContentModel
909 */
910 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
911 $rev = $this->newTestRevision( $text, $title, $model, $format );
912
913 $this->assertEquals( $expectedModel, $rev->getContentModel() );
914 }
915
916 public function provideGetContentFormat() {
917 // NOTE: we expect the help namespace to always contain wikitext
918 return [
919 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ],
920 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ],
921 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ],
922 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting::MODEL_ID ],
923 ];
924 }
925
926 /**
927 * @dataProvider provideGetContentFormat
928 * @covers Revision::getContentFormat
929 */
930 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
931 $rev = $this->newTestRevision( $text, $title, $model, $format );
932
933 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
934 }
935
936 public function provideGetContentHandler() {
937 // NOTE: we expect the help namespace to always contain wikitext
938 return [
939 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler::class ],
940 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler::class ],
941 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting::class ],
942 ];
943 }
944
945 /**
946 * @dataProvider provideGetContentHandler
947 * @covers Revision::getContentHandler
948 */
949 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
950 $rev = $this->newTestRevision( $text, $title, $model, $format );
951
952 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
953 }
954
955 public function provideGetContent() {
956 // NOTE: we expect the help namespace to always contain wikitext
957 return [
958 [ 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ],
959 [
960 serialize( 'hello world' ),
961 'Hello',
962 DummyContentForTesting::MODEL_ID,
963 null,
964 Revision::FOR_PUBLIC,
965 serialize( 'hello world' )
966 ],
967 [
968 serialize( 'hello world' ),
969 'Dummy:Hello',
970 null,
971 null,
972 Revision::FOR_PUBLIC,
973 serialize( 'hello world' )
974 ],
975 ];
976 }
977
978 /**
979 * @dataProvider provideGetContent
980 * @covers Revision::getContent
981 */
982 public function testGetContent( $text, $title, $model, $format,
983 $audience, $expectedSerialization
984 ) {
985 $rev = $this->newTestRevision( $text, $title, $model, $format );
986 $content = $rev->getContent( $audience );
987
988 $this->assertEquals(
989 $expectedSerialization,
990 is_null( $content ) ? null : $content->serialize( $format )
991 );
992 }
993
994 /**
995 * @covers Revision::getContent
996 */
997 public function testGetContent_failure() {
998 $rev = new Revision( [
999 'page' => $this->testPage->getId(),
1000 'content_model' => $this->testPage->getContentModel(),
1001 'id' => 123456789, // not in the test DB
1002 ] );
1003
1004 Wikimedia\suppressWarnings(); // bad text_id will trigger a warning.
1005
1006 $this->assertNull( $rev->getContent(),
1007 "getContent() should return null if the revision's text blob could not be loaded." );
1008
1009 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
1010 $this->assertNull( $rev->getContent(),
1011 "getContent() should return null if the revision's text blob could not be loaded." );
1012
1013 Wikimedia\restoreWarnings();
1014 }
1015
1016 public function provideGetSize() {
1017 return [
1018 [ "hello world.", CONTENT_MODEL_WIKITEXT, 12 ],
1019 [ serialize( "hello world." ), DummyContentForTesting::MODEL_ID, 12 ],
1020 ];
1021 }
1022
1023 /**
1024 * @covers Revision::getSize
1025 * @dataProvider provideGetSize
1026 */
1027 public function testGetSize( $text, $model, $expected_size ) {
1028 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
1029 $this->assertEquals( $expected_size, $rev->getSize() );
1030 }
1031
1032 public function provideGetSha1() {
1033 return [
1034 [ "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ],
1035 [
1036 serialize( "hello world." ),
1037 DummyContentForTesting::MODEL_ID,
1038 Revision::base36Sha1( serialize( "hello world." ) )
1039 ],
1040 ];
1041 }
1042
1043 /**
1044 * @covers Revision::getSha1
1045 * @dataProvider provideGetSha1
1046 */
1047 public function testGetSha1( $text, $model, $expected_hash ) {
1048 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
1049 $this->assertEquals( $expected_hash, $rev->getSha1() );
1050 }
1051
1052 /**
1053 * Tests whether $rev->getContent() returns a clone when needed.
1054 *
1055 * @covers Revision::getContent
1056 */
1057 public function testGetContentClone() {
1058 $content = new RevisionTestModifyableContent( "foo" );
1059
1060 $rev = new Revision(
1061 [
1062 'id' => 42,
1063 'page' => 23,
1064 'title' => Title::newFromText( "testGetContentClone_dummy" ),
1065
1066 'content' => $content,
1067 'length' => $content->getSize(),
1068 'comment' => "testing",
1069 'minor_edit' => false,
1070 ]
1071 );
1072
1073 /** @var RevisionTestModifyableContent $content */
1074 $content = $rev->getContent( Revision::RAW );
1075 $content->setText( "bar" );
1076
1077 /** @var RevisionTestModifyableContent $content2 */
1078 $content2 = $rev->getContent( Revision::RAW );
1079 // content is mutable, expect clone
1080 $this->assertNotSame( $content, $content2, "expected a clone" );
1081 // clone should contain the original text
1082 $this->assertEquals( "foo", $content2->getText() );
1083
1084 $content2->setText( "bla bla" );
1085 // clones should be independent
1086 $this->assertEquals( "bar", $content->getText() );
1087 }
1088
1089 /**
1090 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1091 * @covers Revision::getContent
1092 */
1093 public function testGetContentUncloned() {
1094 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT );
1095 $content = $rev->getContent( Revision::RAW );
1096 $content2 = $rev->getContent( Revision::RAW );
1097
1098 // for immutable content like wikitext, this should be the same object
1099 $this->assertSame( $content, $content2 );
1100 }
1101
1102 /**
1103 * @covers Revision::loadFromId
1104 */
1105 public function testLoadFromId() {
1106 $rev = $this->testPage->getRevision();
1107 $this->hideDeprecated( 'Revision::loadFromId' );
1108 $this->assertRevEquals(
1109 $rev,
1110 Revision::loadFromId( wfGetDB( DB_MASTER ), $rev->getId() )
1111 );
1112 }
1113
1114 /**
1115 * @covers Revision::loadFromPageId
1116 */
1117 public function testLoadFromPageId() {
1118 $this->assertRevEquals(
1119 $this->testPage->getRevision(),
1120 Revision::loadFromPageId( wfGetDB( DB_MASTER ), $this->testPage->getId() )
1121 );
1122 }
1123
1124 /**
1125 * @covers Revision::loadFromPageId
1126 */
1127 public function testLoadFromPageIdWithLatestRevId() {
1128 $this->assertRevEquals(
1129 $this->testPage->getRevision(),
1130 Revision::loadFromPageId(
1131 wfGetDB( DB_MASTER ),
1132 $this->testPage->getId(),
1133 $this->testPage->getLatest()
1134 )
1135 );
1136 }
1137
1138 /**
1139 * @covers Revision::loadFromPageId
1140 */
1141 public function testLoadFromPageIdWithNotLatestRevId() {
1142 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1143 $this->assertRevEquals(
1144 $this->testPage->getRevision()->getPrevious(),
1145 Revision::loadFromPageId(
1146 wfGetDB( DB_MASTER ),
1147 $this->testPage->getId(),
1148 $this->testPage->getRevision()->getPrevious()->getId()
1149 )
1150 );
1151 }
1152
1153 /**
1154 * @covers Revision::loadFromTitle
1155 */
1156 public function testLoadFromTitle() {
1157 $this->assertRevEquals(
1158 $this->testPage->getRevision(),
1159 Revision::loadFromTitle( wfGetDB( DB_MASTER ), $this->testPage->getTitle() )
1160 );
1161 }
1162
1163 /**
1164 * @covers Revision::loadFromTitle
1165 */
1166 public function testLoadFromTitleWithLatestRevId() {
1167 $this->assertRevEquals(
1168 $this->testPage->getRevision(),
1169 Revision::loadFromTitle(
1170 wfGetDB( DB_MASTER ),
1171 $this->testPage->getTitle(),
1172 $this->testPage->getLatest()
1173 )
1174 );
1175 }
1176
1177 /**
1178 * @covers Revision::loadFromTitle
1179 */
1180 public function testLoadFromTitleWithNotLatestRevId() {
1181 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1182 $this->assertRevEquals(
1183 $this->testPage->getRevision()->getPrevious(),
1184 Revision::loadFromTitle(
1185 wfGetDB( DB_MASTER ),
1186 $this->testPage->getTitle(),
1187 $this->testPage->getRevision()->getPrevious()->getId()
1188 )
1189 );
1190 }
1191
1192 /**
1193 * @covers Revision::loadFromTimestamp()
1194 */
1195 public function testLoadFromTimestamp() {
1196 $this->assertRevEquals(
1197 $this->testPage->getRevision(),
1198 Revision::loadFromTimestamp(
1199 wfGetDB( DB_MASTER ),
1200 $this->testPage->getTitle(),
1201 $this->testPage->getRevision()->getTimestamp()
1202 )
1203 );
1204 }
1205
1206 /**
1207 * @covers Revision::getParentLengths
1208 */
1209 public function testGetParentLengths_noRevIds() {
1210 $this->assertSame(
1211 [],
1212 Revision::getParentLengths(
1213 wfGetDB( DB_MASTER ),
1214 []
1215 )
1216 );
1217 }
1218
1219 /**
1220 * @covers Revision::getParentLengths
1221 */
1222 public function testGetParentLengths_oneRevId() {
1223 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1224 $textLength = strlen( $text );
1225
1226 $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
1227 $rev[1] = $this->testPage->getLatest();
1228
1229 $this->assertSame(
1230 [ $rev[1] => $textLength ],
1231 Revision::getParentLengths(
1232 wfGetDB( DB_MASTER ),
1233 [ $rev[1] ]
1234 )
1235 );
1236 }
1237
1238 /**
1239 * @covers Revision::getParentLengths
1240 */
1241 public function testGetParentLengths_multipleRevIds() {
1242 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1243 $textOneLength = strlen( $textOne );
1244 $textTwo = '831jr091jr092121j09rj1';
1245 $textTwoLength = strlen( $textTwo );
1246
1247 $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
1248 $rev[1] = $this->testPage->getLatest();
1249 $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
1250 $rev[2] = $this->testPage->getLatest();
1251
1252 $this->assertSame(
1253 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1254 Revision::getParentLengths(
1255 wfGetDB( DB_MASTER ),
1256 [ $rev[1], $rev[2] ]
1257 )
1258 );
1259 }
1260
1261 /**
1262 * @covers Revision::getTitle
1263 */
1264 public function testGetTitle_fromExistingRevision() {
1265 $this->assertTrue(
1266 $this->testPage->getTitle()->equals(
1267 $this->testPage->getRevision()->getTitle()
1268 )
1269 );
1270 }
1271
1272 /**
1273 * @covers Revision::getTitle
1274 */
1275 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1276 $rev = new Revision( [ 'id' => $this->testPage->getLatest() ] );
1277 $this->assertTrue(
1278 $this->testPage->getTitle()->equals(
1279 $rev->getTitle()
1280 )
1281 );
1282 }
1283
1284 /**
1285 * @covers Revision::isMinor
1286 */
1287 public function testIsMinor_true() {
1288 // Use a sysop to ensure we can mark edits as minor
1289 $sysop = $this->getTestSysop()->getUser();
1290
1291 $this->testPage->doEditContent(
1292 new WikitextContent( __METHOD__ ),
1293 __METHOD__,
1294 EDIT_MINOR,
1295 false,
1296 $sysop
1297 );
1298 $rev = $this->testPage->getRevision();
1299
1300 $this->assertSame( true, $rev->isMinor() );
1301 }
1302
1303 /**
1304 * @covers Revision::isMinor
1305 */
1306 public function testIsMinor_false() {
1307 $this->testPage->doEditContent(
1308 new WikitextContent( __METHOD__ ),
1309 __METHOD__,
1310 0
1311 );
1312 $rev = $this->testPage->getRevision();
1313
1314 $this->assertSame( false, $rev->isMinor() );
1315 }
1316
1317 /**
1318 * @covers Revision::getTimestamp
1319 */
1320 public function testGetTimestamp() {
1321 $testTimestamp = wfTimestampNow();
1322
1323 $this->testPage->doEditContent(
1324 new WikitextContent( __METHOD__ ),
1325 __METHOD__
1326 );
1327 $rev = $this->testPage->getRevision();
1328
1329 $this->assertInternalType( 'string', $rev->getTimestamp() );
1330 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1331 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1332 }
1333
1334 /**
1335 * @covers Revision::getUser
1336 * @covers Revision::getUserText
1337 */
1338 public function testGetUserAndText() {
1339 $sysop = $this->getTestSysop()->getUser();
1340
1341 $this->testPage->doEditContent(
1342 new WikitextContent( __METHOD__ ),
1343 __METHOD__,
1344 0,
1345 false,
1346 $sysop
1347 );
1348 $rev = $this->testPage->getRevision();
1349
1350 $this->assertSame( $sysop->getId(), $rev->getUser() );
1351 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1352 }
1353
1354 /**
1355 * @covers Revision::isDeleted
1356 */
1357 public function testIsDeleted_nothingDeleted() {
1358 $rev = $this->testPage->getRevision();
1359
1360 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_TEXT ) );
1361 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_COMMENT ) );
1362 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_RESTRICTED ) );
1363 $this->assertSame( false, $rev->isDeleted( Revision::DELETED_USER ) );
1364 }
1365
1366 /**
1367 * @covers Revision::getVisibility
1368 */
1369 public function testGetVisibility_nothingDeleted() {
1370 $rev = $this->testPage->getRevision();
1371
1372 $this->assertSame( 0, $rev->getVisibility() );
1373 }
1374
1375 /**
1376 * @covers Revision::getComment
1377 */
1378 public function testGetComment_notDeleted() {
1379 $expectedSummary = 'goatlicious summary';
1380
1381 $this->testPage->doEditContent(
1382 new WikitextContent( __METHOD__ ),
1383 $expectedSummary
1384 );
1385 $rev = $this->testPage->getRevision();
1386
1387 $this->assertSame( $expectedSummary, $rev->getComment() );
1388 }
1389
1390 /**
1391 * @covers Revision::isUnpatrolled
1392 */
1393 public function testIsUnpatrolled_returnsRecentChangesId() {
1394 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1395 $rev = $this->testPage->getRevision();
1396
1397 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1398 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1399 }
1400
1401 /**
1402 * @covers Revision::isUnpatrolled
1403 */
1404 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1405 // This assumes that sysops are auto patrolled
1406 $sysop = $this->getTestSysop()->getUser();
1407 $this->testPage->doEditContent(
1408 new WikitextContent( __METHOD__ ),
1409 __METHOD__,
1410 0,
1411 false,
1412 $sysop
1413 );
1414 $rev = $this->testPage->getRevision();
1415
1416 $this->assertSame( 0, $rev->isUnpatrolled() );
1417 }
1418
1419 /**
1420 * This is a simple blanket test for all simple content getters and is methods to provide some
1421 * coverage before the split of Revision into multiple classes for MCR work.
1422 * @covers Revision::getContent
1423 * @covers Revision::getSerializedData
1424 * @covers Revision::getContentModel
1425 * @covers Revision::getContentFormat
1426 * @covers Revision::getContentHandler
1427 */
1428 public function testSimpleContentGetters() {
1429 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1430 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1431
1432 $this->testPage->doEditContent(
1433 new WikitextContent( $expectedText ),
1434 $expectedSummary
1435 );
1436 $rev = $this->testPage->getRevision();
1437
1438 $this->assertSame( $expectedText, $rev->getContent()->getText() );
1439 $this->assertSame( $expectedText, $rev->getSerializedData() );
1440 $this->assertSame( $this->testPage->getContentModel(), $rev->getContentModel() );
1441 $this->assertSame( $this->testPage->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1442 $this->assertSame( $this->testPage->getContentHandler(), $rev->getContentHandler() );
1443 }
1444
1445 /**
1446 * @covers Revision::newKnownCurrent
1447 */
1448 public function testNewKnownCurrent() {
1449 // Setup the services
1450 $this->overrideMwServices();
1451 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1452 $this->setService( 'MainWANObjectCache', $cache );
1453 $db = wfGetDB( DB_MASTER );
1454
1455 $now = 1553893742;
1456 $cache->setMockTime( $now );
1457
1458 // Get a fresh revision to use during testing
1459 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1460 $rev = $this->testPage->getRevision();
1461
1462 // Clear any previous cache for the revision during creation
1463 $key = $cache->makeGlobalKey(
1464 RevisionStore::ROW_CACHE_KEY,
1465 $db->getDomainID(),
1466 $rev->getPage(),
1467 $rev->getId()
1468 );
1469 $cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE );
1470 $this->assertFalse( $cache->get( $key ) );
1471
1472 ++$now;
1473
1474 // Get the new revision and make sure it is in the cache and correct
1475 $newRev = Revision::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1476 $this->assertRevEquals( $rev, $newRev );
1477
1478 $cachedRow = $cache->get( $key );
1479 $this->assertNotFalse( $cachedRow );
1480 $this->assertEquals( $rev->getId(), $cachedRow->rev_id );
1481 }
1482
1483 public function testNewKnownCurrent_withPageId() {
1484 $db = wfGetDB( DB_MASTER );
1485
1486 $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
1487 $rev = $this->testPage->getRevision();
1488
1489 $pageId = $this->testPage->getId();
1490
1491 $newRev = Revision::newKnownCurrent( $db, $pageId, $rev->getId() );
1492 $this->assertRevEquals( $rev, $newRev );
1493 }
1494
1495 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1496 $db = wfGetDB( DB_MASTER );
1497
1498 $this->assertFalse( Revision::newKnownCurrent( $db, 0 ) );
1499 }
1500
1501 public function provideUserCanBitfield() {
1502 yield [ 0, 0, [], null, true ];
1503 // Bitfields match, user has no permissions
1504 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], null, false ];
1505 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], null, false ];
1506 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], null, false ];
1507 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], null, false ];
1508 // Bitfields match, user (admin) does have permissions
1509 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], null, true ];
1510 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], null, true ];
1511 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], null, true ];
1512 // Bitfields match, user (admin) does not have permissions
1513 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], null, false ];
1514 // Bitfields match, user (oversight) does have permissions
1515 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], null, true ];
1516 // Check permissions using the title
1517 yield [
1518 Revision::DELETED_TEXT,
1519 Revision::DELETED_TEXT,
1520 [ 'sysop' ],
1521 __METHOD__,
1522 true,
1523 ];
1524 yield [
1525 Revision::DELETED_TEXT,
1526 Revision::DELETED_TEXT,
1527 [],
1528 __METHOD__,
1529 false,
1530 ];
1531 }
1532
1533 /**
1534 * @dataProvider provideUserCanBitfield
1535 * @covers Revision::userCanBitfield
1536 */
1537 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1538 $title = Title::newFromText( $title );
1539
1540 $this->setGroupPermissions(
1541 [
1542 'sysop' => [
1543 'deletedtext' => true,
1544 'deletedhistory' => true,
1545 ],
1546 'oversight' => [
1547 'viewsuppressed' => true,
1548 'suppressrevision' => true,
1549 ],
1550 ]
1551 );
1552 $user = $this->getTestUser( $userGroups )->getUser();
1553
1554 $this->assertSame(
1555 $expected,
1556 Revision::userCanBitfield( $bitField, $field, $user, $title )
1557 );
1558
1559 // Fallback to $wgUser
1560 $this->setMwGlobals(
1561 'wgUser',
1562 $user
1563 );
1564 $this->assertSame(
1565 $expected,
1566 Revision::userCanBitfield( $bitField, $field, null, $title )
1567 );
1568 }
1569
1570 public function provideUserCan() {
1571 yield [ 0, 0, [], true ];
1572 // Bitfields match, user has no permissions
1573 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [], false ];
1574 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [], false ];
1575 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [], false ];
1576 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [], false ];
1577 // Bitfields match, user (admin) does have permissions
1578 yield [ Revision::DELETED_TEXT, Revision::DELETED_TEXT, [ 'sysop' ], true ];
1579 yield [ Revision::DELETED_COMMENT, Revision::DELETED_COMMENT, [ 'sysop' ], true ];
1580 yield [ Revision::DELETED_USER, Revision::DELETED_USER, [ 'sysop' ], true ];
1581 // Bitfields match, user (admin) does not have permissions
1582 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'sysop' ], false ];
1583 // Bitfields match, user (oversight) does have permissions
1584 yield [ Revision::DELETED_RESTRICTED, Revision::DELETED_RESTRICTED, [ 'oversight' ], true ];
1585 }
1586
1587 /**
1588 * @dataProvider provideUserCan
1589 * @covers Revision::userCan
1590 */
1591 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1592 $this->setGroupPermissions(
1593 [
1594 'sysop' => [
1595 'deletedtext' => true,
1596 'deletedhistory' => true,
1597 ],
1598 'oversight' => [
1599 'viewsuppressed' => true,
1600 'suppressrevision' => true,
1601 ],
1602 ]
1603 );
1604 $user = $this->getTestUser( $userGroups )->getUser();
1605 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage->getTitle() );
1606
1607 $this->assertSame(
1608 $expected,
1609 $revision->userCan( $field, $user )
1610 );
1611 }
1612
1613 public function provideGetTextId() {
1614 yield [ [], null ];
1615
1616 $slot = new SlotRecord( (object)[
1617 'slot_revision_id' => 42,
1618 'slot_content_id' => 1,
1619 'content_address' => 'tt:789',
1620 'model_name' => CONTENT_MODEL_WIKITEXT,
1621 'role_name' => SlotRecord::MAIN,
1622 'slot_origin' => 1,
1623 ], new WikitextContent( 'Test' ) );
1624
1625 $rec = new MutableRevisionRecord( $this->testPage->getTitle() );
1626 $rec->setId( 42 );
1627 $rec->setSlot( $slot );
1628
1629 yield [ $rec, 789 ];
1630 }
1631
1632 /**
1633 * @dataProvider provideGetTextId
1634 * @covers Revision::getTextId()
1635 */
1636 public function testGetTextId( $spec, $expected ) {
1637 $rev = new Revision( $spec, 0, $this->testPage->getTitle() );
1638 $this->assertSame( $expected, $rev->getTextId() );
1639 }
1640
1641 abstract public function provideGetRevisionText();
1642
1643 /**
1644 * @dataProvider provideGetRevisionText
1645 * @covers Revision::getRevisionText
1646 */
1647 public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
1648 $rev = $this->testPage->getRevisionRecord();
1649
1650 $queryInfo = Revision::getQueryInfo( $queryInfoOptions );
1651 $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ?? [] );
1652 $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ?? [] );
1653 $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ?? [] );
1654
1655 $conds = [ 'rev_id' => $rev->getId() ];
1656 $row = $this->db->selectRow(
1657 $queryInfo['tables'],
1658 $queryInfo['fields'],
1659 $conds,
1660 __METHOD__,
1661 [],
1662 $queryInfo['joins']
1663 );
1664
1665 $expected = $rev->getContent( SlotRecord::MAIN )->serialize();
1666
1667 $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
1668 $this->assertSame( $expected, Revision::getRevisionText( $row ) );
1669 }
1670
1671 }