2011-01-02 19:58:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
2012-04-02 13:33:43 +00:00
|
|
|
* @group API
|
2011-01-02 19:58:27 +00:00
|
|
|
* @group Database
|
2013-01-18 19:02:28 +00:00
|
|
|
* @group medium
|
2013-10-23 16:01:33 +00:00
|
|
|
*
|
|
|
|
|
* @covers ApiBlock
|
2011-01-02 19:58:27 +00:00
|
|
|
*/
|
2011-07-01 16:34:02 +00:00
|
|
|
class ApiBlockTest extends ApiTestCase {
|
2018-03-18 18:48:51 +00:00
|
|
|
protected $mUser = null;
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2011-01-02 19:58:27 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
$this->doLogin();
|
2018-03-18 18:48:51 +00:00
|
|
|
|
|
|
|
|
$this->mUser = $this->getMutableTestUser()->getUser();
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2016-12-07 17:04:02 +00:00
|
|
|
protected function tearDown() {
|
2018-03-18 18:48:51 +00:00
|
|
|
$block = Block::newFromTarget( $this->mUser->getName() );
|
2016-12-07 17:04:02 +00:00
|
|
|
if ( !is_null( $block ) ) {
|
|
|
|
|
$block->delete();
|
|
|
|
|
}
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-23 16:01:33 +00:00
|
|
|
protected function getTokens() {
|
2011-07-01 16:34:02 +00:00
|
|
|
return $this->getTokenList( self::$users['sysop'] );
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 13:05:50 +00:00
|
|
|
/**
|
2018-03-18 18:48:51 +00:00
|
|
|
* @param array $extraParams Extra API parameters to pass to doApiRequest
|
|
|
|
|
* @param User $blocker User to do the blocking, null to pick
|
|
|
|
|
* arbitrarily
|
2012-04-03 13:05:50 +00:00
|
|
|
*/
|
2018-03-18 18:48:51 +00:00
|
|
|
private function doBlock( array $extraParams = [], User $blocker = null ) {
|
|
|
|
|
if ( $blocker === null ) {
|
|
|
|
|
$blocker = self::$users['sysop']->getUser();
|
|
|
|
|
}
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$tokens = $this->getTokens();
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->assertNotNull( $this->mUser, 'Sanity check' );
|
|
|
|
|
$this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$params = [
|
2011-01-02 19:58:27 +00:00
|
|
|
'action' => 'block',
|
2018-03-18 18:48:51 +00:00
|
|
|
'user' => $this->mUser->getName(),
|
2011-08-08 16:08:48 +00:00
|
|
|
'reason' => 'Some reason',
|
2018-03-18 18:48:51 +00:00
|
|
|
'token' => $tokens['blocktoken'],
|
|
|
|
|
];
|
|
|
|
|
if ( array_key_exists( 'userid', $extraParams ) ) {
|
|
|
|
|
// Make sure we don't have both user and userid
|
|
|
|
|
unset( $params['user'] );
|
|
|
|
|
}
|
|
|
|
|
$ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
|
|
|
|
|
false, $blocker );
|
2011-03-21 19:12:41 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$block = Block::newFromTarget( $this->mUser->getName() );
|
2011-06-02 19:32:45 +00:00
|
|
|
|
2011-01-02 19:58:27 +00:00
|
|
|
$this->assertTrue( !is_null( $block ), 'Block is valid' );
|
|
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
|
|
|
|
|
$this->assertSame( 'Some reason', $block->mReason );
|
|
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Block by username
|
|
|
|
|
*/
|
|
|
|
|
public function testNormalBlock() {
|
|
|
|
|
$this->doBlock();
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
2016-12-07 17:04:02 +00:00
|
|
|
/**
|
|
|
|
|
* Block by user ID
|
|
|
|
|
*/
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockById() {
|
|
|
|
|
$this->doBlock( [ 'userid' => $this->mUser->getId() ] );
|
|
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
/**
|
|
|
|
|
* A blocked user can't block
|
|
|
|
|
*/
|
|
|
|
|
public function testBlockByBlockedUser() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
'You cannot block or unblock other users because you are yourself blocked.' );
|
|
|
|
|
|
|
|
|
|
$blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
|
|
|
|
|
$block = new Block( [
|
|
|
|
|
'address' => $blocked->getName(),
|
|
|
|
|
'by' => self::$users['sysop']->getUser()->getId(),
|
|
|
|
|
'reason' => 'Capriciousness',
|
|
|
|
|
'timestamp' => '19370101000000',
|
|
|
|
|
'expiry' => 'infinity',
|
|
|
|
|
] );
|
|
|
|
|
$block->insert();
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [], $blocked );
|
|
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockOfNonexistentUser() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
'There is no user by the name "Nonexistent". Check your spelling.' );
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->doBlock( [ 'user' => 'Nonexistent' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockOfNonexistentUserId() {
|
|
|
|
|
$id = 948206325;
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
"There is no user with ID $id." );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( User::whoIs( $id ), 'Sanity check' );
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [ 'userid' => $id ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithTag() {
|
|
|
|
|
ChangeTags::defineTag( 'custom tag' );
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$this->assertSame( 'custom tag', $dbw->selectField(
|
|
|
|
|
[ 'change_tag', 'logging' ],
|
|
|
|
|
'ct_tag',
|
|
|
|
|
[ 'log_type' => 'block' ],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
[],
|
|
|
|
|
[ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithProhibitedTag() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
'You do not have permission to apply change tags along with your changes.' );
|
|
|
|
|
|
|
|
|
|
ChangeTags::defineTag( 'custom tag' );
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgRevokePermissions',
|
|
|
|
|
[ 'user' => [ 'applychangetags' => true ] ] );
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithHide() {
|
|
|
|
|
global $wgGroupPermissions;
|
|
|
|
|
$newPermissions = $wgGroupPermissions['sysop'];
|
|
|
|
|
$newPermissions['hideuser'] = true;
|
|
|
|
|
$this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
|
|
|
|
|
[ 'sysop' => $newPermissions ] );
|
|
|
|
|
|
|
|
|
|
$res = $this->doBlock( [ 'hidename' => '' ] );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$this->assertSame( '1', $dbw->selectField(
|
|
|
|
|
'ipblocks',
|
|
|
|
|
'ipb_deleted',
|
|
|
|
|
[ 'ipb_id' => $res[0]['block']['id'] ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithProhibitedHide() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
"You don't have permission to hide user names from the block log." );
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [ 'hidename' => '' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithEmailBlock() {
|
|
|
|
|
$res = $this->doBlock( [ 'noemail' => '' ] );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$this->assertSame( '1', $dbw->selectField(
|
|
|
|
|
'ipblocks',
|
|
|
|
|
'ipb_block_email',
|
|
|
|
|
[ 'ipb_id' => $res[0]['block']['id'] ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithProhibitedEmailBlock() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class,
|
|
|
|
|
"You don't have permission to block users from sending email through the wiki." );
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( 'wgRevokePermissions',
|
|
|
|
|
[ 'sysop' => [ 'blockemail' => true ] ] );
|
|
|
|
|
|
|
|
|
|
$this->doBlock( [ 'noemail' => '' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testBlockWithExpiry() {
|
|
|
|
|
$res = $this->doBlock( [ 'expiry' => '1 day' ] );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$expiry = $dbw->selectField(
|
|
|
|
|
'ipblocks',
|
|
|
|
|
'ipb_expiry',
|
|
|
|
|
[ 'ipb_id' => $res[0]['block']['id'] ],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Allow flakiness up to one second
|
|
|
|
|
$this->assertLessThan( 1,
|
|
|
|
|
abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
|
|
|
|
|
}
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
public function testBlockWithInvalidExpiry() {
|
|
|
|
|
$this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
|
2016-12-07 17:04:02 +00:00
|
|
|
|
2018-03-18 18:48:51 +00:00
|
|
|
$this->doBlock( [ 'expiry' => '' ] );
|
2016-12-07 17:04:02 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-03 20:02:27 +00:00
|
|
|
/**
|
2016-10-19 16:54:25 +00:00
|
|
|
* @expectedException ApiUsageException
|
|
|
|
|
* @expectedExceptionMessage The "token" parameter must be set
|
2012-04-03 20:02:27 +00:00
|
|
|
*/
|
2015-06-16 19:06:19 +00:00
|
|
|
public function testBlockingActionWithNoToken() {
|
2012-04-03 20:02:27 +00:00
|
|
|
$this->doApiRequest(
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2013-10-23 16:01:33 +00:00
|
|
|
'action' => 'block',
|
2018-03-18 18:48:51 +00:00
|
|
|
'user' => $this->mUser->getName(),
|
2012-04-03 20:02:27 +00:00
|
|
|
'reason' => 'Some reason',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2012-04-03 20:02:27 +00:00
|
|
|
null,
|
|
|
|
|
false,
|
2015-08-07 16:10:26 +00:00
|
|
|
self::$users['sysop']->getUser()
|
2012-04-03 20:02:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
2011-01-02 19:58:27 +00:00
|
|
|
}
|