IpAddressBlockingTest.php 3.51 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\ban\Functional;
4

5
use Drupal\Tests\BrowserTestBase;
6 7
use Drupal\Core\Database\Database;
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 20
   *
   * @var array
   */
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 30 31 32
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/config/people/ban');

    // 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 45 46
    $this->assertText(t('This IP address is already banned.'));

    // 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 51 52
    $this->assertText(t('Enter a valid IP address.'));

    // 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 57 58
    $this->assertText(t('Enter a valid IP address.'));

    // Submit an empty form.
59
    $edit = [];
60
    $edit['ip'] = '';
61
    $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
62 63 64 65
    $this->assertText(t('Enter a valid IP address.'));

    // Pass an IP address as a URL parameter and submit it.
    $submit_ip = '1.2.3.4';
66 67
    $this->drupalPostForm('admin/config/people/ban/' . $submit_ip, [], t('Add'));
    $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 72

    // Submit your own IP address. This fails, although it works when testing
    // 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 80 81 82 83 84 85 86 87

    // Test duplicate ip address are not present in the 'blocked_ips' table.
    // when they are entered programmatically.
    $connection = Database::getConnection();
    $banIp = new BanIpManager($connection);
    $ip = '1.0.0.0';
    $banIp->banIp($ip);
    $banIp->banIp($ip);
    $banIp->banIp($ip);
    $query = db_select('ban_ip', 'bip');
88
    $query->fields('bip', ['iid']);
89 90 91 92 93 94 95
    $query->condition('bip.ip', $ip);
    $ip_count = $query->execute()->fetchAll();
    $this->assertEqual(1, count($ip_count));
    $ip = '';
    $banIp->banIp($ip);
    $banIp->banIp($ip);
    $query = db_select('ban_ip', 'bip');
96
    $query->fields('bip', ['iid']);
97 98 99
    $query->condition('bip.ip', $ip);
    $ip_count = $query->execute()->fetchAll();
    $this->assertEqual(1, count($ip_count));
100
  }
101

102
}