function ContentEntityCloneTest::testFieldValuesAfterSerialize

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

Tests the field values after serializing an entity and its clone.

File

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

Class

ContentEntityCloneTest
Tests proper cloning of content entities.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFieldValuesAfterSerialize() {
    // 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',
        'language' => 'en',
    ]);
    $entity->addTranslation('de');
    $entity->save();
    // Clone the entity.
    $clone = clone $entity;
    // Alter the name field value of the cloned entity object.
    $clone->setName('clone');
    // Serialize the entity and the cloned object in order to destroy the field
    // objects and put the field values into the entity property $values, so
    // that on accessing a field again it will be newly created with the value
    // from the $values property.
    serialize($entity);
    serialize($clone);
    // Assert that the original and the cloned entity both have different names.
    $this->assertEquals('original', $entity->getName());
    $this->assertEquals('clone', $clone->getName());
}

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