Update API tests to comply with the new backend for change tags
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiBlockTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiBlock
9 */
10 class ApiBlockTest extends ApiTestCase {
11 protected $mUser = null;
12
13 protected function setUp() {
14 parent::setUp();
15 $this->tablesUsed = array_merge(
16 $this->tablesUsed,
17 [ 'change_tag', 'change_tag_def', 'logging' ]
18 );
19
20 $this->mUser = $this->getMutableTestUser()->getUser();
21 }
22
23 protected function tearDown() {
24 $block = Block::newFromTarget( $this->mUser->getName() );
25 if ( !is_null( $block ) ) {
26 $block->delete();
27 }
28 parent::tearDown();
29 }
30
31 protected function getTokens() {
32 return $this->getTokenList( self::$users['sysop'] );
33 }
34
35 /**
36 * @param array $extraParams Extra API parameters to pass to doApiRequest
37 * @param User $blocker User to do the blocking, null to pick
38 * arbitrarily
39 */
40 private function doBlock( array $extraParams = [], User $blocker = null ) {
41 if ( $blocker === null ) {
42 $blocker = self::$users['sysop']->getUser();
43 }
44
45 $tokens = $this->getTokens();
46
47 $this->assertNotNull( $this->mUser, 'Sanity check' );
48 $this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
49
50 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
51
52 $params = [
53 'action' => 'block',
54 'user' => $this->mUser->getName(),
55 'reason' => 'Some reason',
56 'token' => $tokens['blocktoken'],
57 ];
58 if ( array_key_exists( 'userid', $extraParams ) ) {
59 // Make sure we don't have both user and userid
60 unset( $params['user'] );
61 }
62 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
63 false, $blocker );
64
65 $block = Block::newFromTarget( $this->mUser->getName() );
66
67 $this->assertTrue( !is_null( $block ), 'Block is valid' );
68
69 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
70 $this->assertSame( 'Some reason', $block->mReason );
71
72 return $ret;
73 }
74
75 /**
76 * Block by username
77 */
78 public function testNormalBlock() {
79 $this->doBlock();
80 }
81
82 /**
83 * Block by user ID
84 */
85 public function testBlockById() {
86 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
87 }
88
89 /**
90 * A blocked user can't block
91 */
92 public function testBlockByBlockedUser() {
93 $this->setExpectedException( ApiUsageException::class,
94 'You cannot block or unblock other users because you are yourself blocked.' );
95
96 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
97 $block = new Block( [
98 'address' => $blocked->getName(),
99 'by' => self::$users['sysop']->getUser()->getId(),
100 'reason' => 'Capriciousness',
101 'timestamp' => '19370101000000',
102 'expiry' => 'infinity',
103 ] );
104 $block->insert();
105
106 $this->doBlock( [], $blocked );
107 }
108
109 public function testBlockOfNonexistentUser() {
110 $this->setExpectedException( ApiUsageException::class,
111 'There is no user by the name "Nonexistent". Check your spelling.' );
112
113 $this->doBlock( [ 'user' => 'Nonexistent' ] );
114 }
115
116 public function testBlockOfNonexistentUserId() {
117 $id = 948206325;
118 $this->setExpectedException( ApiUsageException::class,
119 "There is no user with ID $id." );
120
121 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
122
123 $this->doBlock( [ 'userid' => $id ] );
124 }
125
126 public function testBlockWithTag() {
127 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_WRITE_BOTH );
128 ChangeTags::defineTag( 'custom tag' );
129
130 $this->doBlock( [ 'tags' => 'custom tag' ] );
131
132 $dbw = wfGetDB( DB_MASTER );
133 $this->assertSame( 1, (int)$dbw->selectField(
134 [ 'change_tag', 'logging' ],
135 'COUNT(*)',
136 [ 'log_type' => 'block', 'ct_tag' => 'custom tag' ],
137 __METHOD__,
138 [],
139 [ 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ] ]
140 ) );
141 }
142
143 public function testBlockWithTagNewBackend() {
144 $this->setMwGlobals( 'wgChangeTagsSchemaMigrationStage', MIGRATION_NEW );
145 ChangeTags::defineTag( 'custom tag' );
146
147 $this->doBlock( [ 'tags' => 'custom tag' ] );
148
149 $dbw = wfGetDB( DB_MASTER );
150 $this->assertSame( 1, (int)$dbw->selectField(
151 [ 'change_tag', 'logging', 'change_tag_def' ],
152 'COUNT(*)',
153 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
154 __METHOD__,
155 [],
156 [
157 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
158 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
159 ]
160 ) );
161 }
162
163 public function testBlockWithProhibitedTag() {
164 $this->setExpectedException( ApiUsageException::class,
165 'You do not have permission to apply change tags along with your changes.' );
166
167 ChangeTags::defineTag( 'custom tag' );
168
169 $this->setMwGlobals( 'wgRevokePermissions',
170 [ 'user' => [ 'applychangetags' => true ] ] );
171
172 $this->doBlock( [ 'tags' => 'custom tag' ] );
173 }
174
175 public function testBlockWithHide() {
176 global $wgGroupPermissions;
177 $newPermissions = $wgGroupPermissions['sysop'];
178 $newPermissions['hideuser'] = true;
179 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
180 [ 'sysop' => $newPermissions ] );
181
182 $res = $this->doBlock( [ 'hidename' => '' ] );
183
184 $dbw = wfGetDB( DB_MASTER );
185 $this->assertSame( '1', $dbw->selectField(
186 'ipblocks',
187 'ipb_deleted',
188 [ 'ipb_id' => $res[0]['block']['id'] ],
189 __METHOD__
190 ) );
191 }
192
193 public function testBlockWithProhibitedHide() {
194 $this->setExpectedException( ApiUsageException::class,
195 "You don't have permission to hide user names from the block log." );
196
197 $this->doBlock( [ 'hidename' => '' ] );
198 }
199
200 public function testBlockWithEmailBlock() {
201 $res = $this->doBlock( [ 'noemail' => '' ] );
202
203 $dbw = wfGetDB( DB_MASTER );
204 $this->assertSame( '1', $dbw->selectField(
205 'ipblocks',
206 'ipb_block_email',
207 [ 'ipb_id' => $res[0]['block']['id'] ],
208 __METHOD__
209 ) );
210 }
211
212 public function testBlockWithProhibitedEmailBlock() {
213 $this->setExpectedException( ApiUsageException::class,
214 "You don't have permission to block users from sending email through the wiki." );
215
216 $this->setMwGlobals( 'wgRevokePermissions',
217 [ 'sysop' => [ 'blockemail' => true ] ] );
218
219 $this->doBlock( [ 'noemail' => '' ] );
220 }
221
222 public function testBlockWithExpiry() {
223 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
224
225 $dbw = wfGetDB( DB_MASTER );
226 $expiry = $dbw->selectField(
227 'ipblocks',
228 'ipb_expiry',
229 [ 'ipb_id' => $res[0]['block']['id'] ],
230 __METHOD__
231 );
232
233 // Allow flakiness up to one second
234 $this->assertLessThanOrEqual( 1,
235 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
236 }
237
238 public function testBlockWithInvalidExpiry() {
239 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
240
241 $this->doBlock( [ 'expiry' => '' ] );
242 }
243
244 /**
245 * @expectedException ApiUsageException
246 * @expectedExceptionMessage The "token" parameter must be set
247 */
248 public function testBlockingActionWithNoToken() {
249 $this->doApiRequest(
250 [
251 'action' => 'block',
252 'user' => $this->mUser->getName(),
253 'reason' => 'Some reason',
254 ],
255 null,
256 false,
257 self::$users['sysop']->getUser()
258 );
259 }
260 }