summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-05-10 07:32:02 +0000
committerDries Buytaert <dries@buytaert.net>2008-05-10 07:32:02 +0000
commit0a31af3ed2d12e40ec62b907bc24f33c11b0ab1e (patch)
treefbad80a1bc236515732ae7d55ca7638e504235cd /modules
parentccef07e59e9f5197452b2fdf54ce0818ea8a5ae1 (diff)
downloadbrdo-0a31af3ed2d12e40ec62b907bc24f33c11b0ab1e.tar.gz
brdo-0a31af3ed2d12e40ec62b907bc24f33c11b0ab1e.tar.bz2
- Patch #256152 by catch: added additional tests for the IP address blocking and added a small usability improvement.
Diffstat (limited to 'modules')
-rw-r--r--modules/statistics/statistics.test2
-rw-r--r--modules/system/system.admin.inc9
-rw-r--r--modules/system/system.test69
3 files changed, 76 insertions, 4 deletions
diff --git a/modules/statistics/statistics.test b/modules/statistics/statistics.test
index 955f30b99..5375d1f25 100644
--- a/modules/statistics/statistics.test
+++ b/modules/statistics/statistics.test
@@ -22,7 +22,7 @@ class StatisticsBlockVisitorsTestCase extends DrupalWebTestCase {
// Create user.
$this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics'));
- // Insert dummy access by anonymous user into accessi log.
+ // Insert dummy access by anonymous user into access log.
db_query("INSERT INTO {accesslog} (title, path, url, hostname, uid, sid, timer, timestamp) values('%s', '%s', '%s', '%s', %d, '%s', %d, %d)", 'test', 'node/1', 'http://example.com', '192.168.1.1', '0', '10', '10', time());
}
diff --git a/modules/system/system.admin.inc b/modules/system/system.admin.inc
index 7ebac6c37..87ef36dd9 100644
--- a/modules/system/system.admin.inc
+++ b/modules/system/system.admin.inc
@@ -1137,17 +1137,20 @@ function system_ip_blocking_form($form_state) {
}
function system_ip_blocking_form_validate($form, &$form_state) {
- $ip = $form_state['values']['ip'];
+ $ip = trim($form_state['values']['ip']);
if (db_result(db_query("SELECT * FROM {blocked_ips} WHERE ip = '%s'", $ip))) {
form_set_error('ip', t('This IP address is already blocked.'));
}
+ else if ($ip == ip_address()) {
+ form_set_error('ip', t('You may not block your own IP address.'));
+ }
else if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE) == FALSE) {
form_set_error('ip', t('Please enter a valid IP address.'));
}
}
function system_ip_blocking_form_submit($form, &$form_state) {
- $ip = $form_state['values']['ip'];
+ $ip = trim($form_state['values']['ip']);
db_query("INSERT INTO {blocked_ips} (ip) VALUES ('%s')", $ip);
drupal_set_message(t('The IP address %ip has been blocked.', array('%ip' => $ip)));
$form_state['redirect'] = 'admin/settings/ip-blocking';
@@ -2260,4 +2263,4 @@ function theme_system_themes_form($form) {
$output = theme('table', $header, $rows);
$output .= drupal_render($form);
return $output;
-}
+} \ No newline at end of file
diff --git a/modules/system/system.test b/modules/system/system.test
index 657c4b95e..a8f947740 100644
--- a/modules/system/system.test
+++ b/modules/system/system.test
@@ -126,3 +126,72 @@ class EnableDisableCoreTestCase extends DrupalWebTestCase {
}
}
}
+
+class IPAddressBlocking extends DrupalWebTestCase {
+ /**
+ * Implementation of getInfo().
+ */
+ function getInfo() {
+ return array(
+ 'name' => t('IP address blocking'),
+ 'description' => t('Tests IP address blocking.'),
+ 'group' => t('System')
+ );
+ }
+
+ /**
+ * Implementation of setUp().
+ */
+ function setUp() {
+ parent::setUp();
+
+ // Create user.
+ $this->blocking_user = $this->drupalCreateUser(array('block IP addresses', 'access statistics'));
+ }
+
+ /**
+ * Tests a variety of user input to confirm correct validation and saving of data.
+ */
+ function testIPAddressValidation() {
+ $this->drupalLogin($this->blocking_user);
+ $this->drupalGet('admin/settings/ip-blocking');
+
+ // Block a valid IP address.
+ $edit = array();
+ $edit['ip'] = '192.168.1.1';
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $ip = db_result(db_query("SELECT iid from {blocked_ips} WHERE ip = '%s'", $edit['ip']));
+ $this->assertNotNull($ip, t('IP address found in database'));
+ $this->assertRaw(t('The IP address %ip has been blocked.', array('%ip' => $edit['ip'])), t('IP address was blocked.'));
+
+ // Try to block an IP address that's already blocked.
+ $edit = array();
+ $edit['ip'] = '192.168.1.1';
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $this->assertText(t('This IP address is already blocked.'));
+
+ // Try to block a reserved IP address.
+ $edit = array();
+ $edit['ip'] = '255.255.255.255';
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $this->assertText(t('Please enter a valid IP address.'));
+
+ // Try to block a reserved IP address.
+ $edit = array();
+ $edit['ip'] = 'test.example.com';
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $this->assertText(t('Please enter a valid IP address.'));
+
+ // Submit an empty form.
+ $edit = array();
+ $edit['ip'] = '';
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $this->assertText(t('Please enter a valid IP address.'));
+
+ // Submit your own IP address. This fails, although it works when testing manually.
+ $edit = array();
+ $edit['ip'] = ip_address();
+ $this->drupalPost('admin/settings/ip-blocking', $edit, t('Save'));
+ $this->assertText(t('You may not block your own IP address.'));
+ }
+}