Merge "ServiceWiring: Use RequestContext instead of $wgUser global"
[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 $res = $this->doBlock( [ 'noemail' => '' ] );
179
180 $dbw = wfGetDB( DB_MASTER );
181 $this->assertSame( '1', $dbw->selectField(
182 'ipblocks',
183 'ipb_block_email',
184 [ 'ipb_id' => $res[0]['block']['id'] ],
185 __METHOD__
186 ) );
187 }
188
189 public function testBlockWithProhibitedEmailBlock() {
190 $this->setExpectedException( ApiUsageException::class,
191 "You don't have permission to block users from sending email through the wiki." );
192
193 $this->setMwGlobals( 'wgRevokePermissions',
194 [ 'sysop' => [ 'blockemail' => true ] ] );
195
196 $this->doBlock( [ 'noemail' => '' ] );
197 }
198
199 public function testBlockWithExpiry() {
200 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
201
202 $dbw = wfGetDB( DB_MASTER );
203 $expiry = $dbw->selectField(
204 'ipblocks',
205 'ipb_expiry',
206 [ 'ipb_id' => $res[0]['block']['id'] ],
207 __METHOD__
208 );
209
210 // Allow flakiness up to one second
211 $this->assertLessThanOrEqual( 1,
212 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
213 }
214
215 public function testBlockWithInvalidExpiry() {
216 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
217
218 $this->doBlock( [ 'expiry' => '' ] );
219 }
220
221 public function testBlockWithoutRestrictions() {
222 $this->setMwGlobals( [
223 'wgEnablePartialBlocks' => true,
224 ] );
225
226 $this->doBlock();
227
228 $block = Block::newFromTarget( $this->mUser->getName() );
229
230 $this->assertTrue( $block->isSitewide() );
231 $this->assertCount( 0, $block->getRestrictions() );
232 }
233
234 public function testBlockWithRestrictions() {
235 $this->setMwGlobals( [
236 'wgEnablePartialBlocks' => true,
237 ] );
238
239 $title = 'Foo';
240 $page = $this->getExistingTestPage( $title );
241 $namespace = NS_TALK;
242
243 $this->doBlock( [
244 'partial' => true,
245 'pagerestrictions' => $title,
246 'namespacerestrictions' => $namespace,
247 ] );
248
249 $block = Block::newFromTarget( $this->mUser->getName() );
250
251 $this->assertFalse( $block->isSitewide() );
252 $this->assertCount( 2, $block->getRestrictions() );
253 $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
254 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
255 $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[1] );
256 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
257 }
258
259 /**
260 * @expectedException ApiUsageException
261 * @expectedExceptionMessage The "token" parameter must be set
262 */
263 public function testBlockingActionWithNoToken() {
264 $this->doApiRequest(
265 [
266 'action' => 'block',
267 'user' => $this->mUser->getName(),
268 'reason' => 'Some reason',
269 ],
270 null,
271 false,
272 self::$users['sysop']->getUser()
273 );
274 }
275
276 /**
277 * @expectedException ApiUsageException
278 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
279 */
280 public function testBlockWithLargeRange() {
281 $tokens = $this->getTokens();
282
283 $this->doApiRequest(
284 [
285 'action' => 'block',
286 'user' => '127.0.0.1/64',
287 'reason' => 'Some reason',
288 'token' => $tokens['blocktoken'],
289 ],
290 null,
291 false,
292 self::$users['sysop']->getUser()
293 );
294 }
295
296 /**
297 * @expectedException ApiUsageException
298 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
299 * limit is 10.
300 */
301 public function testBlockingToManyPageRestrictions() {
302 $this->setMwGlobals( [
303 'wgEnablePartialBlocks' => true,
304 ] );
305
306 $tokens = $this->getTokens();
307
308 $this->doApiRequest(
309 [
310 'action' => 'block',
311 'user' => $this->mUser->getName(),
312 'reason' => 'Some reason',
313 'partial' => true,
314 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
315 'token' => $tokens['blocktoken'],
316 ],
317 null,
318 false,
319 self::$users['sysop']->getUser()
320 );
321 }
322 }