Debian Codegraph @

extensions/AbuseFilter/tests/phpunit/unit/AbuseFilterPermissionManagerTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\AbuseFilter\Tests\Unit;
4
5 use Generator;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\Config\ServiceOptions;
8 use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
9 use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter;
10 use MediaWiki\Extension\AbuseFilter\Filter\Flags;
11 use MediaWiki\Extension\AbuseFilter\Filter\MutableFilter;
12 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
13 use MediaWiki\User\Options\StaticUserOptionsLookup;
14 use MediaWiki\User\UserIdentityValue;
15 use MediaWikiUnitTestCase;
16
17 /**
18 * @covers \MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager
19 */
20 class AbuseFilterPermissionManagerTest extends MediaWikiUnitTestCase {
21 use MockAuthorityTrait;
22
23 private function getPermMan(): AbuseFilterPermissionManager {
24 $userOptions = new StaticUserOptionsLookup(
25 [
26 'User1' => [
27 'abusefilter-protected-vars-view-agreement' => 1
28 ],
29 'User2' => [
30 'abusefilter-protected-vars-view-agreement' => ''
31 ],
32 ]
33 );
34 return new AbuseFilterPermissionManager(
35 new ServiceOptions(
36 AbuseFilterPermissionManager::CONSTRUCTOR_OPTIONS,
37 [
38 'AbuseFilterProtectedVariables' => [ 'user_unnamed_ip' ]
39 ]
40 ),
41 $userOptions
42 );
43 }
44
45 public function provideCanEdit(): Generator {
46 $sitewideBlock = $this->createMock( DatabaseBlock::class );
47 $sitewideBlock->method( 'isSiteWide' )->willReturn( true );
48 yield 'blocked sitewide' => [ $sitewideBlock, [], false ];
49
50 $partialBlock = $this->createMock( DatabaseBlock::class );
51 $partialBlock->method( 'isSiteWide' )->willReturn( false );
52 yield 'partially blocked' => [ $partialBlock, [], false ];
53
54 yield 'unblocked, no right' => [ null, [], false ];
55
56 yield 'success' => [ null, [ 'abusefilter-modify' ], true ];
57 }
58
59 /**
60 * @param ?DatabaseBlock $block
61 * @param array $rights
62 * @param bool $expected
63 * @dataProvider provideCanEdit
64 */
65 public function testCanEdit( ?DatabaseBlock $block, array $rights, bool $expected ) {
66 if ( $block !== null ) {
67 $performer = $this->mockUserAuthorityWithBlock(
68 $this->mockRegisteredUltimateAuthority()->getUser(),
69 $block,
70 $rights
71 );
72 } else {
73 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
74 }
75 $this->assertSame(
76 $expected,
77 $this->getPermMan()->canEdit( $performer )
78 );
79 }
80
81 public static function provideCanEditGlobal(): Generator {
82 yield 'not allowed' => [ [], false ];
83 yield 'allowed' => [ [ 'abusefilter-modify-global' ], true ];
84 }
85
86 /**
87 * @dataProvider provideCanEditGlobal
88 */
89 public function testCanEditGlobal( array $rights, bool $expected ) {
90 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
91 $this->assertSame(
92 $expected,
93 $this->getPermMan()->canEditGlobal( $performer )
94 );
95 }
96
97 public function provideCanEditFilter(): Generator {
98 $localFilter = MutableFilter::newDefault();
99 $localFilter->setGlobal( false );
100 $globalFilter = MutableFilter::newDefault();
101 $globalFilter->setGlobal( true );
102 foreach ( $this->provideCanEdit() as $name => $editArgs ) {
103 foreach ( self::provideCanEditGlobal() as $allowed => $globalArgs ) {
104 yield "can edit: $name; can edit global: $allowed; local filter" => [
105 $localFilter,
106 $editArgs[0],
107 array_merge( $editArgs[1], $globalArgs[0] ),
108 $editArgs[2]
109 ];
110 yield "can edit: $name; can edit global: $allowed; global filter" => [
111 $globalFilter,
112 $editArgs[0],
113 array_merge( $editArgs[1], $globalArgs[0] ),
114 $editArgs[2] && $globalArgs[1]
115 ];
116 }
117 }
118 }
119
120 /**
121 * @param AbstractFilter $filter
122 * @param ?DatabaseBlock $block
123 * @param array $rights
124 * @param bool $expected
125 * @dataProvider provideCanEditFilter
126 */
127 public function testCanEditFilter(
128 AbstractFilter $filter,
129 ?DatabaseBlock $block,
130 array $rights,
131 bool $expected
132 ) {
133 if ( $block !== null ) {
134 $performer = $this->mockUserAuthorityWithBlock(
135 $this->mockRegisteredUltimateAuthority()->getUser(),
136 $block,
137 $rights
138 );
139 } else {
140 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
141 }
142 $this->assertSame(
143 $expected,
144 $this->getPermMan()->canEditFilter( $performer, $filter )
145 );
146 }
147
148 public static function provideCanViewPrivateFilters(): Generator {
149 yield 'not privileged' => [ [], false ];
150 yield 'modify' => [ [ 'abusefilter-modify' ], true ];
151 yield 'private' => [ [ 'abusefilter-view-private' ], true ];
152 yield 'both' => [ [ 'abusefilter-modify', 'abusefilter-view-private' ], true ];
153 }
154
155 /**
156 * @dataProvider provideCanViewPrivateFilters
157 */
158 public function testCanViewPrivateFilters( array $rights, bool $expected ) {
159 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
160 $this->assertSame(
161 $expected,
162 $this->getPermMan()->canViewPrivateFilters( $performer )
163 );
164 }
165
166 public static function provideCanViewPrivateFiltersLogs(): Generator {
167 yield 'not privileged' => [ [], false ];
168 yield 'can view private' => [ [ 'abusefilter-view-private' ], true ];
169 yield 'can view logs' => [ [ 'abusefilter-log-private' ], true ];
170 yield 'both' => [ [ 'abusefilter-view-private', 'abusefilter-log-private' ], true ];
171 }
172
173 /**
174 * @param array $rights
175 * @param bool $expected
176 * @dataProvider provideCanViewPrivateFiltersLogs
177 */
178 public function testCanViewPrivateFiltersLogs( array $rights, bool $expected ) {
179 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
180 $this->assertSame(
181 $expected,
182 $this->getPermMan()->canViewPrivateFiltersLogs( $performer )
183 );
184 }
185
186 public static function provideCanSeeLogDetailsForFilter(): Generator {
187 $details = [ 0 => 'abusefilter-log-detail' ];
188 $private = [ 1 => 'abusefilter-log-private' ];
189 $protected = [ 2 => 'abusefilter-access-protected-vars' ];
190
191 yield 'filter hidden, not privileged' => [ Flags::FILTER_HIDDEN, [], false ];
192 yield 'filter hidden, details only' => [ Flags::FILTER_HIDDEN, $details, false ];
193 yield 'filter hidden, private logs only' => [ Flags::FILTER_HIDDEN, $private, false ];
194 yield 'filter hidden, details and private logs' => [ Flags::FILTER_HIDDEN, $details + $private, true ];
195
196 yield 'filter protected, not privileged' => [ Flags::FILTER_USES_PROTECTED_VARS, [], false ];
197 yield 'filter protected, details only' => [ Flags::FILTER_USES_PROTECTED_VARS, $details, false ];
198 yield 'filter protected, protected logs only' => [ Flags::FILTER_USES_PROTECTED_VARS, $protected, false ];
199 yield 'filter protected, privileged' => [ Flags::FILTER_USES_PROTECTED_VARS, $details + $protected, true ];
200
201 $hiddenProtected = Flags::FILTER_HIDDEN | Flags::FILTER_USES_PROTECTED_VARS;
202 yield 'filter hidden and protected, not privileged' => [ $hiddenProtected, [], false ];
203 yield 'filter hidden and protected, details only' => [ $hiddenProtected, $details, false ];
204 yield 'filter hidden and protected, private only' => [ $hiddenProtected, $private, false ];
205 yield 'filter hidden and protected, protected only' => [ $hiddenProtected, $protected, false ];
206 yield 'filter hidden and protected, details and private only' => [
207 $hiddenProtected, $details + $private, false
208 ];
209 yield 'filter hidden and protected, details and protected only' => [
210 $hiddenProtected, $details + $protected, false
211 ];
212 yield 'filter hidden and protected, private and protected only' => [
213 $hiddenProtected, $private + $protected, false
214 ];
215 yield 'filter hidden and protected, privileged' => [
216 $hiddenProtected, $details + $private + $protected, true
217 ];
218
219 yield 'filter visible, not privileged' => [ Flags::FILTER_PUBLIC, [], false ];
220 yield 'filter visible, privileged' => [ Flags::FILTER_PUBLIC, $details, true ];
221 }
222
223 /**
224 * @param int $privacyLevel
225 * @param array $rights
226 * @param bool $expected
227 * @dataProvider provideCanSeeLogDetailsForFilter
228 */
229 public function testCanSeeLogDetailsForFilter( int $privacyLevel, array $rights, bool $expected ) {
230 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
231 $this->assertSame(
232 $expected,
233 $this->getPermMan()->canSeeLogDetailsForFilter( $performer, $privacyLevel )
234 );
235 }
236
237 public function provideCanViewProtectedVariables(): Generator {
238 $block = $this->createMock( DatabaseBlock::class );
239 $block->method( 'isSiteWide' )->willReturn( true );
240 yield 'not privileged, blocked' => [ $block, [], false ];
241 yield 'not privileged, not blocked' => [ null, [], false ];
242 yield 'has right, blocked' => [ $block, [ 'abusefilter-access-protected-vars' ], false ];
243 yield 'has right, not blocked' => [ null, [ 'abusefilter-access-protected-vars' ], true ];
244 }
245
246 /**
247 * @dataProvider provideCanViewProtectedVariables
248 */
249 public function testCanViewProtectedVariables( ?DatabaseBlock $block, array $rights, bool $expected ) {
250 if ( $block !== null ) {
251 $performer = $this->mockUserAuthorityWithBlock(
252 $this->mockRegisteredUltimateAuthority()->getUser(),
253 $block,
254 $rights
255 );
256 } else {
257 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
258 }
259
260 $this->assertSame(
261 $expected,
262 $this->getPermMan()->canViewProtectedVariables( $performer )
263 );
264 }
265
266 public function provideCanViewProtectedVariableValues(): Generator {
267 $userCheckedPreference = new UserIdentityValue( 1, 'User1' );
268 $userUncheckedPreference = new UserIdentityValue( 2, 'User2' );
269 yield 'can view protected variables, has checked preference' => [
270 [ 'abusefilter-access-protected-vars' ], $userCheckedPreference, true
271 ];
272 yield 'can view protected variables, has not checked preference' => [
273 [ 'abusefilter-access-protected-vars' ], $userUncheckedPreference, false
274 ];
275 yield 'cannot view protected variables, has checked preference' => [
276 [], $userCheckedPreference, false
277 ];
278 yield 'cannot view protected variables, has not checked preference' => [
279 [], $userUncheckedPreference, false
280 ];
281 }
282
283 /**
284 * @dataProvider provideCanViewProtectedVariableValues
285 */
286 public function testCanViewProtectedVariableValues( array $rights, UserIdentityValue $user, bool $expected ) {
287 $performer = $this->mockUserAuthorityWithPermissions( $user, $rights );
288 $this->assertSame(
289 $expected,
290 $this->getPermMan()->canViewProtectedVariableValues( $performer )
291 );
292 }
293
294 public function provideTestGetUsedProtectedVariables(): Generator {
295 $userCheckedPreference = new UserIdentityValue( 1, 'User1' );
296 $userUncheckedPreference = new UserIdentityValue( 2, 'User2' );
297 yield 'uses protected variables' => [
298 [ 'user_unnamed_ip', 'user_name' ], [ 'user_unnamed_ip' ]
299 ];
300 yield 'no protected variables' => [
301 [ 'user_name' ], []
302 ];
303 }
304
305 /**
306 * @dataProvider provideTestGetUsedProtectedVariables
307 */
308 public function testGetUsedProtectedVariables( array $usedVariables, $expected ) {
309 $this->assertSame(
310 $expected,
311 $this->getPermMan()->getUsedProtectedVariables( $usedVariables )
312 );
313 }
314
315 public static function provideGetForbiddenVariables(): Generator {
316 yield 'cannot view, protected vars' => [
317 [
318 'rights' => [],
319 'usedVars' => [ 'user_unnamed_ip' ]
320 ],
321 [ 'user_unnamed_ip' ]
322 ];
323 yield 'cannot view, no protected vars' => [
324 [
325 'rights' => [],
326 'usedVars' => []
327 ],
328 []
329 ];
330 yield 'can view, protected vars' => [
331 [
332 'rights' => [ 'abusefilter-access-protected-vars' ],
333 'usedVars' => [ 'user_unnamed_ip' ]
334 ],
335 []
336 ];
337 yield 'can view, no protected vars' => [
338 [
339 'rights' => [ 'abusefilter-access-protected-vars' ],
340 'usedVars' => []
341 ],
342 []
343 ];
344 }
345
346 /**
347 * @dataProvider provideGetForbiddenVariables
348 */
349 public function testGetForbiddenVariables( array $data, $expected ) {
350 $performer = $this->mockRegisteredAuthorityWithPermissions( $data[ 'rights' ] );
351 $this->assertSame(
352 $expected,
353 $this->getPermMan()->getForbiddenVariables( $performer, $data[ 'usedVars' ] )
354 );
355 }
356
357 public function testGetProtectedVariables() {
358 $this->assertSame(
359 [ 'user_unnamed_ip' ],
360 $this->getPermMan()->getProtectedVariables()
361 );
362 }
363
364 public static function provideSimpleCases(): array {
365 return [
366 'not allowed' => [ false ],
367 'allowed' => [ true ],
368 ];
369 }
370
371 /**
372 * @dataProvider provideSimpleCases
373 */
374 public function testCanEditFilterWithRestrictedActions( bool $allowed ) {
375 $rights = $allowed ? [ 'abusefilter-modify-restricted' ] : [];
376 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
377 $this->assertSame(
378 $allowed,
379 $this->getPermMan()->canEditFilterWithRestrictedActions( $performer )
380 );
381 }
382
383 /**
384 * @dataProvider provideSimpleCases
385 */
386 public function testCanViewAbuseLog( bool $allowed ) {
387 $rights = $allowed ? [ 'abusefilter-log' ] : [];
388 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
389 $this->assertSame(
390 $allowed,
391 $this->getPermMan()->canViewAbuseLog( $performer )
392 );
393 }
394
395 /**
396 * @dataProvider provideSimpleCases
397 */
398 public function testCanHideAbuseLog( bool $allowed ) {
399 $rights = $allowed ? [ 'abusefilter-hide-log' ] : [];
400 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
401 $this->assertSame(
402 $allowed,
403 $this->getPermMan()->canHideAbuseLog( $performer )
404 );
405 }
406
407 /**
408 * @dataProvider provideSimpleCases
409 */
410 public function testCanRevertFilterActions( bool $allowed ) {
411 $rights = $allowed ? [ 'abusefilter-revert' ] : [];
412 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
413 $this->assertSame(
414 $allowed,
415 $this->getPermMan()->canRevertFilterActions( $performer )
416 );
417 }
418
419 /**
420 * @dataProvider provideSimpleCases
421 */
422 public function testCanSeeLogDetails( bool $allowed ) {
423 $rights = $allowed ? [ 'abusefilter-log-detail' ] : [];
424 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
425 $this->assertSame(
426 $allowed,
427 $this->getPermMan()->canSeeLogDetails( $performer )
428 );
429 }
430
431 /**
432 * @dataProvider provideSimpleCases
433 */
434 public function testCanSeePrivateDetails( bool $allowed ) {
435 $rights = $allowed ? [ 'abusefilter-privatedetails' ] : [];
436 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
437 $this->assertSame(
438 $allowed,
439 $this->getPermMan()->canSeePrivateDetails( $performer )
440 );
441 }
442
443 /**
444 * @dataProvider provideSimpleCases
445 */
446 public function testCanSeeHiddenLogEntries( bool $allowed ) {
447 $rights = $allowed ? [ 'abusefilter-hidden-log' ] : [];
448 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
449 $this->assertSame(
450 $allowed,
451 $this->getPermMan()->canSeeHiddenLogEntries( $performer )
452 );
453 }
454
455 /**
456 * @dataProvider provideSimpleCases
457 */
458 public function testCanUseTestTools( bool $allowed ) {
459 $rights = $allowed ? [ 'abusefilter-modify' ] : [];
460 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
461 $this->assertSame(
462 $allowed,
463 $this->getPermMan()->canUseTestTools( $performer )
464 );
465 }
466
467 }