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
->select( '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" );
80 function testBug26425BlockTimestampDefaultsToTime() {
81 // delta to stop one-off errors when things happen to go over a second mark.
82 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
83 $this->assertLessThan( 2, $delta, "If no timestamp is specified, the block is recorded as time()" );
87 * This is the method previously used to load block info in CheckUser etc
88 * passing an empty value (empty string, null, etc) as the ip parameter bypasses IP lookup checks.
90 * This stopped working with r84475 and friends: regression being fixed for bug 29116.
92 * @dataProvider provideBug29116Data
94 function testBug29116LoadWithEmptyIp( $vagueTarget ) {
95 $this->hideDeprecated( 'Block::load' );
97 $uid = User
::idFromName( 'UTBlockee' );
98 $this->assertTrue( ( $uid > 0 ), 'Must be able to look up the target user during tests' );
100 $block = new Block();
101 $ok = $block->load( $vagueTarget, $uid );
102 $this->assertTrue( $ok, "Block->load() with empty IP and user ID '$uid' should return a block" );
104 $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 ) );
108 * CheckUser since being changed to use Block::newFromTarget started failing
109 * because the new function didn't accept empty strings like Block::load()
110 * had. Regression bug 29116.
112 * @dataProvider provideBug29116Data
114 function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
115 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
116 $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 ) );
119 public static function provideBug29116Data() {
127 function testBlockedUserCanNotCreateAccount() {
128 $username = 'BlockedUserToCreateAccountWith';
129 $u = User
::newFromName( $username );
130 $u->setPassword( 'NotRandomPass' );
136 Block
::newFromTarget( $username ),
137 "$username should not be blocked"
141 $u = User
::newFromName( $username );
143 $u->isBlockedFromCreateAccount(),
144 "Our sandbox user should be able to create account before being blocked"
147 // Foreign perspective (blockee not on current wiki)...
149 /* $address */ $username,
152 /* $reason */ 'crosswiki block...',
153 /* $timestamp */ wfTimestampNow(),
155 /* $expiry */ $this->db
->getInfinity(),
156 /* anonOnly */ false,
157 /* $createAccount */ true,
158 /* $enableAutoblock */ true,
159 /* $hideName (ipb_deleted) */ true,
160 /* $blockEmail */ true,
161 /* $allowUsertalk */ false,
162 /* $byName */ 'MetaWikiUser'
166 // Reload block from DB
167 $userBlock = Block
::newFromTarget( $username );
169 (bool)$block->prevents( 'createaccount' ),
170 "Block object in DB should prevents 'createaccount'"
173 $this->assertInstanceOf(
176 "'$username' block block object should be existent"
180 $u = User
::newFromName( $username );
182 (bool)$u->isBlockedFromCreateAccount(),
183 "Our sandbox user '$username' should NOT be able to create account"
187 function testCrappyCrossWikiBlocks() {
188 // Delete the last round's block if it's still there
189 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
191 // An old block will prevent our new one from saving.
195 // Foreign perspective (blockee not on current wiki)...
197 /* $address */ 'UserOnForeignWiki',
200 /* $reason */ 'crosswiki block...',
201 /* $timestamp */ wfTimestampNow(),
203 /* $expiry */ $this->db
->getInfinity(),
204 /* anonOnly */ false,
205 /* $createAccount */ true,
206 /* $enableAutoblock */ true,
207 /* $hideName (ipb_deleted) */ true,
208 /* $blockEmail */ true,
209 /* $allowUsertalk */ false,
210 /* $byName */ 'MetaWikiUser'
213 $res = $block->insert( $this->db
);
214 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
216 // Local perspective (blockee on current wiki)...
217 $user = User
::newFromName( 'UserOnForeignWiki' );
218 $user->addToDatabase();
219 // Set user ID to match the test value
220 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
221 $user = null; // clear
223 $block = Block
::newFromID( $res['id'] );
224 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
225 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
226 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
227 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
228 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
231 protected function addXffBlocks() {
232 static $inited = false;
241 array( 'target' => '70.2.0.0/16',
242 'type' => Block
::TYPE_RANGE
,
243 'desc' => 'Range Hardblock',
244 'ACDisable' => false,
245 'isHardblock' => true,
246 'isAutoBlocking' => false,
248 array( 'target' => '2001:4860:4001::/48',
249 'type' => Block
::TYPE_RANGE
,
250 'desc' => 'Range6 Hardblock',
251 'ACDisable' => false,
252 'isHardblock' => true,
253 'isAutoBlocking' => false,
255 array( 'target' => '60.2.0.0/16',
256 'type' => Block
::TYPE_RANGE
,
257 'desc' => 'Range Softblock with AC Disabled',
259 'isHardblock' => false,
260 'isAutoBlocking' => false,
262 array( 'target' => '50.2.0.0/16',
263 'type' => Block
::TYPE_RANGE
,
264 'desc' => 'Range Softblock',
265 'ACDisable' => false,
266 'isHardblock' => false,
267 'isAutoBlocking' => false,
269 array( 'target' => '50.1.1.1',
270 'type' => Block
::TYPE_IP
,
271 'desc' => 'Exact Softblock',
272 'ACDisable' => false,
273 'isHardblock' => false,
274 'isAutoBlocking' => false,
278 foreach ( $blockList as $insBlock ) {
279 $target = $insBlock['target'];
281 if ( $insBlock['type'] === Block
::TYPE_IP
) {
282 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
283 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
284 $target = IP
::sanitizeRange( $target );
287 $block = new Block();
288 $block->setTarget( $target );
289 $block->setBlocker( 'testblocker@global' );
290 $block->mReason
= $insBlock['desc'];
291 $block->mExpiry
= 'infinity';
292 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
293 $block->isHardblock( $insBlock['isHardblock'] );
294 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
299 public static function providerXff() {
301 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
303 'result' => 'Range Hardblock'
305 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
307 'result' => 'Range Softblock with AC Disabled'
309 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
311 'result' => 'Exact Softblock'
313 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
315 'result' => 'Exact Softblock'
317 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
319 'result' => 'Range Hardblock'
321 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
323 'result' => 'Range Hardblock'
325 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
327 'result' => 'Range Softblock with AC Disabled'
329 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
331 'result' => 'Exact Softblock'
333 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
335 'result' => 'Range Softblock with AC Disabled'
337 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
339 'result' => 'Range6 Hardblock'
345 * @dataProvider providerXff
347 function testBlocksOnXff( $xff, $exCount, $exResult ) {
348 $list = array_map( 'trim', explode( ',', $xff ) );
349 $xffblocks = Block
::getBlocksForIPList( $list, true );
350 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
351 $block = Block
::chooseBlock( $xffblocks, $list );
352 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );