MediaWiki  1.33.0
RevisionRecordTests.php
Go to the documentation of this file.
1 <?php
2 
3 // phpcs:disable MediaWiki.Commenting.PhpunitAnnotations.NotClass
4 
6 
8 use LogicException;
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,
242  $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ) !== null,
243  'public can'
244  );
245  $this->assertSame(
246  $userCan,
247  $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $user ) !== null,
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' => [
491  [
496  ]
497  ];
498  yield 'all 4 deleted' => [
503  [
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 }
MediaWiki\Tests\Revision\testSerialization_fails
testSerialization_fails()
Definition: RevisionRecordTests.php:92
MediaWiki\User\UserIdentityValue
Value object representing a user's identity.
Definition: UserIdentityValue.php:32
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:66
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
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:306
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:45
MediaWiki\Tests\Revision\RevisionRecordTests
trait RevisionRecordTests
\MediaWiki\Revision\RevisionRecord
Definition: RevisionRecordTests.php:23
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Revision\SuppressedDataException
Exception raised in response to an audience check when attempting to access suppressed information wi...
Definition: SuppressedDataException.php:32
MediaWiki\Tests\Revision\provideHasSameContent
provideHasSameContent()
Definition: RevisionRecordTests.php:381
MediaWiki\Tests\Revision
Definition: FallbackSlotRoleHandlerTest.php:3
MediaWiki\Tests\Revision\testGetUser_audience
testGetUser_audience( $visibility, $groups, $userCan, $publicCan)
provideGetUser_audience
Definition: RevisionRecordTests.php:158
serialize
serialize()
Definition: ApiMessageTrait.php:134
MediaWiki\Tests\Revision\provideGetComment_audience
provideGetComment_audience()
Definition: RevisionRecordTests.php:98
MediaWiki\Tests\Revision\provideIsDeleted
provideIsDeleted()
Definition: RevisionRecordTests.php:470
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
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
MediaWiki\Tests\Revision\testIsDeleted
testIsDeleted( $revDeleted, $assertionMap)
provideIsDeleted \MediaWiki\Revision\RevisionRecord::isDeleted
Definition: RevisionRecordTests.php:516
MediaWiki\Tests\Revision\makeHasSameContentTestRecord
makeHasSameContentTestRecord(array $slots, $revId)
Definition: RevisionRecordTests.php:427
MediaWiki\Tests\Revision\testGetComment_audience
testGetComment_audience( $visibility, $groups, $userCan, $publicCan)
provideGetComment_audience
Definition: RevisionRecordTests.php:131
MediaWiki\Tests\Revision\provideAudienceCheckData
provideAudienceCheckData( $field)
Definition: RevisionRecordTests.php:32
MediaWiki\Tests\Revision\testIsReadyForInsertion
testIsReadyForInsertion()
Definition: RevisionRecordTests.php:523
MediaWiki\Tests\Revision\forceStandardPermissions
forceStandardPermissions()
Definition: RevisionRecordTests.php:102
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
Revision\RevisionRecord\userCanBitfield
static userCanBitfield( $bitfield, $field, User $user, Title $title=null)
Determine if the current user is allowed to view a particular field of this revision,...
Definition: RevisionRecord.php:508
Revision\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:59
MediaWiki\Tests\Revision\testGetContent_audience
testGetContent_audience( $visibility, $groups, $userCan, $publicCan)
provideGetSlot_audience
Definition: RevisionRecordTests.php:232
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
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\provideGetSlot_audience
provideGetSlot_audience()
Definition: RevisionRecordTests.php:178
Revision\RevisionRecord\DELETED_USER
const DELETED_USER
Definition: RevisionRecord.php:50
MediaWiki\Tests\Revision\testHasSlot
testHasSlot()
Definition: RevisionRecordTests.php:260
MediaWiki\Tests\Revision\testGetSlot
testGetSlot()
Definition: RevisionRecordTests.php:252
Revision\RevisionStoreRecord
A RevisionRecord representing an existing revision persisted in the revision table.
Definition: RevisionStoreRecord.php:39
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:37
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:57
Title
Represents a title within MediaWiki.
Definition: Title.php:40
MediaWiki\Tests\Revision\provideGetUser_audience
provideGetUser_audience()
Definition: RevisionRecordTests.php:151
Revision\RevisionRecord\DELETED_COMMENT
const DELETED_COMMENT
Definition: RevisionRecord.php:49
Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:48
$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:1769
as
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
MediaWiki\Tests\Revision\testGetSlot_audience
testGetSlot_audience( $visibility, $groups, $userCan, $publicCan)
provideGetSlot_audience
Definition: RevisionRecordTests.php:185
Revision\RevisionSlots
Value object representing the set of slots belonging to a revision.
Definition: RevisionSlots.php:35
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:1985
$content
$content
Definition: pageupdater.txt:72
MediaWiki\Tests\Revision\testHasSameContent
testHasSameContent( $expected, $recordSpec1, $recordSpec2)
provideHasSameContent \MediaWiki\Revision\RevisionRecord::hasSameContent Database
Definition: RevisionRecordTests.php:456
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
Revision\RevisionRecord\SUPPRESSED_ALL
const SUPPRESSED_ALL
Definition: RevisionRecord.php:53
Revision\RevisionRecord\DELETED_RESTRICTED
const DELETED_RESTRICTED
Definition: RevisionRecord.php:51
CONTENT_MODEL_TEXT
const CONTENT_MODEL_TEXT
Definition: Defines.php:238
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:58
MediaWiki\Tests\Revision\testUserCanBitfield
testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected)
provideUserCanBitfield \MediaWiki\Revision\RevisionRecord::userCanBitfield
Definition: RevisionRecordTests.php:365
CommentStoreComment
CommentStoreComment represents a comment stored by CommentStore.
Definition: CommentStoreComment.php:29
MediaWiki\Tests\Revision\provideUserCanBitfield
provideUserCanBitfield()
Definition: RevisionRecordTests.php:275
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39
MediaWiki\Tests\Revision\testGetContent
testGetContent()
Definition: RevisionRecordTests.php:267