MediaWiki  REL1_31
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->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() );
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 () {
59  },
60  'slot_revision_id' => '2',
61  'slot_origin' => '2',
62  ] );
63 
64  $content = function () {
65  return new WikitextContent( 'A' );
66  };
67 
68  $record = new SlotRecord( $row, $content );
69 
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() );
83  }
84 
85  public function testNewUnsaved() {
86  $record = SlotRecord::newUnsaved( 'myRole', new WikitextContent( 'A' ) );
87 
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() );
96  }
97 
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 ];
104  }
105 
109  public function testInvalidConstruction( $row, $content ) {
110  $this->setExpectedException( InvalidArgumentException::class );
111  new SlotRecord( $row, $content );
112  }
113 
114  public function testGetContentId_fails() {
115  $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
116  $this->setExpectedException( IncompleteRevisionException::class );
117 
118  $record->getContentId();
119  }
120 
121  public function testGetAddress_fails() {
122  $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
123  $this->setExpectedException( IncompleteRevisionException::class );
124 
125  $record->getAddress();
126  }
127 
128  public function provideIncomplete() {
129  $unsaved = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
130  yield 'unsaved' => [ $unsaved ];
131 
132  $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
133  $inherited = SlotRecord::newInherited( $parent );
134  yield 'inherited' => [ $inherited ];
135  }
136 
140  public function testGetRevision_fails( SlotRecord $record ) {
141  $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
142  $this->setExpectedException( IncompleteRevisionException::class );
143 
144  $record->getRevision();
145  }
146 
150  public function testGetOrigin_fails( SlotRecord $record ) {
151  $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
152  $this->setExpectedException( IncompleteRevisionException::class );
153 
154  $record->getOrigin();
155  }
156 
157  public function provideHashStability() {
158  yield [ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ];
159  yield [ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ];
160  }
161 
165  public function testHashStability( $text, $hash ) {
166  // Changing the output of the hash function will break things horribly!
167 
168  $this->assertSame( $hash, SlotRecord::base36Sha1( $text ) );
169 
170  $record = SlotRecord::newUnsaved( 'main', new WikitextContent( $text ) );
171  $this->assertSame( $hash, $record->getSha1() );
172  }
173 
174  public function testNewWithSuppressedContent() {
175  $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
177 
178  $this->setExpectedException( SuppressedDataException::class );
179  $output->getContent();
180  }
181 
182  public function testNewInherited() {
183  $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] );
184  $parent = new SlotRecord( $row, new WikitextContent( 'A' ) );
185 
186  // This would happen while doing an edit, before saving revision meta-data.
187  $inherited = SlotRecord::newInherited( $parent );
188 
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() );
194 
195  // make sure we didn't mess with the internal state of $parent
196  $this->assertFalse( $parent->isInherited() );
197  $this->assertSame( 7, $parent->getRevision() );
198 
199  // This would happen while doing an edit, after saving the revision meta-data
200  // and content meta-data.
201  $saved = SlotRecord::newSaved(
202  10,
203  $inherited->getContentId(),
204  $inherited->getAddress(),
205  $inherited
206  );
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() );
213 
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() );
217  }
218 
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' ) );
222 
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() );
234 
235  // make sure we didn't mess with the internal state of $unsaved
236  $this->assertFalse( $unsaved->hasAddress() );
237  $this->assertFalse( $unsaved->hasRevision() );
238  }
239 
240  public function provideNewSaved_LogicException() {
241  $freshRow = $this->makeRow( [
242  'content_id' => 10,
243  'content_address' => 'address:1',
244  'slot_origin' => 1,
245  'slot_revision_id' => 1,
246  ] );
247 
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 ];
252 
253  $inheritedRow = $this->makeRow( [
254  'content_id' => null,
255  'content_address' => null,
256  'slot_origin' => 0,
257  'slot_revision_id' => 1,
258  ] );
259 
260  $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) );
261  yield 'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ];
262  }
263 
268  $revisionId,
269  $contentId,
270  $contentAddress,
271  SlotRecord $protoSlot
272  ) {
273  $this->setExpectedException( LogicException::class );
274  SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
275  }
276 
278  $unsaved = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) );
279 
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 ];
283  }
284 
289  $revisionId,
290  $contentId,
291  $contentAddress,
292  SlotRecord $protoSlot
293  ) {
294  $this->setExpectedException( InvalidArgumentException::class );
295  SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot );
296  }
297 
298 }
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 For a description of the see design txt $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:64
MediaWiki\Tests\Storage\SlotRecordTest\testConstructionDeferred
testConstructionDeferred()
Definition: SlotRecordTest.php:53
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWiki\Storage\SlotRecord\getRevision
getRevision()
Returns the ID of the revision this slot is associated with.
Definition: SlotRecord.php:390
MediaWiki\Storage\SlotRecord\base36Sha1
static base36Sha1( $blob)
Get the base 36 SHA-1 value for a string of text.
Definition: SlotRecord.php:564
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2255
MediaWiki\Storage\SuppressedDataException
Exception raised in response to an audience check when attempting to access suppressed information wi...
Definition: SuppressedDataException.php:31
MediaWiki\Tests\Storage\SlotRecordTest\testNewWithSuppressedContent
testNewWithSuppressedContent()
Definition: SlotRecordTest.php:174
MediaWiki\Storage\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:159
MediaWiki\Tests\Storage\SlotRecordTest\provideNewSaved_LogicException
provideNewSaved_LogicException()
Definition: SlotRecordTest.php:240
MediaWiki\Tests\Storage\SlotRecordTest\testNewInherited
testNewInherited()
Definition: SlotRecordTest.php:182
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:245
MediaWiki\Tests\Storage\SlotRecordTest\testGetOrigin_fails
testGetOrigin_fails(SlotRecord $record)
provideIncomplete
Definition: SlotRecordTest.php:150
MediaWiki\Tests\Storage\SlotRecordTest\testGetAddress_fails
testGetAddress_fails()
Definition: SlotRecordTest.php:121
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:37
MediaWiki\Tests\Storage\SlotRecordTest\makeRow
makeRow( $data=[])
Definition: SlotRecordTest.php:18
MediaWiki\Tests\Storage\SlotRecordTest\testNewSaved_LogicException
testNewSaved_LogicException( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
provideNewSaved_LogicException
Definition: SlotRecordTest.php:267
MediaWiki\Tests\Storage
Definition: BlobStoreFactoryTest.php:3
$input
if(is_array( $mode)) switch( $mode) $input
Definition: postprocess-phan.php:145
MediaWiki\Tests\Storage\SlotRecordTest\testGetRevision_fails
testGetRevision_fails(SlotRecord $record)
provideIncomplete
Definition: SlotRecordTest.php:140
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Storage\SlotRecord\newUnsaved
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:124
MediaWiki\Tests\Storage\SlotRecordTest\testNewUnsaved
testNewUnsaved()
Definition: SlotRecordTest.php:85
MediaWiki\Storage\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:98
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:33
MediaWiki\Tests\Storage\SlotRecordTest\provideNewSaved_InvalidArgumentException
provideNewSaved_InvalidArgumentException()
Definition: SlotRecordTest.php:277
CONTENT_FORMAT_WIKITEXT
const CONTENT_FORMAT_WIKITEXT
Definition: Defines.php:260
MediaWiki\Tests\Storage\SlotRecordTest\testCompleteConstruction
testCompleteConstruction()
Definition: SlotRecordTest.php:34
MediaWiki\Tests\Storage\SlotRecordTest\testGetContentId_fails
testGetContentId_fails()
Definition: SlotRecordTest.php:114
MediaWiki\Tests\Storage\SlotRecordTest
\MediaWiki\Storage\SlotRecord
Definition: SlotRecordTest.php:16
MediaWiki\Tests\Storage\SlotRecordTest\testInvalidConstruction
testInvalidConstruction( $row, $content)
provideInvalidConstruction
Definition: SlotRecordTest.php:109
MediaWiki\Tests\Storage\SlotRecordTest\provideHashStability
provideHashStability()
Definition: SlotRecordTest.php:157
MediaWiki\Tests\Storage\SlotRecordTest\provideInvalidConstruction
provideInvalidConstruction()
Definition: SlotRecordTest.php:98
MediaWiki\Tests\Storage\SlotRecordTest\testNewSaved_InvalidArgumentException
testNewSaved_InvalidArgumentException( $revisionId, $contentId, $contentAddress, SlotRecord $protoSlot)
provideNewSaved_InvalidArgumentException
Definition: SlotRecordTest.php:288
MediaWiki\Tests\Storage\SlotRecordTest\testHashStability
testHashStability( $text, $hash)
provideHashStability
Definition: SlotRecordTest.php:165
MediaWiki\Storage\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:58
MediaWiki\Tests\Storage\SlotRecordTest\testNewSaved
testNewSaved()
Definition: SlotRecordTest.php:219
MediaWiki\Storage\IncompleteRevisionException
Exception throw when trying to access undefined fields on an incomplete RevisionRecord.
Definition: IncompleteRevisionException.php:30
MediaWiki\Storage\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:38
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:56
MediaWiki\Tests\Storage\SlotRecordTest\provideIncomplete
provideIncomplete()
Definition: SlotRecordTest.php:128
MediaWiki\Storage\SlotRecord\getOrigin
getOrigin()
Returns the revision ID of the revision that originated the slot's content.
Definition: SlotRecord.php:399