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