You are here

public function LocaleTranslationUiTest::testUICustomizedStrings 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::testUICustomizedStrings()
  2. 10 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 541

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
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains($translation
    ->getString());

  // Submit the translations without changing the translation.
  $textarea = $this
    ->assertSession()
    ->elementExists('xpath', '//textarea');
  $lid = $textarea
    ->getAttribute('name');
  $edit = [
    $lid => $translation
      ->getString(),
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($edit, 'Save translations');

  // Ensure unchanged translation string does appear if searching
  // non-customized translation.
  $search = [
    'string' => $string
      ->getString(),
    'langcode' => 'de',
    'translation' => 'translated',
    'customized' => '0',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains($string
    ->getString());

  // Submit the translations with a new translation.
  $textarea = $this
    ->assertSession()
    ->elementExists('xpath', '//textarea');
  $lid = $textarea
    ->getAttribute('name');
  $edit = [
    $lid => $this
      ->randomMachineName(100),
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($edit, 'Save translations');

  // Ensure changed translation string does appear if searching customized
  // translation.
  $search = [
    'string' => $string
      ->getString(),
    'langcode' => 'de',
    'translation' => 'translated',
    'customized' => '1',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains($string
    ->getString());
}