10 class ApiBlockTest
extends ApiTestCase
{
11 protected $mUser = null;
13 protected function setUp() {
15 $this->tablesUsed
= array_merge(
17 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
20 $this->mUser
= $this->getMutableTestUser()->getUser();
23 protected function getTokens() {
24 return $this->getTokenList( self
::$users['sysop'] );
28 * @param array $extraParams Extra API parameters to pass to doApiRequest
29 * @param User $blocker User to do the blocking, null to pick
32 private function doBlock( array $extraParams = [], User
$blocker = null ) {
33 if ( $blocker === null ) {
34 $blocker = self
::$users['sysop']->getUser();
37 $tokens = $this->getTokens();
39 $this->assertNotNull( $this->mUser
, 'Sanity check' );
40 $this->assertNotSame( 0, $this->mUser
->getId(), 'Sanity check' );
42 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
46 'user' => $this->mUser
->getName(),
47 'reason' => 'Some reason',
48 'token' => $tokens['blocktoken'],
50 if ( array_key_exists( 'userid', $extraParams ) ) {
51 // Make sure we don't have both user and userid
52 unset( $params['user'] );
54 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
57 $block = Block
::newFromTarget( $this->mUser
->getName() );
59 $this->assertTrue( !is_null( $block ), 'Block is valid' );
61 $this->assertSame( $this->mUser
->getName(), (string)$block->getTarget() );
62 $this->assertSame( 'Some reason', $block->mReason
);
70 public function testNormalBlock() {
77 public function testBlockById() {
78 $this->doBlock( [ 'userid' => $this->mUser
->getId() ] );
82 * A blocked user can't block
84 public function testBlockByBlockedUser() {
85 $this->setExpectedException( ApiUsageException
::class,
86 'You cannot block or unblock other users because you are yourself blocked.' );
88 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
90 'address' => $blocked->getName(),
91 'by' => self
::$users['sysop']->getUser()->getId(),
92 'reason' => 'Capriciousness',
93 'timestamp' => '19370101000000',
94 'expiry' => 'infinity',
98 $this->doBlock( [], $blocked );
101 public function testBlockOfNonexistentUser() {
102 $this->setExpectedException( ApiUsageException
::class,
103 'There is no user by the name "Nonexistent". Check your spelling.' );
105 $this->doBlock( [ 'user' => 'Nonexistent' ] );
108 public function testBlockOfNonexistentUserId() {
110 $this->setExpectedException( ApiUsageException
::class,
111 "There is no user with ID $id." );
113 $this->assertFalse( User
::whoIs( $id ), 'Sanity check' );
115 $this->doBlock( [ 'userid' => $id ] );
118 public function testBlockWithTag() {
119 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH
);
120 ChangeTags
::defineTag( 'custom tag' );
122 $this->doBlock( [ 'tags' => 'custom tag' ] );
124 $dbw = wfGetDB( DB_MASTER
);
125 $this->assertSame( 1, (int)$dbw->selectField(
126 [ 'change_tag', 'logging' ],
128 [ 'log_type' => 'block', 'ct_tag' => 'custom tag' ],
131 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
135 public function testBlockWithTagNewBackend() {
136 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW
);
137 ChangeTags
::defineTag( 'custom tag' );
139 $this->doBlock( [ 'tags' => 'custom tag' ] );
141 $dbw = wfGetDB( DB_MASTER
);
142 $this->assertSame( 1, (int)$dbw->selectField(
143 [ 'change_tag', 'logging', 'change_tag_def' ],
145 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
149 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
150 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
155 public function testBlockWithProhibitedTag() {
156 $this->setExpectedException( ApiUsageException
::class,
157 'You do not have permission to apply change tags along with your changes.' );
159 ChangeTags
::defineTag( 'custom tag' );
161 $this->setMwGlobals( 'wgRevokePermissions',
162 [ 'user' => [ 'applychangetags' => true ] ] );
164 $this->doBlock( [ 'tags' => 'custom tag' ] );
167 public function testBlockWithHide() {
168 global $wgGroupPermissions;
169 $newPermissions = $wgGroupPermissions['sysop'];
170 $newPermissions['hideuser'] = true;
171 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
172 [ 'sysop' => $newPermissions ] );
174 $res = $this->doBlock( [ 'hidename' => '' ] );
176 $dbw = wfGetDB( DB_MASTER
);
177 $this->assertSame( '1', $dbw->selectField(
180 [ 'ipb_id' => $res[0]['block']['id'] ],
185 public function testBlockWithProhibitedHide() {
186 $this->setExpectedException( ApiUsageException
::class,
187 "You don't have permission to hide user names from the block log." );
189 $this->doBlock( [ 'hidename' => '' ] );
192 public function testBlockWithEmailBlock() {
193 $res = $this->doBlock( [ 'noemail' => '' ] );
195 $dbw = wfGetDB( DB_MASTER
);
196 $this->assertSame( '1', $dbw->selectField(
199 [ 'ipb_id' => $res[0]['block']['id'] ],
204 public function testBlockWithProhibitedEmailBlock() {
205 $this->setExpectedException( ApiUsageException
::class,
206 "You don't have permission to block users from sending email through the wiki." );
208 $this->setMwGlobals( 'wgRevokePermissions',
209 [ 'sysop' => [ 'blockemail' => true ] ] );
211 $this->doBlock( [ 'noemail' => '' ] );
214 public function testBlockWithExpiry() {
215 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
217 $dbw = wfGetDB( DB_MASTER
);
218 $expiry = $dbw->selectField(
221 [ 'ipb_id' => $res[0]['block']['id'] ],
225 // Allow flakiness up to one second
226 $this->assertLessThanOrEqual( 1,
227 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
230 public function testBlockWithInvalidExpiry() {
231 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
233 $this->doBlock( [ 'expiry' => '' ] );
237 * @expectedException ApiUsageException
238 * @expectedExceptionMessage The "token" parameter must be set
240 public function testBlockingActionWithNoToken() {
244 'user' => $this->mUser
->getName(),
245 'reason' => 'Some reason',
249 self
::$users['sysop']->getUser()