MutableRevisionRecordTest.php (mediawiki-1.31.1) | : | MutableRevisionRecordTest.php (mediawiki-1.32.0) | ||
---|---|---|---|---|
<?php | <?php | |||
namespace MediaWiki\Tests\Storage; | namespace MediaWiki\Tests\Revision; | |||
use CommentStoreComment; | use CommentStoreComment; | |||
use InvalidArgumentException; | use InvalidArgumentException; | |||
use MediaWiki\Storage\MutableRevisionRecord; | use MediaWiki\Revision\MutableRevisionRecord; | |||
use MediaWiki\Storage\RevisionAccessException; | use MediaWiki\Revision\MutableRevisionSlots; | |||
use MediaWiki\Storage\RevisionRecord; | use MediaWiki\Revision\RevisionAccessException; | |||
use MediaWiki\Storage\SlotRecord; | use MediaWiki\Revision\RevisionRecord; | |||
use MediaWiki\Revision\SlotRecord; | ||||
use MediaWiki\Storage\RevisionSlotsUpdate; | ||||
use MediaWiki\User\UserIdentityValue; | use MediaWiki\User\UserIdentityValue; | |||
use MediaWikiTestCase; | use MediaWikiTestCase; | |||
use TextContent; | use TextContent; | |||
use Title; | use Title; | |||
use User; | ||||
use WikitextContent; | use WikitextContent; | |||
/** | /** | |||
* @covers \MediaWiki\Storage\MutableRevisionRecord | * @covers \MediaWiki\Revision\MutableRevisionRecord | |||
* @covers \MediaWiki\Storage\RevisionRecord | * @covers \MediaWiki\Revision\RevisionRecord | |||
*/ | */ | |||
class MutableRevisionRecordTest extends MediaWikiTestCase { | class MutableRevisionRecordTest extends MediaWikiTestCase { | |||
use RevisionRecordTests; | use RevisionRecordTests; | |||
/** | /** | |||
* @param array $rowOverrides | * @param array $rowOverrides | |||
* | * | |||
* @return MutableRevisionRecord | * @return MutableRevisionRecord | |||
*/ | */ | |||
skipping to change at line 51 | skipping to change at line 54 | |||
} | } | |||
if ( isset( $rowOverrides['rev_id'] ) ) { | if ( isset( $rowOverrides['rev_id'] ) ) { | |||
$record->setId( $rowOverrides['rev_id'] ); | $record->setId( $rowOverrides['rev_id'] ); | |||
} | } | |||
if ( isset( $rowOverrides['rev_page'] ) ) { | if ( isset( $rowOverrides['rev_page'] ) ) { | |||
$record->setPageId( $rowOverrides['rev_page'] ); | $record->setPageId( $rowOverrides['rev_page'] ); | |||
} | } | |||
$record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) ); | $record->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ip sum' ) ); | |||
$record->setComment( $comment ); | $record->setComment( $comment ); | |||
$record->setUser( $user ); | $record->setUser( $user ); | |||
$record->setTimestamp( '20101010000000' ); | ||||
return $record; | return $record; | |||
} | } | |||
public function provideConstructor() { | public function provideConstructor() { | |||
$title = Title::newFromText( 'Dummy' ); | $title = Title::newFromText( 'Dummy' ); | |||
$title->resetArticleID( 17 ); | $title->resetArticleID( 17 ); | |||
yield [ | yield [ | |||
$title, | $title, | |||
skipping to change at line 140 | skipping to change at line 144 | |||
public function testSetGetParentId() { | public function testSetGetParentId() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->assertNull( $record->getParentId() ); | $this->assertNull( $record->getParentId() ); | |||
$record->setParentId( 100 ); | $record->setParentId( 100 ); | |||
$this->assertSame( 100, $record->getParentId() ); | $this->assertSame( 100, $record->getParentId() ); | |||
} | } | |||
public function testGetMainContentWhenEmpty() { | public function testGetMainContentWhenEmpty() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->setExpectedException( RevisionAccessException::class ); | $this->setExpectedException( RevisionAccessException::class ); | |||
$this->assertNull( $record->getContent( 'main' ) ); | $this->assertNull( $record->getContent( SlotRecord::MAIN ) ); | |||
} | } | |||
public function testSetGetMainContent() { | public function testSetGetMainContent() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$content = new WikitextContent( 'Badger' ); | $content = new WikitextContent( 'Badger' ); | |||
$record->setContent( 'main', $content ); | $record->setContent( SlotRecord::MAIN, $content ); | |||
$this->assertSame( $content, $record->getContent( 'main' ) ); | $this->assertSame( $content, $record->getContent( SlotRecord::MAI | |||
N ) ); | ||||
} | } | |||
public function testGetSlotWhenEmpty() { | public function testGetSlotWhenEmpty() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->assertFalse( $record->hasSlot( 'main' ) ); | $this->assertFalse( $record->hasSlot( SlotRecord::MAIN ) ); | |||
$this->setExpectedException( RevisionAccessException::class ); | $this->setExpectedException( RevisionAccessException::class ); | |||
$record->getSlot( 'main' ); | $record->getSlot( SlotRecord::MAIN ); | |||
} | } | |||
public function testSetGetSlot() { | public function testSetGetSlot() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$slot = SlotRecord::newUnsaved( | $slot = SlotRecord::newUnsaved( | |||
'main', | SlotRecord::MAIN, | |||
new WikitextContent( 'x' ) | new WikitextContent( 'x' ) | |||
); | ); | |||
$record->setSlot( $slot ); | $record->setSlot( $slot ); | |||
$this->assertTrue( $record->hasSlot( 'main' ) ); | $this->assertTrue( $record->hasSlot( SlotRecord::MAIN ) ); | |||
$this->assertSame( $slot, $record->getSlot( 'main' ) ); | $this->assertSame( $slot, $record->getSlot( SlotRecord::MAIN ) ); | |||
} | } | |||
public function testSetGetMinor() { | public function testSetGetMinor() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->assertFalse( $record->isMinor() ); | $this->assertFalse( $record->isMinor() ); | |||
$record->setMinorEdit( true ); | $record->setMinorEdit( true ); | |||
$this->assertSame( true, $record->isMinor() ); | $this->assertSame( true, $record->isMinor() ); | |||
} | } | |||
public function testSetGetTimestamp() { | public function testSetGetTimestamp() { | |||
skipping to change at line 197 | skipping to change at line 201 | |||
$this->assertSame( RevisionRecord::DELETED_USER, $record->getVisi bility() ); | $this->assertSame( RevisionRecord::DELETED_USER, $record->getVisi bility() ); | |||
} | } | |||
public function testSetGetSha1() { | public function testSetGetSha1() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->ge tSha1() ); | $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->ge tSha1() ); | |||
$record->setSha1( 'someHash' ); | $record->setSha1( 'someHash' ); | |||
$this->assertSame( 'someHash', $record->getSha1() ); | $this->assertSame( 'someHash', $record->getSha1() ); | |||
} | } | |||
public function testGetSlots() { | ||||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) | ||||
); | ||||
$this->assertInstanceOf( MutableRevisionSlots::class, $record->ge | ||||
tSlots() ); | ||||
} | ||||
public function testSetGetSize() { | public function testSetGetSize() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$this->assertSame( 0, $record->getSize() ); | $this->assertSame( 0, $record->getSize() ); | |||
$record->setSize( 775 ); | $record->setSize( 775 ); | |||
$this->assertSame( 775, $record->getSize() ); | $this->assertSame( 775, $record->getSize() ); | |||
} | } | |||
public function testSetGetComment() { | public function testSetGetComment() { | |||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); | |||
$comment = new CommentStoreComment( 1, 'foo' ); | $comment = new CommentStoreComment( 1, 'foo' ); | |||
$this->assertNull( $record->getComment() ); | $this->assertNull( $record->getComment() ); | |||
$record->setComment( $comment ); | $record->setComment( $comment ); | |||
$this->assertSame( $comment, $record->getComment() ); | $this->assertSame( $comment, $record->getComment() ); | |||
} | } | |||
public function testSimpleGetOriginalAndInheritedSlots() { | ||||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) | ||||
); | ||||
$mainSlot = new SlotRecord( | ||||
(object)[ | ||||
'slot_id' => 1, | ||||
'slot_revision_id' => null, // unsaved | ||||
'slot_content_id' => 1, | ||||
'content_address' => null, // touched | ||||
'model_name' => 'x', | ||||
'role_name' => 'main', | ||||
'slot_origin' => null // touched | ||||
], | ||||
new WikitextContent( 'main' ) | ||||
); | ||||
$auxSlot = new SlotRecord( | ||||
(object)[ | ||||
'slot_id' => 2, | ||||
'slot_revision_id' => null, // unsaved | ||||
'slot_content_id' => 1, | ||||
'content_address' => 'foo', // inherited | ||||
'model_name' => 'x', | ||||
'role_name' => 'aux', | ||||
'slot_origin' => 1 // inherited | ||||
], | ||||
new WikitextContent( 'aux' ) | ||||
); | ||||
$record->setSlot( $mainSlot ); | ||||
$record->setSlot( $auxSlot ); | ||||
$this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSl | ||||
otRoles() ); | ||||
$this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlo | ||||
t( SlotRecord::MAIN ) ); | ||||
$this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSl | ||||
otRoles() ); | ||||
$this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlo | ||||
t( 'aux' ) ); | ||||
} | ||||
public function testSimpleremoveSlot() { | ||||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) | ||||
); | ||||
$a = new WikitextContent( 'a' ); | ||||
$b = new WikitextContent( 'b' ); | ||||
$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord | ||||
::newUnsaved( 'a', $a ) ) ); | ||||
$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord | ||||
::newUnsaved( 'b', $b ) ) ); | ||||
$record->removeSlot( 'b' ); | ||||
$this->assertTrue( $record->hasSlot( 'a' ) ); | ||||
$this->assertFalse( $record->hasSlot( 'b' ) ); | ||||
} | ||||
public function testApplyUpdate() { | ||||
$update = new RevisionSlotsUpdate(); | ||||
$a = new WikitextContent( 'a' ); | ||||
$b = new WikitextContent( 'b' ); | ||||
$c = new WikitextContent( 'c' ); | ||||
$x = new WikitextContent( 'x' ); | ||||
$update->modifyContent( 'b', $x ); | ||||
$update->modifyContent( 'c', $x ); | ||||
$update->removeSlot( 'c' ); | ||||
$update->removeSlot( 'd' ); | ||||
$record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) | ||||
); | ||||
$record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord | ||||
::newUnsaved( 'a', $a ) ) ); | ||||
$record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord | ||||
::newUnsaved( 'b', $b ) ) ); | ||||
$record->inheritSlot( SlotRecord::newSaved( 7, 5, 'c', SlotRecord | ||||
::newUnsaved( 'c', $c ) ) ); | ||||
$record->applyUpdate( $update ); | ||||
$this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlo | ||||
ts()->getSlots() ) ); | ||||
$this->assertEquals( $a, $record->getSlot( 'a' )->getContent() ); | ||||
$this->assertEquals( $x, $record->getSlot( 'b' )->getContent() ); | ||||
$this->assertFalse( $record->hasSlot( 'c' ) ); | ||||
} | ||||
public function provideNotReadyForInsertion() { | ||||
/** @var Title $title */ | ||||
$title = $this->getMock( Title::class ); | ||||
/** @var User $user */ | ||||
$user = $this->getMock( User::class ); | ||||
/** @var CommentStoreComment $comment */ | ||||
$comment = $this->getMockBuilder( CommentStoreComment::class ) | ||||
->disableOriginalConstructor() | ||||
->getMock(); | ||||
$content = new TextContent( 'Test' ); | ||||
$rev = new MutableRevisionRecord( $title ); | ||||
yield 'empty' => [ $rev ]; | ||||
$rev = new MutableRevisionRecord( $title ); | ||||
$rev->setContent( SlotRecord::MAIN, $content ); | ||||
$rev->setUser( $user ); | ||||
$rev->setComment( $comment ); | ||||
yield 'no timestamp' => [ $rev ]; | ||||
$rev = new MutableRevisionRecord( $title ); | ||||
$rev->setUser( $user ); | ||||
$rev->setComment( $comment ); | ||||
$rev->setTimestamp( '20101010000000' ); | ||||
yield 'no content' => [ $rev ]; | ||||
$rev = new MutableRevisionRecord( $title ); | ||||
$rev->setContent( SlotRecord::MAIN, $content ); | ||||
$rev->setComment( $comment ); | ||||
$rev->setTimestamp( '20101010000000' ); | ||||
yield 'no user' => [ $rev ]; | ||||
$rev = new MutableRevisionRecord( $title ); | ||||
$rev->setUser( $user ); | ||||
$rev->setContent( SlotRecord::MAIN, $content ); | ||||
$rev->setTimestamp( '20101010000000' ); | ||||
yield 'no comment' => [ $rev ]; | ||||
} | ||||
/** | ||||
* @dataProvider provideNotReadyForInsertion | ||||
*/ | ||||
public function testNotReadyForInsertion( $rev ) { | ||||
$this->assertFalse( $rev->isReadyForInsertion() ); | ||||
} | ||||
} | } | |||
End of changes. 14 change blocks. | ||||
16 lines changed or deleted | 167 lines changed or added |