You are here

public function LocaleJavascriptTranslationTest::testLocaleTranslationJsDependencies in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/locale/tests/src/Functional/LocaleJavascriptTranslationTest.php \Drupal\Tests\locale\Functional\LocaleJavascriptTranslationTest::testLocaleTranslationJsDependencies()
  2. 10 core/modules/locale/tests/src/Functional/LocaleJavascriptTranslationTest.php \Drupal\Tests\locale\Functional\LocaleJavascriptTranslationTest::testLocaleTranslationJsDependencies()

Assert translations JS is added before drupal.js, because it depends on it.

File

core/modules/locale/tests/src/Functional/LocaleJavascriptTranslationTest.php, line 117

Class

LocaleJavascriptTranslationTest
Tests parsing js files for translatable strings.

Namespace

Drupal\Tests\locale\Functional

Code

public function testLocaleTranslationJsDependencies() {

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

  // Add custom language.
  $this
    ->drupalLogin($admin_user);

  // Code for the language.
  $langcode = 'es';

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

  // The domain prefix.
  $prefix = $langcode;
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $name,
    'direction' => LanguageInterface::DIRECTION_LTR,
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Set path prefix.
  $edit = [
    "prefix[{$langcode}]" => $prefix,
  ];
  $this
    ->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));

  // This forces locale.admin.js string sources to be imported, which contains
  // the next translation.
  $this
    ->drupalGet($prefix . '/admin/config/regional/translate');

  // Translate a string in locale.admin.js to our new language.
  $strings = \Drupal::service('locale.storage')
    ->getStrings([
    'source' => 'Show description',
    'type' => 'javascript',
    'name' => 'core/modules/locale/locale.admin.js',
  ]);
  $string = $strings[0];
  $this
    ->drupalPostForm(NULL, [
    'string' => 'Show description',
  ], t('Filter'));
  $edit = [
    'strings[' . $string->lid . '][translations][0]' => 'Mostrar descripcion',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save translations'));

  // Calculate the filename of the JS including the translations.
  $js_translation_files = \Drupal::state()
    ->get('locale.translation.javascript');
  $js_filename = $prefix . '_' . $js_translation_files[$prefix] . '.js';
  $content = $this
    ->getSession()
    ->getPage()
    ->getContent();
  $this
    ->assertRaw('core/misc/drupal.js');
  $this
    ->assertRaw($js_filename);

  // Assert translations JS is included before drupal.js.
  $this
    ->assertTrue(strpos($content, $js_filename) < strpos($content, 'core/misc/drupal.js'), 'Translations are included before Drupal.t.');
}