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 $props['user_text'] = 'Tester';
114 if ( !isset( $props['user'] ) ) {
118 if ( !isset( $props['comment'] ) ) {
119 $props['comment'] = 'just a test';
122 if ( !isset( $props['page'] ) ) {
123 $props['page'] = $this->testPage
->getId();
126 if ( !isset( $props['content_model'] ) ) {
127 $props['content_model'] = CONTENT_MODEL_WIKITEXT
;
130 $rev = new Revision( $props );
132 $dbw = wfGetDB( DB_MASTER
);
133 $rev->insertOn( $dbw );
139 * @param string $titleString
140 * @param string $text
141 * @param string|null $model
145 private function createPage( $titleString, $text, $model = null ) {
146 if ( !preg_match( '/:/', $titleString ) &&
147 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
149 $ns = $this->getDefaultWikitextNS();
150 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
153 $title = Title
::newFromText( $titleString );
154 $wikipage = new WikiPage( $title );
156 // Delete the article if it already exists
157 if ( $wikipage->exists() ) {
158 $wikipage->doDeleteArticle( "done" );
161 $content = ContentHandler
::makeContent( $text, $title, $model );
162 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
167 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
168 $this->assertNotNull( $rev, 'missing revision' );
170 $this->assertEquals( $orig->getId(), $rev->getId() );
171 $this->assertEquals( $orig->getPage(), $rev->getPage() );
172 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
173 $this->assertEquals( $orig->getUser(), $rev->getUser() );
174 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
175 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
176 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
180 * @covers Revision::getRecentChange
182 public function testGetRecentChange() {
183 $rev = $this->testPage
->getRevision();
184 $recentChange = $rev->getRecentChange();
186 // Make sure various attributes look right / the correct entry has been retrieved.
187 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
189 $rev->getTitle()->getNamespace(),
190 $recentChange->getAttribute( 'rc_namespace' )
193 $rev->getTitle()->getDBkey(),
194 $recentChange->getAttribute( 'rc_title' )
196 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
197 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
198 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
199 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
200 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
204 * @covers Revision::insertOn
206 public function testInsertOn_success() {
207 $parentId = $this->testPage
->getLatest();
209 // If an ExternalStore is set don't use it.
210 $this->setMwGlobals( 'wgDefaultExternalStore', false );
212 $rev = new Revision( [
213 'page' => $this->testPage
->getId(),
214 'title' => $this->testPage
->getTitle(),
215 'text' => 'Revision Text',
216 'comment' => 'Revision comment',
219 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
221 $this->assertInternalType( 'integer', $revId );
222 $this->assertSame( $revId, $rev->getId() );
224 // getTextId() must be an int!
225 $this->assertInternalType( 'integer', $rev->getTextId() );
227 $mainSlot = $rev->getRevisionRecord()->getSlot( 'main', RevisionRecord
::RAW
);
229 // we currently only support storage in the text table
230 $textId = MediaWikiServices
::getInstance()
232 ->getTextIdFromAddress( $mainSlot->getAddress() );
236 [ 'old_id', 'old_text' ],
238 [ [ strval( $textId ), 'Revision Text' ] ]
253 "rev_id = {$rev->getId()}",
255 strval( $rev->getId() ),
256 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()
403 $store->setContentHandlerUseDB( $this->getContentHandlerUseDB() );
404 $this->setService( 'RevisionStore', $store );
406 $page = $this->createPage(
407 'RevisionStorageTest_testNewFromArchiveRow',
409 CONTENT_MODEL_WIKITEXT
411 $orig = $page->getRevision();
412 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
414 $dbr = wfGetDB( DB_REPLICA
);
415 $arQuery = Revision
::getArchiveQueryInfo();
416 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
418 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
419 __METHOD__
, [], $arQuery['joins']
421 $this->assertTrue( is_object( $res ), 'query failed' );
423 $row = $res->fetchObject();
426 // MCR migration note: $row is now required to contain ar_title and ar_namespace.
427 // Alternatively, a Title object can be passed to RevisionStore::newRevisionFromArchiveRow
428 $rev = Revision
::newFromArchiveRow( $row );
430 $this->assertRevEquals( $orig, $rev );
434 * @covers Revision::newFromArchiveRow
436 public function testNewFromArchiveRowOverrides() {
437 $page = $this->createPage(
438 'RevisionStorageTest_testNewFromArchiveRow',
440 CONTENT_MODEL_WIKITEXT
442 $orig = $page->getRevision();
443 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
445 $dbr = wfGetDB( DB_REPLICA
);
446 $arQuery = Revision
::getArchiveQueryInfo();
448 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
449 __METHOD__
, [], $arQuery['joins']
451 $this->assertTrue( is_object( $res ), 'query failed' );
453 $row = $res->fetchObject();
456 $rev = Revision
::newFromArchiveRow( $row, [ 'comment_text' => 'SOMEOVERRIDE' ] );
458 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
459 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
463 * @covers Revision::newFromId
465 public function testNewFromId() {
466 $orig = $this->testPage
->getRevision();
467 $rev = Revision
::newFromId( $orig->getId() );
468 $this->assertRevEquals( $orig, $rev );
472 * @covers Revision::newFromPageId
474 public function testNewFromPageId() {
475 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
476 $this->assertRevEquals(
477 $this->testPage
->getRevision(),
483 * @covers Revision::newFromPageId
485 public function testNewFromPageIdWithLatestId() {
486 $rev = Revision
::newFromPageId(
487 $this->testPage
->getId(),
488 $this->testPage
->getLatest()
490 $this->assertRevEquals(
491 $this->testPage
->getRevision(),
497 * @covers Revision::newFromPageId
499 public function testNewFromPageIdWithNotLatestId() {
500 $content = new WikitextContent( __METHOD__
);
501 $this->testPage
->doEditContent( $content, __METHOD__
);
502 $rev = Revision
::newFromPageId(
503 $this->testPage
->getId(),
504 $this->testPage
->getRevision()->getPrevious()->getId()
506 $this->assertRevEquals(
507 $this->testPage
->getRevision()->getPrevious(),
513 * @covers Revision::fetchRevision
515 public function testFetchRevision() {
516 // Hidden process cache assertion below
517 $this->testPage
->getRevision()->getId();
519 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
520 $id = $this->testPage
->getRevision()->getId();
522 $this->hideDeprecated( 'Revision::fetchRevision' );
523 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
525 # note: order is unspecified
527 while ( ( $row = $res->fetchObject() ) ) {
528 $rows[$row->rev_id
] = $row;
531 $this->assertEmpty( $rows, 'expected empty set' );
535 * @covers Revision::getPage
537 public function testGetPage() {
538 $page = $this->testPage
;
540 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
541 $rev = Revision
::newFromId( $orig->getId() );
543 $this->assertEquals( $page->getId(), $rev->getPage() );
547 * @covers Revision::isCurrent
549 public function testIsCurrent() {
550 $rev1 = $this->testPage
->getRevision();
552 # @todo find out if this should be true
553 # $this->assertTrue( $rev1->isCurrent() );
555 $rev1x = Revision
::newFromId( $rev1->getId() );
556 $this->assertTrue( $rev1x->isCurrent() );
558 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
559 $rev2 = $this->testPage
->getRevision();
561 # @todo find out if this should be true
562 # $this->assertTrue( $rev2->isCurrent() );
564 $rev1x = Revision
::newFromId( $rev1->getId() );
565 $this->assertFalse( $rev1x->isCurrent() );
567 $rev2x = Revision
::newFromId( $rev2->getId() );
568 $this->assertTrue( $rev2x->isCurrent() );
572 * @covers Revision::getPrevious
574 public function testGetPrevious() {
575 $oldestRevision = $this->testPage
->getOldestRevision();
576 $latestRevision = $this->testPage
->getLatest();
578 $this->assertNull( $oldestRevision->getPrevious() );
580 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
581 $newRevision = $this->testPage
->getRevision();
583 $this->assertNotNull( $newRevision->getPrevious() );
584 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
588 * @covers Revision::getNext
590 public function testGetNext() {
591 $rev1 = $this->testPage
->getRevision();
593 $this->assertNull( $rev1->getNext() );
595 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
596 $rev2 = $this->testPage
->getRevision();
598 $this->assertNotNull( $rev1->getNext() );
599 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
603 * @covers Revision::newNullRevision
605 public function testNewNullRevision() {
606 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
607 $orig = $this->testPage
->getRevision();
609 $dbw = wfGetDB( DB_MASTER
);
610 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
612 $this->assertNotEquals( $orig->getId(), $rev->getId(),
613 'new null revision should have a different id from the original revision' );
614 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
615 'new null revision should have the same text id as the original revision' );
616 $this->assertEquals( $orig->getSha1(), $rev->getSha1(),
617 'new null revision should have the same SHA1 as the original revision' );
618 $this->assertTrue( $orig->getRevisionRecord()->hasSameContent( $rev->getRevisionRecord() ),
619 'new null revision should have the same content as the original revision' );
620 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
624 * @covers Revision::newNullRevision
626 public function testNewNullRevision_badPage() {
627 $dbw = wfGetDB( DB_MASTER
);
628 $rev = Revision
::newNullRevision( $dbw, -1, 'a null revision', false );
630 $this->assertNull( $rev );
634 * @covers Revision::insertOn
636 public function testInsertOn() {
637 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
639 $orig = $this->makeRevisionWithProps( [
643 // Make sure the revision was copied to ip_changes
644 $dbr = wfGetDB( DB_REPLICA
);
645 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
646 $row = $res->fetchObject();
648 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
650 $orig->getTimestamp(),
651 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
655 public static function provideUserWasLastToEdit() {
656 yield
'actually the last edit' => [ 3, true ];
657 yield
'not the current edit, but still by this user' => [ 2, true ];
658 yield
'edit by another user' => [ 1, false ];
659 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
663 * @covers Revision::userWasLastToEdit
664 * @dataProvider provideUserWasLastToEdit
666 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
667 $userA = User
::newFromName( "RevisionStorageTest_userA" );
668 $userB = User
::newFromName( "RevisionStorageTest_userB" );
670 if ( $userA->getId() === 0 ) {
671 $userA = User
::createNew( $userA->getName() );
674 if ( $userB->getId() === 0 ) {
675 $userB = User
::createNew( $userB->getName() );
678 $ns = $this->getDefaultWikitextNS();
680 $dbw = wfGetDB( DB_MASTER
);
683 // create revisions -----------------------------
684 $page = WikiPage
::factory( Title
::newFromText(
685 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
686 $page->insertOn( $dbw );
688 $revisions[0] = new Revision( [
689 'page' => $page->getId(),
690 // we need the title to determine the page's default content model
691 'title' => $page->getTitle(),
692 'timestamp' => '20120101000000',
693 'user' => $userA->getId(),
695 'content_model' => CONTENT_MODEL_WIKITEXT
,
696 'comment' => 'edit zero'
698 $revisions[0]->insertOn( $dbw );
700 $revisions[1] = new Revision( [
701 'page' => $page->getId(),
702 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
703 'title' => $page->getTitle(),
704 'timestamp' => '20120101000100',
705 'user' => $userA->getId(),
707 'content_model' => CONTENT_MODEL_WIKITEXT
,
708 'comment' => 'edit one'
710 $revisions[1]->insertOn( $dbw );
712 $revisions[2] = new Revision( [
713 'page' => $page->getId(),
714 'title' => $page->getTitle(),
715 'timestamp' => '20120101000200',
716 'user' => $userB->getId(),
718 'content_model' => CONTENT_MODEL_WIKITEXT
,
719 'comment' => 'edit two'
721 $revisions[2]->insertOn( $dbw );
723 $revisions[3] = new Revision( [
724 'page' => $page->getId(),
725 'title' => $page->getTitle(),
726 'timestamp' => '20120101000300',
727 'user' => $userA->getId(),
729 'content_model' => CONTENT_MODEL_WIKITEXT
,
730 'comment' => 'edit three'
732 $revisions[3]->insertOn( $dbw );
734 $revisions[4] = new Revision( [
735 'page' => $page->getId(),
736 'title' => $page->getTitle(),
737 'timestamp' => '20120101000200',
738 'user' => $userA->getId(),
740 'content_model' => CONTENT_MODEL_WIKITEXT
,
741 'comment' => 'edit four'
743 $revisions[4]->insertOn( $dbw );
745 // test it ---------------------------------
746 $since = $revisions[$sinceIdx]->getTimestamp();
748 $allRows = iterator_to_array( $dbw->select(
750 [ 'rev_id', 'rev_timestamp', 'rev_user' ],
752 'rev_page' => $page->getId(),
753 //'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $since ) )
756 [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ]
759 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
761 $this->assertEquals( $expectedLast, $wasLast );
765 * @param string $text
766 * @param string $title
767 * @param string $model
768 * @param string $format
772 private function newTestRevision( $text, $title = "Test",
773 $model = CONTENT_MODEL_WIKITEXT
, $format = null
775 if ( is_string( $title ) ) {
776 $title = Title
::newFromText( $title );
779 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
787 'content' => $content,
788 'length' => $content->getSize(),
789 'comment' => "testing",
790 'minor_edit' => false,
792 'content_format' => $format,
799 public function provideGetContentModel() {
800 // NOTE: we expect the help namespace to always contain wikitext
802 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
803 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
804 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
809 * @dataProvider provideGetContentModel
810 * @covers Revision::getContentModel
812 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
813 $rev = $this->newTestRevision( $text, $title, $model, $format );
815 $this->assertEquals( $expectedModel, $rev->getContentModel() );
818 public function provideGetContentFormat() {
819 // NOTE: we expect the help namespace to always contain wikitext
821 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
822 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
823 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
824 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
829 * @dataProvider provideGetContentFormat
830 * @covers Revision::getContentFormat
832 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
833 $rev = $this->newTestRevision( $text, $title, $model, $format );
835 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
838 public function provideGetContentHandler() {
839 // NOTE: we expect the help namespace to always contain wikitext
841 [ 'hello world', 'Help:Hello', null, null, WikitextContentHandler
::class ],
842 [ 'hello world', 'User:hello/there.css', null, null, CssContentHandler
::class ],
843 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentHandlerForTesting
::class ],
848 * @dataProvider provideGetContentHandler
849 * @covers Revision::getContentHandler
851 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
852 $rev = $this->newTestRevision( $text, $title, $model, $format );
854 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
857 public function provideGetContent() {
858 // NOTE: we expect the help namespace to always contain wikitext
860 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
862 serialize( 'hello world' ),
864 DummyContentForTesting
::MODEL_ID
,
866 Revision
::FOR_PUBLIC
,
867 serialize( 'hello world' )
870 serialize( 'hello world' ),
874 Revision
::FOR_PUBLIC
,
875 serialize( 'hello world' )
881 * @dataProvider provideGetContent
882 * @covers Revision::getContent
884 public function testGetContent( $text, $title, $model, $format,
885 $audience, $expectedSerialization
887 $rev = $this->newTestRevision( $text, $title, $model, $format );
888 $content = $rev->getContent( $audience );
891 $expectedSerialization,
892 is_null( $content ) ?
null : $content->serialize( $format )
897 * @covers Revision::getContent
899 public function testGetContent_failure() {
900 $rev = new Revision( [
901 'page' => $this->testPage
->getId(),
902 'content_model' => $this->testPage
->getContentModel(),
903 'text_id' => 123456789, // not in the test DB
906 MediaWiki\
suppressWarnings(); // bad text_id will trigger a warning.
908 $this->assertNull( $rev->getContent(),
909 "getContent() should return null if the revision's text blob could not be loaded." );
911 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
912 $this->assertNull( $rev->getContent(),
913 "getContent() should return null if the revision's text blob could not be loaded." );
915 MediaWiki\
suppressWarnings( 'end' );
918 public function provideGetSize() {
920 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
921 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
926 * @covers Revision::getSize
927 * @dataProvider provideGetSize
929 public function testGetSize( $text, $model, $expected_size ) {
930 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
931 $this->assertEquals( $expected_size, $rev->getSize() );
934 public function provideGetSha1() {
936 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
938 serialize( "hello world." ),
939 DummyContentForTesting
::MODEL_ID
,
940 Revision
::base36Sha1( serialize( "hello world." ) )
946 * @covers Revision::getSha1
947 * @dataProvider provideGetSha1
949 public function testGetSha1( $text, $model, $expected_hash ) {
950 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
951 $this->assertEquals( $expected_hash, $rev->getSha1() );
955 * Tests whether $rev->getContent() returns a clone when needed.
957 * @covers Revision::getContent
959 public function testGetContentClone() {
960 $content = new RevisionTestModifyableContent( "foo" );
966 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
968 'content' => $content,
969 'length' => $content->getSize(),
970 'comment' => "testing",
971 'minor_edit' => false,
975 /** @var RevisionTestModifyableContent $content */
976 $content = $rev->getContent( Revision
::RAW
);
977 $content->setText( "bar" );
979 /** @var RevisionTestModifyableContent $content2 */
980 $content2 = $rev->getContent( Revision
::RAW
);
981 // content is mutable, expect clone
982 $this->assertNotSame( $content, $content2, "expected a clone" );
983 // clone should contain the original text
984 $this->assertEquals( "foo", $content2->getText() );
986 $content2->setText( "bla bla" );
987 // clones should be independent
988 $this->assertEquals( "bar", $content->getText() );
992 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
993 * @covers Revision::getContent
995 public function testGetContentUncloned() {
996 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
997 $content = $rev->getContent( Revision
::RAW
);
998 $content2 = $rev->getContent( Revision
::RAW
);
1000 // for immutable content like wikitext, this should be the same object
1001 $this->assertSame( $content, $content2 );
1005 * @covers Revision::loadFromId
1007 public function testLoadFromId() {
1008 $rev = $this->testPage
->getRevision();
1009 $this->hideDeprecated( 'Revision::loadFromId' );
1010 $this->assertRevEquals(
1012 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
1017 * @covers Revision::loadFromPageId
1019 public function testLoadFromPageId() {
1020 $this->assertRevEquals(
1021 $this->testPage
->getRevision(),
1022 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
1027 * @covers Revision::loadFromPageId
1029 public function testLoadFromPageIdWithLatestRevId() {
1030 $this->assertRevEquals(
1031 $this->testPage
->getRevision(),
1032 Revision
::loadFromPageId(
1033 wfGetDB( DB_MASTER
),
1034 $this->testPage
->getId(),
1035 $this->testPage
->getLatest()
1041 * @covers Revision::loadFromPageId
1043 public function testLoadFromPageIdWithNotLatestRevId() {
1044 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1045 $this->assertRevEquals(
1046 $this->testPage
->getRevision()->getPrevious(),
1047 Revision
::loadFromPageId(
1048 wfGetDB( DB_MASTER
),
1049 $this->testPage
->getId(),
1050 $this->testPage
->getRevision()->getPrevious()->getId()
1056 * @covers Revision::loadFromTitle
1058 public function testLoadFromTitle() {
1059 $this->assertRevEquals(
1060 $this->testPage
->getRevision(),
1061 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
1066 * @covers Revision::loadFromTitle
1068 public function testLoadFromTitleWithLatestRevId() {
1069 $this->assertRevEquals(
1070 $this->testPage
->getRevision(),
1071 Revision
::loadFromTitle(
1072 wfGetDB( DB_MASTER
),
1073 $this->testPage
->getTitle(),
1074 $this->testPage
->getLatest()
1080 * @covers Revision::loadFromTitle
1082 public function testLoadFromTitleWithNotLatestRevId() {
1083 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1084 $this->assertRevEquals(
1085 $this->testPage
->getRevision()->getPrevious(),
1086 Revision
::loadFromTitle(
1087 wfGetDB( DB_MASTER
),
1088 $this->testPage
->getTitle(),
1089 $this->testPage
->getRevision()->getPrevious()->getId()
1095 * @covers Revision::loadFromTimestamp()
1097 public function testLoadFromTimestamp() {
1098 $this->assertRevEquals(
1099 $this->testPage
->getRevision(),
1100 Revision
::loadFromTimestamp(
1101 wfGetDB( DB_MASTER
),
1102 $this->testPage
->getTitle(),
1103 $this->testPage
->getRevision()->getTimestamp()
1109 * @covers Revision::getParentLengths
1111 public function testGetParentLengths_noRevIds() {
1114 Revision
::getParentLengths(
1115 wfGetDB( DB_MASTER
),
1122 * @covers Revision::getParentLengths
1124 public function testGetParentLengths_oneRevId() {
1125 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1126 $textLength = strlen( $text );
1128 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1129 $rev[1] = $this->testPage
->getLatest();
1132 [ $rev[1] => $textLength ],
1133 Revision
::getParentLengths(
1134 wfGetDB( DB_MASTER
),
1141 * @covers Revision::getParentLengths
1143 public function testGetParentLengths_multipleRevIds() {
1144 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1145 $textOneLength = strlen( $textOne );
1146 $textTwo = '831jr091jr092121j09rj1';
1147 $textTwoLength = strlen( $textTwo );
1149 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1150 $rev[1] = $this->testPage
->getLatest();
1151 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1152 $rev[2] = $this->testPage
->getLatest();
1155 [ $rev[1] => $textOneLength, $rev[2] => $textTwoLength ],
1156 Revision
::getParentLengths(
1157 wfGetDB( DB_MASTER
),
1158 [ $rev[1], $rev[2] ]
1164 * @covers Revision::getTitle
1166 public function testGetTitle_fromExistingRevision() {
1168 $this->testPage
->getTitle()->equals(
1169 $this->testPage
->getRevision()->getTitle()
1175 * @covers Revision::getTitle
1177 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1178 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1180 $this->testPage
->getTitle()->equals(
1187 * @covers Revision::isMinor
1189 public function testIsMinor_true() {
1190 // Use a sysop to ensure we can mark edits as minor
1191 $sysop = $this->getTestSysop()->getUser();
1193 $this->testPage
->doEditContent(
1194 new WikitextContent( __METHOD__
),
1200 $rev = $this->testPage
->getRevision();
1202 $this->assertSame( true, $rev->isMinor() );
1206 * @covers Revision::isMinor
1208 public function testIsMinor_false() {
1209 $this->testPage
->doEditContent(
1210 new WikitextContent( __METHOD__
),
1214 $rev = $this->testPage
->getRevision();
1216 $this->assertSame( false, $rev->isMinor() );
1220 * @covers Revision::getTimestamp
1222 public function testGetTimestamp() {
1223 $testTimestamp = wfTimestampNow();
1225 $this->testPage
->doEditContent(
1226 new WikitextContent( __METHOD__
),
1229 $rev = $this->testPage
->getRevision();
1231 $this->assertInternalType( 'string', $rev->getTimestamp() );
1232 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1233 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1237 * @covers Revision::getUser
1238 * @covers Revision::getUserText
1240 public function testGetUserAndText() {
1241 $sysop = $this->getTestSysop()->getUser();
1243 $this->testPage
->doEditContent(
1244 new WikitextContent( __METHOD__
),
1250 $rev = $this->testPage
->getRevision();
1252 $this->assertSame( $sysop->getId(), $rev->getUser() );
1253 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1257 * @covers Revision::isDeleted
1259 public function testIsDeleted_nothingDeleted() {
1260 $rev = $this->testPage
->getRevision();
1262 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1263 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1264 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1265 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1269 * @covers Revision::getVisibility
1271 public function testGetVisibility_nothingDeleted() {
1272 $rev = $this->testPage
->getRevision();
1274 $this->assertSame( 0, $rev->getVisibility() );
1278 * @covers Revision::getComment
1280 public function testGetComment_notDeleted() {
1281 $expectedSummary = 'goatlicious summary';
1283 $this->testPage
->doEditContent(
1284 new WikitextContent( __METHOD__
),
1287 $rev = $this->testPage
->getRevision();
1289 $this->assertSame( $expectedSummary, $rev->getComment() );
1293 * @covers Revision::isUnpatrolled
1295 public function testIsUnpatrolled_returnsRecentChangesId() {
1296 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1297 $rev = $this->testPage
->getRevision();
1299 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1300 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1304 * @covers Revision::isUnpatrolled
1306 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1307 // This assumes that sysops are auto patrolled
1308 $sysop = $this->getTestSysop()->getUser();
1309 $this->testPage
->doEditContent(
1310 new WikitextContent( __METHOD__
),
1316 $rev = $this->testPage
->getRevision();
1318 $this->assertSame( 0, $rev->isUnpatrolled() );
1322 * This is a simple blanket test for all simple content getters and is methods to provide some
1323 * coverage before the split of Revision into multiple classes for MCR work.
1324 * @covers Revision::getContent
1325 * @covers Revision::getSerializedData
1326 * @covers Revision::getContentModel
1327 * @covers Revision::getContentFormat
1328 * @covers Revision::getContentHandler
1330 public function testSimpleContentGetters() {
1331 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1332 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1334 $this->testPage
->doEditContent(
1335 new WikitextContent( $expectedText ),
1338 $rev = $this->testPage
->getRevision();
1340 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1341 $this->assertSame( $expectedText, $rev->getSerializedData() );
1342 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1343 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1344 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1348 * @covers Revision::newKnownCurrent
1350 public function testNewKnownCurrent() {
1351 // Setup the services
1352 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1353 $this->setService( 'MainWANObjectCache', $cache );
1354 $db = wfGetDB( DB_MASTER
);
1356 // Get a fresh revision to use during testing
1357 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1358 $rev = $this->testPage
->getRevision();
1360 // Clear any previous cache for the revision during creation
1361 $key = $cache->makeGlobalKey( 'revision-row-1.29',
1366 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1367 $this->assertFalse( $cache->get( $key ) );
1369 // Get the new revision and make sure it is in the cache and correct
1370 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1371 $this->assertRevEquals( $rev, $newRev );
1373 $cachedRow = $cache->get( $key );
1374 $this->assertNotFalse( $cachedRow );
1375 $this->assertEquals( $rev->getId(), $cachedRow->rev_id
);
1378 public function testNewKnownCurrent_withPageId() {
1379 $db = wfGetDB( DB_MASTER
);
1381 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1382 $rev = $this->testPage
->getRevision();
1384 $pageId = $this->testPage
->getId();
1386 $newRev = Revision
::newKnownCurrent( $db, $pageId, $rev->getId() );
1387 $this->assertRevEquals( $rev, $newRev );
1390 public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
1391 $db = wfGetDB( DB_MASTER
);
1393 $this->assertFalse( Revision
::newKnownCurrent( $db, 0 ) );
1396 public function provideUserCanBitfield() {
1397 yield
[ 0, 0, [], null, true ];
1398 // Bitfields match, user has no permissions
1399 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1400 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1401 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1402 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1403 // Bitfields match, user (admin) does have permissions
1404 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1405 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1406 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1407 // Bitfields match, user (admin) does not have permissions
1408 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1409 // Bitfields match, user (oversight) does have permissions
1410 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1411 // Check permissions using the title
1413 Revision
::DELETED_TEXT
,
1414 Revision
::DELETED_TEXT
,
1416 Title
::newFromText( __METHOD__
),
1420 Revision
::DELETED_TEXT
,
1421 Revision
::DELETED_TEXT
,
1423 Title
::newFromText( __METHOD__
),
1429 * @dataProvider provideUserCanBitfield
1430 * @covers Revision::userCanBitfield
1432 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1433 $this->setMwGlobals(
1434 'wgGroupPermissions',
1437 'deletedtext' => true,
1438 'deletedhistory' => true,
1441 'viewsuppressed' => true,
1442 'suppressrevision' => true,
1446 $user = $this->getTestUser( $userGroups )->getUser();
1450 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1453 // Fallback to $wgUser
1454 $this->setMwGlobals(
1460 Revision
::userCanBitfield( $bitField, $field, null, $title )
1464 public function provideUserCan() {
1465 yield
[ 0, 0, [], true ];
1466 // Bitfields match, user has no permissions
1467 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1468 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1469 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1470 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1471 // Bitfields match, user (admin) does have permissions
1472 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1473 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1474 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1475 // Bitfields match, user (admin) does not have permissions
1476 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1477 // Bitfields match, user (oversight) does have permissions
1478 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1482 * @dataProvider provideUserCan
1483 * @covers Revision::userCan
1485 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1486 $this->setMwGlobals(
1487 'wgGroupPermissions',
1490 'deletedtext' => true,
1491 'deletedhistory' => true,
1494 'viewsuppressed' => true,
1495 'suppressrevision' => true,
1499 $user = $this->getTestUser( $userGroups )->getUser();
1500 $revision = new Revision( [ 'deleted' => $bitField ], 0, $this->testPage
->getTitle() );
1504 $revision->userCan( $field, $user )