public function ip2countryTestCase::testIpLookup in IP-based Determination of a Visitor's Country 7
Same name and namespace in other branches
- 6 ip2country.test \ip2countryTestCase::testIPLookup()
Tests IP lookup for addresses in / not in the database.
File
- tests/
ip2country.test, line 89 - Tests suite for the ip2country module.
Class
- ip2countryTestCase
- Tests operations of the IP to Country module.
Code
public function testIpLookup() {
ip2country_update_database('arin');
$this
->assertTrue(($count = ip2country_get_count()) != 0, t('Database has been updated with @rows rows.', array(
'@rows' => $count,
)));
// Real working IPs.
$ip_array = array(
'125.29.33.201',
'212.58.224.138',
'184.51.240.110',
'210.87.9.66',
'93.184.216.119',
);
foreach ($ip_array as $ip_address) {
// Test dotted quad string form of address.
$country = ip2country_get_country($ip_address);
$this
->assertTrue($country, t('@ip found, resolved to @country.', array(
'@ip' => $ip_address,
'@country' => $country,
)));
// Test 32-bit unsigned long form of address.
$usl_country = ip2country_get_country(ip2long($ip_address));
$this
->assertTrue($usl_country == $country, t('Unsigned long lookup found same country code.'));
$this
->pass(t('Valid IP found in database.'));
}
// Invalid and reserved IPs.
$ip_array = array(
'127.0.0.1',
'358.1.1.0',
);
foreach ($ip_array as $ip_address) {
$country = ip2country_get_country($ip_address);
$this
->assertFalse($country, t('@ip not found in database.', array(
'@ip' => $ip_address,
)));
$this
->pass(t('Invalid IP not found in database.'));
}
ip2country_empty_database();
$this
->assertTrue(ip2country_get_count() == 0, t('Database is Empty.'));
}