public function DynamicEntityReferenceFieldTest::testReferencedEntitiesMixId in Dynamic Entity Reference 8.2
Tests referencing entities with string and int IDs.
File
- tests/
src/ Kernel/ DynamicEntityReferenceFieldTest.php, line 386
Class
- DynamicEntityReferenceFieldTest
- Tests for the dynamic entity reference field.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testReferencedEntitiesMixId() {
$field_name = 'entity_reference_mix_id';
$this
->installEntitySchema('entity_test_string_id');
// Create a field.
FieldStorageConfig::create([
'field_name' => $field_name,
'type' => 'dynamic_entity_reference',
'entity_type' => $this->entityType,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
$this->referencedEntityType,
'entity_test_string_id',
],
],
])
->save();
FieldConfig::create([
'field_name' => $field_name,
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'label' => 'Field test',
'settings' => [
'entity_test_string_id' => [
'handler' => "default:entity_test_string_id",
'handler_settings' => [
'target_bundles' => [
'entity_test_string_id' => 'entity_test_string_id',
],
],
],
$this->referencedEntityType => [
'handler' => 'default:' . $this->referencedEntityType,
'handler_settings' => [
'target_bundles' => [
$this->bundle => $this->bundle,
],
],
],
],
])
->save();
// Create the parent entity.
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
// Create the default target entity.
$target_entity = EntityTestStringId::create([
'id' => $this
->randomString(),
'type' => 'entity_test_string_id',
]);
$target_entity
->save();
$referenced_entity = $this->container
->get('entity_type.manager')
->getStorage($this->referencedEntityType)
->create([
'type' => $this->bundle,
]);
$referenced_entity
->save();
// Set the field value.
$entity->{$field_name}
->setValue([
[
'target_id' => $target_entity
->id(),
'target_type' => $target_entity
->getEntityTypeId(),
],
[
'target_id' => $referenced_entity
->id(),
'target_type' => $referenced_entity
->getEntityTypeId(),
],
]);
// Load the target entities using
// DynamicEntityReferenceFieldItemList::referencedEntities().
$entities = $entity->{$field_name}
->referencedEntities();
$this
->assertEquals($entities[0]
->id(), $target_entity
->id());
$this
->assertEquals($entities[1]
->id(), $referenced_entity
->id());
// Test that a string ID works as a default value and the field's config
// schema is correct.
$field = FieldConfig::loadByName($this->entityType, $this->bundle, $field_name);
$field
->setDefaultValue([
[
'target_id' => $target_entity
->id(),
'target_type' => $target_entity
->getEntityTypeId(),
],
[
'target_id' => $referenced_entity
->id(),
'target_type' => $referenced_entity
->getEntityTypeId(),
],
]);
$field
->save();
$this
->assertConfigSchema(\Drupal::service('config.typed'), 'field.field.' . $field
->id(), $field
->toArray());
// Test that the default value works.
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entities = $entity->{$field_name}
->referencedEntities();
$this
->assertEquals($entities[0]
->id(), $target_entity
->id());
$this
->assertEquals($entities[1]
->id(), $referenced_entity
->id());
}