You are here

function ip2countryTestCase::testIPLookup in IP-based Determination of a Visitor's Country 6

Same name and namespace in other branches
  1. 7 tests/ip2country.test \ip2countryTestCase::testIpLookup()

Tests IP lookup for addresses in / not in the database.

File

./ip2country.test, line 112
Tests suite for the ip2country module.

Class

ip2countryTestCase
Need 1 class for unit tests, 1 class for functional tests 1 function for DB tests because filling takes so long

Code

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.'));
}