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 setUp() {
51 $this->mergeMwGlobalArrayValue(
55 12313 => 'Dummy_talk',
59 $this->mergeMwGlobalArrayValue(
60 'wgNamespaceContentModels',
62 12312 => DummyContentForTesting
::MODEL_ID
,
66 $this->mergeMwGlobalArrayValue(
69 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
70 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
74 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
76 MWNamespace
::clearCaches();
77 // Reset namespace cache
78 $wgContLang->resetNamespaces();
80 if ( !$this->testPage
) {
82 * We have to create a new page for each subclass as the page creation may result
83 * in different DB fields being filled based on configuration.
85 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
89 protected function tearDown() {
94 MWNamespace
::clearCaches();
95 // Reset namespace cache
96 $wgContLang->resetNamespaces();
99 abstract protected function getContentHandlerUseDB();
101 private function makeRevisionWithProps( $props = null ) {
102 if ( $props === null ) {
106 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
107 $props['text'] = 'Lorem Ipsum';
110 if ( !isset( $props['user_text'] ) ) {
111 $user = $this->getTestUser()->getUser();
112 $props['user_text'] = $user->getName();
113 $props['user'] = $user->getId();
116 if ( !isset( $props['user'] ) ) {
120 if ( !isset( $props['comment'] ) ) {
121 $props['comment'] = 'just a test';
124 if ( !isset( $props['page'] ) ) {
125 $props['page'] = $this->testPage
->getId();
128 if ( !isset( $props['content_model'] ) ) {
129 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
132 $rev = new Revision( $props );
134 $dbw = wfGetDB( DB_MASTER
);
135 $rev->insertOn( $dbw );
141 * @param string $titleString
142 * @param string $text
143 * @param string|null $model
147 private function createPage( $titleString, $text, $model = null ) {
148 if ( !preg_match( '/:/', $titleString ) &&
149 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
151 $ns = $this->getDefaultWikitextNS();
152 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
155 $title = Title
::newFromText( $titleString );
156 $wikipage = new WikiPage( $title );
158 // Delete the article if it already exists
159 if ( $wikipage->exists() ) {
160 $wikipage->doDeleteArticle( "done" );
163 $content = ContentHandler
::makeContent( $text, $title, $model );
164 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
169 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
170 $this->assertNotNull( $rev, 'missing revision' );
172 $this->assertEquals( $orig->getId(), $rev->getId() );
173 $this->assertEquals( $orig->getPage(), $rev->getPage() );
174 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
175 $this->assertEquals( $orig->getUser(), $rev->getUser() );
176 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
177 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
178 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
182 * @covers Revision::getRecentChange
184 public function testGetRecentChange() {
185 $rev = $this->testPage
->getRevision();
186 $recentChange = $rev->getRecentChange();
188 // Make sure various attributes look right / the correct entry has been retrieved.
189 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
191 $rev->getTitle()->getNamespace(),
192 $recentChange->getAttribute( 'rc_namespace' )
195 $rev->getTitle()->getDBkey(),
196 $recentChange->getAttribute( 'rc_title' )
198 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
199 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
200 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
201 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
202 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
206 * @covers Revision::insertOn
208 public function testInsertOn_success() {
209 $parentId = $this->testPage
->getLatest();
211 // If an ExternalStore is set don't use it.
212 $this->setMwGlobals( 'wgDefaultExternalStore', false );
214 $rev = new Revision( [
215 'page' => $this->testPage
->getId(),
216 'title' => $this->testPage
->getTitle(),
217 'text' => 'Revision Text',
218 'comment' => 'Revision comment',
221 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
223 $this->assertInternalType( 'integer', $revId );
224 $this->assertSame( $revId, $rev->getId() );
226 // getTextId() must be an int!
227 $this->assertInternalType( 'integer', $rev->getTextId() );
229 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
231 // we currently only support storage in the text table
232 $textId = MediaWikiServices
::getInstance()
234 ->getTextIdFromAddress( $mainSlot->getAddress() );
238 [ 'old_id', 'old_text' ],
240 [ [ strval( $textId ), 'Revision Text' ] ]
254 "rev_id = {$rev->getId()}",
256 strval( $rev->getId() ),
257 strval( $this->testPage
->getId() ),
263 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
269 * @covers Revision::insertOn
271 public function testInsertOn_exceptionOnNoPage() {
272 // If an ExternalStore is set don't use it.
273 $this->setMwGlobals( 'wgDefaultExternalStore', false );
274 $this->setExpectedException(
275 IncompleteRevisionException
::class,
276 "rev_page field must not be 0!"
279 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
280 $rev = new Revision( [], 0, $title );
282 $rev->insertOn( wfGetDB( DB_MASTER
) );
286 * @covers Revision::newFromTitle
288 public function testNewFromTitle_withoutId() {
289 $latestRevId = $this->testPage
->getLatest();
291 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
293 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
294 $this->assertEquals( $latestRevId, $rev->getId() );
298 * @covers Revision::newFromTitle
300 public function testNewFromTitle_withId() {
301 $latestRevId = $this->testPage
->getLatest();
303 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
305 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
306 $this->assertEquals( $latestRevId, $rev->getId() );
310 * @covers Revision::newFromTitle
312 public function testNewFromTitle_withBadId() {
313 $latestRevId = $this->testPage
->getLatest();
315 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
317 $this->assertNull( $rev );
321 * @covers Revision::newFromRow
323 public function testNewFromRow() {
324 $orig = $this->makeRevisionWithProps();
326 $dbr = wfGetDB( DB_REPLICA
);
327 $revQuery = Revision
::getQueryInfo();
328 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
329 __METHOD__
, [], $revQuery['joins'] );
330 $this->assertTrue( is_object( $res ), 'query failed' );
332 $row = $res->fetchObject();
335 $rev = Revision
::newFromRow( $row );
337 $this->assertRevEquals( $orig, $rev );
340 public function provideNewFromArchiveRow() {
348 return $f +
[ 'ar_namespace', 'ar_title' ];
353 unset( $f['ar_text'] );
359 unset( $f['ar_text_id'] );
365 unset( $f['ar_page_id'] );
371 unset( $f['ar_parent_id'] );
377 unset( $f['ar_rev_id'] );
383 unset( $f['ar_sha1'] );
390 * @dataProvider provideNewFromArchiveRow
391 * @covers Revision::newFromArchiveRow
393 public function testNewFromArchiveRow( $selectModifier ) {
394 $services = MediaWikiServices
::getInstance();
396 $store = new RevisionStore(
397 $services->getDBLoadBalancer(),
398 $services->getService( '_SqlBlobStore' ),
399 $services->getMainWANObjectCache(),
400 $services->getCommentStore(),
401 $services->getActorMigration()
404 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
405 $this->setService( 'RevisionStore', $store );
407 $page = $this->createPage(
408 'RevisionStorageTest_testNewFromArchiveRow',
410 CONTENT_MODEL_WIKITEXT
412 $orig = $page->getRevision();
413 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
415 $dbr = wfGetDB( DB_REPLICA
);
416 $arQuery = Revision
::getArchiveQueryInfo();
417 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
419 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
420 __METHOD__
, [], $arQuery['joins']
422 $this->assertTrue( is_object( $res ), 'query failed' );
424 $row = $res->fetchObject();
427 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
428 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
429 $rev = Revision
::newFromArchiveRow( $row );
431 $this->assertRevEquals( $orig, $rev );
435 * @covers Revision::newFromArchiveRow
437 public function testNewFromArchiveRowOverrides() {
438 $page = $this->createPage(
439 'RevisionStorageTest_testNewFromArchiveRow',
441 CONTENT_MODEL_WIKITEXT
443 $orig = $page->getRevision();
444 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
446 $dbr = wfGetDB( DB_REPLICA
);
447 $arQuery = Revision
::getArchiveQueryInfo();
449 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
450 __METHOD__
, [], $arQuery['joins']
452 $this->assertTrue( is_object( $res ), 'query failed' );
454 $row = $res->fetchObject();
457 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
459 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
460 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
464 * @covers Revision::newFromId
466 public function testNewFromId() {
467 $orig = $this->testPage
->getRevision();
468 $rev = Revision
::newFromId( $orig->getId() );
469 $this->assertRevEquals( $orig, $rev );
473 * @covers Revision::newFromPageId
475 public function testNewFromPageId() {
476 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
477 $this->assertRevEquals(
478 $this->testPage
->getRevision(),
484 * @covers Revision::newFromPageId
486 public function testNewFromPageIdWithLatestId() {
487 $rev = Revision
::newFromPageId(
488 $this->testPage
->getId(),
489 $this->testPage
->getLatest()
491 $this->assertRevEquals(
492 $this->testPage
->getRevision(),
498 * @covers Revision::newFromPageId
500 public function testNewFromPageIdWithNotLatestId() {
501 $content = new WikitextContent( __METHOD__
);
502 $this->testPage
->doEditContent( $content, __METHOD__
);
503 $rev = Revision
::newFromPageId(
504 $this->testPage
->getId(),
505 $this->testPage
->getRevision()->getPrevious()->getId()
507 $this->assertRevEquals(
508 $this->testPage
->getRevision()->getPrevious(),
514 * @covers Revision::fetchRevision
516 public function testFetchRevision() {
517 // Hidden process cache assertion below
518 $this->testPage
->getRevision()->getId();
520 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
521 $id = $this->testPage
->getRevision()->getId();
523 $this->hideDeprecated( 'Revision::fetchRevision' );
524 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
526 # note: order is unspecified
528 while ( ( $row = $res->fetchObject() ) ) {
529 $rows[$row->rev_id
] = $row;
532 $this->assertEmpty( $rows, 'expected empty set' );
536 * @covers Revision::getPage
538 public function testGetPage() {
539 $page = $this->testPage
;
541 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
542 $rev = Revision
::newFromId( $orig->getId() );
544 $this->assertEquals( $page->getId(), $rev->getPage() );
548 * @covers Revision::isCurrent
550 public function testIsCurrent() {
551 $rev1 = $this->testPage
->getRevision();
553 # @todo find out if this should be true
554 # $this->assertTrue( $rev1->isCurrent() );
556 $rev1x = Revision
::newFromId( $rev1->getId() );
557 $this->assertTrue( $rev1x->isCurrent() );
559 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
560 $rev2 = $this->testPage
->getRevision();
562 # @todo find out if this should be true
563 # $this->assertTrue( $rev2->isCurrent() );
565 $rev1x = Revision
::newFromId( $rev1->getId() );
566 $this->assertFalse( $rev1x->isCurrent() );
568 $rev2x = Revision
::newFromId( $rev2->getId() );
569 $this->assertTrue( $rev2x->isCurrent() );
573 * @covers Revision::getPrevious
575 public function testGetPrevious() {
576 $oldestRevision = $this->testPage
->getOldestRevision();
577 $latestRevision = $this->testPage
->getLatest();
579 $this->assertNull( $oldestRevision->getPrevious() );
581 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
582 $newRevision = $this->testPage
->getRevision();
584 $this->assertNotNull( $newRevision->getPrevious() );
585 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
589 * @covers Revision::getNext
591 public function testGetNext() {
592 $rev1 = $this->testPage
->getRevision();
594 $this->assertNull( $rev1->getNext() );
596 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
597 $rev2 = $this->testPage
->getRevision();
599 $this->assertNotNull( $rev1->getNext() );
600 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
604 * @covers Revision::newNullRevision
606 public function testNewNullRevision() {
607 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
608 $orig = $this->testPage
->getRevision();
610 $dbw = wfGetDB( DB_MASTER
);
611 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
613 $this->assertNotEquals( $orig->getId(), $rev->getId(),
614 'new null revision should have a different id from the original revision' );
615 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
616 'new null revision should have the same text id as the original revision' );
617 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
618 'new null revision should have the same SHA1 as the original revision' );
619 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
620 'new null revision should have the same content as the original revision' );
621 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
625 * @covers Revision::newNullRevision
627 public function testNewNullRevision_badPage() {
628 $dbw = wfGetDB( DB_MASTER
);
629 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
631 $this->assertNull( $rev );
635 * @covers Revision::insertOn
637 public function testInsertOn() {
638 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
640 $orig = $this->makeRevisionWithProps( [
644 // Make sure the revision was copied to ip_changes
645 $dbr = wfGetDB( DB_REPLICA
);
646 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
647 $row = $res->fetchObject();
649 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
651 $orig->getTimestamp(),
652 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
656 public static function provideUserWasLastToEdit() {
657 yield
'actually the last edit' => [ 3, true ];
658 yield
'not the current edit, but still by this user' => [ 2, true ];
659 yield
'edit by another user' => [ 1, false ];
660 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
664 * @covers Revision::userWasLastToEdit
665 * @dataProvider provideUserWasLastToEdit
667 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
668 $userA = User
::newFromName( "RevisionStorageTest_userA" );
669 $userB = User
::newFromName( "RevisionStorageTest_userB" );
671 if ( $userA->getId() === 0 ) {
672 $userA = User
::createNew( $userA->getName() );
675 if ( $userB->getId() === 0 ) {
676 $userB = User
::createNew( $userB->getName() );
679 $ns = $this->getDefaultWikitextNS();
681 $dbw = wfGetDB( DB_MASTER
);
684 // create revisions -----------------------------
685 $page = WikiPage
::factory( Title
::newFromText(
686 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
687 $page->insertOn( $dbw );
689 $revisions[0] = new Revision( [
690 'page' => $page->getId(),
691 // we need the title to determine the page's default content model
692 'title' => $page->getTitle(),
693 'timestamp' => '20120101000000',
694 'user' => $userA->getId(),
696 'content_model' => CONTENT_MODEL_WIKITEXT
,
697 'comment' => 'edit zero'
699 $revisions[0]->insertOn( $dbw );
701 $revisions[1] = new Revision( [
702 'page' => $page->getId(),
703 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
704 'title' => $page->getTitle(),
705 'timestamp' => '20120101000100',
706 'user' => $userA->getId(),
708 'content_model' => CONTENT_MODEL_WIKITEXT
,
709 'comment' => 'edit one'
711 $revisions[1]->insertOn( $dbw );
713 $revisions[2] = new Revision( [
714 'page' => $page->getId(),
715 'title' => $page->getTitle(),
716 'timestamp' => '20120101000200',
717 'user' => $userB->getId(),
719 'content_model' => CONTENT_MODEL_WIKITEXT
,
720 'comment' => 'edit two'
722 $revisions[2]->insertOn( $dbw );
724 $revisions[3] = new Revision( [
725 'page' => $page->getId(),
726 'title' => $page->getTitle(),
727 'timestamp' => '20120101000300',
728 'user' => $userA->getId(),
730 'content_model' => CONTENT_MODEL_WIKITEXT
,
731 'comment' => 'edit three'
733 $revisions[3]->insertOn( $dbw );
735 $revisions[4] = new Revision( [
736 'page' => $page->getId(),
737 'title' => $page->getTitle(),
738 'timestamp' => '20120101000200',
739 'user' => $userA->getId(),
741 'content_model' => CONTENT_MODEL_WIKITEXT
,
742 'comment' => 'edit four'
744 $revisions[4]->insertOn( $dbw );
746 // test it ---------------------------------
747 $since = $revisions[$sinceIdx]->getTimestamp();
749 $revQuery = Revision
::getQueryInfo();
750 $allRows = iterator_to_array( $dbw->select(
752 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
754 'rev_page' => $page->getId(),
755 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
758 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
762 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
764 $this->assertEquals( $expectedLast, $wasLast );
768 * @param string $text
769 * @param string $title
770 * @param string $model
771 * @param string $format
775 private function newTestRevision( $text, $title = "Test",
776 $model = CONTENT_MODEL_WIKITEXT
, $format = null
778 if ( is_string( $title ) ) {
779 $title = Title
::newFromText( $title );
782 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
790 'content' => $content,
791 'length' => $content->getSize(),
792 'comment' => "testing",
793 'minor_edit' => false,
795 'content_format' => $format,
802 public function provideGetContentModel() {
803 // NOTE: we expect the help namespace to always contain wikitext
805 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
806 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
807 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
812 * @dataProvider provideGetContentModel
813 * @covers Revision::getContentModel
815 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
816 $rev = $this->newTestRevision( $text, $title, $model, $format );
818 $this->assertEquals( $expectedModel, $rev->getContentModel() );
821 public function provideGetContentFormat() {
822 // NOTE: we expect the help namespace to always contain wikitext
824 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
825 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
826 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
827 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
832 * @dataProvider provideGetContentFormat
833 * @covers Revision::getContentFormat
835 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
836 $rev = $this->newTestRevision( $text, $title, $model, $format );
838 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
841 public function provideGetContentHandler() {
842 // NOTE: we expect the help namespace to always contain wikitext
844 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
845 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
846 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
851 * @dataProvider provideGetContentHandler
852 * @covers Revision::getContentHandler
854 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
855 $rev = $this->newTestRevision( $text, $title, $model, $format );
857 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
860 public function provideGetContent() {
861 // NOTE: we expect the help namespace to always contain wikitext
863 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
865 serialize( 'hello world' ),
867 DummyContentForTesting
::MODEL_ID
,
869 Revision
::FOR_PUBLIC
,
870 serialize( 'hello world' )
873 serialize( 'hello world' ),
877 Revision
::FOR_PUBLIC
,
878 serialize( 'hello world' )
884 * @dataProvider provideGetContent
885 * @covers Revision::getContent
887 public function testGetContent( $text, $title, $model, $format,
888 $audience, $expectedSerialization
890 $rev = $this->newTestRevision( $text, $title, $model, $format );
891 $content = $rev->getContent( $audience );
894 $expectedSerialization,
895 is_null( $content ) ?
null : $content->serialize( $format )
900 * @covers Revision::getContent
902 public function testGetContent_failure() {
903 $rev = new Revision( [
904 'page' => $this->testPage
->getId(),
905 'content_model' => $this->testPage
->getContentModel(),
906 'text_id' => 123456789, // not in the test DB
909 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
911 $this->assertNull( $rev->getContent(),
912 "getContent() should return null if the revision's text blob could not be loaded." );
914 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
915 $this->assertNull( $rev->getContent(),
916 "getContent() should return null if the revision's text blob could not be loaded." );
918 Wikimedia\restoreWarnings
();
921 public function provideGetSize() {
923 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
924 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
929 * @covers Revision::getSize
930 * @dataProvider provideGetSize
932 public function testGetSize( $text, $model, $expected_size ) {
933 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
934 $this->assertEquals( $expected_size, $rev->getSize() );
937 public function provideGetSha1() {
939 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
941 serialize( "hello world." ),
942 DummyContentForTesting
::MODEL_ID
,
943 Revision
::base36Sha1( serialize( "hello world." ) )
949 * @covers Revision::getSha1
950 * @dataProvider provideGetSha1
952 public function testGetSha1( $text, $model, $expected_hash ) {
953 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
954 $this->assertEquals( $expected_hash, $rev->getSha1() );
958 * Tests whether $rev->getContent() returns a clone when needed.
960 * @covers Revision::getContent
962 public function testGetContentClone() {
963 $content = new RevisionTestModifyableContent( "foo" );
969 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
971 'content' => $content,
972 'length' => $content->getSize(),
973 'comment' => "testing",
974 'minor_edit' => false,
978 /** @var RevisionTestModifyableContent $content */
979 $content = $rev->getContent( Revision
::RAW
);
980 $content->setText( "bar" );
982 /** @var RevisionTestModifyableContent $content2 */
983 $content2 = $rev->getContent( Revision
::RAW
);
984 // content is mutable, expect clone
985 $this->assertNotSame( $content, $content2, "expected a clone" );
986 // clone should contain the original text
987 $this->assertEquals( "foo", $content2->getText() );
989 $content2->setText( "bla bla" );
990 // clones should be independent
991 $this->assertEquals( "bar", $content->getText() );
995 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
996 * @covers Revision::getContent
998 public function testGetContentUncloned() {
999 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1000 $content = $rev->getContent( Revision
::RAW
);
1001 $content2 = $rev->getContent( Revision
::RAW
);
1003 // for immutable content like wikitext, this should be the same object
1004 $this->assertSame( $content, $content2 );
1008 * @covers Revision::loadFromId
1010 public function testLoadFromId() {
1011 $rev = $this->testPage
->getRevision();
1012 $this->hideDeprecated( 'Revision::loadFromId' );
1013 $this->assertRevEquals(
1015 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1020 * @covers Revision::loadFromPageId
1022 public function testLoadFromPageId() {
1023 $this->assertRevEquals(
1024 $this->testPage
->getRevision(),
1025 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1030 * @covers Revision::loadFromPageId
1032 public function testLoadFromPageIdWithLatestRevId() {
1033 $this->assertRevEquals(
1034 $this->testPage
->getRevision(),
1035 Revision
::loadFromPageId(
1036 wfGetDB( DB_MASTER
),
1037 $this->testPage
->getId(),
1038 $this->testPage
->getLatest()
1044 * @covers Revision::loadFromPageId
1046 public function testLoadFromPageIdWithNotLatestRevId() {
1047 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1048 $this->assertRevEquals(
1049 $this->testPage
->getRevision()->getPrevious(),
1050 Revision
::loadFromPageId(
1051 wfGetDB( DB_MASTER
),
1052 $this->testPage
->getId(),
1053 $this->testPage
->getRevision()->getPrevious()->getId()
1059 * @covers Revision::loadFromTitle
1061 public function testLoadFromTitle() {
1062 $this->assertRevEquals(
1063 $this->testPage
->getRevision(),
1064 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1069 * @covers Revision::loadFromTitle
1071 public function testLoadFromTitleWithLatestRevId() {
1072 $this->assertRevEquals(
1073 $this->testPage
->getRevision(),
1074 Revision
::loadFromTitle(
1075 wfGetDB( DB_MASTER
),
1076 $this->testPage
->getTitle(),
1077 $this->testPage
->getLatest()
1083 * @covers Revision::loadFromTitle
1085 public function testLoadFromTitleWithNotLatestRevId() {
1086 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1087 $this->assertRevEquals(
1088 $this->testPage
->getRevision()->getPrevious(),
1089 Revision
::loadFromTitle(
1090 wfGetDB( DB_MASTER
),
1091 $this->testPage
->getTitle(),
1092 $this->testPage
->getRevision()->getPrevious()->getId()
1098 * @covers Revision::loadFromTimestamp()
1100 public function testLoadFromTimestamp() {
1101 $this->assertRevEquals(
1102 $this->testPage
->getRevision(),
1103 Revision
::loadFromTimestamp(
1104 wfGetDB( DB_MASTER
),
1105 $this->testPage
->getTitle(),
1106 $this->testPage
->getRevision()->getTimestamp()
1112 * @covers Revision::getParentLengths
1114 public function testGetParentLengths_noRevIds() {
1117 Revision
::getParentLengths(
1118 wfGetDB( DB_MASTER
),
1125 * @covers Revision::getParentLengths
1127 public function testGetParentLengths_oneRevId() {
1128 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1129 $textLength = strlen( $text );
1131 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1132 $rev[1] = $this->testPage
->getLatest();
1135 [ $rev[1] => $textLength ],
1136 Revision
::getParentLengths(
1137 wfGetDB( DB_MASTER
),
1144 * @covers Revision::getParentLengths
1146 public function testGetParentLengths_multipleRevIds() {
1147 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1148 $textOneLength = strlen( $textOne );
1149 $textTwo = '831jr091jr092121j09rj1';
1150 $textTwoLength = strlen( $textTwo );
1152 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1153 $rev[1] = $this->testPage
->getLatest();
1154 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1155 $rev[2] = $this->testPage
->getLatest();
1158 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1159 Revision
::getParentLengths(
1160 wfGetDB( DB_MASTER
),
1161 [ $rev[1], $rev[2] ]
1167 * @covers Revision::getTitle
1169 public function testGetTitle_fromExistingRevision() {
1171 $this->testPage
->getTitle()->equals(
1172 $this->testPage
->getRevision()->getTitle()
1178 * @covers Revision::getTitle
1180 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1181 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1183 $this->testPage
->getTitle()->equals(
1190 * @covers Revision::isMinor
1192 public function testIsMinor_true() {
1193 // Use a sysop to ensure we can mark edits as minor
1194 $sysop = $this->getTestSysop()->getUser();
1196 $this->testPage
->doEditContent(
1197 new WikitextContent( __METHOD__
),
1203 $rev = $this->testPage
->getRevision();
1205 $this->assertSame( true, $rev->isMinor() );
1209 * @covers Revision::isMinor
1211 public function testIsMinor_false() {
1212 $this->testPage
->doEditContent(
1213 new WikitextContent( __METHOD__
),
1217 $rev = $this->testPage
->getRevision();
1219 $this->assertSame( false, $rev->isMinor() );
1223 * @covers Revision::getTimestamp
1225 public function testGetTimestamp() {
1226 $testTimestamp = wfTimestampNow();
1228 $this->testPage
->doEditContent(
1229 new WikitextContent( __METHOD__
),
1232 $rev = $this->testPage
->getRevision();
1234 $this->assertInternalType( 'string', $rev->getTimestamp() );
1235 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1236 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1240 * @covers Revision::getUser
1241 * @covers Revision::getUserText
1243 public function testGetUserAndText() {
1244 $sysop = $this->getTestSysop()->getUser();
1246 $this->testPage
->doEditContent(
1247 new WikitextContent( __METHOD__
),
1253 $rev = $this->testPage
->getRevision();
1255 $this->assertSame( $sysop->getId(), $rev->getUser() );
1256 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1260 * @covers Revision::isDeleted
1262 public function testIsDeleted_nothingDeleted() {
1263 $rev = $this->testPage
->getRevision();
1265 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1266 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1267 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1268 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1272 * @covers Revision::getVisibility
1274 public function testGetVisibility_nothingDeleted() {
1275 $rev = $this->testPage
->getRevision();
1277 $this->assertSame( 0, $rev->getVisibility() );
1281 * @covers Revision::getComment
1283 public function testGetComment_notDeleted() {
1284 $expectedSummary = 'goatlicious summary';
1286 $this->testPage
->doEditContent(
1287 new WikitextContent( __METHOD__
),
1290 $rev = $this->testPage
->getRevision();
1292 $this->assertSame( $expectedSummary, $rev->getComment() );
1296 * @covers Revision::isUnpatrolled
1298 public function testIsUnpatrolled_returnsRecentChangesId() {
1299 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1300 $rev = $this->testPage
->getRevision();
1302 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1303 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1307 * @covers Revision::isUnpatrolled
1309 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1310 // This assumes that sysops are auto patrolled
1311 $sysop = $this->getTestSysop()->getUser();
1312 $this->testPage
->doEditContent(
1313 new WikitextContent( __METHOD__
),
1319 $rev = $this->testPage
->getRevision();
1321 $this->assertSame( 0, $rev->isUnpatrolled() );
1325 * This is a simple blanket test for all simple content getters and is methods to provide some
1326 * coverage before the split of Revision into multiple classes for MCR work.
1327 * @covers Revision::getContent
1328 * @covers Revision::getSerializedData
1329 * @covers Revision::getContentModel
1330 * @covers Revision::getContentFormat
1331 * @covers Revision::getContentHandler
1333 public function testSimpleContentGetters() {
1334 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1335 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1337 $this->testPage
->doEditContent(
1338 new WikitextContent( $expectedText ),
1341 $rev = $this->testPage
->getRevision();
1343 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1344 $this->assertSame( $expectedText, $rev->getSerializedData() );
1345 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1346 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1347 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1351 * @covers Revision::newKnownCurrent
1353 public function testNewKnownCurrent() {
1354 // Setup the services
1355 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1356 $this->setService( 'MainWANObjectCache', $cache );
1357 $db = wfGetDB( DB_MASTER
);
1359 // Get a fresh revision to use during testing
1360 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1361 $rev = $this->testPage
->getRevision();
1363 // Clear any previous cache for the revision during creation
1364 $key = $cache->makeGlobalKey( 'revision-row-1.29',
1369 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1370 $this->assertFalse( $cache->get( $key ) );
1372 // Get the new revision and make sure it is in the cache and correct
1373 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1374 $this->assertRevEquals( $rev, $newRev );
1376 $cachedRow = $cache->get( $key );
1377 $this->assertNotFalse( $cachedRow );
1378 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1381 public function testNewKnownCurrent_withPageId() {
1382 $db = wfGetDB( DB_MASTER
);
1384 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1385 $rev = $this->testPage
->getRevision();
1387 $pageId = $this->testPage
->getId();
1389 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1390 $this->assertRevEquals( $rev, $newRev );
1393 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1394 $db = wfGetDB( DB_MASTER
);
1396 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1399 public function provideUserCanBitfield() {
1400 yield
[ 0, 0, [], null, true ];
1401 // Bitfields match, user has no permissions
1402 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1403 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1404 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1405 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1406 // Bitfields match, user (admin) does have permissions
1407 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1408 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1409 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1410 // Bitfields match, user (admin) does not have permissions
1411 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1412 // Bitfields match, user (oversight) does have permissions
1413 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1414 // Check permissions using the title
1416 Revision
::DELETED_TEXT
,
1417 Revision
::DELETED_TEXT
,
1419 Title
::newFromText( __METHOD__
),
1423 Revision
::DELETED_TEXT
,
1424 Revision
::DELETED_TEXT
,
1426 Title
::newFromText( __METHOD__
),
1432 * @dataProvider provideUserCanBitfield
1433 * @covers Revision::userCanBitfield
1435 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1436 $this->setMwGlobals(
1437 'wgGroupPermissions',
1440 'deletedtext' => true,
1441 'deletedhistory' => true,
1444 'viewsuppressed' => true,
1445 'suppressrevision' => true,
1449 $user = $this->getTestUser( $userGroups )->getUser();
1453 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1456 // Fallback to $wgUser
1457 $this->setMwGlobals(
1463 Revision
::userCanBitfield( $bitField, $field, null, $title )
1467 public function provideUserCan() {
1468 yield
[ 0, 0, [], true ];
1469 // Bitfields match, user has no permissions
1470 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1471 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1472 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1473 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1474 // Bitfields match, user (admin) does have permissions
1475 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1476 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1477 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1478 // Bitfields match, user (admin) does not have permissions
1479 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1480 // Bitfields match, user (oversight) does have permissions
1481 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1485 * @dataProvider provideUserCan
1486 * @covers Revision::userCan
1488 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1489 $this->setMwGlobals(
1490 'wgGroupPermissions',
1493 'deletedtext' => true,
1494 'deletedhistory' => true,
1497 'viewsuppressed' => true,
1498 'suppressrevision' => true,
1502 $user = $this->getTestUser( $userGroups )->getUser();
1503 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1507 $revision->userCan( $field, $user )