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
);
564 $orig->getTimestamp(),
565 wfTimestamp( TS_MW
, $row->ipc_rev_timestamp
)
569 public static function provideUserWasLastToEdit() {
570 yield
'actually the last edit' => [ 3, true ];
571 yield
'not the current edit, but still by this user' => [ 2, true ];
572 yield
'edit by another user' => [ 1, false ];
573 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
577 * @dataProvider provideUserWasLastToEdit
579 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
580 $userA = User
::newFromName( "RevisionStorageTest_userA" );
581 $userB = User
::newFromName( "RevisionStorageTest_userB" );
583 if ( $userA->getId() === 0 ) {
584 $userA = User
::createNew( $userA->getName() );
587 if ( $userB->getId() === 0 ) {
588 $userB = User
::createNew( $userB->getName() );
591 $ns = $this->getDefaultWikitextNS();
593 $dbw = wfGetDB( DB_MASTER
);
596 // create revisions -----------------------------
597 $page = WikiPage
::factory( Title
::newFromText(
598 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
599 $page->insertOn( $dbw );
601 $revisions[0] = new Revision( [
602 'page' => $page->getId(),
603 // we need the title to determine the page's default content model
604 'title' => $page->getTitle(),
605 'timestamp' => '20120101000000',
606 'user' => $userA->getId(),
608 'content_model' => CONTENT_MODEL_WIKITEXT
,
609 'summary' => 'edit zero'
611 $revisions[0]->insertOn( $dbw );
613 $revisions[1] = new Revision( [
614 'page' => $page->getId(),
615 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
616 'title' => $page->getTitle(),
617 'timestamp' => '20120101000100',
618 'user' => $userA->getId(),
620 'content_model' => CONTENT_MODEL_WIKITEXT
,
621 'summary' => 'edit one'
623 $revisions[1]->insertOn( $dbw );
625 $revisions[2] = new Revision( [
626 'page' => $page->getId(),
627 'title' => $page->getTitle(),
628 'timestamp' => '20120101000200',
629 'user' => $userB->getId(),
631 'content_model' => CONTENT_MODEL_WIKITEXT
,
632 'summary' => 'edit two'
634 $revisions[2]->insertOn( $dbw );
636 $revisions[3] = new Revision( [
637 'page' => $page->getId(),
638 'title' => $page->getTitle(),
639 'timestamp' => '20120101000300',
640 'user' => $userA->getId(),
642 'content_model' => CONTENT_MODEL_WIKITEXT
,
643 'summary' => 'edit three'
645 $revisions[3]->insertOn( $dbw );
647 $revisions[4] = new Revision( [
648 'page' => $page->getId(),
649 'title' => $page->getTitle(),
650 'timestamp' => '20120101000200',
651 'user' => $userA->getId(),
653 'content_model' => CONTENT_MODEL_WIKITEXT
,
654 'summary' => 'edit four'
656 $revisions[4]->insertOn( $dbw );
658 // test it ---------------------------------
659 $since = $revisions[$sinceIdx]->getTimestamp();
661 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
663 $this->assertEquals( $expectedLast, $wasLast );
667 * @param string $text
668 * @param string $title
669 * @param string $model
670 * @param string $format
674 private function newTestRevision( $text, $title = "Test",
675 $model = CONTENT_MODEL_WIKITEXT
, $format = null
677 if ( is_string( $title ) ) {
678 $title = Title
::newFromText( $title );
681 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
689 'content' => $content,
690 'length' => $content->getSize(),
691 'comment' => "testing",
692 'minor_edit' => false,
694 'content_format' => $format,
701 public function provideGetContentModel() {
702 // NOTE: we expect the help namespace to always contain wikitext
704 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
705 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
706 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
711 * @dataProvider provideGetContentModel
712 * @covers Revision::getContentModel
714 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
715 $rev = $this->newTestRevision( $text, $title, $model, $format );
717 $this->assertEquals( $expectedModel, $rev->getContentModel() );
720 public function provideGetContentFormat() {
721 // NOTE: we expect the help namespace to always contain wikitext
723 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
724 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
725 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
726 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
731 * @dataProvider provideGetContentFormat
732 * @covers Revision::getContentFormat
734 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
735 $rev = $this->newTestRevision( $text, $title, $model, $format );
737 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
740 public function provideGetContentHandler() {
741 // NOTE: we expect the help namespace to always contain wikitext
743 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
744 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
745 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
750 * @dataProvider provideGetContentHandler
751 * @covers Revision::getContentHandler
753 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
754 $rev = $this->newTestRevision( $text, $title, $model, $format );
756 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
759 public function provideGetContent() {
760 // NOTE: we expect the help namespace to always contain wikitext
762 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
764 serialize( 'hello world' ),
766 DummyContentForTesting
::MODEL_ID
,
768 Revision
::FOR_PUBLIC
,
769 serialize( 'hello world' )
772 serialize( 'hello world' ),
776 Revision
::FOR_PUBLIC
,
777 serialize( 'hello world' )
783 * @dataProvider provideGetContent
784 * @covers Revision::getContent
786 public function testGetContent( $text, $title, $model, $format,
787 $audience, $expectedSerialization
789 $rev = $this->newTestRevision( $text, $title, $model, $format );
790 $content = $rev->getContent( $audience );
793 $expectedSerialization,
794 is_null( $content ) ?
null : $content->serialize( $format )
799 * @covers Revision::getContent
801 public function testGetContent_failure() {
802 $rev = new Revision( [
803 'page' => $this->testPage
->getId(),
804 'content_model' => $this->testPage
->getContentModel(),
805 'text_id' => 123456789, // not in the test DB
808 $this->assertNull( $rev->getContent(),
809 "getContent() should return null if the revision's text blob could not be loaded." );
811 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
812 $this->assertNull( $rev->getContent(),
813 "getContent() should return null if the revision's text blob could not be loaded." );
816 public function provideGetSize() {
818 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
819 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
824 * @covers Revision::getSize
825 * @dataProvider provideGetSize
827 public function testGetSize( $text, $model, $expected_size ) {
828 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
829 $this->assertEquals( $expected_size, $rev->getSize() );
832 public function provideGetSha1() {
834 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
836 serialize( "hello world." ),
837 DummyContentForTesting
::MODEL_ID
,
838 Revision
::base36Sha1( serialize( "hello world." ) )
844 * @covers Revision::getSha1
845 * @dataProvider provideGetSha1
847 public function testGetSha1( $text, $model, $expected_hash ) {
848 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
849 $this->assertEquals( $expected_hash, $rev->getSha1() );
853 * Tests whether $rev->getContent() returns a clone when needed.
855 * @covers Revision::getContent
857 public function testGetContentClone() {
858 $content = new RevisionTestModifyableContent( "foo" );
864 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
866 'content' => $content,
867 'length' => $content->getSize(),
868 'comment' => "testing",
869 'minor_edit' => false,
873 /** @var RevisionTestModifyableContent $content */
874 $content = $rev->getContent( Revision
::RAW
);
875 $content->setText( "bar" );
877 /** @var RevisionTestModifyableContent $content2 */
878 $content2 = $rev->getContent( Revision
::RAW
);
879 // content is mutable, expect clone
880 $this->assertNotSame( $content, $content2, "expected a clone" );
881 // clone should contain the original text
882 $this->assertEquals( "foo", $content2->getText() );
884 $content2->setText( "bla bla" );
885 // clones should be independent
886 $this->assertEquals( "bar", $content->getText() );
890 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
891 * @covers Revision::getContent
893 public function testGetContentUncloned() {
894 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
895 $content = $rev->getContent( Revision
::RAW
);
896 $content2 = $rev->getContent( Revision
::RAW
);
898 // for immutable content like wikitext, this should be the same object
899 $this->assertSame( $content, $content2 );
903 * @covers Revision::loadFromId
905 public function testLoadFromId() {
906 $rev = $this->testPage
->getRevision();
907 $this->assertRevEquals(
909 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
914 * @covers Revision::loadFromPageId
916 public function testLoadFromPageId() {
917 $this->assertRevEquals(
918 $this->testPage
->getRevision(),
919 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
924 * @covers Revision::loadFromPageId
926 public function testLoadFromPageIdWithLatestRevId() {
927 $this->assertRevEquals(
928 $this->testPage
->getRevision(),
929 Revision
::loadFromPageId(
930 wfGetDB( DB_MASTER
),
931 $this->testPage
->getId(),
932 $this->testPage
->getLatest()
938 * @covers Revision::loadFromPageId
940 public function testLoadFromPageIdWithNotLatestRevId() {
941 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
942 $this->assertRevEquals(
943 $this->testPage
->getRevision()->getPrevious(),
944 Revision
::loadFromPageId(
945 wfGetDB( DB_MASTER
),
946 $this->testPage
->getId(),
947 $this->testPage
->getRevision()->getPrevious()->getId()
953 * @covers Revision::loadFromTitle
955 public function testLoadFromTitle() {
956 $this->assertRevEquals(
957 $this->testPage
->getRevision(),
958 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
963 * @covers Revision::loadFromTitle
965 public function testLoadFromTitleWithLatestRevId() {
966 $this->assertRevEquals(
967 $this->testPage
->getRevision(),
968 Revision
::loadFromTitle(
969 wfGetDB( DB_MASTER
),
970 $this->testPage
->getTitle(),
971 $this->testPage
->getLatest()
977 * @covers Revision::loadFromTitle
979 public function testLoadFromTitleWithNotLatestRevId() {
980 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
981 $this->assertRevEquals(
982 $this->testPage
->getRevision()->getPrevious(),
983 Revision
::loadFromTitle(
984 wfGetDB( DB_MASTER
),
985 $this->testPage
->getTitle(),
986 $this->testPage
->getRevision()->getPrevious()->getId()
992 * @covers Revision::loadFromTimestamp()
994 public function testLoadFromTimestamp() {
995 $this->assertRevEquals(
996 $this->testPage
->getRevision(),
997 Revision
::loadFromTimestamp(
998 wfGetDB( DB_MASTER
),
999 $this->testPage
->getTitle(),
1000 $this->testPage
->getRevision()->getTimestamp()
1006 * @covers Revision::getParentLengths
1008 public function testGetParentLengths_noRevIds() {
1011 Revision
::getParentLengths(
1012 wfGetDB( DB_MASTER
),
1019 * @covers Revision::getParentLengths
1021 public function testGetParentLengths_oneRevId() {
1022 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1023 $textLength = strlen( $text );
1025 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1026 $rev[1] = $this->testPage
->getLatest();
1029 [ $rev[1] => strval( $textLength ) ],
1030 Revision
::getParentLengths(
1031 wfGetDB( DB_MASTER
),
1038 * @covers Revision::getParentLengths
1040 public function testGetParentLengths_multipleRevIds() {
1041 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1042 $textOneLength = strlen( $textOne );
1043 $textTwo = '831jr091jr092121j09rj1';
1044 $textTwoLength = strlen( $textTwo );
1046 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1047 $rev[1] = $this->testPage
->getLatest();
1048 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1049 $rev[2] = $this->testPage
->getLatest();
1052 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1053 Revision
::getParentLengths(
1054 wfGetDB( DB_MASTER
),
1055 [ $rev[1], $rev[2] ]
1061 * @covers Revision::getTitle
1063 public function testGetTitle_fromExistingRevision() {
1065 $this->testPage
->getTitle()->equals(
1066 $this->testPage
->getRevision()->getTitle()
1072 * @covers Revision::getTitle
1074 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1075 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1077 $this->testPage
->getTitle()->equals(
1084 * @covers Revision::getTitle
1086 public function testGetTitle_forBadRevision() {
1087 $rev = new Revision( [] );
1088 $this->assertNull( $rev->getTitle() );
1092 * @covers Revision::isMinor
1094 public function testIsMinor_true() {
1095 // Use a sysop to ensure we can mark edits as minor
1096 $sysop = $this->getTestSysop()->getUser();
1098 $this->testPage
->doEditContent(
1099 new WikitextContent( __METHOD__
),
1105 $rev = $this->testPage
->getRevision();
1107 $this->assertSame( true, $rev->isMinor() );
1111 * @covers Revision::isMinor
1113 public function testIsMinor_false() {
1114 $this->testPage
->doEditContent(
1115 new WikitextContent( __METHOD__
),
1119 $rev = $this->testPage
->getRevision();
1121 $this->assertSame( false, $rev->isMinor() );
1125 * @covers Revision::getTimestamp
1127 public function testGetTimestamp() {
1128 $testTimestamp = wfTimestampNow();
1130 $this->testPage
->doEditContent(
1131 new WikitextContent( __METHOD__
),
1134 $rev = $this->testPage
->getRevision();
1136 $this->assertInternalType( 'string', $rev->getTimestamp() );
1137 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1138 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1142 * @covers Revision::getUser
1143 * @covers Revision::getUserText
1145 public function testGetUserAndText() {
1146 $sysop = $this->getTestSysop()->getUser();
1148 $this->testPage
->doEditContent(
1149 new WikitextContent( __METHOD__
),
1155 $rev = $this->testPage
->getRevision();
1157 $this->assertSame( $sysop->getId(), $rev->getUser() );
1158 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1162 * @covers Revision::isDeleted
1164 public function testIsDeleted_nothingDeleted() {
1165 $rev = $this->testPage
->getRevision();
1167 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1168 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1169 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1170 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1174 * @covers Revision::getVisibility
1176 public function testGetVisibility_nothingDeleted() {
1177 $rev = $this->testPage
->getRevision();
1179 $this->assertSame( 0, $rev->getVisibility() );
1183 * @covers Revision::getComment
1185 public function testGetComment_notDeleted() {
1186 $expectedSummary = 'goatlicious summary';
1188 $this->testPage
->doEditContent(
1189 new WikitextContent( __METHOD__
),
1192 $rev = $this->testPage
->getRevision();
1194 $this->assertSame( $expectedSummary, $rev->getComment() );
1198 * @covers Revision::isUnpatrolled
1200 public function testIsUnpatrolled_returnsRecentChangesId() {
1201 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1202 $rev = $this->testPage
->getRevision();
1204 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1205 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1209 * @covers Revision::isUnpatrolled
1211 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1212 // This assumes that sysops are auto patrolled
1213 $sysop = $this->getTestSysop()->getUser();
1214 $this->testPage
->doEditContent(
1215 new WikitextContent( __METHOD__
),
1221 $rev = $this->testPage
->getRevision();
1223 $this->assertSame( 0, $rev->isUnpatrolled() );
1227 * This is a simple blanket test for all simple content getters and is methods to provide some
1228 * coverage before the split of Revision into multiple classes for MCR work.
1229 * @covers Revision::getContent
1230 * @covers Revision::getSerializedData
1231 * @covers Revision::getContentModel
1232 * @covers Revision::getContentFormat
1233 * @covers Revision::getContentHandler
1235 public function testSimpleContentGetters() {
1236 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1237 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1239 $this->testPage
->doEditContent(
1240 new WikitextContent( $expectedText ),
1243 $rev = $this->testPage
->getRevision();
1245 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1246 $this->assertSame( $expectedText, $rev->getSerializedData() );
1247 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1248 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1249 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );
1253 * @covers Revision::newKnownCurrent
1255 public function testNewKnownCurrent() {
1256 // Setup the services
1257 $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
1258 $this->setService( 'MainWANObjectCache', $cache );
1259 $db = wfGetDB( DB_MASTER
);
1261 // Get a fresh revision to use during testing
1262 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1263 $rev = $this->testPage
->getRevision();
1265 // Clear any previous cache for the revision during creation
1266 $key = $cache->makeGlobalKey( 'revision', $db->getDomainID(), $rev->getPage(), $rev->getId() );
1267 $cache->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1268 $this->assertFalse( $cache->get( $key ) );
1270 // Get the new revision and make sure it is in the cache and correct
1271 $newRev = Revision
::newKnownCurrent( $db, $rev->getPage(), $rev->getId() );
1272 $this->assertRevEquals( $rev, $newRev );
1273 $this->assertRevEquals( $rev, $cache->get( $key ) );
1276 public function provideUserCanBitfield() {
1277 yield
[ 0, 0, [], null, true ];
1278 // Bitfields match, user has no permissions
1279 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], null, false ];
1280 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], null, false ];
1281 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], null, false ];
1282 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], null, false ];
1283 // Bitfields match, user (admin) does have permissions
1284 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], null, true ];
1285 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], null, true ];
1286 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], null, true ];
1287 // Bitfields match, user (admin) does not have permissions
1288 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], null, false ];
1289 // Bitfields match, user (oversight) does have permissions
1290 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], null, true ];
1291 // Check permissions using the title
1293 Revision
::DELETED_TEXT
,
1294 Revision
::DELETED_TEXT
,
1296 Title
::newFromText( __METHOD__
),
1300 Revision
::DELETED_TEXT
,
1301 Revision
::DELETED_TEXT
,
1303 Title
::newFromText( __METHOD__
),
1309 * @dataProvider provideUserCanBitfield
1310 * @covers Revision::userCanBitfield
1312 public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
1313 $this->setMwGlobals(
1314 'wgGroupPermissions',
1317 'deletedtext' => true,
1318 'deletedhistory' => true,
1321 'viewsuppressed' => true,
1322 'suppressrevision' => true,
1326 $user = $this->getTestUser( $userGroups )->getUser();
1330 Revision
::userCanBitfield( $bitField, $field, $user, $title )
1333 // Fallback to $wgUser
1334 $this->setMwGlobals(
1340 Revision
::userCanBitfield( $bitField, $field, null, $title )
1344 public function provideUserCan() {
1345 yield
[ 0, 0, [], true ];
1346 // Bitfields match, user has no permissions
1347 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [], false ];
1348 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [], false ];
1349 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [], false ];
1350 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [], false ];
1351 // Bitfields match, user (admin) does have permissions
1352 yield
[ Revision
::DELETED_TEXT
, Revision
::DELETED_TEXT
, [ 'sysop' ], true ];
1353 yield
[ Revision
::DELETED_COMMENT
, Revision
::DELETED_COMMENT
, [ 'sysop' ], true ];
1354 yield
[ Revision
::DELETED_USER
, Revision
::DELETED_USER
, [ 'sysop' ], true ];
1355 // Bitfields match, user (admin) does not have permissions
1356 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'sysop' ], false ];
1357 // Bitfields match, user (oversight) does have permissions
1358 yield
[ Revision
::DELETED_RESTRICTED
, Revision
::DELETED_RESTRICTED
, [ 'oversight' ], true ];
1362 * @dataProvider provideUserCan
1363 * @covers Revision::userCan
1365 public function testUserCan( $bitField, $field, $userGroups, $expected ) {
1366 $this->setMwGlobals(
1367 'wgGroupPermissions',
1370 'deletedtext' => true,
1371 'deletedhistory' => true,
1374 'viewsuppressed' => true,
1375 'suppressrevision' => true,
1379 $user = $this->getTestUser( $userGroups )->getUser();
1380 $revision = new Revision( [ 'deleted' => $bitField ] );
1384 $revision->userCan( $field, $user )