3 namespace Drupal\Tests\field\Kernel\EntityReference;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\taxonomy\Entity\Vocabulary;
13 * Tests entity reference field settings.
17 class EntityReferenceSettingsTest extends KernelTestBase {
19 use EntityReferenceTestTrait;
24 public static $modules = ['node', 'taxonomy', 'field', 'user', 'text', 'entity_reference', 'entity_test', 'system'];
29 * @var \Drupal\node\Entity\NodeType
36 * @var \Drupal\taxonomy\Entity\Vocabulary
38 protected $vocabulary;
41 * An entity bundle that is not stored as a configuration entity.
45 protected $customBundle;
50 protected function setUp() {
53 $this->installEntitySchema('node');
54 $this->installEntitySchema('taxonomy_term');
55 $this->installEntitySchema('entity_test');
57 $this->nodeType = NodeType::create([
58 'type' => Unicode::strtolower($this->randomMachineName()),
59 'name' => $this->randomString(),
61 $this->nodeType->save();
63 $this->vocabulary = Vocabulary::create([
64 'vid' => Unicode::strtolower($this->randomMachineName()),
65 'name' => $this->randomString(),
67 $this->vocabulary->save();
69 // Create a custom bundle.
70 $this->customBundle = 'test_bundle_' . Unicode::strtolower($this->randomMachineName());
71 entity_test_create_bundle($this->customBundle, NULL, 'entity_test');
75 * Tests that config bundle deletions are mirrored in field config settings.
77 public function testConfigTargetBundleDeletion() {
78 // Attach an entity reference field to $this->nodeType.
79 $name = Unicode::strtolower($this->randomMachineName());
80 $label = $this->randomString();
81 $vid = $this->vocabulary->id();
82 $handler_settings = ['target_bundles' => [$vid => $vid]];
83 $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'taxonomy_term', 'default', $handler_settings);
85 // Check that the 'target_bundle' setting contains the vocabulary.
86 $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
87 $actual_handler_settings = $field_config->getSetting('handler_settings');
88 $this->assertEqual($handler_settings, $actual_handler_settings);
90 // Delete the vocabulary.
91 $this->vocabulary->delete();
93 // Check that the deleted vocabulary is no longer present in the
94 // 'target_bundles' field setting.
95 $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
96 $handler_settings = $field_config->getSetting('handler_settings');
97 $this->assertTrue(empty($handler_settings['target_bundles']));
101 * Tests that deletions of custom bundles are mirrored in field settings.
103 public function testCustomTargetBundleDeletion() {
104 // Attach an entity reference field to $this->nodeType.
105 $name = Unicode::strtolower($this->randomMachineName());
106 $label = $this->randomString();
107 $handler_settings = ['target_bundles' => [$this->customBundle => $this->customBundle]];
108 $this->createEntityReferenceField('node', $this->nodeType->id(), $name, $label, 'entity_test', 'default', $handler_settings);
110 // Check that the 'target_bundle' setting contains the custom bundle.
111 $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
112 $actual_handler_settings = $field_config->getSetting('handler_settings');
113 $this->assertEqual($handler_settings, $actual_handler_settings);
115 // Delete the custom bundle.
116 entity_test_delete_bundle($this->customBundle, 'entity_test');
118 // Check that the deleted bundle is no longer present in the
119 // 'target_bundles' field setting.
120 $field_config = FieldConfig::loadByName('node', $this->nodeType->id(), $name);
121 $handler_settings = $field_config->getSetting('handler_settings');
122 $this->assertTrue(empty($handler_settings['target_bundles']));