View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
class DynamicEntityReferenceConfigEntityTest extends EntityKernelTestBase {
protected $entityType = 'entity_test';
protected $referencedEntityTypes = [
'config_test',
];
protected $bundle = 'entity_test';
protected $fieldName = 'field_test';
protected $fieldStorage;
protected $fieldConfig;
protected $configTestReference;
protected $contentEntityReference;
protected static $modules = [
'config_test',
'dynamic_entity_reference',
];
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'config_test',
]);
$this
->installEntitySchema('entity_test_rev');
$this
->installEntitySchema('entity_test_string_id');
$this->configTestReference = $this->container
->get('entity_type.manager')
->getStorage('config_test')
->create([
'id' => 'foo',
'label' => 'Foo',
'style' => 'bar',
]);
$this->configTestReference
->save();
$this->contentEntityReference = $this->container
->get('entity_type.manager')
->getStorage('entity_test_rev')
->create([
'type' => $this->bundle,
]);
$this->contentEntityReference
->save();
}
protected function setUpField() {
$this->fieldStorage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'type' => 'dynamic_entity_reference',
'entity_type' => $this->entityType,
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => $this->referencedEntityTypes,
],
]);
$this->fieldStorage
->save();
$settings = [];
foreach ($this->referencedEntityTypes as $entity_type_id) {
$settings[$entity_type_id] = [
'handler' => "default:{$entity_type_id}",
'handler_settings' => [],
];
}
$this->fieldConfig = FieldConfig::create([
'field_name' => $this->fieldName,
'entity_type' => $this->entityType,
'bundle' => $this->bundle,
'label' => 'Field test',
'settings' => $settings,
]);
$this->fieldConfig
->save();
}
public function testConfigurableField() {
$this
->setUpField();
$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
->assertEquals($violations
->count(), 0, 'Validation passes.');
}
public function testMixedConfigurableField() {
$this->referencedEntityTypes[] = 'entity_test_rev';
$this
->setUpField();
$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 = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->contentEntityReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->contentEntityReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
}
public function testMixedConfigurableFieldStringId() {
$this->referencedEntityTypes[] = 'entity_test_string_id';
$this
->setUpField();
$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_string_id')
->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 testMixedConfigurableFieldMixedIds() {
$this->referencedEntityTypes[] = 'entity_test_rev';
$this->referencedEntityTypes[] = 'entity_test_string_id';
$this
->setUpField();
$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_string_id')
->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.');
$entity = $this->container
->get('entity_type.manager')
->getStorage($this->entityType)
->create([
'type' => $this->bundle,
]);
$entity->{$this->fieldName}->target_type = $this->contentEntityReference
->getEntityTypeId();
$entity->{$this->fieldName}->target_id = $this->contentEntityReference
->id();
$violations = $entity->{$this->fieldName}
->validate();
$this
->assertEmpty($violations
->count(), 'Validation passes.');
}
}