Merge "jquery.suggestions: Do not show suggestions on prefilled values"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiUnblockTest.php
1 <?php
2
3 use MediaWiki\Block\DatabaseBlock;
4
5 /**
6 * @group API
7 * @group Database
8 * @group medium
9 *
10 * @covers ApiUnblock
11 */
12 class ApiUnblockTest extends ApiTestCase {
13 /** @var User */
14 private $blocker;
15
16 /** @var User */
17 private $blockee;
18
19 public function setUp() {
20 parent::setUp();
21
22 $this->tablesUsed = array_merge(
23 $this->tablesUsed,
24 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
25 );
26
27 $this->blocker = $this->getTestSysop()->getUser();
28 $this->blockee = $this->getMutableTestUser()->getUser();
29
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(),
34 ] );
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' );
39 }
40
41 private function getBlockFromParams( array $params ) {
42 if ( array_key_exists( 'user', $params ) ) {
43 return DatabaseBlock::newFromTarget( $params['user'] );
44 }
45 if ( array_key_exists( 'userid', $params ) ) {
46 return DatabaseBlock::newFromTarget( User::newFromId( $params['userid'] ) );
47 }
48 return DatabaseBlock::newFromId( $params['id'] );
49 }
50
51 /**
52 * Try to submit the unblock API request and check that the block no longer exists.
53 *
54 * @param array $params API request query parameters
55 */
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() ];
60 }
61
62 $originalBlock = $this->getBlockFromParams( $params );
63
64 $this->doApiRequestWithToken( $params );
65
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' );
71 }
72
73 /**
74 * @expectedException ApiUsageException
75 */
76 public function testWithNoToken() {
77 $this->doApiRequest( [
78 'action' => 'unblock',
79 'user' => $this->blockee->getName(),
80 'reason' => 'Some reason',
81 ] );
82 }
83
84 public function testNormalUnblock() {
85 $this->doUnblock();
86 }
87
88 public function testUnblockNoPermission() {
89 $this->setExpectedApiException( 'apierror-permissiondenied-unblock' );
90
91 $this->setGroupPermissions( 'sysop', 'block', false );
92 $this->overrideMwServices();
93
94 $this->doUnblock();
95 }
96
97 public function testUnblockWhenBlocked() {
98 $this->setExpectedApiException( 'ipbblocked' );
99
100 $block = new DatabaseBlock( [
101 'address' => $this->blocker->getName(),
102 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
103 ] );
104 $block->insert();
105
106 $this->doUnblock();
107 }
108
109 public function testUnblockSelfWhenBlocked() {
110 $block = new DatabaseBlock( [
111 'address' => $this->blocker->getName(),
112 'by' => $this->getTestUser( 'sysop' )->getUser()->getId(),
113 ] );
114 $result = $block->insert();
115 $this->assertNotFalse( $result, 'Could not insert block' );
116
117 $this->doUnblock( [ 'user' => $this->blocker->getName() ] );
118 }
119
120 public function testUnblockWithTagNewBackend() {
121 ChangeTags::defineTag( 'custom tag' );
122
123 $this->doUnblock( [ 'tags' => 'custom tag' ] );
124
125 $dbw = wfGetDB( DB_MASTER );
126 $this->assertSame( 1, (int)$dbw->selectField(
127 [ 'change_tag', 'logging', 'change_tag_def' ],
128 'COUNT(*)',
129 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
130 __METHOD__,
131 [],
132 [
133 'change_tag' => [ 'JOIN', 'ct_log_id = log_id' ],
134 'change_tag_def' => [ 'JOIN', 'ctd_id = ct_tag_id' ],
135 ]
136 ) );
137 }
138
139 public function testUnblockWithProhibitedTag() {
140 $this->setExpectedApiException( 'tags-apply-no-permission' );
141
142 ChangeTags::defineTag( 'custom tag' );
143
144 $this->setGroupPermissions( 'user', 'applychangetags', false );
145 $this->overrideMwServices();
146
147 $this->doUnblock( [ 'tags' => 'custom tag' ] );
148 }
149
150 public function testUnblockById() {
151 $this->doUnblock( [ 'userid' => $this->blockee->getId() ] );
152 }
153
154 public function testUnblockByInvalidId() {
155 $this->setExpectedApiException( [ 'apierror-nosuchuserid', 1234567890 ] );
156
157 $this->doUnblock( [ 'userid' => 1234567890 ] );
158 }
159
160 public function testUnblockNonexistentBlock() {
161 $this->setExpectedAPIException( [ 'ipb_cant_unblock', $this->blocker->getName() ] );
162
163 $this->doUnblock( [ 'user' => $this->blocker ] );
164 }
165 }