You are here

public function LocaleTranslationUiTest::testStringSearch in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/src/Tests/LocaleTranslationUiTest.php \Drupal\locale\Tests\LocaleTranslationUiTest::testStringSearch()

Tests translation search form.

File

core/modules/locale/src/Tests/LocaleTranslationUiTest.php, line 333
Contains \Drupal\locale\Tests\LocaleTranslationUiTest.

Class

LocaleTranslationUiTest
Adds a new locale and translates its name. Checks the validation of translation strings and search results.

Namespace

Drupal\locale\Tests

Code

public function testStringSearch() {

  // User to add and remove language.
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer languages',
    'access administration pages',
  ));

  // User to translate and delete string.
  $translate_user = $this
    ->drupalCreateUser(array(
    '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 = array(
    'predefined_langcode' => 'custom',
    'langcode' => $langcode,
    'label' => $name,
    'direction' => LanguageInterface::DIRECTION_LTR,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
  $edit = array(
    'predefined_langcode' => 'custom',
    'langcode' => 'yy',
    'label' => $this
      ->randomMachineName(16),
    'direction' => LanguageInterface::DIRECTION_LTR,
  );
  $this
    ->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));

  // Add string.
  t($name, array(), array(
    'langcode' => $langcode,
  ))
    ->render();

  // Reset locale cache.
  $this->container
    ->get('string_translation')
    ->reset();
  $this
    ->drupalLogout();

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

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

  // Ensure untranslated string doesn't appear if searching on 'only
  // translated strings'.
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'translated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), "Search didn't find the string.");

  // Ensure untranslated string appears if searching on 'only untranslated
  // strings'.
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), 'Search found the string.');

  // Add translation.
  // Assume this is the only result, given the random name.
  // We save the lid from the path.
  $textarea = current($this
    ->xpath('//textarea'));
  $lid = (string) $textarea[0]['name'];
  $edit = array(
    $lid => $translation,
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $edit, t('Save translations'));

  // Ensure translated string does appear if searching on 'only
  // translated strings'.
  $search = array(
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'translated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), 'Search found the translation.');

  // Ensure translated source string doesn't appear if searching on 'only
  // untranslated strings'.
  $search = array(
    'string' => $name,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), "Search didn't find the source string.");

  // Ensure translated string doesn't appear if searching on 'only
  // untranslated strings'.
  $search = array(
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'untranslated',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), "Search didn't find the translation.");

  // Ensure translated string does appear if searching on the custom language.
  $search = array(
    'string' => $translation,
    'langcode' => $langcode,
    'translation' => 'all',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertNoText(t('No strings available.'), 'Search found the translation.');

  // Ensure translated string doesn't appear if searching in System (English).
  $search = array(
    'string' => $translation,
    'langcode' => 'yy',
    'translation' => 'all',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), "Search didn't find the translation.");

  // Search for a string that isn't in the system.
  $unavailable_string = $this
    ->randomMachineName(16);
  $search = array(
    'string' => $unavailable_string,
    'langcode' => $langcode,
    'translation' => 'all',
  );
  $this
    ->drupalPostForm('admin/config/regional/translate', $search, t('Filter'));
  $this
    ->assertText(t('No strings available.'), "Search didn't find the invalid string.");
}