7 class BlockTest
extends MediaWikiLangTestCase
{
12 private function getUserForBlocking() {
13 $testUser = $this->getMutableTestUser();
14 $user = $testUser->getUser();
15 $user->addToDatabase();
16 TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
17 $user->saveSettings();
27 private function addBlockForUser( User
$user ) {
28 // Delete the last round's block if it's still there
29 $oldBlock = Block
::newFromTarget( $user->getName() );
31 // An old block will prevent our new one from saving.
36 'address' => $user->getName(),
37 'user' => $user->getId(),
38 'reason' => 'Parce que',
39 'expiry' => time() +
100500,
41 $block = new Block( $blockOptions );
44 // save up ID for use in assertion. Since ID is an autoincrement,
45 // its value might change depending on the order the tests are run.
46 // ApiBlockTest insert its own blocks!
47 if ( !$block->getId() ) {
48 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
51 $this->addXffBlocks();
57 * @covers Block::newFromTarget
59 public function testINewFromTargetReturnsCorrectBlock() {
60 $user = $this->getUserForBlocking();
61 $block = $this->addBlockForUser( $user );
64 $block->equals( Block
::newFromTarget( $user->getName() ) ),
65 "newFromTarget() returns the same block as the one that was made"
70 * @covers Block::newFromID
72 public function testINewFromIDReturnsCorrectBlock() {
73 $user = $this->getUserForBlocking();
74 $block = $this->addBlockForUser( $user );
77 $block->equals( Block
::newFromID( $block->getId() ) ),
78 "newFromID() returns the same block as the one that was made"
85 public function testBug26425BlockTimestampDefaultsToTime() {
86 $user = $this->getUserForBlocking();
87 $block = $this->addBlockForUser( $user );
88 $madeAt = wfTimestamp( TS_MW
);
90 // delta to stop one-off errors when things happen to go over a second mark.
91 $delta = abs( $madeAt - $block->mTimestamp
);
92 $this->assertLessThan(
95 "If no timestamp is specified, the block is recorded as time()"
100 * CheckUser since being changed to use Block::newFromTarget started failing
101 * because the new function didn't accept empty strings like Block::load()
102 * had. Regression T31116.
104 * @dataProvider provideBug29116Data
105 * @covers Block::newFromTarget
107 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
108 $user = $this->getUserForBlocking();
109 $initialBlock = $this->addBlockForUser( $user );
110 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
113 $initialBlock->equals( $block ),
114 "newFromTarget() returns the same block as the one that was made when "
115 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
119 public static function provideBug29116Data() {
128 * @covers Block::prevents
130 public function testBlockedUserCanNotCreateAccount() {
131 $username = 'BlockedUserToCreateAccountWith';
132 $u = User
::newFromName( $username );
134 $userId = $u->getId();
135 $this->assertNotEquals( 0, $userId, 'sanity' );
136 TestUser
::setPasswordForUser( $u, '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,
156 'reason' => 'crosswiki block...',
157 'timestamp' => wfTimestampNow(),
158 'expiry' => $this->db
->getInfinity(),
159 'createAccount' => true,
160 'enableAutoblock' => true,
162 'blockEmail' => true,
163 'byText' => 'MetaWikiUser',
165 $block = new Block( $blockOptions );
168 // Reload block from DB
169 $userBlock = Block
::newFromTarget( $username );
171 (bool)$block->prevents( 'createaccount' ),
172 "Block object in DB should prevents 'createaccount'"
175 $this->assertInstanceOf(
178 "'$username' block block object should be existent"
182 $u = User
::newFromName( $username );
184 (bool)$u->isBlockedFromCreateAccount(),
185 "Our sandbox user '$username' should NOT be able to create account"
190 * @covers Block::insert
192 public function testCrappyCrossWikiBlocks() {
193 // Delete the last round's block if it's still there
194 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
196 // An old block will prevent our new one from saving.
200 // Local perspective (blockee on current wiki)...
201 $user = User
::newFromName( 'UserOnForeignWiki' );
202 $user->addToDatabase();
203 $userId = $user->getId();
204 $this->assertNotEquals( 0, $userId, 'sanity' );
206 // Foreign perspective (blockee not on current wiki)...
208 'address' => 'UserOnForeignWiki',
209 'user' => $user->getId(),
210 'reason' => 'crosswiki block...',
211 'timestamp' => wfTimestampNow(),
212 'expiry' => $this->db
->getInfinity(),
213 'createAccount' => true,
214 'enableAutoblock' => true,
216 'blockEmail' => true,
217 'byText' => 'MetaWikiUser',
219 $block = new Block( $blockOptions );
221 $res = $block->insert( $this->db
);
222 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
224 $user = null; // clear
226 $block = Block
::newFromID( $res['id'] );
229 $block->getTarget()->getName(),
230 'Correct blockee name'
232 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
233 $this->assertEquals( 'MetaWikiUser', $block->getBlocker(), 'Correct blocker name' );
234 $this->assertEquals( 'MetaWikiUser', $block->getByName(), 'Correct blocker name' );
235 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
238 protected function addXffBlocks() {
239 static $inited = false;
248 [ 'target' => '70.2.0.0/16',
249 'type' => Block
::TYPE_RANGE
,
250 'desc' => 'Range Hardblock',
251 'ACDisable' => false,
252 'isHardblock' => true,
253 'isAutoBlocking' => false,
255 [ 'target' => '2001:4860:4001::/48',
256 'type' => Block
::TYPE_RANGE
,
257 'desc' => 'Range6 Hardblock',
258 'ACDisable' => false,
259 'isHardblock' => true,
260 'isAutoBlocking' => false,
262 [ 'target' => '60.2.0.0/16',
263 'type' => Block
::TYPE_RANGE
,
264 'desc' => 'Range Softblock with AC Disabled',
266 'isHardblock' => false,
267 'isAutoBlocking' => false,
269 [ 'target' => '50.2.0.0/16',
270 'type' => Block
::TYPE_RANGE
,
271 'desc' => 'Range Softblock',
272 'ACDisable' => false,
273 'isHardblock' => false,
274 'isAutoBlocking' => false,
276 [ 'target' => '50.1.1.1',
277 'type' => Block
::TYPE_IP
,
278 'desc' => 'Exact Softblock',
279 'ACDisable' => false,
280 'isHardblock' => false,
281 'isAutoBlocking' => false,
285 foreach ( $blockList as $insBlock ) {
286 $target = $insBlock['target'];
288 if ( $insBlock['type'] === Block
::TYPE_IP
) {
289 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
290 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
291 $target = IP
::sanitizeRange( $target );
294 $block = new Block();
295 $block->setTarget( $target );
296 $block->setBlocker( 'testblocker@global' );
297 $block->mReason
= $insBlock['desc'];
298 $block->mExpiry
= 'infinity';
299 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
300 $block->isHardblock( $insBlock['isHardblock'] );
301 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
306 public static function providerXff() {
308 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
310 'result' => 'Range Hardblock'
312 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
314 'result' => 'Range Softblock with AC Disabled'
316 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
318 'result' => 'Exact Softblock'
320 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
322 'result' => 'Exact Softblock'
324 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
326 'result' => 'Range Hardblock'
328 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
330 'result' => 'Range Hardblock'
332 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
334 'result' => 'Range Softblock with AC Disabled'
336 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
338 'result' => 'Exact Softblock'
340 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
342 'result' => 'Range Softblock with AC Disabled'
344 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
346 'result' => 'Range6 Hardblock'
352 * @dataProvider providerXff
353 * @covers Block::getBlocksForIPList
354 * @covers Block::chooseBlock
356 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
357 $user = $this->getUserForBlocking();
358 $this->addBlockForUser( $user );
360 $list = array_map( 'trim', explode( ',', $xff ) );
361 $xffblocks = Block
::getBlocksForIPList( $list, true );
362 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
363 $block = Block
::chooseBlock( $xffblocks, $list );
364 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );
367 public function testDeprecatedConstructor() {
368 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
369 $username = 'UnthinkablySecretRandomUsername';
370 $reason = 'being irrational';
373 $u = User
::newFromName( $username );
374 if ( $u->getId() == 0 ) {
376 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
380 # Make sure the user isn't blocked
382 Block
::newFromTarget( $username ),
383 "$username should not be blocked"
388 /* address */ $username,
391 /* reason */ $reason,
400 $block->getTarget()->getName(),
402 "Target should be set properly"
405 # Check supplied parameter
409 "Reason should be non-default"
412 # Check default parameter
414 (bool)$block->prevents( 'createaccount' ),
415 "Account creation should not be blocked by default"
419 public function testSystemBlocks() {
420 $user = $this->getUserForBlocking();
421 $this->addBlockForUser( $user );
424 'address' => $user->getName(),
425 'reason' => 'test system block',
426 'timestamp' => wfTimestampNow(),
427 'expiry' => $this->db
->getInfinity(),
428 'byText' => 'MetaWikiUser',
429 'systemBlock' => 'test',
430 'enableAutoblock' => true,
432 $block = new Block( $blockOptions );
434 $this->assertSame( 'test', $block->getSystemBlockType() );
438 $this->fail( 'Expected exception not thrown' );
439 } catch ( MWException
$ex ) {
440 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
444 $block->doAutoblock( '192.0.2.2' );
445 $this->fail( 'Expected exception not thrown' );
446 } catch ( MWException
$ex ) {
447 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );