function LocaleSourceTest::testSingularTerm

Tests translation of a locale singular term.

File

sources/locale/tests/src/Kernel/LocaleSourceTest.php, line 44

Class

LocaleSourceTest
Basic Locale Source tests.

Namespace

Drupal\Tests\tmgmt_locale\Kernel

Code

function testSingularTerm() {

  // Obtain one locale string with translation.
  $locale_object = \Drupal::database()
    ->query('SELECT * FROM {locales_source} WHERE source = :source LIMIT 1', array(
    ':source' => 'Hello World',
  ))
    ->fetchObject();
  $source_text = $locale_object->source;

  // Create the new job and job item.
  $job = $this
    ->createJob();
  $job->translator = $this->default_translator
    ->id();
  $job->settings = array();
  $job
    ->save();
  $item1 = $job
    ->addItem('locale', 'default', $locale_object->lid);

  // Check the structure of the imported data.
  $this
    ->assertEquals($locale_object->lid, $item1
    ->getItemId(), 'Locale Strings object correctly saved');
  $this
    ->assertEquals('Locale', $item1
    ->getSourceType());
  $this
    ->assertEquals('Hello World', $item1
    ->getSourceLabel());
  $job
    ->requestTranslation();
  foreach ($job
    ->getItems() as $item) {

    /* @var $item JobItemInterface */
    $item
      ->acceptTranslation();
    $this
      ->assertTrue($item
      ->isAccepted());

    // The source is now available in en and de.
    $this
      ->assertJobItemLangCodes($item, 'en', array(
      'en',
      'de',
    ));
  }

  // Check string translation.
  $expected_translation = $job
    ->getTargetLangcode() . '(' . $job
    ->getRemoteTargetLanguage() . '): ' . $source_text;
  $this
    ->assertTranslation($locale_object->lid, 'de', $expected_translation);

  // Translate the german translation to spanish.
  $target_langcode = 'es';
  $job = $this
    ->createJob('de', $target_langcode);
  $job->translator = $this->default_translator
    ->id();
  $job->settings = array();
  $job
    ->save();
  $item1 = $job
    ->addItem('locale', 'default', $locale_object->lid);
  $this
    ->assertEquals('Locale', $item1
    ->getSourceType());
  $this
    ->assertEquals($expected_translation, $item1
    ->getSourceLabel());
  $job
    ->requestTranslation();
  foreach ($job
    ->getItems() as $item) {

    /* @var $item JobItemInterface */
    $item
      ->acceptTranslation();
    $this
      ->assertTrue($item
      ->isAccepted());

    // The source should be now available for en, de and es languages.
    $this
      ->assertJobItemLangCodes($item, 'en', array(
      'en',
      'de',
      'es',
    ));
  }

  // Check string translation.
  $this
    ->assertTranslation($locale_object->lid, $target_langcode, $job
    ->getTargetLangcode() . ': ' . $expected_translation);
}