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"
84 * @covers Block::__construct
86 public function testBug26425BlockTimestampDefaultsToTime() {
87 $user = $this->getUserForBlocking();
88 $block = $this->addBlockForUser( $user );
89 $madeAt = wfTimestamp( TS_MW
);
91 // delta to stop one-off errors when things happen to go over a second mark.
92 $delta = abs( $madeAt - $block->mTimestamp
);
93 $this->assertLessThan(
96 "If no timestamp is specified, the block is recorded as time()"
101 * CheckUser since being changed to use Block::newFromTarget started failing
102 * because the new function didn't accept empty strings like Block::load()
103 * had. Regression T31116.
105 * @dataProvider provideBug29116Data
106 * @covers Block::newFromTarget
108 public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
109 $user = $this->getUserForBlocking();
110 $initialBlock = $this->addBlockForUser( $user );
111 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
114 $initialBlock->equals( $block ),
115 "newFromTarget() returns the same block as the one that was made when "
116 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
120 public static function provideBug29116Data() {
129 * @covers Block::prevents
131 public function testBlockedUserCanNotCreateAccount() {
132 $username = 'BlockedUserToCreateAccountWith';
133 $u = User
::newFromName( $username );
135 $userId = $u->getId();
136 $this->assertNotEquals( 0, $userId, 'sanity' );
137 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
142 Block
::newFromTarget( $username ),
143 "$username should not be blocked"
147 $u = User
::newFromName( $username );
149 $u->isBlockedFromCreateAccount(),
150 "Our sandbox user should be able to create account before being blocked"
153 // Foreign perspective (blockee not on current wiki)...
155 'address' => $username,
157 'reason' => 'crosswiki block...',
158 'timestamp' => wfTimestampNow(),
159 'expiry' => $this->db
->getInfinity(),
160 'createAccount' => true,
161 'enableAutoblock' => true,
163 'blockEmail' => true,
164 'byText' => 'm>MetaWikiUser',
166 $block = new Block( $blockOptions );
169 // Reload block from DB
170 $userBlock = Block
::newFromTarget( $username );
172 (bool)$block->prevents( 'createaccount' ),
173 "Block object in DB should prevents 'createaccount'"
176 $this->assertInstanceOf(
179 "'$username' block block object should be existent"
183 $u = User
::newFromName( $username );
185 (bool)$u->isBlockedFromCreateAccount(),
186 "Our sandbox user '$username' should NOT be able to create account"
191 * @covers Block::insert
193 public function testCrappyCrossWikiBlocks() {
194 // Delete the last round's block if it's still there
195 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
197 // An old block will prevent our new one from saving.
201 // Local perspective (blockee on current wiki)...
202 $user = User
::newFromName( 'UserOnForeignWiki' );
203 $user->addToDatabase();
204 $userId = $user->getId();
205 $this->assertNotEquals( 0, $userId, 'sanity' );
207 // Foreign perspective (blockee not on current wiki)...
209 'address' => 'UserOnForeignWiki',
210 'user' => $user->getId(),
211 'reason' => 'crosswiki block...',
212 'timestamp' => wfTimestampNow(),
213 'expiry' => $this->db
->getInfinity(),
214 'createAccount' => true,
215 'enableAutoblock' => true,
217 'blockEmail' => true,
218 'byText' => 'Meta>MetaWikiUser',
220 $block = new Block( $blockOptions );
222 $res = $block->insert( $this->db
);
223 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
225 $user = null; // clear
227 $block = Block
::newFromID( $res['id'] );
230 $block->getTarget()->getName(),
231 'Correct blockee name'
233 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
234 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
235 'Correct blocker name' );
236 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
237 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
240 protected function addXffBlocks() {
241 static $inited = false;
250 [ 'target' => '70.2.0.0/16',
251 'type' => Block
::TYPE_RANGE
,
252 'desc' => 'Range Hardblock',
253 'ACDisable' => false,
254 'isHardblock' => true,
255 'isAutoBlocking' => false,
257 [ 'target' => '2001:4860:4001::/48',
258 'type' => Block
::TYPE_RANGE
,
259 'desc' => 'Range6 Hardblock',
260 'ACDisable' => false,
261 'isHardblock' => true,
262 'isAutoBlocking' => false,
264 [ 'target' => '60.2.0.0/16',
265 'type' => Block
::TYPE_RANGE
,
266 'desc' => 'Range Softblock with AC Disabled',
268 'isHardblock' => false,
269 'isAutoBlocking' => false,
271 [ 'target' => '50.2.0.0/16',
272 'type' => Block
::TYPE_RANGE
,
273 'desc' => 'Range Softblock',
274 'ACDisable' => false,
275 'isHardblock' => false,
276 'isAutoBlocking' => false,
278 [ 'target' => '50.1.1.1',
279 'type' => Block
::TYPE_IP
,
280 'desc' => 'Exact Softblock',
281 'ACDisable' => false,
282 'isHardblock' => false,
283 'isAutoBlocking' => false,
287 $blocker = $this->getTestUser()->getUser();
288 foreach ( $blockList as $insBlock ) {
289 $target = $insBlock['target'];
291 if ( $insBlock['type'] === Block
::TYPE_IP
) {
292 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
293 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
294 $target = IP
::sanitizeRange( $target );
297 $block = new Block();
298 $block->setTarget( $target );
299 $block->setBlocker( $blocker );
300 $block->mReason
= $insBlock['desc'];
301 $block->mExpiry
= 'infinity';
302 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
303 $block->isHardblock( $insBlock['isHardblock'] );
304 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
309 public static function providerXff() {
311 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
313 'result' => 'Range Hardblock'
315 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
317 'result' => 'Range Softblock with AC Disabled'
319 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
321 'result' => 'Exact Softblock'
323 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
325 'result' => 'Exact Softblock'
327 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
329 'result' => 'Range Hardblock'
331 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
333 'result' => 'Range Hardblock'
335 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
337 'result' => 'Range Softblock with AC Disabled'
339 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
341 'result' => 'Exact Softblock'
343 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
345 'result' => 'Range Softblock with AC Disabled'
347 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
349 'result' => 'Range6 Hardblock'
355 * @dataProvider providerXff
356 * @covers Block::getBlocksForIPList
357 * @covers Block::chooseBlock
359 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
360 $user = $this->getUserForBlocking();
361 $this->addBlockForUser( $user );
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 );
371 * @covers Block::__construct
373 public function testDeprecatedConstructor() {
374 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
375 $username = 'UnthinkablySecretRandomUsername';
376 $reason = 'being irrational';
379 $u = User
::newFromName( $username );
380 if ( $u->getId() == 0 ) {
382 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
386 # Make sure the user isn't blocked
388 Block
::newFromTarget( $username ),
389 "$username should not be blocked"
394 /* address */ $username,
397 /* reason */ $reason,
406 $block->getTarget()->getName(),
408 "Target should be set properly"
411 # Check supplied parameter
415 "Reason should be non-default"
418 # Check default parameter
420 (bool)$block->prevents( 'createaccount' ),
421 "Account creation should not be blocked by default"
426 * @covers Block::getSystemBlockType
427 * @covers Block::insert
428 * @covers Block::doAutoblock
430 public function testSystemBlocks() {
431 $user = $this->getUserForBlocking();
432 $this->addBlockForUser( $user );
435 'address' => $user->getName(),
436 'reason' => 'test system block',
437 'timestamp' => wfTimestampNow(),
438 'expiry' => $this->db
->getInfinity(),
439 'byText' => 'MediaWiki default',
440 'systemBlock' => 'test',
441 'enableAutoblock' => true,
443 $block = new Block( $blockOptions );
445 $this->assertSame( 'test', $block->getSystemBlockType() );
449 $this->fail( 'Expected exception not thrown' );
450 } catch ( MWException
$ex ) {
451 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
455 $block->doAutoblock( '192.0.2.2' );
456 $this->fail( 'Expected exception not thrown' );
457 } catch ( MWException
$ex ) {
458 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );