IpAddressBlockingTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Drupal\Tests\ban\Functional;
  3. use Drupal\Tests\BrowserTestBase;
  4. use Drupal\Core\Database\Database;
  5. use Drupal\ban\BanIpManager;
  6. /**
  7. * Tests IP address banning.
  8. *
  9. * @group ban
  10. */
  11. class IpAddressBlockingTest extends BrowserTestBase {
  12. /**
  13. * Modules to install.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['ban'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected $defaultTheme = 'stark';
  22. /**
  23. * Tests various user input to confirm correct validation and saving of data.
  24. */
  25. public function testIPAddressValidation() {
  26. // Create user.
  27. $admin_user = $this->drupalCreateUser(['ban IP addresses']);
  28. $this->drupalLogin($admin_user);
  29. $this->drupalGet('admin/config/people/ban');
  30. $connection = Database::getConnection();
  31. // Ban a valid IP address.
  32. $edit = [];
  33. $edit['ip'] = '1.2.3.3';
  34. $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
  35. $ip = $connection->select('ban_ip', 'bi')->fields('bi', ['iid'])->condition('ip', $edit['ip'])->execute()->fetchField();
  36. $this->assertNotEmpty($ip, 'IP address found in database.');
  37. $this->assertRaw(t('The IP address %ip has been banned.', ['%ip' => $edit['ip']]), 'IP address was banned.');
  38. // Try to block an IP address that's already blocked.
  39. $edit = [];
  40. $edit['ip'] = '1.2.3.3';
  41. $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
  42. $this->assertText(t('This IP address is already banned.'));
  43. // Try to block a reserved IP address.
  44. $edit = [];
  45. $edit['ip'] = '255.255.255.255';
  46. $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
  47. $this->assertText(t('Enter a valid IP address.'));
  48. // Try to block a reserved IP address.
  49. $edit = [];
  50. $edit['ip'] = 'test.example.com';
  51. $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
  52. $this->assertText(t('Enter a valid IP address.'));
  53. // Submit an empty form.
  54. $edit = [];
  55. $edit['ip'] = '';
  56. $this->drupalPostForm('admin/config/people/ban', $edit, t('Add'));
  57. $this->assertText(t('Enter a valid IP address.'));
  58. // Pass an IP address as a URL parameter and submit it.
  59. $submit_ip = '1.2.3.4';
  60. $this->drupalPostForm('admin/config/people/ban/' . $submit_ip, [], t('Add'));
  61. $ip = $connection->select('ban_ip', 'bi')->fields('bi', ['iid'])->condition('ip', $submit_ip)->execute()->fetchField();
  62. $this->assertNotEmpty($ip, 'IP address found in database');
  63. $this->assertRaw(t('The IP address %ip has been banned.', ['%ip' => $submit_ip]), 'IP address was banned.');
  64. // Submit your own IP address. This fails, although it works when testing
  65. // manually.
  66. // TODO: On some systems this test fails due to a bug/inconsistency in cURL.
  67. // $edit = array();
  68. // $edit['ip'] = \Drupal::request()->getClientIP();
  69. // $this->drupalPostForm('admin/config/people/ban', $edit, t('Save'));
  70. // $this->assertText(t('You may not ban your own IP address.'));
  71. // Test duplicate ip address are not present in the 'blocked_ips' table.
  72. // when they are entered programmatically.
  73. $banIp = new BanIpManager($connection);
  74. $ip = '1.0.0.0';
  75. $banIp->banIp($ip);
  76. $banIp->banIp($ip);
  77. $banIp->banIp($ip);
  78. $query = $connection->select('ban_ip', 'bip');
  79. $query->fields('bip', ['iid']);
  80. $query->condition('bip.ip', $ip);
  81. $ip_count = $query->execute()->fetchAll();
  82. $this->assertCount(1, $ip_count);
  83. $ip = '';
  84. $banIp->banIp($ip);
  85. $banIp->banIp($ip);
  86. $query = $connection->select('ban_ip', 'bip');
  87. $query->fields('bip', ['iid']);
  88. $query->condition('bip.ip', $ip);
  89. $ip_count = $query->execute()->fetchAll();
  90. $this->assertCount(1, $ip_count);
  91. }
  92. }