10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
16 $this->mUser
= $this->getMutableTestUser()->getUser();
19 protected function tearDown() {
20 $block = Block
::newFromTarget( $this->mUser
->getName() );
21 if ( !is_null( $block ) ) {
27 protected function getTokens() {
28 return $this->getTokenList( self
::$users['sysop'] );
32 * @param array $extraParams Extra API parameters to pass to doApiRequest
33 * @param User $blocker User to do the blocking, null to pick
36 private function doBlock( array $extraParams = [], User
$blocker = null ) {
37 if ( $blocker === null ) {
38 $blocker = self
::$users['sysop']->getUser();
41 $tokens = $this->getTokens();
43 $this->assertNotNull( $this->mUser
, 'Sanity check' );
44 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
46 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
50 'user' => $this->mUser
->getName(),
51 'reason' => 'Some reason',
52 'token' => $tokens['blocktoken'],
54 if ( array_key_exists( 'userid', $extraParams ) ) {
55 // Make sure we don't have both user and userid
56 unset( $params['user'] );
58 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
61 $block = Block
::newFromTarget( $this->mUser
->getName() );
63 $this->assertTrue( !is_null( $block ), 'Block is valid' );
65 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
66 $this->assertSame( 'Some reason', $block->mReason
);
74 public function testNormalBlock() {
81 public function testBlockById() {
82 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
86 * A blocked user can't block
88 public function testBlockByBlockedUser() {
89 $this->setExpectedException( ApiUsageException
::class,
90 'You cannot block or unblock other users because you are yourself blocked.' );
92 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
94 'address' => $blocked->getName(),
95 'by' => self
::$users['sysop']->getUser()->getId(),
96 'reason' => 'Capriciousness',
97 'timestamp' => '19370101000000',
98 'expiry' => 'infinity',
102 $this->doBlock( [], $blocked );
105 public function testBlockOfNonexistentUser() {
106 $this->setExpectedException( ApiUsageException
::class,
107 'There is no user by the name "Nonexistent". Check your spelling.' );
109 $this->doBlock( [ 'user' => 'Nonexistent' ] );
112 public function testBlockOfNonexistentUserId() {
114 $this->setExpectedException( ApiUsageException
::class,
115 "There is no user with ID $id." );
117 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
119 $this->doBlock( [ 'userid' => $id ] );
122 public function testBlockWithTag() {
123 ChangeTags
::defineTag( 'custom tag' );
125 $this->doBlock( [ 'tags' => 'custom tag' ] );
127 $dbw = wfGetDB( DB_MASTER
);
128 $this->assertSame( 'custom tag', $dbw->selectField(
129 [ 'change_tag', 'logging' ],
131 [ 'log_type' => 'block' ],
134 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
138 public function testBlockWithProhibitedTag() {
139 $this->setExpectedException( ApiUsageException
::class,
140 'You do not have permission to apply change tags along with your changes.' );
142 ChangeTags
::defineTag( 'custom tag' );
144 $this->setMwGlobals( 'wgRevokePermissions',
145 [ 'user' => [ 'applychangetags' => true ] ] );
147 $this->doBlock( [ 'tags' => 'custom tag' ] );
150 public function testBlockWithHide() {
151 global $wgGroupPermissions;
152 $newPermissions = $wgGroupPermissions['sysop'];
153 $newPermissions['hideuser'] = true;
154 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
155 [ 'sysop' => $newPermissions ] );
157 $res = $this->doBlock( [ 'hidename' => '' ] );
159 $dbw = wfGetDB( DB_MASTER
);
160 $this->assertSame( '1', $dbw->selectField(
163 [ 'ipb_id' => $res[0]['block']['id'] ],
168 public function testBlockWithProhibitedHide() {
169 $this->setExpectedException( ApiUsageException
::class,
170 "You don't have permission to hide user names from the block log." );
172 $this->doBlock( [ 'hidename' => '' ] );
175 public function testBlockWithEmailBlock() {
176 $res = $this->doBlock( [ 'noemail' => '' ] );
178 $dbw = wfGetDB( DB_MASTER
);
179 $this->assertSame( '1', $dbw->selectField(
182 [ 'ipb_id' => $res[0]['block']['id'] ],
187 public function testBlockWithProhibitedEmailBlock() {
188 $this->setExpectedException( ApiUsageException
::class,
189 "You don't have permission to block users from sending email through the wiki." );
191 $this->setMwGlobals( 'wgRevokePermissions',
192 [ 'sysop' => [ 'blockemail' => true ] ] );
194 $this->doBlock( [ 'noemail' => '' ] );
197 public function testBlockWithExpiry() {
198 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
200 $dbw = wfGetDB( DB_MASTER
);
201 $expiry = $dbw->selectField(
204 [ 'ipb_id' => $res[0]['block']['id'] ],
208 // Allow flakiness up to one second
209 $this->assertLessThanOrEqual( 1,
210 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
213 public function testBlockWithInvalidExpiry() {
214 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
216 $this->doBlock( [ 'expiry' => '' ] );
220 * @expectedException ApiUsageException
221 * @expectedExceptionMessage The "token" parameter must be set
223 public function testBlockingActionWithNoToken() {
227 'user' => $this->mUser
->getName(),
228 'reason' => 'Some reason',
232 self
::$users['sysop']->getUser()