View source
<?php
namespace Drupal\Tests\advban\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class IpAddressBlockingTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'advban',
];
public function testIpAddressValidation() {
$admin_user = $this
->drupalCreateUser([
'advanced ban IP addresses',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/people/advban');
$ipManagerService = $this->container
->get('advban.ip_manager');
$connection = $this->container
->get('database');
$edit = [];
$edit['ip'] = '1.2.3.3';
$post = $this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->verbose($post);
$ip = $connection
->query("SELECT iid from {advban_ip} WHERE ip = :ip", [
':ip' => $edit['ip'],
])
->fetchField();
$this
->assertNotEmpty($ip, 'IP address found in database.');
$ban_result = $ipManagerService
->isBanned($edit['ip'], [
'expiry_check' => TRUE,
'info_output' => TRUE,
]);
$this
->assertNotEmpty($ban_result['is_banned'], 'IP address found in database.');
$edit = [];
$edit['ip'] = '1.2.4.1';
$edit['ip_end'] = '1.2.4.9';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$ip_test = '1.2.4.3';
$ip_long = ip2long($ip_test);
$ip = $connection
->query("SELECT iid FROM {advban_ip} WHERE ip_end <> '' AND ip <= :ip AND ip_end >= :ip LIMIT 1", [
':ip' => $ip_long,
])
->fetchField();
$this
->assertNotEmpty($ip, 'IP address found in database.');
$ban_result = $ipManagerService
->isBanned($ip_test, [
'expiry_check' => TRUE,
'info_output' => TRUE,
]);
$this
->assertNotEmpty($ban_result['is_banned'], 'IP address is banned.');
$edit = [];
$edit['ip'] = '1.1.1.1';
$edit['ip_end'] = '2.2.2.2';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$ip_test = '127.0.0.1';
$ip_long = ip2long($ip_test);
$ip = $connection
->query("SELECT iid FROM {advban_ip} WHERE ip_end <> '' AND ip <= {$ip_long} AND ip_end >= {$ip_long} LIMIT 1")
->fetchField();
$this
->assertEmpty($ip, 'IP address not found in database.');
$ban_result = $ipManagerService
->isBanned($ip_test, [
'expiry_check' => TRUE,
'info_output' => TRUE,
]);
$this
->assertEmpty($ban_result['is_banned'], 'IP address is not banned.');
$edit = [];
$edit['ip'] = '1.2.3.3';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->assertText($this
->t('This IP address is already banned.'));
$edit = [];
$edit['ip'] = '1.2.4.3';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->assertText($this
->t('This IP address is already banned.'));
$edit = [];
$edit['ip'] = '255.255.255.255';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->assertText($this
->t('Enter a valid IP address.'));
$edit = [];
$edit['ip'] = 'test.example.com';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->assertText($this
->t('Enter a valid IP address.'));
$edit = [];
$edit['ip'] = '';
$edit['ip_end'] = '';
$this
->drupalGet('admin/config/people/advban');
$this
->submitForm($edit, $this
->t('Add'));
$this
->assertText($this
->t('Enter a valid IP address.'));
$ip = '1.0.0.0';
$ipManagerService
->banIp($ip);
$ipManagerService
->banIp($ip);
$ipManagerService
->banIp($ip);
$query = $connection
->select('advban_ip', 'bip');
$query
->fields('bip', [
'iid',
]);
$query
->condition('bip.ip', $ip);
$ip_count = $query
->execute()
->fetchAll();
$this
->assertEquals(1, count($ip_count));
$ip = '';
$ipManagerService
->banIp($ip);
$ipManagerService
->banIp($ip);
$query = $connection
->select('advban_ip', 'bip');
$query
->fields('bip', [
'iid',
]);
$query
->condition('bip.ip', $ip);
$ip_count = $query
->execute()
->fetchAll();
$this
->assertEquals(1, count($ip_count));
}
}