| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Tests\Integration\Permissions; |
| 22 |
|
| 23 |
use MediaWiki\Block\Block; |
| 24 |
use MediaWiki\Block\BlockErrorFormatter; |
| 25 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 26 |
use MediaWiki\Language\FormatterFactory; |
| 27 |
use MediaWiki\Language\RawMessage; |
| 28 |
use MediaWiki\Permissions\PermissionStatus; |
| 29 |
use MediaWiki\User\UserIdentityValue; |
| 30 |
use MediaWikiIntegrationTestCase; |
| 31 |
use PermissionsError; |
| 32 |
use ThrottledError; |
| 33 |
use UserBlockedError; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class PermissionStatusIntegrationTest extends MediaWikiIntegrationTestCase { |
| 39 |
|
| 40 |
private function makeBlockedStatus() { |
| 41 |
$user = new UserIdentityValue( 1, 'Primus' ); |
| 42 |
$blocker = new UserIdentityValue( 2, 'Secundus' ); |
| 43 |
|
| 44 |
$block = $this->createNoOpMock( |
| 45 |
Block::class, |
| 46 |
[ |
| 47 |
'getTargetUserIdentity', |
| 48 |
'getBlocker', |
| 49 |
'getIdentifier', |
| 50 |
'getTargetName', |
| 51 |
'getReasonComment', |
| 52 |
'getExpiry', |
| 53 |
'getTimestamp', |
| 54 |
] |
| 55 |
); |
| 56 |
$block->method( 'getTargetUserIdentity' )->willReturn( $user ); |
| 57 |
$block->method( 'getTargetName' )->willReturn( $user->getName() ); |
| 58 |
$block->method( 'getBlocker' )->willReturn( $blocker ); |
| 59 |
$block->method( 'getIdentifier' )->willReturn( 'TEST' ); |
| 60 |
$block->method( 'getReasonComment' )->willReturn( |
| 61 |
CommentStoreComment::newUnsavedComment( 'testing' ) |
| 62 |
); |
| 63 |
$block->method( 'getTimestamp' )->willReturn( '20220102334455' ); |
| 64 |
$block->method( 'getExpiry' )->willReturn( '20250102334455' ); |
| 65 |
|
| 66 |
$status = PermissionStatus::newEmpty(); |
| 67 |
$status->fatal( 'blockednoreason' ); |
| 68 |
$status->setBlock( $block ); |
| 69 |
return $status; |
| 70 |
} |
| 71 |
|
| 72 |
private function assertThrowErrorPageError( PermissionStatus $status, $expected ) { |
| 73 |
$this->expectExceptionMessage( $expected->getMessage() ); |
| 74 |
|
| 75 |
$status->throwErrorPageError(); |
| 76 |
} |
| 77 |
|
| 78 |
public function testThrowErrorPageError_unknown() { |
| 79 |
$status = PermissionStatus::newEmpty(); |
| 80 |
$status->fatal( 'permissionserrorstext-withaction', 'move' ); |
| 81 |
$expected = new PermissionsError( |
| 82 |
null, |
| 83 |
[ [ 'permissionserrorstext-withaction', 'move' ] ] |
| 84 |
); |
| 85 |
|
| 86 |
$this->assertThrowErrorPageError( $status, $expected ); |
| 87 |
} |
| 88 |
|
| 89 |
public function testThrowErrorPageError_known() { |
| 90 |
$status = PermissionStatus::newEmpty(); |
| 91 |
$status->setPermission( 'move' ); |
| 92 |
$status->fatal( 'permissionserrorstext-withaction', 'move' ); |
| 93 |
$expected = new PermissionsError( |
| 94 |
'move', |
| 95 |
[ [ 'permissionserrorstext-withaction', 'move' ] ] |
| 96 |
); |
| 97 |
|
| 98 |
$this->assertThrowErrorPageError( $status, $expected ); |
| 99 |
} |
| 100 |
|
| 101 |
public function testThrowErrorPageError_blocked() { |
| 102 |
$blockErrorFormatter = $this->createNoOpMock( BlockErrorFormatter::class, [ 'getMessages' ] ); |
| 103 |
$blockErrorFormatter->method( 'getMessages' )->willReturn( [ new RawMessage( 'testing' ) ] ); |
| 104 |
|
| 105 |
$formatterFactory = $this->createNoOpMock( FormatterFactory::class, [ 'getBlockErrorFormatter' ] ); |
| 106 |
$formatterFactory->method( 'getBlockErrorFormatter' )->willReturn( $blockErrorFormatter ); |
| 107 |
|
| 108 |
$this->setService( 'FormatterFactory', $formatterFactory ); |
| 109 |
|
| 110 |
$status = $this->makeBlockedStatus(); |
| 111 |
$block = $status->getBlock(); |
| 112 |
$expected = new UserBlockedError( $block ); |
| 113 |
|
| 114 |
$this->assertThrowErrorPageError( $status, $expected ); |
| 115 |
} |
| 116 |
|
| 117 |
public function testThrowErrorPageError_throttled() { |
| 118 |
$status = PermissionStatus::newEmpty(); |
| 119 |
$status->setRateLimitExceeded(); |
| 120 |
$expected = new ThrottledError(); |
| 121 |
|
| 122 |
$this->assertThrowErrorPageError( $status, $expected ); |
| 123 |
} |
| 124 |
|
| 125 |
} |