3 namespace MediaWiki\Tests\Revision
;
5 use InvalidArgumentException
;
7 use MediaWiki\Revision\IncompleteRevisionException
;
8 use MediaWiki\Revision\SlotRecord
;
9 use MediaWiki\Revision\SuppressedDataException
;
13 * @covers \MediaWiki\Revision\SlotRecord
15 class SlotRecordTest
extends \MediaWikiUnitTestCase
{
17 private function makeRow( $data = [] ) {
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',
28 'role_name' => 'myRole',
33 public function testCompleteConstruction() {
34 $row = $this->makeRow();
35 $record = new SlotRecord( $row, new WikitextContent( 'A' ) );
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() );
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
;
60 'slot_revision_id' => '2',
62 'slot_content_id' => function () {
67 $content = function () {
68 return new WikitextContent( 'A' );
71 $record = new SlotRecord( $row, $content );
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() );
88 public function testNewUnsaved() {
89 $record = SlotRecord
::newUnsaved( 'myRole', new WikitextContent( 'A' ) );
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() );
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 ];
112 * @dataProvider provideInvalidConstruction
114 public function testInvalidConstruction( $row, $content ) {
115 $this->setExpectedException( InvalidArgumentException
::class );
116 new SlotRecord( $row, $content );
119 public function testGetContentId_fails() {
120 $record = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
121 $this->setExpectedException( IncompleteRevisionException
::class );
123 $record->getContentId();
126 public function testGetAddress_fails() {
127 $record = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
128 $this->setExpectedException( IncompleteRevisionException
::class );
130 $record->getAddress();
133 public function provideIncomplete() {
134 $unsaved = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
135 yield
'unsaved' => [ $unsaved ];
137 $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
138 $inherited = SlotRecord
::newInherited( $parent );
139 yield
'inherited' => [ $inherited ];
143 * @dataProvider provideIncomplete
145 public function testGetRevision_fails( SlotRecord
$record ) {
146 $record = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
147 $this->setExpectedException( IncompleteRevisionException
::class );
149 $record->getRevision();
153 * @dataProvider provideIncomplete
155 public function testGetOrigin_fails( SlotRecord
$record ) {
156 $record = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
157 $this->setExpectedException( IncompleteRevisionException
::class );
159 $record->getOrigin();
162 public function provideHashStability() {
163 yield
[ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ];
164 yield
[ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ];
168 * @dataProvider provideHashStability
170 public function testHashStability( $text, $hash ) {
171 // Changing the output of the hash function will break things horribly!
173 $this->assertSame( $hash, SlotRecord
::base36Sha1( $text ) );
175 $record = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( $text ) );
176 $this->assertSame( $hash, $record->getSha1() );
179 public function testNewWithSuppressedContent() {
180 $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
181 $output = SlotRecord
::newWithSuppressedContent( $input );
183 $this->setExpectedException( SuppressedDataException
::class );
184 $output->getContent();
187 public function testNewInherited() {
188 $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] );
189 $parent = new SlotRecord( $row, new WikitextContent( 'A' ) );
191 // This would happen while doing an edit, before saving revision meta-data.
192 $inherited = SlotRecord
::newInherited( $parent );
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() );
201 // make sure we didn't mess with the internal state of $parent
202 $this->assertFalse( $parent->isInherited() );
203 $this->assertSame( 7, $parent->getRevision() );
205 // This would happen while doing an edit, after saving the revision meta-data
206 // and content meta-data.
207 $saved = SlotRecord
::newSaved(
209 $inherited->getContentId(),
210 $inherited->getAddress(),
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() );
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() );
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' ) );
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() );
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() );
249 public function provideNewSaved_LogicException() {
250 $freshRow = $this->makeRow( [
252 'content_address' => 'address:1',
254 'slot_revision_id' => 1,
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 ];
262 $inheritedRow = $this->makeRow( [
263 'content_id' => null,
264 'content_address' => null,
266 'slot_revision_id' => 1,
269 $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) );
270 yield
'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ];
274 * @dataProvider provideNewSaved_LogicException
276 public function testNewSaved_LogicException(
280 SlotRecord
$protoSlot
282 $this->setExpectedException( LogicException
::class );
283 SlotRecord
::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
286 public function provideNewSaved_InvalidArgumentException() {
287 $unsaved = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new WikitextContent( 'A' ) );
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 ];
295 * @dataProvider provideNewSaved_InvalidArgumentException
297 public function testNewSaved_InvalidArgumentException(
301 SlotRecord
$protoSlot
303 $this->setExpectedException( InvalidArgumentException
::class );
304 SlotRecord
::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
307 public function provideHasSameContent() {
308 $fail = function () {
309 self
::fail( 'There should be no need to actually load the content.' );
312 $a100a1 = new SlotRecord(
316 'content_size' => 100,
317 'content_sha1' => 'hash-a',
318 'content_address' => 'xxx:a1',
323 $a100a1b = new SlotRecord(
327 'content_size' => 100,
328 'content_sha1' => 'hash-a',
329 'content_address' => 'xxx:a1',
334 $a100null = new SlotRecord(
338 'content_size' => 100,
339 'content_sha1' => 'hash-a',
340 'content_address' => null,
345 $a100a2 = new SlotRecord(
349 'content_size' => 100,
350 'content_sha1' => 'hash-a',
351 'content_address' => 'xxx:a2',
356 $b100a1 = new SlotRecord(
360 'content_size' => 100,
361 'content_sha1' => 'hash-a',
362 'content_address' => 'xxx:a1',
367 $a200a1 = new SlotRecord(
371 'content_size' => 200,
372 'content_sha1' => 'hash-a',
373 'content_address' => 'xxx:a2',
378 $a100x1 = new SlotRecord(
382 'content_size' => 100,
383 'content_sha1' => 'hash-x',
384 'content_address' => 'xxx:x1',
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 ];
400 * @dataProvider provideHasSameContent
402 public function testHasSameContent( SlotRecord
$a, SlotRecord
$b, $sameContent ) {
403 $this->assertSame( $sameContent, $a->hasSameContent( $b ) );
404 $this->assertSame( $sameContent, $b->hasSameContent( $a ) );