public function DynamicEntityReferenceBaseFieldTest::testMultipleEntityReference in Dynamic Entity Reference 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php \Drupal\Tests\dynamic_entity_reference\Kernel\DynamicEntityReferenceBaseFieldTest::testMultipleEntityReference()
Tests the der field type for referencing multiple content entities.
File
- tests/
src/ Kernel/ DynamicEntityReferenceBaseFieldTest.php, line 214
Class
- DynamicEntityReferenceBaseFieldTest
- Tests for the dynamic entity reference base field.
Namespace
Drupal\Tests\dynamic_entity_reference\KernelCode
public function testMultipleEntityReference() {
\Drupal::state()
->set('dynamic_entity_reference_entity_test_with_two_base_fields', TRUE);
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$this
->installEntitySchema('entity_test_mul');
// Update entity_test schema.
$entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$mock_entity_type = $this
->prophesize(EntityTypeInterface::class);
$mock_entity_type
->id()
->willReturn('entity_test');
$field_storage_definitions = dynamic_entity_reference_entity_test_entity_base_field_info($mock_entity_type
->reveal());
foreach ($field_storage_definitions as $field_name => $field_storage_definition) {
$entity_definition_update_manager
->installFieldStorageDefinition($field_name, 'entity_test', 'dynamic_entity_reference_entity_test', $field_storage_definition);
}
// Create some test entities which link each other.
$referenced_entity = EntityTest::create();
$referenced_entity
->save();
$referenced_entity_mul = EntityTestMul::create();
$referenced_entity_mul
->save();
$entity = EntityTest::create();
$entity->dynamic_references[] = $referenced_entity;
$entity->der[] = $referenced_entity_mul;
$entity
->save();
// Loads an unchanged entity from the database.
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->loadUnchanged($entity
->id());
// Check references correctly for dynamic_references field.
$this
->assertEquals($entity->dynamic_references[0]->target_id, $referenced_entity
->id());
$this
->assertEquals($entity->dynamic_references[0]->target_type, $referenced_entity
->getEntityTypeId());
$this
->assertEquals($entity->dynamic_references[0]->entity
->getName(), $referenced_entity
->getName());
$this
->assertEquals($entity->dynamic_references[0]->entity
->id(), $referenced_entity
->id());
$this
->assertEquals($entity->dynamic_references[0]->entity
->uuid(), $referenced_entity
->uuid());
// Check references correctly for der field.
$this
->assertEquals($entity->der[0]->target_id, $referenced_entity_mul
->id());
$this
->assertEquals($entity->der[0]->target_type, $referenced_entity_mul
->getEntityTypeId());
$this
->assertEquals($entity->der[0]->entity
->getName(), $referenced_entity_mul
->getName());
$this
->assertEquals($entity->der[0]->entity
->id(), $referenced_entity_mul
->id());
$this
->assertEquals($entity->der[0]->entity
->uuid(), $referenced_entity_mul
->uuid());
$entity = EntityTestMul::create();
$entity->der[] = $referenced_entity;
$entity->dynamic_references[] = $referenced_entity_mul;
$entity
->save();
// Loads an unchanged entity from the database.
$entity = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mul')
->loadUnchanged($entity
->id());
// Check references correctly for dynamic_references field.
$this
->assertEquals($entity->dynamic_references[0]->target_id, $referenced_entity_mul
->id());
$this
->assertEquals($entity->dynamic_references[0]->target_type, $referenced_entity_mul
->getEntityTypeId());
$this
->assertEquals($entity->dynamic_references[0]->entity
->getName(), $referenced_entity_mul
->getName());
$this
->assertEquals($entity->dynamic_references[0]->entity
->id(), $referenced_entity_mul
->id());
$this
->assertEquals($entity->dynamic_references[0]->entity
->uuid(), $referenced_entity_mul
->uuid());
// Check references correctly for der field.
$this
->assertEquals($entity->der[0]->target_id, $referenced_entity
->id());
$this
->assertEquals($entity->der[0]->target_type, $referenced_entity
->getEntityTypeId());
$this
->assertEquals($entity->der[0]->entity
->getName(), $referenced_entity
->getName());
$this
->assertEquals($entity->der[0]->entity
->id(), $referenced_entity
->id());
$this
->assertEquals($entity->der[0]->entity
->uuid(), $referenced_entity
->uuid());
}