View source
<?php
namespace Drupal\Tests\ban\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Database\Database;
use Drupal\ban\BanIpManager;
class IpAddressBlockingTest extends BrowserTestBase {
public static $modules = [
'ban',
];
protected $defaultTheme = 'stark';
public function testIPAddressValidation() {
$admin_user = $this
->drupalCreateUser([
'ban IP addresses',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/ban');
$connection = Database::getConnection();
$edit = [];
$edit['ip'] = '1.2.3.3';
$this
->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
$ip = $connection
->select('ban_ip', 'bi')
->fields('bi', [
'iid',
])
->condition('ip', $edit['ip'])
->execute()
->fetchField();
$this
->assertNotEmpty($ip, 'IP address found in database.');
$this
->assertRaw(t('The IP address %ip has been banned.', [
'%ip' => $edit['ip'],
]), 'IP address was banned.');
$edit = [];
$edit['ip'] = '1.2.3.3';
$this
->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
$this
->assertText(t('This IP address is already banned.'));
$edit = [];
$edit['ip'] = '255.255.255.255';
$this
->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
$this
->assertText(t('Enter a valid IP address.'));
$edit = [];
$edit['ip'] = 'test.example.com';
$this
->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
$this
->assertText(t('Enter a valid IP address.'));
$edit = [];
$edit['ip'] = '';
$this
->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
$this
->assertText(t('Enter a valid IP address.'));
$submit_ip = '1.2.3.4';
$this
->drupalPostForm('admin/config/people/ban/' . $submit_ip, [], t('Add'));
$ip = $connection
->select('ban_ip', 'bi')
->fields('bi', [
'iid',
])
->condition('ip', $submit_ip)
->execute()
->fetchField();
$this
->assertNotEmpty($ip, 'IP address found in database');
$this
->assertRaw(t('The IP address %ip has been banned.', [
'%ip' => $submit_ip,
]), 'IP address was banned.');
$banIp = new BanIpManager($connection);
$ip = '1.0.0.0';
$banIp
->banIp($ip);
$banIp
->banIp($ip);
$banIp
->banIp($ip);
$query = $connection
->select('ban_ip', 'bip');
$query
->fields('bip', [
'iid',
]);
$query
->condition('bip.ip', $ip);
$ip_count = $query
->execute()
->fetchAll();
$this
->assertCount(1, $ip_count);
$ip = '';
$banIp
->banIp($ip);
$banIp
->banIp($ip);
$query = $connection
->select('ban_ip', 'bip');
$query
->fields('bip', [
'iid',
]);
$query
->condition('bip.ip', $ip);
$ip_count = $query
->execute()
->fetchAll();
$this
->assertCount(1, $ip_count);
}
}