You are here

public function LocaleTranslationUiTest::testStringTranslation in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/src/Tests/LocaleTranslationUiTest.php \Drupal\locale\Tests\LocaleTranslationUiTest::testStringTranslation()

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

File

core/modules/locale/src/Tests/LocaleTranslationUiTest.php, line 44
Contains \Drupal\locale\Tests\LocaleTranslationUiTest.

Class

LocaleTranslationUiTest
Adds a new locale and translates its name. Checks the validation of translation strings and search results.

Namespace

Drupal\locale\Tests

Code

public function testStringTranslation() {

  // 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
    ->randomMachineName(16);

  // This will be the translation of $name.
  $translation = $this
    ->randomMachineName(16);
  $translation_to_en = $this
    ->randomMachineName(16);

  // Add custom language.
  $this
    ->drupalLogin($admin_user);
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $name,
    'direction' => LanguageInterface::DIRECTION_LTR,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Add string.
  t($name, array(), array(
    'langcode' => $langcode,
  ))
    ->render();

  // Reset locale cache.
  $this->container
    ->get('string_translation')
    ->reset();
  $this
    ->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
  $this
    ->assertText(t($name), '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
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText($name, 'Search found the string as untranslated.');

  // No t() here, it's surely not translated yet.
  $this
    ->assertText($name, 'name found on edit screen.');
  $this
    ->assertNoOption('edit-langcode', 'en', 'No way to translate the string to English.');
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalPostForm('admin/config/regional/language/edit/en', array(
    'locale_translate_english' => TRUE,
  ), t('Save language'));
  $this
    ->drupalLogout();
  $this
    ->drupalLogin($translate_user);
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText($name, '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
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
  $this
    ->assertText(t('The strings have been saved.'), 'The strings have been saved.');
  $url_bits = explode('?', $this
    ->getUrl());
  $this
    ->assertEqual($url_bits[0], \Drupal::url('locale.translate_page', array(), array(
    'absolute' => TRUE,
  )), 'Correct page redirection.');
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'translated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertRaw($translation, 'Non-English translation properly saved.');
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $translation_to_en,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'translated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $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.
  $this->container
    ->get('string_translation')
    ->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' => LanguageInterface::LANGCODE_SYSTEM,
  )) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), 'String is translated.');

  // Test invalidation of 'rendered' cache tag after string translation.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('xx/user/login');
  $this
    ->assertText('Enter the password that accompanies your username.');
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => 'accompanies your username',
    'langcode' => $langcode,
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => 'Please enter your Llama username.',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
  $this
    ->drupalLogout();
  $this
    ->drupalGet('xx/user/login');
  $this
    ->assertText('Please enter your Llama username.');

  // 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
    ->drupalPostForm($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
    ->drupalGet($path);

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

  // Delete the string.
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'translated',
  );
  $this
    ->drupalPostForm('admin/config/regional/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
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
  $this
    ->assertRaw($name, 'The strings have been saved.');
  $this
    ->drupalLogin($translate_user);
  $search = array(
    'string' => $name,
    'langcode' => 'en',
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), 'The translation has been removed');
}