Debian Codegraph @

tests/phpunit/unit/includes/Revision/SlotRecordTest.php

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