function LocaleTranslationUiTest::testStringTranslation

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

Adds a language and tests string translation by users with the appropriate permissions.

File

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

Class

LocaleTranslationUiTest
Tests the validation of translation strings and search results.

Namespace

Drupal\Tests\locale\Functional

Code

public function testStringTranslation() {
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser([
        'administer languages',
        'access administration pages',
    ]);
    // User to translate and delete string.
    $translate_user = $this->drupalCreateUser([
        'translate interface',
        'access administration pages',
    ]);
    // Code for the language.
    $langcode = 'xx';
    // The English name for the language. This will be translated.
    $name = 'cucurbitaceae';
    // This will be the translation of $name.
    $translation = $this->randomMachineName(16);
    $translation_to_en = $this->randomMachineName(16);
    // Add custom language.
    $this->drupalLogin($admin_user);
    $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');
    // Add string.
    t($name, [], [
        'langcode' => $langcode,
    ])->render();
    // Reset locale cache.
    $this->container
        ->get('string_translation')
        ->reset();
    $this->assertSession()
        ->responseContains('"edit-languages-' . $langcode . '-weight"');
    // Ensure that test language was added.
    $this->assertSession()
        ->pageTextContains($name);
    $this->drupalLogout();
    // Add a whitespace at the end of string to ensure it is found.
    $name_ws = $name . " ";
    // Search for the name and translate it.
    $this->drupalLogin($translate_user);
    $search = [
        'string' => $name_ws,
        'langcode' => $langcode,
        'translation' => 'untranslated',
    ];
    // Check that search finds the string as untranslated.
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextContains($name);
    // No t() here, it's surely not translated yet.
    $this->assertSession()
        ->pageTextContains($name);
    // Verify that there is no way to translate the string to English.
    $this->assertSession()
        ->optionNotExists('edit-langcode', 'en');
    $this->drupalLogout();
    $this->drupalLogin($admin_user);
    $this->drupalGet('admin/config/regional/language/edit/en');
    $this->submitForm([
        'locale_translate_english' => TRUE,
    ], 'Save language');
    $this->drupalLogout();
    $this->drupalLogin($translate_user);
    // Check that search finds the string as untranslated.
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextContains($name);
    // Assume this is the only result, given the random name.
    $textarea = $this->assertSession()
        ->elementExists('xpath', '//textarea');
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => $translation,
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($edit, 'Save translations');
    $this->assertSession()
        ->pageTextContains('The strings have been saved.');
    $url_bits = explode('?', $this->getUrl());
    $this->assertEquals(Url::fromRoute('locale.translate_page', [], [
        'absolute' => TRUE,
    ])->toString(), $url_bits[0], 'Correct page redirection.');
    $search = [
        'string' => $name,
        'langcode' => $langcode,
        'translation' => 'translated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextContains($translation);
    $search = [
        'string' => $name,
        'langcode' => 'en',
        'translation' => 'untranslated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $textarea = $this->assertSession()
        ->elementExists('xpath', '//textarea');
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => $translation_to_en,
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($edit, 'Save translations');
    $search = [
        'string' => $name,
        'langcode' => 'en',
        'translation' => 'translated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextContains($translation_to_en);
    $this->assertNotEquals($translation, $name);
    $this->assertEquals($translation, t($name, [], [
        'langcode' => $langcode,
    ]), 't() works for non-English.');
    // Refresh the locale() cache to get fresh data from t() below. We are in
    // the same HTTP request and therefore t() is not refreshed by saving the
    // translation above.
    $this->container
        ->get('string_translation')
        ->reset();
    // Now we should get the proper fresh translation from t().
    $this->assertNotEquals($translation_to_en, $name);
    $this->assertEquals($translation_to_en, t($name, [], [
        'langcode' => 'en',
    ]), 't() works for English.');
    $this->assertTrue(t($name, [], [
        'langcode' => LanguageInterface::LANGCODE_SYSTEM,
    ]) == $name, 't() works for LanguageInterface::LANGCODE_SYSTEM.');
    $search = [
        'string' => $name,
        'langcode' => 'en',
        'translation' => 'untranslated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextContains('No strings available.');
    // Test invalidation of 'rendered' cache tag after string translation.
    $this->drupalLogout();
    $this->drupalGet('xx/user/login');
    $this->assertSession()
        ->pageTextContains('Enter the password that accompanies your username.');
    $this->drupalLogin($translate_user);
    $search = [
        'string' => 'accompanies your username',
        'langcode' => $langcode,
        'translation' => 'untranslated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $textarea = $this->assertSession()
        ->elementExists('xpath', '//textarea');
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => 'Please enter your Llama username.',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($edit, 'Save translations');
    $this->drupalLogout();
    $this->drupalGet('xx/user/login');
    $this->assertSession()
        ->pageTextContains('Please enter your Llama username.');
    // Delete the language.
    $this->drupalLogin($admin_user);
    $path = 'admin/config/regional/language/delete/' . $langcode;
    // This a confirm form, we do not need any fields changed.
    $this->drupalGet($path);
    $this->submitForm([], 'Delete');
    $this->assertSession()
        ->pageTextContains("The {$name} ({$langcode}) language has been removed.");
    // Reload to remove $name.
    $this->drupalGet($path);
    // Verify that language is no longer found.
    $this->assertSession()
        ->statusCodeEquals(404);
    $this->drupalLogout();
    // Delete the string.
    $this->drupalLogin($translate_user);
    $search = [
        'string' => $name,
        'langcode' => 'en',
        'translation' => 'translated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    // Assume this is the only result, given the random name.
    $textarea = $this->assertSession()
        ->elementExists('xpath', '//textarea');
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => '',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($edit, 'Save translations');
    $this->assertSession()
        ->responseContains($name);
    $this->drupalLogin($translate_user);
    $search = [
        'string' => $name,
        'langcode' => 'en',
        'translation' => 'untranslated',
    ];
    $this->drupalGet('admin/config/regional/translate');
    $this->submitForm($search, 'Filter');
    $this->assertSession()
        ->pageTextNotContains('No strings available.');
}

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