10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
16 $this->mUser
= $this->getMutableTestUser()->getUser();
17 $this->setMwGlobals( 'wgBlockCIDRLimit', [
23 protected function tearDown() {
24 $block = Block
::newFromTarget( $this->mUser
->getName() );
25 if ( !is_null( $block ) ) {
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 = Block
::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->mReason
);
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();
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( 'custom tag', $dbw->selectField(
132 [ 'change_tag', 'logging' ],
134 [ 'log_type' => 'block' ],
137 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
141 public function testBlockWithProhibitedTag() {
142 $this->setExpectedException( ApiUsageException
::class,
143 'You do not have permission to apply change tags along with your changes.' );
145 ChangeTags
::defineTag( 'custom tag' );
147 $this->setMwGlobals( 'wgRevokePermissions',
148 [ 'user' => [ 'applychangetags' => true ] ] );
150 $this->doBlock( [ 'tags' => 'custom tag' ] );
153 public function testBlockWithHide() {
154 global $wgGroupPermissions;
155 $newPermissions = $wgGroupPermissions['sysop'];
156 $newPermissions['hideuser'] = true;
157 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
158 [ 'sysop' => $newPermissions ] );
160 $res = $this->doBlock( [ 'hidename' => '' ] );
162 $dbw = wfGetDB( DB_MASTER
);
163 $this->assertSame( '1', $dbw->selectField(
166 [ 'ipb_id' => $res[0]['block']['id'] ],
171 public function testBlockWithProhibitedHide() {
172 $this->setExpectedException( ApiUsageException
::class,
173 "You don't have permission to hide user names from the block log." );
175 $this->doBlock( [ 'hidename' => '' ] );
178 public function testBlockWithEmailBlock() {
179 $res = $this->doBlock( [ 'noemail' => '' ] );
181 $dbw = wfGetDB( DB_MASTER
);
182 $this->assertSame( '1', $dbw->selectField(
185 [ 'ipb_id' => $res[0]['block']['id'] ],
190 public function testBlockWithProhibitedEmailBlock() {
191 $this->setExpectedException( ApiUsageException
::class,
192 "You don't have permission to block users from sending email through the wiki." );
194 $this->setMwGlobals( 'wgRevokePermissions',
195 [ 'sysop' => [ 'blockemail' => true ] ] );
197 $this->doBlock( [ 'noemail' => '' ] );
200 public function testBlockWithExpiry() {
201 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
203 $dbw = wfGetDB( DB_MASTER
);
204 $expiry = $dbw->selectField(
207 [ 'ipb_id' => $res[0]['block']['id'] ],
211 // Allow flakiness up to one second
212 $this->assertLessThanOrEqual( 1,
213 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
216 public function testBlockWithInvalidExpiry() {
217 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
219 $this->doBlock( [ 'expiry' => '' ] );
223 * @expectedException ApiUsageException
224 * @expectedExceptionMessage The "token" parameter must be set
226 public function testBlockingActionWithNoToken() {
230 'user' => $this->mUser
->getName(),
231 'reason' => 'Some reason',
235 self
::$users['sysop']->getUser()
239 public function testRangeBlock() {
240 $this->mUser
= User
::newFromName( '128.0.0.0/16', false );
245 * @expectedException ApiUsageException
246 * @expectedExceptionMessage Range blocks larger than /16 are not allowed.
248 public function testVeryLargeRangeBlock() {
249 $this->mUser
= User
::newFromName( '128.0.0.0/1', false );