2 use MediaWiki\MediaWikiServices
;
3 use MediaWiki\Storage\RevisionStore
;
4 use MediaWiki\Storage\IncompleteRevisionException
;
5 use MediaWiki\Storage\RevisionRecord
;
8 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
13 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
16 * @var WikiPage $testPage
20 public function __construct( $name = null, array $data = [], $dataName = '' ) {
21 parent
::__construct( $name, $data, $dataName );
23 $this->tablesUsed
= array_merge( $this->tablesUsed
,
46 protected function addCoreDBData() {
47 // Blank out. This would fail with a modified schema, and we don't need it.
53 abstract protected function getMcrMigrationStage();
58 abstract protected function getMcrTablesToReset();
60 protected function setUp() {
61 $this->tablesUsed +
= $this->getMcrTablesToReset();
65 $this->mergeMwGlobalArrayValue(
69 12313 => 'Dummy_talk',
73 $this->mergeMwGlobalArrayValue(
74 'wgNamespaceContentModels',
76 12312 => DummyContentForTesting
::MODEL_ID
,
80 $this->mergeMwGlobalArrayValue(
83 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
84 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
88 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
90 'wgMultiContentRevisionSchemaMigrationStage',
91 $this->getMcrMigrationStage()
94 $this->overrideMwServices();
96 if ( !$this->testPage
) {
98 * We have to create a new page for each subclass as the page creation may result
99 * in different DB fields being filled based on configuration.
101 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
105 abstract protected function getContentHandlerUseDB();
107 private function makeRevisionWithProps( $props = null ) {
108 if ( $props === null ) {
112 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
113 $props['text'] = 'Lorem Ipsum';
116 if ( !isset( $props['user_text'] ) ) {
117 $user = $this->getTestUser()->getUser();
118 $props['user_text'] = $user->getName();
119 $props['user'] = $user->getId();
122 if ( !isset( $props['user'] ) ) {
126 if ( !isset( $props['comment'] ) ) {
127 $props['comment'] = 'just a test';
130 if ( !isset( $props['page'] ) ) {
131 $props['page'] = $this->testPage
->getId();
134 if ( !isset( $props['content_model'] ) ) {
135 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
138 $rev = new Revision( $props );
140 $dbw = wfGetDB( DB_MASTER
);
141 $rev->insertOn( $dbw );
147 * @param string $titleString
148 * @param string $text
149 * @param string|null $model
153 private function createPage( $titleString, $text, $model = null ) {
154 if ( !preg_match( '/:/', $titleString ) &&
155 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
157 $ns = $this->getDefaultWikitextNS();
158 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
161 $title = Title
::newFromText( $titleString );
162 $wikipage = new WikiPage( $title );
164 // Delete the article if it already exists
165 if ( $wikipage->exists() ) {
166 $wikipage->doDeleteArticle( "done" );
169 $content = ContentHandler
::makeContent( $text, $title, $model );
170 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
175 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
176 $this->assertNotNull( $rev, 'missing revision' );
178 $this->assertEquals( $orig->getId(), $rev->getId() );
179 $this->assertEquals( $orig->getPage(), $rev->getPage() );
180 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
181 $this->assertEquals( $orig->getUser(), $rev->getUser() );
182 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
183 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
184 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
188 * @covers Revision::getRecentChange
190 public function testGetRecentChange() {
191 $rev = $this->testPage
->getRevision();
192 $recentChange = $rev->getRecentChange();
194 // Make sure various attributes look right / the correct entry has been retrieved.
195 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
197 $rev->getTitle()->getNamespace(),
198 $recentChange->getAttribute( 'rc_namespace' )
201 $rev->getTitle()->getDBkey(),
202 $recentChange->getAttribute( 'rc_title' )
204 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
205 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
206 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
207 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
208 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
212 * @covers Revision::insertOn
214 public function testInsertOn_success() {
215 $parentId = $this->testPage
->getLatest();
217 // If an ExternalStore is set don't use it.
218 $this->setMwGlobals( 'wgDefaultExternalStore', false );
220 $rev = new Revision( [
221 'page' => $this->testPage
->getId(),
222 'title' => $this->testPage
->getTitle(),
223 'text' => 'Revision Text',
224 'comment' => 'Revision comment',
227 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
229 $this->assertInternalType( 'integer', $revId );
230 $this->assertSame( $revId, $rev->getId() );
232 // getTextId() must be an int!
233 $this->assertInternalType( 'integer', $rev->getTextId() );
235 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
237 // we currently only support storage in the text table
238 $textId = MediaWikiServices
::getInstance()
240 ->getTextIdFromAddress( $mainSlot->getAddress() );
244 [ 'old_id', 'old_text' ],
246 [ [ strval( $textId ), 'Revision Text' ] ]
259 "rev_id = {$rev->getId()}",
261 strval( $rev->getId() ),
262 strval( $this->testPage
->getId() ),
267 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
272 public function provideInsertOn_exceptionOnIncomplete() {
273 $content = new TextContent( '' );
274 $user = User
::newFromName( 'Foo' );
276 yield
'no parent' => [
278 'content' => $content,
282 IncompleteRevisionException
::class,
283 "rev_page field must not be 0!"
286 yield
'no comment' => [
288 'content' => $content,
292 IncompleteRevisionException
::class,
293 "comment must not be NULL!"
296 yield
'no content' => [
302 IncompleteRevisionException
::class,
303 "Uninitialized field: content_address" // XXX: message may change
308 * @dataProvider provideInsertOn_exceptionOnIncomplete
309 * @covers Revision::insertOn
311 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
312 // If an ExternalStore is set don't use it.
313 $this->setMwGlobals( 'wgDefaultExternalStore', false );
314 $this->setExpectedException( $expException, $expMessage );
316 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
317 $rev = new Revision( $array, 0, $title );
319 $rev->insertOn( wfGetDB( DB_MASTER
) );
323 * @covers Revision::newFromTitle
325 public function testNewFromTitle_withoutId() {
326 $latestRevId = $this->testPage
->getLatest();
328 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
330 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
331 $this->assertEquals( $latestRevId, $rev->getId() );
335 * @covers Revision::newFromTitle
337 public function testNewFromTitle_withId() {
338 $latestRevId = $this->testPage
->getLatest();
340 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
342 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
343 $this->assertEquals( $latestRevId, $rev->getId() );
347 * @covers Revision::newFromTitle
349 public function testNewFromTitle_withBadId() {
350 $latestRevId = $this->testPage
->getLatest();
352 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
354 $this->assertNull( $rev );
358 * @covers Revision::newFromRow
360 public function testNewFromRow() {
361 $orig = $this->makeRevisionWithProps();
363 $dbr = wfGetDB( DB_REPLICA
);
364 $revQuery = Revision
::getQueryInfo();
365 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
366 __METHOD__
, [], $revQuery['joins'] );
367 $this->assertTrue( is_object( $res ), 'query failed' );
369 $row = $res->fetchObject();
372 $rev = Revision
::newFromRow( $row );
374 $this->assertRevEquals( $orig, $rev );
377 public function provideNewFromArchiveRow() {
385 return $f +
[ 'ar_namespace', 'ar_title' ];
390 unset( $f['ar_text_id'] );
396 unset( $f['ar_page_id'] );
402 unset( $f['ar_parent_id'] );
408 unset( $f['ar_rev_id'] );
414 unset( $f['ar_sha1'] );
421 * @dataProvider provideNewFromArchiveRow
422 * @covers Revision::newFromArchiveRow
424 public function testNewFromArchiveRow( $selectModifier ) {
425 $services = MediaWikiServices
::getInstance();
427 $store = new RevisionStore(
428 $services->getDBLoadBalancer(),
429 $services->getService( '_SqlBlobStore' ),
430 $services->getMainWANObjectCache(),
431 $services->getCommentStore(),
432 $services->getContentModelStore(),
433 $services->getSlotRoleStore(),
434 $this->getMcrMigrationStage(),
435 $services->getActorMigration()
438 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
439 $this->setService( 'RevisionStore', $store );
441 $page = $this->createPage(
442 'RevisionStorageTest_testNewFromArchiveRow',
444 CONTENT_MODEL_WIKITEXT
446 $orig = $page->getRevision();
447 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
449 $dbr = wfGetDB( DB_REPLICA
);
450 $arQuery = Revision
::getArchiveQueryInfo();
451 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
453 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
454 __METHOD__
, [], $arQuery['joins']
456 $this->assertTrue( is_object( $res ), 'query failed' );
458 $row = $res->fetchObject();
461 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
462 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
463 $rev = Revision
::newFromArchiveRow( $row );
465 $this->assertRevEquals( $orig, $rev );
469 * @covers Revision::newFromArchiveRow
471 public function testNewFromArchiveRowOverrides() {
472 $page = $this->createPage(
473 'RevisionStorageTest_testNewFromArchiveRow',
475 CONTENT_MODEL_WIKITEXT
477 $orig = $page->getRevision();
478 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
480 $dbr = wfGetDB( DB_REPLICA
);
481 $arQuery = Revision
::getArchiveQueryInfo();
483 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
484 __METHOD__
, [], $arQuery['joins']
486 $this->assertTrue( is_object( $res ), 'query failed' );
488 $row = $res->fetchObject();
491 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
493 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
494 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
498 * @covers Revision::newFromId
500 public function testNewFromId() {
501 $orig = $this->testPage
->getRevision();
502 $rev = Revision
::newFromId( $orig->getId() );
503 $this->assertRevEquals( $orig, $rev );
507 * @covers Revision::newFromPageId
509 public function testNewFromPageId() {
510 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
511 $this->assertRevEquals(
512 $this->testPage
->getRevision(),
518 * @covers Revision::newFromPageId
520 public function testNewFromPageIdWithLatestId() {
521 $rev = Revision
::newFromPageId(
522 $this->testPage
->getId(),
523 $this->testPage
->getLatest()
525 $this->assertRevEquals(
526 $this->testPage
->getRevision(),
532 * @covers Revision::newFromPageId
534 public function testNewFromPageIdWithNotLatestId() {
535 $content = new WikitextContent( __METHOD__
);
536 $this->testPage
->doEditContent( $content, __METHOD__
);
537 $rev = Revision
::newFromPageId(
538 $this->testPage
->getId(),
539 $this->testPage
->getRevision()->getPrevious()->getId()
541 $this->assertRevEquals(
542 $this->testPage
->getRevision()->getPrevious(),
548 * @covers Revision::fetchRevision
550 public function testFetchRevision() {
551 // Hidden process cache assertion below
552 $this->testPage
->getRevision()->getId();
554 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
555 $id = $this->testPage
->getRevision()->getId();
557 $this->hideDeprecated( 'Revision::fetchRevision' );
558 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
560 # note: order is unspecified
562 while ( ( $row = $res->fetchObject() ) ) {
563 $rows[$row->rev_id
] = $row;
566 $this->assertEmpty( $rows, 'expected empty set' );
570 * @covers Revision::getPage
572 public function testGetPage() {
573 $page = $this->testPage
;
575 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
576 $rev = Revision
::newFromId( $orig->getId() );
578 $this->assertEquals( $page->getId(), $rev->getPage() );
582 * @covers Revision::isCurrent
584 public function testIsCurrent() {
585 $rev1 = $this->testPage
->getRevision();
587 # @todo find out if this should be true
588 # $this->assertTrue( $rev1->isCurrent() );
590 $rev1x = Revision
::newFromId( $rev1->getId() );
591 $this->assertTrue( $rev1x->isCurrent() );
593 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
594 $rev2 = $this->testPage
->getRevision();
596 # @todo find out if this should be true
597 # $this->assertTrue( $rev2->isCurrent() );
599 $rev1x = Revision
::newFromId( $rev1->getId() );
600 $this->assertFalse( $rev1x->isCurrent() );
602 $rev2x = Revision
::newFromId( $rev2->getId() );
603 $this->assertTrue( $rev2x->isCurrent() );
607 * @covers Revision::getPrevious
609 public function testGetPrevious() {
610 $oldestRevision = $this->testPage
->getOldestRevision();
611 $latestRevision = $this->testPage
->getLatest();
613 $this->assertNull( $oldestRevision->getPrevious() );
615 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
616 $newRevision = $this->testPage
->getRevision();
618 $this->assertNotNull( $newRevision->getPrevious() );
619 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
623 * @covers Revision::getNext
625 public function testGetNext() {
626 $rev1 = $this->testPage
->getRevision();
628 $this->assertNull( $rev1->getNext() );
630 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
631 $rev2 = $this->testPage
->getRevision();
633 $this->assertNotNull( $rev1->getNext() );
634 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
638 * @covers Revision::newNullRevision
640 public function testNewNullRevision() {
641 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
642 $orig = $this->testPage
->getRevision();
644 $dbw = wfGetDB( DB_MASTER
);
645 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
647 $this->assertNotEquals( $orig->getId(), $rev->getId(),
648 'new null revision should have a different id from the original revision' );
649 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
650 'new null revision should have the same text id as the original revision' );
651 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
652 'new null revision should have the same SHA1 as the original revision' );
653 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
654 'new null revision should have the same content as the original revision' );
655 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
659 * @covers Revision::newNullRevision
661 public function testNewNullRevision_badPage() {
662 $dbw = wfGetDB( DB_MASTER
);
663 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
665 $this->assertNull( $rev );
669 * @covers Revision::insertOn
671 public function testInsertOn() {
672 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
674 $orig = $this->makeRevisionWithProps( [
678 // Make sure the revision was copied to ip_changes
679 $dbr = wfGetDB( DB_REPLICA
);
680 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
681 $row = $res->fetchObject();
683 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
685 $orig->getTimestamp(),
686 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
690 public static function provideUserWasLastToEdit() {
691 yield
'actually the last edit' => [ 3, true ];
692 yield
'not the current edit, but still by this user' => [ 2, true ];
693 yield
'edit by another user' => [ 1, false ];
694 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
698 * @covers Revision::userWasLastToEdit
699 * @dataProvider provideUserWasLastToEdit
701 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
702 $userA = User
::newFromName( "RevisionStorageTest_userA" );
703 $userB = User
::newFromName( "RevisionStorageTest_userB" );
705 if ( $userA->getId() === 0 ) {
706 $userA = User
::createNew( $userA->getName() );
709 if ( $userB->getId() === 0 ) {
710 $userB = User
::createNew( $userB->getName() );
713 $ns = $this->getDefaultWikitextNS();
715 $dbw = wfGetDB( DB_MASTER
);
718 // create revisions -----------------------------
719 $page = WikiPage
::factory( Title
::newFromText(
720 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
721 $page->insertOn( $dbw );
723 $revisions[0] = new Revision( [
724 'page' => $page->getId(),
725 // we need the title to determine the page's default content model
726 'title' => $page->getTitle(),
727 'timestamp' => '20120101000000',
728 'user' => $userA->getId(),
730 'content_model' => CONTENT_MODEL_WIKITEXT
,
731 'comment' => 'edit zero'
733 $revisions[0]->insertOn( $dbw );
735 $revisions[1] = new Revision( [
736 'page' => $page->getId(),
737 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
738 'title' => $page->getTitle(),
739 'timestamp' => '20120101000100',
740 'user' => $userA->getId(),
742 'content_model' => CONTENT_MODEL_WIKITEXT
,
743 'comment' => 'edit one'
745 $revisions[1]->insertOn( $dbw );
747 $revisions[2] = new Revision( [
748 'page' => $page->getId(),
749 'title' => $page->getTitle(),
750 'timestamp' => '20120101000200',
751 'user' => $userB->getId(),
753 'content_model' => CONTENT_MODEL_WIKITEXT
,
754 'comment' => 'edit two'
756 $revisions[2]->insertOn( $dbw );
758 $revisions[3] = new Revision( [
759 'page' => $page->getId(),
760 'title' => $page->getTitle(),
761 'timestamp' => '20120101000300',
762 'user' => $userA->getId(),
764 'content_model' => CONTENT_MODEL_WIKITEXT
,
765 'comment' => 'edit three'
767 $revisions[3]->insertOn( $dbw );
769 $revisions[4] = new Revision( [
770 'page' => $page->getId(),
771 'title' => $page->getTitle(),
772 'timestamp' => '20120101000200',
773 'user' => $userA->getId(),
775 'content_model' => CONTENT_MODEL_WIKITEXT
,
776 'comment' => 'edit four'
778 $revisions[4]->insertOn( $dbw );
780 // test it ---------------------------------
781 $since = $revisions[$sinceIdx]->getTimestamp();
783 $revQuery = Revision
::getQueryInfo();
784 $allRows = iterator_to_array( $dbw->select(
786 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
788 'rev_page' => $page->getId(),
789 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
792 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
796 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
798 $this->assertEquals( $expectedLast, $wasLast );
802 * @param string $text
803 * @param string $title
804 * @param string $model
805 * @param string $format
809 private function newTestRevision( $text, $title = "Test",
810 $model = CONTENT_MODEL_WIKITEXT
, $format = null
812 if ( is_string( $title ) ) {
813 $title = Title
::newFromText( $title );
816 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
824 'content' => $content,
825 'length' => $content->getSize(),
826 'comment' => "testing",
827 'minor_edit' => false,
829 'content_format' => $format,
836 public function provideGetContentModel() {
837 // NOTE: we expect the help namespace to always contain wikitext
839 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
840 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
841 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
846 * @dataProvider provideGetContentModel
847 * @covers Revision::getContentModel
849 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
850 $rev = $this->newTestRevision( $text, $title, $model, $format );
852 $this->assertEquals( $expectedModel, $rev->getContentModel() );
855 public function provideGetContentFormat() {
856 // NOTE: we expect the help namespace to always contain wikitext
858 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
859 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
860 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
861 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
866 * @dataProvider provideGetContentFormat
867 * @covers Revision::getContentFormat
869 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
870 $rev = $this->newTestRevision( $text, $title, $model, $format );
872 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
875 public function provideGetContentHandler() {
876 // NOTE: we expect the help namespace to always contain wikitext
878 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
879 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
880 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
885 * @dataProvider provideGetContentHandler
886 * @covers Revision::getContentHandler
888 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
889 $rev = $this->newTestRevision( $text, $title, $model, $format );
891 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
894 public function provideGetContent() {
895 // NOTE: we expect the help namespace to always contain wikitext
897 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
899 serialize( 'hello world' ),
901 DummyContentForTesting
::MODEL_ID
,
903 Revision
::FOR_PUBLIC
,
904 serialize( 'hello world' )
907 serialize( 'hello world' ),
911 Revision
::FOR_PUBLIC
,
912 serialize( 'hello world' )
918 * @dataProvider provideGetContent
919 * @covers Revision::getContent
921 public function testGetContent( $text, $title, $model, $format,
922 $audience, $expectedSerialization
924 $rev = $this->newTestRevision( $text, $title, $model, $format );
925 $content = $rev->getContent( $audience );
928 $expectedSerialization,
929 is_null( $content ) ?
null : $content->serialize( $format )
934 * @covers Revision::getContent
936 public function testGetContent_failure() {
937 $rev = new Revision( [
938 'page' => $this->testPage
->getId(),
939 'content_model' => $this->testPage
->getContentModel(),
940 'id' => 123456789, // not in the test DB
943 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
945 $this->assertNull( $rev->getContent(),
946 "getContent() should return null if the revision's text blob could not be loaded." );
948 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
949 $this->assertNull( $rev->getContent(),
950 "getContent() should return null if the revision's text blob could not be loaded." );
952 Wikimedia\restoreWarnings
();
955 public function provideGetSize() {
957 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
958 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
963 * @covers Revision::getSize
964 * @dataProvider provideGetSize
966 public function testGetSize( $text, $model, $expected_size ) {
967 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
968 $this->assertEquals( $expected_size, $rev->getSize() );
971 public function provideGetSha1() {
973 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
975 serialize( "hello world." ),
976 DummyContentForTesting
::MODEL_ID
,
977 Revision
::base36Sha1( serialize( "hello world." ) )
983 * @covers Revision::getSha1
984 * @dataProvider provideGetSha1
986 public function testGetSha1( $text, $model, $expected_hash ) {
987 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
988 $this->assertEquals( $expected_hash, $rev->getSha1() );
992 * Tests whether $rev->getContent() returns a clone when needed.
994 * @covers Revision::getContent
996 public function testGetContentClone() {
997 $content = new RevisionTestModifyableContent( "foo" );
1003 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1005 'content' => $content,
1006 'length' => $content->getSize(),
1007 'comment' => "testing",
1008 'minor_edit' => false,
1012 /** @var RevisionTestModifyableContent $content */
1013 $content = $rev->getContent( Revision
::RAW
);
1014 $content->setText( "bar" );
1016 /** @var RevisionTestModifyableContent $content2 */
1017 $content2 = $rev->getContent( Revision
::RAW
);
1018 // content is mutable, expect clone
1019 $this->assertNotSame( $content, $content2, "expected a clone" );
1020 // clone should contain the original text
1021 $this->assertEquals( "foo", $content2->getText() );
1023 $content2->setText( "bla bla" );
1024 // clones should be independent
1025 $this->assertEquals( "bar", $content->getText() );
1029 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1030 * @covers Revision::getContent
1032 public function testGetContentUncloned() {
1033 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1034 $content = $rev->getContent( Revision
::RAW
);
1035 $content2 = $rev->getContent( Revision
::RAW
);
1037 // for immutable content like wikitext, this should be the same object
1038 $this->assertSame( $content, $content2 );
1042 * @covers Revision::loadFromId
1044 public function testLoadFromId() {
1045 $rev = $this->testPage
->getRevision();
1046 $this->hideDeprecated( 'Revision::loadFromId' );
1047 $this->assertRevEquals(
1049 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1054 * @covers Revision::loadFromPageId
1056 public function testLoadFromPageId() {
1057 $this->assertRevEquals(
1058 $this->testPage
->getRevision(),
1059 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1064 * @covers Revision::loadFromPageId
1066 public function testLoadFromPageIdWithLatestRevId() {
1067 $this->assertRevEquals(
1068 $this->testPage
->getRevision(),
1069 Revision
::loadFromPageId(
1070 wfGetDB( DB_MASTER
),
1071 $this->testPage
->getId(),
1072 $this->testPage
->getLatest()
1078 * @covers Revision::loadFromPageId
1080 public function testLoadFromPageIdWithNotLatestRevId() {
1081 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1082 $this->assertRevEquals(
1083 $this->testPage
->getRevision()->getPrevious(),
1084 Revision
::loadFromPageId(
1085 wfGetDB( DB_MASTER
),
1086 $this->testPage
->getId(),
1087 $this->testPage
->getRevision()->getPrevious()->getId()
1093 * @covers Revision::loadFromTitle
1095 public function testLoadFromTitle() {
1096 $this->assertRevEquals(
1097 $this->testPage
->getRevision(),
1098 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1103 * @covers Revision::loadFromTitle
1105 public function testLoadFromTitleWithLatestRevId() {
1106 $this->assertRevEquals(
1107 $this->testPage
->getRevision(),
1108 Revision
::loadFromTitle(
1109 wfGetDB( DB_MASTER
),
1110 $this->testPage
->getTitle(),
1111 $this->testPage
->getLatest()
1117 * @covers Revision::loadFromTitle
1119 public function testLoadFromTitleWithNotLatestRevId() {
1120 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1121 $this->assertRevEquals(
1122 $this->testPage
->getRevision()->getPrevious(),
1123 Revision
::loadFromTitle(
1124 wfGetDB( DB_MASTER
),
1125 $this->testPage
->getTitle(),
1126 $this->testPage
->getRevision()->getPrevious()->getId()
1132 * @covers Revision::loadFromTimestamp()
1134 public function testLoadFromTimestamp() {
1135 $this->assertRevEquals(
1136 $this->testPage
->getRevision(),
1137 Revision
::loadFromTimestamp(
1138 wfGetDB( DB_MASTER
),
1139 $this->testPage
->getTitle(),
1140 $this->testPage
->getRevision()->getTimestamp()
1146 * @covers Revision::getParentLengths
1148 public function testGetParentLengths_noRevIds() {
1151 Revision
::getParentLengths(
1152 wfGetDB( DB_MASTER
),
1159 * @covers Revision::getParentLengths
1161 public function testGetParentLengths_oneRevId() {
1162 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1163 $textLength = strlen( $text );
1165 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1166 $rev[1] = $this->testPage
->getLatest();
1169 [ $rev[1] => $textLength ],
1170 Revision
::getParentLengths(
1171 wfGetDB( DB_MASTER
),
1178 * @covers Revision::getParentLengths
1180 public function testGetParentLengths_multipleRevIds() {
1181 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1182 $textOneLength = strlen( $textOne );
1183 $textTwo = '831jr091jr092121j09rj1';
1184 $textTwoLength = strlen( $textTwo );
1186 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1187 $rev[1] = $this->testPage
->getLatest();
1188 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1189 $rev[2] = $this->testPage
->getLatest();
1192 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1193 Revision
::getParentLengths(
1194 wfGetDB( DB_MASTER
),
1195 [ $rev[1], $rev[2] ]
1201 * @covers Revision::getTitle
1203 public function testGetTitle_fromExistingRevision() {
1205 $this->testPage
->getTitle()->equals(
1206 $this->testPage
->getRevision()->getTitle()
1212 * @covers Revision::getTitle
1214 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1215 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1217 $this->testPage
->getTitle()->equals(
1224 * @covers Revision::isMinor
1226 public function testIsMinor_true() {
1227 // Use a sysop to ensure we can mark edits as minor
1228 $sysop = $this->getTestSysop()->getUser();
1230 $this->testPage
->doEditContent(
1231 new WikitextContent( __METHOD__
),
1237 $rev = $this->testPage
->getRevision();
1239 $this->assertSame( true, $rev->isMinor() );
1243 * @covers Revision::isMinor
1245 public function testIsMinor_false() {
1246 $this->testPage
->doEditContent(
1247 new WikitextContent( __METHOD__
),
1251 $rev = $this->testPage
->getRevision();
1253 $this->assertSame( false, $rev->isMinor() );
1257 * @covers Revision::getTimestamp
1259 public function testGetTimestamp() {
1260 $testTimestamp = wfTimestampNow();
1262 $this->testPage
->doEditContent(
1263 new WikitextContent( __METHOD__
),
1266 $rev = $this->testPage
->getRevision();
1268 $this->assertInternalType( 'string', $rev->getTimestamp() );
1269 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1270 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1274 * @covers Revision::getUser
1275 * @covers Revision::getUserText
1277 public function testGetUserAndText() {
1278 $sysop = $this->getTestSysop()->getUser();
1280 $this->testPage
->doEditContent(
1281 new WikitextContent( __METHOD__
),
1287 $rev = $this->testPage
->getRevision();
1289 $this->assertSame( $sysop->getId(), $rev->getUser() );
1290 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1294 * @covers Revision::isDeleted
1296 public function testIsDeleted_nothingDeleted() {
1297 $rev = $this->testPage
->getRevision();
1299 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1300 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1301 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1302 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1306 * @covers Revision::getVisibility
1308 public function testGetVisibility_nothingDeleted() {
1309 $rev = $this->testPage
->getRevision();
1311 $this->assertSame( 0, $rev->getVisibility() );
1315 * @covers Revision::getComment
1317 public function testGetComment_notDeleted() {
1318 $expectedSummary = 'goatlicious summary';
1320 $this->testPage
->doEditContent(
1321 new WikitextContent( __METHOD__
),
1324 $rev = $this->testPage
->getRevision();
1326 $this->assertSame( $expectedSummary, $rev->getComment() );
1330 * @covers Revision::isUnpatrolled
1332 public function testIsUnpatrolled_returnsRecentChangesId() {
1333 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1334 $rev = $this->testPage
->getRevision();
1336 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1337 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1341 * @covers Revision::isUnpatrolled
1343 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1344 // This assumes that sysops are auto patrolled
1345 $sysop = $this->getTestSysop()->getUser();
1346 $this->testPage
->doEditContent(
1347 new WikitextContent( __METHOD__
),
1353 $rev = $this->testPage
->getRevision();
1355 $this->assertSame( 0, $rev->isUnpatrolled() );
1359 * This is a simple blanket test for all simple content getters and is methods to provide some
1360 * coverage before the split of Revision into multiple classes for MCR work.
1361 * @covers Revision::getContent
1362 * @covers Revision::getSerializedData
1363 * @covers Revision::getContentModel
1364 * @covers Revision::getContentFormat
1365 * @covers Revision::getContentHandler
1367 public function testSimpleContentGetters() {
1368 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1369 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1371 $this->testPage
->doEditContent(
1372 new WikitextContent( $expectedText ),
1375 $rev = $this->testPage
->getRevision();
1377 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1378 $this->assertSame( $expectedText, $rev->getSerializedData() );
1379 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1380 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1381 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1385 * @covers Revision::newKnownCurrent
1387 public function testNewKnownCurrent() {
1388 // Setup the services
1389 $this->overrideMwServices();
1390 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1391 $this->setService( 'MainWANObjectCache', $cache );
1392 $db = wfGetDB( DB_MASTER
);
1394 // Get a fresh revision to use during testing
1395 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1396 $rev = $this->testPage
->getRevision();
1398 // Clear any previous cache for the revision during creation
1399 $key = $cache->makeGlobalKey(
1400 RevisionStore
::ROW_CACHE_KEY
,
1405 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1406 $this->assertFalse( $cache->get( $key ) );
1408 // Get the new revision and make sure it is in the cache and correct
1409 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1410 $this->assertRevEquals( $rev, $newRev );
1412 $cachedRow = $cache->get( $key );
1413 $this->assertNotFalse( $cachedRow );
1414 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1417 public function testNewKnownCurrent_withPageId() {
1418 $db = wfGetDB( DB_MASTER
);
1420 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1421 $rev = $this->testPage
->getRevision();
1423 $pageId = $this->testPage
->getId();
1425 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1426 $this->assertRevEquals( $rev, $newRev );
1429 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1430 $db = wfGetDB( DB_MASTER
);
1432 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1435 public function provideUserCanBitfield() {
1436 yield
[ 0, 0, [], null, true ];
1437 // Bitfields match, user has no permissions
1438 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1439 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1440 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1441 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1442 // Bitfields match, user (admin) does have permissions
1443 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1444 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1445 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1446 // Bitfields match, user (admin) does not have permissions
1447 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1448 // Bitfields match, user (oversight) does have permissions
1449 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1450 // Check permissions using the title
1452 Revision
::DELETED_TEXT
,
1453 Revision
::DELETED_TEXT
,
1455 Title
::newFromText( __METHOD__
),
1459 Revision
::DELETED_TEXT
,
1460 Revision
::DELETED_TEXT
,
1462 Title
::newFromText( __METHOD__
),
1468 * @dataProvider provideUserCanBitfield
1469 * @covers Revision::userCanBitfield
1471 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1472 $this->setMwGlobals(
1473 'wgGroupPermissions',
1476 'deletedtext' => true,
1477 'deletedhistory' => true,
1480 'viewsuppressed' => true,
1481 'suppressrevision' => true,
1485 $user = $this->getTestUser( $userGroups )->getUser();
1489 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1492 // Fallback to $wgUser
1493 $this->setMwGlobals(
1499 Revision
::userCanBitfield( $bitField, $field, null, $title )
1503 public function provideUserCan() {
1504 yield
[ 0, 0, [], true ];
1505 // Bitfields match, user has no permissions
1506 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1507 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1508 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1509 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1510 // Bitfields match, user (admin) does have permissions
1511 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1512 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1513 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1514 // Bitfields match, user (admin) does not have permissions
1515 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1516 // Bitfields match, user (oversight) does have permissions
1517 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1521 * @dataProvider provideUserCan
1522 * @covers Revision::userCan
1524 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1525 $this->setMwGlobals(
1526 'wgGroupPermissions',
1529 'deletedtext' => true,
1530 'deletedhistory' => true,
1533 'viewsuppressed' => true,
1534 'suppressrevision' => true,
1538 $user = $this->getTestUser( $userGroups )->getUser();
1539 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1543 $revision->userCan( $field, $user )