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' => 'm>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' => 'Meta>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( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
234 'Correct blocker name' );
235 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
236 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
239 protected function addXffBlocks() {
240 static $inited = false;
249 [ 'target' => '70.2.0.0/16',
250 'type' => Block
::TYPE_RANGE
,
251 'desc' => 'Range Hardblock',
252 'ACDisable' => false,
253 'isHardblock' => true,
254 'isAutoBlocking' => false,
256 [ 'target' => '2001:4860:4001::/48',
257 'type' => Block
::TYPE_RANGE
,
258 'desc' => 'Range6 Hardblock',
259 'ACDisable' => false,
260 'isHardblock' => true,
261 'isAutoBlocking' => false,
263 [ 'target' => '60.2.0.0/16',
264 'type' => Block
::TYPE_RANGE
,
265 'desc' => 'Range Softblock with AC Disabled',
267 'isHardblock' => false,
268 'isAutoBlocking' => false,
270 [ 'target' => '50.2.0.0/16',
271 'type' => Block
::TYPE_RANGE
,
272 'desc' => 'Range Softblock',
273 'ACDisable' => false,
274 'isHardblock' => false,
275 'isAutoBlocking' => false,
277 [ 'target' => '50.1.1.1',
278 'type' => Block
::TYPE_IP
,
279 'desc' => 'Exact Softblock',
280 'ACDisable' => false,
281 'isHardblock' => false,
282 'isAutoBlocking' => false,
286 $blocker = $this->getTestUser()->getUser();
287 foreach ( $blockList as $insBlock ) {
288 $target = $insBlock['target'];
290 if ( $insBlock['type'] === Block
::TYPE_IP
) {
291 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
292 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
293 $target = IP
::sanitizeRange( $target );
296 $block = new Block();
297 $block->setTarget( $target );
298 $block->setBlocker( $blocker );
299 $block->mReason
= $insBlock['desc'];
300 $block->mExpiry
= 'infinity';
301 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
302 $block->isHardblock( $insBlock['isHardblock'] );
303 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
308 public static function providerXff() {
310 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
312 'result' => 'Range Hardblock'
314 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
316 'result' => 'Range Softblock with AC Disabled'
318 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
320 'result' => 'Exact Softblock'
322 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
324 'result' => 'Exact Softblock'
326 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
328 'result' => 'Range Hardblock'
330 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
332 'result' => 'Range Hardblock'
334 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
336 'result' => 'Range Softblock with AC Disabled'
338 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
340 'result' => 'Exact Softblock'
342 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
344 'result' => 'Range Softblock with AC Disabled'
346 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
348 'result' => 'Range6 Hardblock'
354 * @dataProvider providerXff
355 * @covers Block::getBlocksForIPList
356 * @covers Block::chooseBlock
358 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
359 $user = $this->getUserForBlocking();
360 $this->addBlockForUser( $user );
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"
421 public function testSystemBlocks() {
422 $user = $this->getUserForBlocking();
423 $this->addBlockForUser( $user );
426 'address' => $user->getName(),
427 'reason' => 'test system block',
428 'timestamp' => wfTimestampNow(),
429 'expiry' => $this->db
->getInfinity(),
430 'byText' => 'MediaWiki default',
431 'systemBlock' => 'test',
432 'enableAutoblock' => true,
434 $block = new Block( $blockOptions );
436 $this->assertSame( 'test', $block->getSystemBlockType() );
440 $this->fail( 'Expected exception not thrown' );
441 } catch ( MWException
$ex ) {
442 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
446 $block->doAutoblock( '192.0.2.2' );
447 $this->fail( 'Expected exception not thrown' );
448 } catch ( MWException
$ex ) {
449 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );