4 * @group ContentHandler
9 class RevisionIntegrationTest
extends MediaWikiTestCase
{
12 * @var WikiPage $the_page
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->the_page
) {
74 $this->the_page
= $this->createPage(
75 'RevisionStorageTest_the_page',
77 CONTENT_MODEL_WIKITEXT
82 protected function tearDown() {
87 MWNamespace
::clearCaches();
88 // Reset namespace cache
89 $wgContLang->resetNamespaces();
92 protected function makeRevision( $props = null ) {
93 if ( $props === null ) {
97 if ( !isset( $props['content'] ) && !isset( $props['text'] ) ) {
98 $props['text'] = 'Lorem Ipsum';
101 if ( !isset( $props['comment'] ) ) {
102 $props['comment'] = 'just a test';
105 if ( !isset( $props['page'] ) ) {
106 $props['page'] = $this->the_page
->getId();
109 $rev = new Revision( $props );
111 $dbw = wfGetDB( DB_MASTER
);
112 $rev->insertOn( $dbw );
118 * @param string $titleString
119 * @param string $text
120 * @param string|null $model
124 protected function createPage( $titleString, $text, $model = null ) {
125 if ( !preg_match( '/:/', $titleString ) &&
126 ( $model === null ||
$model === CONTENT_MODEL_WIKITEXT
)
128 $ns = $this->getDefaultWikitextNS();
129 $titleString = MWNamespace
::getCanonicalName( $ns ) . ':' . $titleString;
132 $title = Title
::newFromText( $titleString );
133 $wikipage = new WikiPage( $title );
135 // Delete the article if it already exists
136 if ( $wikipage->exists() ) {
137 $wikipage->doDeleteArticle( "done" );
140 $content = ContentHandler
::makeContent( $text, $title, $model );
141 $wikipage->doEditContent( $content, __METHOD__
, EDIT_NEW
);
146 protected function assertRevEquals( Revision
$orig, Revision
$rev = null ) {
147 $this->assertNotNull( $rev, 'missing revision' );
149 $this->assertEquals( $orig->getId(), $rev->getId() );
150 $this->assertEquals( $orig->getPage(), $rev->getPage() );
151 $this->assertEquals( $orig->getTimestamp(), $rev->getTimestamp() );
152 $this->assertEquals( $orig->getUser(), $rev->getUser() );
153 $this->assertEquals( $orig->getContentModel(), $rev->getContentModel() );
154 $this->assertEquals( $orig->getContentFormat(), $rev->getContentFormat() );
155 $this->assertEquals( $orig->getSha1(), $rev->getSha1() );
159 * @covers Revision::__construct
161 public function testConstructFromRow() {
162 $orig = $this->makeRevision();
164 $dbr = wfGetDB( DB_REPLICA
);
165 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
166 $this->assertTrue( is_object( $res ), 'query failed' );
168 $row = $res->fetchObject();
171 $rev = new Revision( $row );
173 $this->assertRevEquals( $orig, $rev );
177 * @covers Revision::newFromTitle
179 public function testNewFromTitle_withoutId() {
180 $page = $this->createPage(
183 CONTENT_MODEL_WIKITEXT
185 $latestRevId = $page->getLatest();
187 $rev = Revision
::newFromTitle( $page->getTitle() );
189 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
190 $this->assertEquals( $latestRevId, $rev->getId() );
194 * @covers Revision::newFromTitle
196 public function testNewFromTitle_withId() {
197 $page = $this->createPage(
200 CONTENT_MODEL_WIKITEXT
202 $latestRevId = $page->getLatest();
204 $rev = Revision
::newFromTitle( $page->getTitle(), $latestRevId );
206 $this->assertTrue( $page->getTitle()->equals( $rev->getTitle() ) );
207 $this->assertEquals( $latestRevId, $rev->getId() );
211 * @covers Revision::newFromTitle
213 public function testNewFromTitle_withBadId() {
214 $page = $this->createPage(
217 CONTENT_MODEL_WIKITEXT
219 $latestRevId = $page->getLatest();
221 $rev = Revision
::newFromTitle( $page->getTitle(), $latestRevId +
1 );
223 $this->assertNull( $rev );
227 * @covers Revision::newFromRow
229 public function testNewFromRow() {
230 $orig = $this->makeRevision();
232 $dbr = wfGetDB( DB_REPLICA
);
233 $res = $dbr->select( 'revision', Revision
::selectFields(), [ 'rev_id' => $orig->getId() ] );
234 $this->assertTrue( is_object( $res ), 'query failed' );
236 $row = $res->fetchObject();
239 $rev = Revision
::newFromRow( $row );
241 $this->assertRevEquals( $orig, $rev );
245 * @covers Revision::newFromArchiveRow
247 public function testNewFromArchiveRow() {
248 $page = $this->createPage(
249 'RevisionStorageTest_testNewFromArchiveRow',
251 CONTENT_MODEL_WIKITEXT
253 $orig = $page->getRevision();
254 $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
256 $dbr = wfGetDB( DB_REPLICA
);
258 'archive', Revision
::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
260 $this->assertTrue( is_object( $res ), 'query failed' );
262 $row = $res->fetchObject();
265 $rev = Revision
::newFromArchiveRow( $row );
267 $this->assertRevEquals( $orig, $rev );
271 * @covers Revision::newFromId
273 public function testNewFromId() {
274 $orig = $this->makeRevision();
276 $rev = Revision
::newFromId( $orig->getId() );
278 $this->assertRevEquals( $orig, $rev );
282 * @covers Revision::fetchRevision
284 public function testFetchRevision() {
285 $page = $this->createPage(
286 'RevisionStorageTest_testFetchRevision',
288 CONTENT_MODEL_WIKITEXT
291 // Hidden process cache assertion below
292 $page->getRevision()->getId();
294 $page->doEditContent( new WikitextContent( 'two' ), 'second rev' );
295 $id = $page->getRevision()->getId();
297 $res = Revision
::fetchRevision( $page->getTitle() );
299 # note: order is unspecified
301 while ( ( $row = $res->fetchObject() ) ) {
302 $rows[$row->rev_id
] = $row;
305 $this->assertEquals( 1, count( $rows ), 'expected exactly one revision' );
306 $this->assertArrayHasKey( $id, $rows, 'missing revision with id ' . $id );
310 * @covers Revision::selectFields
312 public function testSelectFields() {
313 global $wgContentHandlerUseDB;
315 $fields = Revision
::selectFields();
317 $this->assertTrue( in_array( 'rev_id', $fields ), 'missing rev_id in list of fields' );
318 $this->assertTrue( in_array( 'rev_page', $fields ), 'missing rev_page in list of fields' );
320 in_array( 'rev_timestamp', $fields ),
321 'missing rev_timestamp in list of fields'
323 $this->assertTrue( in_array( 'rev_user', $fields ), 'missing rev_user in list of fields' );
325 if ( $wgContentHandlerUseDB ) {
326 $this->assertTrue( in_array( 'rev_content_model', $fields ),
327 'missing rev_content_model in list of fields' );
328 $this->assertTrue( in_array( 'rev_content_format', $fields ),
329 'missing rev_content_format in list of fields' );
334 * @covers Revision::getPage
336 public function testGetPage() {
337 $page = $this->the_page
;
339 $orig = $this->makeRevision( [ '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 $page = $this->createPage(
350 'RevisionStorageTest_testIsCurrent',
352 CONTENT_MODEL_WIKITEXT
354 $rev1 = $page->getRevision();
356 # @todo find out if this should be true
357 # $this->assertTrue( $rev1->isCurrent() );
359 $rev1x = Revision
::newFromId( $rev1->getId() );
360 $this->assertTrue( $rev1x->isCurrent() );
362 $page->doEditContent(
363 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
366 $rev2 = $page->getRevision();
368 # @todo find out if this should be true
369 # $this->assertTrue( $rev2->isCurrent() );
371 $rev1x = Revision
::newFromId( $rev1->getId() );
372 $this->assertFalse( $rev1x->isCurrent() );
374 $rev2x = Revision
::newFromId( $rev2->getId() );
375 $this->assertTrue( $rev2x->isCurrent() );
379 * @covers Revision::getPrevious
381 public function testGetPrevious() {
382 $page = $this->createPage(
383 'RevisionStorageTest_testGetPrevious',
384 'Lorem Ipsum testGetPrevious',
385 CONTENT_MODEL_WIKITEXT
387 $rev1 = $page->getRevision();
389 $this->assertNull( $rev1->getPrevious() );
391 $page->doEditContent(
392 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
393 'second rev testGetPrevious' );
394 $rev2 = $page->getRevision();
396 $this->assertNotNull( $rev2->getPrevious() );
397 $this->assertEquals( $rev1->getId(), $rev2->getPrevious()->getId() );
401 * @covers Revision::getNext
403 public function testGetNext() {
404 $page = $this->createPage(
405 'RevisionStorageTest_testGetNext',
406 'Lorem Ipsum testGetNext',
407 CONTENT_MODEL_WIKITEXT
409 $rev1 = $page->getRevision();
411 $this->assertNull( $rev1->getNext() );
413 $page->doEditContent(
414 ContentHandler
::makeContent( 'Bla bla', $page->getTitle(), CONTENT_MODEL_WIKITEXT
),
415 'second rev testGetNext'
417 $rev2 = $page->getRevision();
419 $this->assertNotNull( $rev1->getNext() );
420 $this->assertEquals( $rev2->getId(), $rev1->getNext()->getId() );
424 * @covers Revision::newNullRevision
426 public function testNewNullRevision() {
427 $page = $this->createPage(
428 'RevisionStorageTest_testNewNullRevision',
430 CONTENT_MODEL_WIKITEXT
432 $orig = $page->getRevision();
434 $dbw = wfGetDB( DB_MASTER
);
435 $rev = Revision
::newNullRevision( $dbw, $page->getId(), 'a null revision', false );
437 $this->assertNotEquals( $orig->getId(), $rev->getId(),
438 'new null revision shold have a different id from the original revision' );
439 $this->assertEquals( $orig->getTextId(), $rev->getTextId(),
440 'new null revision shold have the same text id as the original revision' );
441 $this->assertEquals( 'some testing text', $rev->getContent()->getNativeData() );
445 * @covers Revision::insertOn
447 public function testInsertOn() {
448 $ip = '2600:387:ed7:947e:8c16:a1ad:dd34:1dd7';
450 $orig = $this->makeRevision( [
454 // Make sure the revision was copied to ip_changes
455 $dbr = wfGetDB( DB_REPLICA
);
456 $res = $dbr->select( 'ip_changes', '*', [ 'ipc_rev_id' => $orig->getId() ] );
457 $row = $res->fetchObject();
459 $this->assertEquals( IP
::toHex( $ip ), $row->ipc_hex
);
460 $this->assertEquals( $orig->getTimestamp(), $row->ipc_rev_timestamp
);
463 public static function provideUserWasLastToEdit() {
464 yield
'actually the last edit' => [ 3, true ];
465 yield
'not the current edit, but still by this user' => [ 2, true ];
466 yield
'edit by another user' => [ 1, false ];
467 yield
'first edit, by this user, but another user edited in the mean time' => [ 0, false ];
471 * @dataProvider provideUserWasLastToEdit
473 public function testUserWasLastToEdit( $sinceIdx, $expectedLast ) {
474 $userA = User
::newFromName( "RevisionStorageTest_userA" );
475 $userB = User
::newFromName( "RevisionStorageTest_userB" );
477 if ( $userA->getId() === 0 ) {
478 $userA = User
::createNew( $userA->getName() );
481 if ( $userB->getId() === 0 ) {
482 $userB = User
::createNew( $userB->getName() );
485 $ns = $this->getDefaultWikitextNS();
487 $dbw = wfGetDB( DB_MASTER
);
490 // create revisions -----------------------------
491 $page = WikiPage
::factory( Title
::newFromText(
492 'RevisionStorageTest_testUserWasLastToEdit', $ns ) );
493 $page->insertOn( $dbw );
495 $revisions[0] = new Revision( [
496 'page' => $page->getId(),
497 // we need the title to determine the page's default content model
498 'title' => $page->getTitle(),
499 'timestamp' => '20120101000000',
500 'user' => $userA->getId(),
502 'content_model' => CONTENT_MODEL_WIKITEXT
,
503 'summary' => 'edit zero'
505 $revisions[0]->insertOn( $dbw );
507 $revisions[1] = new Revision( [
508 'page' => $page->getId(),
509 // still need the title, because $page->getId() is 0 (there's no entry in the page table)
510 'title' => $page->getTitle(),
511 'timestamp' => '20120101000100',
512 'user' => $userA->getId(),
514 'content_model' => CONTENT_MODEL_WIKITEXT
,
515 'summary' => 'edit one'
517 $revisions[1]->insertOn( $dbw );
519 $revisions[2] = new Revision( [
520 'page' => $page->getId(),
521 'title' => $page->getTitle(),
522 'timestamp' => '20120101000200',
523 'user' => $userB->getId(),
525 'content_model' => CONTENT_MODEL_WIKITEXT
,
526 'summary' => 'edit two'
528 $revisions[2]->insertOn( $dbw );
530 $revisions[3] = new Revision( [
531 'page' => $page->getId(),
532 'title' => $page->getTitle(),
533 'timestamp' => '20120101000300',
534 'user' => $userA->getId(),
536 'content_model' => CONTENT_MODEL_WIKITEXT
,
537 'summary' => 'edit three'
539 $revisions[3]->insertOn( $dbw );
541 $revisions[4] = new Revision( [
542 'page' => $page->getId(),
543 'title' => $page->getTitle(),
544 'timestamp' => '20120101000200',
545 'user' => $userA->getId(),
547 'content_model' => CONTENT_MODEL_WIKITEXT
,
548 'summary' => 'edit four'
550 $revisions[4]->insertOn( $dbw );
552 // test it ---------------------------------
553 $since = $revisions[$sinceIdx]->getTimestamp();
555 $wasLast = Revision
::userWasLastToEdit( $dbw, $page->getId(), $userA->getId(), $since );
557 $this->assertEquals( $expectedLast, $wasLast );
561 * @param string $text
562 * @param string $title
563 * @param string $model
564 * @param string $format
568 private function newTestRevision( $text, $title = "Test",
569 $model = CONTENT_MODEL_WIKITEXT
, $format = null
571 if ( is_string( $title ) ) {
572 $title = Title
::newFromText( $title );
575 $content = ContentHandler
::makeContent( $text, $title, $model, $format );
583 'content' => $content,
584 'length' => $content->getSize(),
585 'comment' => "testing",
586 'minor_edit' => false,
588 'content_format' => $format,
595 public function provideGetContentModel() {
596 // NOTE: we expect the help namespace to always contain wikitext
598 [ 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT
],
599 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS
],
600 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
605 * @dataProvider provideGetContentModel
606 * @covers Revision::getContentModel
608 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) {
609 $rev = $this->newTestRevision( $text, $title, $model, $format );
611 $this->assertEquals( $expectedModel, $rev->getContentModel() );
614 public function provideGetContentFormat() {
615 // NOTE: we expect the help namespace to always contain wikitext
617 [ 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT
],
618 [ 'hello world', 'Help:Hello', CONTENT_MODEL_CSS
, null, CONTENT_FORMAT_CSS
],
619 [ 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS
],
620 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, DummyContentForTesting
::MODEL_ID
],
625 * @dataProvider provideGetContentFormat
626 * @covers Revision::getContentFormat
628 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) {
629 $rev = $this->newTestRevision( $text, $title, $model, $format );
631 $this->assertEquals( $expectedFormat, $rev->getContentFormat() );
634 public function provideGetContentHandler() {
635 // NOTE: we expect the help namespace to always contain wikitext
637 [ 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ],
638 [ 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ],
639 [ serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ],
644 * @dataProvider provideGetContentHandler
645 * @covers Revision::getContentHandler
647 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) {
648 $rev = $this->newTestRevision( $text, $title, $model, $format );
650 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) );
653 public function provideGetContent() {
654 // NOTE: we expect the help namespace to always contain wikitext
656 [ 'hello world', 'Help:Hello', null, null, Revision
::FOR_PUBLIC
, 'hello world' ],
658 serialize( 'hello world' ),
660 DummyContentForTesting
::MODEL_ID
,
662 Revision
::FOR_PUBLIC
,
663 serialize( 'hello world' )
666 serialize( 'hello world' ),
670 Revision
::FOR_PUBLIC
,
671 serialize( 'hello world' )
677 * @dataProvider provideGetContent
678 * @covers Revision::getContent
680 public function testGetContent( $text, $title, $model, $format,
681 $audience, $expectedSerialization
683 $rev = $this->newTestRevision( $text, $title, $model, $format );
684 $content = $rev->getContent( $audience );
687 $expectedSerialization,
688 is_null( $content ) ?
null : $content->serialize( $format )
693 * @covers Revision::getContent
695 public function testGetContent_failure() {
696 $rev = new Revision( [
697 'page' => $this->the_page
->getId(),
698 'content_model' => $this->the_page
->getContentModel(),
699 'text_id' => 123456789, // not in the test DB
702 $this->assertNull( $rev->getContent(),
703 "getContent() should return null if the revision's text blob could not be loaded." );
705 // NOTE: check this twice, once for lazy initialization, and once with the cached value.
706 $this->assertNull( $rev->getContent(),
707 "getContent() should return null if the revision's text blob could not be loaded." );
710 public function provideGetSize() {
712 [ "hello world.", CONTENT_MODEL_WIKITEXT
, 12 ],
713 [ serialize( "hello world." ), DummyContentForTesting
::MODEL_ID
, 12 ],
718 * @covers Revision::getSize
719 * @dataProvider provideGetSize
721 public function testGetSize( $text, $model, $expected_size ) {
722 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model );
723 $this->assertEquals( $expected_size, $rev->getSize() );
726 public function provideGetSha1() {
728 [ "hello world.", CONTENT_MODEL_WIKITEXT
, Revision
::base36Sha1( "hello world." ) ],
730 serialize( "hello world." ),
731 DummyContentForTesting
::MODEL_ID
,
732 Revision
::base36Sha1( serialize( "hello world." ) )
738 * @covers Revision::getSha1
739 * @dataProvider provideGetSha1
741 public function testGetSha1( $text, $model, $expected_hash ) {
742 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model );
743 $this->assertEquals( $expected_hash, $rev->getSha1() );
747 * Tests whether $rev->getContent() returns a clone when needed.
749 * @covers Revision::getContent
751 public function testGetContentClone() {
752 $content = new RevisionTestModifyableContent( "foo" );
758 'title' => Title
::newFromText( "testGetContentClone_dummy" ),
760 'content' => $content,
761 'length' => $content->getSize(),
762 'comment' => "testing",
763 'minor_edit' => false,
767 /** @var RevisionTestModifyableContent $content */
768 $content = $rev->getContent( Revision
::RAW
);
769 $content->setText( "bar" );
771 /** @var RevisionTestModifyableContent $content2 */
772 $content2 = $rev->getContent( Revision
::RAW
);
773 // content is mutable, expect clone
774 $this->assertNotSame( $content, $content2, "expected a clone" );
775 // clone should contain the original text
776 $this->assertEquals( "foo", $content2->getText() );
778 $content2->setText( "bla bla" );
779 // clones should be independent
780 $this->assertEquals( "bar", $content->getText() );
784 * Tests whether $rev->getContent() returns the same object repeatedly if appropriate.
785 * @covers Revision::getContent
787 public function testGetContentUncloned() {
788 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT
);
789 $content = $rev->getContent( Revision
::RAW
);
790 $content2 = $rev->getContent( Revision
::RAW
);
792 // for immutable content like wikitext, this should be the same object
793 $this->assertSame( $content, $content2 );