1.20.x locale.test LocaleTranslationFunctionalTest::testStringTranslation()

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

File

modules/locale/tests/locale.test, line 180
Tests for locale.module.

Class

LocaleTranslationFunctionalTest
Functional test for string translation and validation.

Code

function testStringTranslation() {
  global $base_url;

  // User to add and remove language.
  $admin_user = $this->backdropCreateUser(array('administer languages', 'access administration pages'));
  // User to translate and delete string.
  $translate_user = $this->backdropCreateUser(array('translate interface', 'access administration pages'));
  // Code for the language.
  $langcode = 'xx';
  // The English name for the language. This will be translated.
  $name = $this->randomName(16);
  // The native name for the language.
  $native = $this->randomName(16);
  // This is the language indicator on the translation search screen for
  // untranslated strings. Copied straight from locale.inc.
  $language_indicator = "<em class=\"locale-untranslated\">$langcode</em> ";
  // This will be the translation of $name.
  $translation = $this->randomName(16);
  $translation_to_en = $this->randomName(16);

  // Add custom language.
  $this->backdropLogin($admin_user);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'name' => $name,
    'native' => $native,
    'direction' => '0',
  );
  $this->backdropPost('admin/config/regional/language/add', $edit, t('Add custom language'));
  // Add string.
  t($name, array(), array('langcode' => $langcode));
  // Reset locale cache.
  locale_reset();
  $this->assertRaw('"edit-site-default-' . $langcode . '"', 'Language code found.');
  $this->assertText(t($name), 'Test language added.');
  $this->backdropLogout();

  // Search for the name and translate it.
  $this->backdropLogin($translate_user);
  $search = array(
    'string' => $name,
    'language' => 'all',
    'translation' => 'all',
  );
  $this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
  // assertText() seems to remove the input field where $name always could be
  // found, so this is not a false assert. See how assertNoText succeeds
  // later.
  $this->assertText($name, 'Search found the name.');
  $this->assertRaw($language_indicator, 'Name is untranslated.');
  // Assume this is the only result, given the random name.
  $this->clickLink(t('Edit'));
  // We save the lid from the path.
  $matches = array();
  preg_match('!admin/config/regional/translate/edit/(\d+)!', $this->getUrl(), $matches);
  $lid = $matches[1];
  // No t() here, it's surely not translated yet.
  $this->assertText($name, 'name found on edit screen.');
  $this->assertNoText('English', 'No way to translate the string to English.');
  $this->backdropLogout();
  $this->backdropLogin($admin_user);
  $this->backdropPost('admin/config/regional/language/edit/en', array('locale_translate_english' => TRUE), t('Save language'));
  $this->backdropLogout();
  $this->backdropLogin($translate_user);
  $this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
  // assertText() seems to remove the input field where $name always could be
  // found, so this is not a false assert. See how assertNoText succeeds
  // later.
  $this->assertText($name, 'Search found the name.');
  $this->assertRaw($language_indicator, 'Name is untranslated.');
  // Assume this is the only result, given the random name.
  $this->clickLink(t('Edit'));
  $string_edit_url = $this->getUrl();
  $edit = array(
    "translations[$langcode]" => $translation,
    'translations[en]' => $translation_to_en,
  );
  $this->backdropPost(NULL, $edit, t('Save translations'));
  $this->assertText(t('The string has been saved.'), 'The string has been saved.');
  $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  $this->backdropGet($string_edit_url);
  $this->assertRaw($translation, 'Non-English translation properly saved.');
  $this->assertRaw($translation_to_en, 'English translation properly saved.');
  $this->assertTrue($name != $translation && t($name, array(), array('langcode' => $langcode)) == $translation, 't() works for non-English.');
  // Refresh the locale() cache to get fresh data from t() below. We are in
  // the same HTTP request and therefore t() is not refreshed by saving the
  // translation above.
  locale_reset();
  // Now we should get the proper fresh translation from t().
  $this->assertTrue($name != $translation_to_en && t($name, array(), array('langcode' => 'en')) == $translation_to_en, 't() works for English.');
  $this->assertTrue(t($name, array(), array('langcode' => LANGUAGE_SYSTEM)) == $name, 't() works for LANGUAGE_SYSTEM.');
  $this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
  // The indicator should not be here.
  $this->assertNoRaw($language_indicator, 'String is translated.');

  // Verify that a translation set which has an empty target string can be
  // updated without any database error.
  db_update('locales_target')
    ->fields(array('translation' => ''))
    ->condition('language', $langcode, '=')
    ->condition('lid', $lid, '=')
    ->execute();
  $this->backdropPost('admin/config/regional/translate/edit/' . $lid, $edit, t('Save translations'));
  $this->assertText(t('The string has been saved.'), 'The string has been saved.');

  // Try to edit a non-existent string and ensure we're redirected correctly.
  // Assuming we don't have 999,999 strings already.
  $random_lid = 999999;
  $this->backdropGet('admin/config/regional/translate/edit/' . $random_lid);
  $this->assertText(t('String not found'), 'String not found.');
  $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  $this->backdropLogout();

  // Delete the language.
  $this->backdropLogin($admin_user);
  $path = 'admin/config/regional/language/delete/' . $langcode;
  // This a confirm form, we do not need any fields changed.
  $this->backdropPost($path, array(), t('Delete'));
  // We need raw here because %language and %langcode will add HTML.
  $t_args = array('%language' => $name, '%langcode' => $langcode);
  $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
  // Reload to remove $name.
  $this->backdropGet($path);
  // Verify that language is no longer found.
  $this->assertResponse(404, 'Language no longer found.');
  $this->backdropLogout();

  // Delete the string.
  $this->backdropLogin($translate_user);
  $search = array(
    'string' => $name,
    'language' => 'all',
    'translation' => 'all',
  );
  $this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
  // Assume this is the only result, given the random name.
  $this->clickLink(t('Delete'));
  $this->assertText(t('Are you sure you want to delete the string'), '"delete" link is correct.');
  // Delete the string.
  $path = 'admin/config/regional/translate/delete/' . $lid;
  $this->backdropGet($path);
  // First test the 'cancel' link.
  $this->clickLink(t('Cancel'));
  $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  $this->assertRaw($name, 'The string was not deleted.');
  // Delete the name string.
  $this->backdropPost('admin/config/regional/translate/delete/' . $lid, array(), t('Delete'));
  $this->assertText(t('The string has been removed.'), 'The string has been removed message.');
  $this->assertEqual($this->getUrl(), url('admin/config/regional/translate/translate', array('absolute' => TRUE)), 'Correct page redirection.');
  $this->backdropPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this->assertNoText($name, 'Search now can not find the name.');
}