class BanIpTest
Same name and namespace in other branches
- 11.x core/modules/ban/tests/src/Kernel/BanIpTest.php \Drupal\Tests\ban\Kernel\BanIpTest
@group ban
Hierarchy
- class \Drupal\Tests\ban\Kernel\BanIpTest
Expanded class hierarchy of BanIpTest
File
-
core/
modules/ ban/ tests/ src/ Kernel/ BanIpTest.php, line 14
Namespace
Drupal\Tests\ban\KernelView source
class BanIpTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'ban',
];
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->installSchema('ban', [
'ban_ip',
]);
}
/**
* Test banning IPs.
*/
public function testBanIp() : void {
$banIp = $this->container
->get(BanIpManagerInterface::class);
// Test valid IP addresses.
$ip = '1.2.3.3';
$this->assertCount(0, $this->getIpBans($ip));
$banIp->banIp($ip);
$this->assertCount(1, $this->getIpBans($ip));
// Test duplicate ip address are not present in the 'blocked_ips' table
// when they are entered programmatically.
$ip = '1.0.0.0';
$banIp->banIp($ip);
$banIp->banIp($ip);
$banIp->banIp($ip);
$this->assertCount(1, $this->getIpBans($ip));
$ip = '';
$banIp->banIp($ip);
$banIp->banIp($ip);
$this->assertCount(1, $this->getIpBans($ip));
}
/**
* Gets the IP bans.
*/
protected function getIpBans(string $ip) : array {
$connection = $this->container
->get(Connection::class);
$query = $connection->select('ban_ip', 'bip');
$query->fields('bip', [
'iid',
]);
$query->condition('bip.ip', $ip);
return $query->execute()
->fetchAll();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
BanIpTest::$modules | protected static | property | Modules to install. |
BanIpTest::getIpBans | protected | function | Gets the IP bans. |
BanIpTest::setUp | protected | function | |
BanIpTest::testBanIp | public | function | Test banning IPs. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.