function LocaleSourceTest::testRequestDataForSpecificLanguage

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

File

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

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
    ->assertEquals('de translation', $data[1]['singular']['#text']);

  // 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
      ->assertEquals((string) t('Unable to load %language translation for the locale %id', [
      '%language' => $languages['es']
        ->getName(),
      '%id' => $locale_object->lid,
    ]), $e
      ->getMessage());
  }
}