You are here

class CountriesCRUDUIUnitTest in Countries 8

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

Test the node_load_multiple() function.

Hierarchy

Expanded class hierarchy of CountriesCRUDUIUnitTest

File

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

View source
class CountriesCRUDUIUnitTest extends CountriesBaseSetupTest {
  public static function getInfo() {
    return array(
      'name' => 'Country administration UI',
      'description' => 'Test the country creation, editting, deletion via the admin UI.',
      'group' => 'Countries',
    );
  }
  function setUp() {
    parent::setUp();
    $this
      ->drupalLogin($this->admin_user);
  }

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

    // A basic country with minimal requirements.
    $edit = array(
      'iso2' => 'xa',
      'name' => 'A test country (xa)',
    );
    $this
      ->drupalPost('admin/config/regional/countries/add', $edit, t('Save'));
    $this
      ->assertRaw(t('Added country %country.', array(
      '%country' => 'A test country (xa)',
    )), 'New country was added');
    $this
      ->assertCountryListed($edit, 'name');

    // An invalid country
    $edit = array(
      'iso2' => '12',
      // not alpha-numerical
      'name' => 'New Zealand',
      // duplicate
      'official_name' => 'Aruba',
      // duplicate
      'iso3' => 'qw$',
      // special char
      'numcode' => '-1',
    );
    $this
      ->drupalPost('admin/config/regional/countries/add', $edit, t('Save'));
    $this
      ->assertNoRaw(t('Added country %country.', array(
      '%country' => 'New Zealand',
    )), 'New country was not added (validation)');

    // Individually test the invalid properties.
    $valid_country = (array) country_create(array(
      'iso2' => 'xb',
      'name' => 'A test xb',
      'official_name' => 'Official test for xb',
      'iso3' => 'Xab',
      'numcode' => '901',
    ));

    // Filter out non-country schema based keys.
    $valid_fields = drupal_map_assoc(array(
      'iso2',
      'iso3',
      'name',
      'official_name',
      'numcode',
      'continent',
      'enabled',
      'language',
    ));
    $valid_country = (object) array_intersect_key($valid_country, $valid_fields);
    $invalid_properties = array(
      'iso2' => array(
        '12',
        // numerical
        '',
        // empty
        '$s',
        // special char
        'au',
        // duplicate
        'd',
        // single property
        'ddd',
      ),
      'name' => array(
        '',
        // empty
        'Australia',
        // duplicate
        'aUstralia',
        // duplicate, different case
        'Kingdom of Bahrain',
        // duplicate from official name column
        'kingdom Of Bahrain',
        // duplicate from official name column, different case
        'Tooo long 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789',
      ),
      'official_name' => array(
        'Bahrain',
        // duplicate from name column
        'BahraiN',
        // duplicate from name column, different case
        'Kingdom of Bahrain',
        // duplicate from official name column
        'kingdom Of Bahrain',
        // duplicate from official name column, different case
        'Tooo long 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789',
      ),
      'iso3' => array(
        '123',
        // numerical
        '$ss',
        // special char
        'aus',
        // duplicate
        'd',
        // single char
        'dc',
        // double char
        'xaaa',
      ),
      'numcode' => array(
        '1001',
        // too big
        '-1',
        // too small
        '-12',
        // too small
        '-123',
        // too small
        '$23',
        // special char
        '23#',
        // special char
        '048',
        // duplicate
        '4',
        // duplicate
        '04',
      ),
    );
    foreach ($invalid_properties as $property => $values) {
      foreach ($values as $value) {
        $test = clone $valid_country;
        $test->{$property} = $value;
        $this
          ->drupalPost('admin/config/regional/countries/add', (array) $test, t('Save'));
        $this
          ->assertNoRaw(t('Added country %country.', array(
          '%country' => $test->name,
        )), 'New country was not added (validation)');
      }
    }

    // Try and edit the first country
    $country = country_load('xa');
    $this
      ->drupalPost('admin/config/regional/countries/xa', array(
      'numcode' => 905,
      'continent' => 'NA',
    ), t('Save'));
    countries_clear_caches();
    $editted = country_load('xa');
    $this
      ->assertEqual($editted->numcode, 905, 'Country edit: Test number code');
    $this
      ->assertEqual($editted->continent, 'NA', 'Country edit: Test continent');
    $this
      ->drupalPost('admin/config/regional/countries/xa', array(
      'iso2' => 'Xb',
      'iso3' => 'xbc',
      'numcode' => 910,
      'name' => 'A test for xa to xb',
      'continent' => 'SA',
      'enabled' => FALSE,
      'official_name' => 'A test for xa to xb official',
    ), t('Save'));
    countries_clear_caches();
    $editted = country_load('xb');
    if ($this
      ->assertTrue(!empty($editted), 'Test loading by new iso alpha-2 code')) {
      $this
        ->assertEqual($editted->iso2, 'XB', 'Country edit: Test iso2');
      $this
        ->assertEqual($editted->iso3, 'XBC', 'Country edit: Test iso3');
      $this
        ->assertEqual($editted->numcode, 910, 'Country edit: Test numeric code');
      $this
        ->assertEqual($editted->name, 'A test for xa to xb', 'Country edit: Test name');
      $this
        ->assertEqual($editted->official_name, 'A test for xa to xb official', 'Country edit: Test official name');
      $this
        ->assertEqual($editted->enabled, 0, 'Country edit: Test enabled');
      $this
        ->assertEqual($editted->continent, 'SA', 'Country edit: Test continent');
      $old = country_load('xa');
      $this
        ->assertTrue(empty($old), 'Previous instance is no longer available');
    }

    // Try to delete core countries.
    $this
      ->drupalGet('admin/config/regional/countries/TT/delete');
    $this
      ->assertText(t('Core countries defined by the system can not be deleted.'), 'Core country deletion failed');
    $this
      ->drupalGet('admin/config/regional/countries/xx/delete');
    $this
      ->assertResponse(404, 'Can not delete fake countries.');
    $this
      ->drupalPost('admin/config/regional/countries/xb/delete', array(), t('Delete'));
    $this
      ->assertRaw(t('Deleted country %country.', array(
      '%country' => $editted->name,
    )), 'Custom country was deleted');
    countries_clear_caches();
    $editted = country_load('xb');
    $this
      ->assertTrue(empty($editted), 'Custom country was really deleted.');
  }

  /**
   * Special case where the ISO matches an existing ISO code.
   *
   * If the same country is added twice, there are multiple errors generated due
   * to the ISO alpha-2 conflict.
   */
  function testISO2Conflict() {
    $oz = country_load('au');
    $edit = array();
    foreach (countries_core_properties() as $key => $label) {
      $edit[$key] = $oz->{$key};
    }
    $this
      ->drupalPost('admin/config/regional/countries/add', $edit, t('Save'));
    $this
      ->assertNoRaw(t('Added country %country.', array(
      '%country' => $oz->name,
    )), 'New country was not added (iso2 validation)');
    $this
      ->assertRaw(t('Another country was found with this ISO alpha-2 code; !link', array(
      '!link' => l(country_property($oz), 'admin/config/regional/countries/' . $oz->iso2),
    )), 'New country was not added (iso2 validation)');
    $nz = country_load('nz');
    $edit = array();
    foreach (countries_core_properties() as $key => $label) {
      $edit[$key] = $nz->{$key};
    }
    $edit['iso2'] = 'au';
    $this
      ->drupalPost('admin/config/regional/countries/add', $edit, t('Save'));
    $this
      ->assertRaw(t('Another country was found with this ISO alpha-2 code; !link', array(
      '!link' => l(country_property($oz), 'admin/config/regional/countries/' . $oz->iso2),
    )), 'New country was not added (iso2 validation)');
  }

}

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.
CountriesCRUDUIUnitTest::getInfo public static function
CountriesCRUDUIUnitTest::setUp function Overrides CountriesBaseSetupTest::setUp
CountriesCRUDUIUnitTest::testCountriesAddition function This test that the country property lookup is working.
CountriesCRUDUIUnitTest::testISO2Conflict function Special case where the ISO matches an existing ISO code.