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' );
141 Block
::newFromTarget( $username ),
142 "$username should not be blocked"
146 $u = User
::newFromName( $username );
148 $u->isBlockedFromCreateAccount(),
149 "Our sandbox user should be able to create account before being blocked"
152 // Foreign perspective (blockee not on current wiki)...
154 /* $address */ $username,
157 /* $reason */ 'crosswiki block...',
158 /* $timestamp */ wfTimestampNow(),
160 /* $expiry */ $this->db
->getInfinity(),
161 /* anonOnly */ false,
162 /* $createAccount */ true,
163 /* $enableAutoblock */ true,
164 /* $hideName (ipb_deleted) */ true,
165 /* $blockEmail */ true,
166 /* $allowUsertalk */ false,
167 /* $byName */ 'MetaWikiUser'
171 // Reload block from DB
172 $userBlock = Block
::newFromTarget( $username );
174 (bool)$block->prevents( 'createaccount' ),
175 "Block object in DB should prevents 'createaccount'"
178 $this->assertInstanceOf(
181 "'$username' block block object should be existent"
185 $u = User
::newFromName( $username );
187 (bool)$u->isBlockedFromCreateAccount(),
188 "Our sandbox user '$username' should NOT be able to create account"
193 * @covers Block::insert
195 public function testCrappyCrossWikiBlocks() {
196 // Delete the last round's block if it's still there
197 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
199 // An old block will prevent our new one from saving.
203 // Foreign perspective (blockee not on current wiki)...
205 /* $address */ 'UserOnForeignWiki',
208 /* $reason */ 'crosswiki block...',
209 /* $timestamp */ wfTimestampNow(),
211 /* $expiry */ $this->db
->getInfinity(),
212 /* anonOnly */ false,
213 /* $createAccount */ true,
214 /* $enableAutoblock */ true,
215 /* $hideName (ipb_deleted) */ true,
216 /* $blockEmail */ true,
217 /* $allowUsertalk */ false,
218 /* $byName */ 'MetaWikiUser'
221 $res = $block->insert( $this->db
);
222 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
224 // Local perspective (blockee on current wiki)...
225 $user = User
::newFromName( 'UserOnForeignWiki' );
226 $user->addToDatabase();
227 // Set user ID to match the test value
228 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
229 $user = null; // clear
231 $block = Block
::newFromID( $res['id'] );
234 $block->getTarget()->getName(),
235 'Correct blockee name'
237 $this->assertEquals( '14146', $block->getTarget()->getId(), 'Correct blockee id' );
238 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
239 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
240 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
243 protected function addXffBlocks() {
244 static $inited = false;
253 array( 'target' => '70.2.0.0/16',
254 'type' => Block
::TYPE_RANGE
,
255 'desc' => 'Range Hardblock',
256 'ACDisable' => false,
257 'isHardblock' => true,
258 'isAutoBlocking' => false,
260 array( 'target' => '2001:4860:4001::/48',
261 'type' => Block
::TYPE_RANGE
,
262 'desc' => 'Range6 Hardblock',
263 'ACDisable' => false,
264 'isHardblock' => true,
265 'isAutoBlocking' => false,
267 array( 'target' => '60.2.0.0/16',
268 'type' => Block
::TYPE_RANGE
,
269 'desc' => 'Range Softblock with AC Disabled',
271 'isHardblock' => false,
272 'isAutoBlocking' => false,
274 array( 'target' => '50.2.0.0/16',
275 'type' => Block
::TYPE_RANGE
,
276 'desc' => 'Range Softblock',
277 'ACDisable' => false,
278 'isHardblock' => false,
279 'isAutoBlocking' => false,
281 array( 'target' => '50.1.1.1',
282 'type' => Block
::TYPE_IP
,
283 'desc' => 'Exact Softblock',
284 'ACDisable' => false,
285 'isHardblock' => false,
286 'isAutoBlocking' => false,
290 foreach ( $blockList as $insBlock ) {
291 $target = $insBlock['target'];
293 if ( $insBlock['type'] === Block
::TYPE_IP
) {
294 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
295 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
296 $target = IP
::sanitizeRange( $target );
299 $block = new Block();
300 $block->setTarget( $target );
301 $block->setBlocker( 'testblocker@global' );
302 $block->mReason
= $insBlock['desc'];
303 $block->mExpiry
= 'infinity';
304 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
305 $block->isHardblock( $insBlock['isHardblock'] );
306 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
311 public static function providerXff() {
313 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
315 'result' => 'Range Hardblock'
317 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
319 'result' => 'Range Softblock with AC Disabled'
321 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
323 'result' => 'Exact Softblock'
325 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.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, 2.3.4.5',
331 'result' => 'Range Hardblock'
333 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
335 'result' => 'Range Hardblock'
337 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
339 'result' => 'Range Softblock with AC Disabled'
341 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
343 'result' => 'Exact Softblock'
345 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
347 'result' => 'Range Softblock with AC Disabled'
349 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
351 'result' => 'Range6 Hardblock'
357 * @dataProvider providerXff
358 * @covers Block::getBlocksForIPList
359 * @covers Block::chooseBlock
361 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
362 $list = array_map( 'trim', explode( ',', $xff ) );
363 $xffblocks = Block
::getBlocksForIPList( $list, true );
364 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
365 $block = Block
::chooseBlock( $xffblocks, $list );
366 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );