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