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 'by' => $this->getTestSysop()->getUser()->getId(),
39 'reason' => 'Parce que',
40 'expiry' => time() +
100500,
42 $block = new Block( $blockOptions );
45 // save up ID for use in assertion. Since ID is an autoincrement,
46 // its value might change depending on the order the tests are run.
47 // ApiBlockTest insert its own blocks!
48 if ( !$block->getId() ) {
49 throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
52 $this->addXffBlocks();
58 * @covers Block::newFromTarget
60 public function testINewFromTargetReturnsCorrectBlock() {
61 $user = $this->getUserForBlocking();
62 $block = $this->addBlockForUser( $user );
65 $block->equals( Block
::newFromTarget( $user->getName() ) ),
66 "newFromTarget() returns the same block as the one that was made"
71 * @covers Block::newFromID
73 public function testINewFromIDReturnsCorrectBlock() {
74 $user = $this->getUserForBlocking();
75 $block = $this->addBlockForUser( $user );
78 $block->equals( Block
::newFromID( $block->getId() ) ),
79 "newFromID() returns the same block as the one that was made"
85 * @covers Block::__construct
87 public function testT28425BlockTimestampDefaultsToTime() {
88 $user = $this->getUserForBlocking();
89 $block = $this->addBlockForUser( $user );
90 $madeAt = wfTimestamp( TS_MW
);
92 // delta to stop one-off errors when things happen to go over a second mark.
93 $delta = abs( $madeAt - $block->mTimestamp
);
94 $this->assertLessThan(
97 "If no timestamp is specified, the block is recorded as time()"
102 * CheckUser since being changed to use Block::newFromTarget started failing
103 * because the new function didn't accept empty strings like Block::load()
104 * had. Regression T31116.
106 * @dataProvider provideT31116Data
107 * @covers Block::newFromTarget
109 public function testT31116NewFromTargetWithEmptyIp( $vagueTarget ) {
110 $user = $this->getUserForBlocking();
111 $initialBlock = $this->addBlockForUser( $user );
112 $block = Block
::newFromTarget( $user->getName(), $vagueTarget );
115 $initialBlock->equals( $block ),
116 "newFromTarget() returns the same block as the one that was made when "
117 . "given empty vagueTarget param " . var_export( $vagueTarget, true )
121 public static function provideT31116Data() {
130 * @covers Block::prevents
132 public function testBlockedUserCanNotCreateAccount() {
133 $username = 'BlockedUserToCreateAccountWith';
134 $u = User
::newFromName( $username );
136 $userId = $u->getId();
137 $this->assertNotEquals( 0, $userId, 'sanity' );
138 TestUser
::setPasswordForUser( $u, 'NotRandomPass' );
143 Block
::newFromTarget( $username ),
144 "$username should not be blocked"
148 $u = User
::newFromName( $username );
150 $u->isBlockedFromCreateAccount(),
151 "Our sandbox user should be able to create account before being blocked"
154 // Foreign perspective (blockee not on current wiki)...
156 'address' => $username,
158 'reason' => 'crosswiki block...',
159 'timestamp' => wfTimestampNow(),
160 'expiry' => $this->db
->getInfinity(),
161 'createAccount' => true,
162 'enableAutoblock' => true,
164 'blockEmail' => true,
165 'byText' => 'm>MetaWikiUser',
167 $block = new Block( $blockOptions );
170 // Reload block from DB
171 $userBlock = Block
::newFromTarget( $username );
173 (bool)$block->prevents( 'createaccount' ),
174 "Block object in DB should prevents 'createaccount'"
177 $this->assertInstanceOf(
180 "'$username' block block object should be existent"
184 $u = User
::newFromName( $username );
186 (bool)$u->isBlockedFromCreateAccount(),
187 "Our sandbox user '$username' should NOT be able to create account"
192 * @covers Block::insert
194 public function testCrappyCrossWikiBlocks() {
195 // Delete the last round's block if it's still there
196 $oldBlock = Block
::newFromTarget( 'UserOnForeignWiki' );
198 // An old block will prevent our new one from saving.
202 // Local perspective (blockee on current wiki)...
203 $user = User
::newFromName( 'UserOnForeignWiki' );
204 $user->addToDatabase();
205 $userId = $user->getId();
206 $this->assertNotEquals( 0, $userId, 'sanity' );
208 // Foreign perspective (blockee not on current wiki)...
210 'address' => 'UserOnForeignWiki',
211 'user' => $user->getId(),
212 'reason' => 'crosswiki block...',
213 'timestamp' => wfTimestampNow(),
214 'expiry' => $this->db
->getInfinity(),
215 'createAccount' => true,
216 'enableAutoblock' => true,
218 'blockEmail' => true,
219 'byText' => 'Meta>MetaWikiUser',
221 $block = new Block( $blockOptions );
223 $res = $block->insert( $this->db
);
224 $this->assertTrue( (bool)$res['id'], 'Block succeeded' );
226 $user = null; // clear
228 $block = Block
::newFromID( $res['id'] );
231 $block->getTarget()->getName(),
232 'Correct blockee name'
234 $this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
235 $this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
236 'Correct blocker name' );
237 $this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
238 $this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
241 protected function addXffBlocks() {
242 static $inited = false;
251 [ 'target' => '70.2.0.0/16',
252 'type' => Block
::TYPE_RANGE
,
253 'desc' => 'Range Hardblock',
254 'ACDisable' => false,
255 'isHardblock' => true,
256 'isAutoBlocking' => false,
258 [ 'target' => '2001:4860:4001::/48',
259 'type' => Block
::TYPE_RANGE
,
260 'desc' => 'Range6 Hardblock',
261 'ACDisable' => false,
262 'isHardblock' => true,
263 'isAutoBlocking' => false,
265 [ 'target' => '60.2.0.0/16',
266 'type' => Block
::TYPE_RANGE
,
267 'desc' => 'Range Softblock with AC Disabled',
269 'isHardblock' => false,
270 'isAutoBlocking' => false,
272 [ 'target' => '50.2.0.0/16',
273 'type' => Block
::TYPE_RANGE
,
274 'desc' => 'Range Softblock',
275 'ACDisable' => false,
276 'isHardblock' => false,
277 'isAutoBlocking' => false,
279 [ 'target' => '50.1.1.1',
280 'type' => Block
::TYPE_IP
,
281 'desc' => 'Exact Softblock',
282 'ACDisable' => false,
283 'isHardblock' => false,
284 'isAutoBlocking' => false,
288 $blocker = $this->getTestUser()->getUser();
289 foreach ( $blockList as $insBlock ) {
290 $target = $insBlock['target'];
292 if ( $insBlock['type'] === Block
::TYPE_IP
) {
293 $target = User
::newFromName( IP
::sanitizeIP( $target ), false )->getName();
294 } elseif ( $insBlock['type'] === Block
::TYPE_RANGE
) {
295 $target = IP
::sanitizeRange( $target );
298 $block = new Block();
299 $block->setTarget( $target );
300 $block->setBlocker( $blocker );
301 $block->mReason
= $insBlock['desc'];
302 $block->mExpiry
= 'infinity';
303 $block->prevents( 'createaccount', $insBlock['ACDisable'] );
304 $block->isHardblock( $insBlock['isHardblock'] );
305 $block->isAutoblocking( $insBlock['isAutoBlocking'] );
310 public static function providerXff() {
312 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
314 'result' => 'Range Hardblock'
316 [ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
318 'result' => 'Range Softblock with AC Disabled'
320 [ 'xff' => '1.2.3.4, 70.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, 50.1.1.1, 2.3.4.5',
326 'result' => 'Exact Softblock'
328 [ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
330 'result' => 'Range Hardblock'
332 [ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
334 'result' => 'Range Hardblock'
336 [ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
338 'result' => 'Range Softblock with AC Disabled'
340 [ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
342 'result' => 'Exact Softblock'
344 [ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
346 'result' => 'Range Softblock with AC Disabled'
348 [ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
350 'result' => 'Range6 Hardblock'
356 * @dataProvider providerXff
357 * @covers Block::getBlocksForIPList
358 * @covers Block::chooseBlock
360 public function testBlocksOnXff( $xff, $exCount, $exResult ) {
361 $user = $this->getUserForBlocking();
362 $this->addBlockForUser( $user );
364 $list = array_map( 'trim', explode( ',', $xff ) );
365 $xffblocks = Block
::getBlocksForIPList( $list, true );
366 $this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
367 $block = Block
::chooseBlock( $xffblocks, $list );
368 $this->assertEquals( $exResult, $block->mReason
, 'Correct block type for XFF header ' . $xff );
372 * @covers Block::__construct
374 public function testDeprecatedConstructor() {
375 $this->hideDeprecated( 'Block::__construct with multiple arguments' );
376 $username = 'UnthinkablySecretRandomUsername';
377 $reason = 'being irrational';
380 $u = User
::newFromName( $username );
381 if ( $u->getId() == 0 ) {
383 TestUser
::setPasswordForUser( $u, 'TotallyObvious' );
387 # Make sure the user isn't blocked
389 Block
::newFromTarget( $username ),
390 "$username should not be blocked"
395 /* address */ $username,
397 /* by */ $this->getTestSysop()->getUser()->getId(),
398 /* reason */ $reason,
407 $block->getTarget()->getName(),
409 "Target should be set properly"
412 # Check supplied parameter
416 "Reason should be non-default"
419 # Check default parameter
421 (bool)$block->prevents( 'createaccount' ),
422 "Account creation should not be blocked by default"
427 * @covers Block::getSystemBlockType
428 * @covers Block::insert
429 * @covers Block::doAutoblock
431 public function testSystemBlocks() {
432 $user = $this->getUserForBlocking();
433 $this->addBlockForUser( $user );
436 'address' => $user->getName(),
437 'reason' => 'test system block',
438 'timestamp' => wfTimestampNow(),
439 'expiry' => $this->db
->getInfinity(),
440 'byText' => 'MediaWiki default',
441 'systemBlock' => 'test',
442 'enableAutoblock' => true,
444 $block = new Block( $blockOptions );
446 $this->assertSame( 'test', $block->getSystemBlockType() );
450 $this->fail( 'Expected exception not thrown' );
451 } catch ( MWException
$ex ) {
452 $this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
456 $block->doAutoblock( '192.0.2.2' );
457 $this->fail( 'Expected exception not thrown' );
458 } catch ( MWException
$ex ) {
459 $this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );