You are here

public function EntityReferenceItemTest::testEntityAutoCreate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php \Drupal\field\Tests\EntityReference\EntityReferenceItemTest::testEntityAutoCreate()

Tests entity auto create.

File

core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php, line 255
Contains \Drupal\field\Tests\EntityReference\EntityReferenceItemTest.

Class

EntityReferenceItemTest
Tests the new entity API for the entity reference field type.

Namespace

Drupal\field\Tests\EntityReference

Code

public function testEntityAutoCreate() {

  // The term entity is unsaved here.
  $term = Term::create(array(
    '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_test_taxonomy_term->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 = \Drupal::entityManager()
    ->loadEntityByUuid($term
    ->getEntityTypeId(), $term
    ->uuid());
  $this
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term
    ->id());
}