MediaWiki  master
RevisionRecordTests.php
Go to the documentation of this file.
1 <?php
2 
3 // phpcs:disable MediaWiki.Commenting.PhpunitAnnotations.NotClass
4 
6 
16 use Title;
17 
24 
30  abstract protected function newRevision( array $rowOverrides = [] );
31 
32  private function provideAudienceCheckData( $field ) {
33  yield 'field accessible for oversighter (ALL)' => [
35  [ 'oversight' ],
36  true,
37  false
38  ];
39 
40  yield 'field accessible for oversighter' => [
42  [ 'oversight' ],
43  true,
44  false
45  ];
46 
47  yield 'field not accessible for sysops (ALL)' => [
49  [ 'sysop' ],
50  false,
51  false
52  ];
53 
54  yield 'field not accessible for sysops' => [
56  [ 'sysop' ],
57  false,
58  false
59  ];
60 
61  yield 'field accessible for sysops' => [
62  $field,
63  [ 'sysop' ],
64  true,
65  false
66  ];
67 
68  yield 'field suppressed for logged in users' => [
69  $field,
70  [ 'user' ],
71  false,
72  false
73  ];
74 
75  yield 'unrelated field suppressed' => [
79  [ 'user' ],
80  true,
81  true
82  ];
83 
84  yield 'nothing suppressed' => [
85  0,
86  [ 'user' ],
87  true,
88  true
89  ];
90  }
91 
92  public function testSerialization_fails() {
93  $this->setExpectedException( LogicException::class );
94  $rev = $this->newRevision();
95  serialize( $rev );
96  }
97 
98  public function provideGetComment_audience() {
100  }
101 
102  private function forceStandardPermissions() {
103  $this->setMwGlobals(
104  'wgGroupPermissions',
105  [
106  'user' => [
107  'viewsuppressed' => false,
108  'suppressrevision' => false,
109  'deletedtext' => false,
110  'deletedhistory' => false,
111  ],
112  'sysop' => [
113  'viewsuppressed' => false,
114  'suppressrevision' => false,
115  'deletedtext' => true,
116  'deletedhistory' => true,
117  ],
118  'oversight' => [
119  'deletedtext' => true,
120  'deletedhistory' => true,
121  'viewsuppressed' => true,
122  'suppressrevision' => true,
123  ],
124  ]
125  );
126  }
127 
131  public function testGetComment_audience( $visibility, $groups, $userCan, $publicCan ) {
132  $this->forceStandardPermissions();
133 
134  $user = $this->getTestUser( $groups )->getUser();
135  $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
136 
137  $this->assertNotNull( $rev->getComment( RevisionRecord::RAW ), 'raw can' );
138 
139  $this->assertSame(
140  $publicCan,
141  $rev->getComment( RevisionRecord::FOR_PUBLIC ) !== null,
142  'public can'
143  );
144  $this->assertSame(
145  $userCan,
146  $rev->getComment( RevisionRecord::FOR_THIS_USER, $user ) !== null,
147  'user can'
148  );
149  }
150 
151  public function provideGetUser_audience() {
153  }
154 
158  public function testGetUser_audience( $visibility, $groups, $userCan, $publicCan ) {
159  $this->forceStandardPermissions();
160 
161  $user = $this->getTestUser( $groups )->getUser();
162  $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
163 
164  $this->assertNotNull( $rev->getUser( RevisionRecord::RAW ), 'raw can' );
165 
166  $this->assertSame(
167  $publicCan,
168  $rev->getUser( RevisionRecord::FOR_PUBLIC ) !== null,
169  'public can'
170  );
171  $this->assertSame(
172  $userCan,
173  $rev->getUser( RevisionRecord::FOR_THIS_USER, $user ) !== null,
174  'user can'
175  );
176  }
177 
178  public function provideGetSlot_audience() {
180  }
181 
185  public function testGetSlot_audience( $visibility, $groups, $userCan, $publicCan ) {
186  $this->forceStandardPermissions();
187 
188  $user = $this->getTestUser( $groups )->getUser();
189  $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
190 
191  // NOTE: slot meta-data is never suppressed, just the content is!
192  $this->assertTrue( $rev->hasSlot( SlotRecord::MAIN ), 'hasSlot is never suppressed' );
193  $this->assertNotNull( $rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ), 'raw meta' );
194  $this->assertNotNull( $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ),
195  'public meta' );
196 
197  $this->assertNotNull(
199  'user can'
200  );
201 
202  try {
203  $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC )->getContent();
204  $exception = null;
205  } catch ( SuppressedDataException $ex ) {
206  $exception = $ex;
207  }
208 
209  $this->assertSame(
210  $publicCan,
211  $exception === null,
212  'public can'
213  );
214 
215  try {
216  $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $user )->getContent();
217  $exception = null;
218  } catch ( SuppressedDataException $ex ) {
219  $exception = $ex;
220  }
221 
222  $this->assertSame(
223  $userCan,
224  $exception === null,
225  'user can'
226  );
227  }
228 
232  public function testGetContent_audience( $visibility, $groups, $userCan, $publicCan ) {
233  $this->forceStandardPermissions();
234 
235  $user = $this->getTestUser( $groups )->getUser();
236  $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
237 
238  $this->assertNotNull( $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ), 'raw can' );
239 
240  $this->assertSame(
241  $publicCan,
243  'public can'
244  );
245  $this->assertSame(
246  $userCan,
248  'user can'
249  );
250  }
251 
252  public function testGetSlot() {
253  $rev = $this->newRevision();
254 
255  $slot = $rev->getSlot( SlotRecord::MAIN );
256  $this->assertNotNull( $slot, 'getSlot()' );
257  $this->assertSame( 'main', $slot->getRole(), 'getRole()' );
258  }
259 
260  public function testHasSlot() {
261  $rev = $this->newRevision();
262 
263  $this->assertTrue( $rev->hasSlot( SlotRecord::MAIN ) );
264  $this->assertFalse( $rev->hasSlot( 'xyz' ) );
265  }
266 
267  public function testGetContent() {
268  $rev = $this->newRevision();
269 
270  $content = $rev->getSlot( SlotRecord::MAIN );
271  $this->assertNotNull( $content, 'getContent()' );
272  $this->assertSame( CONTENT_MODEL_TEXT, $content->getModel(), 'getModel()' );
273  }
274 
275  public function provideUserCanBitfield() {
276  yield [ 0, 0, [], null, true ];
277  // Bitfields match, user has no permissions
278  yield [
281  [],
282  null,
283  false
284  ];
285  yield [
288  [],
289  null,
290  false,
291  ];
292  yield [
295  [],
296  null,
297  false
298  ];
299  yield [
302  [],
303  null,
304  false,
305  ];
306  // Bitfields match, user (admin) does have permissions
307  yield [
310  [ 'sysop' ],
311  null,
312  true,
313  ];
314  yield [
317  [ 'sysop' ],
318  null,
319  true,
320  ];
321  yield [
324  [ 'sysop' ],
325  null,
326  true,
327  ];
328  // Bitfields match, user (admin) does not have permissions
329  yield [
332  [ 'sysop' ],
333  null,
334  false,
335  ];
336  // Bitfields match, user (oversight) does have permissions
337  yield [
340  [ 'oversight' ],
341  null,
342  true,
343  ];
344  // Check permissions using the title
345  yield [
348  [ 'sysop' ],
349  __METHOD__,
350  true,
351  ];
352  yield [
355  [],
356  __METHOD__,
357  false,
358  ];
359  }
360 
365  public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) {
366  if ( is_string( $title ) ) {
367  // NOTE: Data providers cannot instantiate Title objects! See T202641.
369  }
370 
371  $this->forceStandardPermissions();
372 
373  $user = $this->getTestUser( $userGroups )->getUser();
374 
375  $this->assertSame(
376  $expected,
377  RevisionRecord::userCanBitfield( $bitField, $field, $user, $title )
378  );
379  }
380 
381  public function provideHasSameContent() {
382  // Create some slots with content
383  $mainA = SlotRecord::newUnsaved( SlotRecord::MAIN, new TextContent( 'A' ) );
384  $mainB = SlotRecord::newUnsaved( SlotRecord::MAIN, new TextContent( 'B' ) );
385  $auxA = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) );
386  $auxB = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) );
387 
388  $initialRecordSpec = [ [ $mainA ], 12 ];
389 
390  return [
391  'same record object' => [
392  true,
393  $initialRecordSpec,
394  $initialRecordSpec,
395  ],
396  'same record content, different object' => [
397  true,
398  [ [ $mainA ], 12 ],
399  [ [ $mainA ], 13 ],
400  ],
401  'same record content, aux slot, different object' => [
402  true,
403  [ [ $auxA ], 12 ],
404  [ [ $auxB ], 13 ],
405  ],
406  'different content' => [
407  false,
408  [ [ $mainA ], 12 ],
409  [ [ $mainB ], 13 ],
410  ],
411  'different content and number of slots' => [
412  false,
413  [ [ $mainA ], 12 ],
414  [ [ $mainA, $mainB ], 13 ],
415  ],
416  ];
417  }
418 
427  private function makeHasSameContentTestRecord( array $slots, $revId ) {
428  $title = Title::newFromText( 'provideHasSameContent' );
429  $title->resetArticleID( 19 );
430  $slots = new RevisionSlots( $slots );
431 
432  return new RevisionStoreRecord(
433  $title,
434  new UserIdentityValue( 11, __METHOD__, 0 ),
436  (object)[
437  'rev_id' => strval( $revId ),
438  'rev_page' => strval( $title->getArticleID() ),
439  'rev_timestamp' => '20200101000000',
440  'rev_deleted' => 0,
441  'rev_minor_edit' => 0,
442  'rev_parent_id' => '5',
443  'rev_len' => $slots->computeSize(),
444  'rev_sha1' => $slots->computeSha1(),
445  'page_latest' => '18',
446  ],
447  $slots
448  );
449  }
450 
456  public function testHasSameContent(
457  $expected,
458  $recordSpec1,
459  $recordSpec2
460  ) {
461  $record1 = $this->makeHasSameContentTestRecord( ...$recordSpec1 );
462  $record2 = $this->makeHasSameContentTestRecord( ...$recordSpec2 );
463 
464  $this->assertSame(
465  $expected,
466  $record1->hasSameContent( $record2 )
467  );
468  }
469 
470  public function provideIsDeleted() {
471  yield 'no deletion' => [
472  0,
473  [
478  ]
479  ];
480  yield 'text deleted' => [
482  [
487  ]
488  ];
489  yield 'text and comment deleted' => [
490  RevisionRecord::DELETED_TEXT + RevisionRecord::DELETED_COMMENT,
491  [
492  RevisionRecord::DELETED_TEXT => true,
493  RevisionRecord::DELETED_COMMENT => true,
496  ]
497  ];
498  yield 'all 4 deleted' => [
499  RevisionRecord::DELETED_TEXT +
500  RevisionRecord::DELETED_COMMENT +
503  [
504  RevisionRecord::DELETED_TEXT => true,
505  RevisionRecord::DELETED_COMMENT => true,
506  RevisionRecord::DELETED_USER => true,
508  ]
509  ];
510  }
511 
516  public function testIsDeleted( $revDeleted, $assertionMap ) {
517  $rev = $this->newRevision( [ 'rev_deleted' => $revDeleted ] );
518  foreach ( $assertionMap as $deletionLevel => $expected ) {
519  $this->assertSame( $expected, $rev->isDeleted( $deletionLevel ) );
520  }
521  }
522 
523  public function testIsReadyForInsertion() {
524  $rev = $this->newRevision();
525  $this->assertTrue( $rev->isReadyForInsertion() );
526  }
527 
528 }
A RevisionRecord representing an existing revision persisted in the revision table.
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:193
serialize()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
const CONTENT_MODEL_TEXT
Definition: Defines.php:218
makeHasSameContentTestRecord(array $slots, $revId)
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
testIsDeleted( $revDeleted, $assertionMap)
provideIsDeleted \MediaWiki\Revision\RevisionRecord::isDeleted
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:1978
testGetComment_audience( $visibility, $groups, $userCan, $publicCan)
provideGetComment_audience
testGetUser_audience( $visibility, $groups, $userCan, $publicCan)
provideGetUser_audience
static userCanBitfield( $bitfield, $field, User $user, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision, if it&#39;s marked as deleted.
Exception raised in response to an audience check when attempting to access suppressed information wi...
testGetContent_audience( $visibility, $groups, $userCan, $publicCan)
provideGetSlot_audience
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:773
Value object representing a user&#39;s identity.
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:918
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:1754
testHasSameContent( $expected, $recordSpec1, $recordSpec2)
provideHasSameContent \MediaWiki\Revision\RevisionRecord::hasSameContent Database ...
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
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
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
testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected)
provideUserCanBitfield \MediaWiki\Revision\RevisionRecord::userCanBitfield
static newUnsaved( $role, Content $content)
Constructs a new Slot from a Content object for a new revision.
Definition: SlotRecord.php:129
Value object representing the set of slots belonging to a revision.
$content
Definition: pageupdater.txt:72
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1460
testGetSlot_audience( $visibility, $groups, $userCan, $publicCan)
provideGetSlot_audience
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:322