You are here

function CountriesFunctionsUnitTest::testCountriesLookupListing in Countries 7.2

Same name and namespace in other branches
  1. 8 tests/countries.test \CountriesFunctionsUnitTest::testCountriesLookupListing()

This browes the admin listing, making sure that the countries are correctly listed.

File

tests/countries.test, line 431
Tests for countries.module.

Class

CountriesFunctionsUnitTest
Test the general functions provided by the module.

Code

function testCountriesLookupListing() {

  // Complete list of checks using built in countries.
  $countries = countries_get_countries();
  $passed_all = TRUE;
  foreach ($countries as $country) {
    $passed = TRUE;
    foreach (array(
      'name',
      'official_name',
      'iso2',
      'iso3',
      'numcode',
    ) as $property) {
      $value = $country->{$property};
      if (!empty($value)) {

        // Fixed property
        $lookup = countries_country_lookup($value, $property);
        if (!$lookup || $lookup->iso2 != $country->iso2) {
          $this
            ->fail('Lookup found the ' . $country->iso2 . ' by property ' . $property);
          $passed = FALSE;
        }

        // Guessing
        $lookup = countries_country_lookup($value);
        if (!$lookup || $lookup->iso2 != $country->iso2) {
          $this
            ->fail('Lookup found the ' . $country->iso2 . ' by guessing the property ' . $property);
          $passed = FALSE;
        }
        if ($property == 'official_name') {
          $lookup = countries_country_lookup($value, 'name');
          if (!$lookup || $lookup->iso2 != $country->iso2) {
            $this
              ->fail('Lookup found the ' . $country->iso2 . ' by official name using name property');
            $passed = FALSE;
          }
        }
      }
    }
    if (!$passed) {
      $this
        ->fail('One of more property lookups for ' . $country->name . ' (' . $country->iso2 . ') failed');
      $passed_all = FALSE;
    }
  }
  if ($passed_all) {
    $this
      ->pass('All property lookups for all countries, both by property and guessing passed');
  }

  // Test some invalid properties using garbage values.
  foreach (array(
    'name',
    'official_name',
    'iso2',
    'iso3',
    'numcode',
    'invalid',
  ) as $property) {
    foreach (array(
      'abcde',
      'xc',
      'XC',
      0,
      FALSE,
      TRUE,
      NULL,
      -1,
      1,
    ) as $invalid) {
      try {
        $lookup = countries_country_lookup($invalid, $property);
        $this
          ->assertFalse($lookup, 'Lookup not found by property ' . $property);
      } catch (Exception $e) {
        if ($property == 'invalid') {
          $this
            ->pass('Invalid property correctly triggered an exception during countries_country_lookup()');
        }
        else {
          $this
            ->fail('Valid property incorrectly triggered an exception during countries_country_lookup()');
        }
      }
      $lookup = countries_country_lookup($invalid);
      $this
        ->assertFalse($lookup, 'Lookup not found by guessing');
    }
  }

  // TODO Imported countries, disabled countries
}