function LocaleTranslationUiTest::testUICustomizedStrings

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

Tests that only changed strings are saved customized when edited.

File

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

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 testUICustomizedStrings() {
    $user = $this->drupalCreateUser([
        'translate interface',
        'administer languages',
        'access administration pages',
    ]);
    $this->drupalLogin($user);
    ConfigurableLanguage::createFromLangcode('de')->save();
    // Create test source string.
    $string = $this->container
        ->get('locale.storage')
        ->createString([
        'source' => $this->randomMachineName(100),
        'context' => $this->randomMachineName(20),
    ])
        ->save();
    // Create translation for new string and save it as non-customized.
    $translation = $this->container
        ->get('locale.storage')
        ->createTranslation([
        'lid' => $string->lid,
        'language' => 'de',
        'translation' => $this->randomMachineName(100),
        'customized' => 0,
    ])
        ->save();
    // Reset locale cache.
    $this->container
        ->get('string_translation')
        ->reset();
    // Ensure non-customized translation string does appear if searching
    // non-customized translation.
    $search = [
        'string' => $string->getString(),
        'langcode' => 'de',
        'translation' => 'translated',
        'customized' => '0',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertText($translation->getString(), 'Translation is found in search result.');
    // Submit the translations without changing the translation.
    $textarea = current($this->xpath('//textarea'));
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => $translation->getString(),
    ];
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
    // Ensure unchanged translation string does appear if searching
    // non-customized translation.
    $search = [
        'string' => $string->getString(),
        'langcode' => 'de',
        'translation' => 'translated',
        'customized' => '0',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertText($string->getString(), 'Translation is not marked as customized.');
    // Submit the translations with a new translation.
    $textarea = current($this->xpath('//textarea'));
    $lid = $textarea->getAttribute('name');
    $edit = [
        $lid => $this->randomMachineName(100),
    ];
    $this->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));
    // Ensure changed translation string does appear if searching customized
    // translation.
    $search = [
        'string' => $string->getString(),
        'langcode' => 'de',
        'translation' => 'translated',
        'customized' => '1',
    ];
    $this->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
    $this->assertText($string->getString(), "Translation is marked as customized.");
}

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