10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
15 $this->tablesUsed
= array_merge(
17 [ 'change_tag', 'change_tag_def', 'logging' ]
20 $this->mUser
= $this->getMutableTestUser()->getUser();
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' );
48 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
50 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
54 'user' => $this->mUser
->getName(),
55 'reason' => 'Some reason',
56 'token' => $tokens['blocktoken'],
58 if ( array_key_exists( 'userid', $extraParams ) ) {
59 // Make sure we don't have both user and userid
60 unset( $params['user'] );
62 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
65 $block = Block
::newFromTarget( $this->mUser
->getName() );
67 $this->assertTrue( !is_null( $block ), 'Block is valid' );
69 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
70 $this->assertSame( 'Some reason', $block->mReason
);
78 public function testNormalBlock() {
85 public function testBlockById() {
86 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
90 * A blocked user can't block
92 public function testBlockByBlockedUser() {
93 $this->setExpectedException( ApiUsageException
::class,
94 'You cannot block or unblock other users because you are yourself blocked.' );
96 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
98 'address' => $blocked->getName(),
99 'by' => self
::$users['sysop']->getUser()->getId(),
100 'reason' => 'Capriciousness',
101 'timestamp' => '19370101000000',
102 'expiry' => 'infinity',
106 $this->doBlock( [], $blocked );
109 public function testBlockOfNonexistentUser() {
110 $this->setExpectedException( ApiUsageException
::class,
111 'There is no user by the name "Nonexistent". Check your spelling.' );
113 $this->doBlock( [ 'user' => 'Nonexistent' ] );
116 public function testBlockOfNonexistentUserId() {
118 $this->setExpectedException( ApiUsageException
::class,
119 "There is no user with ID $id." );
121 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
123 $this->doBlock( [ 'userid' => $id ] );
126 public function testBlockWithTag() {
127 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
128 ChangeTags
::defineTag( 'custom tag' );
130 $this->doBlock( [ 'tags' => 'custom tag' ] );
132 $dbw = wfGetDB( DB_MASTER
);
133 $this->assertSame( 1, (int)$dbw->selectField(
134 [ 'change_tag', 'logging' ],
136 [ 'log_type' => 'block', 'ct_tag' => 'custom tag' ],
139 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
143 public function testBlockWithTagNewBackend() {
144 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
145 ChangeTags
::defineTag( 'custom tag' );
147 $this->doBlock( [ 'tags' => 'custom tag' ] );
149 $dbw = wfGetDB( DB_MASTER
);
150 $this->assertSame( 1, (int)$dbw->selectField(
151 [ 'change_tag', 'logging', 'change_tag_def' ],
153 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
157 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
158 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
163 public function testBlockWithProhibitedTag() {
164 $this->setExpectedException( ApiUsageException
::class,
165 'You do not have permission to apply change tags along with your changes.' );
167 ChangeTags
::defineTag( 'custom tag' );
169 $this->setMwGlobals( 'wgRevokePermissions',
170 [ 'user' => [ 'applychangetags' => true ] ] );
172 $this->doBlock( [ 'tags' => 'custom tag' ] );
175 public function testBlockWithHide() {
176 global $wgGroupPermissions;
177 $newPermissions = $wgGroupPermissions['sysop'];
178 $newPermissions['hideuser'] = true;
179 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
180 [ 'sysop' => $newPermissions ] );
182 $res = $this->doBlock( [ 'hidename' => '' ] );
184 $dbw = wfGetDB( DB_MASTER
);
185 $this->assertSame( '1', $dbw->selectField(
188 [ 'ipb_id' => $res[0]['block']['id'] ],
193 public function testBlockWithProhibitedHide() {
194 $this->setExpectedException( ApiUsageException
::class,
195 "You don't have permission to hide user names from the block log." );
197 $this->doBlock( [ 'hidename' => '' ] );
200 public function testBlockWithEmailBlock() {
201 $res = $this->doBlock( [ 'noemail' => '' ] );
203 $dbw = wfGetDB( DB_MASTER
);
204 $this->assertSame( '1', $dbw->selectField(
207 [ 'ipb_id' => $res[0]['block']['id'] ],
212 public function testBlockWithProhibitedEmailBlock() {
213 $this->setExpectedException( ApiUsageException
::class,
214 "You don't have permission to block users from sending email through the wiki." );
216 $this->setMwGlobals( 'wgRevokePermissions',
217 [ 'sysop' => [ 'blockemail' => true ] ] );
219 $this->doBlock( [ 'noemail' => '' ] );
222 public function testBlockWithExpiry() {
223 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
225 $dbw = wfGetDB( DB_MASTER
);
226 $expiry = $dbw->selectField(
229 [ 'ipb_id' => $res[0]['block']['id'] ],
233 // Allow flakiness up to one second
234 $this->assertLessThanOrEqual( 1,
235 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
238 public function testBlockWithInvalidExpiry() {
239 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
241 $this->doBlock( [ 'expiry' => '' ] );
245 * @expectedException ApiUsageException
246 * @expectedExceptionMessage The "token" parameter must be set
248 public function testBlockingActionWithNoToken() {
252 'user' => $this->mUser
->getName(),
253 'reason' => 'Some reason',
257 self
::$users['sysop']->getUser()