You are here

public function EntityReferenceItemTest::testEntitySaveOrder in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testEntitySaveOrder()
  2. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceItemTest::testEntitySaveOrder()

Tests saving order sequence doesn't matter.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceItemTest.php, line 337

Class

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

Namespace

Drupal\Tests\field\Kernel\EntityReference

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_test_taxonomy_term->entity = $term;
  $entity->name->value = $this
    ->randomMachineName();

  // Now get the field value.
  $value = $entity
    ->get('field_test_taxonomy_term');
  $this
    ->assertArrayNotHasKey('target_id', $value);
  $this
    ->assertNull($entity->field_test_taxonomy_term->target_id);

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

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

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