Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBlockTest.php
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4 use MediaWiki\Block\Restriction\PageRestriction;
5 use MediaWiki\Block\Restriction\NamespaceRestriction;
6
7 /**
8 * @group API
9 * @group Database
10 * @group medium
11 *
12 * @covers ApiBlock
13 */
14 class ApiBlockTest extends ApiTestCase {
15 protected $mUser = null;
16
17 protected function setUp() {
18 parent::setUp();
19 $this->tablesUsed = array_merge(
20 $this->tablesUsed,
21 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
22 );
23
24 $this->mUser = $this->getMutableTestUser()->getUser();
25 $this->setMwGlobals( 'wgBlockCIDRLimit', [
26 'IPv4' => 16,
27 'IPv6' => 19,
28 ] );
29 }
30
31 protected function getTokens() {
32 return $this->getTokenList( self::$users['sysop'] );
33 }
34
35 /**
36 * @param array $extraParams Extra API parameters to pass to doApiRequest
37 * @param User $blocker User to do the blocking, null to pick
38 * arbitrarily
39 */
40 private function doBlock( array $extraParams = [], User $blocker = null ) {
41 if ( $blocker === null ) {
42 $blocker = self::$users['sysop']->getUser();
43 }
44
45 $tokens = $this->getTokens();
46
47 $this->assertNotNull( $this->mUser, 'Sanity check' );
48
49 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
50
51 $params = [
52 'action' => 'block',
53 'user' => $this->mUser->getName(),
54 'reason' => 'Some reason',
55 'token' => $tokens['blocktoken'],
56 ];
57 if ( array_key_exists( 'userid', $extraParams ) ) {
58 // Make sure we don't have both user and userid
59 unset( $params['user'] );
60 }
61 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
62 false, $blocker );
63
64 $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
65
66 $this->assertTrue( !is_null( $block ), 'Block is valid' );
67
68 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
69 $this->assertSame( 'Some reason', $block->getReason() );
70
71 return $ret;
72 }
73
74 /**
75 * Block by username
76 */
77 public function testNormalBlock() {
78 $this->doBlock();
79 }
80
81 /**
82 * Block by user ID
83 */
84 public function testBlockById() {
85 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
86 }
87
88 /**
89 * A blocked user can't block
90 */
91 public function testBlockByBlockedUser() {
92 $this->setExpectedException( ApiUsageException::class,
93 'You cannot block or unblock other users because you are yourself blocked.' );
94
95 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
96 $block = new DatabaseBlock( [
97 'address' => $blocked->getName(),
98 'by' => self::$users['sysop']->getUser()->getId(),
99 'reason' => 'Capriciousness',
100 'timestamp' => '19370101000000',
101 'expiry' => 'infinity',
102 ] );
103 $block->insert();
104
105 $this->doBlock( [], $blocked );
106 }
107
108 public function testBlockOfNonexistentUser() {
109 $this->setExpectedException( ApiUsageException::class,
110 'There is no user by the name "Nonexistent". Check your spelling.' );
111
112 $this->doBlock( [ 'user' => 'Nonexistent' ] );
113 }
114
115 public function testBlockOfNonexistentUserId() {
116 $id = 948206325;
117 $this->setExpectedException( ApiUsageException::class,
118 "There is no user with ID $id." );
119
120 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
121
122 $this->doBlock( [ 'userid' => $id ] );
123 }
124
125 public function testBlockWithTag() {
126 ChangeTags::defineTag( 'custom tag' );
127
128 $this->doBlock( [ 'tags' => 'custom tag' ] );
129
130 $dbw = wfGetDB( DB_MASTER );
131 $this->assertSame( 1, (int)$dbw->selectField(
132 [ 'change_tag', 'logging', 'change_tag_def' ],
133 'COUNT(*)',
134 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
135 __METHOD__,
136 [],
137 [
138 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
139 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
140 ]
141 ) );
142 }
143
144 public function testBlockWithProhibitedTag() {
145 $this->setExpectedException( ApiUsageException::class,
146 'You do not have permission to apply change tags along with your changes.' );
147
148 ChangeTags::defineTag( 'custom tag' );
149
150 $this->setMwGlobals( 'wgRevokePermissions',
151 [ 'user' => [ 'applychangetags' => true ] ] );
152
153 $this->resetServices();
154
155 $this->doBlock( [ 'tags' => 'custom tag' ] );
156 }
157
158 public function testBlockWithHide() {
159 global $wgGroupPermissions;
160 $newPermissions = $wgGroupPermissions['sysop'];
161 $newPermissions['hideuser'] = true;
162 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
163 [ 'sysop' => $newPermissions ] );
164
165 $this->resetServices();
166 $res = $this->doBlock( [ 'hidename' => '' ] );
167
168 $dbw = wfGetDB( DB_MASTER );
169 $this->assertSame( '1', $dbw->selectField(
170 'ipblocks',
171 'ipb_deleted',
172 [ 'ipb_id' => $res[0]['block']['id'] ],
173 __METHOD__
174 ) );
175 }
176
177 public function testBlockWithProhibitedHide() {
178 $this->setExpectedException( ApiUsageException::class,
179 "You don't have permission to hide user names from the block log." );
180
181 $this->doBlock( [ 'hidename' => '' ] );
182 }
183
184 public function testBlockWithEmailBlock() {
185 $this->setMwGlobals( [
186 'wgEnableEmail' => true,
187 'wgEnableUserEmail' => true,
188 'wgSysopEmailBans' => true,
189 ] );
190
191 $res = $this->doBlock( [ 'noemail' => '' ] );
192
193 $dbw = wfGetDB( DB_MASTER );
194 $this->assertSame( '1', $dbw->selectField(
195 'ipblocks',
196 'ipb_block_email',
197 [ 'ipb_id' => $res[0]['block']['id'] ],
198 __METHOD__
199 ) );
200 }
201
202 public function testBlockWithProhibitedEmailBlock() {
203 $this->setMwGlobals( [
204 'wgEnableEmail' => true,
205 'wgEnableUserEmail' => true,
206 'wgSysopEmailBans' => true,
207 ] );
208
209 $this->setExpectedException( ApiUsageException::class,
210 "You don't have permission to block users from sending email through the wiki." );
211
212 $this->setMwGlobals( 'wgRevokePermissions',
213 [ 'sysop' => [ 'blockemail' => true ] ] );
214
215 $this->resetServices();
216
217 $this->doBlock( [ 'noemail' => '' ] );
218 }
219
220 public function testBlockWithExpiry() {
221 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
222
223 $dbw = wfGetDB( DB_MASTER );
224 $expiry = $dbw->selectField(
225 'ipblocks',
226 'ipb_expiry',
227 [ 'ipb_id' => $res[0]['block']['id'] ],
228 __METHOD__
229 );
230
231 // Allow flakiness up to one second
232 $this->assertLessThanOrEqual( 1,
233 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
234 }
235
236 public function testBlockWithInvalidExpiry() {
237 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
238
239 $this->doBlock( [ 'expiry' => '' ] );
240 }
241
242 public function testBlockWithoutRestrictions() {
243 $this->setMwGlobals( [
244 'wgEnablePartialBlocks' => true,
245 ] );
246
247 $this->doBlock();
248
249 $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
250
251 $this->assertTrue( $block->isSitewide() );
252 $this->assertCount( 0, $block->getRestrictions() );
253 }
254
255 public function testBlockWithRestrictions() {
256 $this->setMwGlobals( [
257 'wgEnablePartialBlocks' => true,
258 ] );
259
260 $title = 'Foo';
261 $page = $this->getExistingTestPage( $title );
262 $namespace = NS_TALK;
263
264 $this->doBlock( [
265 'partial' => true,
266 'pagerestrictions' => $title,
267 'namespacerestrictions' => $namespace,
268 ] );
269
270 $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
271
272 $this->assertFalse( $block->isSitewide() );
273 $this->assertCount( 2, $block->getRestrictions() );
274 $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
275 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
276 $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[1] );
277 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
278 }
279
280 /**
281 * @expectedException ApiUsageException
282 * @expectedExceptionMessage The "token" parameter must be set
283 */
284 public function testBlockingActionWithNoToken() {
285 $this->doApiRequest(
286 [
287 'action' => 'block',
288 'user' => $this->mUser->getName(),
289 'reason' => 'Some reason',
290 ],
291 null,
292 false,
293 self::$users['sysop']->getUser()
294 );
295 }
296
297 /**
298 * @expectedException ApiUsageException
299 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
300 */
301 public function testBlockWithLargeRange() {
302 $tokens = $this->getTokens();
303
304 $this->doApiRequest(
305 [
306 'action' => 'block',
307 'user' => '127.0.0.1/64',
308 'reason' => 'Some reason',
309 'token' => $tokens['blocktoken'],
310 ],
311 null,
312 false,
313 self::$users['sysop']->getUser()
314 );
315 }
316
317 /**
318 * @expectedException ApiUsageException
319 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
320 * limit is 10.
321 */
322 public function testBlockingTooManyPageRestrictions() {
323 $this->setMwGlobals( [
324 'wgEnablePartialBlocks' => true,
325 ] );
326
327 $tokens = $this->getTokens();
328
329 $this->doApiRequest(
330 [
331 'action' => 'block',
332 'user' => $this->mUser->getName(),
333 'reason' => 'Some reason',
334 'partial' => true,
335 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
336 'token' => $tokens['blocktoken'],
337 ],
338 null,
339 false,
340 self::$users['sysop']->getUser()
341 );
342 }
343
344 public function testRangeBlock() {
345 $this->mUser = User::newFromName( '128.0.0.0/16', false );
346 $this->doBlock();
347 }
348
349 /**
350 * @expectedException ApiUsageException
351 * @expectedExceptionMessage Range blocks larger than /16 are not allowed.
352 */
353 public function testVeryLargeRangeBlock() {
354 $this->mUser = User::newFromName( '128.0.0.0/1', false );
355 $this->doBlock();
356 }
357 }