blocked_ips_expire.base.test in Blocked IPs Expire 7
Contains \BlockedIpsExpireTestCase.
File
tests/blocked_ips_expire.base.testView source
<?php
/**
* @file
* Contains \BlockedIpsExpireTestCase.
*/
/**
* An object containing test functions common to all tests for this module.
*/
class BlockedIpsExpireTestCase extends DrupalWebTestCase {
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp('blocked_ips_expire');
}
/**
* Returns TRUE if an IP address is in the database.
*
* @param string $ip
* The IP address to check.
*
* @return bool
* TRUE if the IP address to check is in the database, otherwise FALSE.
*/
public static function isIpInDatabase($ip) {
$result = db_select('blocked_ips')
->condition('ip', $ip)
->countQuery()
->execute()
->fetchField();
// If the number of results is greater than 0, then we can safely assume
// that the IP address is in the database.
return $result > 0;
}
/**
* Returns TRUE if an IP address is in the database.
*
* @param string $ip
* The IP address to check.
*
* @return int
* The number of times the IP address is in the database.
*/
public static function numberTimesIpInDatabase($ip) {
$result = db_select('blocked_ips')
->condition('ip', $ip)
->countQuery()
->execute()
->fetchField();
return (int) $result;
}
/**
* Return the expiry time of an IP address in the database.
*
* @param string $ip
* The IP address to check.
*
* @return int
* The expiry time, in UNIX time.
*/
public static function getExpiryTime($ip) {
$query = db_select('blocked_ips', 'bi');
$query
->join('blocked_ips_expire', 'bie', 'bi.iid = bie.iid');
$query
->addField('bie', 'expiry_date');
$query
->condition('bi.ip', $ip, '=');
$result = $query
->execute()
->fetchField();
return $result;
}
/**
* Add an IP address without an expiry (simulate core without this module).
*
* @see system_ip_blocking_form_submit()
*/
public static function addIpWithoutExpiryTime($ip) {
$result = db_insert('blocked_ips')
->fields(array(
'ip' => $ip,
))
->execute();
return $result;
}
/**
* Generate a random IP address.
*
* Currently this only generates IPv4 addresses, i.e.: a string in the form
* 'a.b.c.d', where a, b, c, and d are integers
* between 0 and 255.
*
* @return string
* A string containing an IP address.
*/
public static function generateIpAddress() {
$components = array();
for ($i = 0; $i < 4; $i++) {
$components[] = (string) rand(0, 255);
}
return implode('.', $components);
}
/**
* Generates a random date.
*
* Essentially generates a random, positive integer and interprets it as a
* number of seconds from the start of the UNIX epoch (i.e.: 1970-01-01
* 00:00:00 UTC).
*
* This function checks $min and $max: if you try to pass $min < 0, the
* function will use 0 as the minimum; likewise, if you pass $max >
* getrandmax(), the function will use getrandmax() as the maximum.
*
* @param int $min
* The smallest datetime we should generate (in UNIX time). If you pass a
* number less than 0, it will be automatically corrected to 0. If you run
* this function on a system where getrandmax() will return a number
* sufficiently high that it will be after the current UNIX timestamp, then
* you can set this argument to the value of time() and $max = NULL (or $max
* = getrandmax()) to generate a date in the future.
* @param int|null $max
* The largest datetime we should generate, or NULL to use the value of
* getrandmax(). If you pass a number greater than getrandmax(), it will
* automatically be corrected to 0. Set this to the current value of time()
* and $min = 0 to generate a date in the past.
*
* @return \DateTime
* A datetime object representing this random date.
*/
public static function randomDatetime($min = 0, $max = NULL) {
// Don't generate a date before the start of the UNIX epoch.
$min = (int) $min;
$min = $min < 0 ? 0 : $min;
// We can't generate a random integer bigger than getrandmax().
$rand_max = getrandmax();
if (is_null($max) || $max >= $rand_max) {
$max = $rand_max;
}
else {
$max = (int) $max;
}
$random_seconds_since_epoch = rand($min, $max);
return $random_seconds_since_epoch;
}
/**
* Generates a random date in the future.
*
* Note this function makes the assumption that getrandmax() will return a
* number sufficiently high that it will be after the current UNIX timestamp.
*
* @return \DateTime
* A datetime object representing this random date.
*/
public static function randomDatetimeInFuture() {
$current_datetime = time();
$rand_max = getrandmax();
return self::randomDatetime($current_datetime, $rand_max);
}
/**
* Generates a random date in the past.
*
* @return \DateTime
* A datetime object representing this random date.
*/
public static function randomDatetimeInPast() {
$current_datetime = time();
return self::randomDatetime(0, $current_datetime);
}
}
Classes
Name | Description |
---|---|
BlockedIpsExpireTestCase | An object containing test functions common to all tests for this module. |