Tests using entity fields of the entity reference field type.
public function testEntityReferenceItem() {
$tid = $this->term
->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = entity_create('entity_test', array());
$entity->field_test_taxonomy->target_id = $tid;
$entity->name->value = $this
->randomName();
$entity
->save();
$entity = entity_load('entity_test', $entity
->id());
$this
->assertTrue($entity->field_test_taxonomy instanceof FieldInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_test_taxonomy->target_id, $tid);
$this
->assertEqual($entity->field_test_taxonomy->entity->name->value, $this->term->name->value);
$this
->assertEqual($entity->field_test_taxonomy->entity
->id(), $tid);
$this
->assertEqual($entity->field_test_taxonomy->entity
->uuid(), $this->term
->uuid());
// Change the name of the term via the reference.
$new_name = $this
->randomName();
$entity->field_test_taxonomy->entity->name = $new_name;
$entity->field_test_taxonomy->entity
->save();
// Verify it is the correct name.
$term = entity_load('taxonomy_term', $tid);
$this
->assertEqual($term->name->value, $new_name);
// Make sure the computed term reflects updates to the term id.
$term2 = entity_create('taxonomy_term', array(
'name' => $this
->randomName(),
'vid' => $this->term
->bundle(),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
));
$term2
->save();
$entity->field_test_taxonomy->target_id = $term2
->id();
$this
->assertEqual($entity->field_test_taxonomy->entity
->id(), $term2
->id());
$this
->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value);
// Delete terms so we have nothing to reference and try again
$term
->delete();
$term2
->delete();
$entity = entity_create('entity_test', array(
'name' => $this
->randomName(),
));
$entity
->save();
}