function LocaleJavascriptTranslationTest::testLocaleTranslationJsDependencies

Same name in other branches
  1. 8.9.x 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()
  3. 11.x 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->drupalGet('admin/config/regional/language/add');
    $this->submitForm($edit, 'Add custom language');
    // Set path prefix.
    $edit = [
        "prefix[{$langcode}]" => $prefix,
    ];
    $this->drupalGet('admin/config/regional/language/detection/url');
    $this->submitForm($edit, '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->submitForm([
        'string' => 'Show description',
    ], 'Filter');
    $edit = [
        'strings[' . $string->lid . '][translations][0]' => 'Mostrar descripcion',
    ];
    $this->submitForm($edit, '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->assertSession()
        ->responseContains('core/misc/drupal.js');
    $this->assertSession()
        ->responseContains($js_filename);
    // Assert translations JS is included before drupal.js.
    $this->assertLessThan(strpos($content, 'core/misc/drupal.js'), strpos($content, $js_filename));
}

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