3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
14 class ApiBlockTest
extends ApiTestCase
{
15 protected $mUser = null;
17 protected function setUp() {
19 $this->tablesUsed
= array_merge(
21 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
24 $this->mUser
= $this->getMutableTestUser()->getUser();
25 $this->setMwGlobals( 'wgBlockCIDRLimit', [
31 protected function getTokens() {
32 return $this->getTokenList( self
::$users['sysop'] );
36 * @param array $extraParams Extra API parameters to pass to doApiRequest
37 * @param User $blocker User to do the blocking, null to pick
40 private function doBlock( array $extraParams = [], User
$blocker = null ) {
41 if ( $blocker === null ) {
42 $blocker = self
::$users['sysop']->getUser();
45 $tokens = $this->getTokens();
47 $this->assertNotNull( $this->mUser
, 'Sanity check' );
49 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
53 'user' => $this->mUser
->getName(),
54 'reason' => 'Some reason',
55 'token' => $tokens['blocktoken'],
57 if ( array_key_exists( 'userid', $extraParams ) ) {
58 // Make sure we don't have both user and userid
59 unset( $params['user'] );
61 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
64 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
66 $this->assertTrue( !is_null( $block ), 'Block is valid' );
68 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
69 $this->assertSame( 'Some reason', $block->getReason() );
77 public function testNormalBlock() {
84 public function testBlockById() {
85 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
89 * A blocked user can't block
91 public function testBlockByBlockedUser() {
92 $this->setExpectedException( ApiUsageException
::class,
93 'You cannot block or unblock other users because you are yourself blocked.' );
95 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
96 $block = new DatabaseBlock( [
97 'address' => $blocked->getName(),
98 'by' => self
::$users['sysop']->getUser()->getId(),
99 'reason' => 'Capriciousness',
100 'timestamp' => '19370101000000',
101 'expiry' => 'infinity',
105 $this->doBlock( [], $blocked );
108 public function testBlockOfNonexistentUser() {
109 $this->setExpectedException( ApiUsageException
::class,
110 'There is no user by the name "Nonexistent". Check your spelling.' );
112 $this->doBlock( [ 'user' => 'Nonexistent' ] );
115 public function testBlockOfNonexistentUserId() {
117 $this->setExpectedException( ApiUsageException
::class,
118 "There is no user with ID $id." );
120 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
122 $this->doBlock( [ 'userid' => $id ] );
125 public function testBlockWithTag() {
126 ChangeTags
::defineTag( 'custom tag' );
128 $this->doBlock( [ 'tags' => 'custom tag' ] );
130 $dbw = wfGetDB( DB_MASTER
);
131 $this->assertSame( 1, (int)$dbw->selectField(
132 [ 'change_tag', 'logging', 'change_tag_def' ],
134 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
138 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
139 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
144 public function testBlockWithProhibitedTag() {
145 $this->setExpectedException( ApiUsageException
::class,
146 'You do not have permission to apply change tags along with your changes.' );
148 ChangeTags
::defineTag( 'custom tag' );
150 $this->setMwGlobals( 'wgRevokePermissions',
151 [ 'user' => [ 'applychangetags' => true ] ] );
153 $this->doBlock( [ 'tags' => 'custom tag' ] );
156 public function testBlockWithHide() {
157 global $wgGroupPermissions;
158 $newPermissions = $wgGroupPermissions['sysop'];
159 $newPermissions['hideuser'] = true;
160 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
161 [ 'sysop' => $newPermissions ] );
163 $res = $this->doBlock( [ 'hidename' => '' ] );
165 $dbw = wfGetDB( DB_MASTER
);
166 $this->assertSame( '1', $dbw->selectField(
169 [ 'ipb_id' => $res[0]['block']['id'] ],
174 public function testBlockWithProhibitedHide() {
175 $this->setExpectedException( ApiUsageException
::class,
176 "You don't have permission to hide user names from the block log." );
178 $this->doBlock( [ 'hidename' => '' ] );
181 public function testBlockWithEmailBlock() {
182 $this->setMwGlobals( [
183 'wgEnableEmail' => true,
184 'wgEnableUserEmail' => true,
185 'wgSysopEmailBans' => true,
188 $res = $this->doBlock( [ 'noemail' => '' ] );
190 $dbw = wfGetDB( DB_MASTER
);
191 $this->assertSame( '1', $dbw->selectField(
194 [ 'ipb_id' => $res[0]['block']['id'] ],
199 public function testBlockWithProhibitedEmailBlock() {
200 $this->setMwGlobals( [
201 'wgEnableEmail' => true,
202 'wgEnableUserEmail' => true,
203 'wgSysopEmailBans' => true,
206 $this->setExpectedException( ApiUsageException
::class,
207 "You don't have permission to block users from sending email through the wiki." );
209 $this->setMwGlobals( 'wgRevokePermissions',
210 [ 'sysop' => [ 'blockemail' => true ] ] );
212 $this->doBlock( [ 'noemail' => '' ] );
215 public function testBlockWithExpiry() {
216 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
218 $dbw = wfGetDB( DB_MASTER
);
219 $expiry = $dbw->selectField(
222 [ 'ipb_id' => $res[0]['block']['id'] ],
226 // Allow flakiness up to one second
227 $this->assertLessThanOrEqual( 1,
228 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
231 public function testBlockWithInvalidExpiry() {
232 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
234 $this->doBlock( [ 'expiry' => '' ] );
237 public function testBlockWithoutRestrictions() {
238 $this->setMwGlobals( [
239 'wgEnablePartialBlocks' => true,
244 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
246 $this->assertTrue( $block->isSitewide() );
247 $this->assertCount( 0, $block->getRestrictions() );
250 public function testBlockWithRestrictions() {
251 $this->setMwGlobals( [
252 'wgEnablePartialBlocks' => true,
256 $page = $this->getExistingTestPage( $title );
257 $namespace = NS_TALK
;
261 'pagerestrictions' => $title,
262 'namespacerestrictions' => $namespace,
265 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
267 $this->assertFalse( $block->isSitewide() );
268 $this->assertCount( 2, $block->getRestrictions() );
269 $this->assertInstanceOf( PageRestriction
::class, $block->getRestrictions()[0] );
270 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
271 $this->assertInstanceOf( NamespaceRestriction
::class, $block->getRestrictions()[1] );
272 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
276 * @expectedException ApiUsageException
277 * @expectedExceptionMessage The "token" parameter must be set
279 public function testBlockingActionWithNoToken() {
283 'user' => $this->mUser
->getName(),
284 'reason' => 'Some reason',
288 self
::$users['sysop']->getUser()
293 * @expectedException ApiUsageException
294 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
296 public function testBlockWithLargeRange() {
297 $tokens = $this->getTokens();
302 'user' => '127.0.0.1/64',
303 'reason' => 'Some reason',
304 'token' => $tokens['blocktoken'],
308 self
::$users['sysop']->getUser()
313 * @expectedException ApiUsageException
314 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
317 public function testBlockingTooManyPageRestrictions() {
318 $this->setMwGlobals( [
319 'wgEnablePartialBlocks' => true,
322 $tokens = $this->getTokens();
327 'user' => $this->mUser
->getName(),
328 'reason' => 'Some reason',
330 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
331 'token' => $tokens['blocktoken'],
335 self
::$users['sysop']->getUser()
339 public function testRangeBlock() {
340 $this->mUser
= User
::newFromName( '128.0.0.0/16', false );
345 * @expectedException ApiUsageException
346 * @expectedExceptionMessage Range blocks larger than /16 are not allowed.
348 public function testVeryLargeRangeBlock() {
349 $this->mUser
= User
::newFromName( '128.0.0.0/1', false );