Warn if stateful ParserOutput transforms are used
[lhc/web/wiklou.git] / tests / phpunit / includes / BlockTest.php
1 <?php
2
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
8
9 /**
10 * @return User
11 */
12 private function getUserForBlocking() {
13 $testUser = $this->getMutableTestUser();
14 $user = $testUser->getUser();
15 $user->addToDatabase();
16 TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
17 $user->saveSettings();
18 return $user;
19 }
20
21 /**
22 * @param User $user
23 *
24 * @return Block
25 * @throws MWException
26 */
27 private function addBlockForUser( User $user ) {
28 // Delete the last round's block if it's still there
29 $oldBlock = Block::newFromTarget( $user->getName() );
30 if ( $oldBlock ) {
31 // An old block will prevent our new one from saving.
32 $oldBlock->delete();
33 }
34
35 $blockOptions = [
36 'address' => $user->getName(),
37 'user' => $user->getId(),
38 'reason' => 'Parce que',
39 'expiry' => time() + 100500,
40 ];
41 $block = new Block( $blockOptions );
42
43 $block->insert();
44 // save up ID for use in assertion. Since ID is an autoincrement,
45 // its value might change depending on the order the tests are run.
46 // ApiBlockTest insert its own blocks!
47 if ( !$block->getId() ) {
48 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
49 }
50
51 $this->addXffBlocks();
52
53 return $block;
54 }
55
56 /**
57 * @covers Block::newFromTarget
58 */
59 public function testINewFromTargetReturnsCorrectBlock() {
60 $user = $this->getUserForBlocking();
61 $block = $this->addBlockForUser( $user );
62
63 $this->assertTrue(
64 $block->equals( Block::newFromTarget( $user->getName() ) ),
65 "newFromTarget() returns the same block as the one that was made"
66 );
67 }
68
69 /**
70 * @covers Block::newFromID
71 */
72 public function testINewFromIDReturnsCorrectBlock() {
73 $user = $this->getUserForBlocking();
74 $block = $this->addBlockForUser( $user );
75
76 $this->assertTrue(
77 $block->equals( Block::newFromID( $block->getId() ) ),
78 "newFromID() returns the same block as the one that was made"
79 );
80 }
81
82 /**
83 * per T28425
84 */
85 public function testBug26425BlockTimestampDefaultsToTime() {
86 $user = $this->getUserForBlocking();
87 $block = $this->addBlockForUser( $user );
88 $madeAt = wfTimestamp( TS_MW );
89
90 // delta to stop one-off errors when things happen to go over a second mark.
91 $delta = abs( $madeAt - $block->mTimestamp );
92 $this->assertLessThan(
93 2,
94 $delta,
95 "If no timestamp is specified, the block is recorded as time()"
96 );
97 }
98
99 /**
100 * CheckUser since being changed to use Block::newFromTarget started failing
101 * because the new function didn't accept empty strings like Block::load()
102 * had. Regression T31116.
103 *
104 * @dataProvider provideBug29116Data
105 * @covers Block::newFromTarget
106 */
107 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
108 $user = $this->getUserForBlocking();
109 $initialBlock = $this->addBlockForUser( $user );
110 $block = Block::newFromTarget( $user->getName(), $vagueTarget );
111
112 $this->assertTrue(
113 $initialBlock->equals( $block ),
114 "newFromTarget() returns the same block as the one that was made when "
115 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
116 );
117 }
118
119 public static function provideBug29116Data() {
120 return [
121 [ null ],
122 [ '' ],
123 [ false ]
124 ];
125 }
126
127 /**
128 * @covers Block::prevents
129 */
130 public function testBlockedUserCanNotCreateAccount() {
131 $username = 'BlockedUserToCreateAccountWith';
132 $u = User::newFromName( $username );
133 $u->addToDatabase();
134 $userId = $u->getId();
135 $this->assertNotEquals( 0, $userId, 'sanity' );
136 TestUser::setPasswordForUser( $u, 'NotRandomPass' );
137 unset( $u );
138
139 // Sanity check
140 $this->assertNull(
141 Block::newFromTarget( $username ),
142 "$username should not be blocked"
143 );
144
145 // Reload user
146 $u = User::newFromName( $username );
147 $this->assertFalse(
148 $u->isBlockedFromCreateAccount(),
149 "Our sandbox user should be able to create account before being blocked"
150 );
151
152 // Foreign perspective (blockee not on current wiki)...
153 $blockOptions = [
154 'address' => $username,
155 'user' => $userId,
156 'reason' => 'crosswiki block...',
157 'timestamp' => wfTimestampNow(),
158 'expiry' => $this->db->getInfinity(),
159 'createAccount' => true,
160 'enableAutoblock' => true,
161 'hideName' => true,
162 'blockEmail' => true,
163 'byText' => 'm>MetaWikiUser',
164 ];
165 $block = new Block( $blockOptions );
166 $block->insert();
167
168 // Reload block from DB
169 $userBlock = Block::newFromTarget( $username );
170 $this->assertTrue(
171 (bool)$block->prevents( 'createaccount' ),
172 "Block object in DB should prevents 'createaccount'"
173 );
174
175 $this->assertInstanceOf(
176 'Block',
177 $userBlock,
178 "'$username' block block object should be existent"
179 );
180
181 // Reload user
182 $u = User::newFromName( $username );
183 $this->assertTrue(
184 (bool)$u->isBlockedFromCreateAccount(),
185 "Our sandbox user '$username' should NOT be able to create account"
186 );
187 }
188
189 /**
190 * @covers Block::insert
191 */
192 public function testCrappyCrossWikiBlocks() {
193 // Delete the last round's block if it's still there
194 $oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
195 if ( $oldBlock ) {
196 // An old block will prevent our new one from saving.
197 $oldBlock->delete();
198 }
199
200 // Local perspective (blockee on current wiki)...
201 $user = User::newFromName( 'UserOnForeignWiki' );
202 $user->addToDatabase();
203 $userId = $user->getId();
204 $this->assertNotEquals( 0, $userId, 'sanity' );
205
206 // Foreign perspective (blockee not on current wiki)...
207 $blockOptions = [
208 'address' => 'UserOnForeignWiki',
209 'user' => $user->getId(),
210 'reason' => 'crosswiki block...',
211 'timestamp' => wfTimestampNow(),
212 'expiry' => $this->db->getInfinity(),
213 'createAccount' => true,
214 'enableAutoblock' => true,
215 'hideName' => true,
216 'blockEmail' => true,
217 'byText' => 'Meta>MetaWikiUser',
218 ];
219 $block = new Block( $blockOptions );
220
221 $res = $block->insert( $this->db );
222 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
223
224 $user = null; // clear
225
226 $block = Block::newFromID( $res['id'] );
227 $this->assertEquals(
228 'UserOnForeignWiki',
229 $block->getTarget()->getName(),
230 'Correct blockee name'
231 );
232 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
233 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
234 'Correct blocker name' );
235 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
236 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
237 }
238
239 protected function addXffBlocks() {
240 static $inited = false;
241
242 if ( $inited ) {
243 return;
244 }
245
246 $inited = true;
247
248 $blockList = [
249 [ 'target' => '70.2.0.0/16',
250 'type' => Block::TYPE_RANGE,
251 'desc' => 'Range Hardblock',
252 'ACDisable' => false,
253 'isHardblock' => true,
254 'isAutoBlocking' => false,
255 ],
256 [ 'target' => '2001:4860:4001::/48',
257 'type' => Block::TYPE_RANGE,
258 'desc' => 'Range6 Hardblock',
259 'ACDisable' => false,
260 'isHardblock' => true,
261 'isAutoBlocking' => false,
262 ],
263 [ 'target' => '60.2.0.0/16',
264 'type' => Block::TYPE_RANGE,
265 'desc' => 'Range Softblock with AC Disabled',
266 'ACDisable' => true,
267 'isHardblock' => false,
268 'isAutoBlocking' => false,
269 ],
270 [ 'target' => '50.2.0.0/16',
271 'type' => Block::TYPE_RANGE,
272 'desc' => 'Range Softblock',
273 'ACDisable' => false,
274 'isHardblock' => false,
275 'isAutoBlocking' => false,
276 ],
277 [ 'target' => '50.1.1.1',
278 'type' => Block::TYPE_IP,
279 'desc' => 'Exact Softblock',
280 'ACDisable' => false,
281 'isHardblock' => false,
282 'isAutoBlocking' => false,
283 ],
284 ];
285
286 $blocker = $this->getTestUser()->getUser();
287 foreach ( $blockList as $insBlock ) {
288 $target = $insBlock['target'];
289
290 if ( $insBlock['type'] === Block::TYPE_IP ) {
291 $target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
292 } elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
293 $target = IP::sanitizeRange( $target );
294 }
295
296 $block = new Block();
297 $block->setTarget( $target );
298 $block->setBlocker( $blocker );
299 $block->mReason = $insBlock['desc'];
300 $block->mExpiry = 'infinity';
301 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
302 $block->isHardblock( $insBlock['isHardblock'] );
303 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
304 $block->insert();
305 }
306 }
307
308 public static function providerXff() {
309 return [
310 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
311 'count' => 2,
312 'result' => 'Range Hardblock'
313 ],
314 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
315 'count' => 2,
316 'result' => 'Range Softblock with AC Disabled'
317 ],
318 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
319 'count' => 2,
320 'result' => 'Exact Softblock'
321 ],
322 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
323 'count' => 3,
324 'result' => 'Exact Softblock'
325 ],
326 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
327 'count' => 2,
328 'result' => 'Range Hardblock'
329 ],
330 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
331 'count' => 2,
332 'result' => 'Range Hardblock'
333 ],
334 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
335 'count' => 2,
336 'result' => 'Range Softblock with AC Disabled'
337 ],
338 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
339 'count' => 2,
340 'result' => 'Exact Softblock'
341 ],
342 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
343 'count' => 1,
344 'result' => 'Range Softblock with AC Disabled'
345 ],
346 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
347 'count' => 2,
348 'result' => 'Range6 Hardblock'
349 ],
350 ];
351 }
352
353 /**
354 * @dataProvider providerXff
355 * @covers Block::getBlocksForIPList
356 * @covers Block::chooseBlock
357 */
358 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
359 $user = $this->getUserForBlocking();
360 $this->addBlockForUser( $user );
361
362 $list = array_map( 'trim', explode( ',', $xff ) );
363 $xffblocks = Block::getBlocksForIPList( $list, true );
364 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
365 $block = Block::chooseBlock( $xffblocks, $list );
366 $this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
367 }
368
369 public function testDeprecatedConstructor() {
370 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
371 $username = 'UnthinkablySecretRandomUsername';
372 $reason = 'being irrational';
373
374 # Set up the target
375 $u = User::newFromName( $username );
376 if ( $u->getId() == 0 ) {
377 $u->addToDatabase();
378 TestUser::setPasswordForUser( $u, 'TotallyObvious' );
379 }
380 unset( $u );
381
382 # Make sure the user isn't blocked
383 $this->assertNull(
384 Block::newFromTarget( $username ),
385 "$username should not be blocked"
386 );
387
388 # Perform the block
389 $block = new Block(
390 /* address */ $username,
391 /* user */ 0,
392 /* by */ 0,
393 /* reason */ $reason,
394 /* timestamp */ 0,
395 /* auto */ false,
396 /* expiry */ 0
397 );
398 $block->insert();
399
400 # Check target
401 $this->assertEquals(
402 $block->getTarget()->getName(),
403 $username,
404 "Target should be set properly"
405 );
406
407 # Check supplied parameter
408 $this->assertEquals(
409 $block->mReason,
410 $reason,
411 "Reason should be non-default"
412 );
413
414 # Check default parameter
415 $this->assertFalse(
416 (bool)$block->prevents( 'createaccount' ),
417 "Account creation should not be blocked by default"
418 );
419 }
420
421 public function testSystemBlocks() {
422 $user = $this->getUserForBlocking();
423 $this->addBlockForUser( $user );
424
425 $blockOptions = [
426 'address' => $user->getName(),
427 'reason' => 'test system block',
428 'timestamp' => wfTimestampNow(),
429 'expiry' => $this->db->getInfinity(),
430 'byText' => 'MediaWiki default',
431 'systemBlock' => 'test',
432 'enableAutoblock' => true,
433 ];
434 $block = new Block( $blockOptions );
435
436 $this->assertSame( 'test', $block->getSystemBlockType() );
437
438 try {
439 $block->insert();
440 $this->fail( 'Expected exception not thrown' );
441 } catch ( MWException $ex ) {
442 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
443 }
444
445 try {
446 $block->doAutoblock( '192.0.2.2' );
447 $this->fail( 'Expected exception not thrown' );
448 } catch ( MWException $ex ) {
449 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
450 }
451 }
452
453 }