function LocaleTranslationTest::testStringTranslation

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

File

drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php, line 46
Definition of Drupal\locale\Tests\LocaleTranslationTest.

Class

LocaleTranslationTest
Functional test for string translation and validation.

Namespace

Drupal\locale\Tests

Code

function testStringTranslation() {
  global $base_url;

  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer languages',
    'access administration pages',
  ));

  // User to translate and delete string.
  $translate_user = $this
    ->drupalCreateUser(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);

  // This is the language indicator on the translation search screen for
  // untranslated strings.
  $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
    ->drupalLogin($admin_user);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'name' => $name,
    'direction' => '0',
  );
  $this
    ->drupalPost('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 . '"', t('Language code found.'));
  $this
    ->assertText(t($name), t('Test language added.'));
  $this
    ->drupalLogout();

  // Search for the name and translate it.
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertText($name, t('Search found the string as untranslated.'));

  // Assume this is the only result, given the random name.
  // We save the lid from the path.
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $this
      ->randomName(),
  );

  // No t() here, it's surely not translated yet.
  $this
    ->assertText($name, t('name found on edit screen.'));
  $this
    ->assertNoText('English', t('No way to translate the string to English.'));
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalPost('admin/config/regional/language/edit/en', array(
    'locale_translate_english' => TRUE,
  ), t('Save language'));
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($translate_user);
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertText($name, t('Search found the string as untranslated.'));

  // Assume this is the only result, given the random name.
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $translation,
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $edit, t('Save translations'));
  $this
    ->assertText(t('The strings have been saved.'), t('The strings have been saved.'));
  $this
    ->assertEqual($this
    ->getUrl(), url('admin/config/regional/translate/translate', array(
    'absolute' => TRUE,
  )), t('Correct page redirection.'));
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertRaw($translation, t('Non-English translation properly saved.'));
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $translation_to_en,
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $edit, t('Save translations'));
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertRaw($translation_to_en, t('English translation properly saved.'));

  // Reset the tag cache on the tester side in order to pick up the call to
  // cache()->deleteTags() on the tested side.
  drupal_static_reset('Drupal\\Core\\Cache\\CacheBackendInterface::tagCache');
  $this
    ->assertTrue($name != $translation && t($name, array(), array(
    'langcode' => $langcode,
  )) == $translation, t('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('t() works for English.'));
  $this
    ->assertTrue(t($name, array(), array(
    'langcode' => LANGUAGE_SYSTEM,
  )) == $name, t('t() works for LANGUAGE_SYSTEM.'));
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), t('String is translated.'));
  $this
    ->drupalLogout();

  // Delete the language.
  $this
    ->drupalLogin($admin_user);
  $path = 'admin/config/regional/language/delete/' . $langcode;

  // This a confirm form, we do not need any fields changed.
  $this
    ->drupalPost($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), t('The test language has been removed.'));

  // Reload to remove $name.
  $this
    ->drupalGet($path);

  // Verify that language is no longer found.
  $this
    ->assertResponse(404, t('Language no longer found.'));
  $this
    ->drupalLogout();

  // Delete the string.
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'translated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));

  // Assume this is the only result, given the random name.
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => '',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $edit, t('Save translations'));
  $this
    ->assertRaw($name, t('The strings have been saved.'));
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPost('admin/config/regional/translate/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), t('The translation has been removed'));
}