3 use MediaWiki\Block\DatabaseBlock
;
4 use MediaWiki\Block\Restriction\PageRestriction
;
5 use MediaWiki\Block\Restriction\NamespaceRestriction
;
14 class ApiBlockTest
extends ApiTestCase
{
15 protected $mUser = null;
17 protected function setUp() {
19 $this->tablesUsed
= array_merge(
21 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
24 $this->mUser
= $this->getMutableTestUser()->getUser();
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 = DatabaseBlock
::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->getReason() );
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();
93 $block = new DatabaseBlock( [
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( 1, (int)$dbw->selectField(
129 [ 'change_tag', 'logging', 'change_tag_def' ],
131 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
135 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
136 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_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 $this->setMwGlobals( [
180 'wgEnableEmail' => true,
181 'wgEnableUserEmail' => true,
182 'wgSysopEmailBans' => true,
185 $res = $this->doBlock( [ 'noemail' => '' ] );
187 $dbw = wfGetDB( DB_MASTER
);
188 $this->assertSame( '1', $dbw->selectField(
191 [ 'ipb_id' => $res[0]['block']['id'] ],
196 public function testBlockWithProhibitedEmailBlock() {
197 $this->setMwGlobals( [
198 'wgEnableEmail' => true,
199 'wgEnableUserEmail' => true,
200 'wgSysopEmailBans' => true,
203 $this->setExpectedException( ApiUsageException
::class,
204 "You don't have permission to block users from sending email through the wiki." );
206 $this->setMwGlobals( 'wgRevokePermissions',
207 [ 'sysop' => [ 'blockemail' => true ] ] );
209 $this->doBlock( [ 'noemail' => '' ] );
212 public function testBlockWithExpiry() {
213 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
215 $dbw = wfGetDB( DB_MASTER
);
216 $expiry = $dbw->selectField(
219 [ 'ipb_id' => $res[0]['block']['id'] ],
223 // Allow flakiness up to one second
224 $this->assertLessThanOrEqual( 1,
225 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
228 public function testBlockWithInvalidExpiry() {
229 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
231 $this->doBlock( [ 'expiry' => '' ] );
234 public function testBlockWithoutRestrictions() {
235 $this->setMwGlobals( [
236 'wgEnablePartialBlocks' => true,
241 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
243 $this->assertTrue( $block->isSitewide() );
244 $this->assertCount( 0, $block->getRestrictions() );
247 public function testBlockWithRestrictions() {
248 $this->setMwGlobals( [
249 'wgEnablePartialBlocks' => true,
253 $page = $this->getExistingTestPage( $title );
254 $namespace = NS_TALK
;
258 'pagerestrictions' => $title,
259 'namespacerestrictions' => $namespace,
262 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
264 $this->assertFalse( $block->isSitewide() );
265 $this->assertCount( 2, $block->getRestrictions() );
266 $this->assertInstanceOf( PageRestriction
::class, $block->getRestrictions()[0] );
267 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
268 $this->assertInstanceOf( NamespaceRestriction
::class, $block->getRestrictions()[1] );
269 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
273 * @expectedException ApiUsageException
274 * @expectedExceptionMessage The "token" parameter must be set
276 public function testBlockingActionWithNoToken() {
280 'user' => $this->mUser
->getName(),
281 'reason' => 'Some reason',
285 self
::$users['sysop']->getUser()
290 * @expectedException ApiUsageException
291 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
293 public function testBlockWithLargeRange() {
294 $tokens = $this->getTokens();
299 'user' => '127.0.0.1/64',
300 'reason' => 'Some reason',
301 'token' => $tokens['blocktoken'],
305 self
::$users['sysop']->getUser()
310 * @expectedException ApiUsageException
311 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
314 public function testBlockingToManyPageRestrictions() {
315 $this->setMwGlobals( [
316 'wgEnablePartialBlocks' => true,
319 $tokens = $this->getTokens();
324 'user' => $this->mUser
->getName(),
325 'reason' => 'Some reason',
327 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
328 'token' => $tokens['blocktoken'],
332 self
::$users['sysop']->getUser()