public function TranslationTest::testImportTranslationForExistingNode in Feeds 8.3
Tests importing a translation for an existing node.
This test creates a node with a Dutch translation. It then imports a translation for an other language: Spanish. It is then expected that the Dutch translation is kept.
File
- tests/
src/ Kernel/ Feeds/ Target/ TranslationTest.php, line 343
Class
- TranslationTest
- Test for the entity field translation.
Namespace
Drupal\Tests\feeds\Kernel\Feeds\TargetCode
public function testImportTranslationForExistingNode() {
// Create a node with Dutch values. Set a value for feed item's
// guid, even though the node was not originally imported with Feeds.
Node::create([
'type' => 'article',
'title' => 'HALLO WERELD',
'field_alpha' => 'Dit is de bodytekst.',
'langcode' => 'nl',
'feeds_item' => [
'guid' => 1,
'target_id' => 1,
],
])
->save();
// Set to update existing nodes.
$configuration = $this->feedType
->getProcessor()
->getConfiguration();
$configuration['update_existing'] = ProcessorInterface::UPDATE_EXISTING;
$this->feedType
->getProcessor()
->setConfiguration($configuration);
// Remove mapping to 'normal' title.
$this->feedType
->removeMapping(1);
// And add Spanish mappings.
$this
->addMappings($this
->getMappingsInLanguage('es'));
$this->feedType
->save();
// Update this item with Feeds.
$this
->importContent($this
->resourcesPath() . '/csv/translation/content_es.csv');
$this
->assertNodeCount(1);
// Assert that a Spanish translation was 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);
$this
->assertEquals($node->uid->value, $spanish_translation->uid->value);
$this
->assertNotEmpty($spanish_translation->field_tags
->referencedEntities());
$spanish_referenced_entities = $spanish_translation->field_tags
->referencedEntities();
$spanish_translation_first_tag = reset($spanish_referenced_entities);
$this
->assertEquals('Termino de taxonomía', $spanish_translation_first_tag->name->value);
// Assert that Dutch values still exist.
$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);
}