Debian Codegraph @

tests/phpunit/includes/api/ApiBlockTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Api;
4
5 use LogEntryBase;
6 use MediaWiki\Block\DatabaseBlock;
7 use MediaWiki\Block\Restriction\ActionRestriction;
8 use MediaWiki\Block\Restriction\NamespaceRestriction;
9 use MediaWiki\Block\Restriction\PageRestriction;
10 use MediaWiki\MainConfigNames;
11 use MediaWiki\Permissions\Authority;
12 use MediaWiki\Permissions\UltimateAuthority;
13 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
14 use MediaWiki\User\User;
15 use MediaWiki\User\UserRigorOptions;
16 use MediaWiki\Utils\MWTimestamp;
17 use Wikimedia\Rdbms\SelectQueryBuilder;
18
19 /**
20 * @group API
21 * @group Database
22 * @group medium
23 *
24 * @covers \MediaWiki\Api\ApiBlock
25 */
26 class ApiBlockTest extends ApiTestCase {
27 use MockAuthorityTrait;
28
29 /** @var User|null */
30 protected $mUser = null;
31 /** @var DatabaseBlockStore */
32 private $blockStore;
33
34 protected function setUp(): void {
35 parent::setUp();
36
37 $this->mUser = $this->getMutableTestUser()->getUser();
38 $this->overrideConfigValue(
39 MainConfigNames::BlockCIDRLimit,
40 [
41 'IPv4' => 16,
42 'IPv6' => 19,
43 ]
44 );
45 $this->blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
46 }
47
48 /**
49 * @param array $extraParams Extra API parameters to pass to doApiRequest
50 * @param Authority|null $blocker User to do the blocking, null to pick arbitrarily
51 * @return array result of doApiRequest
52 */
53 private function doBlock( array $extraParams = [], ?Authority $blocker = null ) {
54 $this->assertNotNull( $this->mUser );
55
56 $params = [
57 'action' => 'block',
58 'user' => $this->mUser->getName(),
59 'reason' => 'Some reason',
60 ];
61 if ( array_key_exists( 'userid', $extraParams ) ) {
62 // Make sure we don't have both user and userid
63 unset( $params['user'] );
64 }
65 $ret = $this->doApiRequestWithToken( array_merge( $params, $extraParams ), null, $blocker );
66
67 $block = $this->blockStore->newFromTarget( $this->mUser->getName() );
68
69 $this->assertInstanceOf( DatabaseBlock::class, $block, 'Block is valid' );
70
71 $this->assertSame( $this->mUser->getName(), $block->getTargetName() );
72 $this->assertSame( 'Some reason', $block->getReasonComment()->text );
73
74 return $ret;
75 }
76
77 /**
78 * Block by username
79 */
80 public function testNormalBlock() {
81 $this->doBlock();
82 }
83
84 /**
85 * Block by user ID
86 */
87 public function testBlockById() {
88 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
89 }
90
91 /**
92 * A blocked user can't block
93 */
94 public function testBlockByBlockedUser() {
95 $this->expectApiErrorCode( 'ipbblocked' );
96
97 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
98 $block = new DatabaseBlock( [
99 'address' => $blocked->getName(),
100 'by' => $this->getTestSysop()->getUser(),
101 'reason' => 'Capriciousness',
102 'timestamp' => '19370101000000',
103 'expiry' => 'infinity',
104 ] );
105 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
106
107 $this->doBlock( [], $blocked );
108 }
109
110 public function testBlockOfNonexistentUser() {
111 $this->expectApiErrorCode( 'nosuchuser' );
112
113 $this->doBlock( [ 'user' => 'Nonexistent' ] );
114 }
115
116 public function testBlockOfNonexistentUserId() {
117 $id = 948206325;
118 $this->expectApiErrorCode( 'nosuchuserid' );
119
120 $this->assertNull( $this->getServiceContainer()->getUserIdentityLookup()->getUserIdentityByUserId( $id ) );
121
122 $this->doBlock( [ 'userid' => $id ] );
123 }
124
125 public function testBlockWithTag() {
126 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
127
128 $this->doBlock( [ 'tags' => 'custom tag' ] );
129
130 $this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
131 ->select( 'COUNT(*)' )
132 ->from( 'logging' )
133 ->join( 'change_tag', null, 'ct_log_id = log_id' )
134 ->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
135 ->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
136 ->caller( __METHOD__ )->fetchField() );
137 }
138
139 public function testBlockWithProhibitedTag() {
140 $this->expectApiErrorCode( 'tags-apply-no-permission' );
141
142 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
143
144 $this->overrideConfigValue(
145 MainConfigNames::RevokePermissions,
146 [ 'user' => [ 'applychangetags' => true ] ]
147 );
148
149 $this->doBlock( [ 'tags' => 'custom tag' ] );
150 }
151
152 public function testBlockWithHide() {
153 $res = $this->doBlock(
154 [ 'hidename' => '' ],
155 new UltimateAuthority( $this->getTestSysop()->getUser() )
156 );
157
158 $this->assertSame( '1', $this->getDb()->newSelectQueryBuilder()
159 ->select( 'bl_deleted' )
160 ->from( 'block' )
161 ->where( [ 'bl_id' => $res[0]['block']['id'] ] )
162 ->caller( __METHOD__ )->fetchField() );
163 }
164
165 public function testBlockWithProhibitedHide() {
166 $performer = $this->mockUserAuthorityWithoutPermissions(
167 $this->getTestUser()->getUser(),
168 [ 'hideuser' ]
169 );
170 $this->expectApiErrorCode( 'permissiondenied' );
171
172 $this->doBlock( [ 'hidename' => '' ], $performer );
173 }
174
175 public function testBlockWithEmailBlock() {
176 $this->overrideConfigValues( [
177 MainConfigNames::EnableEmail => true,
178 MainConfigNames::EnableUserEmail => true,
179 ] );
180
181 $res = $this->doBlock( [ 'noemail' => '' ] );
182 $this->assertSame( '1', $this->getDb()->newSelectQueryBuilder()
183 ->select( 'bl_block_email' )
184 ->from( 'block' )
185 ->where( [ 'bl_id' => $res[0]['block']['id'] ] )
186 ->caller( __METHOD__ )->fetchField() );
187 }
188
189 public function testBlockWithProhibitedEmailBlock() {
190 $this->overrideConfigValues( [
191 MainConfigNames::EnableEmail => true,
192 MainConfigNames::EnableUserEmail => true,
193 MainConfigNames::RevokePermissions => [ 'sysop' => [ 'blockemail' => true ] ],
194 ] );
195
196 $this->expectApiErrorCode( 'cantblock-email' );
197 $this->doBlock( [ 'noemail' => '' ] );
198 }
199
200 public function testBlockWithExpiry() {
201 $fakeTime = 1616432035;
202 MWTimestamp::setFakeTime( $fakeTime );
203 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
204 $expiry = $this->getDb()->newSelectQueryBuilder()
205 ->select( 'bl_expiry' )
206 ->from( 'block' )
207 ->where( [ 'bl_id' => $res[0]['block']['id'] ] )
208 ->caller( __METHOD__ )->fetchField();
209 $this->assertSame( (int)wfTimestamp( TS_UNIX, $expiry ), $fakeTime + 86400 );
210
211 // Check log format (T248196)
212 $blob = $this->newSelectQueryBuilder()
213 ->select( 'log_params' )
214 ->from( 'logging' )
215 ->where( [
216 'log_action' => 'block',
217 'log_type' => 'block'
218 ] )
219 ->orderBy( 'log_timestamp', SelectQueryBuilder::SORT_DESC )
220 ->caller( __METHOD__ )
221 ->fetchField();
222 $params = LogEntryBase::extractParams( $blob, 'block/block' );
223 $this->assertSame( '1 day', $params['5::duration'] );
224 }
225
226 public function testBlockWithInvalidExpiry() {
227 $this->expectApiErrorCode( 'invalidexpiry' );
228
229 $this->doBlock( [ 'expiry' => '' ] );
230 }
231
232 public function testBlockWithoutRestrictions() {
233 $this->doBlock();
234
235 $block = $this->blockStore->newFromTarget( $this->mUser->getName() );
236
237 $this->assertTrue( $block->isSitewide() );
238 $this->assertSame( [], $block->getRestrictions() );
239 }
240
241 public function testBlockWithRestrictionsPage() {
242 $title = 'Foo';
243 $this->getExistingTestPage( $title );
244
245 $this->doBlock( [
246 'partial' => true,
247 'pagerestrictions' => $title,
248 'allowusertalk' => true,
249 ] );
250
251 $block = $this->blockStore->newFromTarget( $this->mUser->getName() );
252
253 $this->assertFalse( $block->isSitewide() );
254 $this->assertInstanceOf( PageRestriction::class, $block->getRestrictions()[0] );
255 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
256 }
257
258 public function testBlockWithRestrictionsNamespace() {
259 $namespace = NS_TALK;
260
261 $this->doBlock( [
262 'partial' => true,
263 'namespacerestrictions' => $namespace,
264 'allowusertalk' => true,
265 ] );
266
267 $block = $this->blockStore->newFromTarget( $this->mUser->getName() );
268
269 $this->assertInstanceOf( NamespaceRestriction::class, $block->getRestrictions()[0] );
270 $this->assertEquals( $namespace, $block->getRestrictions()[0]->getValue() );
271 }
272
273 public function testBlockWithRestrictionsAction() {
274 $this->overrideConfigValue(
275 MainConfigNames::EnablePartialActionBlocks,
276 true
277 );
278
279 $blockActionInfo = $this->getServiceContainer()->getBlockActionInfo();
280 $action = 'upload';
281
282 $this->doBlock( [
283 'partial' => true,
284 'actionrestrictions' => $action,
285 'allowusertalk' => true,
286 ] );
287
288 $block = $this->blockStore->newFromTarget( $this->mUser->getName() );
289
290 $this->assertInstanceOf( ActionRestriction::class, $block->getRestrictions()[0] );
291 $this->assertEquals( $action, $blockActionInfo->getActionFromId( $block->getRestrictions()[0]->getValue() ) );
292 }
293
294 public function testBlockingActionWithNoToken() {
295 $this->expectApiErrorCode( 'missingparam' );
296 $this->doApiRequest(
297 [
298 'action' => 'block',
299 'user' => $this->mUser->getName(),
300 'reason' => 'Some reason',
301 ],
302 null,
303 false,
304 $this->getTestSysop()->getUser()
305 );
306 }
307
308 public function testBlockWithLargeRange() {
309 $this->expectApiErrorCode( 'baduser' );
310 $this->doApiRequestWithToken(
311 [
312 'action' => 'block',
313 'user' => '127.0.0.1/64',
314 'reason' => 'Some reason',
315 ],
316 null,
317 $this->getTestSysop()->getUser()
318 );
319 }
320
321 public function testBlockingTooManyPageRestrictions() {
322 $this->expectApiErrorCode( 'toomanyvalues' );
323 $this->doApiRequestWithToken(
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 ],
331 null,
332 $this->getTestSysop()->getUser()
333 );
334 }
335
336 public function testRangeBlock() {
337 $this->mUser = $this->getServiceContainer()->getUserFactory()->newFromName( '128.0.0.0/16', UserRigorOptions::RIGOR_NONE );
338 $this->doBlock();
339 }
340
341 public function testVeryLargeRangeBlock() {
342 $this->mUser = $this->getServiceContainer()->getUserFactory()->newFromName( '128.0.0.0/1', UserRigorOptions::RIGOR_NONE );
343 $this->expectApiErrorCode( 'ip_range_toolarge' );
344 $this->doBlock();
345 }
346
347 public function testBlockByIdReturns() {
348 // See T189073 and Ifdced735b694b85116cb0e43dadbfa8e4cdb8cab for context
349 $userId = $this->mUser->getId();
350
351 $res = $this->doBlock(
352 [ 'userid' => $userId ]
353 );
354
355 $blockResult = $res[0]['block'];
356
357 $this->assertArrayHasKey( 'user', $blockResult );
358 $this->assertSame( $this->mUser->getName(), $blockResult['user'] );
359
360 $this->assertArrayHasKey( 'userID', $blockResult );
361 $this->assertSame( $userId, $blockResult['userID'] );
362 }
363 }