You are here

public function LanguageHierarchyI18nStringTranslationWebTestCase::testI18nStringTranslation in Language Hierarchy 7

Adds a language and tests i18n_string translation by users with the appropriate permissions.

File

tests/language_hierarchy.test, line 404
Tests for Language Hierarchy module.

Class

LanguageHierarchyI18nStringTranslationWebTestCase
Functional tests for i18n_string (user-defined strings) translation.

Code

public function testI18nStringTranslation() {

  // This will be the string to be translated.
  $string = $this
    ->randomName(16);

  // This will be the translation of $string.
  $translation = $this
    ->randomName(16);
  $this
    ->loginAdminUser();

  // Add languages used by this test case.
  $this
    ->addLanguages();
  $this
    ->drupalLogout();

  // Search for the string and translate it.
  $this
    ->loginTranslatorUser();
  $textgroup = 'menu';

  // Save source string and store translation.
  $name = "{$textgroup}:item:1:title";
  i18n_string_update($name, $string);

  // Reset cache for text group
  i18n_string_textgroup($textgroup)
    ->cache_reset();
  $parent_langcode = $this->languages[0]['langcode'];
  $child_langcode = $this->languages[1]['langcode'];
  $child_langcode2 = $this->languages[2]['langcode'];
  $translation2 = $this
    ->randomName(16);
  $translations = array(
    // Translate $string for parent language.
    $parent_langcode => $translation,
    // Translate $string for one of child languages.
    $child_langcode2 => $translation2,
  );
  $this
    ->addI18nStringTranslation($textgroup, $string, $translations);

  // Check if i18n_string() works as expected: a translation should be found
  // for the parent language.
  $parent_translation = i18n_string_translate($name, 'NOT FOUND', array(
    'langcode' => $parent_langcode,
  ));
  $this
    ->assertTrue($string != $translation && $parent_translation == $translation, 'i18n_string() works for parent.');

  // Check if inheritance of i18n_string() works as expected: no specific
  // translation for this child language, so translation in parent language
  // returned.
  $child_translation = i18n_string_translate($name, 'NOT FOUND', array(
    'langcode' => $child_langcode,
  ));
  $this
    ->assertTrue($string != $translation && $child_translation == $translation, 'i18n_string() inherited from parent.');

  // Check if translated string for child language works as expected: a
  // specific translation for this child language should be found.
  $child_translation2 = i18n_string_translate($name, 'NOT FOUND', array(
    'langcode' => $child_langcode2,
  ));
  $this
    ->assertTrue($string != $translation2 && $child_translation2 == $translation2, 'i18n_string() with translation for child.');
}