You are here

function i18nStringTestCase::testCaching in Internationalization 7

Test base i18n_string caching.

File

i18n_string/i18n_string.test, line 83
Test case for multilingual string

Class

i18nStringTestCase
Class for testing i18n_string and modules using these features

Code

function testCaching() {

  // Create a bunch of strings for all languages.
  $textgroup = 'test_cached';
  $strings = $this
    ->stringCreateArray(2);
  $translations = array();
  $textgroup_object = i18n_string_textgroup($textgroup);

  // Save source strings and store translations.
  foreach ($strings as $key => $string) {
    $name = "{$textgroup}:item:{$key}:title";
    i18n_string_update($name, $string);
    $translations[$key] = $this
      ->createStringTranslation($textgroup, $string);
  }

  // Now fetch the strings to fill the cache.
  foreach ($textgroup_object->strings as $context => $string_object) {
    $this
      ->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
  }
  foreach ($strings as $key => $string) {
    $this
      ->drupalGet('tests/i18n/i18n_string_translation_search/' . $textgroup . ':item:' . $key . ':*/es');
  }

  // Check the persistent cache for contents.
  $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':strings');
  if ($this
    ->assertNotEqual($cache, FALSE, 'Textgroup strings cache found')) {
    foreach ($textgroup_object->strings as $context => $string_object) {
      if ($this
        ->assertTrue(isset($cache->data[$context]), format_string('Cached string %context found', array(
        '%context' => $context,
      )))) {
        $this
          ->assertEqual($cache->data[$context], $context, 'Cached string is a string and not an object');
      }

      // Check if the string object cache is also available.
      $string_cache = cache_get($string_object
        ->get_cid());
      if ($this
        ->assertNotEqual($string_cache, FALSE, format_string('Cached string object %cid found', array(
        '%cid' => $string_object
          ->get_cid(),
      )))) {
        $this
          ->assertTrue(is_array($string_cache->data), 'Cached string object is an array.');
      }
    }
  }
  $cache = cache_get('i18n:string:tgroup:' . $textgroup . ':cache_multiple');
  if ($this
    ->assertNotEqual($cache, FALSE, 'Textgroup cache_multiple cache found')) {
    foreach ($strings as $key => $string) {
      $pattern = 'item:' . $key . ':*:es';
      if ($this
        ->assertTrue(isset($cache->data[$pattern]), format_string('Cached multiple cache for pattern %pattern found', array(
        '%pattern_key' => $pattern,
      )))) {
        $property_pattern = 'item:' . $key . ':title';
        if ($this
          ->assertTrue(isset($cache->data[$pattern][$property_pattern]), format_string('Cached multiple property title found', array(
          '%pattern_key' => $pattern,
        )))) {
          $this
            ->assertEqual($cache->data[$pattern][$property_pattern], $property_pattern);
        }
      }
    }
  }

  // Test cache injection.
  foreach ($textgroup_object->strings as $context => $string_object) {

    // Check if the string object cache is also available.
    $string_cache = cache_get($string_object
      ->get_cid());
    if (isset($string_cache->data)) {

      // Modify cache.
      $string_cache->data['string'] = "Injected value.";
      cache_set($string_object
        ->get_cid(), $string_cache->data, 'cache', CACHE_TEMPORARY);

      // Check if modification is reflected on the next page call.
      $this
        ->drupalGet('tests/i18n/i18n_string_build/' . $textgroup . ':' . $context);
      $this
        ->assertText($string_cache->data['string']);
    }
  }

  // Test that un-translated strings are cached correctly.
  $textgroup = 'test_cached';
  $key = 3;
  $string = self::randomName(100);
  $name = "{$textgroup}:item:{$key}:title";
  i18n_string_update($name, $string);

  // Generate the cache entry.
  $string_object = i18n_string_build($name, $string);
  $langcode = i18n_langcode();
  $string_object
    ->get_translation($langcode);

  // Destroy the textgroup object to write the cache entry.
  $textgroup_object = i18n_string_textgroup($textgroup);
  $textgroup_object
    ->__destruct();
  $this
    ->assertTrue(cache_get($string_object
    ->get_cid()) !== FALSE, "Cache entry created.");
  drupal_static_reset('i18n_string_textgroup');

  // Reset the loaded translation variable.
  variable_del('i18n_loaded_translations');
  $loaded_translations = variable_get('i18n_loaded_translations', array());
  $this
    ->verbose(var_export($loaded_translations, TRUE));

  // Rebuild the string.
  $string_object = i18n_string_build($name, $string);
  $string_object
    ->get_translation($langcode);

  // Check that the string hasn't been loaded.
  $loaded_translations = variable_get('i18n_loaded_translations', array());
  $this
    ->verbose(var_export($loaded_translations, TRUE));
  $this
    ->assertFalse(isset($loaded_translations['test_cached:item:3:title']), "The untranslated string was correctly cached.");
}