7 class BlockTest
extends MediaWikiLangTestCase
{
9 private $block, $madeAt;
11 /* variable used to save up the blockID we insert in this test suite */
14 protected function setUp() {
16 $this->setMwGlobals( array(
17 'wgLanguageCode' => 'en',
18 'wgContLang' => Language
::factory( 'en' )
22 function addDBData() {
24 $user = User
::newFromName( 'UTBlockee' );
25 if ( $user->getID() == 0 ) {
26 $user->addToDatabase();
27 $user->setPassword( 'UTBlockeePassword' );
29 $user->saveSettings();
32 // Delete the last round's block if it's still there
33 $oldBlock = Block
::newFromTarget( 'UTBlockee' );
35 // An old block will prevent our new one from saving.
39 $this->block
= new Block( 'UTBlockee', $user->getID(), 0,
40 'Parce que', 0, false, time() +
100500
42 $this->madeAt
= wfTimestamp( TS_MW
);
44 $this->block
->insert();
45 // save up ID for use in assertion. Since ID is an autoincrement,
46 // its value might change depending on the order the tests are run.
47 // ApiBlockTest insert its own blocks!
48 $newBlockId = $this->block
->getId();
50 $this->blockId
= $newBlockId;
52 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
55 $this->addXffBlocks();
59 * debug function : dump the ipblocks table
61 function dumpBlocks() {
62 $v = $this->db
->query( 'SELECT * FROM unittest_ipblocks' );
63 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
64 foreach ( $v as $row ) {
69 function testInitializerFunctionsReturnCorrectBlock() {
70 // $this->dumpBlocks();
72 $this->assertTrue( $this->block
->equals( Block
::newFromTarget( 'UTBlockee' ) ), "newFromTarget() returns the same block as the one that was made" );
74 $this->assertTrue( $this->block
->equals( Block
::newFromID( $this->blockId
) ), "newFromID() returns the same block as the one that was made" );
81 function testBug26425BlockTimestampDefaultsToTime() {
82 // delta to stop one-off errors when things happen to go over a second mark.
83 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
84 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
89 * This is the method previously used to load block info in CheckUser etc
90 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
92 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
94 * @dataProvider provideBug29116Data
96 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
97 $this->hideDeprecated( 'Block::load' );
99 $uid = User
::idFromName( 'UTBlockee' );
100 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' );
102 $block = new Block();
103 $ok = $block->load( $vagueTarget, $uid );
104 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
106 $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 ) );
110 * CheckUser since being changed to use Block::newFromTarget started failing
111 * because the new function didn't accept empty strings like Block::load()
112 * had. Regression bug 29116.
114 * @dataProvider provideBug29116Data
116 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
117 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
118 $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 ) );
121 public static function provideBug29116Data() {
129 function testBlockedUserCanNotCreateAccount() {
130 $username = 'BlockedUserToCreateAccountWith';
131 $u = User
::newFromName( $username );
132 $u->setPassword( 'NotRandomPass' );
139 Block
::newFromTarget( $username ),
140 "$username should not be blocked"
144 $u = User
::newFromName( $username );
146 $u->isBlockedFromCreateAccount(),
147 "Our sandbox user should be able to create account before being blocked"
150 // Foreign perspective (blockee not on current wiki)...
152 /* $address */ $username,
155 /* $reason */ 'crosswiki block...',
156 /* $timestamp */ wfTimestampNow(),
158 /* $expiry */ $this->db
->getInfinity(),
159 /* anonOnly */ false,
160 /* $createAccount */ true,
161 /* $enableAutoblock */ true,
162 /* $hideName (ipb_deleted) */ true,
163 /* $blockEmail */ true,
164 /* $allowUsertalk */ false,
165 /* $byName */ 'MetaWikiUser'
169 // Reload block from DB
170 $userBlock = Block
::newFromTarget( $username );
172 (bool)$block->prevents( 'createaccount' ),
173 "Block object in DB should prevents 'createaccount'"
176 $this->assertInstanceOf(
179 "'$username' block block object should be existent"
183 $u = User
::newFromName( $username );
185 (bool)$u->isBlockedFromCreateAccount(),
186 "Our sandbox user '$username' should NOT be able to create account"
190 function testCrappyCrossWikiBlocks() {
191 // Delete the last round's block if it's still there
192 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
194 // An old block will prevent our new one from saving.
198 // Foreign perspective (blockee not on current wiki)...
200 /* $address */ 'UserOnForeignWiki',
203 /* $reason */ 'crosswiki block...',
204 /* $timestamp */ wfTimestampNow(),
206 /* $expiry */ $this->db
->getInfinity(),
207 /* anonOnly */ false,
208 /* $createAccount */ true,
209 /* $enableAutoblock */ true,
210 /* $hideName (ipb_deleted) */ true,
211 /* $blockEmail */ true,
212 /* $allowUsertalk */ false,
213 /* $byName */ 'MetaWikiUser'
216 $res = $block->insert( $this->db
);
217 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
219 // Local perspective (blockee on current wiki)...
220 $user = User
::newFromName( 'UserOnForeignWiki' );
221 $user->addToDatabase();
222 // Set user ID to match the test value
223 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
224 $user = null; // clear
226 $block = Block
::newFromID( $res['id'] );
227 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
228 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
229 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
230 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
231 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
234 protected function addXffBlocks() {
235 static $inited = false;
244 array( 'target' => '70.2.0.0/16',
245 'type' => Block
::TYPE_RANGE
,
246 'desc' => 'Range Hardblock',
247 'ACDisable' => false,
248 'isHardblock' => true,
249 'isAutoBlocking' => false,
251 array( 'target' => '2001:4860:4001::/48',
252 'type' => Block
::TYPE_RANGE
,
253 'desc' => 'Range6 Hardblock',
254 'ACDisable' => false,
255 'isHardblock' => true,
256 'isAutoBlocking' => false,
258 array( 'target' => '60.2.0.0/16',
259 'type' => Block
::TYPE_RANGE
,
260 'desc' => 'Range Softblock with AC Disabled',
262 'isHardblock' => false,
263 'isAutoBlocking' => false,
265 array( 'target' => '50.2.0.0/16',
266 'type' => Block
::TYPE_RANGE
,
267 'desc' => 'Range Softblock',
268 'ACDisable' => false,
269 'isHardblock' => false,
270 'isAutoBlocking' => false,
272 array( 'target' => '50.1.1.1',
273 'type' => Block
::TYPE_IP
,
274 'desc' => 'Exact Softblock',
275 'ACDisable' => false,
276 'isHardblock' => false,
277 'isAutoBlocking' => false,
281 foreach ( $blockList as $insBlock ) {
282 $target = $insBlock['target'];
284 if ( $insBlock['type'] === Block
::TYPE_IP
) {
285 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
286 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
287 $target = IP
::sanitizeRange( $target );
290 $block = new Block();
291 $block->setTarget( $target );
292 $block->setBlocker( 'testblocker@global' );
293 $block->mReason
= $insBlock['desc'];
294 $block->mExpiry
= 'infinity';
295 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
296 $block->isHardblock( $insBlock['isHardblock'] );
297 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
302 public static function providerXff() {
304 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
306 'result' => 'Range Hardblock'
308 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
310 'result' => 'Range Softblock with AC Disabled'
312 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
314 'result' => 'Exact Softblock'
316 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
318 'result' => 'Exact Softblock'
320 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
322 'result' => 'Range Hardblock'
324 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
326 'result' => 'Range Hardblock'
328 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
330 'result' => 'Range Softblock with AC Disabled'
332 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
334 'result' => 'Exact Softblock'
336 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
338 'result' => 'Range Softblock with AC Disabled'
340 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
342 'result' => 'Range6 Hardblock'
348 * @dataProvider providerXff
350 function testBlocksOnXff( $xff, $exCount, $exResult ) {
351 $list = array_map( 'trim', explode( ',', $xff ) );
352 $xffblocks = Block
::getBlocksForIPList( $list, true );
353 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
354 $block = Block
::chooseBlock( $xffblocks, $list );
355 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );