You are here

public function LocaleTranslationUiTest::testJavaScriptTranslation in Drupal 9

Same name and namespace in other branches
  1. 8 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()

Adds a language and checks that the JavaScript translation files are properly created and rebuilt on deletion.

File

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

Class

LocaleTranslationUiTest
Adds a new locale and translates its name. Checks 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);
}