comparison core/modules/ban/tests/src/Functional/IpAddressBlockingTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children af1871eacc83
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\ban\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Core\Database\Database;
7 use Drupal\ban\BanIpManager;
8
9 /**
10 * Tests IP address banning.
11 *
12 * @group ban
13 */
14 class IpAddressBlockingTest extends BrowserTestBase {
15
16 /**
17 * Modules to install.
18 *
19 * @var array
20 */
21 public static $modules = ['ban'];
22
23 /**
24 * Tests various user input to confirm correct validation and saving of data.
25 */
26 public function testIPAddressValidation() {
27 // Create user.
28 $admin_user = $this->drupalCreateUser(['ban IP addresses']);
29 $this->drupalLogin($admin_user);
30 $this->drupalGet('admin/config/people/ban');
31
32 // Ban a valid IP address.
33 $edit = [];
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.');
39
40 // Try to block an IP address that's already blocked.
41 $edit = [];
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.'));
45
46 // Try to block a reserved IP address.
47 $edit = [];
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.'));
51
52 // Try to block a reserved IP address.
53 $edit = [];
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.'));
57
58 // Submit an empty form.
59 $edit = [];
60 $edit['ip'] = '';
61 $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
62 $this->assertText(t('Enter a valid IP address.'));
63
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.');
70
71 // Submit your own IP address. This fails, although it works when testing
72 // manually.
73 // TODO: On some systems this test fails due to a bug/inconsistency in cURL.
74 // $edit = array();
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.'));
78
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);
83 $ip = '1.0.0.0';
84 $banIp->banIp($ip);
85 $banIp->banIp($ip);
86 $banIp->banIp($ip);
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));
92 $ip = '';
93 $banIp->banIp($ip);
94 $banIp->banIp($ip);
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));
100 }
101
102 }