| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Block\AbstractBlock; |
| 4 |
use MediaWiki\Block\BlockPermissionChecker; |
| 5 |
use MediaWiki\Block\BlockUtils; |
| 6 |
use MediaWiki\Block\DatabaseBlock; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Permissions\Authority; |
| 10 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 11 |
use MediaWiki\User\UserIdentity; |
| 12 |
use MediaWiki\User\UserIdentityValue; |
| 13 |
use PHPUnit\Framework\MockObject\MockObject; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class BlockPermissionCheckerTest extends MediaWikiUnitTestCase { |
| 21 |
use MockAuthorityTrait; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private function getBlockPermissionChecker( |
| 30 |
bool $enableUserEmail, |
| 31 |
array $targetAndType, |
| 32 |
Authority $performer |
| 33 |
) { |
| 34 |
$options = new ServiceOptions( |
| 35 |
BlockPermissionChecker::CONSTRUCTOR_OPTIONS, |
| 36 |
[ MainConfigNames::EnableUserEmail => $enableUserEmail ] |
| 37 |
); |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
$blockUtils = $this->createNoOpMock( BlockUtils::class, [ 'parseBlockTarget' ] ); |
| 43 |
$blockUtils->expects( $this->once() ) |
| 44 |
->method( 'parseBlockTarget' ) |
| 45 |
->willReturn( $targetAndType ); |
| 46 |
|
| 47 |
return new BlockPermissionChecker( |
| 48 |
$options, |
| 49 |
$blockUtils, |
| 50 |
'foo', |
| 51 |
$performer |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
private function getBlock( bool $isSitewide, string $byName ) { |
| 61 |
|
| 62 |
$block = $this->createNoOpMock( |
| 63 |
DatabaseBlock::class, |
| 64 |
[ 'isSitewide', 'getBlocker' ] |
| 65 |
); |
| 66 |
$block->method( 'isSitewide' )->willReturn( $isSitewide ); |
| 67 |
$block->method( 'getBlocker' )->willReturn( new UserIdentityValue( 7, $byName ) ); |
| 68 |
return $block; |
| 69 |
} |
| 70 |
|
| 71 |
public static function provideCheckBasePermissions() { |
| 72 |
|
| 73 |
yield 'need block' => [ [], false, 'badaccess-group0' ]; |
| 74 |
yield 'block enough for not hiding' => [ [ 'block' ], false, true ]; |
| 75 |
yield 'need hideuser' => [ [ 'block' ], true, 'unblock-hideuser' ]; |
| 76 |
yield 'can hideuser' => [ [ 'block', 'hideuser' ], true, true ]; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function testCheckBasePermissions( $rights, $checkHideuser, $expect ) { |
| 84 |
$performer = $this->mockRegisteredAuthorityWithPermissions( $rights ); |
| 85 |
$blockPermissionChecker = $this->getBlockPermissionChecker( |
| 86 |
true, |
| 87 |
[ null, null ], |
| 88 |
$performer |
| 89 |
); |
| 90 |
$this->assertSame( |
| 91 |
$expect, |
| 92 |
$blockPermissionChecker->checkBasePermissions( $checkHideuser ) |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function testNotBlockedPerformer() { |
| 100 |
|
| 101 |
$blockPermissionChecker = $this->getBlockPermissionChecker( |
| 102 |
true, |
| 103 |
[ null, null ], |
| 104 |
$this->mockRegisteredAuthorityWithoutPermissions( [] ) |
| 105 |
); |
| 106 |
$this->assertTrue( |
| 107 |
$blockPermissionChecker->checkBlockPermissions() |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function testPartialBlockedPerformer() { |
| 115 |
|
| 116 |
$blocker = new UserIdentityValue( 1, 'blocker', UserIdentity::LOCAL ); |
| 117 |
$performer = $this->mockUserAuthorityWithBlock( $blocker, $this->getBlock( false, '' ) ); |
| 118 |
|
| 119 |
$blockPermissionChecker = $this->getBlockPermissionChecker( |
| 120 |
true, |
| 121 |
[ null, null ], |
| 122 |
$performer |
| 123 |
); |
| 124 |
$this->assertTrue( |
| 125 |
$blockPermissionChecker->checkBlockPermissions() |
| 126 |
); |
| 127 |
} |
| 128 |
|
| 129 |
public static function provideCheckBlockPermissions() { |
| 130 |
|
| 131 |
yield 'Self blocked' => [ 'blocker', 1, 'blocker', false, true ]; |
| 132 |
yield 'unblockself' => [ 'another admin', 1, 'blocker', true, true ]; |
| 133 |
yield 'ipbnounblockself' => [ 'another admin', 1, 'blocker', false, 'ipbnounblockself' ]; |
| 134 |
|
| 135 |
|
| 136 |
yield 'Block my blocker' => [ 'another admin', 2, 'another admin', false, true ]; |
| 137 |
|
| 138 |
|
| 139 |
yield 'Block someone else' => [ 'another admin', 3, 'someone else', false, 'ipbblocked' ]; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public function testCheckBlockPermissions( |
| 147 |
string $blockedBy, |
| 148 |
int $targetUserId, |
| 149 |
string $targetUserName, |
| 150 |
bool $unblockSelf, |
| 151 |
$expect |
| 152 |
) { |
| 153 |
|
| 154 |
|
| 155 |
$block = $this->getBlock( |
| 156 |
true, |
| 157 |
$blockedBy |
| 158 |
); |
| 159 |
$rights = $unblockSelf ? [ 'unblockself' ] : []; |
| 160 |
|
| 161 |
$blocker = new UserIdentityValue( 1, 'blocker', UserIdentity::LOCAL ); |
| 162 |
$performer = $this->mockUserAuthorityWithBlock( $blocker, $block, $rights ); |
| 163 |
|
| 164 |
$target = new UserIdentityValue( $targetUserId, $targetUserName ); |
| 165 |
|
| 166 |
$blockPermissionChecker = $this->getBlockPermissionChecker( |
| 167 |
true, |
| 168 |
[ $target, AbstractBlock::TYPE_USER ], |
| 169 |
$performer |
| 170 |
); |
| 171 |
$this->assertSame( |
| 172 |
$expect, |
| 173 |
$blockPermissionChecker->checkBlockPermissions() |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
public static function provideCheckEmailPermissions() { |
| 178 |
|
| 179 |
yield 'Email not enabled, without permissions' => [ false, [], false ]; |
| 180 |
yield 'Email not enabled, with permissions' => [ false, [ 'blockemail' ], false ]; |
| 181 |
yield 'Email enabled, without permissions' => [ true, [], false ]; |
| 182 |
yield 'Email enabled, with permissions' => [ true, [ 'blockemail' ], true ]; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public function testCheckEmailPermissionOkay( $enableEmail, $rights, $expect ) { |
| 190 |
$performer = $this->mockRegisteredAuthorityWithPermissions( $rights ); |
| 191 |
$blockPermissionChecker = $this->getBlockPermissionChecker( |
| 192 |
$enableEmail, |
| 193 |
[ null, null ], |
| 194 |
$performer |
| 195 |
); |
| 196 |
$this->assertSame( $expect, $blockPermissionChecker->checkEmailPermissions() ); |
| 197 |
} |
| 198 |
} |