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 );
229 * @covers Revision::newFromArchiveRow
231 public function testNewFromArchiveRow() {
232 $page = $this->createPage(
233 'RevisionStorageTest_testNewFromArchiveRow',
235 CONTENT_MODEL_WIKITEXT
237 $orig = $page->getRevision();
238 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
240 $dbr = wfGetDB( DB_REPLICA
);
242 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
244 $this->assertTrue( is_object( $res ), 'query failed' );
246 $row = $res->fetchObject();
249 $rev = Revision
::newFromArchiveRow( $row );
251 $this->assertRevEquals( $orig, $rev );
255 * @covers Revision::newFromId
257 public function testNewFromId() {
258 $orig = $this->testPage
->getRevision();
259 $rev = Revision
::newFromId( $orig->getId() );
260 $this->assertRevEquals( $orig, $rev );
264 * @covers Revision::newFromPageId
266 public function testNewFromPageId() {
267 $rev = Revision
::newFromPageId( $this->testPage
->getId() );
268 $this->assertRevEquals(
269 $this->testPage
->getRevision(),
275 * @covers Revision::newFromPageId
277 public function testNewFromPageIdWithLatestId() {
278 $rev = Revision
::newFromPageId(
279 $this->testPage
->getId(),
280 $this->testPage
->getLatest()
282 $this->assertRevEquals(
283 $this->testPage
->getRevision(),
289 * @covers Revision::newFromPageId
291 public function testNewFromPageIdWithNotLatestId() {
292 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
293 $rev = Revision
::newFromPageId(
294 $this->testPage
->getId(),
295 $this->testPage
->getRevision()->getPrevious()->getId()
297 $this->assertRevEquals(
298 $this->testPage
->getRevision()->getPrevious(),
304 * @covers Revision::fetchRevision
306 public function testFetchRevision() {
307 // Hidden process cache assertion below
308 $this->testPage
->getRevision()->getId();
310 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
311 $id = $this->testPage
->getRevision()->getId();
313 $res = Revision
::fetchRevision( $this->testPage
->getTitle() );
315 # note: order is unspecified
317 while ( ( $row = $res->fetchObject() ) ) {
318 $rows[$row->rev_id
] = $row;
321 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
322 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
326 * @covers Revision::selectFields
328 public function testSelectFields() {
329 global $wgContentHandlerUseDB;
331 $fields = Revision
::selectFields();
333 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
334 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
336 in_array( 'rev_timestamp', $fields ),
337 'missing rev_timestamp in list of fields'
339 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
341 if ( $wgContentHandlerUseDB ) {
342 $this->assertTrue( in_array( 'rev_content_model', $fields ),
343 'missing rev_content_model in list of fields' );
344 $this->assertTrue( in_array( 'rev_content_format', $fields ),
345 'missing rev_content_format in list of fields' );
350 * @covers Revision::getPage
352 public function testGetPage() {
353 $page = $this->testPage
;
355 $orig = $this->makeRevisionWithProps( [ 'page' => $page->getId() ] );
356 $rev = Revision
::newFromId( $orig->getId() );
358 $this->assertEquals( $page->getId(), $rev->getPage() );
362 * @covers Revision::isCurrent
364 public function testIsCurrent() {
365 $rev1 = $this->testPage
->getRevision();
367 # @todo find out if this should be true
368 # $this->assertTrue( $rev1->isCurrent() );
370 $rev1x = Revision
::newFromId( $rev1->getId() );
371 $this->assertTrue( $rev1x->isCurrent() );
373 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
374 $rev2 = $this->testPage
->getRevision();
376 # @todo find out if this should be true
377 # $this->assertTrue( $rev2->isCurrent() );
379 $rev1x = Revision
::newFromId( $rev1->getId() );
380 $this->assertFalse( $rev1x->isCurrent() );
382 $rev2x = Revision
::newFromId( $rev2->getId() );
383 $this->assertTrue( $rev2x->isCurrent() );
387 * @covers Revision::getPrevious
389 public function testGetPrevious() {
390 $oldestRevision = $this->testPage
->getOldestRevision();
391 $latestRevision = $this->testPage
->getLatest();
393 $this->assertNull( $oldestRevision->getPrevious() );
395 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
396 $newRevision = $this->testPage
->getRevision();
398 $this->assertNotNull( $newRevision->getPrevious() );
399 $this->assertEquals( $latestRevision, $newRevision->getPrevious()->getId() );
403 * @covers Revision::getNext
405 public function testGetNext() {
406 $rev1 = $this->testPage
->getRevision();
408 $this->assertNull( $rev1->getNext() );
410 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
411 $rev2 = $this->testPage
->getRevision();
413 $this->assertNotNull( $rev1->getNext() );
414 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
418 * @covers Revision::newNullRevision
420 public function testNewNullRevision() {
421 $this->testPage
->doEditContent( new WikitextContent( __METHOD__
), __METHOD__
);
422 $orig = $this->testPage
->getRevision();
424 $dbw = wfGetDB( DB_MASTER
);
425 $rev = Revision
::newNullRevision( $dbw, $this->testPage
->getId(), 'a null revision', false );
427 $this->assertNotEquals( $orig->getId(), $rev->getId(),
428 'new null revision should have a different id from the original revision' );
429 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
430 'new null revision should have the same text id as the original revision' );
431 $this->assertEquals( __METHOD__
, $rev->getContent()->getNativeData() );
435 * @covers Revision::insertOn
437 public function testInsertOn() {
438 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
440 $orig = $this->makeRevisionWithProps( [
444 // Make sure the revision was copied to ip_changes
445 $dbr = wfGetDB( DB_REPLICA
);
446 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
447 $row = $res->fetchObject();
449 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
450 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
453 public static function provideUserWasLastToEdit() {
454 yield
'actually the last edit' => [ 3, true ];
455 yield
'not the current edit, but still by this user' => [ 2, true ];
456 yield
'edit by another user' => [ 1, false ];
457 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
461 * @dataProvider provideUserWasLastToEdit
463 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
464 $userA = User
::newFromName( "RevisionStorageTest_userA" );
465 $userB = User
::newFromName( "RevisionStorageTest_userB" );
467 if ( $userA->getId() === 0 ) {
468 $userA = User
::createNew( $userA->getName() );
471 if ( $userB->getId() === 0 ) {
472 $userB = User
::createNew( $userB->getName() );
475 $ns = $this->getDefaultWikitextNS();
477 $dbw = wfGetDB( DB_MASTER
);
480 // create revisions -----------------------------
481 $page = WikiPage
::factory( Title
::newFromText(
482 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
483 $page->insertOn( $dbw );
485 $revisions[0] = new Revision( [
486 'page' => $page->getId(),
487 // we need the title to determine the page's default content model
488 'title' => $page->getTitle(),
489 'timestamp' => '20120101000000',
490 'user' => $userA->getId(),
492 'content_model' => CONTENT_MODEL_WIKITEXT
,
493 'summary' => 'edit zero'
495 $revisions[0]->insertOn( $dbw );
497 $revisions[1] = new Revision( [
498 'page' => $page->getId(),
499 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
500 'title' => $page->getTitle(),
501 'timestamp' => '20120101000100',
502 'user' => $userA->getId(),
504 'content_model' => CONTENT_MODEL_WIKITEXT
,
505 'summary' => 'edit one'
507 $revisions[1]->insertOn( $dbw );
509 $revisions[2] = new Revision( [
510 'page' => $page->getId(),
511 'title' => $page->getTitle(),
512 'timestamp' => '20120101000200',
513 'user' => $userB->getId(),
515 'content_model' => CONTENT_MODEL_WIKITEXT
,
516 'summary' => 'edit two'
518 $revisions[2]->insertOn( $dbw );
520 $revisions[3] = new Revision( [
521 'page' => $page->getId(),
522 'title' => $page->getTitle(),
523 'timestamp' => '20120101000300',
524 'user' => $userA->getId(),
526 'content_model' => CONTENT_MODEL_WIKITEXT
,
527 'summary' => 'edit three'
529 $revisions[3]->insertOn( $dbw );
531 $revisions[4] = new Revision( [
532 'page' => $page->getId(),
533 'title' => $page->getTitle(),
534 'timestamp' => '20120101000200',
535 'user' => $userA->getId(),
537 'content_model' => CONTENT_MODEL_WIKITEXT
,
538 'summary' => 'edit four'
540 $revisions[4]->insertOn( $dbw );
542 // test it ---------------------------------
543 $since = $revisions[$sinceIdx]->getTimestamp();
545 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
547 $this->assertEquals( $expectedLast, $wasLast );
551 * @param string $text
552 * @param string $title
553 * @param string $model
554 * @param string $format
558 private function newTestRevision( $text, $title = "Test",
559 $model = CONTENT_MODEL_WIKITEXT
, $format = null
561 if ( is_string( $title ) ) {
562 $title = Title
::newFromText( $title );
565 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
573 'content' => $content,
574 'length' => $content->getSize(),
575 'comment' => "testing",
576 'minor_edit' => false,
578 'content_format' => $format,
585 public function provideGetContentModel() {
586 // NOTE: we expect the help namespace to always contain wikitext
588 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
589 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
590 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
595 * @dataProvider provideGetContentModel
596 * @covers Revision::getContentModel
598 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
599 $rev = $this->newTestRevision( $text, $title, $model, $format );
601 $this->assertEquals( $expectedModel, $rev->getContentModel() );
604 public function provideGetContentFormat() {
605 // NOTE: we expect the help namespace to always contain wikitext
607 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
608 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
609 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
610 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
615 * @dataProvider provideGetContentFormat
616 * @covers Revision::getContentFormat
618 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
619 $rev = $this->newTestRevision( $text, $title, $model, $format );
621 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
624 public function provideGetContentHandler() {
625 // NOTE: we expect the help namespace to always contain wikitext
627 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
628 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
629 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
634 * @dataProvider provideGetContentHandler
635 * @covers Revision::getContentHandler
637 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
638 $rev = $this->newTestRevision( $text, $title, $model, $format );
640 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
643 public function provideGetContent() {
644 // NOTE: we expect the help namespace to always contain wikitext
646 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
648 serialize( 'hello world' ),
650 DummyContentForTesting
::MODEL_ID
,
652 Revision
::FOR_PUBLIC
,
653 serialize( 'hello world' )
656 serialize( 'hello world' ),
660 Revision
::FOR_PUBLIC
,
661 serialize( 'hello world' )
667 * @dataProvider provideGetContent
668 * @covers Revision::getContent
670 public function testGetContent( $text, $title, $model, $format,
671 $audience, $expectedSerialization
673 $rev = $this->newTestRevision( $text, $title, $model, $format );
674 $content = $rev->getContent( $audience );
677 $expectedSerialization,
678 is_null( $content ) ?
null : $content->serialize( $format )
683 * @covers Revision::getContent
685 public function testGetContent_failure() {
686 $rev = new Revision( [
687 'page' => $this->testPage
->getId(),
688 'content_model' => $this->testPage
->getContentModel(),
689 'text_id' => 123456789, // not in the test DB
692 $this->assertNull( $rev->getContent(),
693 "getContent() should return null if the revision's text blob could not be loaded." );
695 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
696 $this->assertNull( $rev->getContent(),
697 "getContent() should return null if the revision's text blob could not be loaded." );
700 public function provideGetSize() {
702 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
703 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
708 * @covers Revision::getSize
709 * @dataProvider provideGetSize
711 public function testGetSize( $text, $model, $expected_size ) {
712 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
713 $this->assertEquals( $expected_size, $rev->getSize() );
716 public function provideGetSha1() {
718 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
720 serialize( "hello world." ),
721 DummyContentForTesting
::MODEL_ID
,
722 Revision
::base36Sha1( serialize( "hello world." ) )
728 * @covers Revision::getSha1
729 * @dataProvider provideGetSha1
731 public function testGetSha1( $text, $model, $expected_hash ) {
732 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
733 $this->assertEquals( $expected_hash, $rev->getSha1() );
737 * Tests whether $rev->getContent() returns a clone when needed.
739 * @covers Revision::getContent
741 public function testGetContentClone() {
742 $content = new RevisionTestModifyableContent( "foo" );
748 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
750 'content' => $content,
751 'length' => $content->getSize(),
752 'comment' => "testing",
753 'minor_edit' => false,
757 /** @var RevisionTestModifyableContent $content */
758 $content = $rev->getContent( Revision
::RAW
);
759 $content->setText( "bar" );
761 /** @var RevisionTestModifyableContent $content2 */
762 $content2 = $rev->getContent( Revision
::RAW
);
763 // content is mutable, expect clone
764 $this->assertNotSame( $content, $content2, "expected a clone" );
765 // clone should contain the original text
766 $this->assertEquals( "foo", $content2->getText() );
768 $content2->setText( "bla bla" );
769 // clones should be independent
770 $this->assertEquals( "bar", $content->getText() );
774 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
775 * @covers Revision::getContent
777 public function testGetContentUncloned() {
778 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
779 $content = $rev->getContent( Revision
::RAW
);
780 $content2 = $rev->getContent( Revision
::RAW
);
782 // for immutable content like wikitext, this should be the same object
783 $this->assertSame( $content, $content2 );