View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class DynamicEntityReferenceConfigEntityBaseFieldTest extends EntityKernelTestBase {
protected $entityType = 'entity_test';
protected $referencedEntityTypes = [
'config_test',
];
protected $bundle = 'entity_test';
protected $fieldName = 'dynamic_references';
protected $state;
protected $configTestReference;
protected static $modules = [
'config_test',
'dynamic_entity_reference',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'config_test',
]);
$this
->installEntitySchema('entity_test_rev');
$this->state = $this->container
->get('state');
$this->state
->set('dynamic_entity_reference_entity_test_cardinality', 1);
$this->state
->set('dynamic_entity_reference_entity_test_entities', [
$this->entityType,
'config_test',
]);
$this->state
->set('dynamic_entity_reference_entity_test_exclude', [
$this->entityType,
]);
$this->configTestReference = $this->container
->get('entity_type.manager')
->getStorage('config_test')
->create([
'id' => mb_strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
'style' => mb_strtolower($this
->randomMachineName()),
]);
$this->configTestReference
->save();
}
public function testBaseField() {
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->configTestReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->configTestReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
}
public function testRevisionableBaseField() {
$this->state
->set('dynamic_entity_reference_entity_test_revisionable', TRUE);
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$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);
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->configTestReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->configTestReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
$entity
->save();
$referenced_entity = $this->container
->get('entity_type.manager')
->getStorage('config_test')
->create([
'id' => 'bar',
'label' => 'Bar',
'style' => 'foo',
]);
$referenced_entity
->save();
$entity->{$this->fieldName}->target_type = $referenced_entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $referenced_entity
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
$entity
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->load($entity
->id());
$referenced = $entity->{$this->fieldName}
->referencedEntities();
$this
->assertEquals(1, count($referenced));
$this
->assertEquals('bar', $referenced[0]
->id());
}
public function testMixedBaseField() {
$this->state
->set('dynamic_entity_reference_entity_test_entities', [
$this->entityType,
'config_test',
'entity_test_mul',
]);
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->configTestReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->configTestReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
$referenced_entity = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mul')
->create([
'type' => $this->bundle,
]);
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $referenced_entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $referenced_entity
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
}
public function testMixedRevisionableBaseField() {
$this->state
->set('dynamic_entity_reference_entity_test_revisionable', TRUE);
$this->state
->set('dynamic_entity_reference_entity_test_entities', [
$this->entityType,
'config_test',
'entity_test_mul',
]);
$this
->enableModules([
'dynamic_entity_reference_entity_test',
]);
$this
->installEntitySchema('entity_test_mul');
$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);
}
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->configTestReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->configTestReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
$entity
->save();
$referenced_entity = $this->container
->get('entity_type.manager')
->getStorage('entity_test_mul')
->create([
'type' => $this->bundle,
]);
$referenced_entity
->save();
$entity->{$this->fieldName}->target_type = $referenced_entity
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $referenced_entity
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
$entity
->save();
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->load($entity
->id());
$referenced = $entity->{$this->fieldName}
->referencedEntities();
$this
->assertEquals(1, count($referenced));
$this
->assertEquals($referenced_entity
->id(), $referenced[0]
->id());
}
}