MediaWiki  1.32.5
SlotRecordTest.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
6 use LogicException;
12 
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' => CONTENT_MODEL_WIKITEXT,
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 WikitextContent( '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( CONTENT_MODEL_WIKITEXT, $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  }
53 
54  public function testConstructionDeferred() {
55  $row = $this->makeRow( [
56  'content_size' => null, // to be computed
57  'content_sha1' => null, // to be computed
58  'format_name' => function () {
60  },
61  'slot_revision_id' => '2',
62  'slot_origin' => '2',
63  'slot_content_id' => function () {
64  return null;
65  },
66  ] );
67 
68  $content = function () {
69  return new WikitextContent( 'A' );
70  };
71 
72  $record = new SlotRecord( $row, $content );
73 
74  $this->assertTrue( $record->hasAddress() );
75  $this->assertTrue( $record->hasRevision() );
76  $this->assertFalse( $record->hasContentId() );
77  $this->assertFalse( $record->isInherited() );
78  $this->assertSame( 'A', $record->getContent()->getNativeData() );
79  $this->assertSame( 1, $record->getSize() );
80  $this->assertNotNull( $record->getSha1() );
81  $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
82  $this->assertSame( 2, $record->getRevision() );
83  $this->assertSame( 2, $record->getRevision() );
84  $this->assertSame( 'tt:456', $record->getAddress() );
85  $this->assertSame( CONTENT_FORMAT_WIKITEXT, $record->getFormat() );
86  $this->assertSame( 'myRole', $record->getRole() );
87  }
88 
89  public function testNewUnsaved() {
90  $record = SlotRecord::newUnsaved( 'myRole', new WikitextContent( 'A' ) );
91 
92  $this->assertFalse( $record->hasAddress() );
93  $this->assertFalse( $record->hasContentId() );
94  $this->assertFalse( $record->hasRevision() );
95  $this->assertFalse( $record->isInherited() );
96  $this->assertFalse( $record->hasOrigin() );
97  $this->assertSame( 'A', $record->getContent()->getNativeData() );
98  $this->assertSame( 1, $record->getSize() );
99  $this->assertNotNull( $record->getSha1() );
100  $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
101  $this->assertSame( 'myRole', $record->getRole() );
102  }
103 
104  public function provideInvalidConstruction() {
105  yield 'both null' => [ null, null ];
106  yield 'null row' => [ null, new WikitextContent( 'A' ) ];
107  yield 'array row' => [ [], new WikitextContent( 'A' ) ];
108  yield 'empty row' => [ (object)[], new WikitextContent( 'A' ) ];
109  yield 'null content' => [ (object)[], null ];
110  }
111 
115  public function testInvalidConstruction( $row, $content ) {
116  $this->setExpectedException( InvalidArgumentException::class );
117  new SlotRecord( $row, $content );
118  }
119 
120  public function testGetContentId_fails() {
122  $this->setExpectedException( IncompleteRevisionException::class );
123 
124  $record->getContentId();
125  }
126 
127  public function testGetAddress_fails() {
129  $this->setExpectedException( IncompleteRevisionException::class );
130 
131  $record->getAddress();
132  }
133 
134  public function provideIncomplete() {
135  $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
136  yield 'unsaved' => [ $unsaved ];
137 
138  $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
139  $inherited = SlotRecord::newInherited( $parent );
140  yield 'inherited' => [ $inherited ];
141  }
142 
146  public function testGetRevision_fails( SlotRecord $record ) {
148  $this->setExpectedException( IncompleteRevisionException::class );
149 
150  $record->getRevision();
151  }
152 
156  public function testGetOrigin_fails( SlotRecord $record ) {
158  $this->setExpectedException( IncompleteRevisionException::class );
159 
160  $record->getOrigin();
161  }
162 
163  public function provideHashStability() {
164  yield [ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ];
165  yield [ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ];
166  }
167 
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 WikitextContent( $text ) );
177  $this->assertSame( $hash, $record->getSha1() );
178  }
179 
180  public function testNewWithSuppressedContent() {
181  $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
183 
184  $this->setExpectedException( SuppressedDataException::class );
185  $output->getContent();
186  }
187 
188  public function testNewInherited() {
189  $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] );
190  $parent = new SlotRecord( $row, new WikitextContent( 'A' ) );
191 
192  // This would happen while doing an edit, before saving revision meta-data.
193  $inherited = SlotRecord::newInherited( $parent );
194 
195  $this->assertSame( $parent->getContentId(), $inherited->getContentId() );
196  $this->assertSame( $parent->getAddress(), $inherited->getAddress() );
197  $this->assertSame( $parent->getContent(), $inherited->getContent() );
198  $this->assertTrue( $inherited->isInherited() );
199  $this->assertTrue( $inherited->hasOrigin() );
200  $this->assertFalse( $inherited->hasRevision() );
201 
202  // make sure we didn't mess with the internal state of $parent
203  $this->assertFalse( $parent->isInherited() );
204  $this->assertSame( 7, $parent->getRevision() );
205 
206  // This would happen while doing an edit, after saving the revision meta-data
207  // and content meta-data.
208  $saved = SlotRecord::newSaved(
209  10,
210  $inherited->getContentId(),
211  $inherited->getAddress(),
212  $inherited
213  );
214  $this->assertSame( $parent->getContentId(), $saved->getContentId() );
215  $this->assertSame( $parent->getAddress(), $saved->getAddress() );
216  $this->assertSame( $parent->getContent(), $saved->getContent() );
217  $this->assertTrue( $saved->isInherited() );
218  $this->assertTrue( $saved->hasRevision() );
219  $this->assertSame( 10, $saved->getRevision() );
220 
221  // make sure we didn't mess with the internal state of $parent or $inherited
222  $this->assertSame( 7, $parent->getRevision() );
223  $this->assertFalse( $inherited->hasRevision() );
224  }
225 
226  public function testNewSaved() {
227  // This would happen while doing an edit, before saving revision meta-data.
228  $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
229 
230  // This would happen while doing an edit, after saving the revision meta-data
231  // and content meta-data.
232  $saved = SlotRecord::newSaved( 10, 20, 'theNewAddress', $unsaved );
233  $this->assertFalse( $saved->isInherited() );
234  $this->assertTrue( $saved->hasOrigin() );
235  $this->assertTrue( $saved->hasRevision() );
236  $this->assertTrue( $saved->hasAddress() );
237  $this->assertTrue( $saved->hasContentId() );
238  $this->assertSame( 'theNewAddress', $saved->getAddress() );
239  $this->assertSame( 20, $saved->getContentId() );
240  $this->assertSame( 'A', $saved->getContent()->getNativeData() );
241  $this->assertSame( 10, $saved->getRevision() );
242  $this->assertSame( 10, $saved->getOrigin() );
243 
244  // make sure we didn't mess with the internal state of $unsaved
245  $this->assertFalse( $unsaved->hasAddress() );
246  $this->assertFalse( $unsaved->hasContentId() );
247  $this->assertFalse( $unsaved->hasRevision() );
248  }
249 
250  public function provideNewSaved_LogicException() {
251  $freshRow = $this->makeRow( [
252  'content_id' => 10,
253  'content_address' => 'address:1',
254  'slot_origin' => 1,
255  'slot_revision_id' => 1,
256  ] );
257 
258  $freshSlot = new SlotRecord( $freshRow, new WikitextContent( 'A' ) );
259  yield 'mismatching address' => [ 1, 10, 'address:BAD', $freshSlot ];
260  yield 'mismatching revision' => [ 5, 10, 'address:1', $freshSlot ];
261  yield 'mismatching content ID' => [ 1, 17, 'address:1', $freshSlot ];
262 
263  $inheritedRow = $this->makeRow( [
264  'content_id' => null,
265  'content_address' => null,
266  'slot_origin' => 0,
267  'slot_revision_id' => 1,
268  ] );
269 
270  $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) );
271  yield 'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ];
272  }
273 
278  $revisionId,
279  $contentId,
280  $contentAddress,
281  SlotRecord $protoSlot
282  ) {
283  $this->setExpectedException( LogicException::class );
284  SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
285  }
286 
288  $unsaved = SlotRecord::newUnsaved( SlotRecord::MAIN, new WikitextContent( 'A' ) );
289 
290  yield 'bad revision id' => [ 'xyzzy', 5, 'address', $unsaved ];
291  yield 'bad content id' => [ 7, 'xyzzy', 'address', $unsaved ];
292  yield 'bad content address' => [ 7, 5, 77, $unsaved ];
293  }
294 
299  $revisionId,
300  $contentId,
301  $contentAddress,
302  SlotRecord $protoSlot
303  ) {
304  $this->setExpectedException( InvalidArgumentException::class );
305  SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
306  }
307 
308  public function provideHasSameContent() {
309  $fail = function () {
310  self::fail( 'There should be no need to actually load the content.' );
311  };
312 
313  $a100a1 = new SlotRecord(
314  $this->makeRow(
315  [
316  'model_name' => 'A',
317  'content_size' => 100,
318  'content_sha1' => 'hash-a',
319  'content_address' => 'xxx:a1',
320  ]
321  ),
322  $fail
323  );
324  $a100a1b = new SlotRecord(
325  $this->makeRow(
326  [
327  'model_name' => 'A',
328  'content_size' => 100,
329  'content_sha1' => 'hash-a',
330  'content_address' => 'xxx:a1',
331  ]
332  ),
333  $fail
334  );
335  $a100null = new SlotRecord(
336  $this->makeRow(
337  [
338  'model_name' => 'A',
339  'content_size' => 100,
340  'content_sha1' => 'hash-a',
341  'content_address' => null,
342  ]
343  ),
344  $fail
345  );
346  $a100a2 = new SlotRecord(
347  $this->makeRow(
348  [
349  'model_name' => 'A',
350  'content_size' => 100,
351  'content_sha1' => 'hash-a',
352  'content_address' => 'xxx:a2',
353  ]
354  ),
355  $fail
356  );
357  $b100a1 = new SlotRecord(
358  $this->makeRow(
359  [
360  'model_name' => 'B',
361  'content_size' => 100,
362  'content_sha1' => 'hash-a',
363  'content_address' => 'xxx:a1',
364  ]
365  ),
366  $fail
367  );
368  $a200a1 = new SlotRecord(
369  $this->makeRow(
370  [
371  'model_name' => 'A',
372  'content_size' => 200,
373  'content_sha1' => 'hash-a',
374  'content_address' => 'xxx:a2',
375  ]
376  ),
377  $fail
378  );
379  $a100x1 = new SlotRecord(
380  $this->makeRow(
381  [
382  'model_name' => 'A',
383  'content_size' => 100,
384  'content_sha1' => 'hash-x',
385  'content_address' => 'xxx:x1',
386  ]
387  ),
388  $fail
389  );
390 
391  yield 'same instance' => [ $a100a1, $a100a1, true ];
392  yield 'no address' => [ $a100a1, $a100null, true ];
393  yield 'same address' => [ $a100a1, $a100a1b, true ];
394  yield 'different address' => [ $a100a1, $a100a2, true ];
395  yield 'different model' => [ $a100a1, $b100a1, false ];
396  yield 'different size' => [ $a100a1, $a200a1, false ];
397  yield 'different hash' => [ $a100a1, $a100x1, false ];
398  }
399 
403  public function testHasSameContent( SlotRecord $a, SlotRecord $b, $sameContent ) {
404  $this->assertSame( $sameContent, $a->hasSameContent( $b ) );
405  $this->assertSame( $sameContent, $b->hasSameContent( $a ) );
406  }
407 
408 }
MediaWiki\Tests\Revision\SlotRecordTest\testGetContentId_fails
testGetContentId_fails()
Definition: SlotRecordTest.php:120
MediaWiki\Tests\Revision\SlotRecordTest\testConstructionDeferred
testConstructionDeferred()
Definition: SlotRecordTest.php:54
Revision\IncompleteRevisionException
Exception throw when trying to access undefined fields on an incomplete RevisionRecord.
Definition: IncompleteRevisionException.php:31
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Revision\SlotRecord\newInherited
static newInherited(SlotRecord $slot)
Constructs a new SlotRecord for a new revision, inheriting the content of the given SlotRecord of a p...
Definition: SlotRecord.php:103
Revision\SuppressedDataException
Exception raised in response to an audience check when attempting to access suppressed information wi...
Definition: SuppressedDataException.php:32
MediaWiki\Tests\Revision\SlotRecordTest\testNewSaved_LogicException
testNewSaved_LogicException( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
provideNewSaved_LogicException
Definition: SlotRecordTest.php:277
MediaWiki\Tests\Revision
Definition: McrReadNewRevisionStoreDbTest.php:2
Revision\SlotRecord\getRevision
getRevision()
Returns the ID of the revision this slot is associated with.
Definition: SlotRecord.php:398
MediaWiki\Tests\Revision\SlotRecordTest\testNewUnsaved
testNewUnsaved()
Definition: SlotRecordTest.php:89
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
MediaWiki\Tests\Revision\SlotRecordTest\testInvalidConstruction
testInvalidConstruction( $row, $content)
provideInvalidConstruction
Definition: SlotRecordTest.php:115
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Tests\Revision\SlotRecordTest\provideIncomplete
provideIncomplete()
Definition: SlotRecordTest.php:134
MediaWiki\Tests\Revision\SlotRecordTest\testGetRevision_fails
testGetRevision_fails(SlotRecord $record)
provideIncomplete
Definition: SlotRecordTest.php:146
MediaWiki\Tests\Revision\SlotRecordTest\testHasSameContent
testHasSameContent(SlotRecord $a, SlotRecord $b, $sameContent)
provideHasSameContent
Definition: SlotRecordTest.php:403
MediaWiki\Tests\Revision\SlotRecordTest\provideNewSaved_InvalidArgumentException
provideNewSaved_InvalidArgumentException()
Definition: SlotRecordTest.php:287
MediaWiki\Tests\Revision\SlotRecordTest\makeRow
makeRow( $data=[])
Definition: SlotRecordTest.php:18
Revision\SlotRecord\getOrigin
getOrigin()
Returns the revision ID of the revision that originated the slot's content.
Definition: SlotRecord.php:407
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:141
MediaWikiTestCase
Definition: MediaWikiTestCase.php:16
MediaWiki\Tests\Revision\SlotRecordTest\testNewInherited
testNewInherited()
Definition: SlotRecordTest.php:188
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$output
$output
Definition: SyntaxHighlight.php:334
Revision\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition: SlotRecord.php:609
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:35
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:250
Revision\SlotRecord\newWithSuppressedContent
static newWithSuppressedContent(SlotRecord $slot)
Returns a new SlotRecord just like the given $slot, except that calling getContent() will fail with a...
Definition: SlotRecord.php:63
Revision\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
MediaWiki\Tests\Revision\SlotRecordTest\testNewWithSuppressedContent
testNewWithSuppressedContent()
Definition: SlotRecordTest.php:180
MediaWiki\Tests\Revision\SlotRecordTest\testHashStability
testHashStability( $text, $hash)
provideHashStability
Definition: SlotRecordTest.php:171
MediaWiki\Tests\Revision\SlotRecordTest\testNewSaved_InvalidArgumentException
testNewSaved_InvalidArgumentException( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
provideNewSaved_InvalidArgumentException
Definition: SlotRecordTest.php:298
MediaWiki\Tests\Revision\SlotRecordTest\provideInvalidConstruction
provideInvalidConstruction()
Definition: SlotRecordTest.php:104
MediaWiki\Tests\Revision\SlotRecordTest\testCompleteConstruction
testCompleteConstruction()
Definition: SlotRecordTest.php:34
MediaWiki\Tests\Revision\SlotRecordTest\provideHashStability
provideHashStability()
Definition: SlotRecordTest.php:163
MediaWiki\Tests\Revision\SlotRecordTest\provideNewSaved_LogicException
provideNewSaved_LogicException()
Definition: SlotRecordTest.php:250
MediaWiki\Tests\Revision\SlotRecordTest\provideHasSameContent
provideHasSameContent()
Definition: SlotRecordTest.php:308
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
MediaWiki\Tests\Revision\SlotRecordTest\testNewSaved
testNewSaved()
Definition: SlotRecordTest.php:226
MediaWiki\Tests\Revision\SlotRecordTest\testGetAddress_fails
testGetAddress_fails()
Definition: SlotRecordTest.php:127
$parent
$parent
Definition: pageupdater.txt:71
Revision\SlotRecord\newSaved
static newSaved( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
Constructs a complete SlotRecord for a newly saved revision, based on the incomplete proto-slot.
Definition: SlotRecord.php:164
MediaWiki\Tests\Revision\SlotRecordTest\testGetOrigin_fails
testGetOrigin_fails(SlotRecord $record)
provideIncomplete
Definition: SlotRecordTest.php:156
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2044
$content
$content
Definition: pageupdater.txt:72
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
object
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition: globals.txt:25
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
MediaWiki\Tests\Revision\SlotRecordTest
\MediaWiki\Revision\SlotRecord
Definition: SlotRecordTest.php:16
Revision\SlotRecord\hasSameContent
hasSameContent(SlotRecord $other)
Returns true if $other has the same content as this slot.
Definition: SlotRecord.php:632