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() {
63 $this->tablesUsed +
= $this->getMcrTablesToReset();
67 $this->mergeMwGlobalArrayValue(
71 12313 => 'Dummy_talk',
75 $this->mergeMwGlobalArrayValue(
76 'wgNamespaceContentModels',
78 12312 => DummyContentForTesting
::MODEL_ID
,
82 $this->mergeMwGlobalArrayValue(
85 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
86 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
90 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
92 'wgMultiContentRevisionSchemaMigrationStage',
93 $this->getMcrMigrationStage()
96 MWNamespace
::clearCaches();
97 // Reset namespace cache
98 $wgContLang->resetNamespaces();
100 $this->overrideMwServices();
102 if ( !$this->testPage
) {
104 * We have to create a new page for each subclass as the page creation may result
105 * in different DB fields being filled based on configuration.
107 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
111 protected function tearDown() {
116 MWNamespace
::clearCaches();
117 // Reset namespace cache
118 $wgContLang->resetNamespaces();
121 abstract protected function getContentHandlerUseDB();
123 private function makeRevisionWithProps( $props = null ) {
124 if ( $props === null ) {
128 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
129 $props['text'] = 'Lorem Ipsum';
132 if ( !isset( $props['user_text'] ) ) {
133 $user = $this->getTestUser()->getUser();
134 $props['user_text'] = $user->getName();
135 $props['user'] = $user->getId();
138 if ( !isset( $props['user'] ) ) {
142 if ( !isset( $props['comment'] ) ) {
143 $props['comment'] = 'just a test';
146 if ( !isset( $props['page'] ) ) {
147 $props['page'] = $this->testPage
->getId();
150 if ( !isset( $props['content_model'] ) ) {
151 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
154 $rev = new Revision( $props );
156 $dbw = wfGetDB( DB_MASTER
);
157 $rev->insertOn( $dbw );
163 * @param string $titleString
164 * @param string $text
165 * @param string|null $model
169 private function createPage( $titleString, $text, $model = null ) {
170 if ( !preg_match( '/:/', $titleString ) &&
171 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
173 $ns = $this->getDefaultWikitextNS();
174 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
177 $title = Title
::newFromText( $titleString );
178 $wikipage = new WikiPage( $title );
180 // Delete the article if it already exists
181 if ( $wikipage->exists() ) {
182 $wikipage->doDeleteArticle( "done" );
185 $content = ContentHandler
::makeContent( $text, $title, $model );
186 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
191 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
192 $this->assertNotNull( $rev, 'missing revision' );
194 $this->assertEquals( $orig->getId(), $rev->getId() );
195 $this->assertEquals( $orig->getPage(), $rev->getPage() );
196 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
197 $this->assertEquals( $orig->getUser(), $rev->getUser() );
198 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
199 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
200 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
204 * @covers Revision::getRecentChange
206 public function testGetRecentChange() {
207 $rev = $this->testPage
->getRevision();
208 $recentChange = $rev->getRecentChange();
210 // Make sure various attributes look right / the correct entry has been retrieved.
211 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
213 $rev->getTitle()->getNamespace(),
214 $recentChange->getAttribute( 'rc_namespace' )
217 $rev->getTitle()->getDBkey(),
218 $recentChange->getAttribute( 'rc_title' )
220 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
221 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
222 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
223 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
224 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
228 * @covers Revision::insertOn
230 public function testInsertOn_success() {
231 $parentId = $this->testPage
->getLatest();
233 // If an ExternalStore is set don't use it.
234 $this->setMwGlobals( 'wgDefaultExternalStore', false );
236 $rev = new Revision( [
237 'page' => $this->testPage
->getId(),
238 'title' => $this->testPage
->getTitle(),
239 'text' => 'Revision Text',
240 'comment' => 'Revision comment',
243 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
245 $this->assertInternalType( 'integer', $revId );
246 $this->assertSame( $revId, $rev->getId() );
248 // getTextId() must be an int!
249 $this->assertInternalType( 'integer', $rev->getTextId() );
251 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
253 // we currently only support storage in the text table
254 $textId = MediaWikiServices
::getInstance()
256 ->getTextIdFromAddress( $mainSlot->getAddress() );
260 [ 'old_id', 'old_text' ],
262 [ [ strval( $textId ), 'Revision Text' ] ]
275 "rev_id = {$rev->getId()}",
277 strval( $rev->getId() ),
278 strval( $this->testPage
->getId() ),
283 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
288 public function provideInsertOn_exceptionOnIncomplete() {
289 $content = new TextContent( '' );
290 $user = User
::newFromName( 'Foo' );
292 yield
'no parent' => [
294 'content' => $content,
298 IncompleteRevisionException
::class,
299 "rev_page field must not be 0!"
302 yield
'no comment' => [
304 'content' => $content,
308 IncompleteRevisionException
::class,
309 "comment must not be NULL!"
312 yield
'no content' => [
318 IncompleteRevisionException
::class,
319 "Uninitialized field: content_address" // XXX: message may change
324 * @dataProvider provideInsertOn_exceptionOnIncomplete
325 * @covers Revision::insertOn
327 public function testInsertOn_exceptionOnIncomplete( $array, $expException, $expMessage ) {
328 // If an ExternalStore is set don't use it.
329 $this->setMwGlobals( 'wgDefaultExternalStore', false );
330 $this->setExpectedException( $expException, $expMessage );
332 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
333 $rev = new Revision( $array, 0, $title );
335 $rev->insertOn( wfGetDB( DB_MASTER
) );
339 * @covers Revision::newFromTitle
341 public function testNewFromTitle_withoutId() {
342 $latestRevId = $this->testPage
->getLatest();
344 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
346 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
347 $this->assertEquals( $latestRevId, $rev->getId() );
351 * @covers Revision::newFromTitle
353 public function testNewFromTitle_withId() {
354 $latestRevId = $this->testPage
->getLatest();
356 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
358 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
359 $this->assertEquals( $latestRevId, $rev->getId() );
363 * @covers Revision::newFromTitle
365 public function testNewFromTitle_withBadId() {
366 $latestRevId = $this->testPage
->getLatest();
368 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
370 $this->assertNull( $rev );
374 * @covers Revision::newFromRow
376 public function testNewFromRow() {
377 $orig = $this->makeRevisionWithProps();
379 $dbr = wfGetDB( DB_REPLICA
);
380 $revQuery = Revision
::getQueryInfo();
381 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
382 __METHOD__
, [], $revQuery['joins'] );
383 $this->assertTrue( is_object( $res ), 'query failed' );
385 $row = $res->fetchObject();
388 $rev = Revision
::newFromRow( $row );
390 $this->assertRevEquals( $orig, $rev );
393 public function provideNewFromArchiveRow() {
401 return $f +
[ 'ar_namespace', 'ar_title' ];
406 unset( $f['ar_text_id'] );
412 unset( $f['ar_page_id'] );
418 unset( $f['ar_parent_id'] );
424 unset( $f['ar_rev_id'] );
430 unset( $f['ar_sha1'] );
437 * @dataProvider provideNewFromArchiveRow
438 * @covers Revision::newFromArchiveRow
440 public function testNewFromArchiveRow( $selectModifier ) {
441 $services = MediaWikiServices
::getInstance();
443 $store = new RevisionStore(
444 $services->getDBLoadBalancer(),
445 $services->getService( '_SqlBlobStore' ),
446 $services->getMainWANObjectCache(),
447 $services->getCommentStore(),
448 $services->getContentModelStore(),
449 $services->getSlotRoleStore(),
450 $this->getMcrMigrationStage(),
451 $services->getActorMigration()
454 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
455 $this->setService( 'RevisionStore', $store );
457 $page = $this->createPage(
458 'RevisionStorageTest_testNewFromArchiveRow',
460 CONTENT_MODEL_WIKITEXT
462 $orig = $page->getRevision();
463 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
465 $dbr = wfGetDB( DB_REPLICA
);
466 $arQuery = Revision
::getArchiveQueryInfo();
467 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
469 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
470 __METHOD__
, [], $arQuery['joins']
472 $this->assertTrue( is_object( $res ), 'query failed' );
474 $row = $res->fetchObject();
477 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
478 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
479 $rev = Revision
::newFromArchiveRow( $row );
481 $this->assertRevEquals( $orig, $rev );
485 * @covers Revision::newFromArchiveRow
487 public function testNewFromArchiveRowOverrides() {
488 $page = $this->createPage(
489 'RevisionStorageTest_testNewFromArchiveRow',
491 CONTENT_MODEL_WIKITEXT
493 $orig = $page->getRevision();
494 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
496 $dbr = wfGetDB( DB_REPLICA
);
497 $arQuery = Revision
::getArchiveQueryInfo();
499 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
500 __METHOD__
, [], $arQuery['joins']
502 $this->assertTrue( is_object( $res ), 'query failed' );
504 $row = $res->fetchObject();
507 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
509 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
510 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
514 * @covers Revision::newFromId
516 public function testNewFromId() {
517 $orig = $this->testPage
->getRevision();
518 $rev = Revision
::newFromId( $orig->getId() );
519 $this->assertRevEquals( $orig, $rev );
523 * @covers Revision::newFromPageId
525 public function testNewFromPageId() {
526 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
527 $this->assertRevEquals(
528 $this->testPage
->getRevision(),
534 * @covers Revision::newFromPageId
536 public function testNewFromPageIdWithLatestId() {
537 $rev = Revision
::newFromPageId(
538 $this->testPage
->getId(),
539 $this->testPage
->getLatest()
541 $this->assertRevEquals(
542 $this->testPage
->getRevision(),
548 * @covers Revision::newFromPageId
550 public function testNewFromPageIdWithNotLatestId() {
551 $content = new WikitextContent( __METHOD__
);
552 $this->testPage
->doEditContent( $content, __METHOD__
);
553 $rev = Revision
::newFromPageId(
554 $this->testPage
->getId(),
555 $this->testPage
->getRevision()->getPrevious()->getId()
557 $this->assertRevEquals(
558 $this->testPage
->getRevision()->getPrevious(),
564 * @covers Revision::fetchRevision
566 public function testFetchRevision() {
567 // Hidden process cache assertion below
568 $this->testPage
->getRevision()->getId();
570 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
571 $id = $this->testPage
->getRevision()->getId();
573 $this->hideDeprecated( 'Revision::fetchRevision' );
574 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
576 # note: order is unspecified
578 while ( ( $row = $res->fetchObject() ) ) {
579 $rows[$row->rev_id
] = $row;
582 $this->assertEmpty( $rows, 'expected empty set' );
586 * @covers Revision::getPage
588 public function testGetPage() {
589 $page = $this->testPage
;
591 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
592 $rev = Revision
::newFromId( $orig->getId() );
594 $this->assertEquals( $page->getId(), $rev->getPage() );
598 * @covers Revision::isCurrent
600 public function testIsCurrent() {
601 $rev1 = $this->testPage
->getRevision();
603 # @todo find out if this should be true
604 # $this->assertTrue( $rev1->isCurrent() );
606 $rev1x = Revision
::newFromId( $rev1->getId() );
607 $this->assertTrue( $rev1x->isCurrent() );
609 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
610 $rev2 = $this->testPage
->getRevision();
612 # @todo find out if this should be true
613 # $this->assertTrue( $rev2->isCurrent() );
615 $rev1x = Revision
::newFromId( $rev1->getId() );
616 $this->assertFalse( $rev1x->isCurrent() );
618 $rev2x = Revision
::newFromId( $rev2->getId() );
619 $this->assertTrue( $rev2x->isCurrent() );
623 * @covers Revision::getPrevious
625 public function testGetPrevious() {
626 $oldestRevision = $this->testPage
->getOldestRevision();
627 $latestRevision = $this->testPage
->getLatest();
629 $this->assertNull( $oldestRevision->getPrevious() );
631 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
632 $newRevision = $this->testPage
->getRevision();
634 $this->assertNotNull( $newRevision->getPrevious() );
635 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
639 * @covers Revision::getNext
641 public function testGetNext() {
642 $rev1 = $this->testPage
->getRevision();
644 $this->assertNull( $rev1->getNext() );
646 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
647 $rev2 = $this->testPage
->getRevision();
649 $this->assertNotNull( $rev1->getNext() );
650 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
654 * @covers Revision::newNullRevision
656 public function testNewNullRevision() {
657 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
658 $orig = $this->testPage
->getRevision();
660 $dbw = wfGetDB( DB_MASTER
);
661 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
663 $this->assertNotEquals( $orig->getId(), $rev->getId(),
664 'new null revision should have a different id from the original revision' );
665 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
666 'new null revision should have the same text id as the original revision' );
667 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
668 'new null revision should have the same SHA1 as the original revision' );
669 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
670 'new null revision should have the same content as the original revision' );
671 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
675 * @covers Revision::newNullRevision
677 public function testNewNullRevision_badPage() {
678 $dbw = wfGetDB( DB_MASTER
);
679 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
681 $this->assertNull( $rev );
685 * @covers Revision::insertOn
687 public function testInsertOn() {
688 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
690 $orig = $this->makeRevisionWithProps( [
694 // Make sure the revision was copied to ip_changes
695 $dbr = wfGetDB( DB_REPLICA
);
696 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
697 $row = $res->fetchObject();
699 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
701 $orig->getTimestamp(),
702 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
706 public static function provideUserWasLastToEdit() {
707 yield
'actually the last edit' => [ 3, true ];
708 yield
'not the current edit, but still by this user' => [ 2, true ];
709 yield
'edit by another user' => [ 1, false ];
710 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
714 * @covers Revision::userWasLastToEdit
715 * @dataProvider provideUserWasLastToEdit
717 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
718 $userA = User
::newFromName( "RevisionStorageTest_userA" );
719 $userB = User
::newFromName( "RevisionStorageTest_userB" );
721 if ( $userA->getId() === 0 ) {
722 $userA = User
::createNew( $userA->getName() );
725 if ( $userB->getId() === 0 ) {
726 $userB = User
::createNew( $userB->getName() );
729 $ns = $this->getDefaultWikitextNS();
731 $dbw = wfGetDB( DB_MASTER
);
734 // create revisions -----------------------------
735 $page = WikiPage
::factory( Title
::newFromText(
736 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
737 $page->insertOn( $dbw );
739 $revisions[0] = new Revision( [
740 'page' => $page->getId(),
741 // we need the title to determine the page's default content model
742 'title' => $page->getTitle(),
743 'timestamp' => '20120101000000',
744 'user' => $userA->getId(),
746 'content_model' => CONTENT_MODEL_WIKITEXT
,
747 'comment' => 'edit zero'
749 $revisions[0]->insertOn( $dbw );
751 $revisions[1] = new Revision( [
752 'page' => $page->getId(),
753 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
754 'title' => $page->getTitle(),
755 'timestamp' => '20120101000100',
756 'user' => $userA->getId(),
758 'content_model' => CONTENT_MODEL_WIKITEXT
,
759 'comment' => 'edit one'
761 $revisions[1]->insertOn( $dbw );
763 $revisions[2] = new Revision( [
764 'page' => $page->getId(),
765 'title' => $page->getTitle(),
766 'timestamp' => '20120101000200',
767 'user' => $userB->getId(),
769 'content_model' => CONTENT_MODEL_WIKITEXT
,
770 'comment' => 'edit two'
772 $revisions[2]->insertOn( $dbw );
774 $revisions[3] = new Revision( [
775 'page' => $page->getId(),
776 'title' => $page->getTitle(),
777 'timestamp' => '20120101000300',
778 'user' => $userA->getId(),
780 'content_model' => CONTENT_MODEL_WIKITEXT
,
781 'comment' => 'edit three'
783 $revisions[3]->insertOn( $dbw );
785 $revisions[4] = new Revision( [
786 'page' => $page->getId(),
787 'title' => $page->getTitle(),
788 'timestamp' => '20120101000200',
789 'user' => $userA->getId(),
791 'content_model' => CONTENT_MODEL_WIKITEXT
,
792 'comment' => 'edit four'
794 $revisions[4]->insertOn( $dbw );
796 // test it ---------------------------------
797 $since = $revisions[$sinceIdx]->getTimestamp();
799 $revQuery = Revision
::getQueryInfo();
800 $allRows = iterator_to_array( $dbw->select(
802 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
804 'rev_page' => $page->getId(),
805 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
808 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
812 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
814 $this->assertEquals( $expectedLast, $wasLast );
818 * @param string $text
819 * @param string $title
820 * @param string $model
821 * @param string $format
825 private function newTestRevision( $text, $title = "Test",
826 $model = CONTENT_MODEL_WIKITEXT
, $format = null
828 if ( is_string( $title ) ) {
829 $title = Title
::newFromText( $title );
832 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
840 'content' => $content,
841 'length' => $content->getSize(),
842 'comment' => "testing",
843 'minor_edit' => false,
845 'content_format' => $format,
852 public function provideGetContentModel() {
853 // NOTE: we expect the help namespace to always contain wikitext
855 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
856 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
857 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
862 * @dataProvider provideGetContentModel
863 * @covers Revision::getContentModel
865 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
866 $rev = $this->newTestRevision( $text, $title, $model, $format );
868 $this->assertEquals( $expectedModel, $rev->getContentModel() );
871 public function provideGetContentFormat() {
872 // NOTE: we expect the help namespace to always contain wikitext
874 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
875 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
876 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
877 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
882 * @dataProvider provideGetContentFormat
883 * @covers Revision::getContentFormat
885 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
886 $rev = $this->newTestRevision( $text, $title, $model, $format );
888 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
891 public function provideGetContentHandler() {
892 // NOTE: we expect the help namespace to always contain wikitext
894 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
895 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
896 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
901 * @dataProvider provideGetContentHandler
902 * @covers Revision::getContentHandler
904 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
905 $rev = $this->newTestRevision( $text, $title, $model, $format );
907 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
910 public function provideGetContent() {
911 // NOTE: we expect the help namespace to always contain wikitext
913 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
915 serialize( 'hello world' ),
917 DummyContentForTesting
::MODEL_ID
,
919 Revision
::FOR_PUBLIC
,
920 serialize( 'hello world' )
923 serialize( 'hello world' ),
927 Revision
::FOR_PUBLIC
,
928 serialize( 'hello world' )
934 * @dataProvider provideGetContent
935 * @covers Revision::getContent
937 public function testGetContent( $text, $title, $model, $format,
938 $audience, $expectedSerialization
940 $rev = $this->newTestRevision( $text, $title, $model, $format );
941 $content = $rev->getContent( $audience );
944 $expectedSerialization,
945 is_null( $content ) ?
null : $content->serialize( $format )
950 * @covers Revision::getContent
952 public function testGetContent_failure() {
953 $rev = new Revision( [
954 'page' => $this->testPage
->getId(),
955 'content_model' => $this->testPage
->getContentModel(),
956 'id' => 123456789, // not in the test DB
959 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
961 $this->assertNull( $rev->getContent(),
962 "getContent() should return null if the revision's text blob could not be loaded." );
964 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
965 $this->assertNull( $rev->getContent(),
966 "getContent() should return null if the revision's text blob could not be loaded." );
968 Wikimedia\restoreWarnings
();
971 public function provideGetSize() {
973 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
974 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
979 * @covers Revision::getSize
980 * @dataProvider provideGetSize
982 public function testGetSize( $text, $model, $expected_size ) {
983 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
984 $this->assertEquals( $expected_size, $rev->getSize() );
987 public function provideGetSha1() {
989 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
991 serialize( "hello world." ),
992 DummyContentForTesting
::MODEL_ID
,
993 Revision
::base36Sha1( serialize( "hello world." ) )
999 * @covers Revision::getSha1
1000 * @dataProvider provideGetSha1
1002 public function testGetSha1( $text, $model, $expected_hash ) {
1003 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
1004 $this->assertEquals( $expected_hash, $rev->getSha1() );
1008 * Tests whether $rev->getContent() returns a clone when needed.
1010 * @covers Revision::getContent
1012 public function testGetContentClone() {
1013 $content = new RevisionTestModifyableContent( "foo" );
1015 $rev = new Revision(
1019 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
1021 'content' => $content,
1022 'length' => $content->getSize(),
1023 'comment' => "testing",
1024 'minor_edit' => false,
1028 /** @var RevisionTestModifyableContent $content */
1029 $content = $rev->getContent( Revision
::RAW
);
1030 $content->setText( "bar" );
1032 /** @var RevisionTestModifyableContent $content2 */
1033 $content2 = $rev->getContent( Revision
::RAW
);
1034 // content is mutable, expect clone
1035 $this->assertNotSame( $content, $content2, "expected a clone" );
1036 // clone should contain the original text
1037 $this->assertEquals( "foo", $content2->getText() );
1039 $content2->setText( "bla bla" );
1040 // clones should be independent
1041 $this->assertEquals( "bar", $content->getText() );
1045 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1046 * @covers Revision::getContent
1048 public function testGetContentUncloned() {
1049 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1050 $content = $rev->getContent( Revision
::RAW
);
1051 $content2 = $rev->getContent( Revision
::RAW
);
1053 // for immutable content like wikitext, this should be the same object
1054 $this->assertSame( $content, $content2 );
1058 * @covers Revision::loadFromId
1060 public function testLoadFromId() {
1061 $rev = $this->testPage
->getRevision();
1062 $this->hideDeprecated( 'Revision::loadFromId' );
1063 $this->assertRevEquals(
1065 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1070 * @covers Revision::loadFromPageId
1072 public function testLoadFromPageId() {
1073 $this->assertRevEquals(
1074 $this->testPage
->getRevision(),
1075 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1080 * @covers Revision::loadFromPageId
1082 public function testLoadFromPageIdWithLatestRevId() {
1083 $this->assertRevEquals(
1084 $this->testPage
->getRevision(),
1085 Revision
::loadFromPageId(
1086 wfGetDB( DB_MASTER
),
1087 $this->testPage
->getId(),
1088 $this->testPage
->getLatest()
1094 * @covers Revision::loadFromPageId
1096 public function testLoadFromPageIdWithNotLatestRevId() {
1097 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1098 $this->assertRevEquals(
1099 $this->testPage
->getRevision()->getPrevious(),
1100 Revision
::loadFromPageId(
1101 wfGetDB( DB_MASTER
),
1102 $this->testPage
->getId(),
1103 $this->testPage
->getRevision()->getPrevious()->getId()
1109 * @covers Revision::loadFromTitle
1111 public function testLoadFromTitle() {
1112 $this->assertRevEquals(
1113 $this->testPage
->getRevision(),
1114 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1119 * @covers Revision::loadFromTitle
1121 public function testLoadFromTitleWithLatestRevId() {
1122 $this->assertRevEquals(
1123 $this->testPage
->getRevision(),
1124 Revision
::loadFromTitle(
1125 wfGetDB( DB_MASTER
),
1126 $this->testPage
->getTitle(),
1127 $this->testPage
->getLatest()
1133 * @covers Revision::loadFromTitle
1135 public function testLoadFromTitleWithNotLatestRevId() {
1136 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1137 $this->assertRevEquals(
1138 $this->testPage
->getRevision()->getPrevious(),
1139 Revision
::loadFromTitle(
1140 wfGetDB( DB_MASTER
),
1141 $this->testPage
->getTitle(),
1142 $this->testPage
->getRevision()->getPrevious()->getId()
1148 * @covers Revision::loadFromTimestamp()
1150 public function testLoadFromTimestamp() {
1151 $this->assertRevEquals(
1152 $this->testPage
->getRevision(),
1153 Revision
::loadFromTimestamp(
1154 wfGetDB( DB_MASTER
),
1155 $this->testPage
->getTitle(),
1156 $this->testPage
->getRevision()->getTimestamp()
1162 * @covers Revision::getParentLengths
1164 public function testGetParentLengths_noRevIds() {
1167 Revision
::getParentLengths(
1168 wfGetDB( DB_MASTER
),
1175 * @covers Revision::getParentLengths
1177 public function testGetParentLengths_oneRevId() {
1178 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1179 $textLength = strlen( $text );
1181 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1182 $rev[1] = $this->testPage
->getLatest();
1185 [ $rev[1] => $textLength ],
1186 Revision
::getParentLengths(
1187 wfGetDB( DB_MASTER
),
1194 * @covers Revision::getParentLengths
1196 public function testGetParentLengths_multipleRevIds() {
1197 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1198 $textOneLength = strlen( $textOne );
1199 $textTwo = '831jr091jr092121j09rj1';
1200 $textTwoLength = strlen( $textTwo );
1202 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1203 $rev[1] = $this->testPage
->getLatest();
1204 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1205 $rev[2] = $this->testPage
->getLatest();
1208 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1209 Revision
::getParentLengths(
1210 wfGetDB( DB_MASTER
),
1211 [ $rev[1], $rev[2] ]
1217 * @covers Revision::getTitle
1219 public function testGetTitle_fromExistingRevision() {
1221 $this->testPage
->getTitle()->equals(
1222 $this->testPage
->getRevision()->getTitle()
1228 * @covers Revision::getTitle
1230 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1231 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1233 $this->testPage
->getTitle()->equals(
1240 * @covers Revision::isMinor
1242 public function testIsMinor_true() {
1243 // Use a sysop to ensure we can mark edits as minor
1244 $sysop = $this->getTestSysop()->getUser();
1246 $this->testPage
->doEditContent(
1247 new WikitextContent( __METHOD__
),
1253 $rev = $this->testPage
->getRevision();
1255 $this->assertSame( true, $rev->isMinor() );
1259 * @covers Revision::isMinor
1261 public function testIsMinor_false() {
1262 $this->testPage
->doEditContent(
1263 new WikitextContent( __METHOD__
),
1267 $rev = $this->testPage
->getRevision();
1269 $this->assertSame( false, $rev->isMinor() );
1273 * @covers Revision::getTimestamp
1275 public function testGetTimestamp() {
1276 $testTimestamp = wfTimestampNow();
1278 $this->testPage
->doEditContent(
1279 new WikitextContent( __METHOD__
),
1282 $rev = $this->testPage
->getRevision();
1284 $this->assertInternalType( 'string', $rev->getTimestamp() );
1285 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1286 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1290 * @covers Revision::getUser
1291 * @covers Revision::getUserText
1293 public function testGetUserAndText() {
1294 $sysop = $this->getTestSysop()->getUser();
1296 $this->testPage
->doEditContent(
1297 new WikitextContent( __METHOD__
),
1303 $rev = $this->testPage
->getRevision();
1305 $this->assertSame( $sysop->getId(), $rev->getUser() );
1306 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1310 * @covers Revision::isDeleted
1312 public function testIsDeleted_nothingDeleted() {
1313 $rev = $this->testPage
->getRevision();
1315 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1316 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1317 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1318 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1322 * @covers Revision::getVisibility
1324 public function testGetVisibility_nothingDeleted() {
1325 $rev = $this->testPage
->getRevision();
1327 $this->assertSame( 0, $rev->getVisibility() );
1331 * @covers Revision::getComment
1333 public function testGetComment_notDeleted() {
1334 $expectedSummary = 'goatlicious summary';
1336 $this->testPage
->doEditContent(
1337 new WikitextContent( __METHOD__
),
1340 $rev = $this->testPage
->getRevision();
1342 $this->assertSame( $expectedSummary, $rev->getComment() );
1346 * @covers Revision::isUnpatrolled
1348 public function testIsUnpatrolled_returnsRecentChangesId() {
1349 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1350 $rev = $this->testPage
->getRevision();
1352 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1353 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1357 * @covers Revision::isUnpatrolled
1359 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1360 // This assumes that sysops are auto patrolled
1361 $sysop = $this->getTestSysop()->getUser();
1362 $this->testPage
->doEditContent(
1363 new WikitextContent( __METHOD__
),
1369 $rev = $this->testPage
->getRevision();
1371 $this->assertSame( 0, $rev->isUnpatrolled() );
1375 * This is a simple blanket test for all simple content getters and is methods to provide some
1376 * coverage before the split of Revision into multiple classes for MCR work.
1377 * @covers Revision::getContent
1378 * @covers Revision::getSerializedData
1379 * @covers Revision::getContentModel
1380 * @covers Revision::getContentFormat
1381 * @covers Revision::getContentHandler
1383 public function testSimpleContentGetters() {
1384 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1385 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1387 $this->testPage
->doEditContent(
1388 new WikitextContent( $expectedText ),
1391 $rev = $this->testPage
->getRevision();
1393 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1394 $this->assertSame( $expectedText, $rev->getSerializedData() );
1395 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1396 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1397 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1401 * @covers Revision::newKnownCurrent
1403 public function testNewKnownCurrent() {
1404 // Setup the services
1405 $this->resetGlobalServices();
1406 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1407 $this->setService( 'MainWANObjectCache', $cache );
1408 $db = wfGetDB( DB_MASTER
);
1410 // Get a fresh revision to use during testing
1411 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1412 $rev = $this->testPage
->getRevision();
1414 // Clear any previous cache for the revision during creation
1415 $key = $cache->makeGlobalKey(
1416 RevisionStore
::ROW_CACHE_KEY
,
1421 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1422 $this->assertFalse( $cache->get( $key ) );
1424 // Get the new revision and make sure it is in the cache and correct
1425 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1426 $this->assertRevEquals( $rev, $newRev );
1428 $cachedRow = $cache->get( $key );
1429 $this->assertNotFalse( $cachedRow );
1430 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1433 public function testNewKnownCurrent_withPageId() {
1434 $db = wfGetDB( DB_MASTER
);
1436 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1437 $rev = $this->testPage
->getRevision();
1439 $pageId = $this->testPage
->getId();
1441 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1442 $this->assertRevEquals( $rev, $newRev );
1445 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1446 $db = wfGetDB( DB_MASTER
);
1448 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1451 public function provideUserCanBitfield() {
1452 yield
[ 0, 0, [], null, true ];
1453 // Bitfields match, user has no permissions
1454 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1455 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1456 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1457 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1458 // Bitfields match, user (admin) does have permissions
1459 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1460 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1461 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1462 // Bitfields match, user (admin) does not have permissions
1463 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1464 // Bitfields match, user (oversight) does have permissions
1465 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1466 // Check permissions using the title
1468 Revision
::DELETED_TEXT
,
1469 Revision
::DELETED_TEXT
,
1471 Title
::newFromText( __METHOD__
),
1475 Revision
::DELETED_TEXT
,
1476 Revision
::DELETED_TEXT
,
1478 Title
::newFromText( __METHOD__
),
1484 * @dataProvider provideUserCanBitfield
1485 * @covers Revision::userCanBitfield
1487 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1488 $this->setMwGlobals(
1489 'wgGroupPermissions',
1492 'deletedtext' => true,
1493 'deletedhistory' => true,
1496 'viewsuppressed' => true,
1497 'suppressrevision' => true,
1501 $user = $this->getTestUser( $userGroups )->getUser();
1505 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1508 // Fallback to $wgUser
1509 $this->setMwGlobals(
1515 Revision
::userCanBitfield( $bitField, $field, null, $title )
1519 public function provideUserCan() {
1520 yield
[ 0, 0, [], true ];
1521 // Bitfields match, user has no permissions
1522 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1523 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1524 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1525 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1526 // Bitfields match, user (admin) does have permissions
1527 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1528 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1529 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1530 // Bitfields match, user (admin) does not have permissions
1531 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1532 // Bitfields match, user (oversight) does have permissions
1533 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1537 * @dataProvider provideUserCan
1538 * @covers Revision::userCan
1540 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1541 $this->setMwGlobals(
1542 'wgGroupPermissions',
1545 'deletedtext' => true,
1546 'deletedhistory' => true,
1549 'viewsuppressed' => true,
1550 'suppressrevision' => true,
1554 $user = $this->getTestUser( $userGroups )->getUser();
1555 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1559 $revision->userCan( $field, $user )