You are here

public function TranslationTest::testClearOutValues in Feeds 8.3

Tests if values are cleared out when importing empty values.

When importing empty values for a specific language, it is expected that the values for that translation are getting emptied.

File

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

Class

TranslationTest
Test for the entity field translation.

Namespace

Drupal\Tests\feeds\Kernel\Feeds\Target

Code

public function testClearOutValues() {

  // 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 and Dutch.
  $this
    ->addMappings($this
    ->getMappingsInLanguage('es', '_es'));
  $this
    ->addMappings($this
    ->getMappingsInLanguage('nl', '_nl'));

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

  // Import file that has items with both Spanish and Dutch field values.
  $feed = $this
    ->importContent($this
    ->resourcesPath() . '/csv/translation/content_es_nl.csv');
  $this
    ->assertNodeCount(1);

  // Assert that node 1 has translations for both languages.
  $node = Node::load(1);
  $this
    ->assertTrue($node
    ->hasTranslation('es'));
  $this
    ->assertTrue($node
    ->hasTranslation('nl'));

  // Now import a file where the Dutch remained, but the Spanish values were
  // removed.
  $feed
    ->setSource($this
    ->resourcesPath() . '/csv/translation/content_es_nl_empty.csv');
  $feed
    ->save();
  $feed
    ->import();

  // Check that the Spanish values are gone, but the Dutch values are still
  // there.
  $node = $this
    ->reloadEntity($node);
  $spanish = $node
    ->getTranslation('es');
  $dutch = $node
    ->getTranslation('nl');
  $fields = [
    'field_alpha' => 'value',
    'field_tags' => 'target_id',
  ];
  foreach ($fields as $field_name => $property) {
    $this
      ->assertEmpty($spanish->{$field_name}->{$property});
  }

  // Inspect availability of Dutch values.
  $this
    ->assertEquals('HALLO WERELD', $dutch->title->value);
  $this
    ->assertEquals('Dit is de bodytekst.', $dutch->field_alpha->value);
  $referenced_entities = $dutch->field_tags
    ->referencedEntities();
  $first_tag = reset($referenced_entities);
  $this
    ->assertEquals('Taxonomieterm', $first_tag->name->value);
}