3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Api\ApiUsageException
;
6 use MediaWiki\Block\DatabaseBlock
;
7 use MediaWiki\Block\DatabaseBlockStore
;
8 use MediaWiki\MainConfigNames
;
9 use MediaWiki\Title\Title
;
10 use MediaWiki\User\User
;
17 * @covers \MediaWiki\Api\ApiUnblock
19 class ApiUnblockTest
extends ApiTestCase
{
26 private DatabaseBlockStore
$blockStore;
28 protected function setUp(): void
{
31 $this->blocker
= $this->getTestSysop()->getUser();
32 $this->blockee
= $this->getMutableTestUser()->getUser();
33 $this->blockStore
= $this->getServiceContainer()->getDatabaseBlockStore();
35 $this->overrideConfigValue( MainConfigNames
::EnableMultiBlocks
, true );
37 // Initialize a blocked user (used by most tests, although not all)
41 private function insertBlock( $options = [] ) {
42 $options = array_merge( [
43 'address' => $this->blockee
->getName(),
44 'by' => $this->blocker
,
47 $block = new DatabaseBlock( $options );
48 $result = $this->blockStore
->insertBlock( $block, null );
50 $this->assertNotFalse( $result, 'Could not insert block' );
54 private function getBlocksFromParams( array $params ): array {
55 if ( array_key_exists( 'user', $params ) ) {
56 return $this->blockStore
->newListFromTarget( $params['user'] );
58 if ( array_key_exists( 'userid', $params ) ) {
59 return $this->blockStore
->newListFromTarget(
60 $this->getServiceContainer()->getUserFactory()->newFromId( $params['userid'] )
64 $block = $this->blockStore
->newFromID( $params['id'] );
65 return $block ?
[ $block ] : [];
69 * Try to submit the unblock API request and check that the block no longer exists.
71 * @param array $params API request query parameters
73 private function doUnblock( array $params = [] ) {
74 $params +
= [ 'action' => 'unblock' ];
75 if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) {
76 $params +
= [ 'user' => $this->blockee
->getName() ];
79 $originalBlocks = $this->getBlocksFromParams( $params );
81 $this->doApiRequestWithToken( $params );
83 // We only check later on whether the blocks existed to begin with, because maybe the caller
84 // expects doApiRequestWithToken to throw, in which case the block(s) might not be expected to
85 // exist to begin with.
86 $this->assertTrue( count( $originalBlocks ) > 0, 'Block(s) should initially exist' );
87 $this->assertTrue( !$this->getBlocksFromParams( $params ), 'Block(s)h should have been removed' );
90 public function testWithNoToken() {
91 $this->expectException( ApiUsageException
::class );
92 $this->doApiRequest( [
93 'action' => 'unblock',
94 'user' => $this->blockee
->getName(),
95 'reason' => 'Some reason',
99 public function testNormalUnblock() {
103 public function testUnblockNoPermission() {
104 $this->expectApiErrorCode( 'permissiondenied' );
106 $this->setGroupPermissions( 'sysop', 'block', false );
111 public function testUnblockWhenBlocked() {
112 $this->expectApiErrorCode( 'ipbblocked' );
114 $this->insertBlock( [
115 'address' => $this->blocker
->getName(),
116 'by' => $this->getTestUser( 'sysop' )->getUser(),
122 public function testUnblockSelfWhenBlocked() {
123 $result = $this->insertBlock( [
124 'address' => $this->blocker
->getName(),
125 'by' => $this->getTestUser( 'sysop' )->getUser(),
127 $this->assertNotFalse( $result, 'Could not insert block' );
129 $this->doUnblock( [ 'user' => $this->blocker
->getName() ] );
132 public function testUnblockSelfByIdWhenBlocked() {
133 $result = $this->insertBlock( [
134 'address' => $this->blocker
->getName(),
135 'by' => $this->getTestUser( 'sysop' )->getUser(),
137 $this->assertNotFalse( $result, 'Could not insert block' );
139 $this->doUnblock( [ 'id' => $result['id'] ] );
142 public function testUnblockWithTagNewBackend() {
143 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
145 $this->doUnblock( [ 'tags' => 'custom tag' ] );
147 $this->assertSame( 1, (int)$this->getDb()->newSelectQueryBuilder()
148 ->select( 'COUNT(*)' )
150 ->join( 'change_tag', null, 'ct_log_id = log_id' )
151 ->join( 'change_tag_def', null, 'ctd_id = ct_tag_id' )
152 ->where( [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ] )
153 ->caller( __METHOD__
)->fetchField() );
156 public function testUnblockWithProhibitedTag() {
157 $this->expectApiErrorCode( 'tags-apply-no-permission' );
159 $this->getServiceContainer()->getChangeTagsStore()->defineTag( 'custom tag' );
161 $this->setGroupPermissions( 'user', 'applychangetags', false );
163 $this->doUnblock( [ 'tags' => 'custom tag' ] );
166 public function testUnblockByUserId() {
167 $this->doUnblock( [ 'userid' => $this->blockee
->getId() ] );
170 public function testUnblockByInvalidUserId() {
171 $this->expectApiErrorCode( 'nosuchuserid' );
172 $this->doUnblock( [ 'userid' => 1234567890 ] );
175 public function testUnblockNonexistentBlock() {
176 $this->expectApiErrorCode( 'cantunblock' );
177 $this->doUnblock( [ 'user' => $this->blocker
] );
180 public function testNoSuchBlockId() {
181 $this->expectApiErrorCode( 'nosuchblockid' );
182 $this->doUnblock( [ 'id' => 12345 ] );
185 public function testUnblockByBlockId() {
186 $result = $this->insertBlock();
187 $this->doUnblock( [ 'id' => $result['id'] ] );
190 public function testWatched() {
191 $userPage = Title
::makeTitle( NS_USER
, $this->blockee
->getName() );
192 $this->doUnblock( [ 'watchuser' => true ] );
193 $this->assertTrue( $this->getServiceContainer()->getWatchlistManager()
194 ->isWatched( $this->blocker
, $userPage ) );