You are here

public function DynamicEntityReferenceItemTest::testEntitySaveOrder 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::testEntitySaveOrder()

Tests saving order sequence doesn't matter.

File

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

Class

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

Namespace

Drupal\Tests\dynamic_entity_reference\Kernel

Code

public function testEntitySaveOrder() {

  // 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();

  // Now get the field value.
  $value = $entity
    ->get('field_der');
  $this
    ->assertTrue(empty($value['target_id']));
  $this
    ->assertTrue(!isset($entity->field_der->target_id));

  // And then set it.
  $entity->field_der = $value;

  // Now save the term.
  $term
    ->save();

  // And then the entity.
  $entity
    ->save();
  $this
    ->assertEquals($entity->field_der->entity
    ->id(), $term
    ->id());
}