You are here

function CountriesCRUDUIUnitTest::testCountriesAddition in Countries 7.2

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

This test that the country property lookup is working.

File

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

Class

CountriesCRUDUIUnitTest
Test the node_load_multiple() function.

Code

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