You are here

function CountriesFunctionsUnitTest::testCountriesLookupListing in Countries 8

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

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

File

tests/countries.test, line 301
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_clear_caches();
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $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) {
          $this
            ->fail('Lookup found a country by the existing property for ' . $country->iso2 . ' by property ' . $property . ' using the value ' . check_plain($value));
          $passed = FALSE;
        }
        elseif ($lookup->iso2 != $country->iso2) {
          $this
            ->fail('Lookup found a country by the existing property for ' . $country->iso2 . ' by property ' . $property . ' using the value ' . check_plain($value) . ' Found ' . check_plain($lookup->name) . ' (' . $lookup->iso2 . ') instead');
          $passed = FALSE;
        }

        // Guessing
        $lookup = countries_country_lookup($value);
        if (!$lookup) {
          $this
            ->fail('Lookup found a country by the existing property for ' . $country->iso2 . ' by guessing the property ' . $property . ' using the value ' . check_plain($value));
          $passed = FALSE;
        }
        elseif ($lookup->iso2 != $country->iso2) {
          $this
            ->fail('Lookup found a country by the existing property for ' . $country->iso2 . ' by guessing the property ' . $property . ' using the value ' . check_plain($value) . ' Found ' . check_plain($lookup->name) . ' (' . $lookup->iso2 . ') instead');
          $passed = FALSE;
        }
        if ($property == 'official_name') {
          $lookup = countries_country_lookup($value, 'name');
          if (!$lookup) {
            $this
              ->fail('Lookup found the ' . $country->iso2 . ' by official name using name property');
            $passed = FALSE;
          }
          elseif ($lookup->iso2 != $country->iso2) {
            $this
              ->fail('Lookup found the ' . $country->iso2 . ' by official name using name property ' . $property . ' using the value ' . check_plain($value) . ' Found ' . check_plain($lookup->name) . ' (' . $lookup->iso2 . ') instead');
            $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
}