You are here

public function LocaleTranslationUiTest::testJavaScriptTranslation in Zircon Profile 8.0

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

File

core/modules/locale/src/Tests/LocaleTranslationUiTest.php, line 217
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 testJavaScriptTranslation() {
  $user = $this
    ->drupalCreateUser(array(
    'translate interface',
    'administer languages',
    'access administration pages',
  ));
  $this
    ->drupalLogin($user);
  $config = $this
    ->config('locale.settings');
  $langcode = 'xx';

  // The English name for the language. This will be translated.
  $name = $this
    ->randomMachineName(16);

  // Add custom language.
  $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'));
  $this->container
    ->get('language_manager')
    ->reset();

  // Build the JavaScript translation file.
  // Retrieve the source string of the first string available in the
  // {locales_source} table and translate it.
  $query = db_select('locales_source', 's');
  $query
    ->addJoin('INNER', 'locales_location', 'l', 's.lid = l.lid');
  $source = $query
    ->fields('s', array(
    'source',
  ))
    ->condition('l.type', 'javascript')
    ->range(0, 1)
    ->execute()
    ->fetchField();
  $search = array(
    'string' => $source,
    'langcode' => $langcode,
    'translation' => 'all',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $this
      ->randomMachineName(),
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

  // Trigger JavaScript translation parsing and building.
  _locale_rebuild_js($langcode);
  $locale_javascripts = \Drupal::state()
    ->get('locale.translation.javascript') ?: array();
  $js_file = 'public://' . $config
    ->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
  $this
    ->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file created: %file', array(
    '%file' => $result ? $js_file : 'not found',
  )));

  // Test JavaScript translation rebuilding.
  file_unmanaged_delete($js_file);
  $this
    ->assertTrue($result = !file_exists($js_file), SafeMarkup::format('JavaScript file deleted: %file', array(
    '%file' => $result ? $js_file : 'found',
  )));
  _locale_rebuild_js($langcode);
  $this
    ->assertTrue($result = file_exists($js_file), SafeMarkup::format('JavaScript file rebuilt: %file', array(
    '%file' => $result ? $js_file : 'not found',
  )));
}