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::__construct
157 public function testConstructFromRow() {
158 $latestRevisionId = $this->testPage
->getLatest();
159 $latestRevision = $this->testPage
->getRevision();
161 $dbr = wfGetDB( DB_REPLICA
);
164 Revision
::selectFields(),
165 [ 'rev_id' => $latestRevisionId ]
167 $this->assertTrue( is_object( $res ), 'query failed' );
169 $row = $res->fetchObject();
172 $this->assertRevEquals( $latestRevision, new Revision( $row ) );
176 * @covers Revision::newFromTitle
178 public function testNewFromTitle_withoutId() {
179 $latestRevId = $this->testPage
->getLatest();
181 $rev = Revision
::newFromTitle( $this->testPage
->getTitle() );
183 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
184 $this->assertEquals( $latestRevId, $rev->getId() );
188 * @covers Revision::newFromTitle
190 public function testNewFromTitle_withId() {
191 $latestRevId = $this->testPage
->getLatest();
193 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId );
195 $this->assertTrue( $this->testPage
->getTitle()->equals( $rev->getTitle() ) );
196 $this->assertEquals( $latestRevId, $rev->getId() );
200 * @covers Revision::newFromTitle
202 public function testNewFromTitle_withBadId() {
203 $latestRevId = $this->testPage
->getLatest();
205 $rev = Revision
::newFromTitle( $this->testPage
->getTitle(), $latestRevId +
1 );
207 $this->assertNull( $rev );
211 * @covers Revision::newFromRow
213 public function testNewFromRow() {
214 $orig = $this->makeRevisionWithProps();
216 $dbr = wfGetDB( DB_REPLICA
);
217 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
218 $this->assertTrue( is_object( $res ), 'query failed' );
220 $row = $res->fetchObject();
223 $rev = Revision
::newFromRow( $row );
225 $this->assertRevEquals( $orig, $rev );
228 public function provideTrueFalse() {
234 * @dataProvider provideTrueFalse
235 * @covers Revision::newFromArchiveRow
237 public function testNewFromArchiveRow( $contentHandlerUseDB ) {
238 $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
240 $page = $this->createPage(
241 'RevisionStorageTest_testNewFromArchiveRow',
243 CONTENT_MODEL_WIKITEXT
245 $orig = $page->getRevision();
246 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
248 $dbr = wfGetDB( DB_REPLICA
);
250 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
252 $this->assertTrue( is_object( $res ), 'query failed' );
254 $row = $res->fetchObject();
257 $rev = Revision
::newFromArchiveRow( $row );
259 $this->assertRevEquals( $orig, $rev );
263 * @covers Revision::newFromId
265 public function testNewFromId() {
266 $orig = $this->testPage
->getRevision();
267 $rev = Revision
::newFromId( $orig->getId() );
268 $this->assertRevEquals( $orig, $rev );
272 * @covers Revision::newFromPageId
274 public function testNewFromPageId() {
275 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
276 $this->assertRevEquals(
277 $this->testPage
->getRevision(),
283 * @covers Revision::newFromPageId
285 public function testNewFromPageIdWithLatestId() {
286 $rev = Revision
::newFromPageId(
287 $this->testPage
->getId(),
288 $this->testPage
->getLatest()
290 $this->assertRevEquals(
291 $this->testPage
->getRevision(),
297 * @covers Revision::newFromPageId
299 public function testNewFromPageIdWithNotLatestId() {
300 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
301 $rev = Revision
::newFromPageId(
302 $this->testPage
->getId(),
303 $this->testPage
->getRevision()->getPrevious()->getId()
305 $this->assertRevEquals(
306 $this->testPage
->getRevision()->getPrevious(),
312 * @covers Revision::fetchRevision
314 public function testFetchRevision() {
315 // Hidden process cache assertion below
316 $this->testPage
->getRevision()->getId();
318 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
319 $id = $this->testPage
->getRevision()->getId();
321 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
323 # note: order is unspecified
325 while ( ( $row = $res->fetchObject() ) ) {
326 $rows[$row->rev_id
] = $row;
329 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
330 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
334 * @covers Revision::getPage
336 public function testGetPage() {
337 $page = $this->testPage
;
339 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
340 $rev = Revision
::newFromId( $orig->getId() );
342 $this->assertEquals( $page->getId(), $rev->getPage() );
346 * @covers Revision::isCurrent
348 public function testIsCurrent() {
349 $rev1 = $this->testPage
->getRevision();
351 # @todo find out if this should be true
352 # $this->assertTrue( $rev1->isCurrent() );
354 $rev1x = Revision
::newFromId( $rev1->getId() );
355 $this->assertTrue( $rev1x->isCurrent() );
357 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
358 $rev2 = $this->testPage
->getRevision();
360 # @todo find out if this should be true
361 # $this->assertTrue( $rev2->isCurrent() );
363 $rev1x = Revision
::newFromId( $rev1->getId() );
364 $this->assertFalse( $rev1x->isCurrent() );
366 $rev2x = Revision
::newFromId( $rev2->getId() );
367 $this->assertTrue( $rev2x->isCurrent() );
371 * @covers Revision::getPrevious
373 public function testGetPrevious() {
374 $oldestRevision = $this->testPage
->getOldestRevision();
375 $latestRevision = $this->testPage
->getLatest();
377 $this->assertNull( $oldestRevision->getPrevious() );
379 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
380 $newRevision = $this->testPage
->getRevision();
382 $this->assertNotNull( $newRevision->getPrevious() );
383 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
387 * @covers Revision::getNext
389 public function testGetNext() {
390 $rev1 = $this->testPage
->getRevision();
392 $this->assertNull( $rev1->getNext() );
394 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
395 $rev2 = $this->testPage
->getRevision();
397 $this->assertNotNull( $rev1->getNext() );
398 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
402 * @covers Revision::newNullRevision
404 public function testNewNullRevision() {
405 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
406 $orig = $this->testPage
->getRevision();
408 $dbw = wfGetDB( DB_MASTER
);
409 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
411 $this->assertNotEquals( $orig->getId(), $rev->getId(),
412 'new null revision should have a different id from the original revision' );
413 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
414 'new null revision should have the same text id as the original revision' );
415 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
419 * @covers Revision::insertOn
421 public function testInsertOn() {
422 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
424 $orig = $this->makeRevisionWithProps( [
428 // Make sure the revision was copied to ip_changes
429 $dbr = wfGetDB( DB_REPLICA
);
430 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
431 $row = $res->fetchObject();
433 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
434 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
437 public static function provideUserWasLastToEdit() {
438 yield
'actually the last edit' => [ 3, true ];
439 yield
'not the current edit, but still by this user' => [ 2, true ];
440 yield
'edit by another user' => [ 1, false ];
441 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
445 * @dataProvider provideUserWasLastToEdit
447 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
448 $userA = User
::newFromName( "RevisionStorageTest_userA" );
449 $userB = User
::newFromName( "RevisionStorageTest_userB" );
451 if ( $userA->getId() === 0 ) {
452 $userA = User
::createNew( $userA->getName() );
455 if ( $userB->getId() === 0 ) {
456 $userB = User
::createNew( $userB->getName() );
459 $ns = $this->getDefaultWikitextNS();
461 $dbw = wfGetDB( DB_MASTER
);
464 // create revisions -----------------------------
465 $page = WikiPage
::factory( Title
::newFromText(
466 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
467 $page->insertOn( $dbw );
469 $revisions[0] = new Revision( [
470 'page' => $page->getId(),
471 // we need the title to determine the page's default content model
472 'title' => $page->getTitle(),
473 'timestamp' => '20120101000000',
474 'user' => $userA->getId(),
476 'content_model' => CONTENT_MODEL_WIKITEXT
,
477 'summary' => 'edit zero'
479 $revisions[0]->insertOn( $dbw );
481 $revisions[1] = new Revision( [
482 'page' => $page->getId(),
483 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
484 'title' => $page->getTitle(),
485 'timestamp' => '20120101000100',
486 'user' => $userA->getId(),
488 'content_model' => CONTENT_MODEL_WIKITEXT
,
489 'summary' => 'edit one'
491 $revisions[1]->insertOn( $dbw );
493 $revisions[2] = new Revision( [
494 'page' => $page->getId(),
495 'title' => $page->getTitle(),
496 'timestamp' => '20120101000200',
497 'user' => $userB->getId(),
499 'content_model' => CONTENT_MODEL_WIKITEXT
,
500 'summary' => 'edit two'
502 $revisions[2]->insertOn( $dbw );
504 $revisions[3] = new Revision( [
505 'page' => $page->getId(),
506 'title' => $page->getTitle(),
507 'timestamp' => '20120101000300',
508 'user' => $userA->getId(),
510 'content_model' => CONTENT_MODEL_WIKITEXT
,
511 'summary' => 'edit three'
513 $revisions[3]->insertOn( $dbw );
515 $revisions[4] = new Revision( [
516 'page' => $page->getId(),
517 'title' => $page->getTitle(),
518 'timestamp' => '20120101000200',
519 'user' => $userA->getId(),
521 'content_model' => CONTENT_MODEL_WIKITEXT
,
522 'summary' => 'edit four'
524 $revisions[4]->insertOn( $dbw );
526 // test it ---------------------------------
527 $since = $revisions[$sinceIdx]->getTimestamp();
529 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
531 $this->assertEquals( $expectedLast, $wasLast );
535 * @param string $text
536 * @param string $title
537 * @param string $model
538 * @param string $format
542 private function newTestRevision( $text, $title = "Test",
543 $model = CONTENT_MODEL_WIKITEXT
, $format = null
545 if ( is_string( $title ) ) {
546 $title = Title
::newFromText( $title );
549 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
557 'content' => $content,
558 'length' => $content->getSize(),
559 'comment' => "testing",
560 'minor_edit' => false,
562 'content_format' => $format,
569 public function provideGetContentModel() {
570 // NOTE: we expect the help namespace to always contain wikitext
572 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
573 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
574 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
579 * @dataProvider provideGetContentModel
580 * @covers Revision::getContentModel
582 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
583 $rev = $this->newTestRevision( $text, $title, $model, $format );
585 $this->assertEquals( $expectedModel, $rev->getContentModel() );
588 public function provideGetContentFormat() {
589 // NOTE: we expect the help namespace to always contain wikitext
591 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
592 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
593 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
594 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
599 * @dataProvider provideGetContentFormat
600 * @covers Revision::getContentFormat
602 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
603 $rev = $this->newTestRevision( $text, $title, $model, $format );
605 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
608 public function provideGetContentHandler() {
609 // NOTE: we expect the help namespace to always contain wikitext
611 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
612 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
613 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
618 * @dataProvider provideGetContentHandler
619 * @covers Revision::getContentHandler
621 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
622 $rev = $this->newTestRevision( $text, $title, $model, $format );
624 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
627 public function provideGetContent() {
628 // NOTE: we expect the help namespace to always contain wikitext
630 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
632 serialize( 'hello world' ),
634 DummyContentForTesting
::MODEL_ID
,
636 Revision
::FOR_PUBLIC
,
637 serialize( 'hello world' )
640 serialize( 'hello world' ),
644 Revision
::FOR_PUBLIC
,
645 serialize( 'hello world' )
651 * @dataProvider provideGetContent
652 * @covers Revision::getContent
654 public function testGetContent( $text, $title, $model, $format,
655 $audience, $expectedSerialization
657 $rev = $this->newTestRevision( $text, $title, $model, $format );
658 $content = $rev->getContent( $audience );
661 $expectedSerialization,
662 is_null( $content ) ?
null : $content->serialize( $format )
667 * @covers Revision::getContent
669 public function testGetContent_failure() {
670 $rev = new Revision( [
671 'page' => $this->testPage
->getId(),
672 'content_model' => $this->testPage
->getContentModel(),
673 'text_id' => 123456789, // not in the test DB
676 $this->assertNull( $rev->getContent(),
677 "getContent() should return null if the revision's text blob could not be loaded." );
679 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
680 $this->assertNull( $rev->getContent(),
681 "getContent() should return null if the revision's text blob could not be loaded." );
684 public function provideGetSize() {
686 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
687 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
692 * @covers Revision::getSize
693 * @dataProvider provideGetSize
695 public function testGetSize( $text, $model, $expected_size ) {
696 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
697 $this->assertEquals( $expected_size, $rev->getSize() );
700 public function provideGetSha1() {
702 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
704 serialize( "hello world." ),
705 DummyContentForTesting
::MODEL_ID
,
706 Revision
::base36Sha1( serialize( "hello world." ) )
712 * @covers Revision::getSha1
713 * @dataProvider provideGetSha1
715 public function testGetSha1( $text, $model, $expected_hash ) {
716 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
717 $this->assertEquals( $expected_hash, $rev->getSha1() );
721 * Tests whether $rev->getContent() returns a clone when needed.
723 * @covers Revision::getContent
725 public function testGetContentClone() {
726 $content = new RevisionTestModifyableContent( "foo" );
732 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
734 'content' => $content,
735 'length' => $content->getSize(),
736 'comment' => "testing",
737 'minor_edit' => false,
741 /** @var RevisionTestModifyableContent $content */
742 $content = $rev->getContent( Revision
::RAW
);
743 $content->setText( "bar" );
745 /** @var RevisionTestModifyableContent $content2 */
746 $content2 = $rev->getContent( Revision
::RAW
);
747 // content is mutable, expect clone
748 $this->assertNotSame( $content, $content2, "expected a clone" );
749 // clone should contain the original text
750 $this->assertEquals( "foo", $content2->getText() );
752 $content2->setText( "bla bla" );
753 // clones should be independent
754 $this->assertEquals( "bar", $content->getText() );
758 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
759 * @covers Revision::getContent
761 public function testGetContentUncloned() {
762 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
763 $content = $rev->getContent( Revision
::RAW
);
764 $content2 = $rev->getContent( Revision
::RAW
);
766 // for immutable content like wikitext, this should be the same object
767 $this->assertSame( $content, $content2 );
771 * @covers Revision::loadFromId
773 public function testLoadFromId() {
774 $rev = $this->testPage
->getRevision();
775 $this->assertRevEquals(
777 Revision
::loadFromId( wfGetDB( DB_MASTER
), $rev->getId() )
782 * @covers Revision::loadFromPageId
784 public function testLoadFromPageId() {
785 $this->assertRevEquals(
786 $this->testPage
->getRevision(),
787 Revision
::loadFromPageId( wfGetDB( DB_MASTER
), $this->testPage
->getId() )
792 * @covers Revision::loadFromPageId
794 public function testLoadFromPageIdWithLatestRevId() {
795 $this->assertRevEquals(
796 $this->testPage
->getRevision(),
797 Revision
::loadFromPageId(
798 wfGetDB( DB_MASTER
),
799 $this->testPage
->getId(),
800 $this->testPage
->getLatest()
806 * @covers Revision::loadFromPageId
808 public function testLoadFromPageIdWithNotLatestRevId() {
809 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
810 $this->assertRevEquals(
811 $this->testPage
->getRevision()->getPrevious(),
812 Revision
::loadFromPageId(
813 wfGetDB( DB_MASTER
),
814 $this->testPage
->getId(),
815 $this->testPage
->getRevision()->getPrevious()->getId()
821 * @covers Revision::loadFromTitle
823 public function testLoadFromTitle() {
824 $this->assertRevEquals(
825 $this->testPage
->getRevision(),
826 Revision
::loadFromTitle( wfGetDB( DB_MASTER
), $this->testPage
->getTitle() )
831 * @covers Revision::loadFromTitle
833 public function testLoadFromTitleWithLatestRevId() {
834 $this->assertRevEquals(
835 $this->testPage
->getRevision(),
836 Revision
::loadFromTitle(
837 wfGetDB( DB_MASTER
),
838 $this->testPage
->getTitle(),
839 $this->testPage
->getLatest()
845 * @covers Revision::loadFromTitle
847 public function testLoadFromTitleWithNotLatestRevId() {
848 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
849 $this->assertRevEquals(
850 $this->testPage
->getRevision()->getPrevious(),
851 Revision
::loadFromTitle(
852 wfGetDB( DB_MASTER
),
853 $this->testPage
->getTitle(),
854 $this->testPage
->getRevision()->getPrevious()->getId()
860 * @covers Revision::loadFromTimestamp()
862 public function testLoadFromTimestamp() {
863 $this->assertRevEquals(
864 $this->testPage
->getRevision(),
865 Revision
::loadFromTimestamp(
866 wfGetDB( DB_MASTER
),
867 $this->testPage
->getTitle(),
868 $this->testPage
->getRevision()->getTimestamp()