function LocaleTranslationUiTest::testJavaScriptTranslation

Same name and namespace in other branches
  1. 8.9.x core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
  2. 10 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()
  3. 11.x core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testJavaScriptTranslation()

Tests the rebuilding of JavaScript translation files on deletion.

File

core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php, line 249

Class

LocaleTranslationUiTest
Tests the validation of translation strings and search results.

Namespace

Drupal\Tests\locale\Functional

Code

public function testJavaScriptTranslation() {
  $user = $this->drupalCreateUser([
    '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 = [
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $name,
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this->drupalGet('admin/config/regional/language/add');
  $this->submitForm($edit, '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 = Database::getConnection()->select('locales_source', 's');
  $query->addJoin('INNER', 'locales_location', 'l', '[s].[lid] = [l].[lid]');
  $source = $query->fields('s', [
    'source',
  ])
    ->condition('l.type', 'javascript')
    ->range(0, 1)
    ->execute()
    ->fetchField();
  $search = [
    'string' => $source,
    'langcode' => $langcode,
    'translation' => 'all',
  ];
  $this->drupalGet('admin/config/regional/translate');
  $this->submitForm($search, 'Filter');
  $textarea = $this->assertSession()
    ->elementExists('xpath', '//textarea');
  $lid = $textarea->getAttribute('name');
  $edit = [
    $lid => $this->randomMachineName(),
  ];
  $this->drupalGet('admin/config/regional/translate');
  $this->submitForm($edit, 'Save translations');
  // Trigger JavaScript translation parsing and building.
  _locale_rebuild_js($langcode);
  $locale_javascripts = \Drupal::state()->get('locale.translation.javascript', []);
  $js_file = 'public://' . $config->get('javascript.directory') . '/' . $langcode . '_' . $locale_javascripts[$langcode] . '.js';
  $this->assertFileExists($js_file);
  // Test JavaScript translation rebuilding.
  \Drupal::service('file_system')->delete($js_file);
  $this->assertFileDoesNotExist($js_file);
  _locale_rebuild_js($langcode);
  $this->assertFileExists($js_file);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.