| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Unit\Permissions; |
| 4 |
|
| 5 |
use MediaWiki\Block\Block; |
| 6 |
use MediaWiki\Block\BlockErrorFormatter; |
| 7 |
use MediaWiki\Block\SystemBlock; |
| 8 |
use MediaWiki\Context\IContextSource; |
| 9 |
use MediaWiki\Language\Language; |
| 10 |
use MediaWiki\Message\Message; |
| 11 |
use MediaWiki\Permissions\Authority; |
| 12 |
use MediaWiki\Permissions\PermissionManager; |
| 13 |
use MediaWiki\Permissions\PermissionStatus; |
| 14 |
use MediaWiki\Permissions\RateLimiter; |
| 15 |
use MediaWiki\Permissions\RateLimitSubject; |
| 16 |
use MediaWiki\Permissions\SimpleAuthority; |
| 17 |
use MediaWiki\Permissions\UltimateAuthority; |
| 18 |
use MediaWiki\Permissions\UserAuthority; |
| 19 |
use MediaWiki\Request\FauxRequest; |
| 20 |
use MediaWiki\Request\WebRequest; |
| 21 |
use MediaWiki\User\User; |
| 22 |
use MediaWiki\User\UserIdentity; |
| 23 |
use MediaWiki\User\UserIdentityValue; |
| 24 |
use PHPUnit\Framework\MockObject\MockObject; |
| 25 |
use StatusValue; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
trait MockAuthorityTrait { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private function mockAnonUltimateAuthority(): Authority { |
| 39 |
return new UltimateAuthority( new UserIdentityValue( 0, '127.0.0.1' ) ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private function mockTempUltimateAuthority(): Authority { |
| 48 |
return new UltimateAuthority( new UserIdentityValue( 42, '~2024-1' ), true ); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private function mockRegisteredUltimateAuthority(): Authority { |
| 57 |
return new UltimateAuthority( new UserIdentityValue( 9999, 'Petr' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private function mockAnonNullAuthority(): Authority { |
| 66 |
return new SimpleAuthority( new UserIdentityValue( 0, '127.0.0.1' ), [] ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private function mockTempNullAuthority(): Authority { |
| 75 |
return new SimpleAuthority( new UserIdentityValue( 42, '~2024-1' ), [], true ); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private function mockRegisteredNullAuthority(): Authority { |
| 84 |
return new SimpleAuthority( new UserIdentityValue( 9999, 'Petr' ), [] ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
private function mockAnonAuthorityWithPermissions( array $permissions ): Authority { |
| 94 |
return new SimpleAuthority( new UserIdentityValue( 0, '127.0.0.1' ), $permissions ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private function mockTempAuthorityWithPermissions( array $permissions ): Authority { |
| 104 |
return new SimpleAuthority( new UserIdentityValue( 42, '~2024-1' ), $permissions, true ); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
private function mockRegisteredAuthorityWithPermissions( array $permissions ): Authority { |
| 114 |
return new SimpleAuthority( new UserIdentityValue( 9999, 'Petr' ), $permissions ); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
private function mockUserAuthorityWithPermissions( |
| 126 |
UserIdentity $user, |
| 127 |
array $permissions, |
| 128 |
bool $isTemp = false |
| 129 |
): Authority { |
| 130 |
return new SimpleAuthority( $user, $permissions, $isTemp ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
private function mockUserAuthorityWithBlock( |
| 144 |
UserIdentity $user, |
| 145 |
Block $block, |
| 146 |
array $permissions = [], |
| 147 |
bool $isTemp = false |
| 148 |
): Authority { |
| 149 |
return $this->mockAuthority( |
| 150 |
$user, |
| 151 |
static function ( $permission ) use ( $permissions ) { |
| 152 |
return in_array( $permission, $permissions ); |
| 153 |
}, |
| 154 |
$block, |
| 155 |
$isTemp |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
private function mockAnonAuthorityWithoutPermissions( array $permissions ): Authority { |
| 165 |
return $this->mockUserAuthorityWithoutPermissions( |
| 166 |
new UserIdentityValue( 0, '127.0.0.1' ), |
| 167 |
$permissions |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
private function mockTempAuthorityWithoutPermissions( array $permissions ): Authority { |
| 177 |
return $this->mockUserAuthorityWithoutPermissions( |
| 178 |
new UserIdentityValue( 42, '~2024-1' ), |
| 179 |
$permissions, |
| 180 |
true |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
private function mockRegisteredAuthorityWithoutPermissions( array $permissions ): Authority { |
| 190 |
return $this->mockUserAuthorityWithoutPermissions( |
| 191 |
new UserIdentityValue( 9999, 'Petr' ), |
| 192 |
$permissions |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
private function mockUserAuthorityWithoutPermissions( |
| 204 |
UserIdentity $user, |
| 205 |
array $permissions, |
| 206 |
bool $isTemp = false |
| 207 |
): Authority { |
| 208 |
return $this->mockAuthority( |
| 209 |
$user, |
| 210 |
static function ( $permission ) use ( $permissions ) { |
| 211 |
return !in_array( $permission, $permissions ); |
| 212 |
}, |
| 213 |
null, |
| 214 |
$isTemp |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private function mockAnonAuthority( callable $permissionCallback ): Authority { |
| 225 |
return $this->mockAuthority( |
| 226 |
new UserIdentityValue( 0, '127.0.0.1' ), |
| 227 |
$permissionCallback |
| 228 |
); |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
private function mockTempAuthority( callable $permissionCallback ): Authority { |
| 238 |
return $this->mockAuthority( |
| 239 |
new UserIdentityValue( 42, '~2024-1' ), |
| 240 |
$permissionCallback, |
| 241 |
null, |
| 242 |
true |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
private function mockRegisteredAuthority( callable $permissionCallback ): Authority { |
| 253 |
return $this->mockAuthority( |
| 254 |
new UserIdentityValue( 9999, 'Petr' ), |
| 255 |
$permissionCallback |
| 256 |
); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
private function mockAuthority( |
| 270 |
UserIdentity $user, |
| 271 |
callable $permissionCallback, |
| 272 |
?Block $block = null, |
| 273 |
bool $isTemp = false |
| 274 |
): Authority { |
| 275 |
$mock = $this->createMock( Authority::class ); |
| 276 |
$mock->method( 'getUser' )->willReturn( $user ); |
| 277 |
$methods = [ 'isAllowed', 'probablyCan', 'definitelyCan', 'authorizeRead', 'authorizeWrite' ]; |
| 278 |
foreach ( $methods as $method ) { |
| 279 |
$mock->method( $method )->willReturnCallback( $permissionCallback ); |
| 280 |
} |
| 281 |
$mock->method( 'isAllowedAny' ) |
| 282 |
->willReturnCallback( static function ( ...$permissions ) use ( $permissionCallback ) { |
| 283 |
foreach ( $permissions as $permission ) { |
| 284 |
if ( $permissionCallback( $permission ) ) { |
| 285 |
return true; |
| 286 |
} |
| 287 |
} |
| 288 |
return false; |
| 289 |
} ); |
| 290 |
$mock->method( 'isAllowedAll' ) |
| 291 |
->willReturnCallback( static function ( ...$permissions ) use ( $permissionCallback ) { |
| 292 |
foreach ( $permissions as $permission ) { |
| 293 |
if ( !$permissionCallback( $permission ) ) { |
| 294 |
return false; |
| 295 |
} |
| 296 |
} |
| 297 |
return true; |
| 298 |
} ); |
| 299 |
$mock->method( 'getBlock' )->willReturn( $block ); |
| 300 |
$mock->method( 'isTemp' )->willReturn( $isTemp ); |
| 301 |
$mock->method( 'isNamed' )->willReturn( $user->isRegistered() && !$isTemp ); |
| 302 |
return $mock; |
| 303 |
} |
| 304 |
|
| 305 |
|
| 306 |
private function getFakeBlockMessageParams(): array { |
| 307 |
return [ |
| 308 |
'[[User:Blocker|Blocker]]', |
| 309 |
'Block reason that can contain {{templates}}', |
| 310 |
'192.168.0.1', |
| 311 |
'Blocker', |
| 312 |
]; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private function newRateLimiter( $limited = false ): RateLimiter { |
| 320 |
|
| 321 |
$rateLimiter = $this->createNoOpMock( |
| 322 |
RateLimiter::class, |
| 323 |
[ 'limit', 'isLimitable' ] |
| 324 |
); |
| 325 |
|
| 326 |
$rateLimiter->method( 'limit' )->willReturn( $limited ); |
| 327 |
$rateLimiter->method( 'isLimitable' )->willReturn( true ); |
| 328 |
|
| 329 |
return $rateLimiter; |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
private function newPermissionsManager( array $permissions ): PermissionManager { |
| 337 |
|
| 338 |
$permissionManager = $this->createNoOpMock( |
| 339 |
PermissionManager::class, |
| 340 |
[ |
| 341 |
'userHasRight', |
| 342 |
'userHasAnyRight', |
| 343 |
'userHasAllRights', |
| 344 |
'userCan', |
| 345 |
'getPermissionStatus', |
| 346 |
'getPermissionErrors', |
| 347 |
'isBlockedFrom', |
| 348 |
'getApplicableBlock', |
| 349 |
'newFatalPermissionDeniedStatus', |
| 350 |
] |
| 351 |
); |
| 352 |
|
| 353 |
$permissionManager->method( 'userHasRight' )->willReturnCallback( |
| 354 |
static function ( $user, $permission ) use ( $permissions ) { |
| 355 |
return in_array( $permission, $permissions ); |
| 356 |
} |
| 357 |
); |
| 358 |
|
| 359 |
$permissionManager->method( 'userHasAnyRight' )->willReturnCallback( |
| 360 |
static function ( $user, ...$actions ) use ( $permissions ) { |
| 361 |
return array_diff( $actions, $permissions ) != $actions; |
| 362 |
} |
| 363 |
); |
| 364 |
|
| 365 |
$permissionManager->method( 'userHasAllRights' )->willReturnCallback( |
| 366 |
static function ( $user, ...$actions ) use ( $permissions ) { |
| 367 |
return !array_diff( $actions, $permissions ); |
| 368 |
} |
| 369 |
); |
| 370 |
|
| 371 |
$permissionManager->method( 'userCan' )->willReturnCallback( |
| 372 |
static function ( $permission, $user ) use ( $permissionManager ) { |
| 373 |
return $permissionManager->userHasRight( $user, $permission ); |
| 374 |
} |
| 375 |
); |
| 376 |
|
| 377 |
$fakeBlockMessageParams = $this->getFakeBlockMessageParams(); |
| 378 |
|
| 379 |
$permissionManager->method( 'getPermissionStatus' )->willReturnCallback( |
| 380 |
static function ( $permission, $user, $target ) use ( $permissionManager, $fakeBlockMessageParams ) { |
| 381 |
$status = PermissionStatus::newEmpty(); |
| 382 |
if ( !$permissionManager->userCan( $permission, $user, $target ) ) { |
| 383 |
$status->fatal( 'permissionserrors' ); |
| 384 |
} |
| 385 |
if ( $user->getBlock() && $permission !== 'read' ) { |
| 386 |
$status->fatal( 'blockedtext-partial', ...$fakeBlockMessageParams ); |
| 387 |
} |
| 388 |
return $status; |
| 389 |
} |
| 390 |
); |
| 391 |
|
| 392 |
$permissionManager->method( 'getPermissionErrors' )->willReturnCallback( |
| 393 |
static function ( $permission, $user, $target ) use ( $permissionManager, $fakeBlockMessageParams ) { |
| 394 |
return $permissionManager |
| 395 |
->getPermissionStatus( $permission, $user, $target ) |
| 396 |
->toLegacyErrorArray(); |
| 397 |
} |
| 398 |
); |
| 399 |
|
| 400 |
$permissionManager->method( 'newFatalPermissionDeniedStatus' )->willReturnCallback( |
| 401 |
static function ( $permission, $context ) use ( $permissionManager ) { |
| 402 |
return StatusValue::newFatal( 'permissionserrors' ); |
| 403 |
} |
| 404 |
); |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
$permissionManager->method( 'getApplicableBlock' )->willReturnCallback( |
| 409 |
static function ( $action, User $user, $rigor, $page ) { |
| 410 |
if ( $page && $page->getDBkey() === 'Forbidden' ) { |
| 411 |
return new SystemBlock(); |
| 412 |
} |
| 413 |
|
| 414 |
if ( $action === 'blocked' ) { |
| 415 |
return new SystemBlock(); |
| 416 |
} |
| 417 |
|
| 418 |
return null; |
| 419 |
} |
| 420 |
); |
| 421 |
|
| 422 |
$permissionManager->method( 'isBlockedFrom' )->willReturnCallback( |
| 423 |
static function ( User $user, $page ) { |
| 424 |
return $page->getDBkey() === 'Forbidden'; |
| 425 |
} |
| 426 |
); |
| 427 |
|
| 428 |
return $permissionManager; |
| 429 |
} |
| 430 |
|
| 431 |
private function newUser( ?Block $block = null, bool $isTemp = false ): User { |
| 432 |
|
| 433 |
$actor = $this->createNoOpMock( User::class, [ 'getBlock', 'isNewbie', 'toRateLimitSubject' ] ); |
| 434 |
$actor->method( 'getBlock' )->willReturn( $block ); |
| 435 |
$actor->method( 'isNewbie' )->willReturn( false ); |
| 436 |
$actor->method( 'isTemp' )->willReturn( $isTemp ); |
| 437 |
$actor->method( 'isNamed' )->willReturn( !$isTemp ); |
| 438 |
|
| 439 |
$subject = new RateLimitSubject( $actor, '::1', [] ); |
| 440 |
$actor->method( 'toRateLimitSubject' )->willReturn( $subject ); |
| 441 |
return $actor; |
| 442 |
} |
| 443 |
|
| 444 |
private function newBlockErrorFormatter(): BlockErrorFormatter { |
| 445 |
$blockErrorFormatter = $this->createNoOpMock( BlockErrorFormatter::class, [ 'getMessages' ] ); |
| 446 |
$blockErrorFormatter->method( 'getMessages' )->willReturn( [ new Message( 'blocked' ) ] ); |
| 447 |
return $blockErrorFormatter; |
| 448 |
} |
| 449 |
|
| 450 |
private function newContext(): IContextSource { |
| 451 |
$language = $this->createNoOpMock( Language::class, [ 'getCode' ] ); |
| 452 |
$language->method( 'getCode' )->willReturn( 'en' ); |
| 453 |
|
| 454 |
$context = $this->createNoOpMock( IContextSource::class, [ 'getLanguage' ] ); |
| 455 |
$context->method( 'getLanguage' )->willReturn( $language ); |
| 456 |
return $context; |
| 457 |
} |
| 458 |
|
| 459 |
private function newRequest(): WebRequest { |
| 460 |
$request = new FauxRequest(); |
| 461 |
$request->setIP( '1.2.3.4' ); |
| 462 |
return $request; |
| 463 |
} |
| 464 |
|
| 465 |
private function newUserAuthority( array $options = [] ): UserAuthority { |
| 466 |
$permissionManager = $options['permissionManager'] |
| 467 |
?? $this->newPermissionsManager( $options['permissions'] ?? [] ); |
| 468 |
|
| 469 |
$rateLimiter = $options['rateLimiter'] |
| 470 |
?? $this->newRateLimiter( $options['limited'] ?? false ); |
| 471 |
|
| 472 |
$blockErrorFormatter = $options['blockErrorFormatter'] |
| 473 |
?? $this->newBlockErrorFormatter(); |
| 474 |
|
| 475 |
return new UserAuthority( |
| 476 |
$options['actor'] ?? $this->newUser(), |
| 477 |
$options['request'] ?? $this->newRequest(), |
| 478 |
$options['context'] ?? $this->newContext(), |
| 479 |
$permissionManager, |
| 480 |
$rateLimiter, |
| 481 |
$blockErrorFormatter |
| 482 |
); |
| 483 |
} |
| 484 |
} |