public function BlockedIpsExpireHelpers::testAddIp in Blocked IPs Expire 7
Tests that the helper function to add an IP with expiry date works.
@covers _blocked_ips_expire_add_ip()
File
- tests/
blocked_ips_expire.helpers.test, line 170 - Contains \BlockedIpsExpireHelpers.
Class
- BlockedIpsExpireHelpers
- Tests that helper functions are operating correctly.
Code
public function testAddIp() {
// Some variables we will use in this test case.
$ip_with_spaces = ' ' . BlockedIpsExpireHelpers::generateIpAddress() . ' ';
$second_time = BlockedIpsExpireHelpers::randomDatetimeInFuture();
// Test that the test IP address is not in the database to start with.
$num_times_in_db = BlockedIpsExpireHelpers::numberTimesIpInDatabase($this->anyOldIp);
$this
->assertFalse($num_times_in_db > 0, 'Test IP not in database before adding.');
// Block the IP address using the first expiry time.
$iid = _blocked_ips_expire_add_ip($this->anyOldIp, $this->anyOldDate);
// Test that the IP address is now there and the expiry time matches.
$num_times_in_db = BlockedIpsExpireHelpers::numberTimesIpInDatabase($this->anyOldIp);
$this
->assertTrue($num_times_in_db > 0, 'IP added to database.');
$ip_info = _blocked_ips_expire_get_one((int) $iid);
$this
->assertEqual($ip_info->expiry_date, $this->anyOldDate, "IP's expiry time matches what was set.");
// Block the address a second time.
_blocked_ips_expire_add_ip($this->anyOldIp, $second_time);
// Test the IP address is only in there once, and has the second expiry
// time.
$num_times_in_db = BlockedIpsExpireHelpers::numberTimesIpInDatabase($this->anyOldIp);
$this
->assertEqual($num_times_in_db, 1, 'IP only in database once.');
$ip_info = _blocked_ips_expire_get_one((int) $iid);
$this
->assertEqual($ip_info->expiry_date, $second_time, "IP's expiry time matches what was set the second time.");
// Try to add an IP with extra spaces around it.
$spaces_iid = _blocked_ips_expire_add_ip($ip_with_spaces, $second_time);
// Verify that it's stored without the spaces.
$ip_with_spaces_info = _blocked_ips_expire_get_one((int) $spaces_iid);
$trimmed_ip = trim($ip_with_spaces);
$this
->assertEqual($ip_with_spaces_info->ip, $trimmed_ip, 'IPs are not stored with leading or trailing spaces.');
}