View source
<?php
namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
class DynamicEntityReferenceSettingsTest extends KernelTestBase {
protected static $modules = [
'node',
'taxonomy',
'field',
'user',
'text',
'dynamic_entity_reference',
'entity_test',
'system',
];
protected $nodeType;
protected $vocabulary;
protected $customBundle;
protected function setUp() : void {
parent::setup();
$this
->installEntitySchema('node');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('entity_test');
$this->nodeType = NodeType::create([
'type' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->nodeType
->save();
$this->vocabulary = Vocabulary::create([
'vid' => mb_strtolower($this
->randomMachineName()),
'name' => $this
->randomString(),
]);
$this->vocabulary
->save();
$this->customBundle = 'test_bundle_' . mb_strtolower($this
->randomMachineName());
entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
}
public function testConfigTargetBundleDeletion() {
$name = mb_strtolower($this
->randomMachineName());
$label = $this
->randomString();
$vid = $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$vid => $vid,
],
];
FieldStorageConfig::create([
'field_name' => $name,
'type' => 'dynamic_entity_reference',
'entity_type' => 'node',
'cardinality' => 1,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'taxonomy_term',
],
],
])
->save();
FieldConfig::create([
'field_name' => $name,
'entity_type' => 'node',
'bundle' => $this->nodeType
->id(),
'label' => $label,
'settings' => [
'taxonomy_term' => [
'handler' => 'default:taxonomy_term',
'handler_settings' => $handler_settings,
],
],
])
->save();
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$settings = $field_config
->getSettings();
$actual_handler_settings = $settings['taxonomy_term']['handler_settings'];
$this
->assertEquals($handler_settings, $actual_handler_settings);
$this->vocabulary
->delete();
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$settings = $field_config
->getSettings();
$handler_settings = $settings['taxonomy_term']['handler_settings'];
$this
->assertTrue(empty($handler_settings['target_bundles']));
}
public function testCustomTargetBundleDeletion() {
$name = mb_strtolower($this
->randomMachineName());
$label = $this
->randomString();
$handler_settings = [
'target_bundles' => [
$this->customBundle => $this->customBundle,
],
];
FieldStorageConfig::create([
'field_name' => $name,
'type' => 'dynamic_entity_reference',
'entity_type' => 'node',
'cardinality' => 1,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'entity_test',
],
],
])
->save();
FieldConfig::create([
'field_name' => $name,
'entity_type' => 'node',
'bundle' => $this->nodeType
->id(),
'label' => $label,
'settings' => [
'entity_test' => [
'handler' => 'default:entity_test',
'handler_settings' => $handler_settings,
],
],
])
->save();
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$settings = $field_config
->getSettings();
$actual_handler_settings = $settings['entity_test']['handler_settings'];
$this
->assertEquals($handler_settings, $actual_handler_settings);
entity_test_delete_bundle($this->customBundle, 'entity_test');
$field_config = FieldConfig::loadByName('node', $this->nodeType
->id(), $name);
$settings = $field_config
->getSettings();
$handler_settings = $settings['entity_test']['handler_settings'];
$this
->assertTrue(empty($handler_settings['target_bundles']));
}
}