You are here

function CountriesCacheUnitTest::testCache in Countries 8

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

File

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

Class

CountriesCacheUnitTest
Test the cache is not breaking core functionality.

Code

function testCache() {

  // Test the CSV import data.
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $this
    ->assertEqual($countries['US']->name, 'United States');

  // A direct database query should show after caches are reset.
  db_update('countries_country')
    ->fields(array(
    'name' => 'United States America',
  ))
    ->condition('iso2', 'US')
    ->execute();
  countries_clear_caches();
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $this
    ->assertEqual($countries['US']->name, 'United States America');

  // Test a save via the UI.
  $this
    ->drupalPost('admin/config/regional/countries/us', array(
    'name' => 'United States',
  ), t('Save'));

  // Clear the SimpleTest caches too.
  countries_clear_caches();
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $this
    ->assertEqual($countries['US']->name, 'United States');

  // Tests the internal CRUD save action.
  $us = country_load('us');
  $this
    ->assertEqual($us->name, 'United States');
  $us->name = 'United States America';
  country_save($us);
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $this
    ->assertEqual($countries['US']->name, 'United States America');

  // Test a missing country to ensure the import doesn't include it.
  if (isset($countries['XA'])) {
    $this
      ->fail('Test non-existent country XA exists.');
    return;
  }

  // Create, save and delete tests should all result in cache updates.
  $xa = country_create(array(
    'iso2' => 'xa',
    'name' => 'Test',
  ));
  if (country_validate($xa)) {
    country_save($xa);
  }
  else {
    $this
      ->error('Error saving new country');
  }
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  $this
    ->assertEqual($countries['XA']->name, 'Test');
  country_delete('xa');
  $countries = countries_get_countries('all', array(), array(
    'sanitize' => FALSE,
  ));
  if (isset($countries['XA'])) {
    $this
      ->fail('Test deletion failed.');
  }
}