2013-10-23 16:01:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
2019-05-13 14:18:07 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2023-12-11 14:59:55 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2019-05-13 14:18:07 +00:00
|
|
|
|
2013-10-23 16:01:33 +00:00
|
|
|
/**
|
|
|
|
|
* @group API
|
|
|
|
|
* @group Database
|
|
|
|
|
* @group medium
|
|
|
|
|
*
|
|
|
|
|
* @covers ApiUnblock
|
|
|
|
|
*/
|
|
|
|
|
class ApiUnblockTest extends ApiTestCase {
|
2018-10-04 12:57:10 +00:00
|
|
|
/** @var User */
|
|
|
|
|
private $blocker;
|
|
|
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
|
private $blockee;
|
|
|
|
|
|
2021-07-22 03:11:47 +00:00
|
|
|
protected function setUp(): void {
|
2018-10-04 12:57:10 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->blocker = $this->getTestSysop()->getUser();
|
|
|
|
|
$this->blockee = $this->getMutableTestUser()->getUser();
|
|
|
|
|
|
|
|
|
|
// Initialize a blocked user (used by most tests, although not all)
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( [
|
2018-10-04 12:57:10 +00:00
|
|
|
'address' => $this->blockee->getName(),
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $this->blocker,
|
2018-10-04 12:57:10 +00:00
|
|
|
] );
|
2023-10-31 07:57:33 +00:00
|
|
|
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
|
|
|
|
|
$result = $blockStore->insertBlock( $block );
|
2018-10-04 12:57:10 +00:00
|
|
|
$this->assertNotFalse( $result, 'Could not insert block' );
|
2023-10-31 07:57:33 +00:00
|
|
|
$blockFromDB = $blockStore->newFromID( $result['id'] );
|
2023-11-23 08:28:31 +00:00
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $blockFromDB, 'Could not retrieve block' );
|
2018-10-04 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function getBlockFromParams( array $params ) {
|
2023-10-31 07:57:33 +00:00
|
|
|
$blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
|
2018-10-04 12:57:10 +00:00
|
|
|
if ( array_key_exists( 'user', $params ) ) {
|
2023-10-31 07:57:33 +00:00
|
|
|
return $blockStore->newFromTarget( $params['user'] );
|
2018-10-04 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
if ( array_key_exists( 'userid', $params ) ) {
|
2023-10-31 07:57:33 +00:00
|
|
|
return $blockStore->newFromTarget(
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getUserFactory()->newFromId( $params['userid'] )
|
|
|
|
|
);
|
2018-10-04 12:57:10 +00:00
|
|
|
}
|
2023-10-31 07:57:33 +00:00
|
|
|
return $blockStore->newFromID( $params['id'] );
|
2018-10-04 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Try to submit the unblock API request and check that the block no longer exists.
|
|
|
|
|
*
|
|
|
|
|
* @param array $params API request query parameters
|
|
|
|
|
*/
|
|
|
|
|
private function doUnblock( array $params = [] ) {
|
|
|
|
|
$params += [ 'action' => 'unblock' ];
|
|
|
|
|
if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) {
|
|
|
|
|
$params += [ 'user' => $this->blockee->getName() ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$originalBlock = $this->getBlockFromParams( $params );
|
|
|
|
|
|
|
|
|
|
$this->doApiRequestWithToken( $params );
|
|
|
|
|
|
|
|
|
|
// We only check later on whether the block existed to begin with, because maybe the caller
|
|
|
|
|
// expects doApiRequestWithToken to throw, in which case the block might not be expected to
|
|
|
|
|
// exist to begin with.
|
2019-05-13 14:18:07 +00:00
|
|
|
$this->assertInstanceOf( DatabaseBlock::class, $originalBlock, 'Block should initially exist' );
|
2018-10-04 12:57:10 +00:00
|
|
|
$this->assertNull( $this->getBlockFromParams( $params ), 'Block should have been removed' );
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-16 19:06:19 +00:00
|
|
|
public function testWithNoToken() {
|
2019-10-11 22:22:26 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
2018-10-04 12:57:10 +00:00
|
|
|
$this->doApiRequest( [
|
|
|
|
|
'action' => 'unblock',
|
|
|
|
|
'user' => $this->blockee->getName(),
|
|
|
|
|
'reason' => 'Some reason',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNormalUnblock() {
|
|
|
|
|
$this->doUnblock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockNoPermission() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'permissiondenied' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->setGroupPermissions( 'sysop', 'block', false );
|
|
|
|
|
|
|
|
|
|
$this->doUnblock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockWhenBlocked() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'ipbblocked' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( [
|
2018-10-04 12:57:10 +00:00
|
|
|
'address' => $this->blocker->getName(),
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $this->getTestUser( 'sysop' )->getUser(),
|
2018-10-04 12:57:10 +00:00
|
|
|
] );
|
2022-01-12 20:13:39 +00:00
|
|
|
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->doUnblock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockSelfWhenBlocked() {
|
2019-05-13 14:18:07 +00:00
|
|
|
$block = new DatabaseBlock( [
|
2018-10-04 12:57:10 +00:00
|
|
|
'address' => $this->blocker->getName(),
|
2021-06-02 09:44:38 +00:00
|
|
|
'by' => $this->getTestUser( 'sysop' )->getUser(),
|
2018-10-04 12:57:10 +00:00
|
|
|
] );
|
2022-01-12 20:13:39 +00:00
|
|
|
$result = $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
|
2018-10-04 12:57:10 +00:00
|
|
|
$this->assertNotFalse( $result, 'Could not insert block' );
|
|
|
|
|
|
|
|
|
|
$this->doUnblock( [ 'user' => $this->blocker->getName() ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockWithTagNewBackend() {
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->doUnblock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
|
2023-09-25 18:49:16 +00:00
|
|
|
$this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
|
2023-09-21 16:37:37 +00:00
|
|
|
->select( 'COUNT(*)' )
|
|
|
|
|
->from( 'logging' )
|
|
|
|
|
->join( 'change_tag', null, 'ct_log_id = log_id' )
|
|
|
|
|
->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
|
|
|
|
|
->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
|
|
|
|
|
->caller( __METHOD__ )->fetchField() );
|
2018-10-04 12:57:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockWithProhibitedTag() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'tags-apply-no-permission' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
2023-07-29 09:44:29 +00:00
|
|
|
$this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->setGroupPermissions( 'user', 'applychangetags', false );
|
|
|
|
|
|
|
|
|
|
$this->doUnblock( [ 'tags' => 'custom tag' ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockById() {
|
|
|
|
|
$this->doUnblock( [ 'userid' => $this->blockee->getId() ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockByInvalidId() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'nosuchuserid' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->doUnblock( [ 'userid' => 1234567890 ] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUnblockNonexistentBlock() {
|
2023-04-26 09:16:20 +00:00
|
|
|
$this->expectApiErrorCode( 'cantunblock' );
|
2018-10-04 12:57:10 +00:00
|
|
|
|
|
|
|
|
$this->doUnblock( [ 'user' => $this->blocker ] );
|
2013-10-23 16:01:33 +00:00
|
|
|
}
|
2023-03-08 21:42:41 +00:00
|
|
|
|
|
|
|
|
public function testWatched() {
|
|
|
|
|
$userPage = Title::makeTitle( NS_USER, $this->blockee->getName() );
|
|
|
|
|
$this->doUnblock( [ 'watchuser' => true ] );
|
|
|
|
|
$this->assertTrue( $this->getServiceContainer()->getWatchlistManager()
|
|
|
|
|
->isWatched( $this->blocker, $userPage ) );
|
|
|
|
|
}
|
2013-10-23 16:01:33 +00:00
|
|
|
}
|