Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / tests / phpunit / includes / BlockTest.php
blob2a2b6035653127bc9326b9ddccc185e86816ef2b
1 <?php
3 /**
4 * @group Database
5 * @group Blocking
6 */
7 class BlockTest extends MediaWikiLangTestCase {
9 /** @var Block */
10 private $block;
11 private $madeAt;
13 /* variable used to save up the blockID we insert in this test suite */
14 private $blockId;
16 protected function setUp() {
17 parent::setUp();
18 $this->setMwGlobals( array(
19 'wgLanguageCode' => 'en',
20 'wgContLang' => Language::factory( 'en' )
21 ) );
24 function addDBData() {
26 $user = User::newFromName( 'UTBlockee' );
27 if ( $user->getID() == 0 ) {
28 $user->addToDatabase();
29 TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
31 $user->saveSettings();
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();
41 $blockOptions = array(
42 'address' => 'UTBlockee',
43 'user' => $user->getID(),
44 'reason' => 'Parce que',
45 'expiry' => time() + 100500,
47 $this->block = new Block( $blockOptions );
48 $this->madeAt = wfTimestamp( TS_MW );
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?" );
61 $this->addXffBlocks();
64 /**
65 * debug function : dump the ipblocks table
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 );
75 /**
76 * @covers Block::newFromTarget
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"
85 /**
86 * @covers Block::newFromID
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"
95 /**
96 * per bug 26425
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(
103 $delta,
104 "If no timestamp is specified, the block is recorded as time()"
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.
113 * @dataProvider provideBug29116Data
114 * @covers Block::newFromTarget
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 )
125 public static function provideBug29116Data() {
126 return array(
127 array( null ),
128 array( '' ),
129 array( false )
134 * @covers Block::prevents
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 );
145 // Sanity check
146 $this->assertNull(
147 Block::newFromTarget( $username ),
148 "$username should not be blocked"
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"
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',
171 $block = new Block( $blockOptions );
172 $block->insert();
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'"
181 $this->assertInstanceOf(
182 'Block',
183 $userBlock,
184 "'$username' block block object should be existent"
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"
196 * @covers Block::insert
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();
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' );
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',
225 $block = new Block( $blockOptions );
227 $res = $block->insert( $this->db );
228 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
230 $user = null; // clear
232 $block = Block::newFromID( $res['id'] );
233 $this->assertEquals(
234 'UserOnForeignWiki',
235 $block->getTarget()->getName(),
236 'Correct blockee name'
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' );
244 protected function addXffBlocks() {
245 static $inited = false;
247 if ( $inited ) {
248 return;
251 $inited = true;
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,
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,
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,
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,
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,
291 foreach ( $blockList as $insBlock ) {
292 $target = $insBlock['target'];
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 );
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();
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'
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'
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'
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'
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'
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'
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'
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'
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'
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'
358 * @dataProvider providerXff
359 * @covers Block::getBlocksForIPList
360 * @covers Block::chooseBlock
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 );
370 public function testDeprecatedConstructor() {
371 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
372 $username = 'UnthinkablySecretRandomUsername';
373 $reason = 'being irrational';
375 # Set up the target
376 $u = User::newFromName( $username );
377 if ( $u->getID() == 0 ) {
378 $u->addToDatabase();
379 TestUser::setPasswordForUser( $u, 'TotallyObvious' );
381 unset( $u );
383 # Make sure the user isn't blocked
384 $this->assertNull(
385 Block::newFromTarget( $username ),
386 "$username should not be blocked"
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
399 $block->insert();
401 # Check target
402 $this->assertEquals(
403 $block->getTarget()->getName(),
404 $username,
405 "Target should be set properly"
408 # Check supplied parameter
409 $this->assertEquals(
410 $block->mReason,
411 $reason,
412 "Reason should be non-default"
415 # Check default parameter
416 $this->assertFalse(
417 (bool)$block->prevents( 'createaccount' ),
418 "Account creation should not be blocked by default"