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