4 * RevisionDbTestBase contains test cases for the Revision class that have Database interactions.
9 abstract class RevisionDbTestBase
extends MediaWikiTestCase
{
12 * @var WikiPage $testPage
16 public function __construct( $name = null, array $data = [], $dataName = '' ) {
17 parent
::__construct( $name, $data, $dataName );
19 $this->tablesUsed
= array_merge( $this->tablesUsed
,
42 protected function setUp() {
47 $this->mergeMwGlobalArrayValue(
51 12313 => 'Dummy_talk',
55 $this->mergeMwGlobalArrayValue(
56 'wgNamespaceContentModels',
58 12312 => DummyContentForTesting
::MODEL_ID
,
62 $this->mergeMwGlobalArrayValue(
65 DummyContentForTesting
::MODEL_ID
=> 'DummyContentHandlerForTesting',
66 RevisionTestModifyableContent
::MODEL_ID
=> 'RevisionTestModifyableContentHandler',
70 $this->setMwGlobals( 'wgContentHandlerUseDB', $this->getContentHandlerUseDB() );
72 MWNamespace
::clearCaches();
73 // Reset namespace cache
74 $wgContLang->resetNamespaces();
75 if ( !$this->testPage
) {
77 * We have to create a new page for each subclass as the page creation may result
78 * in different DB fields being filled based on configuration.
80 $this->testPage
= $this->createPage( __CLASS__
, __CLASS__
);
84 protected function tearDown() {
89 MWNamespace
::clearCaches();
90 // Reset namespace cache
91 $wgContLang->resetNamespaces();
94 abstract protected function getContentHandlerUseDB();
96 private function makeRevisionWithProps( $props = null ) {
97 if ( $props === null ) {
101 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
102 $props['text'] = 'Lorem Ipsum';
105 if ( !isset( $props['comment'] ) ) {
106 $props['comment'] = 'just a test';
109 if ( !isset( $props['page'] ) ) {
110 $props['page'] = $this->testPage
->getId();
113 $rev = new Revision( $props );
115 $dbw = wfGetDB( DB_MASTER
);
116 $rev->insertOn( $dbw );
122 * @param string $titleString
123 * @param string $text
124 * @param string|null $model
128 private function createPage( $titleString, $text, $model = null ) {
129 if ( !preg_match( '/:/', $titleString ) &&
130 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
132 $ns = $this->getDefaultWikitextNS();
133 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
136 $title = Title
::newFromText( $titleString );
137 $wikipage = new WikiPage( $title );
139 // Delete the article if it already exists
140 if ( $wikipage->exists() ) {
141 $wikipage->doDeleteArticle( "done" );
144 $content = ContentHandler
::makeContent( $text, $title, $model );
145 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
150 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
151 $this->assertNotNull( $rev, 'missing revision' );
153 $this->assertEquals( $orig->getId(), $rev->getId() );
154 $this->assertEquals( $orig->getPage(), $rev->getPage() );
155 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
156 $this->assertEquals( $orig->getUser(), $rev->getUser() );
157 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
158 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
159 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
163 * @covers Revision::getRecentChange
165 public function testGetRecentChange() {
166 $rev = $this->testPage
->getRevision();
167 $recentChange = $rev->getRecentChange();
169 // Make sure various attributes look right / the correct entry has been retrieved.
170 $this->assertEquals( $rev->getTimestamp(), $recentChange->getAttribute( 'rc_timestamp' ) );
172 $rev->getTitle()->getNamespace(),
173 $recentChange->getAttribute( 'rc_namespace' )
176 $rev->getTitle()->getDBkey(),
177 $recentChange->getAttribute( 'rc_title' )
179 $this->assertEquals( $rev->getUser(), $recentChange->getAttribute( 'rc_user' ) );
180 $this->assertEquals( $rev->getUserText(), $recentChange->getAttribute( 'rc_user_text' ) );
181 $this->assertEquals( $rev->getComment(), $recentChange->getAttribute( 'rc_comment' ) );
182 $this->assertEquals( $rev->getPage(), $recentChange->getAttribute( 'rc_cur_id' ) );
183 $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) );
187 * @covers Revision::insertOn
189 public function testInsertOn_success() {
190 $parentId = $this->testPage
->getLatest();
192 // If an ExternalStore is set don't use it.
193 $this->setMwGlobals( 'wgDefaultExternalStore', false );
195 $rev = new Revision( [
196 'page' => $this->testPage
->getId(),
197 'title' => $this->testPage
->getTitle(),
198 'text' => 'Revision Text',
199 'comment' => 'Revision comment',
202 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
204 $this->assertInternalType( 'integer', $revId );
205 $this->assertInternalType( 'integer', $rev->getTextId() );
206 $this->assertSame( $revId, $rev->getId() );
210 [ 'old_id', 'old_text' ],
211 "old_id = {$rev->getTextId()}",
212 [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
227 "rev_id = {$rev->getId()}",
229 strval( $rev->getId() ),
230 strval( $this->testPage
->getId() ),
231 strval( $rev->getTextId() ),
237 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
243 * @covers Revision::insertOn
245 public function testInsertOn_exceptionOnNoPage() {
246 // If an ExternalStore is set don't use it.
247 $this->setMwGlobals( 'wgDefaultExternalStore', false );
248 $this->setExpectedException(
250 "Cannot insert revision: page ID must be nonzero"
253 $rev = new Revision( [] );
255 $rev->insertOn( wfGetDB( DB_MASTER
) );
259 * @covers Revision::newFromTitle
261 public function testNewFromTitle_withoutId() {
262 $latestRevId = $this->testPage
->getLatest();
264 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
266 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
267 $this->assertEquals( $latestRevId, $rev->getId() );
271 * @covers Revision::newFromTitle
273 public function testNewFromTitle_withId() {
274 $latestRevId = $this->testPage
->getLatest();
276 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
278 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
279 $this->assertEquals( $latestRevId, $rev->getId() );
283 * @covers Revision::newFromTitle
285 public function testNewFromTitle_withBadId() {
286 $latestRevId = $this->testPage
->getLatest();
288 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
290 $this->assertNull( $rev );
294 * @covers Revision::newFromRow
296 public function testNewFromRow() {
297 $orig = $this->makeRevisionWithProps();
299 $dbr = wfGetDB( DB_REPLICA
);
300 $revQuery = Revision
::getQueryInfo();
301 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
302 __METHOD__
, [], $revQuery['joins'] );
303 $this->assertTrue( is_object( $res ), 'query failed' );
305 $row = $res->fetchObject();
308 $rev = Revision
::newFromRow( $row );
310 $this->assertRevEquals( $orig, $rev );
313 public function provideNewFromArchiveRow() {
321 return $f +
[ 'ar_namespace', 'ar_title' ];
326 unset( $f['ar_text_id'] );
333 * @dataProvider provideNewFromArchiveRow
334 * @covers Revision::newFromArchiveRow
336 public function testNewFromArchiveRow( $selectModifier ) {
337 $page = $this->createPage(
338 'RevisionStorageTest_testNewFromArchiveRow',
340 CONTENT_MODEL_WIKITEXT
342 $orig = $page->getRevision();
343 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
345 $dbr = wfGetDB( DB_REPLICA
);
346 $arQuery = Revision
::getArchiveQueryInfo();
347 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
349 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
350 __METHOD__
, [], $arQuery['joins']
352 $this->assertTrue( is_object( $res ), 'query failed' );
354 $row = $res->fetchObject();
357 $rev = Revision
::newFromArchiveRow( $row );
359 $this->assertRevEquals( $orig, $rev );
363 * @covers Revision::newFromArchiveRow
365 public function testNewFromArchiveRowOverrides() {
366 $page = $this->createPage(
367 'RevisionStorageTest_testNewFromArchiveRow',
369 CONTENT_MODEL_WIKITEXT
371 $orig = $page->getRevision();
372 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
374 $dbr = wfGetDB( DB_REPLICA
);
375 $arQuery = Revision
::getArchiveQueryInfo();
377 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
378 __METHOD__
, [], $arQuery['joins']
380 $this->assertTrue( is_object( $res ), 'query failed' );
382 $row = $res->fetchObject();
385 $rev = Revision
::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
387 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
388 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
392 * @covers Revision::newFromId
394 public function testNewFromId() {
395 $orig = $this->testPage
->getRevision();
396 $rev = Revision
::newFromId( $orig->getId() );
397 $this->assertRevEquals( $orig, $rev );
401 * @covers Revision::newFromPageId
403 public function testNewFromPageId() {
404 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
405 $this->assertRevEquals(
406 $this->testPage
->getRevision(),
412 * @covers Revision::newFromPageId
414 public function testNewFromPageIdWithLatestId() {
415 $rev = Revision
::newFromPageId(
416 $this->testPage
->getId(),
417 $this->testPage
->getLatest()
419 $this->assertRevEquals(
420 $this->testPage
->getRevision(),
426 * @covers Revision::newFromPageId
428 public function testNewFromPageIdWithNotLatestId() {
429 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
430 $rev = Revision
::newFromPageId(
431 $this->testPage
->getId(),
432 $this->testPage
->getRevision()->getPrevious()->getId()
434 $this->assertRevEquals(
435 $this->testPage
->getRevision()->getPrevious(),
441 * @covers Revision::fetchRevision
443 public function testFetchRevision() {
444 // Hidden process cache assertion below
445 $this->testPage
->getRevision()->getId();
447 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
448 $id = $this->testPage
->getRevision()->getId();
450 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
452 # note: order is unspecified
454 while ( ( $row = $res->fetchObject() ) ) {
455 $rows[$row->rev_id
] = $row;
458 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
459 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
463 * @covers Revision::getPage
465 public function testGetPage() {
466 $page = $this->testPage
;
468 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
469 $rev = Revision
::newFromId( $orig->getId() );
471 $this->assertEquals( $page->getId(), $rev->getPage() );
475 * @covers Revision::isCurrent
477 public function testIsCurrent() {
478 $rev1 = $this->testPage
->getRevision();
480 # @todo find out if this should be true
481 # $this->assertTrue( $rev1->isCurrent() );
483 $rev1x = Revision
::newFromId( $rev1->getId() );
484 $this->assertTrue( $rev1x->isCurrent() );
486 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
487 $rev2 = $this->testPage
->getRevision();
489 # @todo find out if this should be true
490 # $this->assertTrue( $rev2->isCurrent() );
492 $rev1x = Revision
::newFromId( $rev1->getId() );
493 $this->assertFalse( $rev1x->isCurrent() );
495 $rev2x = Revision
::newFromId( $rev2->getId() );
496 $this->assertTrue( $rev2x->isCurrent() );
500 * @covers Revision::getPrevious
502 public function testGetPrevious() {
503 $oldestRevision = $this->testPage
->getOldestRevision();
504 $latestRevision = $this->testPage
->getLatest();
506 $this->assertNull( $oldestRevision->getPrevious() );
508 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
509 $newRevision = $this->testPage
->getRevision();
511 $this->assertNotNull( $newRevision->getPrevious() );
512 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
516 * @covers Revision::getNext
518 public function testGetNext() {
519 $rev1 = $this->testPage
->getRevision();
521 $this->assertNull( $rev1->getNext() );
523 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
524 $rev2 = $this->testPage
->getRevision();
526 $this->assertNotNull( $rev1->getNext() );
527 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
531 * @covers Revision::newNullRevision
533 public function testNewNullRevision() {
534 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
535 $orig = $this->testPage
->getRevision();
537 $dbw = wfGetDB( DB_MASTER
);
538 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
540 $this->assertNotEquals( $orig->getId(), $rev->getId(),
541 'new null revision should have a different id from the original revision' );
542 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
543 'new null revision should have the same text id as the original revision' );
544 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
548 * @covers Revision::insertOn
550 public function testInsertOn() {
551 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
553 $orig = $this->makeRevisionWithProps( [
557 // Make sure the revision was copied to ip_changes
558 $dbr = wfGetDB( DB_REPLICA
);
559 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
560 $row = $res->fetchObject();
562 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
563 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
566 public static function provideUserWasLastToEdit() {
567 yield
'actually the last edit' => [ 3, true ];
568 yield
'not the current edit, but still by this user' => [ 2, true ];
569 yield
'edit by another user' => [ 1, false ];
570 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
574 * @dataProvider provideUserWasLastToEdit
576 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
577 $userA = User
::newFromName( "RevisionStorageTest_userA" );
578 $userB = User
::newFromName( "RevisionStorageTest_userB" );
580 if ( $userA->getId() === 0 ) {
581 $userA = User
::createNew( $userA->getName() );
584 if ( $userB->getId() === 0 ) {
585 $userB = User
::createNew( $userB->getName() );
588 $ns = $this->getDefaultWikitextNS();
590 $dbw = wfGetDB( DB_MASTER
);
593 // create revisions -----------------------------
594 $page = WikiPage
::factory( Title
::newFromText(
595 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
596 $page->insertOn( $dbw );
598 $revisions[0] = new Revision( [
599 'page' => $page->getId(),
600 // we need the title to determine the page's default content model
601 'title' => $page->getTitle(),
602 'timestamp' => '20120101000000',
603 'user' => $userA->getId(),
605 'content_model' => CONTENT_MODEL_WIKITEXT
,
606 'summary' => 'edit zero'
608 $revisions[0]->insertOn( $dbw );
610 $revisions[1] = new Revision( [
611 'page' => $page->getId(),
612 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
613 'title' => $page->getTitle(),
614 'timestamp' => '20120101000100',
615 'user' => $userA->getId(),
617 'content_model' => CONTENT_MODEL_WIKITEXT
,
618 'summary' => 'edit one'
620 $revisions[1]->insertOn( $dbw );
622 $revisions[2] = new Revision( [
623 'page' => $page->getId(),
624 'title' => $page->getTitle(),
625 'timestamp' => '20120101000200',
626 'user' => $userB->getId(),
628 'content_model' => CONTENT_MODEL_WIKITEXT
,
629 'summary' => 'edit two'
631 $revisions[2]->insertOn( $dbw );
633 $revisions[3] = new Revision( [
634 'page' => $page->getId(),
635 'title' => $page->getTitle(),
636 'timestamp' => '20120101000300',
637 'user' => $userA->getId(),
639 'content_model' => CONTENT_MODEL_WIKITEXT
,
640 'summary' => 'edit three'
642 $revisions[3]->insertOn( $dbw );
644 $revisions[4] = new Revision( [
645 'page' => $page->getId(),
646 'title' => $page->getTitle(),
647 'timestamp' => '20120101000200',
648 'user' => $userA->getId(),
650 'content_model' => CONTENT_MODEL_WIKITEXT
,
651 'summary' => 'edit four'
653 $revisions[4]->insertOn( $dbw );
655 // test it ---------------------------------
656 $since = $revisions[$sinceIdx]->getTimestamp();
658 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
660 $this->assertEquals( $expectedLast, $wasLast );
664 * @param string $text
665 * @param string $title
666 * @param string $model
667 * @param string $format
671 private function newTestRevision( $text, $title = "Test",
672 $model = CONTENT_MODEL_WIKITEXT
, $format = null
674 if ( is_string( $title ) ) {
675 $title = Title
::newFromText( $title );
678 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
686 'content' => $content,
687 'length' => $content->getSize(),
688 'comment' => "testing",
689 'minor_edit' => false,
691 'content_format' => $format,
698 public function provideGetContentModel() {
699 // NOTE: we expect the help namespace to always contain wikitext
701 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
702 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
703 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
708 * @dataProvider provideGetContentModel
709 * @covers Revision::getContentModel
711 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
712 $rev = $this->newTestRevision( $text, $title, $model, $format );
714 $this->assertEquals( $expectedModel, $rev->getContentModel() );
717 public function provideGetContentFormat() {
718 // NOTE: we expect the help namespace to always contain wikitext
720 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
721 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
722 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
723 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
728 * @dataProvider provideGetContentFormat
729 * @covers Revision::getContentFormat
731 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
732 $rev = $this->newTestRevision( $text, $title, $model, $format );
734 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
737 public function provideGetContentHandler() {
738 // NOTE: we expect the help namespace to always contain wikitext
740 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
741 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
742 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
747 * @dataProvider provideGetContentHandler
748 * @covers Revision::getContentHandler
750 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
751 $rev = $this->newTestRevision( $text, $title, $model, $format );
753 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
756 public function provideGetContent() {
757 // NOTE: we expect the help namespace to always contain wikitext
759 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
761 serialize( 'hello world' ),
763 DummyContentForTesting
::MODEL_ID
,
765 Revision
::FOR_PUBLIC
,
766 serialize( 'hello world' )
769 serialize( 'hello world' ),
773 Revision
::FOR_PUBLIC
,
774 serialize( 'hello world' )
780 * @dataProvider provideGetContent
781 * @covers Revision::getContent
783 public function testGetContent( $text, $title, $model, $format,
784 $audience, $expectedSerialization
786 $rev = $this->newTestRevision( $text, $title, $model, $format );
787 $content = $rev->getContent( $audience );
790 $expectedSerialization,
791 is_null( $content ) ?
null : $content->serialize( $format )
796 * @covers Revision::getContent
798 public function testGetContent_failure() {
799 $rev = new Revision( [
800 'page' => $this->testPage
->getId(),
801 'content_model' => $this->testPage
->getContentModel(),
802 'text_id' => 123456789, // not in the test DB
805 $this->assertNull( $rev->getContent(),
806 "getContent() should return null if the revision's text blob could not be loaded." );
808 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
809 $this->assertNull( $rev->getContent(),
810 "getContent() should return null if the revision's text blob could not be loaded." );
813 public function provideGetSize() {
815 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
816 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
821 * @covers Revision::getSize
822 * @dataProvider provideGetSize
824 public function testGetSize( $text, $model, $expected_size ) {
825 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
826 $this->assertEquals( $expected_size, $rev->getSize() );
829 public function provideGetSha1() {
831 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
833 serialize( "hello world." ),
834 DummyContentForTesting
::MODEL_ID
,
835 Revision
::base36Sha1( serialize( "hello world." ) )
841 * @covers Revision::getSha1
842 * @dataProvider provideGetSha1
844 public function testGetSha1( $text, $model, $expected_hash ) {
845 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
846 $this->assertEquals( $expected_hash, $rev->getSha1() );
850 * Tests whether $rev->getContent() returns a clone when needed.
852 * @covers Revision::getContent
854 public function testGetContentClone() {
855 $content = new RevisionTestModifyableContent( "foo" );
861 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
863 'content' => $content,
864 'length' => $content->getSize(),
865 'comment' => "testing",
866 'minor_edit' => false,
870 /** @var RevisionTestModifyableContent $content */
871 $content = $rev->getContent( Revision
::RAW
);
872 $content->setText( "bar" );
874 /** @var RevisionTestModifyableContent $content2 */
875 $content2 = $rev->getContent( Revision
::RAW
);
876 // content is mutable, expect clone
877 $this->assertNotSame( $content, $content2, "expected a clone" );
878 // clone should contain the original text
879 $this->assertEquals( "foo", $content2->getText() );
881 $content2->setText( "bla bla" );
882 // clones should be independent
883 $this->assertEquals( "bar", $content->getText() );
887 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
888 * @covers Revision::getContent
890 public function testGetContentUncloned() {
891 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
892 $content = $rev->getContent( Revision
::RAW
);
893 $content2 = $rev->getContent( Revision
::RAW
);
895 // for immutable content like wikitext, this should be the same object
896 $this->assertSame( $content, $content2 );
900 * @covers Revision::loadFromId
902 public function testLoadFromId() {
903 $rev = $this->testPage
->getRevision();
904 $this->assertRevEquals(
906 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
911 * @covers Revision::loadFromPageId
913 public function testLoadFromPageId() {
914 $this->assertRevEquals(
915 $this->testPage
->getRevision(),
916 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
921 * @covers Revision::loadFromPageId
923 public function testLoadFromPageIdWithLatestRevId() {
924 $this->assertRevEquals(
925 $this->testPage
->getRevision(),
926 Revision
::loadFromPageId(
927 wfGetDB( DB_MASTER
),
928 $this->testPage
->getId(),
929 $this->testPage
->getLatest()
935 * @covers Revision::loadFromPageId
937 public function testLoadFromPageIdWithNotLatestRevId() {
938 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
939 $this->assertRevEquals(
940 $this->testPage
->getRevision()->getPrevious(),
941 Revision
::loadFromPageId(
942 wfGetDB( DB_MASTER
),
943 $this->testPage
->getId(),
944 $this->testPage
->getRevision()->getPrevious()->getId()
950 * @covers Revision::loadFromTitle
952 public function testLoadFromTitle() {
953 $this->assertRevEquals(
954 $this->testPage
->getRevision(),
955 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
960 * @covers Revision::loadFromTitle
962 public function testLoadFromTitleWithLatestRevId() {
963 $this->assertRevEquals(
964 $this->testPage
->getRevision(),
965 Revision
::loadFromTitle(
966 wfGetDB( DB_MASTER
),
967 $this->testPage
->getTitle(),
968 $this->testPage
->getLatest()
974 * @covers Revision::loadFromTitle
976 public function testLoadFromTitleWithNotLatestRevId() {
977 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
978 $this->assertRevEquals(
979 $this->testPage
->getRevision()->getPrevious(),
980 Revision
::loadFromTitle(
981 wfGetDB( DB_MASTER
),
982 $this->testPage
->getTitle(),
983 $this->testPage
->getRevision()->getPrevious()->getId()
989 * @covers Revision::loadFromTimestamp()
991 public function testLoadFromTimestamp() {
992 $this->assertRevEquals(
993 $this->testPage
->getRevision(),
994 Revision
::loadFromTimestamp(
995 wfGetDB( DB_MASTER
),
996 $this->testPage
->getTitle(),
997 $this->testPage
->getRevision()->getTimestamp()
1003 * @covers Revision::getParentLengths
1005 public function testGetParentLengths_noRevIds() {
1008 Revision
::getParentLengths(
1009 wfGetDB( DB_MASTER
),
1016 * @covers Revision::getParentLengths
1018 public function testGetParentLengths_oneRevId() {
1019 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1020 $textLength = strlen( $text );
1022 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1023 $rev[1] = $this->testPage
->getLatest();
1026 [ $rev[1] => strval( $textLength ) ],
1027 Revision
::getParentLengths(
1028 wfGetDB( DB_MASTER
),
1035 * @covers Revision::getParentLengths
1037 public function testGetParentLengths_multipleRevIds() {
1038 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1039 $textOneLength = strlen( $textOne );
1040 $textTwo = '831jr091jr092121j09rj1';
1041 $textTwoLength = strlen( $textTwo );
1043 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1044 $rev[1] = $this->testPage
->getLatest();
1045 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1046 $rev[2] = $this->testPage
->getLatest();
1049 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1050 Revision
::getParentLengths(
1051 wfGetDB( DB_MASTER
),
1052 [ $rev[1], $rev[2] ]
1058 * @covers Revision::getTitle
1060 public function testGetTitle_fromExistingRevision() {
1062 $this->testPage
->getTitle()->equals(
1063 $this->testPage
->getRevision()->getTitle()
1069 * @covers Revision::getTitle
1071 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1072 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1074 $this->testPage
->getTitle()->equals(
1081 * @covers Revision::getTitle
1083 public function testGetTitle_forBadRevision() {
1084 $rev = new Revision( [] );
1085 $this->assertNull( $rev->getTitle() );
1089 * @covers Revision::isMinor
1091 public function testIsMinor_true() {
1092 // Use a sysop to ensure we can mark edits as minor
1093 $sysop = $this->getTestSysop()->getUser();
1095 $this->testPage
->doEditContent(
1096 new WikitextContent( __METHOD__
),
1102 $rev = $this->testPage
->getRevision();
1104 $this->assertSame( true, $rev->isMinor() );
1108 * @covers Revision::isMinor
1110 public function testIsMinor_false() {
1111 $this->testPage
->doEditContent(
1112 new WikitextContent( __METHOD__
),
1116 $rev = $this->testPage
->getRevision();
1118 $this->assertSame( false, $rev->isMinor() );
1122 * @covers Revision::getTimestamp
1124 public function testGetTimestamp() {
1125 $testTimestamp = wfTimestampNow();
1127 $this->testPage
->doEditContent(
1128 new WikitextContent( __METHOD__
),
1131 $rev = $this->testPage
->getRevision();
1133 $this->assertInternalType( 'string', $rev->getTimestamp() );
1134 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1135 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1139 * @covers Revision::getUser
1140 * @covers Revision::getUserText
1142 public function testGetUserAndText() {
1143 $sysop = $this->getTestSysop()->getUser();
1145 $this->testPage
->doEditContent(
1146 new WikitextContent( __METHOD__
),
1152 $rev = $this->testPage
->getRevision();
1154 $this->assertSame( $sysop->getId(), $rev->getUser() );
1155 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1159 * @covers Revision::isDeleted
1161 public function testIsDeleted_nothingDeleted() {
1162 $rev = $this->testPage
->getRevision();
1164 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1165 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1166 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1167 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1171 * @covers Revision::getVisibility
1173 public function testGetVisibility_nothingDeleted() {
1174 $rev = $this->testPage
->getRevision();
1176 $this->assertSame( 0, $rev->getVisibility() );
1180 * @covers Revision::getComment
1182 public function testGetComment_notDeleted() {
1183 $expectedSummary = 'goatlicious summary';
1185 $this->testPage
->doEditContent(
1186 new WikitextContent( __METHOD__
),
1189 $rev = $this->testPage
->getRevision();
1191 $this->assertSame( $expectedSummary, $rev->getComment() );
1195 * @covers Revision::isUnpatrolled
1197 public function testIsUnpatrolled_returnsRecentChangesId() {
1198 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1199 $rev = $this->testPage
->getRevision();
1201 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1202 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1206 * @covers Revision::isUnpatrolled
1208 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1209 // This assumes that sysops are auto patrolled
1210 $sysop = $this->getTestSysop()->getUser();
1211 $this->testPage
->doEditContent(
1212 new WikitextContent( __METHOD__
),
1218 $rev = $this->testPage
->getRevision();
1220 $this->assertSame( 0, $rev->isUnpatrolled() );
1224 * This is a simple blanket test for all simple content getters and is methods to provide some
1225 * coverage before the split of Revision into multiple classes for MCR work.
1226 * @covers Revision::getContent
1227 * @covers Revision::getSerializedData
1228 * @covers Revision::getContentModel
1229 * @covers Revision::getContentFormat
1230 * @covers Revision::getContentHandler
1232 public function testSimpleContentGetters() {
1233 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1234 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1236 $this->testPage
->doEditContent(
1237 new WikitextContent( $expectedText ),
1240 $rev = $this->testPage
->getRevision();
1242 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1243 $this->assertSame( $expectedText, $rev->getSerializedData() );
1244 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1245 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1246 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1250 * @covers Revision::newKnownCurrent
1252 public function testNewKnownCurrent() {
1253 // Setup the services
1254 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1255 $this->setService( 'MainWANObjectCache', $cache );
1256 $db = wfGetDB( DB_MASTER
);
1258 // Get a fresh revision to use during testing
1259 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1260 $rev = $this->testPage
->getRevision();
1262 // Clear any previous cache for the revision during creation
1263 $key = $cache->makeGlobalKey( 'revision', $db->getDomainID(), $rev->getPage(), $rev->getId() );
1264 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1265 $this->assertFalse( $cache->get( $key ) );
1267 // Get the new revision and make sure it is in the cache and correct
1268 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1269 $this->assertRevEquals( $rev, $newRev );
1270 $this->assertRevEquals( $rev, $cache->get( $key ) );
1273 public function provideUserCanBitfield() {
1274 yield
[ 0, 0, [], null, true ];
1275 // Bitfields match, user has no permissions
1276 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1277 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1278 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1279 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1280 // Bitfields match, user (admin) does have permissions
1281 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1282 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1283 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1284 // Bitfields match, user (admin) does not have permissions
1285 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1286 // Bitfields match, user (oversight) does have permissions
1287 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1288 // Check permissions using the title
1290 Revision
::DELETED_TEXT
,
1291 Revision
::DELETED_TEXT
,
1293 Title
::newFromText( __METHOD__
),
1297 Revision
::DELETED_TEXT
,
1298 Revision
::DELETED_TEXT
,
1300 Title
::newFromText( __METHOD__
),
1306 * @dataProvider provideUserCanBitfield
1307 * @covers Revision::userCanBitfield
1309 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1310 $this->setMwGlobals(
1311 'wgGroupPermissions',
1314 'deletedtext' => true,
1315 'deletedhistory' => true,
1318 'viewsuppressed' => true,
1319 'suppressrevision' => true,
1323 $user = $this->getTestUser( $userGroups )->getUser();
1327 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1330 // Fallback to $wgUser
1331 $this->setMwGlobals(
1337 Revision
::userCanBitfield( $bitField, $field, null, $title )
1341 public function provideUserCan() {
1342 yield
[ 0, 0, [], true ];
1343 // Bitfields match, user has no permissions
1344 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1345 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1346 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1347 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1348 // Bitfields match, user (admin) does have permissions
1349 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1350 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1351 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1352 // Bitfields match, user (admin) does not have permissions
1353 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1354 // Bitfields match, user (oversight) does have permissions
1355 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1359 * @dataProvider provideUserCan
1360 * @covers Revision::userCan
1362 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1363 $this->setMwGlobals(
1364 'wgGroupPermissions',
1367 'deletedtext' => true,
1368 'deletedhistory' => true,
1371 'viewsuppressed' => true,
1372 'suppressrevision' => true,
1376 $user = $this->getTestUser( $userGroups )->getUser();
1377 $revision = new Revision( [ 'deleted' => $bitField ] );
1381 $revision->userCan( $field, $user )