3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
6 use InvalidArgumentException
;
7 use MediaWiki\Storage\MutableRevisionRecord
;
8 use MediaWiki\Storage\MutableRevisionSlots
;
9 use MediaWiki\Storage\RevisionAccessException
;
10 use MediaWiki\Storage\RevisionRecord
;
11 use MediaWiki\Storage\RevisionSlotsUpdate
;
12 use MediaWiki\Storage\SlotRecord
;
13 use MediaWiki\User\UserIdentityValue
;
14 use MediaWikiTestCase
;
20 * @covers \MediaWiki\Storage\MutableRevisionRecord
21 * @covers \MediaWiki\Storage\RevisionRecord
23 class MutableRevisionRecordTest
extends MediaWikiTestCase
{
25 use RevisionRecordTests
;
28 * @param array $rowOverrides
30 * @return MutableRevisionRecord
32 protected function newRevision( array $rowOverrides = [] ) {
33 $title = Title
::newFromText( 'Dummy' );
34 $title->resetArticleID( 17 );
36 $user = new UserIdentityValue( 11, 'Tester', 0 );
37 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
39 $record = new MutableRevisionRecord( $title );
41 if ( isset( $rowOverrides['rev_deleted'] ) ) {
42 $record->setVisibility( $rowOverrides['rev_deleted'] );
45 if ( isset( $rowOverrides['rev_id'] ) ) {
46 $record->setId( $rowOverrides['rev_id'] );
49 if ( isset( $rowOverrides['rev_page'] ) ) {
50 $record->setPageId( $rowOverrides['rev_page'] );
53 $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
54 $record->setComment( $comment );
55 $record->setUser( $user );
60 public function provideConstructor() {
61 $title = Title
::newFromText( 'Dummy' );
62 $title->resetArticleID( 17 );
71 * @dataProvider provideConstructor
76 public function testConstructorAndGetters(
80 $rec = new MutableRevisionRecord( $title, $wikiId );
82 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
83 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
86 public function provideConstructorFailure() {
87 $title = Title
::newFromText( 'Dummy' );
88 $title->resetArticleID( 17 );
90 yield
'not a wiki id' => [
97 * @dataProvider provideConstructorFailure
100 * @param bool $wikiId
102 public function testConstructorFailure(
106 $this->setExpectedException( InvalidArgumentException
::class );
107 new MutableRevisionRecord( $title, $wikiId );
110 public function testSetGetId() {
111 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
112 $this->assertNull( $record->getId() );
113 $record->setId( 888 );
114 $this->assertSame( 888, $record->getId() );
117 public function testSetGetUser() {
118 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
119 $user = $this->getTestSysop()->getUser();
120 $this->assertNull( $record->getUser() );
121 $record->setUser( $user );
122 $this->assertSame( $user, $record->getUser() );
125 public function testSetGetPageId() {
126 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
127 $this->assertSame( 0, $record->getPageId() );
128 $record->setPageId( 999 );
129 $this->assertSame( 999, $record->getPageId() );
132 public function testSetGetParentId() {
133 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
134 $this->assertNull( $record->getParentId() );
135 $record->setParentId( 100 );
136 $this->assertSame( 100, $record->getParentId() );
139 public function testGetMainContentWhenEmpty() {
140 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
141 $this->setExpectedException( RevisionAccessException
::class );
142 $this->assertNull( $record->getContent( 'main' ) );
145 public function testSetGetMainContent() {
146 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
147 $content = new WikitextContent( 'Badger' );
148 $record->setContent( 'main', $content );
149 $this->assertSame( $content, $record->getContent( 'main' ) );
152 public function testGetSlotWhenEmpty() {
153 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
154 $this->assertFalse( $record->hasSlot( 'main' ) );
156 $this->setExpectedException( RevisionAccessException
::class );
157 $record->getSlot( 'main' );
160 public function testSetGetSlot() {
161 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
162 $slot = SlotRecord
::newUnsaved(
164 new WikitextContent( 'x' )
166 $record->setSlot( $slot );
167 $this->assertTrue( $record->hasSlot( 'main' ) );
168 $this->assertSame( $slot, $record->getSlot( 'main' ) );
171 public function testSetGetMinor() {
172 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
173 $this->assertFalse( $record->isMinor() );
174 $record->setMinorEdit( true );
175 $this->assertSame( true, $record->isMinor() );
178 public function testSetGetTimestamp() {
179 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
180 $this->assertNull( $record->getTimestamp() );
181 $record->setTimestamp( '20180101010101' );
182 $this->assertSame( '20180101010101', $record->getTimestamp() );
185 public function testSetGetVisibility() {
186 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
187 $this->assertSame( 0, $record->getVisibility() );
188 $record->setVisibility( RevisionRecord
::DELETED_USER
);
189 $this->assertSame( RevisionRecord
::DELETED_USER
, $record->getVisibility() );
192 public function testSetGetSha1() {
193 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
194 $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
195 $record->setSha1( 'someHash' );
196 $this->assertSame( 'someHash', $record->getSha1() );
199 public function testGetSlots() {
200 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
201 $this->assertInstanceOf( MutableRevisionSlots
::class, $record->getSlots() );
204 public function testSetGetSize() {
205 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
206 $this->assertSame( 0, $record->getSize() );
207 $record->setSize( 775 );
208 $this->assertSame( 775, $record->getSize() );
211 public function testSetGetComment() {
212 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
213 $comment = new CommentStoreComment( 1, 'foo' );
214 $this->assertNull( $record->getComment() );
215 $record->setComment( $comment );
216 $this->assertSame( $comment, $record->getComment() );
219 public function testSimpleGetOriginalAndInheritedSlots() {
220 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
221 $mainSlot = new SlotRecord(
224 'slot_revision_id' => null, // unsaved
225 'slot_content_id' => 1,
226 'content_address' => null, // touched
228 'role_name' => 'main',
229 'slot_origin' => null // touched
231 new WikitextContent( 'main' )
233 $auxSlot = new SlotRecord(
236 'slot_revision_id' => null, // unsaved
237 'slot_content_id' => 1,
238 'content_address' => 'foo', // inherited
240 'role_name' => 'aux',
241 'slot_origin' => 1 // inherited
243 new WikitextContent( 'aux' )
246 $record->setSlot( $mainSlot );
247 $record->setSlot( $auxSlot );
249 $this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSlotRoles() );
250 $this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( 'main' ) );
252 $this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
253 $this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
256 public function testSimpleremoveSlot() {
257 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
259 $a = new WikitextContent( 'a' );
260 $b = new WikitextContent( 'b' );
262 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
263 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
265 $record->removeSlot( 'b' );
267 $this->assertTrue( $record->hasSlot( 'a' ) );
268 $this->assertFalse( $record->hasSlot( 'b' ) );
271 public function testApplyUpdate() {
272 $update = new RevisionSlotsUpdate();
274 $a = new WikitextContent( 'a' );
275 $b = new WikitextContent( 'b' );
276 $c = new WikitextContent( 'c' );
277 $x = new WikitextContent( 'x' );
279 $update->modifyContent( 'b', $x );
280 $update->modifyContent( 'c', $x );
281 $update->removeSlot( 'c' );
282 $update->removeSlot( 'd' );
284 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
285 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
286 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
287 $record->inheritSlot( SlotRecord
::newSaved( 7, 5, 'c', SlotRecord
::newUnsaved( 'c', $c ) ) );
289 $record->applyUpdate( $update );
291 $this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) );
292 $this->assertEquals( $a, $record->getSlot( 'a' )->getContent() );
293 $this->assertEquals( $x, $record->getSlot( 'b' )->getContent() );
294 $this->assertFalse( $record->hasSlot( 'c' ) );