You are here

public function ContentEntityCloneTest::testClonedEntityFields in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testClonedEntityFields()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testClonedEntityFields()

Tests if the entity fields are properly cloned.

File

core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php, line 107

Class

ContentEntityCloneTest
Tests proper cloning of content entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testClonedEntityFields() {
  $user = $this
    ->createUser();

  // Create a test entity.
  $entity = EntityTestMul::create([
    'name' => $this
      ->randomString(),
    'user_id' => $user
      ->id(),
    'language' => 'en',
  ]);
  $entity
    ->addTranslation('de');
  $entity
    ->save();
  $fields = array_keys($entity
    ->getFieldDefinitions());

  // Reload the entity, clone it and check that both entity objects reference
  // different field instances.
  $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.');

  // Reload the entity, initialize one translation, clone it and check that
  // both entity objects reference different field instances.
  $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.');
}