Merge "resourceloader: Remove support for `addSource(id, url)`"
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / MutableRevisionRecordTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
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;
15 use TextContent;
16 use Title;
17 use WikitextContent;
18
19 /**
20 * @covers \MediaWiki\Storage\MutableRevisionRecord
21 * @covers \MediaWiki\Storage\RevisionRecord
22 */
23 class MutableRevisionRecordTest extends MediaWikiTestCase {
24
25 use RevisionRecordTests;
26
27 /**
28 * @param array $rowOverrides
29 *
30 * @return MutableRevisionRecord
31 */
32 protected function newRevision( array $rowOverrides = [] ) {
33 $title = Title::newFromText( 'Dummy' );
34 $title->resetArticleID( 17 );
35
36 $user = new UserIdentityValue( 11, 'Tester', 0 );
37 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
38
39 $record = new MutableRevisionRecord( $title );
40
41 if ( isset( $rowOverrides['rev_deleted'] ) ) {
42 $record->setVisibility( $rowOverrides['rev_deleted'] );
43 }
44
45 if ( isset( $rowOverrides['rev_id'] ) ) {
46 $record->setId( $rowOverrides['rev_id'] );
47 }
48
49 if ( isset( $rowOverrides['rev_page'] ) ) {
50 $record->setPageId( $rowOverrides['rev_page'] );
51 }
52
53 $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
54 $record->setComment( $comment );
55 $record->setUser( $user );
56
57 return $record;
58 }
59
60 public function provideConstructor() {
61 $title = Title::newFromText( 'Dummy' );
62 $title->resetArticleID( 17 );
63
64 yield [
65 $title,
66 'acmewiki'
67 ];
68 }
69
70 /**
71 * @dataProvider provideConstructor
72 *
73 * @param Title $title
74 * @param bool $wikiId
75 */
76 public function testConstructorAndGetters(
77 Title $title,
78 $wikiId = false
79 ) {
80 $rec = new MutableRevisionRecord( $title, $wikiId );
81
82 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
83 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
84 }
85
86 public function provideConstructorFailure() {
87 $title = Title::newFromText( 'Dummy' );
88 $title->resetArticleID( 17 );
89
90 yield 'not a wiki id' => [
91 $title,
92 null
93 ];
94 }
95
96 /**
97 * @dataProvider provideConstructorFailure
98 *
99 * @param Title $title
100 * @param bool $wikiId
101 */
102 public function testConstructorFailure(
103 Title $title,
104 $wikiId = false
105 ) {
106 $this->setExpectedException( InvalidArgumentException::class );
107 new MutableRevisionRecord( $title, $wikiId );
108 }
109
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() );
115 }
116
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() );
123 }
124
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() );
130 }
131
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() );
137 }
138
139 public function testGetMainContentWhenEmpty() {
140 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
141 $this->setExpectedException( RevisionAccessException::class );
142 $this->assertNull( $record->getContent( 'main' ) );
143 }
144
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' ) );
150 }
151
152 public function testGetSlotWhenEmpty() {
153 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
154 $this->assertFalse( $record->hasSlot( 'main' ) );
155
156 $this->setExpectedException( RevisionAccessException::class );
157 $record->getSlot( 'main' );
158 }
159
160 public function testSetGetSlot() {
161 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
162 $slot = SlotRecord::newUnsaved(
163 'main',
164 new WikitextContent( 'x' )
165 );
166 $record->setSlot( $slot );
167 $this->assertTrue( $record->hasSlot( 'main' ) );
168 $this->assertSame( $slot, $record->getSlot( 'main' ) );
169 }
170
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() );
176 }
177
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() );
183 }
184
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() );
190 }
191
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() );
197 }
198
199 public function testGetSlots() {
200 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
201 $this->assertInstanceOf( MutableRevisionSlots::class, $record->getSlots() );
202 }
203
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() );
209 }
210
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() );
217 }
218
219 public function testSimpleGetOriginalAndInheritedSlots() {
220 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
221 $mainSlot = new SlotRecord(
222 (object)[
223 'slot_id' => 1,
224 'slot_revision_id' => null, // unsaved
225 'slot_content_id' => 1,
226 'content_address' => null, // touched
227 'model_name' => 'x',
228 'role_name' => 'main',
229 'slot_origin' => null // touched
230 ],
231 new WikitextContent( 'main' )
232 );
233 $auxSlot = new SlotRecord(
234 (object)[
235 'slot_id' => 2,
236 'slot_revision_id' => null, // unsaved
237 'slot_content_id' => 1,
238 'content_address' => 'foo', // inherited
239 'model_name' => 'x',
240 'role_name' => 'aux',
241 'slot_origin' => 1 // inherited
242 ],
243 new WikitextContent( 'aux' )
244 );
245
246 $record->setSlot( $mainSlot );
247 $record->setSlot( $auxSlot );
248
249 $this->assertSame( [ 'main' ], $record->getOriginalSlots()->getSlotRoles() );
250 $this->assertSame( $mainSlot, $record->getOriginalSlots()->getSlot( 'main' ) );
251
252 $this->assertSame( [ 'aux' ], $record->getInheritedSlots()->getSlotRoles() );
253 $this->assertSame( $auxSlot, $record->getInheritedSlots()->getSlot( 'aux' ) );
254 }
255
256 public function testSimpleremoveSlot() {
257 $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) );
258
259 $a = new WikitextContent( 'a' );
260 $b = new WikitextContent( 'b' );
261
262 $record->inheritSlot( SlotRecord::newSaved( 7, 3, 'a', SlotRecord::newUnsaved( 'a', $a ) ) );
263 $record->inheritSlot( SlotRecord::newSaved( 7, 4, 'b', SlotRecord::newUnsaved( 'b', $b ) ) );
264
265 $record->removeSlot( 'b' );
266
267 $this->assertTrue( $record->hasSlot( 'a' ) );
268 $this->assertFalse( $record->hasSlot( 'b' ) );
269 }
270
271 public function testApplyUpdate() {
272 $update = new RevisionSlotsUpdate();
273
274 $a = new WikitextContent( 'a' );
275 $b = new WikitextContent( 'b' );
276 $c = new WikitextContent( 'c' );
277 $x = new WikitextContent( 'x' );
278
279 $update->modifyContent( 'b', $x );
280 $update->modifyContent( 'c', $x );
281 $update->removeSlot( 'c' );
282 $update->removeSlot( 'd' );
283
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 ) ) );
288
289 $record->applyUpdate( $update );
290
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' ) );
295 }
296
297 }