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::insertOn
165 public function testInsertOn_success() {
166 $parentId = $this->testPage
->getLatest();
168 // If an ExternalStore is set don't use it.
169 $this->setMwGlobals( 'wgDefaultExternalStore', false );
171 $rev = new Revision( [
172 'page' => $this->testPage
->getId(),
173 'title' => $this->testPage
->getTitle(),
174 'text' => 'Revision Text',
175 'comment' => 'Revision comment',
178 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
180 $this->assertInternalType( 'integer', $revId );
181 $this->assertInternalType( 'integer', $rev->getTextId() );
182 $this->assertSame( $revId, $rev->getId() );
186 [ 'old_id', 'old_text' ],
187 "old_id = {$rev->getTextId()}",
188 [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
203 "rev_id = {$rev->getId()}",
205 strval( $rev->getId() ),
206 strval( $this->testPage
->getId() ),
207 strval( $rev->getTextId() ),
213 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
219 * @covers Revision::insertOn
221 public function testInsertOn_exceptionOnNoPage() {
222 // If an ExternalStore is set don't use it.
223 $this->setMwGlobals( 'wgDefaultExternalStore', false );
224 $this->setExpectedException(
226 "Cannot insert revision: page ID must be nonzero"
229 $rev = new Revision( [] );
231 $rev->insertOn( wfGetDB( DB_MASTER
) );
235 * @covers Revision::newFromTitle
237 public function testNewFromTitle_withoutId() {
238 $latestRevId = $this->testPage
->getLatest();
240 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
242 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
243 $this->assertEquals( $latestRevId, $rev->getId() );
247 * @covers Revision::newFromTitle
249 public function testNewFromTitle_withId() {
250 $latestRevId = $this->testPage
->getLatest();
252 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
254 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
255 $this->assertEquals( $latestRevId, $rev->getId() );
259 * @covers Revision::newFromTitle
261 public function testNewFromTitle_withBadId() {
262 $latestRevId = $this->testPage
->getLatest();
264 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
266 $this->assertNull( $rev );
270 * @covers Revision::newFromRow
272 public function testNewFromRow() {
273 $orig = $this->makeRevisionWithProps();
275 $dbr = wfGetDB( DB_REPLICA
);
276 $revQuery = Revision
::getQueryInfo();
277 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
278 __METHOD__
, [], $revQuery['joins'] );
279 $this->assertTrue( is_object( $res ), 'query failed' );
281 $row = $res->fetchObject();
284 $rev = Revision
::newFromRow( $row );
286 $this->assertRevEquals( $orig, $rev );
289 public function provideNewFromArchiveRow() {
297 return $f +
[ 'ar_namespace', 'ar_title' ];
302 unset( $f['ar_text_id'] );
309 * @dataProvider provideNewFromArchiveRow
310 * @covers Revision::newFromArchiveRow
312 public function testNewFromArchiveRow( $selectModifier ) {
313 $page = $this->createPage(
314 'RevisionStorageTest_testNewFromArchiveRow',
316 CONTENT_MODEL_WIKITEXT
318 $orig = $page->getRevision();
319 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
321 $dbr = wfGetDB( DB_REPLICA
);
322 $arQuery = Revision
::getArchiveQueryInfo();
323 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
325 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
326 __METHOD__
, [], $arQuery['joins']
328 $this->assertTrue( is_object( $res ), 'query failed' );
330 $row = $res->fetchObject();
333 $rev = Revision
::newFromArchiveRow( $row );
335 $this->assertRevEquals( $orig, $rev );
339 * @covers Revision::newFromArchiveRow
341 public function testNewFromArchiveRowOverrides() {
342 $page = $this->createPage(
343 'RevisionStorageTest_testNewFromArchiveRow',
345 CONTENT_MODEL_WIKITEXT
347 $orig = $page->getRevision();
348 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
350 $dbr = wfGetDB( DB_REPLICA
);
351 $arQuery = Revision
::getArchiveQueryInfo();
353 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
354 __METHOD__
, [], $arQuery['joins']
356 $this->assertTrue( is_object( $res ), 'query failed' );
358 $row = $res->fetchObject();
361 $rev = Revision
::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
363 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
364 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
368 * @covers Revision::newFromId
370 public function testNewFromId() {
371 $orig = $this->testPage
->getRevision();
372 $rev = Revision
::newFromId( $orig->getId() );
373 $this->assertRevEquals( $orig, $rev );
377 * @covers Revision::newFromPageId
379 public function testNewFromPageId() {
380 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
381 $this->assertRevEquals(
382 $this->testPage
->getRevision(),
388 * @covers Revision::newFromPageId
390 public function testNewFromPageIdWithLatestId() {
391 $rev = Revision
::newFromPageId(
392 $this->testPage
->getId(),
393 $this->testPage
->getLatest()
395 $this->assertRevEquals(
396 $this->testPage
->getRevision(),
402 * @covers Revision::newFromPageId
404 public function testNewFromPageIdWithNotLatestId() {
405 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
406 $rev = Revision
::newFromPageId(
407 $this->testPage
->getId(),
408 $this->testPage
->getRevision()->getPrevious()->getId()
410 $this->assertRevEquals(
411 $this->testPage
->getRevision()->getPrevious(),
417 * @covers Revision::fetchRevision
419 public function testFetchRevision() {
420 // Hidden process cache assertion below
421 $this->testPage
->getRevision()->getId();
423 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
424 $id = $this->testPage
->getRevision()->getId();
426 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
428 # note: order is unspecified
430 while ( ( $row = $res->fetchObject() ) ) {
431 $rows[$row->rev_id
] = $row;
434 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
435 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
439 * @covers Revision::getPage
441 public function testGetPage() {
442 $page = $this->testPage
;
444 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
445 $rev = Revision
::newFromId( $orig->getId() );
447 $this->assertEquals( $page->getId(), $rev->getPage() );
451 * @covers Revision::isCurrent
453 public function testIsCurrent() {
454 $rev1 = $this->testPage
->getRevision();
456 # @todo find out if this should be true
457 # $this->assertTrue( $rev1->isCurrent() );
459 $rev1x = Revision
::newFromId( $rev1->getId() );
460 $this->assertTrue( $rev1x->isCurrent() );
462 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
463 $rev2 = $this->testPage
->getRevision();
465 # @todo find out if this should be true
466 # $this->assertTrue( $rev2->isCurrent() );
468 $rev1x = Revision
::newFromId( $rev1->getId() );
469 $this->assertFalse( $rev1x->isCurrent() );
471 $rev2x = Revision
::newFromId( $rev2->getId() );
472 $this->assertTrue( $rev2x->isCurrent() );
476 * @covers Revision::getPrevious
478 public function testGetPrevious() {
479 $oldestRevision = $this->testPage
->getOldestRevision();
480 $latestRevision = $this->testPage
->getLatest();
482 $this->assertNull( $oldestRevision->getPrevious() );
484 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
485 $newRevision = $this->testPage
->getRevision();
487 $this->assertNotNull( $newRevision->getPrevious() );
488 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
492 * @covers Revision::getNext
494 public function testGetNext() {
495 $rev1 = $this->testPage
->getRevision();
497 $this->assertNull( $rev1->getNext() );
499 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
500 $rev2 = $this->testPage
->getRevision();
502 $this->assertNotNull( $rev1->getNext() );
503 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
507 * @covers Revision::newNullRevision
509 public function testNewNullRevision() {
510 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
511 $orig = $this->testPage
->getRevision();
513 $dbw = wfGetDB( DB_MASTER
);
514 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
516 $this->assertNotEquals( $orig->getId(), $rev->getId(),
517 'new null revision should have a different id from the original revision' );
518 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
519 'new null revision should have the same text id as the original revision' );
520 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
524 * @covers Revision::insertOn
526 public function testInsertOn() {
527 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
529 $orig = $this->makeRevisionWithProps( [
533 // Make sure the revision was copied to ip_changes
534 $dbr = wfGetDB( DB_REPLICA
);
535 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
536 $row = $res->fetchObject();
538 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
539 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
542 public static function provideUserWasLastToEdit() {
543 yield
'actually the last edit' => [ 3, true ];
544 yield
'not the current edit, but still by this user' => [ 2, true ];
545 yield
'edit by another user' => [ 1, false ];
546 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
550 * @dataProvider provideUserWasLastToEdit
552 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
553 $userA = User
::newFromName( "RevisionStorageTest_userA" );
554 $userB = User
::newFromName( "RevisionStorageTest_userB" );
556 if ( $userA->getId() === 0 ) {
557 $userA = User
::createNew( $userA->getName() );
560 if ( $userB->getId() === 0 ) {
561 $userB = User
::createNew( $userB->getName() );
564 $ns = $this->getDefaultWikitextNS();
566 $dbw = wfGetDB( DB_MASTER
);
569 // create revisions -----------------------------
570 $page = WikiPage
::factory( Title
::newFromText(
571 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
572 $page->insertOn( $dbw );
574 $revisions[0] = new Revision( [
575 'page' => $page->getId(),
576 // we need the title to determine the page's default content model
577 'title' => $page->getTitle(),
578 'timestamp' => '20120101000000',
579 'user' => $userA->getId(),
581 'content_model' => CONTENT_MODEL_WIKITEXT
,
582 'summary' => 'edit zero'
584 $revisions[0]->insertOn( $dbw );
586 $revisions[1] = new Revision( [
587 'page' => $page->getId(),
588 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
589 'title' => $page->getTitle(),
590 'timestamp' => '20120101000100',
591 'user' => $userA->getId(),
593 'content_model' => CONTENT_MODEL_WIKITEXT
,
594 'summary' => 'edit one'
596 $revisions[1]->insertOn( $dbw );
598 $revisions[2] = new Revision( [
599 'page' => $page->getId(),
600 'title' => $page->getTitle(),
601 'timestamp' => '20120101000200',
602 'user' => $userB->getId(),
604 'content_model' => CONTENT_MODEL_WIKITEXT
,
605 'summary' => 'edit two'
607 $revisions[2]->insertOn( $dbw );
609 $revisions[3] = new Revision( [
610 'page' => $page->getId(),
611 'title' => $page->getTitle(),
612 'timestamp' => '20120101000300',
613 'user' => $userA->getId(),
615 'content_model' => CONTENT_MODEL_WIKITEXT
,
616 'summary' => 'edit three'
618 $revisions[3]->insertOn( $dbw );
620 $revisions[4] = new Revision( [
621 'page' => $page->getId(),
622 'title' => $page->getTitle(),
623 'timestamp' => '20120101000200',
624 'user' => $userA->getId(),
626 'content_model' => CONTENT_MODEL_WIKITEXT
,
627 'summary' => 'edit four'
629 $revisions[4]->insertOn( $dbw );
631 // test it ---------------------------------
632 $since = $revisions[$sinceIdx]->getTimestamp();
634 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
636 $this->assertEquals( $expectedLast, $wasLast );
640 * @param string $text
641 * @param string $title
642 * @param string $model
643 * @param string $format
647 private function newTestRevision( $text, $title = "Test",
648 $model = CONTENT_MODEL_WIKITEXT
, $format = null
650 if ( is_string( $title ) ) {
651 $title = Title
::newFromText( $title );
654 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
662 'content' => $content,
663 'length' => $content->getSize(),
664 'comment' => "testing",
665 'minor_edit' => false,
667 'content_format' => $format,
674 public function provideGetContentModel() {
675 // NOTE: we expect the help namespace to always contain wikitext
677 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
678 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
679 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
684 * @dataProvider provideGetContentModel
685 * @covers Revision::getContentModel
687 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
688 $rev = $this->newTestRevision( $text, $title, $model, $format );
690 $this->assertEquals( $expectedModel, $rev->getContentModel() );
693 public function provideGetContentFormat() {
694 // NOTE: we expect the help namespace to always contain wikitext
696 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
697 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
698 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
699 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
704 * @dataProvider provideGetContentFormat
705 * @covers Revision::getContentFormat
707 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
708 $rev = $this->newTestRevision( $text, $title, $model, $format );
710 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
713 public function provideGetContentHandler() {
714 // NOTE: we expect the help namespace to always contain wikitext
716 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
717 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
718 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
723 * @dataProvider provideGetContentHandler
724 * @covers Revision::getContentHandler
726 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
727 $rev = $this->newTestRevision( $text, $title, $model, $format );
729 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
732 public function provideGetContent() {
733 // NOTE: we expect the help namespace to always contain wikitext
735 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
737 serialize( 'hello world' ),
739 DummyContentForTesting
::MODEL_ID
,
741 Revision
::FOR_PUBLIC
,
742 serialize( 'hello world' )
745 serialize( 'hello world' ),
749 Revision
::FOR_PUBLIC
,
750 serialize( 'hello world' )
756 * @dataProvider provideGetContent
757 * @covers Revision::getContent
759 public function testGetContent( $text, $title, $model, $format,
760 $audience, $expectedSerialization
762 $rev = $this->newTestRevision( $text, $title, $model, $format );
763 $content = $rev->getContent( $audience );
766 $expectedSerialization,
767 is_null( $content ) ?
null : $content->serialize( $format )
772 * @covers Revision::getContent
774 public function testGetContent_failure() {
775 $rev = new Revision( [
776 'page' => $this->testPage
->getId(),
777 'content_model' => $this->testPage
->getContentModel(),
778 'text_id' => 123456789, // not in the test DB
781 $this->assertNull( $rev->getContent(),
782 "getContent() should return null if the revision's text blob could not be loaded." );
784 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
785 $this->assertNull( $rev->getContent(),
786 "getContent() should return null if the revision's text blob could not be loaded." );
789 public function provideGetSize() {
791 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
792 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
797 * @covers Revision::getSize
798 * @dataProvider provideGetSize
800 public function testGetSize( $text, $model, $expected_size ) {
801 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
802 $this->assertEquals( $expected_size, $rev->getSize() );
805 public function provideGetSha1() {
807 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
809 serialize( "hello world." ),
810 DummyContentForTesting
::MODEL_ID
,
811 Revision
::base36Sha1( serialize( "hello world." ) )
817 * @covers Revision::getSha1
818 * @dataProvider provideGetSha1
820 public function testGetSha1( $text, $model, $expected_hash ) {
821 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
822 $this->assertEquals( $expected_hash, $rev->getSha1() );
826 * Tests whether $rev->getContent() returns a clone when needed.
828 * @covers Revision::getContent
830 public function testGetContentClone() {
831 $content = new RevisionTestModifyableContent( "foo" );
837 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
839 'content' => $content,
840 'length' => $content->getSize(),
841 'comment' => "testing",
842 'minor_edit' => false,
846 /** @var RevisionTestModifyableContent $content */
847 $content = $rev->getContent( Revision
::RAW
);
848 $content->setText( "bar" );
850 /** @var RevisionTestModifyableContent $content2 */
851 $content2 = $rev->getContent( Revision
::RAW
);
852 // content is mutable, expect clone
853 $this->assertNotSame( $content, $content2, "expected a clone" );
854 // clone should contain the original text
855 $this->assertEquals( "foo", $content2->getText() );
857 $content2->setText( "bla bla" );
858 // clones should be independent
859 $this->assertEquals( "bar", $content->getText() );
863 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
864 * @covers Revision::getContent
866 public function testGetContentUncloned() {
867 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
868 $content = $rev->getContent( Revision
::RAW
);
869 $content2 = $rev->getContent( Revision
::RAW
);
871 // for immutable content like wikitext, this should be the same object
872 $this->assertSame( $content, $content2 );
876 * @covers Revision::loadFromId
878 public function testLoadFromId() {
879 $rev = $this->testPage
->getRevision();
880 $this->assertRevEquals(
882 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
887 * @covers Revision::loadFromPageId
889 public function testLoadFromPageId() {
890 $this->assertRevEquals(
891 $this->testPage
->getRevision(),
892 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
897 * @covers Revision::loadFromPageId
899 public function testLoadFromPageIdWithLatestRevId() {
900 $this->assertRevEquals(
901 $this->testPage
->getRevision(),
902 Revision
::loadFromPageId(
903 wfGetDB( DB_MASTER
),
904 $this->testPage
->getId(),
905 $this->testPage
->getLatest()
911 * @covers Revision::loadFromPageId
913 public function testLoadFromPageIdWithNotLatestRevId() {
914 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
915 $this->assertRevEquals(
916 $this->testPage
->getRevision()->getPrevious(),
917 Revision
::loadFromPageId(
918 wfGetDB( DB_MASTER
),
919 $this->testPage
->getId(),
920 $this->testPage
->getRevision()->getPrevious()->getId()
926 * @covers Revision::loadFromTitle
928 public function testLoadFromTitle() {
929 $this->assertRevEquals(
930 $this->testPage
->getRevision(),
931 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
936 * @covers Revision::loadFromTitle
938 public function testLoadFromTitleWithLatestRevId() {
939 $this->assertRevEquals(
940 $this->testPage
->getRevision(),
941 Revision
::loadFromTitle(
942 wfGetDB( DB_MASTER
),
943 $this->testPage
->getTitle(),
944 $this->testPage
->getLatest()
950 * @covers Revision::loadFromTitle
952 public function testLoadFromTitleWithNotLatestRevId() {
953 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
954 $this->assertRevEquals(
955 $this->testPage
->getRevision()->getPrevious(),
956 Revision
::loadFromTitle(
957 wfGetDB( DB_MASTER
),
958 $this->testPage
->getTitle(),
959 $this->testPage
->getRevision()->getPrevious()->getId()
965 * @covers Revision::loadFromTimestamp()
967 public function testLoadFromTimestamp() {
968 $this->assertRevEquals(
969 $this->testPage
->getRevision(),
970 Revision
::loadFromTimestamp(
971 wfGetDB( DB_MASTER
),
972 $this->testPage
->getTitle(),
973 $this->testPage
->getRevision()->getTimestamp()
979 * @covers Revision::getParentLengths
981 public function testGetParentLengths_noRevIds() {
984 Revision
::getParentLengths(
985 wfGetDB( DB_MASTER
),
992 * @covers Revision::getParentLengths
994 public function testGetParentLengths_oneRevId() {
995 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
996 $textLength = strlen( $text );
998 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
999 $rev[1] = $this->testPage
->getLatest();
1002 [ $rev[1] => strval( $textLength ) ],
1003 Revision
::getParentLengths(
1004 wfGetDB( DB_MASTER
),
1011 * @covers Revision::getParentLengths
1013 public function testGetParentLengths_multipleRevIds() {
1014 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1015 $textOneLength = strlen( $textOne );
1016 $textTwo = '831jr091jr092121j09rj1';
1017 $textTwoLength = strlen( $textTwo );
1019 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1020 $rev[1] = $this->testPage
->getLatest();
1021 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1022 $rev[2] = $this->testPage
->getLatest();
1025 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1026 Revision
::getParentLengths(
1027 wfGetDB( DB_MASTER
),
1028 [ $rev[1], $rev[2] ]
1034 * @covers Revision::getTitle
1036 public function testGetTitle_fromExistingRevision() {
1038 $this->testPage
->getTitle()->equals(
1039 $this->testPage
->getRevision()->getTitle()
1045 * @covers Revision::getTitle
1047 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1048 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1050 $this->testPage
->getTitle()->equals(
1057 * @covers Revision::getTitle
1059 public function testGetTitle_forBadRevision() {
1060 $rev = new Revision( [] );
1061 $this->assertNull( $rev->getTitle() );
1065 * @covers Revision::isMinor
1067 public function testIsMinor_true() {
1068 // Use a sysop to ensure we can mark edits as minor
1069 $sysop = $this->getTestSysop()->getUser();
1071 $this->testPage
->doEditContent(
1072 new WikitextContent( __METHOD__
),
1078 $rev = $this->testPage
->getRevision();
1080 $this->assertSame( true, $rev->isMinor() );
1084 * @covers Revision::isMinor
1086 public function testIsMinor_false() {
1087 $this->testPage
->doEditContent(
1088 new WikitextContent( __METHOD__
),
1092 $rev = $this->testPage
->getRevision();
1094 $this->assertSame( false, $rev->isMinor() );
1098 * @covers Revision::getTimestamp
1100 public function testGetTimestamp() {
1101 $testTimestamp = wfTimestampNow();
1103 $this->testPage
->doEditContent(
1104 new WikitextContent( __METHOD__
),
1107 $rev = $this->testPage
->getRevision();
1109 $this->assertInternalType( 'string', $rev->getTimestamp() );
1110 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1111 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1115 * @covers Revision::getUser
1116 * @covers Revision::getUserText
1118 public function testGetUserAndText() {
1119 $sysop = $this->getTestSysop()->getUser();
1121 $this->testPage
->doEditContent(
1122 new WikitextContent( __METHOD__
),
1128 $rev = $this->testPage
->getRevision();
1130 $this->assertSame( $sysop->getId(), $rev->getUser() );
1131 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1135 * @covers Revision::isDeleted
1137 public function testIsDeleted_nothingDeleted() {
1138 $rev = $this->testPage
->getRevision();
1140 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1141 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1142 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1143 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1147 * @covers Revision::getVisibility
1149 public function testGetVisibility_nothingDeleted() {
1150 $rev = $this->testPage
->getRevision();
1152 $this->assertSame( 0, $rev->getVisibility() );
1156 * @covers Revision::getComment
1158 public function testGetComment_notDeleted() {
1159 $expectedSummary = 'goatlicious summary';
1161 $this->testPage
->doEditContent(
1162 new WikitextContent( __METHOD__
),
1165 $rev = $this->testPage
->getRevision();
1167 $this->assertSame( $expectedSummary, $rev->getComment() );
1171 * @covers Revision::isUnpatrolled
1173 public function testIsUnpatrolled_returnsRecentChangesId() {
1174 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1175 $rev = $this->testPage
->getRevision();
1177 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1178 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1182 * @covers Revision::isUnpatrolled
1184 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1185 // This assumes that sysops are auto patrolled
1186 $sysop = $this->getTestSysop()->getUser();
1187 $this->testPage
->doEditContent(
1188 new WikitextContent( __METHOD__
),
1194 $rev = $this->testPage
->getRevision();
1196 $this->assertSame( 0, $rev->isUnpatrolled() );
1200 * This is a simple blanket test for all simple content getters and is methods to provide some
1201 * coverage before the split of Revision into multiple classes for MCR work.
1202 * @covers Revision::getContent
1203 * @covers Revision::getSerializedData
1204 * @covers Revision::getContentModel
1205 * @covers Revision::getContentFormat
1206 * @covers Revision::getContentHandler
1208 public function testSimpleContentGetters() {
1209 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1210 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1212 $this->testPage
->doEditContent(
1213 new WikitextContent( $expectedText ),
1216 $rev = $this->testPage
->getRevision();
1218 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1219 $this->assertSame( $expectedText, $rev->getSerializedData() );
1220 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1221 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1222 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );