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 * CheckUser since being changed to use Block::newFromTarget started failing
96 * because the new function didn't accept empty strings like Block::load()
97 * had. Regression bug 29116.
99 * @dataProvider provideBug29116Data
100 * @covers Block::newFromTarget
102 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
103 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
104 $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 ) );
107 public static function provideBug29116Data() {
116 * @covers Block::prevents
118 public function testBlockedUserCanNotCreateAccount() {
119 $username = 'BlockedUserToCreateAccountWith';
120 $u = User
::newFromName( $username );
121 $u->setPassword( 'NotRandomPass' );
127 Block
::newFromTarget( $username ),
128 "$username should not be blocked"
132 $u = User
::newFromName( $username );
134 $u->isBlockedFromCreateAccount(),
135 "Our sandbox user should be able to create account before being blocked"
138 // Foreign perspective (blockee not on current wiki)...
140 /* $address */ $username,
143 /* $reason */ 'crosswiki block...',
144 /* $timestamp */ wfTimestampNow(),
146 /* $expiry */ $this->db
->getInfinity(),
147 /* anonOnly */ false,
148 /* $createAccount */ true,
149 /* $enableAutoblock */ true,
150 /* $hideName (ipb_deleted) */ true,
151 /* $blockEmail */ true,
152 /* $allowUsertalk */ false,
153 /* $byName */ 'MetaWikiUser'
157 // Reload block from DB
158 $userBlock = Block
::newFromTarget( $username );
160 (bool)$block->prevents( 'createaccount' ),
161 "Block object in DB should prevents 'createaccount'"
164 $this->assertInstanceOf(
167 "'$username' block block object should be existent"
171 $u = User
::newFromName( $username );
173 (bool)$u->isBlockedFromCreateAccount(),
174 "Our sandbox user '$username' should NOT be able to create account"
179 * @covers Block::insert
181 public function testCrappyCrossWikiBlocks() {
182 // Delete the last round's block if it's still there
183 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
185 // An old block will prevent our new one from saving.
189 // Foreign perspective (blockee not on current wiki)...
191 /* $address */ 'UserOnForeignWiki',
194 /* $reason */ 'crosswiki block...',
195 /* $timestamp */ wfTimestampNow(),
197 /* $expiry */ $this->db
->getInfinity(),
198 /* anonOnly */ false,
199 /* $createAccount */ true,
200 /* $enableAutoblock */ true,
201 /* $hideName (ipb_deleted) */ true,
202 /* $blockEmail */ true,
203 /* $allowUsertalk */ false,
204 /* $byName */ 'MetaWikiUser'
207 $res = $block->insert( $this->db
);
208 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
210 // Local perspective (blockee on current wiki)...
211 $user = User
::newFromName( 'UserOnForeignWiki' );
212 $user->addToDatabase();
213 // Set user ID to match the test value
214 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
215 $user = null; // clear
217 $block = Block
::newFromID( $res['id'] );
218 $this->assertEquals( 'UserOnForeignWiki', $block->getTarget()->getName(), 'Correct blockee name' );
219 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
220 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
221 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
222 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
225 protected function addXffBlocks() {
226 static $inited = false;
235 array( 'target' => '70.2.0.0/16',
236 'type' => Block
::TYPE_RANGE
,
237 'desc' => 'Range Hardblock',
238 'ACDisable' => false,
239 'isHardblock' => true,
240 'isAutoBlocking' => false,
242 array( 'target' => '2001:4860:4001::/48',
243 'type' => Block
::TYPE_RANGE
,
244 'desc' => 'Range6 Hardblock',
245 'ACDisable' => false,
246 'isHardblock' => true,
247 'isAutoBlocking' => false,
249 array( 'target' => '60.2.0.0/16',
250 'type' => Block
::TYPE_RANGE
,
251 'desc' => 'Range Softblock with AC Disabled',
253 'isHardblock' => false,
254 'isAutoBlocking' => false,
256 array( 'target' => '50.2.0.0/16',
257 'type' => Block
::TYPE_RANGE
,
258 'desc' => 'Range Softblock',
259 'ACDisable' => false,
260 'isHardblock' => false,
261 'isAutoBlocking' => false,
263 array( 'target' => '50.1.1.1',
264 'type' => Block
::TYPE_IP
,
265 'desc' => 'Exact Softblock',
266 'ACDisable' => false,
267 'isHardblock' => false,
268 'isAutoBlocking' => false,
272 foreach ( $blockList as $insBlock ) {
273 $target = $insBlock['target'];
275 if ( $insBlock['type'] === Block
::TYPE_IP
) {
276 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
277 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
278 $target = IP
::sanitizeRange( $target );
281 $block = new Block();
282 $block->setTarget( $target );
283 $block->setBlocker( 'testblocker@global' );
284 $block->mReason
= $insBlock['desc'];
285 $block->mExpiry
= 'infinity';
286 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
287 $block->isHardblock( $insBlock['isHardblock'] );
288 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
293 public static function providerXff() {
295 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
297 'result' => 'Range Hardblock'
299 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
301 'result' => 'Range Softblock with AC Disabled'
303 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
305 'result' => 'Exact Softblock'
307 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
309 'result' => 'Exact Softblock'
311 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
313 'result' => 'Range Hardblock'
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' => '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, 50.1.1.1, 60.2.1.1, 2.3.4.5',
325 'result' => 'Exact Softblock'
327 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
329 'result' => 'Range Softblock with AC Disabled'
331 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
333 'result' => 'Range6 Hardblock'
339 * @dataProvider providerXff
340 * @covers Block::getBlocksForIPList
341 * @covers Block::chooseBlock
343 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
344 $list = array_map( 'trim', explode( ',', $xff ) );
345 $xffblocks = Block
::getBlocksForIPList( $list, true );
346 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
347 $block = Block
::chooseBlock( $xffblocks, $list );
348 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );