You are here

public function DynamicEntityReferenceItemTest::testEntityAutoCreate in Dynamic Entity Reference 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/DynamicEntityReferenceItemTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceItemTest::testEntityAutoCreate()

Tests entity auto create.

File

tests/src/Kernel/DynamicEntityReferenceItemTest.php, line 262

Class

DynamicEntityReferenceItemTest
Tests the new entity API for the dynamic entity reference field type.

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityAutoCreate() {

  // The term entity is unsaved here.
  $term = Term::create([
    'name' => $this
      ->randomMachineName(),
    'vid' => $this->term
      ->bundle(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $entity = EntityTest::create();

  // Now assign the unsaved term to the field.
  $entity->field_der->entity = $term;
  $entity->name->value = $this
    ->randomMachineName();

  // This is equal to storing an entity to tempstore or cache and retrieving
  // it back. An example for this is node preview.
  $entity = serialize($entity);
  $entity = unserialize($entity);

  // And then the entity.
  $entity
    ->save();
  $term = $this->container
    ->get('entity.repository')
    ->loadEntityByUuid($term
    ->getEntityTypeId(), $term
    ->uuid());
  $this
    ->assertEquals($entity->field_der->entity
    ->id(), $term
    ->id());
}