2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Revision\MutableRevisionRecord
;
4 use MediaWiki\Revision\RevisionStore
;
5 use MediaWiki\Revision\IncompleteRevisionException
;
6 use MediaWiki\Revision\RevisionRecord
;
7 use MediaWiki\Revision\SlotRecord
;
10 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
15 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
18 * @var WikiPage $testPage
22 public function __construct( $name = null, array $data = [], $dataName = '' ) {
23 parent
::__construct( $name, $data, $dataName );
25 $this->tablesUsed
= array_merge( $this->tablesUsed
,
49 protected function addCoreDBData() {
50 // Blank out. This would fail with a modified schema, and we don't need it.
56 abstract protected function getMcrMigrationStage();
61 abstract protected function getMcrTablesToReset();
63 protected function setUp() {
64 $this->tablesUsed +
= $this->getMcrTablesToReset();
68 $this->mergeMwGlobalArrayValue(
72 12313 => 'Dummy_talk',
76 $this->mergeMwGlobalArrayValue(
77 'wgNamespaceContentModels',
79 12312 => DummyContentForTesting
::MODEL_ID
,
83 $this->mergeMwGlobalArrayValue(
86 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
87 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
91 $this->setMwGlobals( [
92 'wgMultiContentRevisionSchemaMigrationStage' => $this->getMcrMigrationStage(),
93 'wgContentHandlerUseDB' => $this->getContentHandlerUseDB(),
94 'wgActorTableSchemaMigrationStage' => SCHEMA_COMPAT_NEW
,
97 $this->overrideMwServices();
99 if ( !$this->testPage
) {
101 * We have to create a new page for each subclass as the page creation may result
102 * in different DB fields being filled based on configuration.
104 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
109 * @param string $model
112 protected function getMockTitle() {
113 $mock = $this->getMockBuilder( Title
::class )
114 ->disableOriginalConstructor()
116 $mock->expects( $this->any() )
117 ->method( 'getNamespace' )
118 ->will( $this->returnValue( $this->getDefaultWikitextNS() ) );
119 $mock->expects( $this->any() )
120 ->method( 'getPrefixedText' )
121 ->will( $this->returnValue( __CLASS__
) );
122 $mock->expects( $this->any() )
123 ->method( 'getDBkey' )
124 ->will( $this->returnValue( __CLASS__
) );
125 $mock->expects( $this->any() )
126 ->method( 'getArticleID' )
127 ->will( $this->returnValue( 23 ) );
132 abstract protected function getContentHandlerUseDB();
134 private function makeRevisionWithProps( $props = null ) {
135 if ( $props === null ) {
139 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
140 $props['text'] = 'Lorem Ipsum';
143 if ( !isset( $props['user_text'] ) ) {
144 $user = $this->getTestUser()->getUser();
145 $props['user_text'] = $user->getName();
146 $props['user'] = $user->getId();
149 if ( !isset( $props['user'] ) ) {
153 if ( !isset( $props['comment'] ) ) {
154 $props['comment'] = 'just a test';
157 if ( !isset( $props['page'] ) ) {
158 $props['page'] = $this->testPage
->getId();
161 if ( !isset( $props['content_model'] ) ) {
162 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
165 $rev = new Revision( $props );
167 $dbw = wfGetDB( DB_MASTER
);
168 $rev->insertOn( $dbw );
174 * @param string $titleString
175 * @param string $text
176 * @param string|null $model
180 private function createPage( $titleString, $text, $model = null ) {
181 if ( !preg_match( '/:/', $titleString ) &&
182 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
184 $ns = $this->getDefaultWikitextNS();
185 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
188 $title = Title
::newFromText( $titleString );
189 $wikipage = new WikiPage( $title );
191 // Delete the article if it already exists
192 if ( $wikipage->exists() ) {
193 $wikipage->doDeleteArticle( "done" );
196 $content = ContentHandler
::makeContent( $text, $title, $model );
197 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
202 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
203 $this->assertNotNull( $rev, 'missing revision' );
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() );
215 * @covers Revision::getRecentChange
217 public function testGetRecentChange() {
218 $rev = $this->testPage
->getRevision();
219 $recentChange = $rev->getRecentChange();
221 // Make sure various attributes look right / the correct entry has been retrieved.
222 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
224 $rev->getTitle()->getNamespace(),
225 $recentChange->getAttribute( 'rc_namespace' )
228 $rev->getTitle()->getDBkey(),
229 $recentChange->getAttribute( 'rc_title' )
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' ) );
239 * @covers Revision::insertOn
241 public function testInsertOn_success() {
242 $parentId = $this->testPage
->getLatest();
244 // If an ExternalStore is set don't use it.
245 $this->setMwGlobals( 'wgDefaultExternalStore', false );
247 $rev = new Revision( [
248 'page' => $this->testPage
->getId(),
249 'title' => $this->testPage
->getTitle(),
250 'text' => 'Revision Text',
251 'comment' => 'Revision comment',
254 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
256 $this->assertInternalType( 'integer', $revId );
257 $this->assertSame( $revId, $rev->getId() );
259 // getTextId() must be an int!
260 $this->assertInternalType( 'integer', $rev->getTextId() );
262 $mainSlot = $rev->getRevisionRecord()->getSlot( SlotRecord
::MAIN
, RevisionRecord
::RAW
);
264 // we currently only support storage in the text table
265 $textId = MediaWikiServices
::getInstance()
267 ->getTextIdFromAddress( $mainSlot->getAddress() );
271 [ 'old_id', 'old_text' ],
273 [ [ strval( $textId ), 'Revision Text' ] ]
286 "rev_id = {$rev->getId()}",
288 strval( $rev->getId() ),
289 strval( $this->testPage
->getId() ),
294 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
299 public function provideInsertOn_exceptionOnIncomplete() {
300 $content = new TextContent( '' );
301 $user = User
::newFromName( 'Foo' );
303 yield
'no parent' => [
305 'content' => $content,
309 IncompleteRevisionException
::class,
310 "rev_page field must not be 0!"
313 yield
'no comment' => [
315 'content' => $content,
319 IncompleteRevisionException
::class,
320 "comment must not be NULL!"
323 yield
'no content' => [
329 IncompleteRevisionException
::class,
330 "Uninitialized field: content_address" // XXX: message may change
335 * @dataProvider provideInsertOn_exceptionOnIncomplete
336 * @covers Revision::insertOn
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 );
343 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
344 $rev = new Revision( $array, 0, $title );
346 $rev->insertOn( wfGetDB( DB_MASTER
) );
350 * @covers Revision::newFromTitle
352 public function testNewFromTitle_withoutId() {
353 $latestRevId = $this->testPage
->getLatest();
355 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
357 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
358 $this->assertEquals( $latestRevId, $rev->getId() );
362 * @covers Revision::newFromTitle
364 public function testNewFromTitle_withId() {
365 $latestRevId = $this->testPage
->getLatest();
367 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
369 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
370 $this->assertEquals( $latestRevId, $rev->getId() );
374 * @covers Revision::newFromTitle
376 public function testNewFromTitle_withBadId() {
377 $latestRevId = $this->testPage
->getLatest();
379 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
381 $this->assertNull( $rev );
385 * @covers Revision::newFromRow
387 public function testNewFromRow() {
388 $orig = $this->makeRevisionWithProps();
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' );
396 $row = $res->fetchObject();
399 $rev = Revision
::newFromRow( $row );
401 $this->assertRevEquals( $orig, $rev );
404 public function provideNewFromArchiveRow() {
412 return $f +
[ 'ar_namespace', 'ar_title' ];
417 unset( $f['ar_text_id'] );
423 unset( $f['ar_page_id'] );
429 unset( $f['ar_parent_id'] );
435 unset( $f['ar_rev_id'] );
441 unset( $f['ar_sha1'] );
448 * @dataProvider provideNewFromArchiveRow
449 * @covers Revision::newFromArchiveRow
451 public function testNewFromArchiveRow( $selectModifier ) {
452 $services = MediaWikiServices
::getInstance();
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()
466 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
467 $this->setService( 'RevisionStore', $store );
469 $page = $this->createPage(
470 'RevisionStorageTest_testNewFromArchiveRow',
472 CONTENT_MODEL_WIKITEXT
474 $orig = $page->getRevision();
475 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
477 $dbr = wfGetDB( DB_REPLICA
);
478 $arQuery = Revision
::getArchiveQueryInfo();
479 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
481 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
482 __METHOD__
, [], $arQuery['joins']
484 $this->assertTrue( is_object( $res ), 'query failed' );
486 $row = $res->fetchObject();
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 );
493 $this->assertRevEquals( $orig, $rev );
497 * @covers Revision::newFromArchiveRow
499 public function testNewFromArchiveRowOverrides() {
500 $page = $this->createPage(
501 'RevisionStorageTest_testNewFromArchiveRow',
503 CONTENT_MODEL_WIKITEXT
505 $orig = $page->getRevision();
506 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
508 $dbr = wfGetDB( DB_REPLICA
);
509 $arQuery = Revision
::getArchiveQueryInfo();
511 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
512 __METHOD__
, [], $arQuery['joins']
514 $this->assertTrue( is_object( $res ), 'query failed' );
516 $row = $res->fetchObject();
519 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
521 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
522 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
526 * @covers Revision::newFromId
528 public function testNewFromId() {
529 $orig = $this->testPage
->getRevision();
530 $rev = Revision
::newFromId( $orig->getId() );
531 $this->assertRevEquals( $orig, $rev );
535 * @covers Revision::newFromPageId
537 public function testNewFromPageId() {
538 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
539 $this->assertRevEquals(
540 $this->testPage
->getRevision(),
546 * @covers Revision::newFromPageId
548 public function testNewFromPageIdWithLatestId() {
549 $rev = Revision
::newFromPageId(
550 $this->testPage
->getId(),
551 $this->testPage
->getLatest()
553 $this->assertRevEquals(
554 $this->testPage
->getRevision(),
560 * @covers Revision::newFromPageId
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()
569 $this->assertRevEquals(
570 $this->testPage
->getRevision()->getPrevious(),
576 * @covers Revision::getPage
578 public function testGetPage() {
579 $page = $this->testPage
;
581 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
582 $rev = Revision
::newFromId( $orig->getId() );
584 $this->assertEquals( $page->getId(), $rev->getPage() );
588 * @covers Revision::isCurrent
590 public function testIsCurrent() {
591 $rev1 = $this->testPage
->getRevision();
593 # @todo find out if this should be true
594 # $this->assertTrue( $rev1->isCurrent() );
596 $rev1x = Revision
::newFromId( $rev1->getId() );
597 $this->assertTrue( $rev1x->isCurrent() );
599 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
600 $rev2 = $this->testPage
->getRevision();
602 # @todo find out if this should be true
603 # $this->assertTrue( $rev2->isCurrent() );
605 $rev1x = Revision
::newFromId( $rev1->getId() );
606 $this->assertFalse( $rev1x->isCurrent() );
608 $rev2x = Revision
::newFromId( $rev2->getId() );
609 $this->assertTrue( $rev2x->isCurrent() );
613 * @covers Revision::getPrevious
615 public function testGetPrevious() {
616 $oldestRevision = $this->testPage
->getOldestRevision();
617 $latestRevision = $this->testPage
->getLatest();
619 $this->assertNull( $oldestRevision->getPrevious() );
621 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
622 $newRevision = $this->testPage
->getRevision();
624 $this->assertNotNull( $newRevision->getPrevious() );
625 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
629 * @covers Revision::getNext
631 public function testGetNext() {
632 $rev1 = $this->testPage
->getRevision();
634 $this->assertNull( $rev1->getNext() );
636 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
637 $rev2 = $this->testPage
->getRevision();
639 $this->assertNotNull( $rev1->getNext() );
640 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
644 * @covers Revision::newNullRevision
646 public function testNewNullRevision() {
647 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
648 $orig = $this->testPage
->getRevision();
650 $dbw = wfGetDB( DB_MASTER
);
651 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
653 $this->assertNotEquals( $orig->getId(), $rev->getId(),
654 'new null revision should have a different id from the original revision' );
655 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
656 'new null revision should have the same text id as the original revision' );
657 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
658 'new null revision should have the same SHA1 as the original revision' );
659 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
660 'new null revision should have the same content as the original revision' );
661 $this->assertEquals( __METHOD__
, $rev->getContent()->getText() );
665 * @covers Revision::newNullRevision
667 public function testNewNullRevision_badPage() {
668 $dbw = wfGetDB( DB_MASTER
);
669 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
671 $this->assertNull( $rev );
675 * @covers Revision::insertOn
677 public function testInsertOn() {
678 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
680 $orig = $this->makeRevisionWithProps( [
684 // Make sure the revision was copied to ip_changes
685 $dbr = wfGetDB( DB_REPLICA
);
686 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
687 $row = $res->fetchObject();
689 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
691 $orig->getTimestamp(),
692 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
696 public static function provideUserWasLastToEdit() {
697 yield
'actually the last edit' => [ 3, true ];
698 yield
'not the current edit, but still by this user' => [ 2, true ];
699 yield
'edit by another user' => [ 1, false ];
700 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
704 * @covers Revision::userWasLastToEdit
705 * @dataProvider provideUserWasLastToEdit
707 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
708 $userA = User
::newFromName( "RevisionStorageTest_userA" );
709 $userB = User
::newFromName( "RevisionStorageTest_userB" );
711 if ( $userA->getId() === 0 ) {
712 $userA = User
::createNew( $userA->getName() );
715 if ( $userB->getId() === 0 ) {
716 $userB = User
::createNew( $userB->getName() );
719 $ns = $this->getDefaultWikitextNS();
721 $dbw = wfGetDB( DB_MASTER
);
724 // create revisions -----------------------------
725 $page = WikiPage
::factory( Title
::newFromText(
726 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
727 $page->insertOn( $dbw );
729 $revisions[0] = new Revision( [
730 'page' => $page->getId(),
731 // we need the title to determine the page's default content model
732 'title' => $page->getTitle(),
733 'timestamp' => '20120101000000',
734 'user' => $userA->getId(),
736 'content_model' => CONTENT_MODEL_WIKITEXT
,
737 'comment' => 'edit zero'
739 $revisions[0]->insertOn( $dbw );
741 $revisions[1] = new Revision( [
742 'page' => $page->getId(),
743 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
744 'title' => $page->getTitle(),
745 'timestamp' => '20120101000100',
746 'user' => $userA->getId(),
748 'content_model' => CONTENT_MODEL_WIKITEXT
,
749 'comment' => 'edit one'
751 $revisions[1]->insertOn( $dbw );
753 $revisions[2] = new Revision( [
754 'page' => $page->getId(),
755 'title' => $page->getTitle(),
756 'timestamp' => '20120101000200',
757 'user' => $userB->getId(),
759 'content_model' => CONTENT_MODEL_WIKITEXT
,
760 'comment' => 'edit two'
762 $revisions[2]->insertOn( $dbw );
764 $revisions[3] = new Revision( [
765 'page' => $page->getId(),
766 'title' => $page->getTitle(),
767 'timestamp' => '20120101000300',
768 'user' => $userA->getId(),
770 'content_model' => CONTENT_MODEL_WIKITEXT
,
771 'comment' => 'edit three'
773 $revisions[3]->insertOn( $dbw );
775 $revisions[4] = new Revision( [
776 'page' => $page->getId(),
777 'title' => $page->getTitle(),
778 'timestamp' => '20120101000200',
779 'user' => $userA->getId(),
781 'content_model' => CONTENT_MODEL_WIKITEXT
,
782 'comment' => 'edit four'
784 $revisions[4]->insertOn( $dbw );
786 // test it ---------------------------------
787 $since = $revisions[$sinceIdx]->getTimestamp();
789 $revQuery = Revision
::getQueryInfo();
790 $allRows = iterator_to_array( $dbw->select(
792 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
794 'rev_page' => $page->getId(),
795 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
798 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
802 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
804 $this->assertEquals( $expectedLast, $wasLast );
808 * @param string $text
809 * @param string $title
810 * @param string $model
811 * @param string $format
815 private function newTestRevision( $text, $title = "Test",
816 $model = CONTENT_MODEL_WIKITEXT
, $format = null
818 if ( is_string( $title ) ) {
819 $title = Title
::newFromText( $title );
822 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
830 'content' => $content,
831 'length' => $content->getSize(),
832 'comment' => "testing",
833 'minor_edit' => false,
835 'content_format' => $format,
842 public function provideGetContentModel() {
843 // NOTE: we expect the help namespace to always contain wikitext
845 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
846 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
847 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
852 * @dataProvider provideGetContentModel
853 * @covers Revision::getContentModel
855 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
856 $rev = $this->newTestRevision( $text, $title, $model, $format );
858 $this->assertEquals( $expectedModel, $rev->getContentModel() );
861 public function provideGetContentFormat() {
862 // NOTE: we expect the help namespace to always contain wikitext
864 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
865 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
866 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
867 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
872 * @dataProvider provideGetContentFormat
873 * @covers Revision::getContentFormat
875 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
876 $rev = $this->newTestRevision( $text, $title, $model, $format );
878 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
881 public function provideGetContentHandler() {
882 // NOTE: we expect the help namespace to always contain wikitext
884 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
885 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
886 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
891 * @dataProvider provideGetContentHandler
892 * @covers Revision::getContentHandler
894 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
895 $rev = $this->newTestRevision( $text, $title, $model, $format );
897 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
900 public function provideGetContent() {
901 // NOTE: we expect the help namespace to always contain wikitext
903 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
905 serialize( 'hello world' ),
907 DummyContentForTesting
::MODEL_ID
,
909 Revision
::FOR_PUBLIC
,
910 serialize( 'hello world' )
913 serialize( 'hello world' ),
917 Revision
::FOR_PUBLIC
,
918 serialize( 'hello world' )
924 * @dataProvider provideGetContent
925 * @covers Revision::getContent
927 public function testGetContent( $text, $title, $model, $format,
928 $audience, $expectedSerialization
930 $rev = $this->newTestRevision( $text, $title, $model, $format );
931 $content = $rev->getContent( $audience );
934 $expectedSerialization,
935 is_null( $content ) ?
null : $content->serialize( $format )
940 * @covers Revision::getContent
942 public function testGetContent_failure() {
943 $rev = new Revision( [
944 'page' => $this->testPage
->getId(),
945 'content_model' => $this->testPage
->getContentModel(),
946 'id' => 123456789, // not in the test DB
949 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
951 $this->assertNull( $rev->getContent(),
952 "getContent() should return null if the revision's text blob could not be loaded." );
954 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
955 $this->assertNull( $rev->getContent(),
956 "getContent() should return null if the revision's text blob could not be loaded." );
958 Wikimedia\restoreWarnings
();
961 public function provideGetSize() {
963 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
964 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
969 * @covers Revision::getSize
970 * @dataProvider provideGetSize
972 public function testGetSize( $text, $model, $expected_size ) {
973 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
974 $this->assertEquals( $expected_size, $rev->getSize() );
977 public function provideGetSha1() {
979 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
981 serialize( "hello world." ),
982 DummyContentForTesting
::MODEL_ID
,
983 Revision
::base36Sha1( serialize( "hello world." ) )
989 * @covers Revision::getSha1
990 * @dataProvider provideGetSha1
992 public function testGetSha1( $text, $model, $expected_hash ) {
993 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
994 $this->assertEquals( $expected_hash, $rev->getSha1() );
998 * Tests whether $rev->getContent() returns a clone when needed.
1000 * @covers Revision::getContent
1002 public function testGetContentClone() {
1003 $content = new RevisionTestModifyableContent( "foo" );
1005 $rev = new Revision(
1009 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1011 'content' => $content,
1012 'length' => $content->getSize(),
1013 'comment' => "testing",
1014 'minor_edit' => false,
1018 /** @var RevisionTestModifyableContent $content */
1019 $content = $rev->getContent( Revision
::RAW
);
1020 $content->setText( "bar" );
1022 /** @var RevisionTestModifyableContent $content2 */
1023 $content2 = $rev->getContent( Revision
::RAW
);
1024 // content is mutable, expect clone
1025 $this->assertNotSame( $content, $content2, "expected a clone" );
1026 // clone should contain the original text
1027 $this->assertEquals( "foo", $content2->getText() );
1029 $content2->setText( "bla bla" );
1030 // clones should be independent
1031 $this->assertEquals( "bar", $content->getText() );
1035 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1036 * @covers Revision::getContent
1038 public function testGetContentUncloned() {
1039 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1040 $content = $rev->getContent( Revision
::RAW
);
1041 $content2 = $rev->getContent( Revision
::RAW
);
1043 // for immutable content like wikitext, this should be the same object
1044 $this->assertSame( $content, $content2 );
1048 * @covers Revision::loadFromId
1050 public function testLoadFromId() {
1051 $rev = $this->testPage
->getRevision();
1052 $this->hideDeprecated( 'Revision::loadFromId' );
1053 $this->assertRevEquals(
1055 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1060 * @covers Revision::loadFromPageId
1062 public function testLoadFromPageId() {
1063 $this->assertRevEquals(
1064 $this->testPage
->getRevision(),
1065 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1070 * @covers Revision::loadFromPageId
1072 public function testLoadFromPageIdWithLatestRevId() {
1073 $this->assertRevEquals(
1074 $this->testPage
->getRevision(),
1075 Revision
::loadFromPageId(
1076 wfGetDB( DB_MASTER
),
1077 $this->testPage
->getId(),
1078 $this->testPage
->getLatest()
1084 * @covers Revision::loadFromPageId
1086 public function testLoadFromPageIdWithNotLatestRevId() {
1087 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1088 $this->assertRevEquals(
1089 $this->testPage
->getRevision()->getPrevious(),
1090 Revision
::loadFromPageId(
1091 wfGetDB( DB_MASTER
),
1092 $this->testPage
->getId(),
1093 $this->testPage
->getRevision()->getPrevious()->getId()
1099 * @covers Revision::loadFromTitle
1101 public function testLoadFromTitle() {
1102 $this->assertRevEquals(
1103 $this->testPage
->getRevision(),
1104 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1109 * @covers Revision::loadFromTitle
1111 public function testLoadFromTitleWithLatestRevId() {
1112 $this->assertRevEquals(
1113 $this->testPage
->getRevision(),
1114 Revision
::loadFromTitle(
1115 wfGetDB( DB_MASTER
),
1116 $this->testPage
->getTitle(),
1117 $this->testPage
->getLatest()
1123 * @covers Revision::loadFromTitle
1125 public function testLoadFromTitleWithNotLatestRevId() {
1126 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1127 $this->assertRevEquals(
1128 $this->testPage
->getRevision()->getPrevious(),
1129 Revision
::loadFromTitle(
1130 wfGetDB( DB_MASTER
),
1131 $this->testPage
->getTitle(),
1132 $this->testPage
->getRevision()->getPrevious()->getId()
1138 * @covers Revision::loadFromTimestamp()
1140 public function testLoadFromTimestamp() {
1141 $this->assertRevEquals(
1142 $this->testPage
->getRevision(),
1143 Revision
::loadFromTimestamp(
1144 wfGetDB( DB_MASTER
),
1145 $this->testPage
->getTitle(),
1146 $this->testPage
->getRevision()->getTimestamp()
1152 * @covers Revision::getParentLengths
1154 public function testGetParentLengths_noRevIds() {
1157 Revision
::getParentLengths(
1158 wfGetDB( DB_MASTER
),
1165 * @covers Revision::getParentLengths
1167 public function testGetParentLengths_oneRevId() {
1168 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1169 $textLength = strlen( $text );
1171 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1172 $rev[1] = $this->testPage
->getLatest();
1175 [ $rev[1] => $textLength ],
1176 Revision
::getParentLengths(
1177 wfGetDB( DB_MASTER
),
1184 * @covers Revision::getParentLengths
1186 public function testGetParentLengths_multipleRevIds() {
1187 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1188 $textOneLength = strlen( $textOne );
1189 $textTwo = '831jr091jr092121j09rj1';
1190 $textTwoLength = strlen( $textTwo );
1192 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1193 $rev[1] = $this->testPage
->getLatest();
1194 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1195 $rev[2] = $this->testPage
->getLatest();
1198 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1199 Revision
::getParentLengths(
1200 wfGetDB( DB_MASTER
),
1201 [ $rev[1], $rev[2] ]
1207 * @covers Revision::getTitle
1209 public function testGetTitle_fromExistingRevision() {
1211 $this->testPage
->getTitle()->equals(
1212 $this->testPage
->getRevision()->getTitle()
1218 * @covers Revision::getTitle
1220 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1221 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1223 $this->testPage
->getTitle()->equals(
1230 * @covers Revision::isMinor
1232 public function testIsMinor_true() {
1233 // Use a sysop to ensure we can mark edits as minor
1234 $sysop = $this->getTestSysop()->getUser();
1236 $this->testPage
->doEditContent(
1237 new WikitextContent( __METHOD__
),
1243 $rev = $this->testPage
->getRevision();
1245 $this->assertSame( true, $rev->isMinor() );
1249 * @covers Revision::isMinor
1251 public function testIsMinor_false() {
1252 $this->testPage
->doEditContent(
1253 new WikitextContent( __METHOD__
),
1257 $rev = $this->testPage
->getRevision();
1259 $this->assertSame( false, $rev->isMinor() );
1263 * @covers Revision::getTimestamp
1265 public function testGetTimestamp() {
1266 $testTimestamp = wfTimestampNow();
1268 $this->testPage
->doEditContent(
1269 new WikitextContent( __METHOD__
),
1272 $rev = $this->testPage
->getRevision();
1274 $this->assertInternalType( 'string', $rev->getTimestamp() );
1275 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1276 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1280 * @covers Revision::getUser
1281 * @covers Revision::getUserText
1283 public function testGetUserAndText() {
1284 $sysop = $this->getTestSysop()->getUser();
1286 $this->testPage
->doEditContent(
1287 new WikitextContent( __METHOD__
),
1293 $rev = $this->testPage
->getRevision();
1295 $this->assertSame( $sysop->getId(), $rev->getUser() );
1296 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1300 * @covers Revision::isDeleted
1302 public function testIsDeleted_nothingDeleted() {
1303 $rev = $this->testPage
->getRevision();
1305 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1306 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1307 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1308 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1312 * @covers Revision::getVisibility
1314 public function testGetVisibility_nothingDeleted() {
1315 $rev = $this->testPage
->getRevision();
1317 $this->assertSame( 0, $rev->getVisibility() );
1321 * @covers Revision::getComment
1323 public function testGetComment_notDeleted() {
1324 $expectedSummary = 'goatlicious summary';
1326 $this->testPage
->doEditContent(
1327 new WikitextContent( __METHOD__
),
1330 $rev = $this->testPage
->getRevision();
1332 $this->assertSame( $expectedSummary, $rev->getComment() );
1336 * @covers Revision::isUnpatrolled
1338 public function testIsUnpatrolled_returnsRecentChangesId() {
1339 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1340 $rev = $this->testPage
->getRevision();
1342 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1343 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1347 * @covers Revision::isUnpatrolled
1349 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1350 // This assumes that sysops are auto patrolled
1351 $sysop = $this->getTestSysop()->getUser();
1352 $this->testPage
->doEditContent(
1353 new WikitextContent( __METHOD__
),
1359 $rev = $this->testPage
->getRevision();
1361 $this->assertSame( 0, $rev->isUnpatrolled() );
1365 * This is a simple blanket test for all simple content getters and is methods to provide some
1366 * coverage before the split of Revision into multiple classes for MCR work.
1367 * @covers Revision::getContent
1368 * @covers Revision::getSerializedData
1369 * @covers Revision::getContentModel
1370 * @covers Revision::getContentFormat
1371 * @covers Revision::getContentHandler
1373 public function testSimpleContentGetters() {
1374 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1375 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1377 $this->testPage
->doEditContent(
1378 new WikitextContent( $expectedText ),
1381 $rev = $this->testPage
->getRevision();
1383 $this->assertSame( $expectedText, $rev->getContent()->getText() );
1384 $this->assertSame( $expectedText, $rev->getSerializedData() );
1385 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1386 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1387 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1391 * @covers Revision::newKnownCurrent
1393 public function testNewKnownCurrent() {
1394 // Setup the services
1395 $this->overrideMwServices();
1396 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1397 $this->setService( 'MainWANObjectCache', $cache );
1398 $db = wfGetDB( DB_MASTER
);
1401 $cache->setMockTime( $now );
1403 // Get a fresh revision to use during testing
1404 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1405 $rev = $this->testPage
->getRevision();
1407 // Clear any previous cache for the revision during creation
1408 $key = $cache->makeGlobalKey(
1409 RevisionStore
::ROW_CACHE_KEY
,
1414 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1415 $this->assertFalse( $cache->get( $key ) );
1419 // Get the new revision and make sure it is in the cache and correct
1420 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1421 $this->assertRevEquals( $rev, $newRev );
1423 $cachedRow = $cache->get( $key );
1424 $this->assertNotFalse( $cachedRow );
1425 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1428 public function testNewKnownCurrent_withPageId() {
1429 $db = wfGetDB( DB_MASTER
);
1431 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1432 $rev = $this->testPage
->getRevision();
1434 $pageId = $this->testPage
->getId();
1436 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1437 $this->assertRevEquals( $rev, $newRev );
1440 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1441 $db = wfGetDB( DB_MASTER
);
1443 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1446 public function provideUserCanBitfield() {
1447 yield
[ 0, 0, [], null, true ];
1448 // Bitfields match, user has no permissions
1449 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1450 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1451 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1452 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1453 // Bitfields match, user (admin) does have permissions
1454 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1455 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1456 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1457 // Bitfields match, user (admin) does not have permissions
1458 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1459 // Bitfields match, user (oversight) does have permissions
1460 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1461 // Check permissions using the title
1463 Revision
::DELETED_TEXT
,
1464 Revision
::DELETED_TEXT
,
1470 Revision
::DELETED_TEXT
,
1471 Revision
::DELETED_TEXT
,
1479 * @dataProvider provideUserCanBitfield
1480 * @covers Revision::userCanBitfield
1482 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1483 $title = Title
::newFromText( $title );
1485 $this->setMwGlobals(
1486 'wgGroupPermissions',
1489 'deletedtext' => true,
1490 'deletedhistory' => true,
1493 'viewsuppressed' => true,
1494 'suppressrevision' => true,
1498 $user = $this->getTestUser( $userGroups )->getUser();
1502 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1505 // Fallback to $wgUser
1506 $this->setMwGlobals(
1512 Revision
::userCanBitfield( $bitField, $field, null, $title )
1516 public function provideUserCan() {
1517 yield
[ 0, 0, [], true ];
1518 // Bitfields match, user has no permissions
1519 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1520 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1521 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1522 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1523 // Bitfields match, user (admin) does have permissions
1524 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1525 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1526 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1527 // Bitfields match, user (admin) does not have permissions
1528 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1529 // Bitfields match, user (oversight) does have permissions
1530 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1534 * @dataProvider provideUserCan
1535 * @covers Revision::userCan
1537 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1538 $this->setMwGlobals(
1539 'wgGroupPermissions',
1542 'deletedtext' => true,
1543 'deletedhistory' => true,
1546 'viewsuppressed' => true,
1547 'suppressrevision' => true,
1551 $user = $this->getTestUser( $userGroups )->getUser();
1552 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1556 $revision->userCan( $field, $user )
1560 public function provideGetTextId() {
1563 $slot = new SlotRecord( (object)[
1564 'slot_revision_id' => 42,
1565 'slot_content_id' => 1,
1566 'content_address' => 'tt:789',
1567 'model_name' => CONTENT_MODEL_WIKITEXT
,
1568 'role_name' => SlotRecord
::MAIN
,
1570 ], new WikitextContent( 'Test' ) );
1572 $rec = new MutableRevisionRecord( $this->testPage
->getTitle() );
1574 $rec->setSlot( $slot );
1576 yield
[ $rec, 789 ];
1580 * @dataProvider provideGetTextId
1581 * @covers Revision::getTextId()
1583 public function testGetTextId( $spec, $expected ) {
1584 $rev = new Revision( $spec, 0, $this->testPage
->getTitle() );
1585 $this->assertSame( $expected, $rev->getTextId() );
1588 abstract public function provideGetRevisionText();
1591 * @dataProvider provideGetRevisionText
1592 * @covers Revision::getRevisionText
1594 public function testGetRevisionText( array $queryInfoOptions, array $queryInfoExtra = [] ) {
1595 $rev = $this->testPage
->getRevisionRecord();
1597 $queryInfo = Revision
::getQueryInfo( $queryInfoOptions );
1598 $queryInfo['tables'] = array_merge( $queryInfo['tables'], $queryInfoExtra['tables'] ??
[] );
1599 $queryInfo['fields'] = array_merge( $queryInfo['fields'], $queryInfoExtra['fields'] ??
[] );
1600 $queryInfo['joins'] = array_merge( $queryInfo['joins'], $queryInfoExtra['joins'] ??
[] );
1602 $conds = [ 'rev_id' => $rev->getId() ];
1603 $row = $this->db
->selectRow(
1604 $queryInfo['tables'],
1605 $queryInfo['fields'],
1612 $expected = $rev->getContent( SlotRecord
::MAIN
)->serialize();
1614 $this->hideDeprecated( 'Revision::getRevisionText (MCR without SCHEMA_COMPAT_WRITE_OLD)' );
1615 $this->assertSame( $expected, Revision
::getRevisionText( $row ) );