You are here

public function LocaleTranslationUiTest::testStringSearch 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::testStringSearch()
  2. 10 core/modules/locale/tests/src/Functional/LocaleTranslationUiTest.php \Drupal\Tests\locale\Functional\LocaleTranslationUiTest::testStringSearch()

Tests translation search form.

File

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

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 testStringSearch() {

  // 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 = $this
    ->randomMachineName(16);

  // This will be the translation of $name.
  $translation = $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');
  $edit = [
    'predefined_langcode' => 'custom',
    'langcode' => 'yy',
    'label' => $this
      ->randomMachineName(16),
    '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
    ->drupalLogout();

  // Search for the name.
  $this
    ->drupalLogin($translate_user);
  $search = [
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'all',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');

  // pageTextContains() seems to remove the input field where $name always
  // could be found, so this is not a false assert. See how
  // pageTextNotContains succeeds later.
  $this
    ->assertSession()
    ->pageTextContains($name);

  // Ensure untranslated string doesn't appear if searching on 'only
  // translated strings'.
  $search = [
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // Ensure untranslated string appears if searching on 'only untranslated
  // strings'.
  $search = [
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextNotContains('No strings available.');

  // Add translation.
  // Assume this is the only result, given the random name.
  // We save the lid from the path.
  $textarea = $this
    ->assertSession()
    ->elementExists('xpath', '//textarea');
  $lid = $textarea
    ->getAttribute('name');
  $edit = [
    $lid => $translation,
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($edit, 'Save translations');

  // Ensure translated string does appear if searching on 'only
  // translated strings'.
  $search = [
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'translated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextNotContains('No strings available.');

  // Ensure translated source string doesn't appear if searching on 'only
  // untranslated strings'.
  $search = [
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // Ensure translated string doesn't appear if searching on 'only
  // untranslated strings'.
  $search = [
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // Ensure translated string does appear if searching on the custom language.
  $search = [
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'all',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextNotContains('No strings available.');

  // Ensure translated string doesn't appear if searching in System (English).
  $search = [
    'string' => $translation,
    'langcode' => 'yy',
    'translation' => 'all',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');

  // Search for a string that isn't in the system.
  $unavailable_string = $this
    ->randomMachineName(16);
  $search = [
    'string' => $unavailable_string,
    'langcode' => $langcode,
    'translation' => 'all',
  ];
  $this
    ->drupalGet('admin/config/regional/translate');
  $this
    ->submitForm($search, 'Filter');
  $this
    ->assertSession()
    ->pageTextContains('No strings available.');
}