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
,
49 abstract protected function getMcrMigrationStage();
54 abstract protected function getMcrTablesToReset();
56 protected function setUp() {
59 $this->tablesUsed +
= $this->getMcrTablesToReset();
63 $this->mergeMwGlobalArrayValue(
67 12313 => 'Dummy_talk',
71 $this->mergeMwGlobalArrayValue(
72 'wgNamespaceContentModels',
74 12312 => DummyContentForTesting
::MODEL_ID
,
78 $this->mergeMwGlobalArrayValue(
81 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
82 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
86 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
88 'wgMultiContentRevisionSchemaMigrationStage',
89 $this->getMcrMigrationStage()
92 MWNamespace
::clearCaches();
93 // Reset namespace cache
94 $wgContLang->resetNamespaces();
96 $this->overrideMwServices();
98 if ( !$this->testPage
) {
100 * We have to create a new page for each subclass as the page creation may result
101 * in different DB fields being filled based on configuration.
103 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
107 protected function tearDown() {
112 MWNamespace
::clearCaches();
113 // Reset namespace cache
114 $wgContLang->resetNamespaces();
117 abstract protected function getContentHandlerUseDB();
119 private function makeRevisionWithProps( $props = null ) {
120 if ( $props === null ) {
124 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
125 $props['text'] = 'Lorem Ipsum';
128 if ( !isset( $props['user_text'] ) ) {
129 $user = $this->getTestUser()->getUser();
130 $props['user_text'] = $user->getName();
131 $props['user'] = $user->getId();
134 if ( !isset( $props['user'] ) ) {
138 if ( !isset( $props['comment'] ) ) {
139 $props['comment'] = 'just a test';
142 if ( !isset( $props['page'] ) ) {
143 $props['page'] = $this->testPage
->getId();
146 if ( !isset( $props['content_model'] ) ) {
147 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
150 $rev = new Revision( $props );
152 $dbw = wfGetDB( DB_MASTER
);
153 $rev->insertOn( $dbw );
159 * @param string $titleString
160 * @param string $text
161 * @param string|null $model
165 private function createPage( $titleString, $text, $model = null ) {
166 if ( !preg_match( '/:/', $titleString ) &&
167 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
169 $ns = $this->getDefaultWikitextNS();
170 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
173 $title = Title
::newFromText( $titleString );
174 $wikipage = new WikiPage( $title );
176 // Delete the article if it already exists
177 if ( $wikipage->exists() ) {
178 $wikipage->doDeleteArticle( "done" );
181 $content = ContentHandler
::makeContent( $text, $title, $model );
182 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
187 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
188 $this->assertNotNull( $rev, 'missing revision' );
190 $this->assertEquals( $orig->getId(), $rev->getId() );
191 $this->assertEquals( $orig->getPage(), $rev->getPage() );
192 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
193 $this->assertEquals( $orig->getUser(), $rev->getUser() );
194 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
195 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
196 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
200 * @covers Revision::getRecentChange
202 public function testGetRecentChange() {
203 $rev = $this->testPage
->getRevision();
204 $recentChange = $rev->getRecentChange();
206 // Make sure various attributes look right / the correct entry has been retrieved.
207 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
209 $rev->getTitle()->getNamespace(),
210 $recentChange->getAttribute( 'rc_namespace' )
213 $rev->getTitle()->getDBkey(),
214 $recentChange->getAttribute( 'rc_title' )
216 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
217 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
218 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
219 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
220 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
224 * @covers Revision::insertOn
226 public function testInsertOn_success() {
227 $parentId = $this->testPage
->getLatest();
229 // If an ExternalStore is set don't use it.
230 $this->setMwGlobals( 'wgDefaultExternalStore', false );
232 $rev = new Revision( [
233 'page' => $this->testPage
->getId(),
234 'title' => $this->testPage
->getTitle(),
235 'text' => 'Revision Text',
236 'comment' => 'Revision comment',
239 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
241 $this->assertInternalType( 'integer', $revId );
242 $this->assertSame( $revId, $rev->getId() );
244 // getTextId() must be an int!
245 $this->assertInternalType( 'integer', $rev->getTextId() );
247 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
249 // we currently only support storage in the text table
250 $textId = MediaWikiServices
::getInstance()
252 ->getTextIdFromAddress( $mainSlot->getAddress() );
256 [ 'old_id', 'old_text' ],
258 [ [ strval( $textId ), 'Revision Text' ] ]
272 "rev_id = {$rev->getId()}",
274 strval( $rev->getId() ),
275 strval( $this->testPage
->getId() ),
281 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
287 * @covers Revision::insertOn
289 public function testInsertOn_exceptionOnNoPage() {
290 // If an ExternalStore is set don't use it.
291 $this->setMwGlobals( 'wgDefaultExternalStore', false );
292 $this->setExpectedException(
293 IncompleteRevisionException
::class,
294 "rev_page field must not be 0!"
297 $title = Title
::newFromText( 'Nonexistant-' . __METHOD__
);
298 $rev = new Revision( [], 0, $title );
300 $rev->insertOn( wfGetDB( DB_MASTER
) );
304 * @covers Revision::newFromTitle
306 public function testNewFromTitle_withoutId() {
307 $latestRevId = $this->testPage
->getLatest();
309 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
311 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
312 $this->assertEquals( $latestRevId, $rev->getId() );
316 * @covers Revision::newFromTitle
318 public function testNewFromTitle_withId() {
319 $latestRevId = $this->testPage
->getLatest();
321 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
323 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
324 $this->assertEquals( $latestRevId, $rev->getId() );
328 * @covers Revision::newFromTitle
330 public function testNewFromTitle_withBadId() {
331 $latestRevId = $this->testPage
->getLatest();
333 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
335 $this->assertNull( $rev );
339 * @covers Revision::newFromRow
341 public function testNewFromRow() {
342 $orig = $this->makeRevisionWithProps();
344 $dbr = wfGetDB( DB_REPLICA
);
345 $revQuery = Revision
::getQueryInfo();
346 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
347 __METHOD__
, [], $revQuery['joins'] );
348 $this->assertTrue( is_object( $res ), 'query failed' );
350 $row = $res->fetchObject();
353 $rev = Revision
::newFromRow( $row );
355 $this->assertRevEquals( $orig, $rev );
358 public function provideNewFromArchiveRow() {
366 return $f +
[ 'ar_namespace', 'ar_title' ];
371 unset( $f['ar_text_id'] );
377 unset( $f['ar_page_id'] );
383 unset( $f['ar_parent_id'] );
389 unset( $f['ar_rev_id'] );
395 unset( $f['ar_sha1'] );
402 * @dataProvider provideNewFromArchiveRow
403 * @covers Revision::newFromArchiveRow
405 public function testNewFromArchiveRow( $selectModifier ) {
406 $services = MediaWikiServices
::getInstance();
408 $store = new RevisionStore(
409 $services->getDBLoadBalancer(),
410 $services->getService( '_SqlBlobStore' ),
411 $services->getMainWANObjectCache(),
412 $services->getCommentStore(),
413 $services->getActorMigration()
416 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
417 $this->setService( 'RevisionStore', $store );
419 $page = $this->createPage(
420 'RevisionStorageTest_testNewFromArchiveRow',
422 CONTENT_MODEL_WIKITEXT
424 $orig = $page->getRevision();
425 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
427 $dbr = wfGetDB( DB_REPLICA
);
428 $arQuery = Revision
::getArchiveQueryInfo();
429 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
431 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
432 __METHOD__
, [], $arQuery['joins']
434 $this->assertTrue( is_object( $res ), 'query failed' );
436 $row = $res->fetchObject();
439 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
440 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
441 $rev = Revision
::newFromArchiveRow( $row );
443 $this->assertRevEquals( $orig, $rev );
447 * @covers Revision::newFromArchiveRow
449 public function testNewFromArchiveRowOverrides() {
450 $page = $this->createPage(
451 'RevisionStorageTest_testNewFromArchiveRow',
453 CONTENT_MODEL_WIKITEXT
455 $orig = $page->getRevision();
456 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
458 $dbr = wfGetDB( DB_REPLICA
);
459 $arQuery = Revision
::getArchiveQueryInfo();
461 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
462 __METHOD__
, [], $arQuery['joins']
464 $this->assertTrue( is_object( $res ), 'query failed' );
466 $row = $res->fetchObject();
469 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
471 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
472 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
476 * @covers Revision::newFromId
478 public function testNewFromId() {
479 $orig = $this->testPage
->getRevision();
480 $rev = Revision
::newFromId( $orig->getId() );
481 $this->assertRevEquals( $orig, $rev );
485 * @covers Revision::newFromPageId
487 public function testNewFromPageId() {
488 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
489 $this->assertRevEquals(
490 $this->testPage
->getRevision(),
496 * @covers Revision::newFromPageId
498 public function testNewFromPageIdWithLatestId() {
499 $rev = Revision
::newFromPageId(
500 $this->testPage
->getId(),
501 $this->testPage
->getLatest()
503 $this->assertRevEquals(
504 $this->testPage
->getRevision(),
510 * @covers Revision::newFromPageId
512 public function testNewFromPageIdWithNotLatestId() {
513 $content = new WikitextContent( __METHOD__
);
514 $this->testPage
->doEditContent( $content, __METHOD__
);
515 $rev = Revision
::newFromPageId(
516 $this->testPage
->getId(),
517 $this->testPage
->getRevision()->getPrevious()->getId()
519 $this->assertRevEquals(
520 $this->testPage
->getRevision()->getPrevious(),
526 * @covers Revision::fetchRevision
528 public function testFetchRevision() {
529 // Hidden process cache assertion below
530 $this->testPage
->getRevision()->getId();
532 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
533 $id = $this->testPage
->getRevision()->getId();
535 $this->hideDeprecated( 'Revision::fetchRevision' );
536 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
538 # note: order is unspecified
540 while ( ( $row = $res->fetchObject() ) ) {
541 $rows[$row->rev_id
] = $row;
544 $this->assertEmpty( $rows, 'expected empty set' );
548 * @covers Revision::getPage
550 public function testGetPage() {
551 $page = $this->testPage
;
553 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
554 $rev = Revision
::newFromId( $orig->getId() );
556 $this->assertEquals( $page->getId(), $rev->getPage() );
560 * @covers Revision::isCurrent
562 public function testIsCurrent() {
563 $rev1 = $this->testPage
->getRevision();
565 # @todo find out if this should be true
566 # $this->assertTrue( $rev1->isCurrent() );
568 $rev1x = Revision
::newFromId( $rev1->getId() );
569 $this->assertTrue( $rev1x->isCurrent() );
571 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
572 $rev2 = $this->testPage
->getRevision();
574 # @todo find out if this should be true
575 # $this->assertTrue( $rev2->isCurrent() );
577 $rev1x = Revision
::newFromId( $rev1->getId() );
578 $this->assertFalse( $rev1x->isCurrent() );
580 $rev2x = Revision
::newFromId( $rev2->getId() );
581 $this->assertTrue( $rev2x->isCurrent() );
585 * @covers Revision::getPrevious
587 public function testGetPrevious() {
588 $oldestRevision = $this->testPage
->getOldestRevision();
589 $latestRevision = $this->testPage
->getLatest();
591 $this->assertNull( $oldestRevision->getPrevious() );
593 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
594 $newRevision = $this->testPage
->getRevision();
596 $this->assertNotNull( $newRevision->getPrevious() );
597 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
601 * @covers Revision::getNext
603 public function testGetNext() {
604 $rev1 = $this->testPage
->getRevision();
606 $this->assertNull( $rev1->getNext() );
608 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
609 $rev2 = $this->testPage
->getRevision();
611 $this->assertNotNull( $rev1->getNext() );
612 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
616 * @covers Revision::newNullRevision
618 public function testNewNullRevision() {
619 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
620 $orig = $this->testPage
->getRevision();
622 $dbw = wfGetDB( DB_MASTER
);
623 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
625 $this->assertNotEquals( $orig->getId(), $rev->getId(),
626 'new null revision should have a different id from the original revision' );
627 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
628 'new null revision should have the same text id as the original revision' );
629 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
630 'new null revision should have the same SHA1 as the original revision' );
631 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
632 'new null revision should have the same content as the original revision' );
633 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
637 * @covers Revision::newNullRevision
639 public function testNewNullRevision_badPage() {
640 $dbw = wfGetDB( DB_MASTER
);
641 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
643 $this->assertNull( $rev );
647 * @covers Revision::insertOn
649 public function testInsertOn() {
650 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
652 $orig = $this->makeRevisionWithProps( [
656 // Make sure the revision was copied to ip_changes
657 $dbr = wfGetDB( DB_REPLICA
);
658 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
659 $row = $res->fetchObject();
661 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
663 $orig->getTimestamp(),
664 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
668 public static function provideUserWasLastToEdit() {
669 yield
'actually the last edit' => [ 3, true ];
670 yield
'not the current edit, but still by this user' => [ 2, true ];
671 yield
'edit by another user' => [ 1, false ];
672 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
676 * @covers Revision::userWasLastToEdit
677 * @dataProvider provideUserWasLastToEdit
679 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
680 $userA = User
::newFromName( "RevisionStorageTest_userA" );
681 $userB = User
::newFromName( "RevisionStorageTest_userB" );
683 if ( $userA->getId() === 0 ) {
684 $userA = User
::createNew( $userA->getName() );
687 if ( $userB->getId() === 0 ) {
688 $userB = User
::createNew( $userB->getName() );
691 $ns = $this->getDefaultWikitextNS();
693 $dbw = wfGetDB( DB_MASTER
);
696 // create revisions -----------------------------
697 $page = WikiPage
::factory( Title
::newFromText(
698 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
699 $page->insertOn( $dbw );
701 $revisions[0] = new Revision( [
702 'page' => $page->getId(),
703 // we need the title to determine the page's default content model
704 'title' => $page->getTitle(),
705 'timestamp' => '20120101000000',
706 'user' => $userA->getId(),
708 'content_model' => CONTENT_MODEL_WIKITEXT
,
709 'comment' => 'edit zero'
711 $revisions[0]->insertOn( $dbw );
713 $revisions[1] = new Revision( [
714 'page' => $page->getId(),
715 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
716 'title' => $page->getTitle(),
717 'timestamp' => '20120101000100',
718 'user' => $userA->getId(),
720 'content_model' => CONTENT_MODEL_WIKITEXT
,
721 'comment' => 'edit one'
723 $revisions[1]->insertOn( $dbw );
725 $revisions[2] = new Revision( [
726 'page' => $page->getId(),
727 'title' => $page->getTitle(),
728 'timestamp' => '20120101000200',
729 'user' => $userB->getId(),
731 'content_model' => CONTENT_MODEL_WIKITEXT
,
732 'comment' => 'edit two'
734 $revisions[2]->insertOn( $dbw );
736 $revisions[3] = new Revision( [
737 'page' => $page->getId(),
738 'title' => $page->getTitle(),
739 'timestamp' => '20120101000300',
740 'user' => $userA->getId(),
742 'content_model' => CONTENT_MODEL_WIKITEXT
,
743 'comment' => 'edit three'
745 $revisions[3]->insertOn( $dbw );
747 $revisions[4] = new Revision( [
748 'page' => $page->getId(),
749 'title' => $page->getTitle(),
750 'timestamp' => '20120101000200',
751 'user' => $userA->getId(),
753 'content_model' => CONTENT_MODEL_WIKITEXT
,
754 'comment' => 'edit four'
756 $revisions[4]->insertOn( $dbw );
758 // test it ---------------------------------
759 $since = $revisions[$sinceIdx]->getTimestamp();
761 $revQuery = Revision
::getQueryInfo();
762 $allRows = iterator_to_array( $dbw->select(
764 [ 'rev_id', 'rev_timestamp', 'rev_user' => $revQuery['fields']['rev_user'] ],
766 'rev_page' => $page->getId(),
767 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
770 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ],
774 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
776 $this->assertEquals( $expectedLast, $wasLast );
780 * @param string $text
781 * @param string $title
782 * @param string $model
783 * @param string $format
787 private function newTestRevision( $text, $title = "Test",
788 $model = CONTENT_MODEL_WIKITEXT
, $format = null
790 if ( is_string( $title ) ) {
791 $title = Title
::newFromText( $title );
794 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
802 'content' => $content,
803 'length' => $content->getSize(),
804 'comment' => "testing",
805 'minor_edit' => false,
807 'content_format' => $format,
814 public function provideGetContentModel() {
815 // NOTE: we expect the help namespace to always contain wikitext
817 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
818 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
819 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
824 * @dataProvider provideGetContentModel
825 * @covers Revision::getContentModel
827 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
828 $rev = $this->newTestRevision( $text, $title, $model, $format );
830 $this->assertEquals( $expectedModel, $rev->getContentModel() );
833 public function provideGetContentFormat() {
834 // NOTE: we expect the help namespace to always contain wikitext
836 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
837 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
838 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
839 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
844 * @dataProvider provideGetContentFormat
845 * @covers Revision::getContentFormat
847 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
848 $rev = $this->newTestRevision( $text, $title, $model, $format );
850 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
853 public function provideGetContentHandler() {
854 // NOTE: we expect the help namespace to always contain wikitext
856 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
857 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
858 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
863 * @dataProvider provideGetContentHandler
864 * @covers Revision::getContentHandler
866 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
867 $rev = $this->newTestRevision( $text, $title, $model, $format );
869 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
872 public function provideGetContent() {
873 // NOTE: we expect the help namespace to always contain wikitext
875 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
877 serialize( 'hello world' ),
879 DummyContentForTesting
::MODEL_ID
,
881 Revision
::FOR_PUBLIC
,
882 serialize( 'hello world' )
885 serialize( 'hello world' ),
889 Revision
::FOR_PUBLIC
,
890 serialize( 'hello world' )
896 * @dataProvider provideGetContent
897 * @covers Revision::getContent
899 public function testGetContent( $text, $title, $model, $format,
900 $audience, $expectedSerialization
902 $rev = $this->newTestRevision( $text, $title, $model, $format );
903 $content = $rev->getContent( $audience );
906 $expectedSerialization,
907 is_null( $content ) ?
null : $content->serialize( $format )
912 * @covers Revision::getContent
914 public function testGetContent_failure() {
915 $rev = new Revision( [
916 'page' => $this->testPage
->getId(),
917 'content_model' => $this->testPage
->getContentModel(),
918 'text_id' => 123456789, // not in the test DB
921 Wikimedia\
suppressWarnings(); // bad text_id will trigger a warning.
923 $this->assertNull( $rev->getContent(),
924 "getContent() should return null if the revision's text blob could not be loaded." );
926 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
927 $this->assertNull( $rev->getContent(),
928 "getContent() should return null if the revision's text blob could not be loaded." );
930 Wikimedia\restoreWarnings
();
933 public function provideGetSize() {
935 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
936 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
941 * @covers Revision::getSize
942 * @dataProvider provideGetSize
944 public function testGetSize( $text, $model, $expected_size ) {
945 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
946 $this->assertEquals( $expected_size, $rev->getSize() );
949 public function provideGetSha1() {
951 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
953 serialize( "hello world." ),
954 DummyContentForTesting
::MODEL_ID
,
955 Revision
::base36Sha1( serialize( "hello world." ) )
961 * @covers Revision::getSha1
962 * @dataProvider provideGetSha1
964 public function testGetSha1( $text, $model, $expected_hash ) {
965 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
966 $this->assertEquals( $expected_hash, $rev->getSha1() );
970 * Tests whether $rev->getContent() returns a clone when needed.
972 * @covers Revision::getContent
974 public function testGetContentClone() {
975 $content = new RevisionTestModifyableContent( "foo" );
981 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
983 'content' => $content,
984 'length' => $content->getSize(),
985 'comment' => "testing",
986 'minor_edit' => false,
990 /** @var RevisionTestModifyableContent $content */
991 $content = $rev->getContent( Revision
::RAW
);
992 $content->setText( "bar" );
994 /** @var RevisionTestModifyableContent $content2 */
995 $content2 = $rev->getContent( Revision
::RAW
);
996 // content is mutable, expect clone
997 $this->assertNotSame( $content, $content2, "expected a clone" );
998 // clone should contain the original text
999 $this->assertEquals( "foo", $content2->getText() );
1001 $content2->setText( "bla bla" );
1002 // clones should be independent
1003 $this->assertEquals( "bar", $content->getText() );
1007 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
1008 * @covers Revision::getContent
1010 public function testGetContentUncloned() {
1011 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
1012 $content = $rev->getContent( Revision
::RAW
);
1013 $content2 = $rev->getContent( Revision
::RAW
);
1015 // for immutable content like wikitext, this should be the same object
1016 $this->assertSame( $content, $content2 );
1020 * @covers Revision::loadFromId
1022 public function testLoadFromId() {
1023 $rev = $this->testPage
->getRevision();
1024 $this->hideDeprecated( 'Revision::loadFromId' );
1025 $this->assertRevEquals(
1027 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1032 * @covers Revision::loadFromPageId
1034 public function testLoadFromPageId() {
1035 $this->assertRevEquals(
1036 $this->testPage
->getRevision(),
1037 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1042 * @covers Revision::loadFromPageId
1044 public function testLoadFromPageIdWithLatestRevId() {
1045 $this->assertRevEquals(
1046 $this->testPage
->getRevision(),
1047 Revision
::loadFromPageId(
1048 wfGetDB( DB_MASTER
),
1049 $this->testPage
->getId(),
1050 $this->testPage
->getLatest()
1056 * @covers Revision::loadFromPageId
1058 public function testLoadFromPageIdWithNotLatestRevId() {
1059 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1060 $this->assertRevEquals(
1061 $this->testPage
->getRevision()->getPrevious(),
1062 Revision
::loadFromPageId(
1063 wfGetDB( DB_MASTER
),
1064 $this->testPage
->getId(),
1065 $this->testPage
->getRevision()->getPrevious()->getId()
1071 * @covers Revision::loadFromTitle
1073 public function testLoadFromTitle() {
1074 $this->assertRevEquals(
1075 $this->testPage
->getRevision(),
1076 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1081 * @covers Revision::loadFromTitle
1083 public function testLoadFromTitleWithLatestRevId() {
1084 $this->assertRevEquals(
1085 $this->testPage
->getRevision(),
1086 Revision
::loadFromTitle(
1087 wfGetDB( DB_MASTER
),
1088 $this->testPage
->getTitle(),
1089 $this->testPage
->getLatest()
1095 * @covers Revision::loadFromTitle
1097 public function testLoadFromTitleWithNotLatestRevId() {
1098 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1099 $this->assertRevEquals(
1100 $this->testPage
->getRevision()->getPrevious(),
1101 Revision
::loadFromTitle(
1102 wfGetDB( DB_MASTER
),
1103 $this->testPage
->getTitle(),
1104 $this->testPage
->getRevision()->getPrevious()->getId()
1110 * @covers Revision::loadFromTimestamp()
1112 public function testLoadFromTimestamp() {
1113 $this->assertRevEquals(
1114 $this->testPage
->getRevision(),
1115 Revision
::loadFromTimestamp(
1116 wfGetDB( DB_MASTER
),
1117 $this->testPage
->getTitle(),
1118 $this->testPage
->getRevision()->getTimestamp()
1124 * @covers Revision::getParentLengths
1126 public function testGetParentLengths_noRevIds() {
1129 Revision
::getParentLengths(
1130 wfGetDB( DB_MASTER
),
1137 * @covers Revision::getParentLengths
1139 public function testGetParentLengths_oneRevId() {
1140 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1141 $textLength = strlen( $text );
1143 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1144 $rev[1] = $this->testPage
->getLatest();
1147 [ $rev[1] => $textLength ],
1148 Revision
::getParentLengths(
1149 wfGetDB( DB_MASTER
),
1156 * @covers Revision::getParentLengths
1158 public function testGetParentLengths_multipleRevIds() {
1159 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1160 $textOneLength = strlen( $textOne );
1161 $textTwo = '831jr091jr092121j09rj1';
1162 $textTwoLength = strlen( $textTwo );
1164 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1165 $rev[1] = $this->testPage
->getLatest();
1166 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1167 $rev[2] = $this->testPage
->getLatest();
1170 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1171 Revision
::getParentLengths(
1172 wfGetDB( DB_MASTER
),
1173 [ $rev[1], $rev[2] ]
1179 * @covers Revision::getTitle
1181 public function testGetTitle_fromExistingRevision() {
1183 $this->testPage
->getTitle()->equals(
1184 $this->testPage
->getRevision()->getTitle()
1190 * @covers Revision::getTitle
1192 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1193 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1195 $this->testPage
->getTitle()->equals(
1202 * @covers Revision::isMinor
1204 public function testIsMinor_true() {
1205 // Use a sysop to ensure we can mark edits as minor
1206 $sysop = $this->getTestSysop()->getUser();
1208 $this->testPage
->doEditContent(
1209 new WikitextContent( __METHOD__
),
1215 $rev = $this->testPage
->getRevision();
1217 $this->assertSame( true, $rev->isMinor() );
1221 * @covers Revision::isMinor
1223 public function testIsMinor_false() {
1224 $this->testPage
->doEditContent(
1225 new WikitextContent( __METHOD__
),
1229 $rev = $this->testPage
->getRevision();
1231 $this->assertSame( false, $rev->isMinor() );
1235 * @covers Revision::getTimestamp
1237 public function testGetTimestamp() {
1238 $testTimestamp = wfTimestampNow();
1240 $this->testPage
->doEditContent(
1241 new WikitextContent( __METHOD__
),
1244 $rev = $this->testPage
->getRevision();
1246 $this->assertInternalType( 'string', $rev->getTimestamp() );
1247 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1248 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1252 * @covers Revision::getUser
1253 * @covers Revision::getUserText
1255 public function testGetUserAndText() {
1256 $sysop = $this->getTestSysop()->getUser();
1258 $this->testPage
->doEditContent(
1259 new WikitextContent( __METHOD__
),
1265 $rev = $this->testPage
->getRevision();
1267 $this->assertSame( $sysop->getId(), $rev->getUser() );
1268 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1272 * @covers Revision::isDeleted
1274 public function testIsDeleted_nothingDeleted() {
1275 $rev = $this->testPage
->getRevision();
1277 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1278 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1279 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1280 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1284 * @covers Revision::getVisibility
1286 public function testGetVisibility_nothingDeleted() {
1287 $rev = $this->testPage
->getRevision();
1289 $this->assertSame( 0, $rev->getVisibility() );
1293 * @covers Revision::getComment
1295 public function testGetComment_notDeleted() {
1296 $expectedSummary = 'goatlicious summary';
1298 $this->testPage
->doEditContent(
1299 new WikitextContent( __METHOD__
),
1302 $rev = $this->testPage
->getRevision();
1304 $this->assertSame( $expectedSummary, $rev->getComment() );
1308 * @covers Revision::isUnpatrolled
1310 public function testIsUnpatrolled_returnsRecentChangesId() {
1311 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1312 $rev = $this->testPage
->getRevision();
1314 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1315 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1319 * @covers Revision::isUnpatrolled
1321 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1322 // This assumes that sysops are auto patrolled
1323 $sysop = $this->getTestSysop()->getUser();
1324 $this->testPage
->doEditContent(
1325 new WikitextContent( __METHOD__
),
1331 $rev = $this->testPage
->getRevision();
1333 $this->assertSame( 0, $rev->isUnpatrolled() );
1337 * This is a simple blanket test for all simple content getters and is methods to provide some
1338 * coverage before the split of Revision into multiple classes for MCR work.
1339 * @covers Revision::getContent
1340 * @covers Revision::getSerializedData
1341 * @covers Revision::getContentModel
1342 * @covers Revision::getContentFormat
1343 * @covers Revision::getContentHandler
1345 public function testSimpleContentGetters() {
1346 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1347 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1349 $this->testPage
->doEditContent(
1350 new WikitextContent( $expectedText ),
1353 $rev = $this->testPage
->getRevision();
1355 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1356 $this->assertSame( $expectedText, $rev->getSerializedData() );
1357 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1358 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1359 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1363 * @covers Revision::newKnownCurrent
1365 public function testNewKnownCurrent() {
1366 // Setup the services
1367 $this->resetGlobalServices();
1368 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1369 $this->setService( 'MainWANObjectCache', $cache );
1370 $db = wfGetDB( DB_MASTER
);
1372 // Get a fresh revision to use during testing
1373 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1374 $rev = $this->testPage
->getRevision();
1376 // Clear any previous cache for the revision during creation
1377 $key = $cache->makeGlobalKey( 'revision-row-1.29',
1382 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1383 $this->assertFalse( $cache->get( $key ) );
1385 // Get the new revision and make sure it is in the cache and correct
1386 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1387 $this->assertRevEquals( $rev, $newRev );
1389 $cachedRow = $cache->get( $key );
1390 $this->assertNotFalse( $cachedRow );
1391 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1394 public function testNewKnownCurrent_withPageId() {
1395 $db = wfGetDB( DB_MASTER
);
1397 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1398 $rev = $this->testPage
->getRevision();
1400 $pageId = $this->testPage
->getId();
1402 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1403 $this->assertRevEquals( $rev, $newRev );
1406 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1407 $db = wfGetDB( DB_MASTER
);
1409 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1412 public function provideUserCanBitfield() {
1413 yield
[ 0, 0, [], null, true ];
1414 // Bitfields match, user has no permissions
1415 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1416 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1417 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1418 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1419 // Bitfields match, user (admin) does have permissions
1420 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1421 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1422 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1423 // Bitfields match, user (admin) does not have permissions
1424 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1425 // Bitfields match, user (oversight) does have permissions
1426 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1427 // Check permissions using the title
1429 Revision
::DELETED_TEXT
,
1430 Revision
::DELETED_TEXT
,
1432 Title
::newFromText( __METHOD__
),
1436 Revision
::DELETED_TEXT
,
1437 Revision
::DELETED_TEXT
,
1439 Title
::newFromText( __METHOD__
),
1445 * @dataProvider provideUserCanBitfield
1446 * @covers Revision::userCanBitfield
1448 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1449 $this->setMwGlobals(
1450 'wgGroupPermissions',
1453 'deletedtext' => true,
1454 'deletedhistory' => true,
1457 'viewsuppressed' => true,
1458 'suppressrevision' => true,
1462 $user = $this->getTestUser( $userGroups )->getUser();
1466 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1469 // Fallback to $wgUser
1470 $this->setMwGlobals(
1476 Revision
::userCanBitfield( $bitField, $field, null, $title )
1480 public function provideUserCan() {
1481 yield
[ 0, 0, [], true ];
1482 // Bitfields match, user has no permissions
1483 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1484 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1485 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1486 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1487 // Bitfields match, user (admin) does have permissions
1488 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1489 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1490 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1491 // Bitfields match, user (admin) does not have permissions
1492 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1493 // Bitfields match, user (oversight) does have permissions
1494 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1498 * @dataProvider provideUserCan
1499 * @covers Revision::userCan
1501 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1502 $this->setMwGlobals(
1503 'wgGroupPermissions',
1506 'deletedtext' => true,
1507 'deletedhistory' => true,
1510 'viewsuppressed' => true,
1511 'suppressrevision' => true,
1515 $user = $this->getTestUser( $userGroups )->getUser();
1516 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1520 $revision->userCan( $field, $user )