Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Tests\Unit\Permissions;
22
23 use InvalidArgumentException;
24 use MediaWiki\Block\AbstractBlock;
25 use MediaWiki\Page\PageIdentity;
26 use MediaWiki\Page\PageIdentityValue;
27 use MediaWiki\Permissions\PermissionStatus;
28 use MediaWiki\Permissions\RateLimiter;
29 use MediaWiki\Status\StatusFormatter;
30 use MediaWiki\Tests\Unit\FakeQqxMessageLocalizer;
31 use MediaWiki\User\User;
32 use MediaWikiUnitTestCase;
33 use Psr\Log\NullLogger;
34
35 /**
36 * @covers \MediaWiki\Permissions\UserAuthority
37 */
38 class UserAuthorityTest extends MediaWikiUnitTestCase {
39
40 use MockAuthorityTrait;
41
42 public function testGetUser() {
43 $user = $this->newUser();
44 $authority = $this->newUserAuthority( [ 'actor' => $user ] );
45
46 $this->assertSame( $user, $authority->getUser() );
47 }
48
49 public function testGetUserBlockNotBlocked() {
50 $authority = $this->newUserAuthority();
51 $this->assertNull( $authority->getBlock() );
52 }
53
54 public function testGetUserBlockWasBlocked() {
55 $block = $this->createNoOpMock( AbstractBlock::class );
56 $user = $this->newUser( $block );
57
58 $authority = $this->newUserAuthority( [ 'actor' => $user ] );
59 $this->assertSame( $block, $authority->getBlock() );
60 }
61
62 public function testRateLimitApplies() {
63 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
64 $authority = $this->newUserAuthority( [ 'permissions' => [ 'edit' ], 'limited' => true ] );
65
66 $this->assertTrue( $authority->isAllowed( 'edit' ) );
67 $this->assertTrue( $authority->probablyCan( 'edit', $target ) );
68
69 $this->assertFalse( $authority->isDefinitelyAllowed( 'edit' ) );
70 $this->assertFalse( $authority->definitelyCan( 'edit', $target ) );
71
72 $this->assertFalse( $authority->authorizeRead( 'edit', $target ) );
73 $this->assertFalse( $authority->authorizeWrite( 'edit', $target ) );
74 $this->assertFalse( $authority->authorizeAction( 'edit' ) );
75 }
76
77 public function testRateLimiterBypassedForReading() {
78 $permissionManager = $this->newPermissionsManager( [ 'read' ] );
79
80 // Key assertion: limit() is not called.
81 $rateLimiter = $this->createNoOpMock( RateLimiter::class, [ 'isLimitable' ] );
82 $rateLimiter->method( 'isLimitable' )->willReturn( false );
83
84 // Key assertion: toRateLimitSubject() is not called.
85 $actor = $this->createNoOpMock( User::class, [ 'getBlock', 'isNewbie' ] );
86 $actor->method( 'getBlock' )->willReturn( null );
87 $actor->method( 'isNewbie' )->willReturn( false );
88
89 $authority = $this->newUserAuthority( [
90 'actor' => $actor,
91 'permissionManager' => $permissionManager,
92 'rateLimiter' => $rateLimiter
93 ] );
94
95 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
96 $this->assertTrue( $authority->authorizeRead( 'read', $target ) );
97 }
98
99 /**
100 * @covers \MediaWiki\Permissions\UserAuthority::limit
101 */
102 public function testPingLimiterCaching() {
103 $permissionManager = $this->newPermissionsManager( [] );
104 $rateLimiter = $this->createNoOpMock( RateLimiter::class, [ 'limit', 'isLimitable' ] );
105
106 $rateLimiter->method( 'isLimitable' )
107 ->willReturn( true );
108
109 // We expect exactly five calls to go through to the RateLimiter,
110 // see the comments below.
111 $rateLimiter->expects( $this->exactly( 5 ) )
112 ->method( 'limit' )
113 ->willReturn( false );
114
115 $authority = $this->newUserAuthority( [
116 'permissionManager' => $permissionManager,
117 'rateLimiter' => $rateLimiter
118 ] );
119
120 // The rate limit cache is usually disabled during testing.
121 // Enable it so we can test it.
122 $authority->setUseLimitCache( true );
123
124 // The first call should go through to the RateLimiter (count 1).
125 $this->assertFalse( $authority->limit( 'edit', 0, null ) );
126
127 // The second call should also go through to the RateLimiter,
128 // because now we are incrementing, and before we were just peeking (count 2).
129 $this->assertFalse( $authority->limit( 'edit', 1, null ) );
130
131 // The third call should hit the cache
132 $this->assertFalse( $authority->limit( 'edit', 0, null ) );
133
134 // The forth call should hit the cache, even if incrementing.
135 // This makes sure we don't increment the same counter multiple times
136 // during a single request.
137 $this->assertFalse( $authority->limit( 'edit', 1, null ) );
138
139 // The fifth call should go to the RateLimiter again, because we are now
140 // incrementing by more than one (count 3).
141 $this->assertFalse( $authority->limit( 'edit', 5, null ) );
142
143 // The next calls should not go through, since we already hit 5
144 $this->assertFalse( $authority->limit( 'edit', 5, null ) );
145 $this->assertFalse( $authority->limit( 'edit', 2, null ) );
146 $this->assertFalse( $authority->limit( 'edit', 0, null ) );
147
148 // When limiting another action, we should not hit the cache (count 4).
149 $this->assertFalse( $authority->limit( 'move', 1, null ) );
150
151 // After disabling the cache, we should get through to the RateLimiter again (count 5).
152 $authority->setUseLimitCache( false );
153 $this->assertFalse( $authority->limit( 'move', 1, null ) );
154 }
155
156 public function testBlockedUserCanRead() {
157 $block = $this->createNoOpMock( AbstractBlock::class );
158 $user = $this->newUser( $block );
159
160 $authority = $this->newUserAuthority(
161 [ 'permissions' => [ 'read', 'edit' ], 'actor' => $user ]
162 );
163
164 $status = PermissionStatus::newEmpty();
165 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
166 $this->assertTrue( $authority->authorizeRead( 'read', $target, $status ) );
167 $this->assertStatusOK( $status );
168 }
169
170 public function testBlockedUserCanNotWrite() {
171 $block = $this->createNoOpMock( AbstractBlock::class );
172 $user = $this->newUser( $block );
173
174 $authority = $this->newUserAuthority(
175 [ 'permissions' => [ 'read', 'edit' ], 'actor' => $user ]
176 );
177
178 $status = PermissionStatus::newEmpty();
179 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
180 $this->assertFalse( $authority->authorizeWrite( 'edit', $target, $status ) );
181 $this->assertStatusNotOK( $status );
182 $this->assertSame( 'edit', $status->getPermission() );
183 $this->assertSame( $block, $status->getBlock() );
184 }
185
186 public function testBlockedUserAction() {
187 $block = $this->createNoOpMock( AbstractBlock::class );
188 $user = $this->newUser( $block );
189
190 $authority = $this->newUserAuthority(
191 [ 'permissions' => [ 'read', 'blocked' ], 'actor' => $user ]
192 );
193
194 $status = PermissionStatus::newEmpty();
195 $this->assertTrue( $authority->isAllowed( 'blocked' ) );
196 $this->assertFalse( $authority->isDefinitelyAllowed( 'blocked' ) );
197 $this->assertFalse( $authority->authorizeAction( 'blocked', $status ) );
198 $this->assertStatusNotOK( $status );
199 $this->assertSame( 'blocked', $status->getPermission() );
200 }
201
202 public function testPermissions() {
203 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
204
205 $this->assertTrue( $authority->isAllowed( 'foo' ) );
206 $this->assertTrue( $authority->isAllowed( 'bar' ) );
207 $this->assertFalse( $authority->isAllowed( 'quux' ) );
208
209 $this->assertTrue( $authority->isAllowedAll( 'foo', 'bar' ) );
210 $this->assertTrue( $authority->isAllowedAny( 'bar', 'quux' ) );
211
212 $this->assertFalse( $authority->isAllowedAll( 'foo', 'quux' ) );
213 $this->assertFalse( $authority->isAllowedAny( 'xyzzy', 'quux' ) );
214 }
215
216 public function testIsAllowed() {
217 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
218
219 $this->assertTrue( $authority->isAllowed( 'foo' ) );
220 $this->assertTrue( $authority->isAllowed( 'bar' ) );
221 $this->assertFalse( $authority->isAllowed( 'quux' ) );
222
223 $status = new PermissionStatus();
224 $authority->isAllowed( 'foo', $status );
225 $this->assertStatusOK( $status );
226
227 $authority->isAllowed( 'quux', $status );
228 $this->assertStatusNotOK( $status );
229 $this->assertSame( 'quux', $status->getPermission() );
230 }
231
232 public function testProbablyCan() {
233 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
234 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
235
236 $this->assertTrue( $authority->probablyCan( 'foo', $target ) );
237 $this->assertTrue( $authority->probablyCan( 'bar', $target ) );
238 $this->assertFalse( $authority->probablyCan( 'quux', $target ) );
239
240 $status = new PermissionStatus();
241 $authority->probablyCan( 'foo', $target, $status );
242 $this->assertStatusOK( $status );
243
244 $authority->probablyCan( 'quux', $target, $status );
245 $this->assertStatusNotOK( $status );
246 $this->assertSame( 'quux', $status->getPermission() );
247 }
248
249 public function testIsDefinitelyAllowed() {
250 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
251
252 $this->assertTrue( $authority->isDefinitelyAllowed( 'foo' ) );
253 $this->assertTrue( $authority->isDefinitelyAllowed( 'bar' ) );
254 $this->assertFalse( $authority->isDefinitelyAllowed( 'quux' ) );
255
256 $status = new PermissionStatus();
257 $authority->isDefinitelyAllowed( 'foo', $status );
258 $this->assertStatusOK( $status );
259
260 $authority->isDefinitelyAllowed( 'quux', $status );
261 $this->assertStatusNotOK( $status );
262 $this->assertSame( 'quux', $status->getPermission() );
263 }
264
265 public function testDefinitelyCan() {
266 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
267 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
268
269 $this->assertTrue( $authority->definitelyCan( 'foo', $target ) );
270 $this->assertTrue( $authority->definitelyCan( 'bar', $target ) );
271 $this->assertFalse( $authority->definitelyCan( 'quux', $target ) );
272
273 $status = new PermissionStatus();
274 $authority->definitelyCan( 'foo', $target, $status );
275 $this->assertStatusOK( $status );
276
277 $authority->definitelyCan( 'quux', $target, $status );
278 $this->assertStatusNotOK( $status );
279 $this->assertSame( 'quux', $status->getPermission() );
280 }
281
282 public function testAuthorize() {
283 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
284
285 $this->assertTrue( $authority->authorizeAction( 'foo' ) );
286 $this->assertTrue( $authority->authorizeAction( 'bar' ) );
287 $this->assertFalse( $authority->authorizeAction( 'quux' ) );
288
289 $status = new PermissionStatus();
290 $authority->authorizeAction( 'foo', $status );
291 $this->assertStatusOK( $status );
292
293 $authority->authorizeAction( 'quux', $status );
294 $this->assertStatusNotOK( $status );
295 $this->assertSame( 'quux', $status->getPermission() );
296 }
297
298 public function testAuthorizeRead() {
299 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
300 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
301
302 $this->assertTrue( $authority->authorizeRead( 'foo', $target ) );
303 $this->assertTrue( $authority->authorizeRead( 'bar', $target ) );
304 $this->assertFalse( $authority->authorizeRead( 'quux', $target ) );
305
306 $status = new PermissionStatus();
307 $authority->authorizeRead( 'foo', $target, $status );
308 $this->assertStatusOK( $status );
309
310 $authority->authorizeRead( 'quux', $target, $status );
311 $this->assertStatusNotOK( $status );
312 }
313
314 public function testAuthorizeWrite() {
315 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
316 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
317
318 $this->assertTrue( $authority->authorizeWrite( 'foo', $target ) );
319 $this->assertTrue( $authority->authorizeWrite( 'bar', $target ) );
320 $this->assertFalse( $authority->authorizeWrite( 'quux', $target ) );
321
322 $status = new PermissionStatus();
323 $authority->authorizeWrite( 'foo', $target, $status );
324 $this->assertStatusOK( $status );
325
326 $authority->authorizeWrite( 'quux', $target, $status );
327 $this->assertStatusNotOK( $status );
328 }
329
330 public function testIsAllowedAnyThrowsOnEmptySet() {
331 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
332
333 $this->expectException( InvalidArgumentException::class );
334 $authority->isAllowedAny();
335 }
336
337 public function testIsAllowedAllThrowsOnEmptySet() {
338 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ] ] );
339
340 $this->expectException( InvalidArgumentException::class );
341 $authority->isAllowedAll();
342 }
343
344 public function testGetBlock_none() {
345 $actor = $this->newUser();
346
347 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ], 'actor' => $actor ] );
348
349 $this->assertNull( $authority->getBlock() );
350 }
351
352 public function testGetBlock_blocked() {
353 $block = $this->createNoOpMock( AbstractBlock::class );
354 $actor = $this->newUser( $block );
355
356 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ], 'actor' => $actor ] );
357
358 $this->assertSame( $block, $authority->getBlock() );
359 }
360
361 /**
362 * Regression test for T306494: check that when creating a PermissionStatus,
363 * the message contains all parameters and when converted to a Status, the parameters
364 * are not wikitext escaped.
365 */
366 public function testInternalCanWithPermissionStatusMessageFormatting() {
367 $block = $this->createNoOpMock( AbstractBlock::class );
368 $user = $this->newUser( $block );
369
370 $authority = $this->newUserAuthority( [ 'permissions' => [ 'foo', 'bar' ], 'actor' => $user, 'limited' => true ] );
371
372 $permissionStatus = PermissionStatus::newEmpty();
373 $target = new PageIdentityValue( 321, NS_MAIN, __METHOD__, PageIdentity::LOCAL );
374
375 $authority->authorizeWrite(
376 'edit',
377 $target,
378 $permissionStatus
379 );
380
381 $this->assertStatusError( 'actionthrottledtext', $permissionStatus );
382 $this->assertTrue( $permissionStatus->isRateLimitExceeded() );
383
384 $this->assertStatusError( 'blockedtext-partial', $permissionStatus );
385 $this->assertNotNull( $permissionStatus->getBlock() );
386
387 $formatter = new StatusFormatter(
388 new FakeQqxMessageLocalizer(),
389 $this->createNoOpMock( \MessageCache::class ),
390 new NullLogger()
391 );
392 // Despite all the futzing around with services, StatusFormatter depends on this global through wfEscapeWikiText
393 global $wgEnableMagicLinks;
394 $old = $wgEnableMagicLinks;
395 $wgEnableMagicLinks = [];
396
397 try {
398 $wikitext = $formatter->getWikiText( $permissionStatus );
399 // Assert that the formatted wikitext contains the original values of the message parameters,
400 // rather than escaped ones
401 foreach ( $this->getFakeBlockMessageParams() as $param ) {
402 $this->assertStringContainsString( $param, $wikitext );
403 }
404 } finally {
405 $wgEnableMagicLinks = $old;
406 }
407 }
408 }