e229f0c106829e78e4d7c56966fe87c24e599d64
[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 [ 'ipblocks', 'change_tag', 'change_tag_def', 'logging' ]
18 );
19
20 $this->mUser = $this->getMutableTestUser()->getUser();
21 }
22
23 protected function getTokens() {
24 return $this->getTokenList( self::$users['sysop'] );
25 }
26
27 /**
28 * @param array $extraParams Extra API parameters to pass to doApiRequest
29 * @param User $blocker User to do the blocking, null to pick
30 * arbitrarily
31 */
32 private function doBlock( array $extraParams = [], User $blocker = null ) {
33 if ( $blocker === null ) {
34 $blocker = self::$users['sysop']->getUser();
35 }
36
37 $tokens = $this->getTokens();
38
39 $this->assertNotNull( $this->mUser, 'Sanity check' );
40 $this->assertNotSame( 0, $this->mUser->getId(), 'Sanity check' );
41
42 $this->assertArrayHasKey( 'blocktoken', $tokens, 'Sanity check' );
43
44 $params = [
45 'action' => 'block',
46 'user' => $this->mUser->getName(),
47 'reason' => 'Some reason',
48 'token' => $tokens['blocktoken'],
49 ];
50 if ( array_key_exists( 'userid', $extraParams ) ) {
51 // Make sure we don't have both user and userid
52 unset( $params['user'] );
53 }
54 $ret = $this->doApiRequest( array_merge( $params, $extraParams ), null,
55 false, $blocker );
56
57 $block = Block::newFromTarget( $this->mUser->getName() );
58
59 $this->assertTrue( !is_null( $block ), 'Block is valid' );
60
61 $this->assertSame( $this->mUser->getName(), (string)$block->getTarget() );
62 $this->assertSame( 'Some reason', $block->mReason );
63
64 return $ret;
65 }
66
67 /**
68 * Block by username
69 */
70 public function testNormalBlock() {
71 $this->doBlock();
72 }
73
74 /**
75 * Block by user ID
76 */
77 public function testBlockById() {
78 $this->doBlock( [ 'userid' => $this->mUser->getId() ] );
79 }
80
81 /**
82 * A blocked user can't block
83 */
84 public function testBlockByBlockedUser() {
85 $this->setExpectedException( ApiUsageException::class,
86 'You cannot block or unblock other users because you are yourself blocked.' );
87
88 $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser();
89 $block = new Block( [
90 'address' => $blocked->getName(),
91 'by' => self::$users['sysop']->getUser()->getId(),
92 'reason' => 'Capriciousness',
93 'timestamp' => '19370101000000',
94 'expiry' => 'infinity',
95 ] );
96 $block->insert();
97
98 $this->doBlock( [], $blocked );
99 }
100
101 public function testBlockOfNonexistentUser() {
102 $this->setExpectedException( ApiUsageException::class,
103 'There is no user by the name "Nonexistent". Check your spelling.' );
104
105 $this->doBlock( [ 'user' => 'Nonexistent' ] );
106 }
107
108 public function testBlockOfNonexistentUserId() {
109 $id = 948206325;
110 $this->setExpectedException( ApiUsageException::class,
111 "There is no user with ID $id." );
112
113 $this->assertFalse( User::whoIs( $id ), 'Sanity check' );
114
115 $this->doBlock( [ 'userid' => $id ] );
116 }
117
118 public function testBlockWithTag() {
119 ChangeTags::defineTag( 'custom tag' );
120
121 $this->doBlock( [ 'tags' => 'custom tag' ] );
122
123 $dbw = wfGetDB( DB_MASTER );
124 $this->assertSame( 1, (int)$dbw->selectField(
125 [ 'change_tag', 'logging', 'change_tag_def' ],
126 'COUNT(*)',
127 [ 'log_type' => 'block', 'ctd_name' => 'custom tag' ],
128 __METHOD__,
129 [],
130 [
131 'change_tag' => [ 'INNER JOIN', 'ct_log_id = log_id' ],
132 'change_tag_def' => [ 'INNER JOIN', 'ctd_id = ct_tag_id' ],
133 ]
134 ) );
135 }
136
137 public function testBlockWithProhibitedTag() {
138 $this->setExpectedException( ApiUsageException::class,
139 'You do not have permission to apply change tags along with your changes.' );
140
141 ChangeTags::defineTag( 'custom tag' );
142
143 $this->setMwGlobals( 'wgRevokePermissions',
144 [ 'user' => [ 'applychangetags' => true ] ] );
145
146 $this->doBlock( [ 'tags' => 'custom tag' ] );
147 }
148
149 public function testBlockWithHide() {
150 global $wgGroupPermissions;
151 $newPermissions = $wgGroupPermissions['sysop'];
152 $newPermissions['hideuser'] = true;
153 $this->mergeMwGlobalArrayValue( 'wgGroupPermissions',
154 [ 'sysop' => $newPermissions ] );
155
156 $res = $this->doBlock( [ 'hidename' => '' ] );
157
158 $dbw = wfGetDB( DB_MASTER );
159 $this->assertSame( '1', $dbw->selectField(
160 'ipblocks',
161 'ipb_deleted',
162 [ 'ipb_id' => $res[0]['block']['id'] ],
163 __METHOD__
164 ) );
165 }
166
167 public function testBlockWithProhibitedHide() {
168 $this->setExpectedException( ApiUsageException::class,
169 "You don't have permission to hide user names from the block log." );
170
171 $this->doBlock( [ 'hidename' => '' ] );
172 }
173
174 public function testBlockWithEmailBlock() {
175 $res = $this->doBlock( [ 'noemail' => '' ] );
176
177 $dbw = wfGetDB( DB_MASTER );
178 $this->assertSame( '1', $dbw->selectField(
179 'ipblocks',
180 'ipb_block_email',
181 [ 'ipb_id' => $res[0]['block']['id'] ],
182 __METHOD__
183 ) );
184 }
185
186 public function testBlockWithProhibitedEmailBlock() {
187 $this->setExpectedException( ApiUsageException::class,
188 "You don't have permission to block users from sending email through the wiki." );
189
190 $this->setMwGlobals( 'wgRevokePermissions',
191 [ 'sysop' => [ 'blockemail' => true ] ] );
192
193 $this->doBlock( [ 'noemail' => '' ] );
194 }
195
196 public function testBlockWithExpiry() {
197 $res = $this->doBlock( [ 'expiry' => '1 day' ] );
198
199 $dbw = wfGetDB( DB_MASTER );
200 $expiry = $dbw->selectField(
201 'ipblocks',
202 'ipb_expiry',
203 [ 'ipb_id' => $res[0]['block']['id'] ],
204 __METHOD__
205 );
206
207 // Allow flakiness up to one second
208 $this->assertLessThanOrEqual( 1,
209 abs( wfTimestamp( TS_UNIX, $expiry ) - ( time() + 86400 ) ) );
210 }
211
212 public function testBlockWithInvalidExpiry() {
213 $this->setExpectedException( ApiUsageException::class, "Expiry time invalid." );
214
215 $this->doBlock( [ 'expiry' => '' ] );
216 }
217
218 public function testBlockWithRestrictions() {
219 $this->setMwGlobals( [
220 'wgEnablePartialBlocks' => true,
221 ] );
222
223 $title = 'Foo';
224 $page = $this->getExistingTestPage( $title );
225
226 $this->doBlock( [
227 'partial' => true,
228 'pagerestrictions' => $title,
229 ] );
230
231 $block = Block::newFromTarget( $this->mUser->getName() );
232
233 $this->assertFalse( $block->isSitewide() );
234 $this->assertCount( 1, $block->getRestrictions() );
235 $this->assertEquals( $title, $block->getRestrictions()[0]->getTitle()->getText() );
236 }
237
238 /**
239 * @expectedException ApiUsageException
240 * @expectedExceptionMessage The "token" parameter must be set
241 */
242 public function testBlockingActionWithNoToken() {
243 $this->doApiRequest(
244 [
245 'action' => 'block',
246 'user' => $this->mUser->getName(),
247 'reason' => 'Some reason',
248 ],
249 null,
250 false,
251 self::$users['sysop']->getUser()
252 );
253 }
254
255 /**
256 * @expectedException ApiUsageException
257 * @expectedExceptionMessage Invalid value "127.0.0.1/64" for user parameter "user".
258 */
259 public function testBlockWithLargeRange() {
260 $tokens = $this->getTokens();
261
262 $this->doApiRequest(
263 [
264 'action' => 'block',
265 'user' => '127.0.0.1/64',
266 'reason' => 'Some reason',
267 'token' => $tokens['blocktoken'],
268 ],
269 null,
270 false,
271 self::$users['sysop']->getUser()
272 );
273 }
274
275 /**
276 * @expectedException ApiUsageException
277 * @expectedExceptionMessage Too many values supplied for parameter "pagerestrictions". The
278 * limit is 10.
279 */
280 public function testBlockingToManyRestrictions() {
281 $this->setMwGlobals( [
282 'wgEnablePartialBlocks' => true,
283 ] );
284
285 $tokens = $this->getTokens();
286
287 $this->doApiRequest(
288 [
289 'action' => 'block',
290 'user' => $this->mUser->getName(),
291 'reason' => 'Some reason',
292 'partial' => true,
293 'pagerestrictions' => 'One|Two|Three|Four|Five|Six|Seven|Eight|Nine|Ten|Eleven',
294 'token' => $tokens['blocktoken'],
295 ],
296 null,
297 false,
298 self::$users['sysop']->getUser()
299 );
300 }
301 }