Separate MediaWiki unit and integration tests
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / Revision / SlotRecordTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
4
5 use InvalidArgumentException;
6 use LogicException;
7 use MediaWiki\Revision\IncompleteRevisionException;
8 use MediaWiki\Revision\SlotRecord;
9 use MediaWiki\Revision\SuppressedDataException;
10 use WikitextContent;
11
12 /**
13 * @covers \MediaWiki\Revision\SlotRecord
14 */
15 class SlotRecordTest extends \MediaWikiUnitTestCase {
16
17 private function makeRow( $data = [] ) {
18 $data = $data + [
19 'slot_id' => 1234,
20 'slot_content_id' => 33,
21 'content_size' => '5',
22 'content_sha1' => 'someHash',
23 'content_address' => 'tt:456',
24 'model_name' => CONTENT_MODEL_WIKITEXT,
25 'format_name' => CONTENT_FORMAT_WIKITEXT,
26 'slot_revision_id' => '2',
27 'slot_origin' => '1',
28 'role_name' => 'myRole',
29 ];
30 return (object)$data;
31 }
32
33 public function testCompleteConstruction() {
34 $row = $this->makeRow();
35 $record = new SlotRecord( $row, new WikitextContent( 'A' ) );
36
37 $this->assertTrue( $record->hasAddress() );
38 $this->assertTrue( $record->hasContentId() );
39 $this->assertTrue( $record->hasRevision() );
40 $this->assertTrue( $record->isInherited() );
41 $this->assertSame( 'A', $record->getContent()->getText() );
42 $this->assertSame( 5, $record->getSize() );
43 $this->assertSame( 'someHash', $record->getSha1() );
44 $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
45 $this->assertSame( 2, $record->getRevision() );
46 $this->assertSame( 1, $record->getOrigin() );
47 $this->assertSame( 'tt:456', $record->getAddress() );
48 $this->assertSame( 33, $record->getContentId() );
49 $this->assertSame( CONTENT_FORMAT_WIKITEXT, $record->getFormat() );
50 $this->assertSame( 'myRole', $record->getRole() );
51 }
52
53 public function testConstructionDeferred() {
54 $row = $this->makeRow( [
55 'content_size' => null, // to be computed
56 'content_sha1' => null, // to be computed
57 'format_name' => function () {
58 return CONTENT_FORMAT_WIKITEXT;
59 },
60 'slot_revision_id' => '2',
61 'slot_origin' => '2',
62 'slot_content_id' => function () {
63 return null;
64 },
65 ] );
66
67 $content = function () {
68 return new WikitextContent( 'A' );
69 };
70
71 $record = new SlotRecord( $row, $content );
72
73 $this->assertTrue( $record->hasAddress() );
74 $this->assertTrue( $record->hasRevision() );
75 $this->assertFalse( $record->hasContentId() );
76 $this->assertFalse( $record->isInherited() );
77 $this->assertSame( 'A', $record->getContent()->getText() );
78 $this->assertSame( 1, $record->getSize() );
79 $this->assertNotNull( $record->getSha1() );
80 $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
81 $this->assertSame( 2, $record->getRevision() );
82 $this->assertSame( 2, $record->getRevision() );
83 $this->assertSame( 'tt:456', $record->getAddress() );
84 $this->assertSame( CONTENT_FORMAT_WIKITEXT, $record->getFormat() );
85 $this->assertSame( 'myRole', $record->getRole() );
86 }
87
88 public function testNewUnsaved() {
89 $record = SlotRecord::newUnsaved( 'myRole', new WikitextContent( 'A' ) );
90
91 $this->assertFalse( $record->hasAddress() );
92 $this->assertFalse( $record->hasContentId() );
93 $this->assertFalse( $record->hasRevision() );
94 $this->assertFalse( $record->isInherited() );
95 $this->assertFalse( $record->hasOrigin() );
96 $this->assertSame( 'A', $record->getContent()->getText() );
97 $this->assertSame( 1, $record->getSize() );
98 $this->assertNotNull( $record->getSha1() );
99 $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
100 $this->assertSame( 'myRole', $record->getRole() );
101 }
102
103 public function provideInvalidConstruction() {
104 yield 'both null' => [ null, null ];
105 yield 'null row' => [ null, new WikitextContent( 'A' ) ];
106 yield 'array row' => [ [], new WikitextContent( 'A' ) ];
107 yield 'empty row' => [ (object)[], new WikitextContent( 'A' ) ];
108 yield 'null content' => [ (object)[], null ];
109 }
110
111 /**
112 * @dataProvider provideInvalidConstruction
113 */
114 public function testInvalidConstruction( $row, $content ) {
115 $this->setExpectedException( InvalidArgumentException::class );
116 new SlotRecord( $row, $content );
117 }
118
119 public function testGetContentId_fails() {
120 $record = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
121 $this->setExpectedException( IncompleteRevisionException::class );
122
123 $record->getContentId();
124 }
125
126 public function testGetAddress_fails() {
127 $record = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
128 $this->setExpectedException( IncompleteRevisionException::class );
129
130 $record->getAddress();
131 }
132
133 public function provideIncomplete() {
134 $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
135 yield 'unsaved' => [ $unsaved ];
136
137 $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
138 $inherited = SlotRecord::newInherited( $parent );
139 yield 'inherited' => [ $inherited ];
140 }
141
142 /**
143 * @dataProvider provideIncomplete
144 */
145 public function testGetRevision_fails( SlotRecord $record ) {
146 $record = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
147 $this->setExpectedException( IncompleteRevisionException::class );
148
149 $record->getRevision();
150 }
151
152 /**
153 * @dataProvider provideIncomplete
154 */
155 public function testGetOrigin_fails( SlotRecord $record ) {
156 $record = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
157 $this->setExpectedException( IncompleteRevisionException::class );
158
159 $record->getOrigin();
160 }
161
162 public function provideHashStability() {
163 yield [ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ];
164 yield [ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ];
165 }
166
167 /**
168 * @dataProvider provideHashStability
169 */
170 public function testHashStability( $text, $hash ) {
171 // Changing the output of the hash function will break things horribly!
172
173 $this->assertSame( $hash, SlotRecord::base36Sha1( $text ) );
174
175 $record = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( $text ) );
176 $this->assertSame( $hash, $record->getSha1() );
177 }
178
179 public function testNewWithSuppressedContent() {
180 $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
181 $output = SlotRecord::newWithSuppressedContent( $input );
182
183 $this->setExpectedException( SuppressedDataException::class );
184 $output->getContent();
185 }
186
187 public function testNewInherited() {
188 $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] );
189 $parent = new SlotRecord( $row, new WikitextContent( 'A' ) );
190
191 // This would happen while doing an edit, before saving revision meta-data.
192 $inherited = SlotRecord::newInherited( $parent );
193
194 $this->assertSame( $parent->getContentId(), $inherited->getContentId() );
195 $this->assertSame( $parent->getAddress(), $inherited->getAddress() );
196 $this->assertSame( $parent->getContent(), $inherited->getContent() );
197 $this->assertTrue( $inherited->isInherited() );
198 $this->assertTrue( $inherited->hasOrigin() );
199 $this->assertFalse( $inherited->hasRevision() );
200
201 // make sure we didn't mess with the internal state of $parent
202 $this->assertFalse( $parent->isInherited() );
203 $this->assertSame( 7, $parent->getRevision() );
204
205 // This would happen while doing an edit, after saving the revision meta-data
206 // and content meta-data.
207 $saved = SlotRecord::newSaved(
208 10,
209 $inherited->getContentId(),
210 $inherited->getAddress(),
211 $inherited
212 );
213 $this->assertSame( $parent->getContentId(), $saved->getContentId() );
214 $this->assertSame( $parent->getAddress(), $saved->getAddress() );
215 $this->assertSame( $parent->getContent(), $saved->getContent() );
216 $this->assertTrue( $saved->isInherited() );
217 $this->assertTrue( $saved->hasRevision() );
218 $this->assertSame( 10, $saved->getRevision() );
219
220 // make sure we didn't mess with the internal state of $parent or $inherited
221 $this->assertSame( 7, $parent->getRevision() );
222 $this->assertFalse( $inherited->hasRevision() );
223 }
224
225 public function testNewSaved() {
226 // This would happen while doing an edit, before saving revision meta-data.
227 $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
228
229 // This would happen while doing an edit, after saving the revision meta-data
230 // and content meta-data.
231 $saved = SlotRecord::newSaved( 10, 20, 'theNewAddress', $unsaved );
232 $this->assertFalse( $saved->isInherited() );
233 $this->assertTrue( $saved->hasOrigin() );
234 $this->assertTrue( $saved->hasRevision() );
235 $this->assertTrue( $saved->hasAddress() );
236 $this->assertTrue( $saved->hasContentId() );
237 $this->assertSame( 'theNewAddress', $saved->getAddress() );
238 $this->assertSame( 20, $saved->getContentId() );
239 $this->assertSame( 'A', $saved->getContent()->getText() );
240 $this->assertSame( 10, $saved->getRevision() );
241 $this->assertSame( 10, $saved->getOrigin() );
242
243 // make sure we didn't mess with the internal state of $unsaved
244 $this->assertFalse( $unsaved->hasAddress() );
245 $this->assertFalse( $unsaved->hasContentId() );
246 $this->assertFalse( $unsaved->hasRevision() );
247 }
248
249 public function provideNewSaved_LogicException() {
250 $freshRow = $this->makeRow( [
251 'content_id' => 10,
252 'content_address' => 'address:1',
253 'slot_origin' => 1,
254 'slot_revision_id' => 1,
255 ] );
256
257 $freshSlot = new SlotRecord( $freshRow, new WikitextContent( 'A' ) );
258 yield 'mismatching address' => [ 1, 10, 'address:BAD', $freshSlot ];
259 yield 'mismatching revision' => [ 5, 10, 'address:1', $freshSlot ];
260 yield 'mismatching content ID' => [ 1, 17, 'address:1', $freshSlot ];
261
262 $inheritedRow = $this->makeRow( [
263 'content_id' => null,
264 'content_address' => null,
265 'slot_origin' => 0,
266 'slot_revision_id' => 1,
267 ] );
268
269 $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) );
270 yield 'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ];
271 }
272
273 /**
274 * @dataProvider provideNewSaved_LogicException
275 */
276 public function testNewSaved_LogicException(
277 $revisionId,
278 $contentId,
279 $contentAddress,
280 SlotRecord $protoSlot
281 ) {
282 $this->setExpectedException( LogicException::class );
283 SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
284 }
285
286 public function provideNewSaved_InvalidArgumentException() {
287 $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
288
289 yield 'bad revision id' => [ 'xyzzy', 5, 'address', $unsaved ];
290 yield 'bad content id' => [ 7, 'xyzzy', 'address', $unsaved ];
291 yield 'bad content address' => [ 7, 5, 77, $unsaved ];
292 }
293
294 /**
295 * @dataProvider provideNewSaved_InvalidArgumentException
296 */
297 public function testNewSaved_InvalidArgumentException(
298 $revisionId,
299 $contentId,
300 $contentAddress,
301 SlotRecord $protoSlot
302 ) {
303 $this->setExpectedException( InvalidArgumentException::class );
304 SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
305 }
306
307 public function provideHasSameContent() {
308 $fail = function () {
309 self::fail( 'There should be no need to actually load the content.' );
310 };
311
312 $a100a1 = new SlotRecord(
313 $this->makeRow(
314 [
315 'model_name' => 'A',
316 'content_size' => 100,
317 'content_sha1' => 'hash-a',
318 'content_address' => 'xxx:a1',
319 ]
320 ),
321 $fail
322 );
323 $a100a1b = new SlotRecord(
324 $this->makeRow(
325 [
326 'model_name' => 'A',
327 'content_size' => 100,
328 'content_sha1' => 'hash-a',
329 'content_address' => 'xxx:a1',
330 ]
331 ),
332 $fail
333 );
334 $a100null = new SlotRecord(
335 $this->makeRow(
336 [
337 'model_name' => 'A',
338 'content_size' => 100,
339 'content_sha1' => 'hash-a',
340 'content_address' => null,
341 ]
342 ),
343 $fail
344 );
345 $a100a2 = new SlotRecord(
346 $this->makeRow(
347 [
348 'model_name' => 'A',
349 'content_size' => 100,
350 'content_sha1' => 'hash-a',
351 'content_address' => 'xxx:a2',
352 ]
353 ),
354 $fail
355 );
356 $b100a1 = new SlotRecord(
357 $this->makeRow(
358 [
359 'model_name' => 'B',
360 'content_size' => 100,
361 'content_sha1' => 'hash-a',
362 'content_address' => 'xxx:a1',
363 ]
364 ),
365 $fail
366 );
367 $a200a1 = new SlotRecord(
368 $this->makeRow(
369 [
370 'model_name' => 'A',
371 'content_size' => 200,
372 'content_sha1' => 'hash-a',
373 'content_address' => 'xxx:a2',
374 ]
375 ),
376 $fail
377 );
378 $a100x1 = new SlotRecord(
379 $this->makeRow(
380 [
381 'model_name' => 'A',
382 'content_size' => 100,
383 'content_sha1' => 'hash-x',
384 'content_address' => 'xxx:x1',
385 ]
386 ),
387 $fail
388 );
389
390 yield 'same instance' => [ $a100a1, $a100a1, true ];
391 yield 'no address' => [ $a100a1, $a100null, true ];
392 yield 'same address' => [ $a100a1, $a100a1b, true ];
393 yield 'different address' => [ $a100a1, $a100a2, true ];
394 yield 'different model' => [ $a100a1, $b100a1, false ];
395 yield 'different size' => [ $a100a1, $a200a1, false ];
396 yield 'different hash' => [ $a100a1, $a100x1, false ];
397 }
398
399 /**
400 * @dataProvider provideHasSameContent
401 */
402 public function testHasSameContent( SlotRecord $a, SlotRecord $b, $sameContent ) {
403 $this->assertSame( $sameContent, $a->hasSameContent( $b ) );
404 $this->assertSame( $sameContent, $b->hasSameContent( $a ) );
405 }
406
407 }