10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
17 $this->mUser
= $this->getMutableTestUser()->getUser();
20 protected function tearDown() {
21 $block = Block
::newFromTarget( $this->mUser
->getName() );
22 if ( !is_null( $block ) ) {
28 protected function getTokens() {
29 return $this->getTokenList( self
::$users['sysop'] );
33 * @param array $extraParams Extra API parameters to pass to doApiRequest
34 * @param User $blocker User to do the blocking, null to pick
37 private function doBlock( array $extraParams = [], User
$blocker = null ) {
38 if ( $blocker === null ) {
39 $blocker = self
::$users['sysop']->getUser();
42 $tokens = $this->getTokens();
44 $this->assertNotNull( $this->mUser
, 'Sanity check' );
45 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
47 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
51 'user' => $this->mUser
->getName(),
52 'reason' => 'Some reason',
53 'token' => $tokens['blocktoken'],
55 if ( array_key_exists( 'userid', $extraParams ) ) {
56 // Make sure we don't have both user and userid
57 unset( $params['user'] );
59 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
62 $block = Block
::newFromTarget( $this->mUser
->getName() );
64 $this->assertTrue( !is_null( $block ), 'Block is valid' );
66 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
67 $this->assertSame( 'Some reason', $block->mReason
);
75 public function testNormalBlock() {
82 public function testBlockById() {
83 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
87 * A blocked user can't block
89 public function testBlockByBlockedUser() {
90 $this->setExpectedException( ApiUsageException
::class,
91 'You cannot block or unblock other users because you are yourself blocked.' );
93 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
95 'address' => $blocked->getName(),
96 'by' => self
::$users['sysop']->getUser()->getId(),
97 'reason' => 'Capriciousness',
98 'timestamp' => '19370101000000',
99 'expiry' => 'infinity',
103 $this->doBlock( [], $blocked );
106 public function testBlockOfNonexistentUser() {
107 $this->setExpectedException( ApiUsageException
::class,
108 'There is no user by the name "Nonexistent". Check your spelling.' );
110 $this->doBlock( [ 'user' => 'Nonexistent' ] );
113 public function testBlockOfNonexistentUserId() {
115 $this->setExpectedException( ApiUsageException
::class,
116 "There is no user with ID $id." );
118 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
120 $this->doBlock( [ 'userid' => $id ] );
123 public function testBlockWithTag() {
124 ChangeTags
::defineTag( 'custom tag' );
126 $this->doBlock( [ 'tags' => 'custom tag' ] );
128 $dbw = wfGetDB( DB_MASTER
);
129 $this->assertSame( 'custom tag', $dbw->selectField(
130 [ 'change_tag', 'logging' ],
132 [ 'log_type' => 'block' ],
135 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
139 public function testBlockWithProhibitedTag() {
140 $this->setExpectedException( ApiUsageException
::class,
141 'You do not have permission to apply change tags along with your changes.' );
143 ChangeTags
::defineTag( 'custom tag' );
145 $this->setMwGlobals( 'wgRevokePermissions',
146 [ 'user' => [ 'applychangetags' => true ] ] );
148 $this->doBlock( [ 'tags' => 'custom tag' ] );
151 public function testBlockWithHide() {
152 global $wgGroupPermissions;
153 $newPermissions = $wgGroupPermissions['sysop'];
154 $newPermissions['hideuser'] = true;
155 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
156 [ 'sysop' => $newPermissions ] );
158 $res = $this->doBlock( [ 'hidename' => '' ] );
160 $dbw = wfGetDB( DB_MASTER
);
161 $this->assertSame( '1', $dbw->selectField(
164 [ 'ipb_id' => $res[0]['block']['id'] ],
169 public function testBlockWithProhibitedHide() {
170 $this->setExpectedException( ApiUsageException
::class,
171 "You don't have permission to hide user names from the block log." );
173 $this->doBlock( [ 'hidename' => '' ] );
176 public function testBlockWithEmailBlock() {
177 $res = $this->doBlock( [ 'noemail' => '' ] );
179 $dbw = wfGetDB( DB_MASTER
);
180 $this->assertSame( '1', $dbw->selectField(
183 [ 'ipb_id' => $res[0]['block']['id'] ],
188 public function testBlockWithProhibitedEmailBlock() {
189 $this->setExpectedException( ApiUsageException
::class,
190 "You don't have permission to block users from sending email through the wiki." );
192 $this->setMwGlobals( 'wgRevokePermissions',
193 [ 'sysop' => [ 'blockemail' => true ] ] );
195 $this->doBlock( [ 'noemail' => '' ] );
198 public function testBlockWithExpiry() {
199 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
201 $dbw = wfGetDB( DB_MASTER
);
202 $expiry = $dbw->selectField(
205 [ 'ipb_id' => $res[0]['block']['id'] ],
209 // Allow flakiness up to one second
210 $this->assertLessThanOrEqual( 1,
211 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
214 public function testBlockWithInvalidExpiry() {
215 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
217 $this->doBlock( [ 'expiry' => '' ] );
221 * @expectedException ApiUsageException
222 * @expectedExceptionMessage The "token" parameter must be set
224 public function testBlockingActionWithNoToken() {
228 'user' => $this->mUser
->getName(),
229 'reason' => 'Some reason',
233 self
::$users['sysop']->getUser()