Issue #2697993 by aerozeppelin, Novitsh, makbul_khan8: system_block_ip_action() adding empty IP record in blocked_ips table

8.2.x
Alex Pott 2016-07-29 01:01:41 +01:00
parent e94c581e09
commit c31a50baa5
2 changed files with 26 additions and 1 deletions

View File

@ -44,7 +44,8 @@ class BanIpManager implements BanIpManagerInterface {
* {@inheritdoc}
*/
public function banIp($ip) {
$this->connection->insert('ban_ip')
$this->connection->merge('ban_ip')
->key(array('ip' => $ip))
->fields(array('ip' => $ip))
->execute();
}

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\ban\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Core\Database\Database;
use Drupal\ban\BanIpManager;
/**
* Tests IP address banning.
@ -73,6 +75,28 @@ class IpAddressBlockingTest extends BrowserTestBase {
// $edit['ip'] = \Drupal::request()->getClientIP();
// $this->drupalPostForm('admin/config/people/ban', $edit, t('Save'));
// $this->assertText(t('You may not ban your own IP address.'));
// 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');
$query->fields('bip', array('iid'));
$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');
$query->fields('bip', array('iid'));
$query->condition('bip.ip', $ip);
$ip_count = $query->execute()->fetchAll();
$this->assertEqual(1, count($ip_count));
}
}