View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\language\Entity\ConfigurableLanguage;
class ContentEntityCloneTest extends EntityKernelTestBase {
public static $modules = [
'language',
'entity_test',
];
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->installEntitySchema('entity_test_mul');
$this
->installEntitySchema('entity_test_mulrev');
}
public function testFieldEntityReferenceAfterClone() {
$user = $this
->createUser();
$entity = EntityTestMul::create([
'name' => $this
->randomString(),
'user_id' => $user
->id(),
'language' => 'en',
]);
$translation = $entity
->addTranslation('de');
$entity
->getFields();
$translation
->getFields();
$clone = clone $translation;
$this
->assertEqual($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()) {
$args = [
'%field_name' => $field_name,
'%langcode' => $langcode,
];
$this
->assertEqual($langcode, $field
->getEntity()
->language()
->getId(), new FormattableMarkup('Translatable field %field_name on translation %langcode has correct entity reference in translation %langcode after cloning.', $args));
$this
->assertSame($translation, $field
->getEntity(), new FormattableMarkup('Translatable field %field_name on translation %langcode has correct reference to the cloned entity object.', $args));
}
else {
$args = [
'%field_name' => $field_name,
'%langcode' => $langcode,
'%default_langcode' => $default_langcode,
];
$this
->assertEqual($default_langcode, $field
->getEntity()
->language()
->getId(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct entity reference in the default translation %default_langcode after cloning.', $args));
$this
->assertSame($translation
->getUntranslated(), $field
->getEntity(), new FormattableMarkup('Non translatable field %field_name on translation %langcode has correct reference to the cloned entity object in the default translation %default_langcode.', $args));
}
}
}
}
public function testEnforceIsNewOnClonedEntityTranslation() {
$entity = EntityTestMul::create([
'name' => $this
->randomString(),
'language' => 'en',
]);
$entity
->save();
$entity_translation = $entity
->addTranslation('de');
$entity
->save();
$this
->assertFalse($entity_translation
->isNew());
$clone = clone $entity_translation;
$this
->assertFalse($clone
->isNew());
$clone
->enforceIsNew();
$this
->assertTrue($clone
->isNew());
$this
->assertFalse($entity_translation
->isNew());
}
public function testClonedEntityFields() {
$user = $this
->createUser();
$entity = EntityTestMul::create([
'name' => $this
->randomString(),
'user_id' => $user
->id(),
'language' => 'en',
]);
$entity
->addTranslation('de');
$entity
->save();
$fields = array_keys($entity
->getFieldDefinitions());
$entity = $this
->reloadEntity($entity);
$clone = clone $entity;
$different_references = TRUE;
foreach ($fields as $field_name) {
if ($entity
->get($field_name) === $clone
->get($field_name)) {
$different_references = FALSE;
}
}
$this
->assertTrue($different_references, 'The entity object and the cloned entity object reference different field item list objects.');
$entity = $this
->reloadEntity($entity);
$entity
->getTranslation('de');
$clone = clone $entity;
$different_references = TRUE;
foreach ($fields as $field_name) {
if ($entity
->get($field_name) === $clone
->get($field_name)) {
$different_references = FALSE;
}
}
$this
->assertTrue($different_references, 'The entity object and the cloned entity object reference different field item list objects if the entity is cloned after an entity translation has been initialized.');
}
public function testNewRevisionOnCloneEntityTranslation() {
$entity = EntityTestMulRev::create([
'name' => $this
->randomString(),
'language' => 'en',
]);
$entity
->save();
$entity
->addTranslation('de');
$entity
->save();
$entity = EntityTestMulRev::load($entity
->id());
$entity_translation = $entity
->getTranslation('de');
$this
->assertFalse($entity_translation
->isNewRevision());
$clone = clone $entity_translation;
$this
->assertFalse($clone
->isNewRevision());
$clone
->setNewRevision();
$this
->assertTrue($clone
->isNewRevision());
$this
->assertFalse($entity_translation
->isNewRevision());
}
public function testEntityKeysModifications() {
$entity = EntityTestMulRev::create([
'name' => 'original-name',
'uuid' => 'original-uuid',
'language' => 'en',
]);
$entity
->addTranslation('de');
$entity
->save();
$clone = clone $entity;
$uuid_field_name = $entity
->getEntityType()
->getKey('uuid');
$this
->assertFalse($entity
->getFieldDefinition($uuid_field_name)
->isTranslatable());
$clone->{$uuid_field_name}->value = 'clone-uuid';
$this
->assertEquals('original-uuid', $entity
->uuid());
$this
->assertEquals('clone-uuid', $clone
->uuid());
$label_field_name = $entity
->getEntityType()
->getKey('label');
$this
->assertTrue($entity
->getFieldDefinition($label_field_name)
->isTranslatable());
$clone->{$label_field_name}->value = 'clone-name';
$this
->assertEquals('original-name', $entity
->label());
$this
->assertEquals('clone-name', $clone
->label());
}
public function testFieldValuesAfterSerialize() {
$entity = EntityTestMulRev::create([
'name' => 'original',
'language' => 'en',
]);
$entity
->addTranslation('de');
$entity
->save();
$clone = clone $entity;
$clone
->setName('clone');
serialize($entity);
serialize($clone);
$this
->assertEquals('original', $entity
->getName());
$this
->assertEquals('clone', $clone
->getName());
}
public function testDefaultRevision() {
$entity = EntityTestMulRev::create([
'name' => 'original',
'language' => 'en',
]);
$entity
->addTranslation('de');
$entity
->save();
$this
->assertTrue($entity
->isDefaultRevision());
$clone = clone $entity;
$clone
->isDefaultRevision(FALSE);
$this
->assertFalse($clone
->isDefaultRevision());
$this
->assertTrue($entity
->isDefaultRevision());
}
public function testEntityPropertiesModifications() {
$entity = EntityTestMulRev::create([
'name' => 'original',
'language' => 'en',
]);
$translation = $entity
->addTranslation('de');
$entity
->save();
$clone = clone $entity;
$reflection = new \ReflectionClass($entity);
$properties = $reflection
->getProperties(~\ReflectionProperty::IS_STATIC);
$translation_unique_properties = [
'activeLangcode',
'translationInitialize',
'fieldDefinitions',
'languages',
'langcodeKey',
'defaultLangcode',
'defaultLangcodeKey',
'revisionTranslationAffectedKey',
'validated',
'validationRequired',
'entityTypeId',
'typedData',
'cacheContexts',
'cacheTags',
'cacheMaxAge',
'_serviceIds',
'_entityStorages',
];
foreach ($properties as $property) {
$property
->setAccessible(TRUE);
$property
->setValue($entity, 'default-value');
$property
->setValue($translation, 'default-value');
$property
->setValue($clone, 'test-entity-cloning');
if ($property
->isStatic()) {
$this
->assertEquals('test-entity-cloning', $property
->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('test-entity-cloning', $property
->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('test-entity-cloning', $property
->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
}
else {
$this
->assertEquals('default-value', $property
->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('default-value', $property
->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('test-entity-cloning', $property
->getValue($clone), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
}
$property
->setValue($translation, 'test-translation-cloning');
if (in_array($property
->getName(), $translation_unique_properties)) {
$this
->assertEquals('default-value', $property
->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('test-translation-cloning', $property
->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
}
else {
$this
->assertEquals('test-translation-cloning', $property
->getValue($entity), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
$this
->assertEquals('test-translation-cloning', $property
->getValue($translation), (string) new FormattableMarkup('Entity property %property_name is not cloned properly.', [
'%property_name' => $property
->getName(),
]));
}
}
}
}