Merge "FauxRequest: don’t override getValues()"
[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->doBlock( [ 'tags' => 'custom tag' ] );
154 }
155
156 public function testBlockWithHide() {
157 global $wgGroupPermissions;
158 $newPermissions = $wgGroupPermissions['sysop'];
159 $newPermissions['hideuser'] = true;
160 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
161 [ 'sysop' => $newPermissions ] );
162
163 $res = $this->doBlock( [ 'hidename' => '' ] );
164
165 $dbw = wfGetDB( DB_MASTER );
166 $this->assertSame( '1', $dbw->selectField(
167 'ipblocks',
168 'ipb_deleted',
169 [ 'ipb_id' => $res[0]['block']['id'] ],
170 __METHOD__
171 ) );
172 }
173
174 public function testBlockWithProhibitedHide() {
175 $this->setExpectedException( ApiUsageException::class,
176 "You don't have permission to hide user names from the block log." );
177
178 $this->doBlock( [ 'hidename' => '' ] );
179 }
180
181 public function testBlockWithEmailBlock() {
182 $this->setMwGlobals( [
183 'wgEnableEmail' => true,
184 'wgEnableUserEmail' => true,
185 ] );
186
187 $res = $this->doBlock( [ 'noemail' => '' ] );
188
189 $dbw = wfGetDB( DB_MASTER );
190 $this->assertSame( '1', $dbw->selectField(
191 'ipblocks',
192 'ipb_block_email',
193 [ 'ipb_id' => $res[0]['block']['id'] ],
194 __METHOD__
195 ) );
196 }
197
198 public function testBlockWithProhibitedEmailBlock() {
199 $this->setMwGlobals( [
200 'wgEnableEmail' => true,
201 'wgEnableUserEmail' => true,
202 ] );
203
204 $this->setExpectedException( ApiUsageException::class,
205 "You don't have permission to block users from sending email through the wiki." );
206
207 $this->setMwGlobals( 'wgRevokePermissions',
208 [ 'sysop' => [ 'blockemail' => true ] ] );
209
210 $this->doBlock( [ 'noemail' => '' ] );
211 }
212
213 public function testBlockWithExpiry() {
214 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
215
216 $dbw = wfGetDB( DB_MASTER );
217 $expiry = $dbw->selectField(
218 'ipblocks',
219 'ipb_expiry',
220 [ 'ipb_id' => $res[0]['block']['id'] ],
221 __METHOD__
222 );
223
224 // Allow flakiness up to one second
225 $this->assertLessThanOrEqual( 1,
226 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
227 }
228
229 public function testBlockWithInvalidExpiry() {
230 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
231
232 $this->doBlock( [ 'expiry' => '' ] );
233 }
234
235 public function testBlockWithoutRestrictions() {
236 $this->setMwGlobals( [
237 'wgEnablePartialBlocks' => true,
238 ] );
239
240 $this->doBlock();
241
242 $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
243
244 $this->assertTrue( $block->isSitewide() );
245 $this->assertCount( 0, $block->getRestrictions() );
246 }
247
248 public function testBlockWithRestrictions() {
249 $this->setMwGlobals( [
250 'wgEnablePartialBlocks' => true,
251 ] );
252
253 $title = 'Foo';
254 $page = $this->getExistingTestPage( $title );
255 $namespace = NS_TALK;
256
257 $this->doBlock( [
258 'partial' => true,
259 'pagerestrictions' => $title,
260 'namespacerestrictions' => $namespace,
261 'allowusertalk' => true,
262 ] );
263
264 $block = DatabaseBlock::newFromTarget( $this->mUser->getName() );
265
266 $this->assertFalse( $block->isSitewide() );
267 $this->assertCount( 2, $block->getRestrictions() );
268 $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
269 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
270 $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[1] );
271 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
272 }
273
274 /**
275 * @expectedException ApiUsageException
276 * @expectedExceptionMessage The "token" parameter must be set
277 */
278 public function testBlockingActionWithNoToken() {
279 $this->doApiRequest(
280 [
281 'action' => 'block',
282 'user' => $this->mUser->getName(),
283 'reason' => 'Some reason',
284 ],
285 null,
286 false,
287 self::$users['sysop']->getUser()
288 );
289 }
290
291 /**
292 * @expectedException ApiUsageException
293 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
294 */
295 public function testBlockWithLargeRange() {
296 $tokens = $this->getTokens();
297
298 $this->doApiRequest(
299 [
300 'action' => 'block',
301 'user' => '127.0.0.1/64',
302 'reason' => 'Some reason',
303 'token' => $tokens['blocktoken'],
304 ],
305 null,
306 false,
307 self::$users['sysop']->getUser()
308 );
309 }
310
311 /**
312 * @expectedException ApiUsageException
313 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
314 * limit is 10.
315 */
316 public function testBlockingTooManyPageRestrictions() {
317 $this->setMwGlobals( [
318 'wgEnablePartialBlocks' => true,
319 ] );
320
321 $tokens = $this->getTokens();
322
323 $this->doApiRequest(
324 [
325 'action' => 'block',
326 'user' => $this->mUser->getName(),
327 'reason' => 'Some reason',
328 'partial' => true,
329 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
330 'token' => $tokens['blocktoken'],
331 ],
332 null,
333 false,
334 self::$users['sysop']->getUser()
335 );
336 }
337
338 public function testRangeBlock() {
339 $this->mUser = User::newFromName( '128.0.0.0/16', false );
340 $this->doBlock();
341 }
342
343 /**
344 * @expectedException ApiUsageException
345 * @expectedExceptionMessage Range blocks larger than /16 are not allowed.
346 */
347 public function testVeryLargeRangeBlock() {
348 $this->mUser = User::newFromName( '128.0.0.0/1', false );
349 $this->doBlock();
350 }
351 }