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