diff --git a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php index c36c0bd..0144eab 100644 --- a/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php +++ b/core/modules/field/src/Tests/EntityReference/EntityReferenceItemTest.php @@ -11,6 +11,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Language\LanguageInterface; +use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Tests\FieldUnitTestBase; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; @@ -194,6 +195,30 @@ public function testConfigEntityReferenceItem() { } /** + * Tests entity auto create. + */ + 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()); + } + + /** * Test saving order sequence doesn't matter. */ public function testEntitySaveOrder() {