You are here

public function DynamicEntityReferenceItemTest::testEntityReferenceWithProperty in Dynamic Entity Reference 8.2

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

Tests entity auto create with property.

File

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

Class

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

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntityReferenceWithProperty() {

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

  // Now assign the unsaved term to the field.
  $entity->field_der->target_id = $term
    ->id();
  $entity->field_der->target_type = $term
    ->getEntityTypeId();
  $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());
}