Debian Codegraph @

tests/phpunit/unit/includes/block/BlockPermissionCheckerTest.php

raw symbols SCIP
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 * @group Blocking
17 * @coversDefaultClass \MediaWiki\Block\BlockPermissionChecker
18 * @author DannyS712
19 */
20 class BlockPermissionCheckerTest extends MediaWikiUnitTestCase {
21 use MockAuthorityTrait;
22
23 /**
24 * @param bool $enableUserEmail
25 * @param array $targetAndType
26 * @param Authority $performer
27 * @return BlockPermissionChecker
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 // We don't care about how BlockUtils::parseBlockTarget actually works, just
40 // override with whatever. Only used for a single call in the constructor
41 // for getting target and type
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', // input to BlockUtils::parseBlockTarget, not used
51 $performer
52 );
53 }
54
55 /**
56 * @param bool $isSitewide
57 * @param string $byName
58 * @return DatabaseBlock|MockObject
59 */
60 private function getBlock( bool $isSitewide, string $byName ) {
61 // Mock DatabaseBlock instead of AbstractBlock because its easier
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 // $rights, $checkHideuser, $expect
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 * @covers ::checkBasePermissions
81 * @dataProvider provideCheckBasePermissions
82 */
83 public function testCheckBasePermissions( $rights, $checkHideuser, $expect ) {
84 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
85 $blockPermissionChecker = $this->getBlockPermissionChecker(
86 true, // $enableUserEmail, irrelevant
87 [ null, null ], // $targetAndType, irrelevant
88 $performer
89 );
90 $this->assertSame(
91 $expect,
92 $blockPermissionChecker->checkBasePermissions( $checkHideuser )
93 );
94 }
95
96 /**
97 * @covers ::checkBlockPermissions
98 */
99 public function testNotBlockedPerformer() {
100 // checkBlockPermissions has an early return true if the performer has no block
101 $blockPermissionChecker = $this->getBlockPermissionChecker(
102 true, // $enableUserEmail, irrelevant
103 [ null, null ], // $targetAndType, irrelevant
104 $this->mockRegisteredAuthorityWithoutPermissions( [] )
105 );
106 $this->assertTrue(
107 $blockPermissionChecker->checkBlockPermissions()
108 );
109 }
110
111 /**
112 * @covers ::checkBlockPermissions
113 */
114 public function testPartialBlockedPerformer() {
115 // checkBlockPermissions has an early return true if the performer is not sitewide blocked
116 $blocker = new UserIdentityValue( 1, 'blocker', UserIdentity::LOCAL );
117 $performer = $this->mockUserAuthorityWithBlock( $blocker, $this->getBlock( false, '' ) );
118
119 $blockPermissionChecker = $this->getBlockPermissionChecker(
120 true, // $enableUserEmail, irrelevant
121 [ null, null ], // $targetAndType, irrelevant
122 $performer
123 );
124 $this->assertTrue(
125 $blockPermissionChecker->checkBlockPermissions()
126 );
127 }
128
129 public static function provideCheckBlockPermissions() {
130 // Blocked admin changing own block
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 // Blocked admins can always block the admin who blocked them
136 yield 'Block my blocker' => [ 'another admin', 2, 'another admin', false, true ];
137
138 // Blocked admin blocking a third party
139 yield 'Block someone else' => [ 'another admin', 3, 'someone else', false, 'ipbblocked' ];
140 }
141
142 /**
143 * @covers ::checkBlockPermissions
144 * @dataProvider provideCheckBlockPermissions
145 */
146 public function testCheckBlockPermissions(
147 string $blockedBy,
148 int $targetUserId,
149 string $targetUserName,
150 bool $unblockSelf,
151 $expect
152 ) {
153 // cases for the target being a UserIdentity
154 // performing user has id 1 and the name 'blocker'
155 $block = $this->getBlock(
156 true, // sitewide
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, // $enableUserEmail, irrelevant
168 [ $target, AbstractBlock::TYPE_USER ], // $targetAndType, irrelevant
169 $performer
170 );
171 $this->assertSame(
172 $expect,
173 $blockPermissionChecker->checkBlockPermissions()
174 );
175 }
176
177 public static function provideCheckEmailPermissions() {
178 // $enableEmail, $rights, $expect
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 * @covers ::checkEmailPermissions
187 * @dataProvider provideCheckEmailPermissions
188 */
189 public function testCheckEmailPermissionOkay( $enableEmail, $rights, $expect ) {
190 $performer = $this->mockRegisteredAuthorityWithPermissions( $rights );
191 $blockPermissionChecker = $this->getBlockPermissionChecker(
192 $enableEmail,
193 [ null, null ], // $targetAndType, irrelevant
194 $performer
195 );
196 $this->assertSame( $expect, $blockPermissionChecker->checkEmailPermissions() );
197 }
198 }