function ContentEntityCloneTest::testFieldEntityReferenceAfterClone
Same name in other branches
- 9 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testFieldEntityReferenceAfterClone()
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testFieldEntityReferenceAfterClone()
- 11.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testFieldEntityReferenceAfterClone()
Tests if entity references on fields are still correct after cloning.
File
-
core/
tests/ Drupal/ KernelTests/ Core/ Entity/ ContentEntityCloneTest.php, line 39
Class
- ContentEntityCloneTest
- Tests proper cloning of content entities.
Namespace
Drupal\KernelTests\Core\EntityCode
public function testFieldEntityReferenceAfterClone() : void {
$user = $this->createUser();
// Create a test entity.
$entity = EntityTestMul::create([
'name' => $this->randomString(),
'user_id' => $user->id(),
'language' => 'en',
]);
$translation = $entity->addTranslation('de');
// Initialize the fields on the translation objects in order to check that
// they are properly cloned and have a reference to the cloned entity
// object and not to the original one.
$entity->getFields();
$translation->getFields();
$clone = clone $translation;
$this->assertEquals($entity->getTranslationLanguages(), $clone->getTranslationLanguages(), 'The entity and its clone have the same translation languages.');
$default_langcode = $entity->getUntranslated()
->language()
->getId();
foreach (array_keys($clone->getTranslationLanguages()) as $langcode) {
$translation = $clone->getTranslation($langcode);
foreach ($translation->getFields() as $field_name => $field) {
if ($field->getFieldDefinition()
->isTranslatable()) {
$this->assertEquals($langcode, $field->getEntity()
->language()
->getId(), "Translatable field {$field_name} on translation {$langcode} has correct entity reference in translation {$langcode} after cloning.");
$this->assertSame($translation, $field->getEntity(), "Translatable field {$field_name} on translation {$langcode} has correct reference to the cloned entity object.");
}
else {
$this->assertEquals($default_langcode, $field->getEntity()
->language()
->getId(), "Non translatable field {$field_name} on translation {$langcode} has correct entity reference in the default translation {$default_langcode} after cloning.");
$this->assertSame($translation->getUntranslated(), $field->getEntity(), "Non translatable field {$field_name} on translation {$langcode} has correct reference to the cloned entity object in the default translation {$default_langcode}.");
}
}
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.