3 namespace Drupal\Tests\ban\Functional;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Core\Database\Database;
7 use Drupal\ban\BanIpManager;
10 * Tests IP address banning.
14 class IpAddressBlockingTest extends BrowserTestBase {
21 public static $modules = ['ban'];
24 * Tests various user input to confirm correct validation and saving of data.
26 public function testIPAddressValidation() {
28 $admin_user = $this->drupalCreateUser(['ban IP addresses']);
29 $this->drupalLogin($admin_user);
30 $this->drupalGet('admin/config/people/ban');
32 // Ban a valid IP address.
34 $edit['ip'] = '1.2.3.3';
35 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
36 $ip = db_query("SELECT iid from {ban_ip} WHERE ip = :ip", [':ip' => $edit['ip']])->fetchField();
37 $this->assertTrue($ip, 'IP address found in database.');
38 $this->assertRaw(t('The IP address %ip has been banned.', ['%ip' => $edit['ip']]), 'IP address was banned.');
40 // Try to block an IP address that's already blocked.
42 $edit['ip'] = '1.2.3.3';
43 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
44 $this->assertText(t('This IP address is already banned.'));
46 // Try to block a reserved IP address.
48 $edit['ip'] = '255.255.255.255';
49 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
50 $this->assertText(t('Enter a valid IP address.'));
52 // Try to block a reserved IP address.
54 $edit['ip'] = 'test.example.com';
55 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
56 $this->assertText(t('Enter a valid IP address.'));
58 // Submit an empty form.
61 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
62 $this->assertText(t('Enter a valid IP address.'));
64 // Pass an IP address as a URL parameter and submit it.
65 $submit_ip = '1.2.3.4';
66 $this->drupalPostForm('admin/config/people/ban/' . $submit_ip, [], t('Add'));
67 $ip = db_query("SELECT iid from {ban_ip} WHERE ip = :ip", [':ip' => $submit_ip])->fetchField();
68 $this->assertTrue($ip, 'IP address found in database');
69 $this->assertRaw(t('The IP address %ip has been banned.', ['%ip' => $submit_ip]), 'IP address was banned.');
71 // Submit your own IP address. This fails, although it works when testing
73 // TODO: On some systems this test fails due to a bug/inconsistency in cURL.
75 // $edit['ip'] = \Drupal::request()->getClientIP();
76 // $this->drupalPostForm('admin/config/people/ban', $edit, t('Save'));
77 // $this->assertText(t('You may not ban your own IP address.'));
79 // Test duplicate ip address are not present in the 'blocked_ips' table.
80 // when they are entered programmatically.
81 $connection = Database::getConnection();
82 $banIp = new BanIpManager($connection);
87 $query = db_select('ban_ip', 'bip');
88 $query->fields('bip', ['iid']);
89 $query->condition('bip.ip', $ip);
90 $ip_count = $query->execute()->fetchAll();
91 $this->assertEqual(1, count($ip_count));
95 $query = db_select('ban_ip', 'bip');
96 $query->fields('bip', ['iid']);
97 $query->condition('bip.ip', $ip);
98 $ip_count = $query->execute()->fetchAll();
99 $this->assertEqual(1, count($ip_count));