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 {
protected 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
->drupalGet('admin/config/people/ban');
$this
->submitForm($edit, '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
->assertSession()
->pageTextContains('The IP address 1.2.3.3 has been banned.');
$edit = [];
$edit['ip'] = '1.2.3.3';
$this
->drupalGet('admin/config/people/ban');
$this
->submitForm($edit, 'Add');
$this
->assertSession()
->pageTextContains('This IP address is already banned.');
$edit = [];
$edit['ip'] = '255.255.255.255';
$this
->drupalGet('admin/config/people/ban');
$this
->submitForm($edit, 'Add');
$this
->assertSession()
->pageTextContains('Enter a valid IP address.');
$edit = [];
$edit['ip'] = 'test.example.com';
$this
->drupalGet('admin/config/people/ban');
$this
->submitForm($edit, 'Add');
$this
->assertSession()
->pageTextContains('Enter a valid IP address.');
$edit = [];
$edit['ip'] = '';
$this
->drupalGet('admin/config/people/ban');
$this
->submitForm($edit, 'Add');
$this
->assertSession()
->pageTextContains('Enter a valid IP address.');
$submit_ip = '1.2.3.4';
$this
->drupalGet('admin/config/people/ban/' . $submit_ip);
$this
->submitForm([], '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
->assertSession()
->pageTextContains("The IP address {$submit_ip} has been 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);
}
}