// original object behave independently.
$name =
$default_langcode . '_' .
$this->
randomMachineName();
$entity->name->value =
$name;
$name_translated =
$langcode . '_' .
$this->
randomMachineName();
$translation =
$entity->
addTranslation($langcode);
$this->
assertTrue($translation->
isNewTranslation(), 'Newly added translations are marked as new.'
);
$this->
assertNotSame($entity,
$translation, 'The entity and the translation object differ from one another.'
);
$this->
assertTrue($entity->
hasTranslation($langcode), 'The new translation exists.'
);
$this->
assertEquals($langcode,
$translation->
language()->
getId(), 'The translation language matches the specified one.'
);
$this->
assertEquals($langcode,
$translation->
{$langcode_key}->value, 'The translation field language value matches the specified one.'
);
$this->
assertFalse($translation->
{$default_langcode_key}->value, 'The translation object is not the default one.'
);
$this->
assertEquals($default_langcode,
$translation->
getUntranslated()->
language()->
getId(), 'The original language can still be retrieved.'
);
$translation->name->value =
$name_translated;
$this->
assertEquals($name,
$entity->name->value, 'The original name is retained after setting a translated value.'
);
$entity->name->value =
$name;
$this->
assertEquals($name_translated,
$translation->name->value, 'The translated name is retained after setting the original value.'
);
// Save the translation and check that the expected hooks are fired.
$translation->
save();
$hooks =
$this->
getHooksInfo();
$this->
assertEquals($langcode,
$hooks['entity_translation_create'
], 'The generic entity translation creation hook has fired.'
);
$this->
assertEquals($langcode,
$hooks[$entity_type . '_translation_create'
], 'The entity-type-specific entity translation creation hook has fired.'
);