function ContentEntityCloneTest::testEntityKeysModifications

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testEntityKeysModifications()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testEntityKeysModifications()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/ContentEntityCloneTest.php \Drupal\KernelTests\Core\Entity\ContentEntityCloneTest::testEntityKeysModifications()

Tests modifications on entity keys of a cloned entity object.

File

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

Class

ContentEntityCloneTest
Tests proper cloning of content entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testEntityKeysModifications() : void {
    // Create a test entity with a translation, which will internally trigger
    // entity cloning for the new translation and create references for some of
    // the entity properties.
    $entity = EntityTestMulRev::create([
        'name' => 'original-name',
        'uuid' => 'original-uuid',
        'language' => 'en',
    ]);
    $entity->addTranslation('de');
    $entity->save();
    // Clone the entity.
    $clone = clone $entity;
    // Alter a non-translatable and a translatable entity key fields of the
    // cloned entity and assert that retrieving the value through the entity
    // keys local cache will be different for the cloned and the original
    // entity.
    // We first have to call the ::uuid() and ::label() method on the original
    // entity as it is going to cache the field values into the $entityKeys and
    // $translatableEntityKeys properties of the entity object and we want to
    // check that the cloned and the original entity aren't sharing the same
    // reference to those local cache properties.
    $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.