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->resetServices();
155 $this->doBlock( [ 'tags' => 'custom tag' ] );
158 public function testBlockWithHide() {
159 global $wgGroupPermissions;
160 $newPermissions = $wgGroupPermissions['sysop'];
161 $newPermissions['hideuser'] = true;
162 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
163 [ 'sysop' => $newPermissions ] );
165 $this->resetServices();
166 $res = $this->doBlock( [ 'hidename' => '' ] );
168 $dbw = wfGetDB( DB_MASTER
);
169 $this->assertSame( '1', $dbw->selectField(
172 [ 'ipb_id' => $res[0]['block']['id'] ],
177 public function testBlockWithProhibitedHide() {
178 $this->setExpectedException( ApiUsageException
::class,
179 "You don't have permission to hide user names from the block log." );
181 $this->doBlock( [ 'hidename' => '' ] );
184 public function testBlockWithEmailBlock() {
185 $this->setMwGlobals( [
186 'wgEnableEmail' => true,
187 'wgEnableUserEmail' => true,
188 'wgSysopEmailBans' => true,
191 $res = $this->doBlock( [ 'noemail' => '' ] );
193 $dbw = wfGetDB( DB_MASTER
);
194 $this->assertSame( '1', $dbw->selectField(
197 [ 'ipb_id' => $res[0]['block']['id'] ],
202 public function testBlockWithProhibitedEmailBlock() {
203 $this->setMwGlobals( [
204 'wgEnableEmail' => true,
205 'wgEnableUserEmail' => true,
206 'wgSysopEmailBans' => true,
209 $this->setExpectedException( ApiUsageException
::class,
210 "You don't have permission to block users from sending email through the wiki." );
212 $this->setMwGlobals( 'wgRevokePermissions',
213 [ 'sysop' => [ 'blockemail' => true ] ] );
215 $this->resetServices();
217 $this->doBlock( [ 'noemail' => '' ] );
220 public function testBlockWithExpiry() {
221 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
223 $dbw = wfGetDB( DB_MASTER
);
224 $expiry = $dbw->selectField(
227 [ 'ipb_id' => $res[0]['block']['id'] ],
231 // Allow flakiness up to one second
232 $this->assertLessThanOrEqual( 1,
233 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
236 public function testBlockWithInvalidExpiry() {
237 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
239 $this->doBlock( [ 'expiry' => '' ] );
242 public function testBlockWithoutRestrictions() {
243 $this->setMwGlobals( [
244 'wgEnablePartialBlocks' => true,
249 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
251 $this->assertTrue( $block->isSitewide() );
252 $this->assertCount( 0, $block->getRestrictions() );
255 public function testBlockWithRestrictions() {
256 $this->setMwGlobals( [
257 'wgEnablePartialBlocks' => true,
261 $page = $this->getExistingTestPage( $title );
262 $namespace = NS_TALK
;
266 'pagerestrictions' => $title,
267 'namespacerestrictions' => $namespace,
268 'allowusertalk' => true,
271 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
273 $this->assertFalse( $block->isSitewide() );
274 $this->assertCount( 2, $block->getRestrictions() );
275 $this->assertInstanceOf( PageRestriction
::class, $block->getRestrictions()[0] );
276 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
277 $this->assertInstanceOf( NamespaceRestriction
::class, $block->getRestrictions()[1] );
278 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
282 * @expectedException ApiUsageException
283 * @expectedExceptionMessage The "token" parameter must be set
285 public function testBlockingActionWithNoToken() {
289 'user' => $this->mUser
->getName(),
290 'reason' => 'Some reason',
294 self
::$users['sysop']->getUser()
299 * @expectedException ApiUsageException
300 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
302 public function testBlockWithLargeRange() {
303 $tokens = $this->getTokens();
308 'user' => '127.0.0.1/64',
309 'reason' => 'Some reason',
310 'token' => $tokens['blocktoken'],
314 self
::$users['sysop']->getUser()
319 * @expectedException ApiUsageException
320 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
323 public function testBlockingTooManyPageRestrictions() {
324 $this->setMwGlobals( [
325 'wgEnablePartialBlocks' => true,
328 $tokens = $this->getTokens();
333 'user' => $this->mUser
->getName(),
334 'reason' => 'Some reason',
336 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
337 'token' => $tokens['blocktoken'],
341 self
::$users['sysop']->getUser()
345 public function testRangeBlock() {
346 $this->mUser
= User
::newFromName( '128.0.0.0/16', false );
351 * @expectedException ApiUsageException
352 * @expectedExceptionMessage Range blocks larger than /16 are not allowed.
354 public function testVeryLargeRangeBlock() {
355 $this->mUser
= User
::newFromName( '128.0.0.0/1', false );