3 use MediaWiki\Block\DatabaseBlock
;
12 class ApiUnblockTest
extends ApiTestCase
{
19 public function setUp() {
22 $this->tablesUsed
= array_merge(
24 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
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
->getId(),
35 $result = $block->insert();
36 $this->assertNotFalse( $result, 'Could not insert block' );
37 $blockFromDB = DatabaseBlock
::newFromID( $result['id'] );
38 $this->assertTrue( !is_null( $blockFromDB ), 'Could not retrieve block' );
41 private function getBlockFromParams( array $params ) {
42 if ( array_key_exists( 'user', $params ) ) {
43 return DatabaseBlock
::newFromTarget( $params['user'] );
45 if ( array_key_exists( 'userid', $params ) ) {
46 return DatabaseBlock
::newFromTarget( User
::newFromId( $params['userid'] ) );
48 return DatabaseBlock
::newFromId( $params['id'] );
52 * Try to submit the unblock API request and check that the block no longer exists.
54 * @param array $params API request query parameters
56 private function doUnblock( array $params = [] ) {
57 $params +
= [ 'action' => 'unblock' ];
58 if ( !array_key_exists( 'userid', $params ) && !array_key_exists( 'id', $params ) ) {
59 $params +
= [ 'user' => $this->blockee
->getName() ];
62 $originalBlock = $this->getBlockFromParams( $params );
64 $this->doApiRequestWithToken( $params );
66 // We only check later on whether the block existed to begin with, because maybe the caller
67 // expects doApiRequestWithToken to throw, in which case the block might not be expected to
68 // exist to begin with.
69 $this->assertInstanceOf( DatabaseBlock
::class, $originalBlock, 'Block should initially exist' );
70 $this->assertNull( $this->getBlockFromParams( $params ), 'Block should have been removed' );
74 * @expectedException ApiUsageException
76 public function testWithNoToken() {
77 $this->doApiRequest( [
78 'action' => 'unblock',
79 'user' => $this->blockee
->getName(),
80 'reason' => 'Some reason',
84 public function testNormalUnblock() {
88 public function testUnblockNoPermission() {
89 $this->setExpectedApiException( 'apierror-permissiondenied-unblock' );
91 $this->setGroupPermissions( 'sysop', 'block', false );
92 $this->overrideMwServices();
97 public function testUnblockWhenBlocked() {
98 $this->setExpectedApiException( 'ipbblocked' );
100 $block = new DatabaseBlock( [
101 'address' => $this->blocker
->getName(),
102 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
109 public function testUnblockSelfWhenBlocked() {
110 $block = new DatabaseBlock( [
111 'address' => $this->blocker
->getName(),
112 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
114 $result = $block->insert();
115 $this->assertNotFalse( $result, 'Could not insert block' );
117 $this->doUnblock( [ 'user' => $this->blocker
->getName() ] );
120 public function testUnblockWithTagNewBackend() {
121 ChangeTags
::defineTag( 'custom tag' );
123 $this->doUnblock( [ 'tags' => 'custom tag' ] );
125 $dbw = wfGetDB( DB_MASTER
);
126 $this->assertSame( 1, (int)$dbw->selectField(
127 [ 'change_tag', 'logging', 'change_tag_def' ],
129 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
133 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
134 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
139 public function testUnblockWithProhibitedTag() {
140 $this->setExpectedApiException( 'tags-apply-no-permission' );
142 ChangeTags
::defineTag( 'custom tag' );
144 $this->setGroupPermissions( 'user', 'applychangetags', false );
145 $this->overrideMwServices();
147 $this->doUnblock( [ 'tags' => 'custom tag' ] );
150 public function testUnblockById() {
151 $this->doUnblock( [ 'userid' => $this->blockee
->getId() ] );
154 public function testUnblockByInvalidId() {
155 $this->setExpectedApiException( [ 'apierror-nosuchuserid', 1234567890 ] );
157 $this->doUnblock( [ 'userid' => 1234567890 ] );
160 public function testUnblockNonexistentBlock() {
161 $this->setExpectedAPIException( [ 'ipb_cant_unblock', $this->blocker
->getName() ] );
163 $this->doUnblock( [ 'user' => $this->blocker
] );