Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / ApiUnblockTest.php
blobff475754660d1c48497da1ebc9ffbd2bff4523ae
1 <?php
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;
10 /**
11 * @group API
12 * @group Database
13 * @group medium
15 * @covers \MediaWiki\Api\ApiUnblock
17 class ApiUnblockTest extends ApiTestCase {
18 /** @var User */
19 private $blocker;
21 /** @var User */
22 private $blockee;
24 protected function setUp(): void {
25 parent::setUp();
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,
34 ] );
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'] );
55 /**
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',
83 ] );
86 public function testNormalUnblock() {
87 $this->doUnblock();
90 public function testUnblockNoPermission() {
91 $this->expectApiErrorCode( 'permissiondenied' );
93 $this->setGroupPermissions( 'sysop', 'block', false );
95 $this->doUnblock();
98 public function testUnblockWhenBlocked() {
99 $this->expectApiErrorCode( 'ipbblocked' );
101 $block = new DatabaseBlock( [
102 'address' => $this->blocker->getName(),
103 'by' => $this->getTestUser( 'sysop' )->getUser(),
104 ] );
105 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
107 $this->doUnblock();
110 public function testUnblockSelfWhenBlocked() {
111 $block = new DatabaseBlock( [
112 'address' => $this->blocker->getName(),
113 'by' => $this->getTestUser( 'sysop' )->getUser(),
114 ] );
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(*)' )
128 ->from( 'logging' )
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 ) );