You are here

class CountriesFunctionsUnitTest in Countries 8

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

Test the general functions provided by the module.

Hierarchy

Expanded class hierarchy of CountriesFunctionsUnitTest

File

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

View source
class CountriesFunctionsUnitTest extends CountriesBaseSetupTest {
  public static function getInfo() {
    return array(
      'name' => 'Country core',
      'description' => 'Test the country lookup functionality, querries and other core functions.',
      'group' => 'Countries',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalLogin($this->admin_user);
  }

  /**
   * This browes the admin listing, making sure that the countries are
   * correctly listed.
   */
  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
  }

  /**
   * This test that core countries can not be deleted while user ones are.
   */
  function testCountriesLock() {

    // Already have tested the lock above on the listing page.
    $af = country_load('af');
    $this
      ->assertTrue(country_is_locked($af), 'Country is detected as locked');
    $this
      ->drupalGet('admin/config/regional/countries/af/delete');
    $this
      ->assertText(t('Core countries defined by the system can not be deleted.'), 'Checking that delete is disable for core country.');
    $new = country_create(array(
      'iso2' => 'XA',
      'name' => 'A test XA',
      'official_name' => 'A test XA official name',
    ));
    if (country_validate($new)) {
      country_save($new);
    }
    else {
      $this
        ->error('Error saving new country');
    }
    $new = country_load('xa');
    $this
      ->assertFALSE(country_is_locked($new), 'User country is not locked');

    // Will be listed on page 1
    $this
      ->drupalGet('admin/config/regional/countries');
    $this
      ->assertListingLink($new, 'delete', 1);

    // Check the delete page
    $this
      ->drupalGet('admin/config/regional/countries/XA/delete');
    $this
      ->assertRaw(t('Are you sure you want to delete the country %country?', array(
      '%country' => $new->name,
    )));
    $this
      ->drupalPost('admin/config/regional/countries/XA/delete', array(), t('Delete'));
    $this
      ->assertRaw(t('Deleted country %country.', array(
      '%country' => $new->name,
    )));
    $this
      ->assertNoText($new->official_name, 'Delete link appear for core countries.');
  }

  /**
   * This test that the country sort works.
   */
  function testCountriesSort() {
    $strings = array(
      'AA' => 'Zzz',
      'AB' => 'Aaa',
      'AC' => 'Åbc',
      'AD' => 'Acd',
    );
    uasort($strings, 'countries_sort');
    $this
      ->assertEqual(implode('-', array_keys($strings)), 'AB-AC-AD-AA', 'Simple string sort');
    $objects = array(
      'AA' => country_create(array(
        'name' => 'Aec',
        'iso2' => 'aa',
      )),
      'AD' => country_create(array(
        'name' => 'Abd',
        'iso2' => 'ad',
      )),
      'AC' => country_create(array(
        'name' => 'Åbc',
        'iso2' => 'ac',
      )),
      'AB' => country_create(array(
        'name' => 'Aba',
        'iso2' => 'ab',
      )),
    );
    uasort($objects, 'countries_sort');
    $this
      ->assertEqual(implode('-', array_keys($objects)), 'AB-AC-AD-AA', 'Simple object sort');
  }

  /**
   * This test that the country property lookup is working.
   */
  function testCountriesPropertyLookup() {

    // Defaults, all empty.
    $country = country_create();
    $results = array(
      'cid' => NULL,
      'iso2' => '',
      'iso3' => '',
      'name' => '',
      'official_name' => '',
      'continent' => t('Unknown'),
      'continent_code' => 'UN',
      'enabled' => t('Enabled'),
      'numcode' => '',
    );
    $passed_all = TRUE;
    foreach ($results as $key => $expected_results) {
      $results = country_property($country, $key, array(
        'sanitize' => 0,
      ));
      if ($results !== $expected_results) {
        $this
          ->fail('Country property failed on ' . $key . '. Found ' . $results . ', expected ' . $expected_results);
        $passed_all = FALSE;
      }
    }
    if ($passed_all) {
      $this
        ->pass('Country property lookup on default properties passed.');
    }

    // Defaults, all empty.
    unset($country->continent);
    unset($country->enabled);
    $results = array(
      'cid' => NULL,
      'iso2' => 'xxx',
      'iso3' => 'xxx',
      'name' => 'xxx',
      'official_name' => 'xxx',
      'continent' => 'xxx',
      'continent_code' => 'xxx',
      'enabled' => t('Disabled'),
      'numcode' => 'xxx',
    );
    $passed_all = TRUE;
    foreach ($results as $key => $expected_results) {
      $actual_results = country_property($country, $key, array(
        'sanitize' => 0,
        'default' => 'xxx',
      ));
      if ($actual_results !== $expected_results) {
        $this
          ->fail('Country property using default failed on ' . $key . '. Found ' . $actual_results . ', expected ' . $expected_results);
        $passed_all = FALSE;
      }
    }
    if ($passed_all) {
      $this
        ->pass('Country property lookup on default properties using fallback default all passed.');
    }

    // A test with as many nasties as the system allows via the UI.
    $country = country_create(array(
      'cid' => 9999,
      'iso2' => 'XZ',
      'iso3' => 'AAA',
      'name' => '<a>&amp;Test</a>',
      'official_name' => '<a>&amp;Test official</a>',
      'continent' => 'NA',
      'enabled' => 0,
      'numcode' => '1',
    ));
    $results = array(
      'cid' => '9999',
      'iso2' => 'XZ',
      'iso3' => 'AAA',
      'name' => '<a>&amp;Test</a>',
      'official_name' => '<a>&amp;Test official</a>',
      'continent' => t('North America'),
      'continent_code' => 'NA',
      'enabled' => t('Disabled'),
      'numcode' => '001',
    );
    $passed_all = TRUE;
    foreach ($results as $key => $expected_results) {
      $results = (string) country_property($country, $key, array(
        'sanitize' => 1,
      ));
      if ($results !== check_plain($expected_results)) {
        $this
          ->fail('Country property failed on ' . $key . '. Found ' . $results . ', expected ' . $expected_results);
        $passed_all = FALSE;
      }
      $results = (string) country_property($country, $key, array(
        'sanitize' => 1,
        'default' => 'xxx',
      ));
      if ($results !== check_plain($expected_results)) {
        $this
          ->fail('Country property using default failed on ' . $key . '. Found ' . $results . ', expected ' . $expected_results);
        $passed_all = FALSE;
      }
      $results = (string) country_property($country, $key, array(
        'sanitize' => 0,
      ));
      if ($results !== $expected_results) {
        $this
          ->fail('Country property failed on unsanitised results ' . $key . '. Found ' . check_plain($results) . ', expected ' . check_plain($expected_results));
        $passed_all = FALSE;
      }
      $results = (string) country_property($country, $key, array(
        'sanitize' => 0,
        'default' => 'xxx',
      ));
      if ($results !== $expected_results) {
        $this
          ->fail('Country property failed on unsanitised results using default value ' . $key . '. Found ' . check_plain($results) . ', expected ' . check_plain($expected_results));
        $passed_all = FALSE;
      }
    }
    if ($passed_all) {
      $this
        ->pass('Country property lookup on populated country passed.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CountriesBaseSetupTest::assertCountryListed function
CountriesBaseSetupTest::assertListingLink function
CountriesBaseSetupTest::assertNoOptionPresent function
CountriesBaseSetupTest::assertOptionPresent function
CountriesBaseSetupTest::getDisabledCountries function Provides a sample group of countries defined by core that match the ISO Standards.
CountriesBaseSetupTest::getNewCountries function These countries are not part of core.
CountriesBaseSetupTest::getNonStandardCountries function These are the invalid strings in core.
CountriesBaseSetupTest::getNonStandardCountriesCorrected function The valid list if ISO strings
CountriesBaseSetupTest::getUnchangedCountries function Provides a sample group of countries defined by core that match the ISO Standards.
CountriesFunctionsUnitTest::getInfo public static function
CountriesFunctionsUnitTest::setUp function Overrides CountriesBaseSetupTest::setUp
CountriesFunctionsUnitTest::testCountriesLock function This test that core countries can not be deleted while user ones are.
CountriesFunctionsUnitTest::testCountriesLookupListing function This browes the admin listing, making sure that the countries are correctly listed.
CountriesFunctionsUnitTest::testCountriesPropertyLookup function This test that the country property lookup is working.
CountriesFunctionsUnitTest::testCountriesSort function This test that the country sort works.