You are here

function LocaleSourceTest::testRequestDataForSpecificLanguage in Translation Management Tool 8

+ * Test if the source is able to pull content in requested language. +

File

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

Class

LocaleSourceTest
Basic Locale Source tests.

Namespace

Drupal\Tests\tmgmt_locale\Kernel

Code

function testRequestDataForSpecificLanguage() {
  $this
    ->addLanguage('cs');
  $locale_object = \Drupal::database()
    ->query('SELECT * FROM {locales_source} WHERE source = :source LIMIT 1', array(
    ':source' => 'Hello World',
  ))
    ->fetchObject();
  $plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('locale');
  $reflection_plugin = new \ReflectionClass('\\Drupal\\tmgmt_locale\\Plugin\\tmgmt\\Source\\LocaleSource');
  $updateTranslation = $reflection_plugin
    ->getMethod('updateTranslation');
  $updateTranslation
    ->setAccessible(TRUE);
  $updateTranslation
    ->invoke($plugin, $locale_object->lid, 'de', 'de translation');

  // Create the new job and job item.
  $job = $this
    ->createJob('de', 'cs');
  $job
    ->save();
  $job
    ->addItem('locale', 'default', $locale_object->lid);
  $data = $job
    ->getData();
  $this
    ->assertEqual($data[1]['singular']['#text'], 'de translation');

  // Create new job item with a source language for which the translation
  // does not exit.
  $job = $this
    ->createJob('es', 'cs');
  $job
    ->save();
  try {
    $job
      ->addItem('locale', 'default', $locale_object->lid);
    $this
      ->fail('The job item should not be added as there is no translation for language "es"');
  } catch (\Exception $e) {
    $languages = \Drupal::languageManager()
      ->getLanguages();

    // @todo Job item id missing because it is not saved yet.
    $this
      ->assertEqual(t('Unable to load %language translation for the locale %id', array(
      '%language' => $languages['es']
        ->getName(),
      '%id' => $locale_object->lid,
    )), $e
      ->getMessage());
  }
}