You are here

public function TranslationTest::testMappingFieldsAnotherLanguageImport in Feeds 8.3

Tests importing values for two languages separately.

This tests configures the feed type to first import the Spanish values. After importing the Spanish content, the language setting on each target is changed from Spanish to Dutch (for those that had it). It is expected that importing the Dutch content in this case, does not clear out the Spanish values, because there is not being mapped to fields in that language anymore.

File

tests/src/Kernel/Feeds/Target/TranslationTest.php, line 213

Class

TranslationTest
Test for the entity field translation.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testMappingFieldsAnotherLanguageImport() {

  // Set to update existing nodes.
  $configuration = $this->feedType
    ->getProcessor()
    ->getConfiguration();
  $configuration['update_existing'] = ProcessorInterface::UPDATE_EXISTING;
  $this->feedType
    ->getProcessor()
    ->setConfiguration($configuration);

  // Add mappings for Spanish.
  $this
    ->addMappings($this
    ->getMappingsInLanguage('es'));

  // And save the feed type to save all configuration changes.
  $this->feedType
    ->save();

  // Import Spanish content.
  $this
    ->importContent($this
    ->resourcesPath() . '/csv/translation/content_es.csv');
  $this
    ->assertNodeCount(1);

  // Assert that Spanish values were created.
  $node = Node::load(1);
  $this
    ->assertTrue($node
    ->hasTranslation('es'));
  $spanish_translation = $node
    ->getTranslation('es');
  $this
    ->assertEquals('HOLA MUNDO', $spanish_translation->title->value);
  $this
    ->assertEquals('Este es el texto del cuerpo.', $spanish_translation->field_alpha->value);

  // Change the feed type to import Dutch values instead.
  $mappings = $this->feedType
    ->getMappings();
  foreach ($mappings as $delta => &$mapping) {
    if (isset($mapping['settings']['language']) && $mapping['settings']['language'] == 'es') {
      $mapping['settings']['language'] = 'nl';

      // Change configuration on the target plugin as well.
      $this->feedType
        ->getTargetPlugin($delta)
        ->setConfiguration($mapping['settings']);
    }
  }
  $this->feedType
    ->setMappings($mappings);
  $this->feedType
    ->save();

  // Import Dutch content.
  $this
    ->importContent($this
    ->resourcesPath() . '/csv/translation/content_nl.csv');
  $this
    ->assertNodeCount(1);

  // Reload node and check Dutch values.
  $node = $this
    ->reloadEntity($node);
  $this
    ->assertTrue($node
    ->hasTranslation('nl'));
  $dutch_translation = $node
    ->getTranslation('nl');
  $this
    ->assertEquals('HALLO WERELD', $dutch_translation->title->value);
  $this
    ->assertEquals('Dit is de bodytekst.', $dutch_translation->field_alpha->value);

  // Ensure that the Spanish translation still exists.
  $this
    ->assertTrue($node
    ->hasTranslation('es'));
  $spanish_translation = $node
    ->getTranslation('es');
  $this
    ->assertEquals('HOLA MUNDO', $spanish_translation->title->value);
  $this
    ->assertEquals('Este es el texto del cuerpo.', $spanish_translation->field_alpha->value);
}