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