3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
6 use InvalidArgumentException
;
7 use MediaWiki\Storage\MutableRevisionRecord
;
8 use MediaWiki\Storage\RevisionAccessException
;
9 use MediaWiki\Storage\RevisionRecord
;
10 use MediaWiki\Storage\RevisionSlotsUpdate
;
11 use MediaWiki\Storage\SlotRecord
;
12 use MediaWiki\User\UserIdentityValue
;
13 use MediaWikiTestCase
;
19 * @covers \MediaWiki\Storage\MutableRevisionRecord
20 * @covers \MediaWiki\Storage\RevisionRecord
22 class MutableRevisionRecordTest
extends MediaWikiTestCase
{
24 use RevisionRecordTests
;
27 * @param array $rowOverrides
29 * @return MutableRevisionRecord
31 protected function newRevision( array $rowOverrides = [] ) {
32 $title = Title
::newFromText( 'Dummy' );
33 $title->resetArticleID( 17 );
35 $user = new UserIdentityValue( 11, 'Tester', 0 );
36 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
38 $record = new MutableRevisionRecord( $title );
40 if ( isset( $rowOverrides['rev_deleted'] ) ) {
41 $record->setVisibility( $rowOverrides['rev_deleted'] );
44 if ( isset( $rowOverrides['rev_id'] ) ) {
45 $record->setId( $rowOverrides['rev_id'] );
48 if ( isset( $rowOverrides['rev_page'] ) ) {
49 $record->setPageId( $rowOverrides['rev_page'] );
52 $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
53 $record->setComment( $comment );
54 $record->setUser( $user );
59 public function provideConstructor() {
60 $title = Title
::newFromText( 'Dummy' );
61 $title->resetArticleID( 17 );
70 * @dataProvider provideConstructor
75 public function testConstructorAndGetters(
79 $rec = new MutableRevisionRecord( $title, $wikiId );
81 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
82 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
85 public function provideConstructorFailure() {
86 $title = Title
::newFromText( 'Dummy' );
87 $title->resetArticleID( 17 );
89 yield
'not a wiki id' => [
96 * @dataProvider provideConstructorFailure
101 public function testConstructorFailure(
105 $this->setExpectedException( InvalidArgumentException
::class );
106 new MutableRevisionRecord( $title, $wikiId );
109 public function testSetGetId() {
110 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
111 $this->assertNull( $record->getId() );
112 $record->setId( 888 );
113 $this->assertSame( 888, $record->getId() );
116 public function testSetGetUser() {
117 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
118 $user = $this->getTestSysop()->getUser();
119 $this->assertNull( $record->getUser() );
120 $record->setUser( $user );
121 $this->assertSame( $user, $record->getUser() );
124 public function testSetGetPageId() {
125 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
126 $this->assertSame( 0, $record->getPageId() );
127 $record->setPageId( 999 );
128 $this->assertSame( 999, $record->getPageId() );
131 public function testSetGetParentId() {
132 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
133 $this->assertNull( $record->getParentId() );
134 $record->setParentId( 100 );
135 $this->assertSame( 100, $record->getParentId() );
138 public function testGetMainContentWhenEmpty() {
139 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
140 $this->setExpectedException( RevisionAccessException
::class );
141 $this->assertNull( $record->getContent( 'main' ) );
144 public function testSetGetMainContent() {
145 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
146 $content = new WikitextContent( 'Badger' );
147 $record->setContent( 'main', $content );
148 $this->assertSame( $content, $record->getContent( 'main' ) );
151 public function testGetSlotWhenEmpty() {
152 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
153 $this->assertFalse( $record->hasSlot( 'main' ) );
155 $this->setExpectedException( RevisionAccessException
::class );
156 $record->getSlot( 'main' );
159 public function testSetGetSlot() {
160 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
161 $slot = SlotRecord
::newUnsaved(
163 new WikitextContent( 'x' )
165 $record->setSlot( $slot );
166 $this->assertTrue( $record->hasSlot( 'main' ) );
167 $this->assertSame( $slot, $record->getSlot( 'main' ) );
170 public function testSetGetMinor() {
171 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
172 $this->assertFalse( $record->isMinor() );
173 $record->setMinorEdit( true );
174 $this->assertSame( true, $record->isMinor() );
177 public function testSetGetTimestamp() {
178 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
179 $this->assertNull( $record->getTimestamp() );
180 $record->setTimestamp( '20180101010101' );
181 $this->assertSame( '20180101010101', $record->getTimestamp() );
184 public function testSetGetVisibility() {
185 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
186 $this->assertSame( 0, $record->getVisibility() );
187 $record->setVisibility( RevisionRecord
::DELETED_USER
);
188 $this->assertSame( RevisionRecord
::DELETED_USER
, $record->getVisibility() );
191 public function testSetGetSha1() {
192 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
193 $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() );
194 $record->setSha1( 'someHash' );
195 $this->assertSame( 'someHash', $record->getSha1() );
198 public function testSetGetSize() {
199 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
200 $this->assertSame( 0, $record->getSize() );
201 $record->setSize( 775 );
202 $this->assertSame( 775, $record->getSize() );
205 public function testSetGetComment() {
206 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
207 $comment = new CommentStoreComment( 1, 'foo' );
208 $this->assertNull( $record->getComment() );
209 $record->setComment( $comment );
210 $this->assertSame( $comment, $record->getComment() );
213 public function testSimpleGetOriginalAndInheritedSlots() {
214 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
215 $mainSlot = new SlotRecord(
218 'slot_revision_id' => null, // unsaved
219 'slot_content_id' => 1,
220 'content_address' => null, // touched
222 'role_name' => 'main',
223 'slot_origin' => null // touched
225 new WikitextContent( 'main' )
227 $auxSlot = new SlotRecord(
230 'slot_revision_id' => null, // unsaved
231 'slot_content_id' => 1,
232 'content_address' => 'foo', // inherited
234 'role_name' => 'aux',
235 'slot_origin' => 1 // inherited
237 new WikitextContent( 'aux' )
240 $record->setSlot( $mainSlot );
241 $record->setSlot( $auxSlot );
243 $this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSlotRoles() );
244 $this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( 'main' ) );
246 $this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
247 $this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
250 public function testSimpleremoveSlot() {
251 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
253 $a = new WikitextContent( 'a' );
254 $b = new WikitextContent( 'b' );
256 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
257 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
259 $record->removeSlot( 'b' );
261 $this->assertTrue( $record->hasSlot( 'a' ) );
262 $this->assertFalse( $record->hasSlot( 'b' ) );
265 public function testApplyUpdate() {
266 $update = new RevisionSlotsUpdate();
268 $a = new WikitextContent( 'a' );
269 $b = new WikitextContent( 'b' );
270 $c = new WikitextContent( 'c' );
271 $x = new WikitextContent( 'x' );
273 $update->modifyContent( 'b', $x );
274 $update->modifyContent( 'c', $x );
275 $update->removeSlot( 'c' );
276 $update->removeSlot( 'd' );
278 $record = new MutableRevisionRecord( Title
::newFromText( 'Foo' ) );
279 $record->inheritSlot( SlotRecord
::newSaved( 7, 3, 'a', SlotRecord
::newUnsaved( 'a', $a ) ) );
280 $record->inheritSlot( SlotRecord
::newSaved( 7, 4, 'b', SlotRecord
::newUnsaved( 'b', $b ) ) );
281 $record->inheritSlot( SlotRecord
::newSaved( 7, 5, 'c', SlotRecord
::newUnsaved( 'c', $c ) ) );
283 $record->applyUpdate( $update );
285 $this->assertEquals( [ 'b' ], array_keys( $record->getOriginalSlots()->getSlots() ) );
286 $this->assertEquals( $a, $record->getSlot( 'a' )->getContent() );
287 $this->assertEquals( $x, $record->getSlot( 'b' )->getContent() );
288 $this->assertFalse( $record->hasSlot( 'c' ) );