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() {
76 $this->block
->equals( Block
::newFromTarget( 'UTBlockee' ) ),
77 "newFromTarget() returns the same block as the one that was made"
82 * @covers Block::newFromID
84 public function testINewFromIDReturnsCorrectBlock() {
86 $this->block
->equals( Block
::newFromID( $this->blockId
) ),
87 "newFromID() returns the same block as the one that was made"
94 public function testBug26425BlockTimestampDefaultsToTime() {
95 // delta to stop one-off errors when things happen to go over a second mark.
96 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
97 $this->assertLessThan(
100 "If no timestamp is specified, the block is recorded as time()"
105 * CheckUser since being changed to use Block::newFromTarget started failing
106 * because the new function didn't accept empty strings like Block::load()
107 * had. Regression bug 29116.
109 * @dataProvider provideBug29116Data
110 * @covers Block::newFromTarget
112 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
113 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
115 $this->block
->equals( $block ),
116 "newFromTarget() returns the same block as the one that was made when "
117 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
121 public static function provideBug29116Data() {
130 * @covers Block::prevents
132 public function testBlockedUserCanNotCreateAccount() {
133 $username = 'BlockedUserToCreateAccountWith';
134 $u = User
::newFromName( $username );
135 $u->setPassword( 'NotRandomPass' );
142 Block
::newFromTarget( $username ),
143 "$username should not be blocked"
147 $u = User
::newFromName( $username );
149 $u->isBlockedFromCreateAccount(),
150 "Our sandbox user should be able to create account before being blocked"
153 // Foreign perspective (blockee not on current wiki)...
155 /* $address */ $username,
158 /* $reason */ 'crosswiki block...',
159 /* $timestamp */ wfTimestampNow(),
161 /* $expiry */ $this->db
->getInfinity(),
162 /* anonOnly */ false,
163 /* $createAccount */ true,
164 /* $enableAutoblock */ true,
165 /* $hideName (ipb_deleted) */ true,
166 /* $blockEmail */ true,
167 /* $allowUsertalk */ false,
168 /* $byName */ 'MetaWikiUser'
172 // Reload block from DB
173 $userBlock = Block
::newFromTarget( $username );
175 (bool)$block->prevents( 'createaccount' ),
176 "Block object in DB should prevents 'createaccount'"
179 $this->assertInstanceOf(
182 "'$username' block block object should be existent"
186 $u = User
::newFromName( $username );
188 (bool)$u->isBlockedFromCreateAccount(),
189 "Our sandbox user '$username' should NOT be able to create account"
194 * @covers Block::insert
196 public function testCrappyCrossWikiBlocks() {
197 // Delete the last round's block if it's still there
198 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
200 // An old block will prevent our new one from saving.
204 // Local perspective (blockee on current wiki)...
205 $user = User
::newFromName( 'UserOnForeignWiki' );
206 $user->addToDatabase();
207 // Set user ID to match the test value
208 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
210 // Foreign perspective (blockee not on current wiki)...
212 /* $address */ 'UserOnForeignWiki',
215 /* $reason */ 'crosswiki block...',
216 /* $timestamp */ wfTimestampNow(),
218 /* $expiry */ $this->db
->getInfinity(),
219 /* anonOnly */ false,
220 /* $createAccount */ true,
221 /* $enableAutoblock */ true,
222 /* $hideName (ipb_deleted) */ true,
223 /* $blockEmail */ true,
224 /* $allowUsertalk */ false,
225 /* $byName */ 'MetaWikiUser'
228 $res = $block->insert( $this->db
);
229 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
231 $user = null; // clear
233 $block = Block
::newFromID( $res['id'] );
236 $block->getTarget()->getName(),
237 'Correct blockee name'
239 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
240 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
241 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
242 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
245 protected function addXffBlocks() {
246 static $inited = false;
255 array( 'target' => '70.2.0.0/16',
256 'type' => Block
::TYPE_RANGE
,
257 'desc' => 'Range Hardblock',
258 'ACDisable' => false,
259 'isHardblock' => true,
260 'isAutoBlocking' => false,
262 array( 'target' => '2001:4860:4001::/48',
263 'type' => Block
::TYPE_RANGE
,
264 'desc' => 'Range6 Hardblock',
265 'ACDisable' => false,
266 'isHardblock' => true,
267 'isAutoBlocking' => false,
269 array( 'target' => '60.2.0.0/16',
270 'type' => Block
::TYPE_RANGE
,
271 'desc' => 'Range Softblock with AC Disabled',
273 'isHardblock' => false,
274 'isAutoBlocking' => false,
276 array( 'target' => '50.2.0.0/16',
277 'type' => Block
::TYPE_RANGE
,
278 'desc' => 'Range Softblock',
279 'ACDisable' => false,
280 'isHardblock' => false,
281 'isAutoBlocking' => false,
283 array( 'target' => '50.1.1.1',
284 'type' => Block
::TYPE_IP
,
285 'desc' => 'Exact Softblock',
286 'ACDisable' => false,
287 'isHardblock' => false,
288 'isAutoBlocking' => false,
292 foreach ( $blockList as $insBlock ) {
293 $target = $insBlock['target'];
295 if ( $insBlock['type'] === Block
::TYPE_IP
) {
296 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
297 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
298 $target = IP
::sanitizeRange( $target );
301 $block = new Block();
302 $block->setTarget( $target );
303 $block->setBlocker( 'testblocker@global' );
304 $block->mReason
= $insBlock['desc'];
305 $block->mExpiry
= 'infinity';
306 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
307 $block->isHardblock( $insBlock['isHardblock'] );
308 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
313 public static function providerXff() {
315 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
317 'result' => 'Range Hardblock'
319 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
321 'result' => 'Range Softblock with AC Disabled'
323 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
325 'result' => 'Exact Softblock'
327 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
329 'result' => 'Exact Softblock'
331 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
333 'result' => 'Range Hardblock'
335 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
337 'result' => 'Range Hardblock'
339 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
341 'result' => 'Range Softblock with AC Disabled'
343 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
345 'result' => 'Exact Softblock'
347 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
349 'result' => 'Range Softblock with AC Disabled'
351 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
353 'result' => 'Range6 Hardblock'
359 * @dataProvider providerXff
360 * @covers Block::getBlocksForIPList
361 * @covers Block::chooseBlock
363 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
364 $list = array_map( 'trim', explode( ',', $xff ) );
365 $xffblocks = Block
::getBlocksForIPList( $list, true );
366 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
367 $block = Block
::chooseBlock( $xffblocks, $list );
368 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );