You are here

public function EntityReferenceItemTest::testEntitySaveOrder in Drupal 8

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

Test 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
    ->assertTrue(empty($value['target_id']));
  $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
    ->assertEqual($entity->field_test_taxonomy_term->entity
    ->id(), $term
    ->id());
}