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 );
140 $userId = $u->getId();
141 $this->assertNotEquals( 0, $userId, 'sanity' );
142 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
147 Block
::newFromTarget( $username ),
148 "$username should not be blocked"
152 $u = User
::newFromName( $username );
154 $u->isBlockedFromCreateAccount(),
155 "Our sandbox user should be able to create account before being blocked"
158 // Foreign perspective (blockee not on current wiki)...
159 $blockOptions = array(
160 'address' => $username,
162 'reason' => 'crosswiki block...',
163 'timestamp' => wfTimestampNow(),
164 'expiry' => $this->db
->getInfinity(),
165 'createAccount' => true,
166 'enableAutoblock' => true,
168 'blockEmail' => true,
169 'byText' => 'MetaWikiUser',
171 $block = new Block( $blockOptions );
174 // Reload block from DB
175 $userBlock = Block
::newFromTarget( $username );
177 (bool)$block->prevents( 'createaccount' ),
178 "Block object in DB should prevents 'createaccount'"
181 $this->assertInstanceOf(
184 "'$username' block block object should be existent"
188 $u = User
::newFromName( $username );
190 (bool)$u->isBlockedFromCreateAccount(),
191 "Our sandbox user '$username' should NOT be able to create account"
196 * @covers Block::insert
198 public function testCrappyCrossWikiBlocks() {
199 // Delete the last round's block if it's still there
200 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
202 // An old block will prevent our new one from saving.
206 // Local perspective (blockee on current wiki)...
207 $user = User
::newFromName( 'UserOnForeignWiki' );
208 $user->addToDatabase();
209 $userId = $user->getId();
210 $this->assertNotEquals( 0, $userId, 'sanity' );
212 // Foreign perspective (blockee not on current wiki)...
213 $blockOptions = array(
214 'address' => 'UserOnForeignWiki',
215 'user' => $user->getId(),
216 'reason' => 'crosswiki block...',
217 'timestamp' => wfTimestampNow(),
218 'expiry' => $this->db
->getInfinity(),
219 'createAccount' => true,
220 'enableAutoblock' => true,
222 'blockEmail' => true,
223 'byText' => 'MetaWikiUser',
225 $block = new Block( $blockOptions );
227 $res = $block->insert( $this->db
);
228 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
230 $user = null; // clear
232 $block = Block
::newFromID( $res['id'] );
235 $block->getTarget()->getName(),
236 'Correct blockee name'
238 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
239 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
240 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
241 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
244 protected function addXffBlocks() {
245 static $inited = false;
254 array( 'target' => '70.2.0.0/16',
255 'type' => Block
::TYPE_RANGE
,
256 'desc' => 'Range Hardblock',
257 'ACDisable' => false,
258 'isHardblock' => true,
259 'isAutoBlocking' => false,
261 array( 'target' => '2001:4860:4001::/48',
262 'type' => Block
::TYPE_RANGE
,
263 'desc' => 'Range6 Hardblock',
264 'ACDisable' => false,
265 'isHardblock' => true,
266 'isAutoBlocking' => false,
268 array( 'target' => '60.2.0.0/16',
269 'type' => Block
::TYPE_RANGE
,
270 'desc' => 'Range Softblock with AC Disabled',
272 'isHardblock' => false,
273 'isAutoBlocking' => false,
275 array( 'target' => '50.2.0.0/16',
276 'type' => Block
::TYPE_RANGE
,
277 'desc' => 'Range Softblock',
278 'ACDisable' => false,
279 'isHardblock' => false,
280 'isAutoBlocking' => false,
282 array( 'target' => '50.1.1.1',
283 'type' => Block
::TYPE_IP
,
284 'desc' => 'Exact Softblock',
285 'ACDisable' => false,
286 'isHardblock' => false,
287 'isAutoBlocking' => false,
291 foreach ( $blockList as $insBlock ) {
292 $target = $insBlock['target'];
294 if ( $insBlock['type'] === Block
::TYPE_IP
) {
295 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
296 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
297 $target = IP
::sanitizeRange( $target );
300 $block = new Block();
301 $block->setTarget( $target );
302 $block->setBlocker( 'testblocker@global' );
303 $block->mReason
= $insBlock['desc'];
304 $block->mExpiry
= 'infinity';
305 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
306 $block->isHardblock( $insBlock['isHardblock'] );
307 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
312 public static function providerXff() {
314 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
316 'result' => 'Range Hardblock'
318 array( 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
320 'result' => 'Range Softblock with AC Disabled'
322 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
324 'result' => 'Exact Softblock'
326 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
328 'result' => 'Exact Softblock'
330 array( 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
332 'result' => 'Range Hardblock'
334 array( 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
336 'result' => 'Range Hardblock'
338 array( 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
340 'result' => 'Range Softblock with AC Disabled'
342 array( 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
344 'result' => 'Exact Softblock'
346 array( 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
348 'result' => 'Range Softblock with AC Disabled'
350 array( 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
352 'result' => 'Range6 Hardblock'
358 * @dataProvider providerXff
359 * @covers Block::getBlocksForIPList
360 * @covers Block::chooseBlock
362 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
363 $list = array_map( 'trim', explode( ',', $xff ) );
364 $xffblocks = Block
::getBlocksForIPList( $list, true );
365 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
366 $block = Block
::chooseBlock( $xffblocks, $list );
367 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );
370 public function testDeprecatedConstructor() {
371 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
372 $username = 'UnthinkablySecretRandomUsername';
373 $reason = 'being irrational';
376 $u = User
::newFromName( $username );
377 if ( $u->getID() == 0 ) {
379 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
383 # Make sure the user isn't blocked
385 Block
::newFromTarget( $username ),
386 "$username should not be blocked"
391 /* address */ $username,
394 /* reason */ $reason,
403 $block->getTarget()->getName(),
405 "Target should be set properly"
408 # Check supplied parameter
412 "Reason should be non-default"
415 # Check default parameter
417 (bool)$block->prevents( 'createaccount' ),
418 "Account creation should not be blocked by default"