Debian Codegraph @

tests/phpunit/unit/includes/Revision/RevisionRecordTests.php

raw symbols SCIP
1 <?php
2
3 // phpcs:disable MediaWiki.Commenting.PhpunitAnnotations.NotClass
4 // phpcs:disable MediaWiki.Commenting.FunctionComment.MissingParamTag -- Traits are not excluded
5
6 namespace MediaWiki\Tests\Unit\Revision;
7
8 use DummyContentForTesting;
9 use LogicException;
10 use MediaWiki\CommentStore\CommentStoreComment;
11 use MediaWiki\Page\PageIdentity;
12 use MediaWiki\Revision\RevisionRecord;
13 use MediaWiki\Revision\RevisionSlots;
14 use MediaWiki\Revision\RevisionStoreRecord;
15 use MediaWiki\Revision\SlotRecord;
16 use MediaWiki\Revision\SuppressedDataException;
17 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
18 use MediaWiki\User\UserIdentityValue;
19 use MockTitleTrait;
20
21 /**
22 * @covers \MediaWiki\Revision\RevisionRecord
23 *
24 * @note Expects to be used in classes that extend MediaWikiUnitTestCase.
25 */
26 trait RevisionRecordTests {
27 use MockTitleTrait;
28 use MockAuthorityTrait;
29
30 /**
31 * @param array $rowOverrides
32 * @return RevisionRecord
33 */
34 abstract protected function newRevision( array $rowOverrides = [] );
35
36 /**
37 * provided by MediaWikiTestCaseTrait
38 * @param string $regex
39 */
40 abstract public function filterDeprecated( $regex );
41
42 public function testGetIdSuccessful() {
43 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_id' => 5 ] );
44 $this->assertEquals( 5, $revision->getId( 'acmewiki' ) );
45 }
46
47 public function testGetIdTriggerDeprecatedWarning() {
48 $this->expectDeprecationAndContinue( '/Deprecated cross-wiki access/' );
49 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_id' => 5 ] );
50 $revision->getId();
51 }
52
53 public function testGetIdDeprecated() {
54 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_id' => 5 ] );
55 $this->filterDeprecated( '/Deprecated cross-wiki access.*/' );
56 $this->assertEquals( 5, $revision->getId() );
57 }
58
59 public function testGetPageIdSuccessful() {
60 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_page_id' => 17 ] );
61 $this->assertEquals( 17, $revision->getPageId( 'acmewiki' ) );
62 }
63
64 public function testGetPageIdTriggerDeprecatedWarning() {
65 $this->expectDeprecationAndContinue( '/Deprecated cross-wiki access/' );
66 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_page_id' => 17 ] );
67 $revision->getPageId();
68 }
69
70 public function testGetPageIdDeprecated() {
71 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_page_id' => 17 ] );
72 $this->filterDeprecated( '/Deprecated cross-wiki access.*/' );
73 $this->assertEquals( 17, $revision->getPageId() );
74 }
75
76 public function testGetParentIdSuccessful() {
77 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_parent_id' => 1 ] );
78 $this->assertEquals( 1, $revision->getParentId( 'acmewiki' ) );
79 }
80
81 public function testGetParentIdTriggerDeprecatedWarning() {
82 $this->expectDeprecationAndContinue( '/Deprecated cross-wiki access/' );
83 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_parent_id' => 1 ] );
84 $revision->getParentId();
85 }
86
87 public function testGetParentIdDeprecated() {
88 $revision = $this->newRevision( [ 'wikiId' => 'acmewiki', 'rev_parent_id' => 1 ] );
89 $this->filterDeprecated( '/Deprecated cross-wiki access.*/' );
90 $this->assertEquals( 1, $revision->getParentId() );
91 }
92
93 abstract protected function expectedDefaultFieldVisibility( $field ): bool;
94
95 private function provideAudienceCheckData( $field ) {
96 yield 'field accessible for oversighter (ALL)' => [
97 RevisionRecord::SUPPRESSED_ALL,
98 [ 'deletedtext', 'deletedhistory', 'viewsuppressed', 'suppressrevision' ],
99 true,
100 false
101 ];
102
103 yield 'field accessible for oversighter' => [
104 RevisionRecord::DELETED_RESTRICTED | $field,
105 [ 'deletedtext', 'deletedhistory', 'viewsuppressed', 'suppressrevision' ],
106 true,
107 false
108 ];
109
110 yield 'field not accessible for sysops (ALL)' => [
111 RevisionRecord::SUPPRESSED_ALL,
112 [ 'deletedtext', 'deletedhistory' ],
113 false,
114 false
115 ];
116
117 yield 'field not accessible for sysops' => [
118 RevisionRecord::DELETED_RESTRICTED | $field,
119 [ 'deletedtext', 'deletedhistory' ],
120 false,
121 false
122 ];
123
124 yield 'field accessible for sysops' => [
125 $field,
126 [ 'deletedtext', 'deletedhistory' ],
127 true,
128 false
129 ];
130
131 yield 'field suppressed for logged in users' => [
132 $field,
133 [],
134 false,
135 false
136 ];
137
138 yield 'unrelated field suppressed' => [
139 $field === RevisionRecord::DELETED_COMMENT
140 ? RevisionRecord::DELETED_USER
141 : RevisionRecord::DELETED_COMMENT,
142 [],
143 $this->expectedDefaultFieldVisibility( $field ),
144 $this->expectedDefaultFieldVisibility( $field )
145 ];
146
147 yield 'nothing suppressed' => [
148 0,
149 [],
150 $this->expectedDefaultFieldVisibility( $field ),
151 $this->expectedDefaultFieldVisibility( $field )
152 ];
153 }
154
155 public function testSerialization_fails() {
156 $this->expectException( LogicException::class );
157 $rev = $this->newRevision();
158 serialize( $rev );
159 }
160
161 public function provideGetComment_audience() {
162 return $this->provideAudienceCheckData( RevisionRecord::DELETED_COMMENT );
163 }
164
165 /**
166 * @dataProvider provideGetComment_audience
167 */
168 public function testGetComment_audience( $visibility, $permissions, $userCan, $publicCan ) {
169 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
170 $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
171
172 $this->assertNotNull( $rev->getComment( RevisionRecord::RAW ), 'raw can' );
173
174 $this->assertSame(
175 $publicCan,
176 $rev->getComment( RevisionRecord::FOR_PUBLIC ) !== null,
177 'public can'
178 );
179 $this->assertSame(
180 $userCan,
181 $rev->getComment( RevisionRecord::FOR_THIS_USER, $performer ) !== null,
182 'user can'
183 );
184 }
185
186 public function provideGetUser_audience() {
187 return $this->provideAudienceCheckData( RevisionRecord::DELETED_USER );
188 }
189
190 /**
191 * @dataProvider provideGetUser_audience
192 */
193 public function testGetUser_audience( $visibility, $permissions, $userCan, $publicCan ) {
194 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
195 $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
196
197 $this->assertNotNull( $rev->getUser( RevisionRecord::RAW ), 'raw can' );
198
199 $this->assertSame(
200 $publicCan,
201 $rev->getUser( RevisionRecord::FOR_PUBLIC ) !== null,
202 'public can'
203 );
204 $this->assertSame(
205 $userCan,
206 $rev->getUser( RevisionRecord::FOR_THIS_USER, $performer ) !== null,
207 'user can'
208 );
209 }
210
211 public function provideGetSlot_audience() {
212 return $this->provideAudienceCheckData( RevisionRecord::DELETED_TEXT );
213 }
214
215 /**
216 * @dataProvider provideGetSlot_audience
217 */
218 public function testGetSlot_audience( $visibility, $permissions, $userCan, $publicCan ) {
219 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
220 $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
221
222 // NOTE: slot meta-data is never suppressed, just the content is!
223 $this->assertTrue( $rev->hasSlot( SlotRecord::MAIN ), 'hasSlot is never suppressed' );
224 $this->assertNotNull( $rev->getSlot( SlotRecord::MAIN, RevisionRecord::RAW ), 'raw meta' );
225 $this->assertNotNull( $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ),
226 'public meta' );
227
228 $this->assertNotNull(
229 $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $performer ),
230 'user can'
231 );
232
233 try {
234 $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC )->getContent();
235 $exception = null;
236 } catch ( SuppressedDataException $ex ) {
237 $exception = $ex;
238 }
239
240 $this->assertSame(
241 $publicCan,
242 $exception === null,
243 'public can'
244 );
245
246 try {
247 $rev->getSlot( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $performer )->getContent();
248 $exception = null;
249 } catch ( SuppressedDataException $ex ) {
250 $exception = $ex;
251 }
252
253 $this->assertSame(
254 $userCan,
255 $exception === null,
256 'user can'
257 );
258 }
259
260 /**
261 * @dataProvider provideGetSlot_audience
262 */
263 public function testGetContent_audience( $visibility, $permissions, $userCan, $publicCan ) {
264 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
265 $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
266
267 $this->assertNotNull( $rev->getContent( SlotRecord::MAIN, RevisionRecord::RAW ), 'raw can' );
268
269 $this->assertSame(
270 $publicCan,
271 $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ) !== null,
272 'public can'
273 );
274 $this->assertSame(
275 $userCan,
276 $rev->getContent( SlotRecord::MAIN, RevisionRecord::FOR_THIS_USER, $performer ) !== null,
277 'user can'
278 );
279 }
280
281 /**
282 * @dataProvider provideGetSlot_audience
283 */
284 public function testGetContentOrThrow_audience( $visibility, $permissions, $userCan,
285 $publicCan
286 ) {
287 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
288 $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] );
289
290 $exception = null;
291 try {
292 $rev->getContentOrThrow( SlotRecord::MAIN, RevisionRecord::RAW );
293 } catch ( SuppressedDataException $exception ) {
294 }
295 $this->assertNull( $exception, 'raw can' );
296
297 $exception = null;
298 try {
299 $rev->getContentOrThrow( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC );
300 } catch ( SuppressedDataException $exception ) {
301 }
302 $this->assertSame( $publicCan, $exception === null, 'public can' );
303
304 $exception = null;
305 try {
306 $rev->getContentOrThrow( SlotRecord::MAIN,
307 RevisionRecord::FOR_THIS_USER, $performer );
308 } catch ( SuppressedDataException $exception ) {
309 }
310 $this->assertSame( $userCan, $exception === null, 'user can' );
311 }
312
313 public function testGetSlot() {
314 $rev = $this->newRevision();
315
316 $slot = $rev->getSlot( SlotRecord::MAIN );
317 $this->assertNotNull( $slot, 'getSlot()' );
318 $this->assertSame( SlotRecord::MAIN, $slot->getRole(), 'getRole()' );
319 }
320
321 public function testHasSlot() {
322 $rev = $this->newRevision();
323
324 $this->assertTrue( $rev->hasSlot( SlotRecord::MAIN ) );
325 $this->assertFalse( $rev->hasSlot( 'xyz' ) );
326 }
327
328 public function testGetContent() {
329 $rev = $this->newRevision();
330
331 $content = $rev->getSlot( SlotRecord::MAIN );
332 $this->assertNotNull( $content, 'getContent()' );
333 $this->assertSame(
334 DummyContentForTesting::MODEL_ID,
335 $content->getModel(),
336 'getModel()'
337 );
338 }
339
340 public function provideUserCanBitfield() {
341 yield [ 0, 0, [], null, true ];
342 // Bitfields match, user has no permissions
343 yield [
344 RevisionRecord::DELETED_TEXT,
345 RevisionRecord::DELETED_TEXT,
346 [],
347 null,
348 false
349 ];
350 yield [
351 RevisionRecord::DELETED_COMMENT,
352 RevisionRecord::DELETED_COMMENT,
353 [],
354 null,
355 false,
356 ];
357 yield [
358 RevisionRecord::DELETED_USER,
359 RevisionRecord::DELETED_USER,
360 [],
361 null,
362 false
363 ];
364 yield [
365 RevisionRecord::DELETED_RESTRICTED,
366 RevisionRecord::DELETED_RESTRICTED,
367 [],
368 null,
369 false,
370 ];
371 // Bitfields match, user (admin) does have permissions
372 yield [
373 RevisionRecord::DELETED_TEXT,
374 RevisionRecord::DELETED_TEXT,
375 [ 'deletedtext', 'deletedhistory' ],
376 null,
377 true,
378 ];
379 yield [
380 RevisionRecord::DELETED_COMMENT,
381 RevisionRecord::DELETED_COMMENT,
382 [ 'deletedtext', 'deletedhistory' ],
383 null,
384 true,
385 ];
386 yield [
387 RevisionRecord::DELETED_USER,
388 RevisionRecord::DELETED_USER,
389 [ 'deletedtext', 'deletedhistory' ],
390 null,
391 true,
392 ];
393 // Bitfields match, user (admin) does not have permissions
394 yield [
395 RevisionRecord::DELETED_RESTRICTED,
396 RevisionRecord::DELETED_RESTRICTED,
397 [ 'deletedtext', 'deletedhistory' ],
398 null,
399 false,
400 ];
401 // Bitfields match, user (oversight) does have permissions
402 yield [
403 RevisionRecord::DELETED_RESTRICTED,
404 RevisionRecord::DELETED_RESTRICTED,
405 [ 'deletedtext', 'deletedhistory', 'viewsuppressed', 'suppressrevision' ],
406 null,
407 true,
408 ];
409 // Check permissions using the title
410 yield [
411 RevisionRecord::DELETED_TEXT,
412 RevisionRecord::DELETED_TEXT,
413 [ 'deletedtext', 'deletedhistory' ],
414 $this->makeMockTitle( __METHOD__ ),
415 true,
416 ];
417 yield [
418 RevisionRecord::DELETED_TEXT,
419 RevisionRecord::DELETED_TEXT,
420 [],
421 $this->makeMockTitle( __METHOD__ ),
422 false,
423 ];
424 }
425
426 /**
427 * @dataProvider provideUserCanBitfield
428 */
429 public function testUserCanBitfield( $bitField, $field, $permissions, ?PageIdentity $title, $expected ) {
430 $performer = $this->mockRegisteredAuthorityWithPermissions( $permissions );
431
432 $this->assertSame(
433 $expected,
434 RevisionRecord::userCanBitfield( $bitField, $field, $performer, $title )
435 );
436 }
437
438 public function provideHasSameContent() {
439 // Create some slots with content
440 $mainA = SlotRecord::newUnsaved( SlotRecord::MAIN, new DummyContentForTesting( 'A' ) );
441 $mainB = SlotRecord::newUnsaved( SlotRecord::MAIN, new DummyContentForTesting( 'B' ) );
442 $auxA = SlotRecord::newUnsaved( 'aux', new DummyContentForTesting( 'A' ) );
443 $auxB = SlotRecord::newUnsaved( 'aux', new DummyContentForTesting( 'A' ) );
444
445 return [
446 'same record object' => [
447 true,
448 $this->makeHasSameContentTestRecord( [ $mainA ], 12 ),
449 $this->makeHasSameContentTestRecord( [ $mainA ], 12 ),
450 ],
451 'same record content, different object' => [
452 true,
453 $this->makeHasSameContentTestRecord( [ $mainA ], 12 ),
454 $this->makeHasSameContentTestRecord( [ $mainA ], 13 )
455 ],
456 'same record content, aux slot, different object' => [
457 true,
458 $this->makeHasSameContentTestRecord( [ $auxA ], 12 ),
459 $this->makeHasSameContentTestRecord( [ $auxB ], 13 ),
460 ],
461 'different content' => [
462 false,
463 $this->makeHasSameContentTestRecord( [ $mainA ], 12 ),
464 $this->makeHasSameContentTestRecord( [ $mainB ], 13 ),
465 ],
466 'different content and number of slots' => [
467 false,
468 $this->makeHasSameContentTestRecord( [ $mainA ], 12 ),
469 $this->makeHasSameContentTestRecord( [ $mainA, $mainB ], 13 ),
470 ],
471 ];
472 }
473
474 /**
475 * @param SlotRecord[] $slots
476 * @param int $revId
477 * @return RevisionStoreRecord
478 */
479 private function makeHasSameContentTestRecord( array $slots, $revId ) {
480 $slots = new RevisionSlots( $slots );
481
482 return new RevisionStoreRecord(
483 $this->makeMockTitle( 'provideHasSameContent', [ 'id' => 19 ] ),
484 new UserIdentityValue( 11, __METHOD__ ),
485 CommentStoreComment::newUnsavedComment( __METHOD__ ),
486 (object)[
487 'rev_id' => strval( $revId ),
488 'rev_page' => strval( 19 ),
489 'rev_timestamp' => '20200101000000',
490 'rev_deleted' => 0,
491 'rev_minor_edit' => 0,
492 'rev_parent_id' => '5',
493 'rev_len' => $slots->computeSize(),
494 'rev_sha1' => $slots->computeSha1(),
495 'page_latest' => '18',
496 ],
497 $slots
498 );
499 }
500
501 /**
502 * @dataProvider provideHasSameContent
503 */
504 public function testHasSameContent(
505 $expected,
506 $record1,
507 $record2
508 ) {
509 $this->assertSame(
510 $expected,
511 $record1->hasSameContent( $record2 )
512 );
513 }
514
515 public function provideIsDeleted() {
516 yield 'no deletion' => [
517 0,
518 [
519 RevisionRecord::DELETED_TEXT => false,
520 RevisionRecord::DELETED_COMMENT => false,
521 RevisionRecord::DELETED_USER => false,
522 RevisionRecord::DELETED_RESTRICTED => false,
523 ]
524 ];
525 yield 'text deleted' => [
526 RevisionRecord::DELETED_TEXT,
527 [
528 RevisionRecord::DELETED_TEXT => true,
529 RevisionRecord::DELETED_COMMENT => false,
530 RevisionRecord::DELETED_USER => false,
531 RevisionRecord::DELETED_RESTRICTED => false,
532 ]
533 ];
534 yield 'text and comment deleted' => [
535 RevisionRecord::DELETED_TEXT + RevisionRecord::DELETED_COMMENT,
536 [
537 RevisionRecord::DELETED_TEXT => true,
538 RevisionRecord::DELETED_COMMENT => true,
539 RevisionRecord::DELETED_USER => false,
540 RevisionRecord::DELETED_RESTRICTED => false,
541 ]
542 ];
543 yield 'all 4 deleted' => [
544 RevisionRecord::DELETED_TEXT +
545 RevisionRecord::DELETED_COMMENT +
546 RevisionRecord::DELETED_RESTRICTED +
547 RevisionRecord::DELETED_USER,
548 [
549 RevisionRecord::DELETED_TEXT => true,
550 RevisionRecord::DELETED_COMMENT => true,
551 RevisionRecord::DELETED_USER => true,
552 RevisionRecord::DELETED_RESTRICTED => true,
553 ]
554 ];
555 }
556
557 /**
558 * @dataProvider provideIsDeleted
559 */
560 public function testIsDeleted( $revDeleted, $assertionMap ) {
561 $rev = $this->newRevision( [ 'rev_deleted' => $revDeleted ] );
562 foreach ( $assertionMap as $deletionLevel => $expected ) {
563 $this->assertSame( $expected, $rev->isDeleted( $deletionLevel ) );
564 }
565 }
566
567 public function testIsReadyForInsertion() {
568 $rev = $this->newRevision();
569 $this->assertTrue( $rev->isReadyForInsertion() );
570 }
571 }