MediaWiki  REL1_31
RevisionStoreRecordTest.php
Go to the documentation of this file.
1 <?php
2 
4 
6 use InvalidArgumentException;
15 use Title;
16 
22 
24 
30  protected function newRevision( array $rowOverrides = [] ) {
31  $title = Title::newFromText( 'Dummy' );
32  $title->resetArticleID( 17 );
33 
34  $user = new UserIdentityValue( 11, 'Tester', 0 );
35  $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
36 
37  $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) );
38  $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
39  $slots = new RevisionSlots( [ $main, $aux ] );
40 
41  $row = [
42  'rev_id' => '7',
43  'rev_page' => strval( $title->getArticleID() ),
44  'rev_timestamp' => '20200101000000',
45  'rev_deleted' => 0,
46  'rev_minor_edit' => 0,
47  'rev_parent_id' => '5',
48  'rev_len' => $slots->computeSize(),
49  'rev_sha1' => $slots->computeSha1(),
50  'page_latest' => '18',
51  ];
52 
53  $row = array_merge( $row, $rowOverrides );
54 
55  return new RevisionStoreRecord( $title, $user, $comment, (object)$row, $slots );
56  }
57 
58  public function provideConstructor() {
59  $title = Title::newFromText( 'Dummy' );
60  $title->resetArticleID( 17 );
61 
62  $user = new UserIdentityValue( 11, 'Tester', 0 );
63  $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
64 
65  $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) );
66  $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
67  $slots = new RevisionSlots( [ $main, $aux ] );
68 
69  $protoRow = [
70  'rev_id' => '7',
71  'rev_page' => strval( $title->getArticleID() ),
72  'rev_timestamp' => '20200101000000',
73  'rev_deleted' => 0,
74  'rev_minor_edit' => 0,
75  'rev_parent_id' => '5',
76  'rev_len' => $slots->computeSize(),
77  'rev_sha1' => $slots->computeSha1(),
78  'page_latest' => '18',
79  ];
80 
81  $row = $protoRow;
82  yield 'all info' => [
83  $title,
84  $user,
85  $comment,
86  (object)$row,
87  $slots,
88  'acmewiki'
89  ];
90 
91  $row = $protoRow;
92  $row['rev_minor_edit'] = '1';
93  $row['rev_deleted'] = strval( RevisionRecord::DELETED_USER );
94 
95  yield 'minor deleted' => [
96  $title,
97  $user,
98  $comment,
99  (object)$row,
100  $slots
101  ];
102 
103  $row = $protoRow;
104  $row['page_latest'] = $row['rev_id'];
105 
106  yield 'latest' => [
107  $title,
108  $user,
109  $comment,
110  (object)$row,
111  $slots
112  ];
113 
114  $row = $protoRow;
115  unset( $row['rev_parent'] );
116 
117  yield 'no parent' => [
118  $title,
119  $user,
120  $comment,
121  (object)$row,
122  $slots
123  ];
124 
125  $row = $protoRow;
126  $row['rev_len'] = null;
127  $row['rev_sha1'] = '';
128 
129  yield 'rev_len is null, rev_sha1 is ""' => [
130  $title,
131  $user,
132  $comment,
133  (object)$row,
134  $slots
135  ];
136 
137  $row = $protoRow;
138  yield 'no length, no hash' => [
139  Title::newFromText( 'DummyDoesNotExist' ),
140  $user,
141  $comment,
142  (object)$row,
143  $slots
144  ];
145  }
146 
157  public function testConstructorAndGetters(
158  Title $title,
160  CommentStoreComment $comment,
161  $row,
162  RevisionSlots $slots,
163  $wikiId = false
164  ) {
165  $rec = new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $wikiId );
166 
167  $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
168  $this->assertSame( $user, $rec->getUser( RevisionRecord::RAW ), 'getUser' );
169  $this->assertSame( $comment, $rec->getComment(), 'getComment' );
170 
171  $this->assertSame( $slots->getSlotRoles(), $rec->getSlotRoles(), 'getSlotRoles' );
172  $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
173 
174  $this->assertSame( (int)$row->rev_id, $rec->getId(), 'getId' );
175  $this->assertSame( (int)$row->rev_page, $rec->getPageId(), 'getId' );
176  $this->assertSame( $row->rev_timestamp, $rec->getTimestamp(), 'getTimestamp' );
177  $this->assertSame( (int)$row->rev_deleted, $rec->getVisibility(), 'getVisibility' );
178  $this->assertSame( (bool)$row->rev_minor_edit, $rec->isMinor(), 'getIsMinor' );
179 
180  if ( isset( $row->rev_parent_id ) ) {
181  $this->assertSame( (int)$row->rev_parent_id, $rec->getParentId(), 'getParentId' );
182  } else {
183  $this->assertSame( 0, $rec->getParentId(), 'getParentId' );
184  }
185 
186  if ( isset( $row->rev_len ) ) {
187  $this->assertSame( (int)$row->rev_len, $rec->getSize(), 'getSize' );
188  } else {
189  $this->assertSame( $slots->computeSize(), $rec->getSize(), 'getSize' );
190  }
191 
192  if ( !empty( $row->rev_sha1 ) ) {
193  $this->assertSame( $row->rev_sha1, $rec->getSha1(), 'getSha1' );
194  } else {
195  $this->assertSame( $slots->computeSha1(), $rec->getSha1(), 'getSha1' );
196  }
197 
198  if ( isset( $row->page_latest ) ) {
199  $this->assertSame(
200  (int)$row->rev_id === (int)$row->page_latest,
201  $rec->isCurrent(),
202  'isCurrent'
203  );
204  } else {
205  $this->assertSame(
206  false,
207  $rec->isCurrent(),
208  'isCurrent'
209  );
210  }
211  }
212 
213  public function provideConstructorFailure() {
214  $title = Title::newFromText( 'Dummy' );
215  $title->resetArticleID( 17 );
216 
217  $user = new UserIdentityValue( 11, 'Tester', 0 );
218 
219  $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
220 
221  $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) );
222  $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
223  $slots = new RevisionSlots( [ $main, $aux ] );
224 
225  $protoRow = [
226  'rev_id' => '7',
227  'rev_page' => strval( $title->getArticleID() ),
228  'rev_timestamp' => '20200101000000',
229  'rev_deleted' => 0,
230  'rev_minor_edit' => 0,
231  'rev_parent_id' => '5',
232  'rev_len' => $slots->computeSize(),
233  'rev_sha1' => $slots->computeSha1(),
234  'page_latest' => '18',
235  ];
236 
237  yield 'not a row' => [
238  $title,
239  $user,
240  $comment,
241  'not a row',
242  $slots,
243  'acmewiki'
244  ];
245 
246  $row = $protoRow;
247  $row['rev_timestamp'] = 'kittens';
248 
249  yield 'bad timestamp' => [
250  $title,
251  $user,
252  $comment,
253  (object)$row,
254  $slots
255  ];
256 
257  $row = $protoRow;
258  $row['rev_page'] = 99;
259 
260  yield 'page ID mismatch' => [
261  $title,
262  $user,
263  $comment,
264  (object)$row,
265  $slots
266  ];
267 
268  $row = $protoRow;
269 
270  yield 'bad wiki' => [
271  $title,
272  $user,
273  $comment,
274  (object)$row,
275  $slots,
276  12345
277  ];
278  }
279 
290  public function testConstructorFailure(
291  Title $title,
293  CommentStoreComment $comment,
294  $row,
295  RevisionSlots $slots,
296  $wikiId = false
297  ) {
298  $this->setExpectedException( InvalidArgumentException::class );
299  new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $wikiId );
300  }
301 
302  public function provideIsCurrent() {
303  yield [
304  [
305  'rev_id' => 11,
306  'page_latest' => 11,
307  ],
308  true,
309  ];
310  yield [
311  [
312  'rev_id' => 11,
313  'page_latest' => 10,
314  ],
315  false,
316  ];
317  }
318 
322  public function testIsCurrent( $row, $current ) {
323  $rev = $this->newRevision( $row );
324 
325  $this->assertSame( $current, $rev->isCurrent(), 'isCurrent()' );
326  }
327 
328  public function provideGetSlot_audience_latest() {
330  }
331 
335  public function testGetSlot_audience_latest( $visibility, $groups, $userCan, $publicCan ) {
336  $this->forceStandardPermissions();
337 
338  $user = $this->getTestUser( $groups )->getUser();
339  $rev = $this->newRevision(
340  [
341  'rev_deleted' => $visibility,
342  'rev_id' => 11,
343  'page_latest' => 11, // revision is current
344  ]
345  );
346 
347  // NOTE: slot meta-data is never suppressed, just the content is!
348  $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::RAW ), 'raw can' );
349  $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC ), 'public can' );
350 
351  $this->assertNotNull(
352  $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user ),
353  'user can'
354  );
355 
356  $rev->getSlot( 'main', RevisionRecord::RAW )->getContent();
357  // NOTE: the content of the current revision is never suppressed!
358  // Check that getContent() doesn't throw SuppressedDataException
359  $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC )->getContent();
360  $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user )->getContent();
361  }
362 
363 }
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:32
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:247
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:65
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
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:273
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
MediaWiki\Tests\Storage\RevisionStoreRecordTest\provideGetSlot_audience_latest
provideGetSlot_audience_latest()
Definition: RevisionStoreRecordTest.php:328
MediaWikiTestCase\getTestUser
static getTestUser( $groups=[])
Convenience method for getting an immutable test user.
Definition: MediaWikiTestCase.php:153
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaWiki\Tests\Storage\RevisionStoreRecordTest\testConstructorAndGetters
testConstructorAndGetters(Title $title, UserIdentity $user, CommentStoreComment $comment, $row, RevisionSlots $slots, $wikiId=false)
provideConstructor
Definition: RevisionStoreRecordTest.php:157
MediaWiki\Tests\Storage\RevisionStoreRecordTest\provideConstructorFailure
provideConstructorFailure()
Definition: RevisionStoreRecordTest.php:213
MediaWiki\Tests\Storage\RevisionStoreRecordTest\provideIsCurrent
provideIsCurrent()
Definition: RevisionStoreRecordTest.php:302
MediaWiki\Tests\Storage\RevisionStoreRecordTest
\MediaWiki\Storage\RevisionStoreRecord \MediaWiki\Storage\RevisionRecord
Definition: RevisionStoreRecordTest.php:21
MediaWiki\Tests\Storage\provideAudienceCheckData
provideAudienceCheckData( $field)
Definition: RevisionRecordTests.php:33
MediaWiki\Tests\Storage\RevisionStoreRecordTest\testConstructorFailure
testConstructorFailure(Title $title, UserIdentity $user, CommentStoreComment $comment, $row, RevisionSlots $slots, $wikiId=false)
provideConstructorFailure
Definition: RevisionStoreRecordTest.php:290
MediaWiki\Storage\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:34
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
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\Storage\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:55
MediaWiki\Tests\Storage
Definition: BlobStoreFactoryTest.php:3
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
MediaWiki\Storage\RevisionStoreRecord
A RevisionRecord representing an existing revision persisted in the revision table.
Definition: RevisionStoreRecord.php:38
MediaWiki\Tests\Storage\RevisionStoreRecordTest\provideConstructor
provideConstructor()
Definition: RevisionStoreRecordTest.php:58
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\Storage\RevisionSlots\computeSize
computeSize()
Computes the total nominal size of the revision's slots, in bogo-bytes.
Definition: RevisionSlots.php:145
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
MediaWiki\Storage\RevisionSlots\computeSha1
computeSha1()
Computes the combined hash of the revisions's slots.
Definition: RevisionSlots.php:187
MediaWiki\Storage\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:44
MediaWiki\Storage\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:57
MediaWiki\Tests\Storage\RevisionStoreRecordTest\newRevision
newRevision(array $rowOverrides=[])
Definition: RevisionStoreRecordTest.php:30
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1777
Title
Represents a title within MediaWiki.
Definition: Title.php:39
MediaWiki\Tests\Storage\RevisionStoreRecordTest\testIsCurrent
testIsCurrent( $row, $current)
provideIsCurrent
Definition: RevisionStoreRecordTest.php:322
MediaWiki\Storage\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:38
MediaWiki\Storage\RevisionRecord\DELETED_USER
const DELETED_USER
Definition: RevisionRecord.php:49
MediaWiki\Storage\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:47
MediaWiki\Tests\Storage\RevisionRecordTests
trait RevisionRecordTests
\MediaWiki\Storage\RevisionRecord
Definition: RevisionRecordTests.php:24
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\Storage\RevisionSlots\getSlotRoles
getSlotRoles()
Returns the slot names (roles) of all slots present in this revision.
Definition: RevisionSlots.php:132
MediaWiki\Tests\Storage\RevisionStoreRecordTest\testGetSlot_audience_latest
testGetSlot_audience_latest( $visibility, $groups, $userCan, $publicCan)
provideGetSlot_audience_latest
Definition: RevisionStoreRecordTest.php:335
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:28
MediaWiki\Storage\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:56
MediaWiki\Tests\Storage\forceStandardPermissions
forceStandardPermissions()
Definition: RevisionRecordTests.php:103