public function EntityReferenceItemTest::testEntitySaveOrder in Drupal 9
Same name and namespace in other branches
- 8 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\EntityReferenceCode
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
->assertEquals($term
->id(), $entity->field_test_taxonomy_term->entity
->id());
}