7 class BlockTest
extends MediaWikiLangTestCase
{
13 /* variable used to save up the blockID we insert in this test suite */
16 protected function setUp() {
18 $this->setMwGlobals( array(
19 'wgLanguageCode' => 'en',
20 'wgContLang' => Language
::factory( 'en' )
24 function addDBData() {
26 $user = User
::newFromName( 'UTBlockee' );
27 if ( $user->getID() == 0 ) {
28 $user->addToDatabase();
29 $user->setPassword( 'UTBlockeePassword' );
31 $user->saveSettings();
34 // Delete the last round's block if it's still there
35 $oldBlock = Block
::newFromTarget( 'UTBlockee' );
37 // An old block will prevent our new one from saving.
41 $this->block
= new Block( 'UTBlockee', $user->getID(), 0,
42 'Parce que', 0, false, time() +
100500
44 $this->madeAt
= wfTimestamp( TS_MW
);
46 $this->block
->insert();
47 // save up ID for use in assertion. Since ID is an autoincrement,
48 // its value might change depending on the order the tests are run.
49 // ApiBlockTest insert its own blocks!
50 $newBlockId = $this->block
->getId();
52 $this->blockId
= $newBlockId;
54 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
57 $this->addXffBlocks();
61 * debug function : dump the ipblocks table
63 function dumpBlocks() {
64 $v = $this->db
->select( 'ipblocks', '*' );
65 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
66 foreach ( $v as $row ) {
72 * @covers Block::newFromTarget
74 public function testINewFromTargetReturnsCorrectBlock() {
75 $this->assertTrue( $this->block
->equals( Block
::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
79 * @covers Block::newFromID
81 public function testINewFromIDReturnsCorrectBlock() {
82 $this->assertTrue( $this->block
->equals( Block
::newFromID( $this->blockId
) ), "newFromID() returns the same block as the one that was made" );
88 public function testBug26425BlockTimestampDefaultsToTime() {
89 // delta to stop one-off errors when things happen to go over a second mark.
90 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
91 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
95 * This is the method previously used to load block info in CheckUser etc
96 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
98 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
100 * @dataProvider provideBug29116Data
101 * @covers Block::load
103 public function testBug29116LoadWithEmptyIp( $vagueTarget ) {
104 $this->hideDeprecated( 'Block::load' );
106 $uid = User
::idFromName( 'UTBlockee' );
107 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' );
109 $block = new Block();
110 $ok = $block->load( $vagueTarget, $uid );
111 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
113 $this->assertTrue( $this->block
->equals( $block ), "Block->load() returns the same block as the one that was made when given empty ip param " . var_export( $vagueTarget, true ) );
117 * CheckUser since being changed to use Block::newFromTarget started failing
118 * because the new function didn't accept empty strings like Block::load()
119 * had. Regression bug 29116.
121 * @dataProvider provideBug29116Data
122 * @covers Block::newFromTarget
124 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
125 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
126 $this->assertTrue( $this->block
->equals( $block ), "newFromTarget() returns the same block as the one that was made when given empty vagueTarget param " . var_export( $vagueTarget, true ) );
129 public static function provideBug29116Data() {
138 * @covers Block::prevents
140 public function testBlockedUserCanNotCreateAccount() {
141 $username = 'BlockedUserToCreateAccountWith';
142 $u = User
::newFromName( $username );
143 $u->setPassword( 'NotRandomPass' );
149 Block
::newFromTarget( $username ),
150 "$username should not be blocked"
154 $u = User
::newFromName( $username );
156 $u->isBlockedFromCreateAccount(),
157 "Our sandbox user should be able to create account before being blocked"
160 // Foreign perspective (blockee not on current wiki)...
162 /* $address */ $username,
165 /* $reason */ 'crosswiki block...',
166 /* $timestamp */ wfTimestampNow(),
168 /* $expiry */ $this->db
->getInfinity(),
169 /* anonOnly */ false,
170 /* $createAccount */ true,
171 /* $enableAutoblock */ true,
172 /* $hideName (ipb_deleted) */ true,
173 /* $blockEmail */ true,
174 /* $allowUsertalk */ false,
175 /* $byName */ 'MetaWikiUser'
179 // Reload block from DB
180 $userBlock = Block
::newFromTarget( $username );
182 (bool)$block->prevents( 'createaccount' ),
183 "Block object in DB should prevents 'createaccount'"
186 $this->assertInstanceOf(
189 "'$username' block block object should be existent"
193 $u = User
::newFromName( $username );
195 (bool)$u->isBlockedFromCreateAccount(),
196 "Our sandbox user '$username' should NOT be able to create account"
201 * @covers Block::insert
203 public function testCrappyCrossWikiBlocks() {
204 // Delete the last round's block if it's still there
205 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
207 // An old block will prevent our new one from saving.
211 // Foreign perspective (blockee not on current wiki)...
213 /* $address */ 'UserOnForeignWiki',
216 /* $reason */ 'crosswiki block...',
217 /* $timestamp */ wfTimestampNow(),
219 /* $expiry */ $this->db
->getInfinity(),
220 /* anonOnly */ false,
221 /* $createAccount */ true,
222 /* $enableAutoblock */ true,
223 /* $hideName (ipb_deleted) */ true,
224 /* $blockEmail */ true,
225 /* $allowUsertalk */ false,
226 /* $byName */ 'MetaWikiUser'
229 $res = $block->insert( $this->db
);
230 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
232 // Local perspective (blockee on current wiki)...
233 $user = User
::newFromName( 'UserOnForeignWiki' );
234 $user->addToDatabase();
235 // Set user ID to match the test value
236 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
237 $user = null; // clear
239 $block = Block
::newFromID( $res['id'] );
240 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
241 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
242 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
243 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
244 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
247 protected function addXffBlocks() {
248 static $inited = false;
257 array( 'target' => '70.2.0.0/16',
258 'type' => Block
::TYPE_RANGE
,
259 'desc' => 'Range Hardblock',
260 'ACDisable' => false,
261 'isHardblock' => true,
262 'isAutoBlocking' => false,
264 array( 'target' => '2001:4860:4001::/48',
265 'type' => Block
::TYPE_RANGE
,
266 'desc' => 'Range6 Hardblock',
267 'ACDisable' => false,
268 'isHardblock' => true,
269 'isAutoBlocking' => false,
271 array( 'target' => '60.2.0.0/16',
272 'type' => Block
::TYPE_RANGE
,
273 'desc' => 'Range Softblock with AC Disabled',
275 'isHardblock' => false,
276 'isAutoBlocking' => false,
278 array( 'target' => '50.2.0.0/16',
279 'type' => Block
::TYPE_RANGE
,
280 'desc' => 'Range Softblock',
281 'ACDisable' => false,
282 'isHardblock' => false,
283 'isAutoBlocking' => false,
285 array( 'target' => '50.1.1.1',
286 'type' => Block
::TYPE_IP
,
287 'desc' => 'Exact Softblock',
288 'ACDisable' => false,
289 'isHardblock' => false,
290 'isAutoBlocking' => false,
294 foreach ( $blockList as $insBlock ) {
295 $target = $insBlock['target'];
297 if ( $insBlock['type'] === Block
::TYPE_IP
) {
298 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
299 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
300 $target = IP
::sanitizeRange( $target );
303 $block = new Block();
304 $block->setTarget( $target );
305 $block->setBlocker( 'testblocker@global' );
306 $block->mReason
= $insBlock['desc'];
307 $block->mExpiry
= 'infinity';
308 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
309 $block->isHardblock( $insBlock['isHardblock'] );
310 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
315 public static function providerXff() {
317 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
319 'result' => 'Range Hardblock'
321 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
323 'result' => 'Range Softblock with AC Disabled'
325 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
327 'result' => 'Exact Softblock'
329 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
331 'result' => 'Exact Softblock'
333 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
335 'result' => 'Range Hardblock'
337 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
339 'result' => 'Range Hardblock'
341 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
343 'result' => 'Range Softblock with AC Disabled'
345 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
347 'result' => 'Exact Softblock'
349 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
351 'result' => 'Range Softblock with AC Disabled'
353 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
355 'result' => 'Range6 Hardblock'
361 * @dataProvider providerXff
362 * @covers Block::getBlocksForIPList
363 * @covers Block::chooseBlock
365 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
366 $list = array_map( 'trim', explode( ',', $xff ) );
367 $xffblocks = Block
::getBlocksForIPList( $list, true );
368 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
369 $block = Block
::chooseBlock( $xffblocks, $list );
370 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );