3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Api\ApiUsageException
;
6 use MediaWiki\Block\DatabaseBlock
;
7 use MediaWiki\Title\Title
;
8 use MediaWiki\User\User
;
15 * @covers \MediaWiki\Api\ApiUnblock
17 class ApiUnblockTest
extends ApiTestCase
{
24 protected function setUp(): void
{
27 $this->blocker
= $this->getTestSysop()->getUser();
28 $this->blockee
= $this->getMutableTestUser()->getUser();
30 // Initialize a blocked user (used by most tests, although not all)
31 $block = new DatabaseBlock( [
32 'address' => $this->blockee
->getName(),
33 'by' => $this->blocker
,
35 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
36 $result = $blockStore->insertBlock( $block );
37 $this->assertNotFalse( $result, 'Could not insert block' );
38 $blockFromDB = $blockStore->newFromID( $result['id'] );
39 $this->assertInstanceOf( DatabaseBlock
::class, $blockFromDB, 'Could not retrieve block' );
42 private function getBlockFromParams( array $params ) {
43 $blockStore = $this->getServiceContainer()->getDatabaseBlockStore();
44 if ( array_key_exists( 'user', $params ) ) {
45 return $blockStore->newFromTarget( $params['user'] );
47 if ( array_key_exists( 'userid', $params ) ) {
48 return $blockStore->newFromTarget(
49 $this->getServiceContainer()->getUserFactory()->newFromId( $params['userid'] )
52 return $blockStore->newFromID( $params['id'] );
56 * Try to submit the unblock API request and check that the block no longer exists.
58 * @param array $params API request query parameters
60 private function doUnblock( array $params = [] ) {
61 $params +
= [ 'action' => 'unblock' ];
62 if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) {
63 $params +
= [ 'user' => $this->blockee
->getName() ];
66 $originalBlock = $this->getBlockFromParams( $params );
68 $this->doApiRequestWithToken( $params );
70 // We only check later on whether the block existed to begin with, because maybe the caller
71 // expects doApiRequestWithToken to throw, in which case the block might not be expected to
72 // exist to begin with.
73 $this->assertInstanceOf( DatabaseBlock
::class, $originalBlock, 'Block should initially exist' );
74 $this->assertNull( $this->getBlockFromParams( $params ), 'Block should have been removed' );
77 public function testWithNoToken() {
78 $this->expectException( ApiUsageException
::class );
79 $this->doApiRequest( [
80 'action' => 'unblock',
81 'user' => $this->blockee
->getName(),
82 'reason' => 'Some reason',
86 public function testNormalUnblock() {
90 public function testUnblockNoPermission() {
91 $this->expectApiErrorCode( 'permissiondenied' );
93 $this->setGroupPermissions( 'sysop', 'block', false );
98 public function testUnblockWhenBlocked() {
99 $this->expectApiErrorCode( 'ipbblocked' );
101 $block = new DatabaseBlock( [
102 'address' => $this->blocker
->getName(),
103 'by' => $this->getTestUser( 'sysop' )->getUser(),
105 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
110 public function testUnblockSelfWhenBlocked() {
111 $block = new DatabaseBlock( [
112 'address' => $this->blocker
->getName(),
113 'by' => $this->getTestUser( 'sysop' )->getUser(),
115 $result = $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
116 $this->assertNotFalse( $result, 'Could not insert block' );
118 $this->doUnblock( [ 'user' => $this->blocker
->getName() ] );
121 public function testUnblockWithTagNewBackend() {
122 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
124 $this->doUnblock( [ 'tags' => 'custom tag' ] );
126 $this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
127 ->select( 'COUNT(*)' )
129 ->join( 'change_tag', null, 'ct_log_id = log_id' )
130 ->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
131 ->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
132 ->caller( __METHOD__
)->fetchField() );
135 public function testUnblockWithProhibitedTag() {
136 $this->expectApiErrorCode( 'tags-apply-no-permission' );
138 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
140 $this->setGroupPermissions( 'user', 'applychangetags', false );
142 $this->doUnblock( [ 'tags' => 'custom tag' ] );
145 public function testUnblockById() {
146 $this->doUnblock( [ 'userid' => $this->blockee
->getId() ] );
149 public function testUnblockByInvalidId() {
150 $this->expectApiErrorCode( 'nosuchuserid' );
152 $this->doUnblock( [ 'userid' => 1234567890 ] );
155 public function testUnblockNonexistentBlock() {
156 $this->expectApiErrorCode( 'cantunblock' );
158 $this->doUnblock( [ 'user' => $this->blocker
] );
161 public function testWatched() {
162 $userPage = Title
::makeTitle( NS_USER
, $this->blockee
->getName() );
163 $this->doUnblock( [ 'watchuser' => true ] );
164 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()
165 ->isWatched( $this->blocker
, $userPage ) );