2010-12-28 19:12:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
2010-12-29 15:01:47 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
2011-10-31 17:00:29 +00:00
|
|
|
* @group Blocking
|
2010-12-29 15:01:47 +00:00
|
|
|
*/
|
2011-06-14 21:27:07 +00:00
|
|
|
class BlockTest extends MediaWikiLangTestCase {
|
2011-06-03 06:22:18 +00:00
|
|
|
|
2017-10-25 17:22:32 +00:00
|
|
|
/**
|
|
|
|
|
* @return User
|
|
|
|
|
*/
|
|
|
|
|
private function getUserForBlocking() {
|
|
|
|
|
$testUser = $this->getMutableTestUser();
|
|
|
|
|
$user = $testUser->getUser();
|
|
|
|
|
$user->addToDatabase();
|
|
|
|
|
TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
|
|
|
|
|
$user->saveSettings();
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
2011-06-15 20:43:24 +00:00
|
|
|
|
2017-10-25 17:22:32 +00:00
|
|
|
/**
|
|
|
|
|
* @param User $user
|
|
|
|
|
*
|
|
|
|
|
* @return Block
|
|
|
|
|
* @throws MWException
|
|
|
|
|
*/
|
|
|
|
|
private function addBlockForUser( User $user ) {
|
2011-06-15 20:43:24 +00:00
|
|
|
// Delete the last round's block if it's still there
|
2017-10-25 17:22:32 +00:00
|
|
|
$oldBlock = Block::newFromTarget( $user->getName() );
|
2011-06-15 20:43:24 +00:00
|
|
|
if ( $oldBlock ) {
|
|
|
|
|
// An old block will prevent our new one from saving.
|
|
|
|
|
$oldBlock->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$blockOptions = [
|
2017-10-25 17:22:32 +00:00
|
|
|
'address' => $user->getName(),
|
2016-03-18 13:55:54 +00:00
|
|
|
'user' => $user->getId(),
|
2014-05-22 14:45:46 +00:00
|
|
|
'reason' => 'Parce que',
|
|
|
|
|
'expiry' => time() + 100500,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2017-10-25 17:22:32 +00:00
|
|
|
$block = new Block( $blockOptions );
|
2010-12-28 19:12:27 +00:00
|
|
|
|
2017-10-25 17:22:32 +00:00
|
|
|
$block->insert();
|
2011-05-28 21:25:29 +00:00
|
|
|
// save up ID for use in assertion. Since ID is an autoincrement,
|
|
|
|
|
// its value might change depending on the order the tests are run.
|
|
|
|
|
// ApiBlockTest insert its own blocks!
|
2017-10-25 17:22:32 +00:00
|
|
|
if ( !$block->getId() ) {
|
2011-06-15 20:43:24 +00:00
|
|
|
throw new MWException( "Failed to insert block for BlockTest; old leftover block remaining?" );
|
|
|
|
|
}
|
2013-03-30 17:05:15 +00:00
|
|
|
|
|
|
|
|
$this->addXffBlocks();
|
2011-05-28 21:25:29 +00:00
|
|
|
|
2017-10-25 17:22:32 +00:00
|
|
|
return $block;
|
2011-05-28 21:25:29 +00:00
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Block::newFromTarget
|
|
|
|
|
*/
|
|
|
|
|
public function testINewFromTargetReturnsCorrectBlock() {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$block = $this->addBlockForUser( $user );
|
|
|
|
|
|
2014-04-24 09:57:41 +00:00
|
|
|
$this->assertTrue(
|
2017-10-25 17:22:32 +00:00
|
|
|
$block->equals( Block::newFromTarget( $user->getName() ) ),
|
2014-04-24 09:57:41 +00:00
|
|
|
"newFromTarget() returns the same block as the one that was made"
|
|
|
|
|
);
|
2013-10-24 10:54:02 +00:00
|
|
|
}
|
2012-04-10 00:12:06 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Block::newFromID
|
|
|
|
|
*/
|
|
|
|
|
public function testINewFromIDReturnsCorrectBlock() {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$block = $this->addBlockForUser( $user );
|
|
|
|
|
|
2014-04-24 09:57:41 +00:00
|
|
|
$this->assertTrue(
|
2017-10-25 17:22:32 +00:00
|
|
|
$block->equals( Block::newFromID( $block->getId() ) ),
|
2014-04-24 09:57:41 +00:00
|
|
|
"newFromID() returns the same block as the one that was made"
|
|
|
|
|
);
|
2010-12-28 19:12:27 +00:00
|
|
|
}
|
2011-06-26 20:04:38 +00:00
|
|
|
|
2010-12-28 19:27:34 +00:00
|
|
|
/**
|
2017-02-20 23:45:58 +00:00
|
|
|
* per T28425
|
2010-12-28 19:27:34 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testBug26425BlockTimestampDefaultsToTime() {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$block = $this->addBlockForUser( $user );
|
|
|
|
|
$madeAt = wfTimestamp( TS_MW );
|
|
|
|
|
|
2011-08-18 06:23:44 +00:00
|
|
|
// delta to stop one-off errors when things happen to go over a second mark.
|
2017-10-25 17:22:32 +00:00
|
|
|
$delta = abs( $madeAt - $block->mTimestamp );
|
2014-04-24 09:57:41 +00:00
|
|
|
$this->assertLessThan(
|
|
|
|
|
2,
|
|
|
|
|
$delta,
|
|
|
|
|
"If no timestamp is specified, the block is recorded as time()"
|
|
|
|
|
);
|
2010-12-28 19:27:34 +00:00
|
|
|
}
|
2010-12-28 19:12:27 +00:00
|
|
|
|
2011-05-24 21:04:50 +00:00
|
|
|
/**
|
|
|
|
|
* CheckUser since being changed to use Block::newFromTarget started failing
|
|
|
|
|
* because the new function didn't accept empty strings like Block::load()
|
2017-02-20 23:45:58 +00:00
|
|
|
* had. Regression T31116.
|
2011-05-24 21:04:50 +00:00
|
|
|
*
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideBug29116Data
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Block::newFromTarget
|
2011-05-24 21:04:50 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testBug29116NewFromTargetWithEmptyIp( $vagueTarget ) {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$initialBlock = $this->addBlockForUser( $user );
|
|
|
|
|
$block = Block::newFromTarget( $user->getName(), $vagueTarget );
|
|
|
|
|
|
2014-04-24 09:57:41 +00:00
|
|
|
$this->assertTrue(
|
2017-10-25 17:22:32 +00:00
|
|
|
$initialBlock->equals( $block ),
|
2014-04-24 09:57:41 +00:00
|
|
|
"newFromTarget() returns the same block as the one that was made when "
|
|
|
|
|
. "given empty vagueTarget param " . var_export( $vagueTarget, true )
|
|
|
|
|
);
|
2011-05-24 21:04:50 +00:00
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideBug29116Data() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ null ],
|
|
|
|
|
[ '' ],
|
|
|
|
|
[ false ]
|
|
|
|
|
];
|
2011-05-24 21:04:50 +00:00
|
|
|
}
|
2012-04-10 00:12:06 +00:00
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Block::prevents
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testBlockedUserCanNotCreateAccount() {
|
2012-07-13 09:43:17 +00:00
|
|
|
$username = 'BlockedUserToCreateAccountWith';
|
|
|
|
|
$u = User::newFromName( $username );
|
|
|
|
|
$u->addToDatabase();
|
2015-11-19 22:09:29 +00:00
|
|
|
$userId = $u->getId();
|
|
|
|
|
$this->assertNotEquals( 0, $userId, 'sanity' );
|
2015-09-04 16:17:42 +00:00
|
|
|
TestUser::setPasswordForUser( $u, 'NotRandomPass' );
|
2012-07-13 09:43:17 +00:00
|
|
|
unset( $u );
|
|
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
|
$this->assertNull(
|
|
|
|
|
Block::newFromTarget( $username ),
|
|
|
|
|
"$username should not be blocked"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Reload user
|
|
|
|
|
$u = User::newFromName( $username );
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
$u->isBlockedFromCreateAccount(),
|
|
|
|
|
"Our sandbox user should be able to create account before being blocked"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Foreign perspective (blockee not on current wiki)...
|
2016-02-17 09:09:32 +00:00
|
|
|
$blockOptions = [
|
2014-05-22 14:45:46 +00:00
|
|
|
'address' => $username,
|
2015-11-19 22:09:29 +00:00
|
|
|
'user' => $userId,
|
2014-05-22 14:45:46 +00:00
|
|
|
'reason' => 'crosswiki block...',
|
|
|
|
|
'timestamp' => wfTimestampNow(),
|
|
|
|
|
'expiry' => $this->db->getInfinity(),
|
|
|
|
|
'createAccount' => true,
|
|
|
|
|
'enableAutoblock' => true,
|
|
|
|
|
'hideName' => true,
|
|
|
|
|
'blockEmail' => true,
|
2017-10-25 19:26:53 +00:00
|
|
|
'byText' => 'm>MetaWikiUser',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-05-22 14:45:46 +00:00
|
|
|
$block = new Block( $blockOptions );
|
2012-07-13 09:43:17 +00:00
|
|
|
$block->insert();
|
|
|
|
|
|
|
|
|
|
// Reload block from DB
|
|
|
|
|
$userBlock = Block::newFromTarget( $username );
|
|
|
|
|
$this->assertTrue(
|
2013-01-28 10:27:15 +00:00
|
|
|
(bool)$block->prevents( 'createaccount' ),
|
2012-07-13 09:43:17 +00:00
|
|
|
"Block object in DB should prevents 'createaccount'"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf(
|
|
|
|
|
'Block',
|
|
|
|
|
$userBlock,
|
|
|
|
|
"'$username' block block object should be existent"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Reload user
|
|
|
|
|
$u = User::newFromName( $username );
|
|
|
|
|
$this->assertTrue(
|
2013-01-28 10:27:15 +00:00
|
|
|
(bool)$u->isBlockedFromCreateAccount(),
|
2012-07-13 09:43:17 +00:00
|
|
|
"Our sandbox user '$username' should NOT be able to create account"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-24 10:54:02 +00:00
|
|
|
/**
|
|
|
|
|
* @covers Block::insert
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCrappyCrossWikiBlocks() {
|
2012-04-10 00:12:06 +00:00
|
|
|
// Delete the last round's block if it's still there
|
|
|
|
|
$oldBlock = Block::newFromTarget( 'UserOnForeignWiki' );
|
|
|
|
|
if ( $oldBlock ) {
|
|
|
|
|
// An old block will prevent our new one from saving.
|
|
|
|
|
$oldBlock->delete();
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-08 22:18:20 +00:00
|
|
|
// Local perspective (blockee on current wiki)...
|
|
|
|
|
$user = User::newFromName( 'UserOnForeignWiki' );
|
|
|
|
|
$user->addToDatabase();
|
2015-11-19 22:09:29 +00:00
|
|
|
$userId = $user->getId();
|
|
|
|
|
$this->assertNotEquals( 0, $userId, 'sanity' );
|
2013-12-08 22:18:20 +00:00
|
|
|
|
2012-04-10 00:12:06 +00:00
|
|
|
// Foreign perspective (blockee not on current wiki)...
|
2016-02-17 09:09:32 +00:00
|
|
|
$blockOptions = [
|
2014-05-22 14:45:46 +00:00
|
|
|
'address' => 'UserOnForeignWiki',
|
2015-11-19 22:09:29 +00:00
|
|
|
'user' => $user->getId(),
|
2014-05-22 14:45:46 +00:00
|
|
|
'reason' => 'crosswiki block...',
|
|
|
|
|
'timestamp' => wfTimestampNow(),
|
|
|
|
|
'expiry' => $this->db->getInfinity(),
|
|
|
|
|
'createAccount' => true,
|
|
|
|
|
'enableAutoblock' => true,
|
|
|
|
|
'hideName' => true,
|
|
|
|
|
'blockEmail' => true,
|
2017-10-25 19:26:53 +00:00
|
|
|
'byText' => 'Meta>MetaWikiUser',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-05-22 14:45:46 +00:00
|
|
|
$block = new Block( $blockOptions );
|
2012-04-10 00:12:06 +00:00
|
|
|
|
|
|
|
|
$res = $block->insert( $this->db );
|
|
|
|
|
$this->assertTrue( (bool)$res['id'], 'Block succeeded' );
|
|
|
|
|
|
|
|
|
|
$user = null; // clear
|
|
|
|
|
|
|
|
|
|
$block = Block::newFromID( $res['id'] );
|
2014-04-24 09:57:41 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
'UserOnForeignWiki',
|
|
|
|
|
$block->getTarget()->getName(),
|
|
|
|
|
'Correct blockee name'
|
|
|
|
|
);
|
2015-11-19 22:09:29 +00:00
|
|
|
$this->assertEquals( $userId, $block->getTarget()->getId(), 'Correct blockee id' );
|
2017-10-25 19:26:53 +00:00
|
|
|
$this->assertEquals( 'Meta>MetaWikiUser', $block->getBlocker()->getName(),
|
|
|
|
|
'Correct blocker name' );
|
|
|
|
|
$this->assertEquals( 'Meta>MetaWikiUser', $block->getByName(), 'Correct blocker name' );
|
2012-04-10 00:12:06 +00:00
|
|
|
$this->assertEquals( 0, $block->getBy(), 'Correct blocker id' );
|
|
|
|
|
}
|
2012-11-17 23:23:09 +00:00
|
|
|
|
2013-03-30 17:05:15 +00:00
|
|
|
protected function addXffBlocks() {
|
|
|
|
|
static $inited = false;
|
|
|
|
|
|
|
|
|
|
if ( $inited ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$inited = true;
|
2012-11-17 23:23:09 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$blockList = [
|
|
|
|
|
[ 'target' => '70.2.0.0/16',
|
2013-04-13 11:36:24 +00:00
|
|
|
'type' => Block::TYPE_RANGE,
|
2012-11-17 23:23:09 +00:00
|
|
|
'desc' => 'Range Hardblock',
|
|
|
|
|
'ACDisable' => false,
|
|
|
|
|
'isHardblock' => true,
|
|
|
|
|
'isAutoBlocking' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'target' => '2001:4860:4001::/48',
|
2013-04-13 11:36:24 +00:00
|
|
|
'type' => Block::TYPE_RANGE,
|
2012-11-17 23:23:09 +00:00
|
|
|
'desc' => 'Range6 Hardblock',
|
|
|
|
|
'ACDisable' => false,
|
|
|
|
|
'isHardblock' => true,
|
|
|
|
|
'isAutoBlocking' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'target' => '60.2.0.0/16',
|
2013-04-13 11:36:24 +00:00
|
|
|
'type' => Block::TYPE_RANGE,
|
2012-11-17 23:23:09 +00:00
|
|
|
'desc' => 'Range Softblock with AC Disabled',
|
|
|
|
|
'ACDisable' => true,
|
|
|
|
|
'isHardblock' => false,
|
|
|
|
|
'isAutoBlocking' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'target' => '50.2.0.0/16',
|
2013-04-13 11:36:24 +00:00
|
|
|
'type' => Block::TYPE_RANGE,
|
2012-11-17 23:23:09 +00:00
|
|
|
'desc' => 'Range Softblock',
|
|
|
|
|
'ACDisable' => false,
|
|
|
|
|
'isHardblock' => false,
|
|
|
|
|
'isAutoBlocking' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'target' => '50.1.1.1',
|
2013-04-13 11:36:24 +00:00
|
|
|
'type' => Block::TYPE_IP,
|
2012-11-17 23:23:09 +00:00
|
|
|
'desc' => 'Exact Softblock',
|
|
|
|
|
'ACDisable' => false,
|
|
|
|
|
'isHardblock' => false,
|
|
|
|
|
'isAutoBlocking' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2012-11-17 23:23:09 +00:00
|
|
|
|
2017-10-25 19:26:53 +00:00
|
|
|
$blocker = $this->getTestUser()->getUser();
|
2012-11-17 23:23:09 +00:00
|
|
|
foreach ( $blockList as $insBlock ) {
|
|
|
|
|
$target = $insBlock['target'];
|
|
|
|
|
|
|
|
|
|
if ( $insBlock['type'] === Block::TYPE_IP ) {
|
|
|
|
|
$target = User::newFromName( IP::sanitizeIP( $target ), false )->getName();
|
|
|
|
|
} elseif ( $insBlock['type'] === Block::TYPE_RANGE ) {
|
|
|
|
|
$target = IP::sanitizeRange( $target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$block = new Block();
|
|
|
|
|
$block->setTarget( $target );
|
2017-10-25 19:26:53 +00:00
|
|
|
$block->setBlocker( $blocker );
|
2012-11-17 23:23:09 +00:00
|
|
|
$block->mReason = $insBlock['desc'];
|
|
|
|
|
$block->mExpiry = 'infinity';
|
|
|
|
|
$block->prevents( 'createaccount', $insBlock['ACDisable'] );
|
|
|
|
|
$block->isHardblock( $insBlock['isHardblock'] );
|
|
|
|
|
$block->isAutoblocking( $insBlock['isAutoBlocking'] );
|
|
|
|
|
$block->insert();
|
|
|
|
|
}
|
2013-03-30 17:05:15 +00:00
|
|
|
}
|
2012-11-17 23:23:09 +00:00
|
|
|
|
2013-03-30 17:05:15 +00:00
|
|
|
public static function providerXff() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range Hardblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 50.2.1.1, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range Softblock with AC Disabled'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 70.2.1.1, 50.1.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Exact Softblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 50.1.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 3,
|
|
|
|
|
'result' => 'Exact Softblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 70.2.1.1, 50.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range Hardblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 70.2.1.1, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range Hardblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '50.2.1.1, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range Softblock with AC Disabled'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 50.1.1.1, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Exact Softblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, <$A_BUNCH-OF{INVALID}TEXT\>, 60.2.1.1, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 1,
|
|
|
|
|
'result' => 'Range Softblock with AC Disabled'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[ 'xff' => '1.2.3.4, 50.2.1.1, 2001:4860:4001:802::1003, 2.3.4.5',
|
2012-11-17 23:23:09 +00:00
|
|
|
'count' => 2,
|
|
|
|
|
'result' => 'Range6 Hardblock'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2013-03-30 17:05:15 +00:00
|
|
|
}
|
2012-11-17 23:23:09 +00:00
|
|
|
|
2013-03-30 17:05:15 +00:00
|
|
|
/**
|
|
|
|
|
* @dataProvider providerXff
|
2013-10-24 10:54:02 +00:00
|
|
|
* @covers Block::getBlocksForIPList
|
|
|
|
|
* @covers Block::chooseBlock
|
2013-03-30 17:05:15 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testBlocksOnXff( $xff, $exCount, $exResult ) {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$this->addBlockForUser( $user );
|
|
|
|
|
|
2013-03-30 17:05:15 +00:00
|
|
|
$list = array_map( 'trim', explode( ',', $xff ) );
|
|
|
|
|
$xffblocks = Block::getBlocksForIPList( $list, true );
|
|
|
|
|
$this->assertEquals( $exCount, count( $xffblocks ), 'Number of blocks for ' . $xff );
|
|
|
|
|
$block = Block::chooseBlock( $xffblocks, $list );
|
|
|
|
|
$this->assertEquals( $exResult, $block->mReason, 'Correct block type for XFF header ' . $xff );
|
2012-11-17 23:23:09 +00:00
|
|
|
}
|
2014-05-22 14:45:46 +00:00
|
|
|
|
|
|
|
|
public function testDeprecatedConstructor() {
|
|
|
|
|
$this->hideDeprecated( 'Block::__construct with multiple arguments' );
|
|
|
|
|
$username = 'UnthinkablySecretRandomUsername';
|
|
|
|
|
$reason = 'being irrational';
|
|
|
|
|
|
|
|
|
|
# Set up the target
|
|
|
|
|
$u = User::newFromName( $username );
|
2016-03-19 00:08:06 +00:00
|
|
|
if ( $u->getId() == 0 ) {
|
2014-05-22 14:45:46 +00:00
|
|
|
$u->addToDatabase();
|
2015-09-04 16:17:42 +00:00
|
|
|
TestUser::setPasswordForUser( $u, 'TotallyObvious' );
|
2014-05-22 14:45:46 +00:00
|
|
|
}
|
|
|
|
|
unset( $u );
|
|
|
|
|
|
|
|
|
|
# Make sure the user isn't blocked
|
|
|
|
|
$this->assertNull(
|
|
|
|
|
Block::newFromTarget( $username ),
|
|
|
|
|
"$username should not be blocked"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# Perform the block
|
|
|
|
|
$block = new Block(
|
|
|
|
|
/* address */ $username,
|
|
|
|
|
/* user */ 0,
|
|
|
|
|
/* by */ 0,
|
|
|
|
|
/* reason */ $reason,
|
|
|
|
|
/* timestamp */ 0,
|
|
|
|
|
/* auto */ false,
|
|
|
|
|
/* expiry */ 0
|
|
|
|
|
);
|
|
|
|
|
$block->insert();
|
|
|
|
|
|
|
|
|
|
# Check target
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$block->getTarget()->getName(),
|
|
|
|
|
$username,
|
|
|
|
|
"Target should be set properly"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# Check supplied parameter
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$block->mReason,
|
|
|
|
|
$reason,
|
|
|
|
|
"Reason should be non-default"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
# Check default parameter
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
(bool)$block->prevents( 'createaccount' ),
|
|
|
|
|
"Account creation should not be blocked by default"
|
|
|
|
|
);
|
|
|
|
|
}
|
2016-12-01 16:51:03 +00:00
|
|
|
|
|
|
|
|
public function testSystemBlocks() {
|
2017-10-25 17:22:32 +00:00
|
|
|
$user = $this->getUserForBlocking();
|
|
|
|
|
$this->addBlockForUser( $user );
|
|
|
|
|
|
2016-12-01 16:51:03 +00:00
|
|
|
$blockOptions = [
|
2017-10-25 17:22:32 +00:00
|
|
|
'address' => $user->getName(),
|
2016-12-01 16:51:03 +00:00
|
|
|
'reason' => 'test system block',
|
|
|
|
|
'timestamp' => wfTimestampNow(),
|
|
|
|
|
'expiry' => $this->db->getInfinity(),
|
2017-10-25 19:26:53 +00:00
|
|
|
'byText' => 'MediaWiki default',
|
2016-12-01 16:51:03 +00:00
|
|
|
'systemBlock' => 'test',
|
|
|
|
|
'enableAutoblock' => true,
|
|
|
|
|
];
|
|
|
|
|
$block = new Block( $blockOptions );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'test', $block->getSystemBlockType() );
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$block->insert();
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( MWException $ex ) {
|
|
|
|
|
$this->assertSame( 'Cannot insert a system block into the database', $ex->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$block->doAutoblock( '192.0.2.2' );
|
|
|
|
|
$this->fail( 'Expected exception not thrown' );
|
|
|
|
|
} catch ( MWException $ex ) {
|
|
|
|
|
$this->assertSame( 'Cannot autoblock from a system block', $ex->getMessage() );
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-25 17:22:32 +00:00
|
|
|
|
2010-12-28 19:12:27 +00:00
|
|
|
}
|