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 TestUser
::setPasswordForUser( $user, '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 $blockOptions = array(
42 'address' => 'UTBlockee',
43 'user' => $user->getID(),
44 'reason' => 'Parce que',
45 'expiry' => time() +
100500,
47 $this->block
= new Block( $blockOptions );
48 $this->madeAt
= wfTimestamp( TS_MW
);
50 $this->block
->insert();
51 // save up ID for use in assertion. Since ID is an autoincrement,
52 // its value might change depending on the order the tests are run.
53 // ApiBlockTest insert its own blocks!
54 $newBlockId = $this->block
->getId();
56 $this->blockId
= $newBlockId;
58 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
61 $this->addXffBlocks();
65 * debug function : dump the ipblocks table
67 function dumpBlocks() {
68 $v = $this->db
->select( 'ipblocks', '*' );
69 print "Got " . $v->numRows() . " rows. Full dump follow:\n";
70 foreach ( $v as $row ) {
76 * @covers Block::newFromTarget
78 public function testINewFromTargetReturnsCorrectBlock() {
80 $this->block
->equals( Block
::newFromTarget( 'UTBlockee' ) ),
81 "newFromTarget() returns the same block as the one that was made"
86 * @covers Block::newFromID
88 public function testINewFromIDReturnsCorrectBlock() {
90 $this->block
->equals( Block
::newFromID( $this->blockId
) ),
91 "newFromID() returns the same block as the one that was made"
98 public function testBug26425BlockTimestampDefaultsToTime() {
99 // delta to stop one-off errors when things happen to go over a second mark.
100 $delta = abs( $this->madeAt
- $this->block
->mTimestamp
);
101 $this->assertLessThan(
104 "If no timestamp is specified, the block is recorded as time()"
109 * CheckUser since being changed to use Block::newFromTarget started failing
110 * because the new function didn't accept empty strings like Block::load()
111 * had. Regression bug 29116.
113 * @dataProvider provideBug29116Data
114 * @covers Block::newFromTarget
116 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
117 $block = Block
::newFromTarget( 'UTBlockee', $vagueTarget );
119 $this->block
->equals( $block ),
120 "newFromTarget() returns the same block as the one that was made when "
121 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
125 public static function provideBug29116Data() {
134 * @covers Block::prevents
136 public function testBlockedUserCanNotCreateAccount() {
137 $username = 'BlockedUserToCreateAccountWith';
138 $u = User
::newFromName( $username );
141 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
146 Block
::newFromTarget( $username ),
147 "$username should not be blocked"
151 $u = User
::newFromName( $username );
153 $u->isBlockedFromCreateAccount(),
154 "Our sandbox user should be able to create account before being blocked"
157 // Foreign perspective (blockee not on current wiki)...
158 $blockOptions = array(
159 'address' => $username,
161 'reason' => 'crosswiki block...',
162 'timestamp' => wfTimestampNow(),
163 'expiry' => $this->db
->getInfinity(),
164 'createAccount' => true,
165 'enableAutoblock' => true,
167 'blockEmail' => true,
168 'byText' => 'MetaWikiUser',
170 $block = new Block( $blockOptions );
173 // Reload block from DB
174 $userBlock = Block
::newFromTarget( $username );
176 (bool)$block->prevents( 'createaccount' ),
177 "Block object in DB should prevents 'createaccount'"
180 $this->assertInstanceOf(
183 "'$username' block block object should be existent"
187 $u = User
::newFromName( $username );
189 (bool)$u->isBlockedFromCreateAccount(),
190 "Our sandbox user '$username' should NOT be able to create account"
195 * @covers Block::insert
197 public function testCrappyCrossWikiBlocks() {
198 // Delete the last round's block if it's still there
199 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
201 // An old block will prevent our new one from saving.
205 // Local perspective (blockee on current wiki)...
206 $user = User
::newFromName( 'UserOnForeignWiki' );
207 $user->addToDatabase();
208 // Set user ID to match the test value
209 $this->db
->update( 'user', array( 'user_id' => 14146 ), array( 'user_id' => $user->getId() ) );
211 // Foreign perspective (blockee not on current wiki)...
212 $blockOptions = array(
213 'address' => 'UserOnForeignWiki',
215 'reason' => 'crosswiki block...',
216 'timestamp' => wfTimestampNow(),
217 'expiry' => $this->db
->getInfinity(),
218 'createAccount' => true,
219 'enableAutoblock' => true,
221 'blockEmail' => true,
222 'byText' => 'MetaWikiUser',
224 $block = new Block( $blockOptions );
226 $res = $block->insert( $this->db
);
227 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
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 );
369 public function testDeprecatedConstructor() {
370 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
371 $username = 'UnthinkablySecretRandomUsername';
372 $reason = 'being irrational';
375 $u = User
::newFromName( $username );
376 if ( $u->getID() == 0 ) {
378 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
382 # Make sure the user isn't blocked
384 Block
::newFromTarget( $username ),
385 "$username should not be blocked"
390 /* address */ $username,
393 /* reason */ $reason,
402 $block->getTarget()->getName(),
404 "Target should be set properly"
407 # Check supplied parameter
411 "Reason should be non-default"
414 # Check default parameter
416 (bool)$block->prevents( 'createaccount' ),
417 "Account creation should not be blocked by default"