View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
class DynamicEntityReferenceFieldDependencyTest extends KernelTestBase {
protected static $modules = [
'dynamic_entity_reference',
'field',
'entity_test',
'user',
'dynamic_entity_reference_test_entity_provider',
];
protected $fieldStorage;
protected $entityTypeProviders = [];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this->fieldStorage = FieldStorageConfig::create([
'type' => 'dynamic_entity_reference',
'field_name' => strtolower($this
->randomMachineName()),
'entity_type' => 'entity_test',
]);
$entityTypeManager = $this->container
->get('entity_type.manager');
foreach ($entityTypeManager
->getDefinitions() as $entity_id => $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface) {
$provider = $entity_type
->getProvider();
if (!in_array($provider, $this->entityTypeProviders) && $provider !== 'core') {
$this->entityTypeProviders[] = $provider;
}
}
}
}
public function testFieldDependenciesAllEntities() {
$this->fieldStorage
->setSetting('exclude_entity_types', TRUE);
$this->fieldStorage
->setSetting('entity_type_ids', []);
$this->fieldStorage
->save();
$module_dependencies = $this->entityTypeProviders;
$module_dependencies[] = 'dynamic_entity_reference';
$this
->assertEqualsCanonicalizing([
'module' => $module_dependencies,
], $this->fieldStorage
->getDependencies());
}
public function testFieldDependenciesExclude() {
$this->fieldStorage
->setSetting('exclude_entity_types', TRUE);
$this->fieldStorage
->setSetting('entity_type_ids', [
'entity_test_provider',
]);
$this->fieldStorage
->save();
$module_dependencies = $this->entityTypeProviders;
$module_dependencies[] = 'dynamic_entity_reference';
$module_dependencies = array_diff($module_dependencies, [
'dynamic_entity_reference_test_entity_provider',
]);
$this
->assertEqualsCanonicalizing([
'module' => $module_dependencies,
], $this->fieldStorage
->getDependencies());
}
public function testFieldDependenciesStrict() {
$this->fieldStorage
->setSetting('exclude_entity_types', FALSE);
$this->fieldStorage
->setSetting('entity_type_ids', [
'entity_test_provider',
]);
$this->fieldStorage
->save();
$module_dependencies[] = 'entity_test';
$module_dependencies[] = 'dynamic_entity_reference_test_entity_provider';
$module_dependencies[] = 'dynamic_entity_reference';
$this
->assertEqualsCanonicalizing([
'module' => $module_dependencies,
], $this->fieldStorage
->getDependencies());
}
public function testFieldDependenciesNone() {
$this->fieldStorage
->setSetting('exclude_entity_types', FALSE);
$this->fieldStorage
->setSetting('entity_type_ids', []);
$this->fieldStorage
->save();
$module_dependencies[] = 'entity_test';
$module_dependencies[] = 'dynamic_entity_reference';
$this
->assertEqualsCanonicalizing([
'module' => $module_dependencies,
], $this->fieldStorage
->getDependencies());
}
}