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->overrideMwServices();
152 $this->doBlock( [ 'tags' => 'custom tag' ] );
155 public function testBlockWithHide() {
156 global $wgGroupPermissions;
157 $newPermissions = $wgGroupPermissions['sysop'];
158 $newPermissions['hideuser'] = true;
159 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
160 [ 'sysop' => $newPermissions ] );
162 $this->overrideMwServices();
163 $res = $this->doBlock( [ 'hidename' => '' ] );
165 $dbw = wfGetDB( DB_MASTER
);
166 $this->assertSame( '1', $dbw->selectField(
169 [ 'ipb_id' => $res[0]['block']['id'] ],
174 public function testBlockWithProhibitedHide() {
175 $this->setExpectedException( ApiUsageException
::class,
176 "You don't have permission to hide user names from the block log." );
178 $this->doBlock( [ 'hidename' => '' ] );
181 public function testBlockWithEmailBlock() {
182 $this->setMwGlobals( [
183 'wgEnableEmail' => true,
184 'wgEnableUserEmail' => true,
185 'wgSysopEmailBans' => true,
188 $res = $this->doBlock( [ 'noemail' => '' ] );
190 $dbw = wfGetDB( DB_MASTER
);
191 $this->assertSame( '1', $dbw->selectField(
194 [ 'ipb_id' => $res[0]['block']['id'] ],
199 public function testBlockWithProhibitedEmailBlock() {
200 $this->setMwGlobals( [
201 'wgEnableEmail' => true,
202 'wgEnableUserEmail' => true,
203 'wgSysopEmailBans' => true,
206 $this->setExpectedException( ApiUsageException
::class,
207 "You don't have permission to block users from sending email through the wiki." );
209 $this->setMwGlobals( 'wgRevokePermissions',
210 [ 'sysop' => [ 'blockemail' => true ] ] );
212 $this->overrideMwServices();
214 $this->doBlock( [ 'noemail' => '' ] );
217 public function testBlockWithExpiry() {
218 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
220 $dbw = wfGetDB( DB_MASTER
);
221 $expiry = $dbw->selectField(
224 [ 'ipb_id' => $res[0]['block']['id'] ],
228 // Allow flakiness up to one second
229 $this->assertLessThanOrEqual( 1,
230 abs( wfTimestamp( TS_UNIX
, $expiry ) - ( time() +
86400 ) ) );
233 public function testBlockWithInvalidExpiry() {
234 $this->setExpectedException( ApiUsageException
::class, "Expiry time invalid." );
236 $this->doBlock( [ 'expiry' => '' ] );
239 public function testBlockWithoutRestrictions() {
240 $this->setMwGlobals( [
241 'wgEnablePartialBlocks' => true,
246 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
248 $this->assertTrue( $block->isSitewide() );
249 $this->assertCount( 0, $block->getRestrictions() );
252 public function testBlockWithRestrictions() {
253 $this->setMwGlobals( [
254 'wgEnablePartialBlocks' => true,
258 $page = $this->getExistingTestPage( $title );
259 $namespace = NS_TALK
;
263 'pagerestrictions' => $title,
264 'namespacerestrictions' => $namespace,
267 $block = DatabaseBlock
::newFromTarget( $this->mUser
->getName() );
269 $this->assertFalse( $block->isSitewide() );
270 $this->assertCount( 2, $block->getRestrictions() );
271 $this->assertInstanceOf( PageRestriction
::class, $block->getRestrictions()[0] );
272 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
273 $this->assertInstanceOf( NamespaceRestriction
::class, $block->getRestrictions()[1] );
274 $this->assertEquals( $namespace, $block->getRestrictions()[1]->getValue() );
278 * @expectedException ApiUsageException
279 * @expectedExceptionMessage The "token" parameter must be set
281 public function testBlockingActionWithNoToken() {
285 'user' => $this->mUser
->getName(),
286 'reason' => 'Some reason',
290 self
::$users['sysop']->getUser()
295 * @expectedException ApiUsageException
296 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
298 public function testBlockWithLargeRange() {
299 $tokens = $this->getTokens();
304 'user' => '127.0.0.1/64',
305 'reason' => 'Some reason',
306 'token' => $tokens['blocktoken'],
310 self
::$users['sysop']->getUser()
315 * @expectedException ApiUsageException
316 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
319 public function testBlockingToManyPageRestrictions() {
320 $this->setMwGlobals( [
321 'wgEnablePartialBlocks' => true,
324 $tokens = $this->getTokens();
329 'user' => $this->mUser
->getName(),
330 'reason' => 'Some reason',
332 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
333 'token' => $tokens['blocktoken'],
337 self
::$users['sysop']->getUser()