Debian Codegraph @

tests/phpunit/unit/includes/Permissions/RestrictionStoreTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Permissions;
4
5 use DatabaseTestHelper;
6 use MediaWiki\Cache\LinkCache;
7 use MediaWiki\CommentStore\CommentStore;
8 use MediaWiki\Config\ServiceOptions;
9 use MediaWiki\DAO\WikiAwareEntity;
10 use MediaWiki\HookContainer\HookContainer;
11 use MediaWiki\Linker\LinksMigration;
12 use MediaWiki\MainConfigNames;
13 use MediaWiki\Page\PageIdentity;
14 use MediaWiki\Page\PageIdentityValue;
15 use MediaWiki\Page\PageReferenceValue;
16 use MediaWiki\Page\PageStore;
17 use MediaWiki\Permissions\RestrictionStore;
18 use MediaWiki\Tests\Unit\DummyServicesTrait;
19 use MediaWiki\Title\Title;
20 use MediaWikiUnitTestCase;
21 use MockTitleTrait;
22 use ReflectionClass;
23 use ReflectionMethod;
24 use RuntimeException;
25 use UnexpectedValueException;
26 use Wikimedia\Assert\PreconditionException;
27 use Wikimedia\ObjectCache\WANObjectCache;
28 use Wikimedia\Rdbms\DeleteQueryBuilder;
29 use Wikimedia\Rdbms\FakeResultWrapper;
30 use Wikimedia\Rdbms\IDatabase;
31 use Wikimedia\Rdbms\ILoadBalancer;
32 use Wikimedia\Rdbms\SelectQueryBuilder;
33 use Wikimedia\Rdbms\UnionQueryBuilder;
34
35 /**
36 * @covers \MediaWiki\Permissions\RestrictionStore
37 */
38 class RestrictionStoreTest extends MediaWikiUnitTestCase {
39 use DummyServicesTrait;
40 use MockTitleTrait;
41
42 private const DEFAULT_RESTRICTION_TYPES = [ 'create', 'edit', 'move', 'upload' ];
43
44 /**
45 * @param array $expectedCalls E.g.:
46 * [
47 * DB_REPLICA => [
48 * 'update' => callback, # to be called exactly once
49 * 'delete' => [ callback, 2 ], # to be called exactly twice
50 * 'select' => [ callback, -1 ], # may be called 0 or more times
51 * ],
52 * ]
53 * @return ILoadBalancer
54 */
55 private function newMockLoadBalancer( array $expectedCalls = [] ): ILoadBalancer {
56 if ( !isset( $expectedCalls[DB_REPLICA] ) ) {
57 $expectedCalls[DB_REPLICA] = [];
58 }
59 $expectedCalls[DB_REPLICA]['decodeExpiry'] = [
60 static function ( string $expiry ): string {
61 return $expiry;
62 },
63 -1
64 ];
65
66 $dbs = [];
67 foreach ( $expectedCalls as $index => $calls ) {
68 $dbs[$index] = $this->createNoOpMock(
69 IDatabase::class,
70 array_merge( array_keys( $calls ), [ 'newSelectQueryBuilder', 'newDeleteQueryBuilder', 'newUnionQueryBuilder' ] )
71 );
72 foreach ( $calls as $method => $callback ) {
73 $count = 1;
74 if ( is_array( $callback ) ) {
75 [ $callback, $count ] = $callback;
76 }
77 $dbs[$index]->expects( $count < 0 ? $this->any() : $this->exactly( $count ) )
78 ->method( $method )->willReturnCallback( $callback );
79 }
80 $dbs[$index]
81 ->method( 'newSelectQueryBuilder' )
82 ->willReturnCallback( static function () use ( $dbs, $index ) {
83 return new SelectQueryBuilder( $dbs[$index] );
84 } );
85 $dbs[$index]
86 ->method( 'newUnionQueryBuilder' )
87 ->willReturnCallback( static function () use ( $dbs, $index ) {
88 return new UnionQueryBuilder( $dbs[$index] );
89 } );
90 $dbs[$index]
91 ->method( 'newDeleteQueryBuilder' )
92 ->willReturnCallback( static function () use ( $dbs, $index ) {
93 return new DeleteQueryBuilder( $dbs[$index] );
94 } );
95 }
96
97 $lb = $this->createMock( ILoadBalancer::class, [ 'getConnection' ] );
98 $lb->method( 'getConnection' )->willReturnCallback(
99 function ( int $index ) use ( $dbs ): IDatabase {
100 $this->assertArrayHasKey( $index, $dbs );
101 return $dbs[$index];
102 }
103 );
104
105 return $lb;
106 }
107
108 private function newRestrictionStore( array $options = [] ): RestrictionStore {
109 return new RestrictionStore(
110 new ServiceOptions( RestrictionStore::CONSTRUCTOR_OPTIONS, $options + [
111 MainConfigNames::NamespaceProtection => [],
112 MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ],
113 MainConfigNames::RestrictionTypes => self::DEFAULT_RESTRICTION_TYPES,
114 MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed' ],
115 ] ),
116 $this->createNoOpMock( WANObjectCache::class ),
117 $this->newMockLoadBalancer( $options['db'] ?? [] ),
118 // @todo test that these calls work correctly
119 $this->createNoOpMock( LinkCache::class, [ 'addLinkObj', 'getGoodLinkFieldObj' ] ),
120 $this->createNoOpMock( LinksMigration::class, [ 'getLinksConditions' ] ),
121 $this->getDummyCommentStore(),
122 $this->createHookContainer( isset( $options['hookFn'] )
123 ? [ 'TitleGetRestrictionTypes' => $options['hookFn'] ]
124 : [] ),
125 $this->createNoOpMock( PageStore::class )
126 );
127 }
128
129 private static function newImproperPageIdentity(
130 int $ns, string $dbKey, $wikiId = PageIdentity::LOCAL
131 ): PageIdentity {
132 // PageIdentityValue doesn't allow negative namespaces, and Title::exists accesses services
133 // for hooks, so we need another solution to unit-test PageIdentity objects with negative
134 // namespaces (until they cease to exist).
135 return new class( $ns, $dbKey, $wikiId ) extends PageReferenceValue implements PageIdentity {
136 public function getId( $wikiId = PageIdentity::LOCAL ): int {
137 throw new RuntimeException;
138 }
139
140 public function canExist(): bool {
141 return false;
142 }
143
144 public function exists(): bool {
145 return false;
146 }
147 };
148 }
149
150 /**
151 * @dataProvider provideNonLocalPage
152 */
153 public function testNonLocalPage( string $method, ...$extraArgs ) {
154 $this->expectException( PreconditionException::class );
155 $this->expectExceptionMessage( 'otherwiki' );
156
157 $obj = $this->newRestrictionStore();
158 $page = new PageIdentityValue( 1, NS_MAIN, 'X', 'otherwiki' );
159 $obj->$method( $page, ...$extraArgs );
160 }
161
162 public static function provideNonLocalPage() {
163 // We programmatically get all public methods whose first parameter is a PageIdentity. This
164 // way we'll make sure to include any new methods that are added in the future.
165 $ret = [];
166 $methods = ( new ReflectionClass( RestrictionStore::class ) )
167 ->getMethods( ReflectionMethod::IS_PUBLIC );
168 foreach ( $methods as $method ) {
169 $params = $method->getParameters();
170 if ( !$params[0]->hasType() || $params[0]->getType()->getName() !== PageIdentity::class ) {
171 continue;
172 }
173 $ret[$method->getName()] = [ $method->getName() ];
174
175 foreach ( array_slice( $params, 1 ) as $param ) {
176 // Extra required arguments
177 if ( $param->isOptional() ) {
178 break;
179 }
180 switch ( $param->getType()->getName() ) {
181 case 'string':
182 $ret[$method->getName()][] = 'x';
183 break;
184 case 'array':
185 $ret[$method->getName()][] = [];
186 break;
187 default:
188 throw new UnexpectedValueException(
189 "{$param->getType()->getName} type not supported" );
190 }
191 }
192 }
193
194 return $ret;
195 }
196
197 /**
198 * @dataProvider provideGetRestrictions
199 */
200 public function testGetRestrictions(
201 array $expected,
202 PageIdentity $page,
203 string $action,
204 ?array $rowsToLoad,
205 array $options = []
206 ): void {
207 $obj = $this->newRestrictionStore( $options );
208 if ( is_array( $rowsToLoad ) ) {
209 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
210 }
211 $this->assertSame( $expected, $obj->getRestrictions( $page, $action ) );
212 }
213
214 public function provideGetRestrictions(): array {
215 $all = $this->provideGetAllRestrictions();
216 $ret = [];
217
218 foreach ( $all as $name => $arr ) {
219 [ $expected, $page ] = $arr;
220
221 $actions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'vaporize' ],
222 array_keys( $expected ) );
223 foreach ( $actions as $action ) {
224 $ret["$name ($action)"] =
225 array_merge(
226 [ $expected[$action] ?? [], $page, $action ],
227 array_slice( $arr, 2 )
228 );
229 }
230 }
231
232 return $ret;
233 }
234
235 /**
236 * @dataProvider provideGetAllRestrictions
237 */
238 public function testGetAllRestrictions(
239 array $expected, PageIdentity $page, ?array $rowsToLoad, array $options = []
240 ): void {
241 $obj = $this->newRestrictionStore( $options );
242 if ( is_array( $rowsToLoad ) ) {
243 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
244 }
245 $this->assertSame( $expected, $obj->getAllRestrictions( $page ) );
246 }
247
248 public static function provideGetAllRestrictions(): array {
249 return [
250 'Special page' => [
251 [],
252 self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
253 null,
254 ],
255 'Media page' => [
256 [],
257 self::newImproperPageIdentity( NS_MEDIA, 'X' ),
258 null,
259 ],
260 'Simple existing unprotected page' => [
261 [ 'edit' => [], 'move' => [] ],
262 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
263 [],
264 ],
265 'Simple existing protected page' => [
266 [ 'edit' => [ 'sysop', 'bureaucrat' ], 'move' => [ 'sysop' ] ],
267 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
268 [
269 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop,bureaucrat',
270 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
271 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
272 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
273 ],
274 ],
275 'Protection type not allowed' => [
276 [ 'edit' => [ 'sysop' ] ],
277 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
278 [
279 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
280 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
281 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
282 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
283 ],
284 [ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'upload' ] ],
285 ],
286 'Expired protection' => [
287 [ 'edit' => [], 'move' => [ 'sysop' ] ],
288 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
289 [
290 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
291 'pr_expiry' => '20200101000000', 'pr_cascade' => 0 ],
292 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
293 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
294 ],
295 ],
296 ];
297 }
298
299 /**
300 * @dataProvider provideGetRestrictionExpiry
301 */
302 public function testGetRestrictionExpiry(
303 ?string $expected,
304 PageIdentity $page,
305 string $action,
306 ?array $rowsToLoad,
307 array $options = []
308 ): void {
309 $obj = $this->newRestrictionStore( $options );
310 if ( is_array( $rowsToLoad ) ) {
311 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
312 }
313 $this->assertSame( $expected, $obj->getRestrictionExpiry( $page, $action ) );
314 }
315
316 public static function provideGetRestrictionExpiry(): array {
317 return [
318 'Special page' => [
319 null,
320 self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
321 'edit',
322 null,
323 ],
324 'Media page' => [
325 null,
326 self::newImproperPageIdentity( NS_MEDIA, 'X' ),
327 'edit',
328 null,
329 ],
330 'Simple existing unprotected page' => [
331 'infinity',
332 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
333 'edit',
334 [],
335 ],
336 'Simple existing protected page (edit)' => [
337 '20760101000000',
338 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
339 'edit',
340 [
341 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
342 'pr_expiry' => '20760101000000', 'pr_cascade' => 0 ],
343 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
344 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
345 ],
346 ],
347 'Simple existing protected page (move)' => [
348 '20670101000000',
349 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
350 'move',
351 [
352 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
353 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
354 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
355 'pr_expiry' => '20670101000000', 'pr_cascade' => 0 ],
356 ],
357 ],
358 'Simple existing protected page (unrecognized)' => [
359 null,
360 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
361 'unrecognized',
362 [
363 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
364 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
365 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
366 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
367 ],
368 ],
369 'Simple existing expired protected page (edit)' => [
370 'infinity',
371 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
372 'edit',
373 [
374 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
375 'pr_expiry' => '20160101000000', 'pr_cascade' => 0 ],
376 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
377 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
378 ],
379 ],
380 'Simple existing expired protected page (move)' => [
381 'infinity',
382 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
383 'move',
384 [
385 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
386 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
387 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
388 'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
389 ],
390 ],
391 'Simple existing expired protected page (unrecognized)' => [
392 null,
393 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
394 'unrecognized',
395 [
396 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
397 'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
398 (object)[ 'pr_type' => 'unrecognized', 'pr_level' => 'sysop',
399 'pr_expiry' => '20170101000000', 'pr_cascade' => 0 ],
400 ],
401 ],
402 ];
403 }
404
405 /**
406 * @dataProvider provideGetCreateProtection
407 */
408 public function testGetCreateProtection(
409 ?array $expected, PageIdentity $page, $return, array $options = []
410 ): void {
411 if ( $page->canExist() && !$page->exists() ) {
412 $options['db'] = [ DB_REPLICA => [ 'selectRow' =>
413 function (
414 $table, $vars, $conds, string $fname, $options = [], $join_conds = []
415 ) use ( $page, $return ) {
416 $options = (array)$options;
417 $options['LIMIT'] = 1;
418 $db = new DatabaseTestHelper( __CLASS__ );
419 $sql = trim( preg_replace( '/\s+/', ' ', $db->selectSQLText(
420 $table, $vars, $conds, $fname, $options, $join_conds ) ) );
421 $this->assertSame(
422 'SELECT pt_user,pt_expiry,pt_create_perm,comment_pt_reason.comment_text ' .
423 'AS pt_reason_text,comment_pt_reason.comment_data AS pt_reason_data,' .
424 'comment_pt_reason.comment_id AS pt_reason_cid ' .
425 'FROM protected_titles JOIN comment comment_pt_reason ON ' .
426 '((comment_pt_reason.comment_id = pt_reason_id)) ' .
427 "WHERE pt_namespace = {$page->getNamespace()} AND " .
428 "pt_title = '{$page->getDBkey()}' LIMIT 1",
429 $sql );
430
431 return is_array( $return ) ? (object)$return : $return;
432 }
433 ] ];
434 }
435 $obj = $this->newRestrictionStore( $options );
436 $this->assertSame( $expected, $obj->getCreateProtection( $page ) );
437 }
438
439 public static function provideGetCreateProtection(): array {
440 $ret = [
441 'Special page' => [ null, self::newImproperPageIdentity( NS_SPECIAL, 'X' ), null ],
442 'Media page' => [ null, self::newImproperPageIdentity( NS_MEDIA, 'X' ), null ],
443 'Existing page' => [ null, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), null ],
444 'Unprotected' => [ null, PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ), false ],
445 ];
446 $protectedTests = [
447 'sysop' => 'editprotected',
448 'autoconfirmed' => 'editsemiprotected',
449 'editprotected' => 'editprotected',
450 'editsemiprotected' => 'editsemiprotected',
451 'custom' => 'custom',
452 ];
453 foreach ( $protectedTests as $db => $returned ) {
454 $ret["Protected ($db)"] = [
455 [
456 'user' => 123,
457 'expiry' => 'infinity',
458 'permission' => $returned,
459 'reason' => 'reason',
460 ],
461 PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
462 [
463 'pt_user' => 123,
464 'pt_expiry' => 'infinity',
465 'pt_create_perm' => $db,
466 'pt_reason_id' => 456,
467 'pt_reason_data' => '{}',
468 'pt_reason_text' => 'reason',
469 ],
470 ];
471 }
472
473 return $ret;
474 }
475
476 /**
477 * @dataProvider provideDeleteCreateProtection
478 */
479 public function testDeleteCreateProtection( PageIdentity $page ): void {
480 $obj = $this->newRestrictionStore( [ 'db' => [ DB_PRIMARY => [ 'delete' =>
481 function ( string $table, array $where, string $method ) use ( $page ): bool {
482 $this->assertSame( 'protected_titles', $table );
483 $this->assertSame(
484 [ 'pt_namespace' => $page->getNamespace(), 'pt_title' => $page->getDBkey() ],
485 $where
486 );
487 return true;
488 }
489 ] ] ] );
490
491 $obj->deleteCreateProtection( $page );
492 }
493
494 public static function provideDeleteCreateProtection(): array {
495 return [
496 // Most of these don't actually make sense, but test current behavior regardless.
497 'Special page' => [ self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ],
498 'Media page' => [ self::newImproperPageIdentity( NS_MEDIA, 'X' ) ],
499 'Nonexistent page' => [ PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ) ],
500 'Existing page' => [ PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ],
501 ];
502 }
503
504 /**
505 * @dataProvider provideIsSemiProtected
506 */
507 public function testIsSemiProtected(
508 bool $expected, ?array $rowsToLoad, array $options = []
509 ): void {
510 $page = $options['page'] ?? PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
511 $obj = $this->newRestrictionStore( $options );
512 if ( is_array( $rowsToLoad ) ) {
513 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
514 }
515 $this->assertSame( $expected, $obj->isSemiProtected( $page, 'edit' ) );
516 }
517
518 public static function provideIsSemiProtected(): array {
519 return [
520 'Special page' => [ false, null,
521 [ 'page' => self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ] ],
522 'Media page' => [ false, null,
523 [ 'page' => self::newImproperPageIdentity( NS_MEDIA, 'X' ) ] ],
524 'Unprotected page' => [
525 false,
526 [ (object)[ 'pr_type' => 'move', 'pr_level' => 'autoconfirmed',
527 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
528 ],
529 'Semiprotected page' => [
530 true,
531 [
532 (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
533 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
534 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
535 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
536 ],
537 ],
538 'Fully protected page' => [
539 false,
540 [
541 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
542 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
543 (object)[ 'pr_type' => 'move', 'pr_level' => 'autoconfirmed',
544 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
545 ],
546 ],
547 'No semiprotection configured' => [
548 false,
549 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
550 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
551 [ MainConfigNames::SemiprotectedRestrictionLevels => [] ],
552 ],
553 'Config with editsemiprotected' => [
554 true,
555 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
556 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
557 [ MainConfigNames::SemiprotectedRestrictionLevels => [ 'editsemiprotected' ] ],
558 ],
559 'Data with editsemiprotected' => [
560 true,
561 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'editsemiprotected',
562 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
563 ],
564 'Config and data with editsemiprotected' => [
565 true,
566 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'editsemiprotected',
567 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
568 [ MainConfigNames::SemiprotectedRestrictionLevels => [ 'editsemiprotected' ] ],
569 ],
570 'Semiprotection plus other protection level' => [
571 false,
572 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed,superman',
573 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
574 [ MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop', 'superman' ] ],
575 ],
576 'Two semiprotections' => [
577 true,
578 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed,superman',
579 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
580 [ MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop', 'superman' ],
581 MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed', 'superman' ] ],
582 ],
583 ];
584 }
585
586 /**
587 * @dataProvider provideIsProtected
588 */
589 public function testIsProtected(
590 bool $expected, ?array $rowsToLoad, array $options = []
591 ): void {
592 $page = $options['page'] ?? PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
593 $action = $options['action'] ?? '';
594 $obj = $this->newRestrictionStore( $options );
595 if ( is_array( $rowsToLoad ) ) {
596 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
597 }
598 $this->assertSame( $expected, $obj->isProtected( $page, $action ) );
599 }
600
601 public static function provideIsProtected(): array {
602 return [
603 'Special page' => [ true, null,
604 [ 'page' => self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ] ],
605 'Media page' => [ false, null,
606 [ 'page' => self::newImproperPageIdentity( NS_MEDIA, 'X' ) ] ],
607 'Unprotected page' => [ false, [] ],
608 'Semiprotected page' => [
609 true,
610 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'autoconfirmed',
611 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
612 ],
613 'Fully protected page' => [
614 true,
615 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
616 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
617 ],
618 'Protected against empty string' => [
619 false,
620 [ (object)[ 'pr_type' => 'edit', 'pr_level' => '',
621 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
622 ],
623 'Unrecognized protection' => [
624 false,
625 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'unrecognized',
626 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
627 ],
628 'Unrecognized plus recognized protection' => [
629 true,
630 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop,unrecognized',
631 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
632 ],
633
634 'Check unrecognized protection type' => [
635 false,
636 [
637 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
638 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
639 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
640 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
641 (object)[ 'pr_type' => 'unrecognized', 'pr_level' => 'sysop',
642 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
643 ],
644 [ 'action' => 'unrecognized' ],
645 ],
646 'Check custom protection type' => [
647 true,
648 [ (object)[ 'pr_type' => 'custom', 'pr_level' => 'sysop',
649 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ], ],
650 [ 'action' => 'custom', MainConfigNames::RestrictionTypes =>
651 array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'custom' ] ) ],
652 ],
653 'Check custom protection level' => [
654 true,
655 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'custom',
656 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ], ],
657 [ 'action' => 'edit', 'RestrictionLevels' =>
658 [ '', 'autoconfirmed', 'sysop', 'custom' ] ],
659 ],
660
661 'Check edit protection of edit-protected page' => [
662 true,
663 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
664 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
665 [ 'action' => 'edit' ],
666 ],
667 'Check move protection of edit-protected page' => [
668 false,
669 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
670 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
671 [ 'action' => 'move' ],
672 ],
673 'Check move protection of move-protected page' => [
674 true,
675 [ (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
676 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
677 [ 'action' => 'move' ],
678 ],
679 'Check edit protection of move-protected page' => [
680 false,
681 [ (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
682 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ] ],
683 [ 'action' => 'edit' ],
684 ],
685 'Check edit protection of edit- and move-protected page' => [
686 true,
687 [
688 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
689 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
690 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
691 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
692 ],
693 [ 'action' => 'edit' ],
694 ],
695 'Check move protection of edit- and move-protected page' => [
696 true,
697 [
698 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
699 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
700 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
701 'pr_expiry' => 'infinity', 'pr_cascade' => '0' ],
702 ],
703 [ 'action' => 'move' ],
704 ],
705 ];
706 }
707
708 /**
709 * @dataProvider provideListApplicableRestrictionTypes
710 */
711 public function testListApplicableRestrictionTypes(
712 array $expected, PageIdentity $page, array $options = []
713 ): void {
714 $obj = $this->newRestrictionStore( $options );
715
716 $this->assertSame( $expected, $obj->listApplicableRestrictionTypes( $page ) );
717 }
718
719 public function provideListApplicableRestrictionTypes(): array {
720 $expandedRestrictions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'liquify' ] );
721 return [
722 'Special page' => [
723 [],
724 self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
725 ],
726 'Media page' => [
727 [],
728 self::newImproperPageIdentity( NS_MEDIA, 'X' ),
729 ],
730 'Nonexistent page' => [
731 [ 'create' ],
732 PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
733 ],
734 'Existing page' => [
735 [ 'edit', 'move' ],
736 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
737 ],
738 'Nonexistent file' => [
739 [ 'create' ],
740 PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
741 ],
742 'Existing file' => [
743 [ 'edit', 'move', 'upload' ],
744 PageIdentityValue::localIdentity( 1, NS_FILE, 'X' ),
745 ],
746
747 'Nonexistent page with no create' => [
748 [],
749 PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
750 [ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'upload' ] ],
751 ],
752 'Existing page with no move' => [
753 [ 'edit' ],
754 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
755 [ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'upload' ] ],
756 ],
757 'Nonexistent file with no upload' => [
758 [ 'create' ],
759 PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
760 [ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'move' ] ],
761 ],
762
763 'Special page with extra type' => [
764 [],
765 self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
766 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
767 ],
768 'Media page with extra type' => [
769 [],
770 self::newImproperPageIdentity( NS_MEDIA, 'X' ),
771 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
772 ],
773 'Nonexistent page with extra type' => [
774 [ 'create' ],
775 PageIdentityValue::localIdentity( 0, NS_MAIN, 'X' ),
776 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
777 ],
778 'Existing page with extra type' => [
779 [ 'edit', 'move', 'liquify' ],
780 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
781 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
782 ],
783 'Nonexistent file with extra type' => [
784 [ 'create' ],
785 PageIdentityValue::localIdentity( 0, NS_FILE, 'X' ),
786 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
787 ],
788 'Existing file with extra type' => [
789 [ 'edit', 'move', 'upload', 'liquify' ],
790 PageIdentityValue::localIdentity( 1, NS_FILE, 'X' ),
791 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
792 ],
793
794 'Hook' => [
795 [ 'move', 'liquify' ],
796 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
797 [ 'hookFn' => static function ( Title $title, array &$types ): bool {
798 self::assertEquals( Title::castFromPageIdentity(
799 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ), $title );
800 self::assertSame( [ 'edit', 'move' ], $types );
801
802 $types = [ 'move', 'liquify' ];
803
804 return false;
805 } ],
806 ],
807 'Hook not run for special page' => [
808 [],
809 self::newImproperPageIdentity( NS_SPECIAL, 'X' ),
810 [ 'hookFn' => function () {
811 $this->fail( 'Should be unreached' );
812 } ],
813 ],
814 'Hook not run for media page' => [
815 [],
816 self::newImproperPageIdentity( NS_MEDIA, 'X' ),
817 [ 'hookFn' => function () {
818 $this->fail( 'Should be unreached' );
819 } ],
820 ],
821 ];
822 }
823
824 /**
825 * @dataProvider provideListAllRestrictionTypes
826 */
827 public function testListAllRestrictionTypes(
828 array $expected, array $args, array $options = []
829 ) {
830 $obj = $this->newRestrictionStore( $options );
831 $this->assertSame( $expected, $obj->listAllRestrictionTypes( ...$args ) );
832 }
833
834 public static function provideListAllRestrictionTypes() {
835 $expandedRestrictions = array_merge( self::DEFAULT_RESTRICTION_TYPES, [ 'solidify' ] );
836 return [
837 'Exists' => [ [ 'edit', 'move', 'upload' ], [ true ] ],
838 'Default is exists' => [ [ 'edit', 'move', 'upload' ], [] ],
839 'Nonexistent' => [ [ 'create' ], [ false ] ],
840
841 'Exists with extra restriction type' => [
842 [ 'edit', 'move', 'upload', 'solidify' ],
843 [ true ],
844 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
845 ],
846 'Default is exists with extra restriction type' => [
847 [ 'edit', 'move', 'upload', 'solidify' ],
848 [],
849 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
850 ],
851 'Nonexistent with extra restriction type' => [
852 [ 'create' ],
853 [ false ],
854 [ MainConfigNames::RestrictionTypes => $expandedRestrictions ],
855 ],
856
857 'Exists with no edit' => [
858 [ 'move', 'upload' ],
859 [ true ],
860 [ MainConfigNames::RestrictionTypes => [ 'create', 'move', 'upload' ] ],
861 ],
862 'Exists with only create' => [
863 [],
864 [ true ],
865 [ MainConfigNames::RestrictionTypes => [ 'create' ] ],
866 ],
867 'Nonexistent with no create' => [
868 [],
869 [ false ],
870 [ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'upload', 'solidify' ] ],
871 ],
872 'Nonexistent with no upload' => [
873 [ 'create' ],
874 [ false ],
875 [ MainConfigNames::RestrictionTypes => [ 'create', 'edit', 'move', 'solidify' ] ],
876 ],
877 'Nonexistent with no create or upload' => [
878 [],
879 [ false ],
880 [ MainConfigNames::RestrictionTypes => [ 'edit', 'move', 'solidify' ] ],
881 ],
882 ];
883 }
884
885 /**
886 * @dataProvider provideAreRestrictionsLoaded
887 */
888 public function testAreRestrictionsLoaded(
889 bool $expected, PageIdentity $page, ?array $rowsToLoad = null, array $options = []
890 ): void {
891 $obj = $this->newRestrictionStore( $options );
892 if ( is_array( $rowsToLoad ) ) {
893 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
894 }
895 $this->assertSame( $expected, $obj->areRestrictionsLoaded( $page ) );
896 }
897
898 public static function provideAreRestrictionsLoaded(): array {
899 return [
900 'Special page' => [ false, self::newImproperPageIdentity( NS_SPECIAL, 'X' ) ],
901 'Media page' => [ false, self::newImproperPageIdentity( NS_MEDIA, 'X' ) ],
902 'Regular page' => [ false, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ) ],
903 'Regular page with no restrictions' =>
904 [ true, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), [] ],
905 'Regular page with restrictions' => [
906 true,
907 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
908 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
909 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ] ],
910 ],
911 ];
912 }
913
914 /**
915 * @dataProvider provideAreRestrictionsCascading
916 */
917 public function testAreRestrictionsCascading(
918 bool $expected, PageIdentity $page, ?array $rowsToLoad, array $options = []
919 ): void {
920 $obj = $this->newRestrictionStore( $options );
921 if ( is_array( $rowsToLoad ) ) {
922 $obj->loadRestrictionsFromRows( $page, $rowsToLoad );
923 }
924 $this->assertSame( $expected, $obj->areRestrictionsCascading( $page ) );
925 }
926
927 public static function provideAreRestrictionsCascading(): array {
928 return [
929 'Special page' => [ false, self::newImproperPageIdentity( NS_SPECIAL, 'X' ), null ],
930 'Media page' => [ false, self::newImproperPageIdentity( NS_MEDIA, 'X' ), null ],
931 'Regular page with no restrictions' =>
932 [ false, PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ), [] ],
933 'Regular page with restrictions' => [
934 false,
935 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
936 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
937 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ] ],
938 ],
939 'Regular page with cascading restrictions' => [
940 true,
941 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
942 [ (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
943 'pr_expiry' => 'infinity', 'pr_cascade' => 1 ] ],
944 ],
945 'Regular page with some cascading restrictions and some not' => [
946 true,
947 PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' ),
948 [
949 (object)[ 'pr_type' => 'edit', 'pr_level' => 'sysop',
950 'pr_expiry' => 'infinity', 'pr_cascade' => 0 ],
951 (object)[ 'pr_type' => 'move', 'pr_level' => 'sysop',
952 'pr_expiry' => 'infinity', 'pr_cascade' => 1 ],
953 ],
954 ],
955 ];
956 }
957
958 public function testFlushRestrictions(): void {
959 $obj = $this->newRestrictionStore();
960 $page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
961 $this->assertFalse( $obj->areRestrictionsLoaded( $page ) );
962 $obj->loadRestrictionsFromRows( $page, [] );
963 $this->assertTrue( $obj->areRestrictionsLoaded( $page ) );
964 $obj->flushRestrictions( $page );
965 $this->assertFalse( $obj->areRestrictionsLoaded( $page ) );
966 }
967
968 public function testGetCascadeProtectionSources() {
969 $obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [ 'select' =>
970 static function () {
971 return new FakeResultWrapper( [
972 (object)[ 'pr_page' => 1, 'page_namespace' => NS_MAIN, 'page_title' => 'test',
973 'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
974 'type' => 'tl' ]
975 ] );
976 },
977 'addQuotes' => [
978 static function () {
979 return 'noop';
980 },
981 2
982 ]
983 ] ] ] );
984
985 $page = PageIdentityValue::localIdentity( 1, NS_MAIN, 'X' );
986 [ $sources, $restrictions, $tlSources, $ilSources ] = $obj->getCascadeProtectionSources( $page );
987 $this->assertCount( 1, $sources );
988 $this->assertArrayHasKey( 'edit', $restrictions );
989 $this->assertCount( 0, $ilSources );
990 $this->assertCount( 1, $tlSources );
991 }
992
993 public function testGetCascadeProtectionSourcesFile() {
994 $obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [
995 'addQuotes' => [
996 static function () {
997 return 'noop';
998 },
999 2
1000 ],
1001 'selectSQLText' => [
1002 static function () {
1003 return 'noop';
1004 },
1005 2
1006 ],
1007 'unionQueries' => static function () {
1008 return 'noop';
1009 },
1010 'query' => static function () {
1011 return new FakeResultWrapper( [
1012 (object)[ 'pr_page' => 1, 'page_namespace' => NS_MAIN, 'page_title' => 'test1',
1013 'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
1014 'type' => 'il' ],
1015 (object)[ 'pr_page' => 2, 'page_namespace' => NS_MAIN, 'page_title' => 'test2',
1016 'pr_expiry' => 'infinity', 'pr_type' => 'edit', 'pr_level' => 'Sysop',
1017 'type' => 'tl' ]
1018 ] );
1019 },
1020 ] ] ] );
1021
1022 $page = PageIdentityValue::localIdentity( 1, NS_FILE, 'Image.jpg' );
1023 [ $sources, $restrictions, $tlSources, $ilSources ] = $obj->getCascadeProtectionSources( $page );
1024 $this->assertCount( 2, $sources );
1025 $this->assertCount( 1, $ilSources );
1026 $this->assertCount( 1, $tlSources );
1027 $this->assertArrayHasKey( 'edit', $restrictions );
1028 }
1029
1030 public function testGetCascadeProtectionSourcesSpecialPage() {
1031 $obj = $this->newRestrictionStore( [ 'db' => [ DB_REPLICA => [ 'select' => [
1032 static function () {
1033 return [];
1034 },
1035 0
1036 ] ] ] ] );
1037
1038 $page = $this->makeMockTitle( 'Whatlinkshere', [ 'namespace' => NS_SPECIAL ] );
1039 [ $sources, $restrictions, $ilSources ] = $obj->getCascadeProtectionSources( $page );
1040 $this->assertCount( 0, $sources );
1041 $this->assertCount( 0, $restrictions );
1042 $this->assertCount( 0, $ilSources );
1043 }
1044
1045 public function testShouldNotFetchProtectionSettingsIfActionCannotBeRestricted(): void {
1046 $lb = $this->createMock( ILoadBalancer::class );
1047 $lb->expects( $this->never() )
1048 ->method( $this->anything() );
1049
1050 $store = new RestrictionStore(
1051 new ServiceOptions( RestrictionStore::CONSTRUCTOR_OPTIONS, [
1052 MainConfigNames::NamespaceProtection => [],
1053 MainConfigNames::RestrictionLevels => [ '', 'autoconfirmed', 'sysop' ],
1054 MainConfigNames::RestrictionTypes => self::DEFAULT_RESTRICTION_TYPES,
1055 MainConfigNames::SemiprotectedRestrictionLevels => [ 'autoconfirmed' ],
1056 ] ),
1057 WANObjectCache::newEmpty(),
1058 $lb,
1059 $this->createMock( LinkCache::class ),
1060 $this->createMock( LinksMigration::class ),
1061 $this->createMock( CommentStore::class ),
1062 $this->createMock( HookContainer::class ),
1063 $this->createMock( PageStore::class )
1064 );
1065
1066 $page = new PageIdentityValue( 1, NS_MAIN, 'Test', WikiAwareEntity::LOCAL );
1067
1068 $this->assertSame( [], $store->getRestrictions( $page, 'non-restrictable-action' ) );
1069 }
1070
1071 }