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' ] ]
276 "rev_id = {$rev->getId()}",
278 strval( $rev->getId() ),
279 strval( $this->testPage
->getId() ),
285 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
291 * @covers Revision::insertOn
293 public function testInsertOn_exceptionOnNoPage() {
294 // If an ExternalStore is set don't use it.
295 $this->setMwGlobals( 'wgDefaultExternalStore', false );
296 $this->setExpectedException(
297 IncompleteRevisionException
::class,
298 "rev_page field must not be 0!"
301 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
302 $rev = new Revision( [], 0, $title );
304 $rev->insertOn( wfGetDB( DB_MASTER
) );
308 * @covers Revision::newFromTitle
310 public function testNewFromTitle_withoutId() {
311 $latestRevId = $this->testPage
->getLatest();
313 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
315 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
316 $this->assertEquals( $latestRevId, $rev->getId() );
320 * @covers Revision::newFromTitle
322 public function testNewFromTitle_withId() {
323 $latestRevId = $this->testPage
->getLatest();
325 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
327 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
328 $this->assertEquals( $latestRevId, $rev->getId() );
332 * @covers Revision::newFromTitle
334 public function testNewFromTitle_withBadId() {
335 $latestRevId = $this->testPage
->getLatest();
337 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
339 $this->assertNull( $rev );
343 * @covers Revision::newFromRow
345 public function testNewFromRow() {
346 $orig = $this->makeRevisionWithProps();
348 $dbr = wfGetDB( DB_REPLICA
);
349 $revQuery = Revision
::getQueryInfo();
350 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
351 __METHOD__
, [], $revQuery['joins'] );
352 $this->assertTrue( is_object( $res ), 'query failed' );
354 $row = $res->fetchObject();
357 $rev = Revision
::newFromRow( $row );
359 $this->assertRevEquals( $orig, $rev );
362 public function provideNewFromArchiveRow() {
370 return $f +
[ 'ar_namespace', 'ar_title' ];
375 unset( $f['ar_text_id'] );
381 unset( $f['ar_page_id'] );
387 unset( $f['ar_parent_id'] );
393 unset( $f['ar_rev_id'] );
399 unset( $f['ar_sha1'] );
406 * @dataProvider provideNewFromArchiveRow
407 * @covers Revision::newFromArchiveRow
409 public function testNewFromArchiveRow( $selectModifier ) {
410 $services = MediaWikiServices
::getInstance();
412 $store = new RevisionStore(
413 $services->getDBLoadBalancer(),
414 $services->getService( '_SqlBlobStore' ),
415 $services->getMainWANObjectCache(),
416 $services->getCommentStore(),
417 $services->getActorMigration()
420 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
421 $this->setService( 'RevisionStore', $store );
423 $page = $this->createPage(
424 'RevisionStorageTest_testNewFromArchiveRow',
426 CONTENT_MODEL_WIKITEXT
428 $orig = $page->getRevision();
429 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
431 $dbr = wfGetDB( DB_REPLICA
);
432 $arQuery = Revision
::getArchiveQueryInfo();
433 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
435 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
436 __METHOD__
, [], $arQuery['joins']
438 $this->assertTrue( is_object( $res ), 'query failed' );
440 $row = $res->fetchObject();
443 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
444 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
445 $rev = Revision
::newFromArchiveRow( $row );
447 $this->assertRevEquals( $orig, $rev );
451 * @covers Revision::newFromArchiveRow
453 public function testNewFromArchiveRowOverrides() {
454 $page = $this->createPage(
455 'RevisionStorageTest_testNewFromArchiveRow',
457 CONTENT_MODEL_WIKITEXT
459 $orig = $page->getRevision();
460 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
462 $dbr = wfGetDB( DB_REPLICA
);
463 $arQuery = Revision
::getArchiveQueryInfo();
465 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
466 __METHOD__
, [], $arQuery['joins']
468 $this->assertTrue( is_object( $res ), 'query failed' );
470 $row = $res->fetchObject();
473 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
475 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
476 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
480 * @covers Revision::newFromId
482 public function testNewFromId() {
483 $orig = $this->testPage
->getRevision();
484 $rev = Revision
::newFromId( $orig->getId() );
485 $this->assertRevEquals( $orig, $rev );
489 * @covers Revision::newFromPageId
491 public function testNewFromPageId() {
492 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
493 $this->assertRevEquals(
494 $this->testPage
->getRevision(),
500 * @covers Revision::newFromPageId
502 public function testNewFromPageIdWithLatestId() {
503 $rev = Revision
::newFromPageId(
504 $this->testPage
->getId(),
505 $this->testPage
->getLatest()
507 $this->assertRevEquals(
508 $this->testPage
->getRevision(),
514 * @covers Revision::newFromPageId
516 public function testNewFromPageIdWithNotLatestId() {
517 $content = new WikitextContent( __METHOD__
);
518 $this->testPage
->doEditContent( $content, __METHOD__
);
519 $rev = Revision
::newFromPageId(
520 $this->testPage
->getId(),
521 $this->testPage
->getRevision()->getPrevious()->getId()
523 $this->assertRevEquals(
524 $this->testPage
->getRevision()->getPrevious(),
530 * @covers Revision::fetchRevision
532 public function testFetchRevision() {
533 // Hidden process cache assertion below
534 $this->testPage
->getRevision()->getId();
536 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
537 $id = $this->testPage
->getRevision()->getId();
539 $this->hideDeprecated( 'Revision::fetchRevision' );
540 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
542 # note: order is unspecified
544 while ( ( $row = $res->fetchObject() ) ) {
545 $rows[$row->rev_id
] = $row;
548 $this->assertEmpty( $rows, 'expected empty set' );
552 * @covers Revision::getPage
554 public function testGetPage() {
555 $page = $this->testPage
;
557 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
558 $rev = Revision
::newFromId( $orig->getId() );
560 $this->assertEquals( $page->getId(), $rev->getPage() );
564 * @covers Revision::isCurrent
566 public function testIsCurrent() {
567 $rev1 = $this->testPage
->getRevision();
569 # @todo find out if this should be true
570 # $this->assertTrue( $rev1->isCurrent() );
572 $rev1x = Revision
::newFromId( $rev1->getId() );
573 $this->assertTrue( $rev1x->isCurrent() );
575 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
576 $rev2 = $this->testPage
->getRevision();
578 # @todo find out if this should be true
579 # $this->assertTrue( $rev2->isCurrent() );
581 $rev1x = Revision
::newFromId( $rev1->getId() );
582 $this->assertFalse( $rev1x->isCurrent() );
584 $rev2x = Revision
::newFromId( $rev2->getId() );
585 $this->assertTrue( $rev2x->isCurrent() );
589 * @covers Revision::getPrevious
591 public function testGetPrevious() {
592 $oldestRevision = $this->testPage
->getOldestRevision();
593 $latestRevision = $this->testPage
->getLatest();
595 $this->assertNull( $oldestRevision->getPrevious() );
597 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
598 $newRevision = $this->testPage
->getRevision();
600 $this->assertNotNull( $newRevision->getPrevious() );
601 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
605 * @covers Revision::getNext
607 public function testGetNext() {
608 $rev1 = $this->testPage
->getRevision();
610 $this->assertNull( $rev1->getNext() );
612 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
613 $rev2 = $this->testPage
->getRevision();
615 $this->assertNotNull( $rev1->getNext() );
616 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
620 * @covers Revision::newNullRevision
622 public function testNewNullRevision() {
623 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
624 $orig = $this->testPage
->getRevision();
626 $dbw = wfGetDB( DB_MASTER
);
627 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
629 $this->assertNotEquals( $orig->getId(), $rev->getId(),
630 'new null revision should have a different id from the original revision' );
631 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
632 'new null revision should have the same text id as the original revision' );
633 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
634 'new null revision should have the same SHA1 as the original revision' );
635 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
636 'new null revision should have the same content as the original revision' );
637 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
641 * @covers Revision::newNullRevision
643 public function testNewNullRevision_badPage() {
644 $dbw = wfGetDB( DB_MASTER
);
645 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
647 $this->assertNull( $rev );
651 * @covers Revision::insertOn
653 public function testInsertOn() {
654 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
656 $orig = $this->makeRevisionWithProps( [
660 // Make sure the revision was copied to ip_changes
661 $dbr = wfGetDB( DB_REPLICA
);
662 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
663 $row = $res->fetchObject();
665 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
667 $orig->getTimestamp(),
668 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
672 public static function provideUserWasLastToEdit() {
673 yield
'actually the last edit' => [ 3, true ];
674 yield
'not the current edit, but still by this user' => [ 2, true ];
675 yield
'edit by another user' => [ 1, false ];
676 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
680 * @covers Revision::userWasLastToEdit
681 * @dataProvider provideUserWasLastToEdit
683 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
684 $userA = User
::newFromName( "RevisionStorageTest_userA" );
685 $userB = User
::newFromName( "RevisionStorageTest_userB" );
687 if ( $userA->getId() === 0 ) {
688 $userA = User
::createNew( $userA->getName() );
691 if ( $userB->getId() === 0 ) {
692 $userB = User
::createNew( $userB->getName() );
695 $ns = $this->getDefaultWikitextNS();
697 $dbw = wfGetDB( DB_MASTER
);
700 // create revisions -----------------------------
701 $page = WikiPage
::factory( Title
::newFromText(
702 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
703 $page->insertOn( $dbw );
705 $revisions[0] = new Revision( [
706 'page' => $page->getId(),
707 // we need the title to determine the page's default content model
708 'title' => $page->getTitle(),
709 'timestamp' => '20120101000000',
710 'user' => $userA->getId(),
712 'content_model' => CONTENT_MODEL_WIKITEXT
,
713 'comment' => 'edit zero'
715 $revisions[0]->insertOn( $dbw );
717 $revisions[1] = new Revision( [
718 'page' => $page->getId(),
719 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
720 'title' => $page->getTitle(),
721 'timestamp' => '20120101000100',
722 'user' => $userA->getId(),
724 'content_model' => CONTENT_MODEL_WIKITEXT
,
725 'comment' => 'edit one'
727 $revisions[1]->insertOn( $dbw );
729 $revisions[2] = new Revision( [
730 'page' => $page->getId(),
731 'title' => $page->getTitle(),
732 'timestamp' => '20120101000200',
733 'user' => $userB->getId(),
735 'content_model' => CONTENT_MODEL_WIKITEXT
,
736 'comment' => 'edit two'
738 $revisions[2]->insertOn( $dbw );
740 $revisions[3] = new Revision( [
741 'page' => $page->getId(),
742 'title' => $page->getTitle(),
743 'timestamp' => '20120101000300',
744 'user' => $userA->getId(),
746 'content_model' => CONTENT_MODEL_WIKITEXT
,
747 'comment' => 'edit three'
749 $revisions[3]->insertOn( $dbw );
751 $revisions[4] = new Revision( [
752 'page' => $page->getId(),
753 'title' => $page->getTitle(),
754 'timestamp' => '20120101000200',
755 'user' => $userA->getId(),
757 'content_model' => CONTENT_MODEL_WIKITEXT
,
758 'comment' => 'edit four'
760 $revisions[4]->insertOn( $dbw );
762 // test it ---------------------------------
763 $since = $revisions[$sinceIdx]->getTimestamp();
765 $revQuery = Revision
::getQueryInfo();
766 $allRows = iterator_to_array( $dbw->select(
768 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
770 'rev_page' => $page->getId(),
771 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
774 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
778 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
780 $this->assertEquals( $expectedLast, $wasLast );
784 * @param string $text
785 * @param string $title
786 * @param string $model
787 * @param string $format
791 private function newTestRevision( $text, $title = "Test",
792 $model = CONTENT_MODEL_WIKITEXT
, $format = null
794 if ( is_string( $title ) ) {
795 $title = Title
::newFromText( $title );
798 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
806 'content' => $content,
807 'length' => $content->getSize(),
808 'comment' => "testing",
809 'minor_edit' => false,
811 'content_format' => $format,
818 public function provideGetContentModel() {
819 // NOTE: we expect the help namespace to always contain wikitext
821 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
822 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
823 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
828 * @dataProvider provideGetContentModel
829 * @covers Revision::getContentModel
831 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
832 $rev = $this->newTestRevision( $text, $title, $model, $format );
834 $this->assertEquals( $expectedModel, $rev->getContentModel() );
837 public function provideGetContentFormat() {
838 // NOTE: we expect the help namespace to always contain wikitext
840 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
841 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
842 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
843 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
848 * @dataProvider provideGetContentFormat
849 * @covers Revision::getContentFormat
851 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
852 $rev = $this->newTestRevision( $text, $title, $model, $format );
854 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
857 public function provideGetContentHandler() {
858 // NOTE: we expect the help namespace to always contain wikitext
860 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
861 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
862 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
867 * @dataProvider provideGetContentHandler
868 * @covers Revision::getContentHandler
870 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
871 $rev = $this->newTestRevision( $text, $title, $model, $format );
873 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
876 public function provideGetContent() {
877 // NOTE: we expect the help namespace to always contain wikitext
879 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
881 serialize( 'hello world' ),
883 DummyContentForTesting
::MODEL_ID
,
885 Revision
::FOR_PUBLIC
,
886 serialize( 'hello world' )
889 serialize( 'hello world' ),
893 Revision
::FOR_PUBLIC
,
894 serialize( 'hello world' )
900 * @dataProvider provideGetContent
901 * @covers Revision::getContent
903 public function testGetContent( $text, $title, $model, $format,
904 $audience, $expectedSerialization
906 $rev = $this->newTestRevision( $text, $title, $model, $format );
907 $content = $rev->getContent( $audience );
910 $expectedSerialization,
911 is_null( $content ) ?
null : $content->serialize( $format )
916 * @covers Revision::getContent
918 public function testGetContent_failure() {
919 $rev = new Revision( [
920 'page' => $this->testPage
->getId(),
921 'content_model' => $this->testPage
->getContentModel(),
922 'text_id' => 123456789, // not in the test DB
925 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
927 $this->assertNull( $rev->getContent(),
928 "getContent() should return null if the revision's text blob could not be loaded." );
930 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
931 $this->assertNull( $rev->getContent(),
932 "getContent() should return null if the revision's text blob could not be loaded." );
934 Wikimedia\restoreWarnings
();
937 public function provideGetSize() {
939 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
940 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
945 * @covers Revision::getSize
946 * @dataProvider provideGetSize
948 public function testGetSize( $text, $model, $expected_size ) {
949 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
950 $this->assertEquals( $expected_size, $rev->getSize() );
953 public function provideGetSha1() {
955 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
957 serialize( "hello world." ),
958 DummyContentForTesting
::MODEL_ID
,
959 Revision
::base36Sha1( serialize( "hello world." ) )
965 * @covers Revision::getSha1
966 * @dataProvider provideGetSha1
968 public function testGetSha1( $text, $model, $expected_hash ) {
969 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
970 $this->assertEquals( $expected_hash, $rev->getSha1() );
974 * Tests whether $rev->getContent() returns a clone when needed.
976 * @covers Revision::getContent
978 public function testGetContentClone() {
979 $content = new RevisionTestModifyableContent( "foo" );
985 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
987 'content' => $content,
988 'length' => $content->getSize(),
989 'comment' => "testing",
990 'minor_edit' => false,
994 /** @var RevisionTestModifyableContent $content */
995 $content = $rev->getContent( Revision
::RAW
);
996 $content->setText( "bar" );
998 /** @var RevisionTestModifyableContent $content2 */
999 $content2 = $rev->getContent( Revision
::RAW
);
1000 // content is mutable, expect clone
1001 $this->assertNotSame( $content, $content2, "expected a clone" );
1002 // clone should contain the original text
1003 $this->assertEquals( "foo", $content2->getText() );
1005 $content2->setText( "bla bla" );
1006 // clones should be independent
1007 $this->assertEquals( "bar", $content->getText() );
1011 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1012 * @covers Revision::getContent
1014 public function testGetContentUncloned() {
1015 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1016 $content = $rev->getContent( Revision
::RAW
);
1017 $content2 = $rev->getContent( Revision
::RAW
);
1019 // for immutable content like wikitext, this should be the same object
1020 $this->assertSame( $content, $content2 );
1024 * @covers Revision::loadFromId
1026 public function testLoadFromId() {
1027 $rev = $this->testPage
->getRevision();
1028 $this->hideDeprecated( 'Revision::loadFromId' );
1029 $this->assertRevEquals(
1031 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1036 * @covers Revision::loadFromPageId
1038 public function testLoadFromPageId() {
1039 $this->assertRevEquals(
1040 $this->testPage
->getRevision(),
1041 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1046 * @covers Revision::loadFromPageId
1048 public function testLoadFromPageIdWithLatestRevId() {
1049 $this->assertRevEquals(
1050 $this->testPage
->getRevision(),
1051 Revision
::loadFromPageId(
1052 wfGetDB( DB_MASTER
),
1053 $this->testPage
->getId(),
1054 $this->testPage
->getLatest()
1060 * @covers Revision::loadFromPageId
1062 public function testLoadFromPageIdWithNotLatestRevId() {
1063 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1064 $this->assertRevEquals(
1065 $this->testPage
->getRevision()->getPrevious(),
1066 Revision
::loadFromPageId(
1067 wfGetDB( DB_MASTER
),
1068 $this->testPage
->getId(),
1069 $this->testPage
->getRevision()->getPrevious()->getId()
1075 * @covers Revision::loadFromTitle
1077 public function testLoadFromTitle() {
1078 $this->assertRevEquals(
1079 $this->testPage
->getRevision(),
1080 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1085 * @covers Revision::loadFromTitle
1087 public function testLoadFromTitleWithLatestRevId() {
1088 $this->assertRevEquals(
1089 $this->testPage
->getRevision(),
1090 Revision
::loadFromTitle(
1091 wfGetDB( DB_MASTER
),
1092 $this->testPage
->getTitle(),
1093 $this->testPage
->getLatest()
1099 * @covers Revision::loadFromTitle
1101 public function testLoadFromTitleWithNotLatestRevId() {
1102 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1103 $this->assertRevEquals(
1104 $this->testPage
->getRevision()->getPrevious(),
1105 Revision
::loadFromTitle(
1106 wfGetDB( DB_MASTER
),
1107 $this->testPage
->getTitle(),
1108 $this->testPage
->getRevision()->getPrevious()->getId()
1114 * @covers Revision::loadFromTimestamp()
1116 public function testLoadFromTimestamp() {
1117 $this->assertRevEquals(
1118 $this->testPage
->getRevision(),
1119 Revision
::loadFromTimestamp(
1120 wfGetDB( DB_MASTER
),
1121 $this->testPage
->getTitle(),
1122 $this->testPage
->getRevision()->getTimestamp()
1128 * @covers Revision::getParentLengths
1130 public function testGetParentLengths_noRevIds() {
1133 Revision
::getParentLengths(
1134 wfGetDB( DB_MASTER
),
1141 * @covers Revision::getParentLengths
1143 public function testGetParentLengths_oneRevId() {
1144 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1145 $textLength = strlen( $text );
1147 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1148 $rev[1] = $this->testPage
->getLatest();
1151 [ $rev[1] => $textLength ],
1152 Revision
::getParentLengths(
1153 wfGetDB( DB_MASTER
),
1160 * @covers Revision::getParentLengths
1162 public function testGetParentLengths_multipleRevIds() {
1163 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1164 $textOneLength = strlen( $textOne );
1165 $textTwo = '831jr091jr092121j09rj1';
1166 $textTwoLength = strlen( $textTwo );
1168 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1169 $rev[1] = $this->testPage
->getLatest();
1170 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1171 $rev[2] = $this->testPage
->getLatest();
1174 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1175 Revision
::getParentLengths(
1176 wfGetDB( DB_MASTER
),
1177 [ $rev[1], $rev[2] ]
1183 * @covers Revision::getTitle
1185 public function testGetTitle_fromExistingRevision() {
1187 $this->testPage
->getTitle()->equals(
1188 $this->testPage
->getRevision()->getTitle()
1194 * @covers Revision::getTitle
1196 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1197 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1199 $this->testPage
->getTitle()->equals(
1206 * @covers Revision::isMinor
1208 public function testIsMinor_true() {
1209 // Use a sysop to ensure we can mark edits as minor
1210 $sysop = $this->getTestSysop()->getUser();
1212 $this->testPage
->doEditContent(
1213 new WikitextContent( __METHOD__
),
1219 $rev = $this->testPage
->getRevision();
1221 $this->assertSame( true, $rev->isMinor() );
1225 * @covers Revision::isMinor
1227 public function testIsMinor_false() {
1228 $this->testPage
->doEditContent(
1229 new WikitextContent( __METHOD__
),
1233 $rev = $this->testPage
->getRevision();
1235 $this->assertSame( false, $rev->isMinor() );
1239 * @covers Revision::getTimestamp
1241 public function testGetTimestamp() {
1242 $testTimestamp = wfTimestampNow();
1244 $this->testPage
->doEditContent(
1245 new WikitextContent( __METHOD__
),
1248 $rev = $this->testPage
->getRevision();
1250 $this->assertInternalType( 'string', $rev->getTimestamp() );
1251 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1252 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1256 * @covers Revision::getUser
1257 * @covers Revision::getUserText
1259 public function testGetUserAndText() {
1260 $sysop = $this->getTestSysop()->getUser();
1262 $this->testPage
->doEditContent(
1263 new WikitextContent( __METHOD__
),
1269 $rev = $this->testPage
->getRevision();
1271 $this->assertSame( $sysop->getId(), $rev->getUser() );
1272 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1276 * @covers Revision::isDeleted
1278 public function testIsDeleted_nothingDeleted() {
1279 $rev = $this->testPage
->getRevision();
1281 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1282 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1283 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1284 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1288 * @covers Revision::getVisibility
1290 public function testGetVisibility_nothingDeleted() {
1291 $rev = $this->testPage
->getRevision();
1293 $this->assertSame( 0, $rev->getVisibility() );
1297 * @covers Revision::getComment
1299 public function testGetComment_notDeleted() {
1300 $expectedSummary = 'goatlicious summary';
1302 $this->testPage
->doEditContent(
1303 new WikitextContent( __METHOD__
),
1306 $rev = $this->testPage
->getRevision();
1308 $this->assertSame( $expectedSummary, $rev->getComment() );
1312 * @covers Revision::isUnpatrolled
1314 public function testIsUnpatrolled_returnsRecentChangesId() {
1315 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1316 $rev = $this->testPage
->getRevision();
1318 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1319 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1323 * @covers Revision::isUnpatrolled
1325 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1326 // This assumes that sysops are auto patrolled
1327 $sysop = $this->getTestSysop()->getUser();
1328 $this->testPage
->doEditContent(
1329 new WikitextContent( __METHOD__
),
1335 $rev = $this->testPage
->getRevision();
1337 $this->assertSame( 0, $rev->isUnpatrolled() );
1341 * This is a simple blanket test for all simple content getters and is methods to provide some
1342 * coverage before the split of Revision into multiple classes for MCR work.
1343 * @covers Revision::getContent
1344 * @covers Revision::getSerializedData
1345 * @covers Revision::getContentModel
1346 * @covers Revision::getContentFormat
1347 * @covers Revision::getContentHandler
1349 public function testSimpleContentGetters() {
1350 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1351 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1353 $this->testPage
->doEditContent(
1354 new WikitextContent( $expectedText ),
1357 $rev = $this->testPage
->getRevision();
1359 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1360 $this->assertSame( $expectedText, $rev->getSerializedData() );
1361 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1362 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1363 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1367 * @covers Revision::newKnownCurrent
1369 public function testNewKnownCurrent() {
1370 // Setup the services
1371 $this->resetGlobalServices();
1372 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1373 $this->setService( 'MainWANObjectCache', $cache );
1374 $db = wfGetDB( DB_MASTER
);
1376 // Get a fresh revision to use during testing
1377 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1378 $rev = $this->testPage
->getRevision();
1380 // Clear any previous cache for the revision during creation
1381 $key = $cache->makeGlobalKey( 'revision-row-1.29',
1386 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1387 $this->assertFalse( $cache->get( $key ) );
1389 // Get the new revision and make sure it is in the cache and correct
1390 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1391 $this->assertRevEquals( $rev, $newRev );
1393 $cachedRow = $cache->get( $key );
1394 $this->assertNotFalse( $cachedRow );
1395 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1398 public function testNewKnownCurrent_withPageId() {
1399 $db = wfGetDB( DB_MASTER
);
1401 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1402 $rev = $this->testPage
->getRevision();
1404 $pageId = $this->testPage
->getId();
1406 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1407 $this->assertRevEquals( $rev, $newRev );
1410 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1411 $db = wfGetDB( DB_MASTER
);
1413 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1416 public function provideUserCanBitfield() {
1417 yield
[ 0, 0, [], null, true ];
1418 // Bitfields match, user has no permissions
1419 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1420 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1421 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1422 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1423 // Bitfields match, user (admin) does have permissions
1424 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1425 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1426 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1427 // Bitfields match, user (admin) does not have permissions
1428 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1429 // Bitfields match, user (oversight) does have permissions
1430 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1431 // Check permissions using the title
1433 Revision
::DELETED_TEXT
,
1434 Revision
::DELETED_TEXT
,
1436 Title
::newFromText( __METHOD__
),
1440 Revision
::DELETED_TEXT
,
1441 Revision
::DELETED_TEXT
,
1443 Title
::newFromText( __METHOD__
),
1449 * @dataProvider provideUserCanBitfield
1450 * @covers Revision::userCanBitfield
1452 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1453 $this->setMwGlobals(
1454 'wgGroupPermissions',
1457 'deletedtext' => true,
1458 'deletedhistory' => true,
1461 'viewsuppressed' => true,
1462 'suppressrevision' => true,
1466 $user = $this->getTestUser( $userGroups )->getUser();
1470 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1473 // Fallback to $wgUser
1474 $this->setMwGlobals(
1480 Revision
::userCanBitfield( $bitField, $field, null, $title )
1484 public function provideUserCan() {
1485 yield
[ 0, 0, [], true ];
1486 // Bitfields match, user has no permissions
1487 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1488 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1489 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1490 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1491 // Bitfields match, user (admin) does have permissions
1492 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1493 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1494 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1495 // Bitfields match, user (admin) does not have permissions
1496 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1497 // Bitfields match, user (oversight) does have permissions
1498 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1502 * @dataProvider provideUserCan
1503 * @covers Revision::userCan
1505 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1506 $this->setMwGlobals(
1507 'wgGroupPermissions',
1510 'deletedtext' => true,
1511 'deletedhistory' => true,
1514 'viewsuppressed' => true,
1515 'suppressrevision' => true,
1519 $user = $this->getTestUser( $userGroups )->getUser();
1520 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1524 $revision->userCan( $field, $user )