3 namespace MediaWiki\Tests\Storage
;
5 use InvalidArgumentException
;
7 use MediaWiki\Storage\IncompleteRevisionException
;
8 use MediaWiki\Storage\SlotRecord
;
9 use MediaWiki\Storage\SuppressedDataException
;
10 use MediaWikiTestCase
;
14 * @covers \MediaWiki\Storage\SlotRecord
16 class SlotRecordTest
extends MediaWikiTestCase
{
18 private function makeRow( $data = [] ) {
21 'slot_content_id' => 33,
22 'content_size' => '5',
23 'content_sha1' => 'someHash',
24 'content_address' => 'tt:456',
25 'model_name' => CONTENT_MODEL_WIKITEXT
,
26 'format_name' => CONTENT_FORMAT_WIKITEXT
,
27 'slot_revision_id' => '2',
29 'role_name' => 'myRole',
34 public function testCompleteConstruction() {
35 $row = $this->makeRow();
36 $record = new SlotRecord( $row, new WikitextContent( 'A' ) );
38 $this->assertTrue( $record->hasAddress() );
39 $this->assertTrue( $record->hasRevision() );
40 $this->assertTrue( $record->isInherited() );
41 $this->assertSame( 'A', $record->getContent()->getNativeData() );
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',
64 $content = function () {
65 return new WikitextContent( 'A' );
68 $record = new SlotRecord( $row, $content );
70 $this->assertTrue( $record->hasAddress() );
71 $this->assertTrue( $record->hasRevision() );
72 $this->assertFalse( $record->isInherited() );
73 $this->assertSame( 'A', $record->getContent()->getNativeData() );
74 $this->assertSame( 1, $record->getSize() );
75 $this->assertNotNull( $record->getSha1() );
76 $this->assertSame( CONTENT_MODEL_WIKITEXT
, $record->getModel() );
77 $this->assertSame( 2, $record->getRevision() );
78 $this->assertSame( 2, $record->getRevision() );
79 $this->assertSame( 'tt:456', $record->getAddress() );
80 $this->assertSame( 33, $record->getContentId() );
81 $this->assertSame( CONTENT_FORMAT_WIKITEXT
, $record->getFormat() );
82 $this->assertSame( 'myRole', $record->getRole() );
85 public function testNewUnsaved() {
86 $record = SlotRecord
::newUnsaved( 'myRole', new WikitextContent( 'A' ) );
88 $this->assertFalse( $record->hasAddress() );
89 $this->assertFalse( $record->hasRevision() );
90 $this->assertFalse( $record->isInherited() );
91 $this->assertSame( 'A', $record->getContent()->getNativeData() );
92 $this->assertSame( 1, $record->getSize() );
93 $this->assertNotNull( $record->getSha1() );
94 $this->assertSame( CONTENT_MODEL_WIKITEXT
, $record->getModel() );
95 $this->assertSame( 'myRole', $record->getRole() );
98 public function provideInvalidConstruction() {
99 yield
'both null' => [ null, null ];
100 yield
'null row' => [ null, new WikitextContent( 'A' ) ];
101 yield
'array row' => [ [], new WikitextContent( 'A' ) ];
102 yield
'empty row' => [ (object)[], new WikitextContent( 'A' ) ];
103 yield
'null content' => [ (object)[], null ];
107 * @dataProvider provideInvalidConstruction
109 public function testInvalidConstruction( $row, $content ) {
110 $this->setExpectedException( InvalidArgumentException
::class );
111 new SlotRecord( $row, $content );
114 public function testGetContentId_fails() {
115 $record = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
116 $this->setExpectedException( IncompleteRevisionException
::class );
118 $record->getContentId();
121 public function testGetAddress_fails() {
122 $record = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
123 $this->setExpectedException( IncompleteRevisionException
::class );
125 $record->getAddress();
128 public function provideIncomplete() {
129 $unsaved = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
130 yield
'unsaved' => [ $unsaved ];
132 $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
133 $inherited = SlotRecord
::newInherited( $parent );
134 yield
'inherited' => [ $inherited ];
138 * @dataProvider provideIncomplete
140 public function testGetRevision_fails( SlotRecord
$record ) {
141 $record = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
142 $this->setExpectedException( IncompleteRevisionException
::class );
144 $record->getRevision();
148 * @dataProvider provideIncomplete
150 public function testGetOrigin_fails( SlotRecord
$record ) {
151 $record = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
152 $this->setExpectedException( IncompleteRevisionException
::class );
154 $record->getOrigin();
157 public function provideHashStability() {
158 yield
[ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ];
159 yield
[ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ];
163 * @dataProvider provideHashStability
165 public function testHashStability( $text, $hash ) {
166 // Changing the output of the hash function will break things horribly!
168 $this->assertSame( $hash, SlotRecord
::base36Sha1( $text ) );
170 $record = SlotRecord
::newUnsaved( 'main', new WikitextContent( $text ) );
171 $this->assertSame( $hash, $record->getSha1() );
174 public function testNewWithSuppressedContent() {
175 $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
176 $output = SlotRecord
::newWithSuppressedContent( $input );
178 $this->setExpectedException( SuppressedDataException
::class );
179 $output->getContent();
182 public function testNewInherited() {
183 $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] );
184 $parent = new SlotRecord( $row, new WikitextContent( 'A' ) );
186 // This would happen while doing an edit, before saving revision meta-data.
187 $inherited = SlotRecord
::newInherited( $parent );
189 $this->assertSame( $parent->getContentId(), $inherited->getContentId() );
190 $this->assertSame( $parent->getAddress(), $inherited->getAddress() );
191 $this->assertSame( $parent->getContent(), $inherited->getContent() );
192 $this->assertTrue( $inherited->isInherited() );
193 $this->assertFalse( $inherited->hasRevision() );
195 // make sure we didn't mess with the internal state of $parent
196 $this->assertFalse( $parent->isInherited() );
197 $this->assertSame( 7, $parent->getRevision() );
199 // This would happen while doing an edit, after saving the revision meta-data
200 // and content meta-data.
201 $saved = SlotRecord
::newSaved(
203 $inherited->getContentId(),
204 $inherited->getAddress(),
207 $this->assertSame( $parent->getContentId(), $saved->getContentId() );
208 $this->assertSame( $parent->getAddress(), $saved->getAddress() );
209 $this->assertSame( $parent->getContent(), $saved->getContent() );
210 $this->assertTrue( $saved->isInherited() );
211 $this->assertTrue( $saved->hasRevision() );
212 $this->assertSame( 10, $saved->getRevision() );
214 // make sure we didn't mess with the internal state of $parent or $inherited
215 $this->assertSame( 7, $parent->getRevision() );
216 $this->assertFalse( $inherited->hasRevision() );
219 public function testNewSaved() {
220 // This would happen while doing an edit, before saving revision meta-data.
221 $unsaved = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
223 // This would happen while doing an edit, after saving the revision meta-data
224 // and content meta-data.
225 $saved = SlotRecord
::newSaved( 10, 20, 'theNewAddress', $unsaved );
226 $this->assertFalse( $saved->isInherited() );
227 $this->assertTrue( $saved->hasRevision() );
228 $this->assertTrue( $saved->hasAddress() );
229 $this->assertSame( 'theNewAddress', $saved->getAddress() );
230 $this->assertSame( 20, $saved->getContentId() );
231 $this->assertSame( 'A', $saved->getContent()->getNativeData() );
232 $this->assertSame( 10, $saved->getRevision() );
233 $this->assertSame( 10, $saved->getOrigin() );
235 // make sure we didn't mess with the internal state of $unsaved
236 $this->assertFalse( $unsaved->hasAddress() );
237 $this->assertFalse( $unsaved->hasRevision() );
240 public function provideNewSaved_LogicException() {
241 $freshRow = $this->makeRow( [
243 'content_address' => 'address:1',
245 'slot_revision_id' => 1,
248 $freshSlot = new SlotRecord( $freshRow, new WikitextContent( 'A' ) );
249 yield
'mismatching address' => [ 1, 10, 'address:BAD', $freshSlot ];
250 yield
'mismatching revision' => [ 5, 10, 'address:1', $freshSlot ];
251 yield
'mismatching content ID' => [ 1, 17, 'address:1', $freshSlot ];
253 $inheritedRow = $this->makeRow( [
254 'content_id' => null,
255 'content_address' => null,
257 'slot_revision_id' => 1,
260 $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) );
261 yield
'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ];
265 * @dataProvider provideNewSaved_LogicException
267 public function testNewSaved_LogicException(
271 SlotRecord
$protoSlot
273 $this->setExpectedException( LogicException
::class );
274 SlotRecord
::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
277 public function provideNewSaved_InvalidArgumentException() {
278 $unsaved = SlotRecord
::newUnsaved( 'main', new WikitextContent( 'A' ) );
280 yield
'bad revision id' => [ 'xyzzy', 5, 'address', $unsaved ];
281 yield
'bad content id' => [ 7, 'xyzzy', 'address', $unsaved ];
282 yield
'bad content address' => [ 7, 5, 77, $unsaved ];
286 * @dataProvider provideNewSaved_InvalidArgumentException
288 public function testNewSaved_InvalidArgumentException(
292 SlotRecord
$protoSlot
294 $this->setExpectedException( InvalidArgumentException
::class );
295 SlotRecord
::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );