4 * @group ContentHandler
9 class RevisionIntegrationTest
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 MWNamespace
::clearCaches();
71 // Reset namespace cache
72 $wgContLang->resetNamespaces();
73 if ( !$this->testPage
) {
74 $this->testPage
= WikiPage
::factory( Title
::newFromText( 'UTPage' ) );
78 protected function tearDown() {
83 MWNamespace
::clearCaches();
84 // Reset namespace cache
85 $wgContLang->resetNamespaces();
88 private function makeRevisionWithProps( $props = null ) {
89 if ( $props === null ) {
93 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
94 $props['text'] = 'Lorem Ipsum';
97 if ( !isset( $props['comment'] ) ) {
98 $props['comment'] = 'just a test';
101 if ( !isset( $props['page'] ) ) {
102 $props['page'] = $this->testPage
->getId();
105 $rev = new Revision( $props );
107 $dbw = wfGetDB( DB_MASTER
);
108 $rev->insertOn( $dbw );
114 * @param string $titleString
115 * @param string $text
116 * @param string|null $model
120 private function createPage( $titleString, $text, $model = null ) {
121 if ( !preg_match( '/:/', $titleString ) &&
122 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
124 $ns = $this->getDefaultWikitextNS();
125 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
128 $title = Title
::newFromText( $titleString );
129 $wikipage = new WikiPage( $title );
131 // Delete the article if it already exists
132 if ( $wikipage->exists() ) {
133 $wikipage->doDeleteArticle( "done" );
136 $content = ContentHandler
::makeContent( $text, $title, $model );
137 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
142 private function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
143 $this->assertNotNull( $rev, 'missing revision' );
145 $this->assertEquals( $orig->getId(), $rev->getId() );
146 $this->assertEquals( $orig->getPage(), $rev->getPage() );
147 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
148 $this->assertEquals( $orig->getUser(), $rev->getUser() );
149 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
150 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
151 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
155 * @covers Revision::insertOn
157 public function testInsertOn_success() {
158 $parentId = $this->testPage
->getLatest();
160 // If an ExternalStore is set don't use it.
161 $this->setMwGlobals( 'wgDefaultExternalStore', false );
163 $rev = new Revision( [
164 'page' => $this->testPage
->getId(),
165 'title' => $this->testPage
->getTitle(),
166 'text' => 'Revision Text',
167 'comment' => 'Revision comment',
170 $revId = $rev->insertOn( wfGetDB( DB_MASTER
) );
172 $this->assertInternalType( 'integer', $revId );
173 $this->assertInternalType( 'integer', $rev->getTextId() );
174 $this->assertSame( $revId, $rev->getId() );
178 [ 'old_id', 'old_text' ],
179 "old_id = {$rev->getTextId()}",
180 [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
195 "rev_id = {$rev->getId()}",
197 strval( $rev->getId() ),
198 strval( $this->testPage
->getId() ),
199 strval( $rev->getTextId() ),
205 's0ngbdoxagreuf2vjtuxzwdz64n29xm',
211 * @covers Revision::insertOn
213 public function testInsertOn_exceptionOnNoPage() {
214 // If an ExternalStore is set don't use it.
215 $this->setMwGlobals( 'wgDefaultExternalStore', false );
216 $this->setExpectedException(
218 "Cannot insert revision: page ID must be nonzero"
221 $rev = new Revision( [] );
223 $rev->insertOn( wfGetDB( DB_MASTER
) );
227 * @covers Revision::newFromTitle
229 public function testNewFromTitle_withoutId() {
230 $latestRevId = $this->testPage
->getLatest();
232 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
234 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
235 $this->assertEquals( $latestRevId, $rev->getId() );
239 * @covers Revision::newFromTitle
241 public function testNewFromTitle_withId() {
242 $latestRevId = $this->testPage
->getLatest();
244 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
246 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
247 $this->assertEquals( $latestRevId, $rev->getId() );
251 * @covers Revision::newFromTitle
253 public function testNewFromTitle_withBadId() {
254 $latestRevId = $this->testPage
->getLatest();
256 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
258 $this->assertNull( $rev );
262 * @covers Revision::newFromRow
264 public function testNewFromRow() {
265 $orig = $this->makeRevisionWithProps();
267 $dbr = wfGetDB( DB_REPLICA
);
268 $revQuery = Revision
::getQueryInfo();
269 $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
270 __METHOD__
, [], $revQuery['joins'] );
271 $this->assertTrue( is_object( $res ), 'query failed' );
273 $row = $res->fetchObject();
276 $rev = Revision
::newFromRow( $row );
278 $this->assertRevEquals( $orig, $rev );
281 public function provideNewFromArchiveRow() {
297 return $f +
[ 'ar_namespace', 'ar_title' ];
303 return $f +
[ 'ar_namespace', 'ar_title' ];
309 unset( $f['ar_text_id'] );
316 unset( $f['ar_text_id'] );
323 * @dataProvider provideNewFromArchiveRow
324 * @covers Revision::newFromArchiveRow
326 public function testNewFromArchiveRow( $contentHandlerUseDB, $selectModifier ) {
327 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
329 $page = $this->createPage(
330 'RevisionStorageTest_testNewFromArchiveRow',
332 CONTENT_MODEL_WIKITEXT
334 $orig = $page->getRevision();
335 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
337 $dbr = wfGetDB( DB_REPLICA
);
338 $arQuery = Revision
::getArchiveQueryInfo();
339 $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
341 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
342 __METHOD__
, [], $arQuery['joins']
344 $this->assertTrue( is_object( $res ), 'query failed' );
346 $row = $res->fetchObject();
349 $rev = Revision
::newFromArchiveRow( $row );
351 $this->assertRevEquals( $orig, $rev );
355 * @covers Revision::newFromArchiveRow
357 public function testNewFromArchiveRowOverrides() {
358 $page = $this->createPage(
359 'RevisionStorageTest_testNewFromArchiveRow',
361 CONTENT_MODEL_WIKITEXT
363 $orig = $page->getRevision();
364 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
366 $dbr = wfGetDB( DB_REPLICA
);
367 $arQuery = Revision
::getArchiveQueryInfo();
369 $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
370 __METHOD__
, [], $arQuery['joins']
372 $this->assertTrue( is_object( $res ), 'query failed' );
374 $row = $res->fetchObject();
377 $rev = Revision
::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
379 $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
380 $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
384 * @covers Revision::newFromId
386 public function testNewFromId() {
387 $orig = $this->testPage
->getRevision();
388 $rev = Revision
::newFromId( $orig->getId() );
389 $this->assertRevEquals( $orig, $rev );
393 * @covers Revision::newFromPageId
395 public function testNewFromPageId() {
396 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
397 $this->assertRevEquals(
398 $this->testPage
->getRevision(),
404 * @covers Revision::newFromPageId
406 public function testNewFromPageIdWithLatestId() {
407 $rev = Revision
::newFromPageId(
408 $this->testPage
->getId(),
409 $this->testPage
->getLatest()
411 $this->assertRevEquals(
412 $this->testPage
->getRevision(),
418 * @covers Revision::newFromPageId
420 public function testNewFromPageIdWithNotLatestId() {
421 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
422 $rev = Revision
::newFromPageId(
423 $this->testPage
->getId(),
424 $this->testPage
->getRevision()->getPrevious()->getId()
426 $this->assertRevEquals(
427 $this->testPage
->getRevision()->getPrevious(),
433 * @covers Revision::fetchRevision
435 public function testFetchRevision() {
436 // Hidden process cache assertion below
437 $this->testPage
->getRevision()->getId();
439 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
440 $id = $this->testPage
->getRevision()->getId();
442 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
444 # note: order is unspecified
446 while ( ( $row = $res->fetchObject() ) ) {
447 $rows[$row->rev_id
] = $row;
450 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
451 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
455 * @covers Revision::getPage
457 public function testGetPage() {
458 $page = $this->testPage
;
460 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
461 $rev = Revision
::newFromId( $orig->getId() );
463 $this->assertEquals( $page->getId(), $rev->getPage() );
467 * @covers Revision::isCurrent
469 public function testIsCurrent() {
470 $rev1 = $this->testPage
->getRevision();
472 # @todo find out if this should be true
473 # $this->assertTrue( $rev1->isCurrent() );
475 $rev1x = Revision
::newFromId( $rev1->getId() );
476 $this->assertTrue( $rev1x->isCurrent() );
478 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
479 $rev2 = $this->testPage
->getRevision();
481 # @todo find out if this should be true
482 # $this->assertTrue( $rev2->isCurrent() );
484 $rev1x = Revision
::newFromId( $rev1->getId() );
485 $this->assertFalse( $rev1x->isCurrent() );
487 $rev2x = Revision
::newFromId( $rev2->getId() );
488 $this->assertTrue( $rev2x->isCurrent() );
492 * @covers Revision::getPrevious
494 public function testGetPrevious() {
495 $oldestRevision = $this->testPage
->getOldestRevision();
496 $latestRevision = $this->testPage
->getLatest();
498 $this->assertNull( $oldestRevision->getPrevious() );
500 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
501 $newRevision = $this->testPage
->getRevision();
503 $this->assertNotNull( $newRevision->getPrevious() );
504 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
508 * @covers Revision::getNext
510 public function testGetNext() {
511 $rev1 = $this->testPage
->getRevision();
513 $this->assertNull( $rev1->getNext() );
515 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
516 $rev2 = $this->testPage
->getRevision();
518 $this->assertNotNull( $rev1->getNext() );
519 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
523 * @covers Revision::newNullRevision
525 public function testNewNullRevision() {
526 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
527 $orig = $this->testPage
->getRevision();
529 $dbw = wfGetDB( DB_MASTER
);
530 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
532 $this->assertNotEquals( $orig->getId(), $rev->getId(),
533 'new null revision should have a different id from the original revision' );
534 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
535 'new null revision should have the same text id as the original revision' );
536 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
540 * @covers Revision::insertOn
542 public function testInsertOn() {
543 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
545 $orig = $this->makeRevisionWithProps( [
549 // Make sure the revision was copied to ip_changes
550 $dbr = wfGetDB( DB_REPLICA
);
551 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
552 $row = $res->fetchObject();
554 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
555 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
558 public static function provideUserWasLastToEdit() {
559 yield
'actually the last edit' => [ 3, true ];
560 yield
'not the current edit, but still by this user' => [ 2, true ];
561 yield
'edit by another user' => [ 1, false ];
562 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
566 * @dataProvider provideUserWasLastToEdit
568 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
569 $userA = User
::newFromName( "RevisionStorageTest_userA" );
570 $userB = User
::newFromName( "RevisionStorageTest_userB" );
572 if ( $userA->getId() === 0 ) {
573 $userA = User
::createNew( $userA->getName() );
576 if ( $userB->getId() === 0 ) {
577 $userB = User
::createNew( $userB->getName() );
580 $ns = $this->getDefaultWikitextNS();
582 $dbw = wfGetDB( DB_MASTER
);
585 // create revisions -----------------------------
586 $page = WikiPage
::factory( Title
::newFromText(
587 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
588 $page->insertOn( $dbw );
590 $revisions[0] = new Revision( [
591 'page' => $page->getId(),
592 // we need the title to determine the page's default content model
593 'title' => $page->getTitle(),
594 'timestamp' => '20120101000000',
595 'user' => $userA->getId(),
597 'content_model' => CONTENT_MODEL_WIKITEXT
,
598 'summary' => 'edit zero'
600 $revisions[0]->insertOn( $dbw );
602 $revisions[1] = new Revision( [
603 'page' => $page->getId(),
604 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
605 'title' => $page->getTitle(),
606 'timestamp' => '20120101000100',
607 'user' => $userA->getId(),
609 'content_model' => CONTENT_MODEL_WIKITEXT
,
610 'summary' => 'edit one'
612 $revisions[1]->insertOn( $dbw );
614 $revisions[2] = new Revision( [
615 'page' => $page->getId(),
616 'title' => $page->getTitle(),
617 'timestamp' => '20120101000200',
618 'user' => $userB->getId(),
620 'content_model' => CONTENT_MODEL_WIKITEXT
,
621 'summary' => 'edit two'
623 $revisions[2]->insertOn( $dbw );
625 $revisions[3] = new Revision( [
626 'page' => $page->getId(),
627 'title' => $page->getTitle(),
628 'timestamp' => '20120101000300',
629 'user' => $userA->getId(),
631 'content_model' => CONTENT_MODEL_WIKITEXT
,
632 'summary' => 'edit three'
634 $revisions[3]->insertOn( $dbw );
636 $revisions[4] = new Revision( [
637 'page' => $page->getId(),
638 'title' => $page->getTitle(),
639 'timestamp' => '20120101000200',
640 'user' => $userA->getId(),
642 'content_model' => CONTENT_MODEL_WIKITEXT
,
643 'summary' => 'edit four'
645 $revisions[4]->insertOn( $dbw );
647 // test it ---------------------------------
648 $since = $revisions[$sinceIdx]->getTimestamp();
650 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
652 $this->assertEquals( $expectedLast, $wasLast );
656 * @param string $text
657 * @param string $title
658 * @param string $model
659 * @param string $format
663 private function newTestRevision( $text, $title = "Test",
664 $model = CONTENT_MODEL_WIKITEXT
, $format = null
666 if ( is_string( $title ) ) {
667 $title = Title
::newFromText( $title );
670 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
678 'content' => $content,
679 'length' => $content->getSize(),
680 'comment' => "testing",
681 'minor_edit' => false,
683 'content_format' => $format,
690 public function provideGetContentModel() {
691 // NOTE: we expect the help namespace to always contain wikitext
693 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
694 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
695 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
700 * @dataProvider provideGetContentModel
701 * @covers Revision::getContentModel
703 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
704 $rev = $this->newTestRevision( $text, $title, $model, $format );
706 $this->assertEquals( $expectedModel, $rev->getContentModel() );
709 public function provideGetContentFormat() {
710 // NOTE: we expect the help namespace to always contain wikitext
712 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
713 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
714 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
715 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
720 * @dataProvider provideGetContentFormat
721 * @covers Revision::getContentFormat
723 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
724 $rev = $this->newTestRevision( $text, $title, $model, $format );
726 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
729 public function provideGetContentHandler() {
730 // NOTE: we expect the help namespace to always contain wikitext
732 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
733 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
734 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
739 * @dataProvider provideGetContentHandler
740 * @covers Revision::getContentHandler
742 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
743 $rev = $this->newTestRevision( $text, $title, $model, $format );
745 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
748 public function provideGetContent() {
749 // NOTE: we expect the help namespace to always contain wikitext
751 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
753 serialize( 'hello world' ),
755 DummyContentForTesting
::MODEL_ID
,
757 Revision
::FOR_PUBLIC
,
758 serialize( 'hello world' )
761 serialize( 'hello world' ),
765 Revision
::FOR_PUBLIC
,
766 serialize( 'hello world' )
772 * @dataProvider provideGetContent
773 * @covers Revision::getContent
775 public function testGetContent( $text, $title, $model, $format,
776 $audience, $expectedSerialization
778 $rev = $this->newTestRevision( $text, $title, $model, $format );
779 $content = $rev->getContent( $audience );
782 $expectedSerialization,
783 is_null( $content ) ?
null : $content->serialize( $format )
788 * @covers Revision::getContent
790 public function testGetContent_failure() {
791 $rev = new Revision( [
792 'page' => $this->testPage
->getId(),
793 'content_model' => $this->testPage
->getContentModel(),
794 'text_id' => 123456789, // not in the test DB
797 $this->assertNull( $rev->getContent(),
798 "getContent() should return null if the revision's text blob could not be loaded." );
800 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
801 $this->assertNull( $rev->getContent(),
802 "getContent() should return null if the revision's text blob could not be loaded." );
805 public function provideGetSize() {
807 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
808 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
813 * @covers Revision::getSize
814 * @dataProvider provideGetSize
816 public function testGetSize( $text, $model, $expected_size ) {
817 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
818 $this->assertEquals( $expected_size, $rev->getSize() );
821 public function provideGetSha1() {
823 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
825 serialize( "hello world." ),
826 DummyContentForTesting
::MODEL_ID
,
827 Revision
::base36Sha1( serialize( "hello world." ) )
833 * @covers Revision::getSha1
834 * @dataProvider provideGetSha1
836 public function testGetSha1( $text, $model, $expected_hash ) {
837 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
838 $this->assertEquals( $expected_hash, $rev->getSha1() );
842 * Tests whether $rev->getContent() returns a clone when needed.
844 * @covers Revision::getContent
846 public function testGetContentClone() {
847 $content = new RevisionTestModifyableContent( "foo" );
853 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
855 'content' => $content,
856 'length' => $content->getSize(),
857 'comment' => "testing",
858 'minor_edit' => false,
862 /** @var RevisionTestModifyableContent $content */
863 $content = $rev->getContent( Revision
::RAW
);
864 $content->setText( "bar" );
866 /** @var RevisionTestModifyableContent $content2 */
867 $content2 = $rev->getContent( Revision
::RAW
);
868 // content is mutable, expect clone
869 $this->assertNotSame( $content, $content2, "expected a clone" );
870 // clone should contain the original text
871 $this->assertEquals( "foo", $content2->getText() );
873 $content2->setText( "bla bla" );
874 // clones should be independent
875 $this->assertEquals( "bar", $content->getText() );
879 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
880 * @covers Revision::getContent
882 public function testGetContentUncloned() {
883 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
884 $content = $rev->getContent( Revision
::RAW
);
885 $content2 = $rev->getContent( Revision
::RAW
);
887 // for immutable content like wikitext, this should be the same object
888 $this->assertSame( $content, $content2 );
892 * @covers Revision::loadFromId
894 public function testLoadFromId() {
895 $rev = $this->testPage
->getRevision();
896 $this->assertRevEquals(
898 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
903 * @covers Revision::loadFromPageId
905 public function testLoadFromPageId() {
906 $this->assertRevEquals(
907 $this->testPage
->getRevision(),
908 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
913 * @covers Revision::loadFromPageId
915 public function testLoadFromPageIdWithLatestRevId() {
916 $this->assertRevEquals(
917 $this->testPage
->getRevision(),
918 Revision
::loadFromPageId(
919 wfGetDB( DB_MASTER
),
920 $this->testPage
->getId(),
921 $this->testPage
->getLatest()
927 * @covers Revision::loadFromPageId
929 public function testLoadFromPageIdWithNotLatestRevId() {
930 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
931 $this->assertRevEquals(
932 $this->testPage
->getRevision()->getPrevious(),
933 Revision
::loadFromPageId(
934 wfGetDB( DB_MASTER
),
935 $this->testPage
->getId(),
936 $this->testPage
->getRevision()->getPrevious()->getId()
942 * @covers Revision::loadFromTitle
944 public function testLoadFromTitle() {
945 $this->assertRevEquals(
946 $this->testPage
->getRevision(),
947 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
952 * @covers Revision::loadFromTitle
954 public function testLoadFromTitleWithLatestRevId() {
955 $this->assertRevEquals(
956 $this->testPage
->getRevision(),
957 Revision
::loadFromTitle(
958 wfGetDB( DB_MASTER
),
959 $this->testPage
->getTitle(),
960 $this->testPage
->getLatest()
966 * @covers Revision::loadFromTitle
968 public function testLoadFromTitleWithNotLatestRevId() {
969 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
970 $this->assertRevEquals(
971 $this->testPage
->getRevision()->getPrevious(),
972 Revision
::loadFromTitle(
973 wfGetDB( DB_MASTER
),
974 $this->testPage
->getTitle(),
975 $this->testPage
->getRevision()->getPrevious()->getId()
981 * @covers Revision::loadFromTimestamp()
983 public function testLoadFromTimestamp() {
984 $this->assertRevEquals(
985 $this->testPage
->getRevision(),
986 Revision
::loadFromTimestamp(
987 wfGetDB( DB_MASTER
),
988 $this->testPage
->getTitle(),
989 $this->testPage
->getRevision()->getTimestamp()
995 * @covers Revision::getParentLengths
997 public function testGetParentLengths_noRevIds() {
1000 Revision
::getParentLengths(
1001 wfGetDB( DB_MASTER
),
1008 * @covers Revision::getParentLengths
1010 public function testGetParentLengths_oneRevId() {
1011 $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1012 $textLength = strlen( $text );
1014 $this->testPage
->doEditContent( new WikitextContent( $text ), __METHOD__
);
1015 $rev[1] = $this->testPage
->getLatest();
1018 [ $rev[1] => strval( $textLength ) ],
1019 Revision
::getParentLengths(
1020 wfGetDB( DB_MASTER
),
1027 * @covers Revision::getParentLengths
1029 public function testGetParentLengths_multipleRevIds() {
1030 $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
1031 $textOneLength = strlen( $textOne );
1032 $textTwo = '831jr091jr092121j09rj1';
1033 $textTwoLength = strlen( $textTwo );
1035 $this->testPage
->doEditContent( new WikitextContent( $textOne ), __METHOD__
);
1036 $rev[1] = $this->testPage
->getLatest();
1037 $this->testPage
->doEditContent( new WikitextContent( $textTwo ), __METHOD__
);
1038 $rev[2] = $this->testPage
->getLatest();
1041 [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
1042 Revision
::getParentLengths(
1043 wfGetDB( DB_MASTER
),
1044 [ $rev[1], $rev[2] ]
1050 * @covers Revision::getTitle
1052 public function testGetTitle_fromExistingRevision() {
1054 $this->testPage
->getTitle()->equals(
1055 $this->testPage
->getRevision()->getTitle()
1061 * @covers Revision::getTitle
1063 public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
1064 $rev = new Revision( [ 'id' => $this->testPage
->getLatest() ] );
1066 $this->testPage
->getTitle()->equals(
1073 * @covers Revision::getTitle
1075 public function testGetTitle_forBadRevision() {
1076 $rev = new Revision( [] );
1077 $this->assertNull( $rev->getTitle() );
1081 * @covers Revision::isMinor
1083 public function testIsMinor_true() {
1084 // Use a sysop to ensure we can mark edits as minor
1085 $sysop = $this->getTestSysop()->getUser();
1087 $this->testPage
->doEditContent(
1088 new WikitextContent( __METHOD__
),
1094 $rev = $this->testPage
->getRevision();
1096 $this->assertSame( true, $rev->isMinor() );
1100 * @covers Revision::isMinor
1102 public function testIsMinor_false() {
1103 $this->testPage
->doEditContent(
1104 new WikitextContent( __METHOD__
),
1108 $rev = $this->testPage
->getRevision();
1110 $this->assertSame( false, $rev->isMinor() );
1114 * @covers Revision::getTimestamp
1116 public function testGetTimestamp() {
1117 $testTimestamp = wfTimestampNow();
1119 $this->testPage
->doEditContent(
1120 new WikitextContent( __METHOD__
),
1123 $rev = $this->testPage
->getRevision();
1125 $this->assertInternalType( 'string', $rev->getTimestamp() );
1126 $this->assertTrue( strlen( $rev->getTimestamp() ) == strlen( 'YYYYMMDDHHMMSS' ) );
1127 $this->assertContains( substr( $testTimestamp, 0, 10 ), $rev->getTimestamp() );
1131 * @covers Revision::getUser
1132 * @covers Revision::getUserText
1134 public function testGetUserAndText() {
1135 $sysop = $this->getTestSysop()->getUser();
1137 $this->testPage
->doEditContent(
1138 new WikitextContent( __METHOD__
),
1144 $rev = $this->testPage
->getRevision();
1146 $this->assertSame( $sysop->getId(), $rev->getUser() );
1147 $this->assertSame( $sysop->getName(), $rev->getUserText() );
1151 * @covers Revision::isDeleted
1153 public function testIsDeleted_nothingDeleted() {
1154 $rev = $this->testPage
->getRevision();
1156 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_TEXT
) );
1157 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_COMMENT
) );
1158 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_RESTRICTED
) );
1159 $this->assertSame( false, $rev->isDeleted( Revision
::DELETED_USER
) );
1163 * @covers Revision::getVisibility
1165 public function testGetVisibility_nothingDeleted() {
1166 $rev = $this->testPage
->getRevision();
1168 $this->assertSame( 0, $rev->getVisibility() );
1172 * @covers Revision::getComment
1174 public function testGetComment_notDeleted() {
1175 $expectedSummary = 'goatlicious summary';
1177 $this->testPage
->doEditContent(
1178 new WikitextContent( __METHOD__
),
1181 $rev = $this->testPage
->getRevision();
1183 $this->assertSame( $expectedSummary, $rev->getComment() );
1187 * @covers Revision::isUnpatrolled
1189 public function testIsUnpatrolled_returnsRecentChangesId() {
1190 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
1191 $rev = $this->testPage
->getRevision();
1193 $this->assertGreaterThan( 0, $rev->isUnpatrolled() );
1194 $this->assertSame( $rev->getRecentChange()->getAttribute( 'rc_id' ), $rev->isUnpatrolled() );
1198 * @covers Revision::isUnpatrolled
1200 public function testIsUnpatrolled_returnsZeroIfPatrolled() {
1201 // This assumes that sysops are auto patrolled
1202 $sysop = $this->getTestSysop()->getUser();
1203 $this->testPage
->doEditContent(
1204 new WikitextContent( __METHOD__
),
1210 $rev = $this->testPage
->getRevision();
1212 $this->assertSame( 0, $rev->isUnpatrolled() );
1216 * This is a simple blanket test for all simple content getters and is methods to provide some
1217 * coverage before the split of Revision into multiple classes for MCR work.
1218 * @covers Revision::getContent
1219 * @covers Revision::getSerializedData
1220 * @covers Revision::getContentModel
1221 * @covers Revision::getContentFormat
1222 * @covers Revision::getContentHandler
1224 public function testSimpleContentGetters() {
1225 $expectedText = 'testSimpleContentGetters in Revision. Goats love MCR...';
1226 $expectedSummary = 'goatlicious testSimpleContentGetters summary';
1228 $this->testPage
->doEditContent(
1229 new WikitextContent( $expectedText ),
1232 $rev = $this->testPage
->getRevision();
1234 $this->assertSame( $expectedText, $rev->getContent()->getNativeData() );
1235 $this->assertSame( $expectedText, $rev->getSerializedData() );
1236 $this->assertSame( $this->testPage
->getContentModel(), $rev->getContentModel() );
1237 $this->assertSame( $this->testPage
->getContent()->getDefaultFormat(), $rev->getContentFormat() );
1238 $this->assertSame( $this->testPage
->getContentHandler(), $rev->getContentHandler() );