3 namespace Drupal\Tests\field_ui\Kernel;
5 use Drupal\Core\Entity\Entity\EntityFormDisplay;
6 use Drupal\Core\Entity\Entity\EntityFormMode;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\KernelTestBase;
12 * Tests the entity display configuration entities.
16 class EntityFormDisplayTest extends KernelTestBase {
23 public static $modules = ['field_ui', 'field', 'entity_test', 'field_test', 'user', 'text'];
25 protected function setUp() {
30 * Tests entity_get_form_display().
32 public function testEntityGetFromDisplay() {
33 // Check that entity_get_form_display() returns a fresh object when no
34 // configuration entry exists.
35 $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
36 $this->assertTrue($form_display->isNew());
38 // Add some components and save the display.
39 $form_display->setComponent('component_1', ['weight' => 10])
42 // Check that entity_get_form_display() returns the correct object.
43 $form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
44 $this->assertFalse($form_display->isNew());
45 $this->assertEqual($form_display->id(), 'entity_test.entity_test.default');
46 $this->assertEqual($form_display->getComponent('component_1'), ['weight' => 10, 'settings' => [], 'third_party_settings' => [], 'region' => 'content']);
50 * Tests the behavior of a field component within an EntityFormDisplay object.
52 public function testFieldComponent() {
53 // Create a field storage and a field.
54 $field_name = 'test_field';
55 $field_storage = FieldStorageConfig::create([
56 'field_name' => $field_name,
57 'entity_type' => 'entity_test',
58 'type' => 'test_field',
60 $field_storage->save();
61 $field = FieldConfig::create([
62 'field_storage' => $field_storage,
63 'bundle' => 'entity_test',
67 $form_display = EntityFormDisplay::create([
68 'targetEntityType' => 'entity_test',
69 'bundle' => 'entity_test',
73 // Check that providing no options results in default values being used.
74 $form_display->setComponent($field_name);
75 $field_type_info = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_storage->getType());
76 $default_widget = $field_type_info['default_widget'];
77 $widget_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings($default_widget);
80 'type' => $default_widget,
81 'settings' => $widget_settings,
82 'third_party_settings' => [],
84 $this->assertEqual($form_display->getComponent($field_name), $expected);
86 // Check that the getWidget() method returns the correct widget plugin.
87 $widget = $form_display->getRenderer($field_name);
88 $this->assertEqual($widget->getPluginId(), $default_widget);
89 $this->assertEqual($widget->getSettings(), $widget_settings);
91 // Check that the widget is statically persisted, by assigning an
92 // arbitrary property and reading it back.
93 $random_value = $this->randomString();
94 $widget->randomValue = $random_value;
95 $widget = $form_display->getRenderer($field_name);
96 $this->assertEqual($widget->randomValue, $random_value);
98 // Check that changing the definition creates a new widget.
99 $form_display->setComponent($field_name, [
100 'type' => 'field_test_multiple',
102 $widget = $form_display->getRenderer($field_name);
103 $this->assertEqual($widget->getPluginId(), 'test_field_widget');
104 $this->assertFalse(isset($widget->randomValue));
106 // Check that specifying an unknown widget (e.g. case of a disabled module)
107 // gets stored as is in the display, but results in the default widget being
109 $form_display->setComponent($field_name, [
110 'type' => 'unknown_widget',
112 $options = $form_display->getComponent($field_name);
113 $this->assertEqual($options['type'], 'unknown_widget');
114 $widget = $form_display->getRenderer($field_name);
115 $this->assertEqual($widget->getPluginId(), $default_widget);
119 * Tests the behavior of a field component for a base field.
121 public function testBaseFieldComponent() {
122 $display = EntityFormDisplay::create([
123 'targetEntityType' => 'entity_test_base_field_display',
124 'bundle' => 'entity_test_base_field_display',
128 // Check that default options are correctly filled in.
129 $formatter_settings = \Drupal::service('plugin.manager.field.widget')->getDefaultSettings('text_textfield');
131 'test_no_display' => NULL,
132 'test_display_configurable' => [
133 'type' => 'text_textfield',
134 'settings' => $formatter_settings,
135 'third_party_settings' => [],
137 'region' => 'content',
139 'test_display_non_configurable' => [
140 'type' => 'text_textfield',
141 'settings' => $formatter_settings,
142 'third_party_settings' => [],
144 'region' => 'content',
147 foreach ($expected as $field_name => $options) {
148 $this->assertEqual($display->getComponent($field_name), $options);
151 // Check that saving the display only writes data for fields whose display
154 $config = $this->config('core.entity_form_display.' . $display->id());
155 $data = $config->get();
156 $this->assertFalse(isset($data['content']['test_no_display']));
157 $this->assertFalse(isset($data['hidden']['test_no_display']));
158 $this->assertEqual($data['content']['test_display_configurable'], $expected['test_display_configurable']);
159 $this->assertFalse(isset($data['content']['test_display_non_configurable']));
160 $this->assertFalse(isset($data['hidden']['test_display_non_configurable']));
162 // Check that defaults are correctly filled when loading the display.
163 $display = EntityFormDisplay::load($display->id());
164 foreach ($expected as $field_name => $options) {
165 $this->assertEqual($display->getComponent($field_name), $options);
168 // Check that data manually written for fields whose display is not
169 // configurable is discarded when loading the display.
170 $data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
171 $data['content']['test_display_non_configurable']['weight']++;
172 $config->setData($data)->save();
173 $display = EntityFormDisplay::load($display->id());
174 foreach ($expected as $field_name => $options) {
175 $this->assertEqual($display->getComponent($field_name), $options);
180 * Tests deleting field.
182 public function testDeleteField() {
183 $field_name = 'test_field';
184 // Create a field storage and a field.
185 $field_storage = FieldStorageConfig::create([
186 'field_name' => $field_name,
187 'entity_type' => 'entity_test',
188 'type' => 'test_field',
190 $field_storage->save();
191 $field = FieldConfig::create([
192 'field_storage' => $field_storage,
193 'bundle' => 'entity_test',
197 // Create default and compact entity display.
198 EntityFormMode::create(['id' => 'entity_test.compact', 'targetEntityType' => 'entity_test'])->save();
199 EntityFormDisplay::create([
200 'targetEntityType' => 'entity_test',
201 'bundle' => 'entity_test',
203 ])->setComponent($field_name)->save();
204 EntityFormDisplay::create([
205 'targetEntityType' => 'entity_test',
206 'bundle' => 'entity_test',
208 ])->setComponent($field_name)->save();
210 // Check the component exists.
211 $display = entity_get_form_display('entity_test', 'entity_test', 'default');
212 $this->assertTrue($display->getComponent($field_name));
213 $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
214 $this->assertTrue($display->getComponent($field_name));
219 // Check that the component has been removed from the entity displays.
220 $display = entity_get_form_display('entity_test', 'entity_test', 'default');
221 $this->assertFalse($display->getComponent($field_name));
222 $display = entity_get_form_display('entity_test', 'entity_test', 'compact');
223 $this->assertFalse($display->getComponent($field_name));
227 * Tests \Drupal\Core\Entity\EntityDisplayBase::onDependencyRemoval().
229 public function testOnDependencyRemoval() {
230 $this->enableModules(['field_plugins_test']);
232 $field_name = 'test_field';
234 $field_storage = FieldStorageConfig::create([
235 'field_name' => $field_name,
236 'entity_type' => 'entity_test',
239 $field_storage->save();
240 $field = FieldConfig::create([
241 'field_storage' => $field_storage,
242 'bundle' => 'entity_test',
246 EntityFormDisplay::create([
247 'targetEntityType' => 'entity_test',
248 'bundle' => 'entity_test',
250 ])->setComponent($field_name, ['type' => 'field_plugins_test_text_widget'])->save();
252 // Check the component exists and is of the correct type.
253 $display = entity_get_form_display('entity_test', 'entity_test', 'default');
254 $this->assertEqual($display->getComponent($field_name)['type'], 'field_plugins_test_text_widget');
256 // Removing the field_plugins_test module should change the component to use
257 // the default widget for test fields.
258 \Drupal::service('config.manager')->uninstall('module', 'field_plugins_test');
259 $display = entity_get_form_display('entity_test', 'entity_test', 'default');
260 $this->assertEqual($display->getComponent($field_name)['type'], 'text_textfield');
262 // Removing the text module should remove the field from the form display.
263 \Drupal::service('config.manager')->uninstall('module', 'text');
264 $display = entity_get_form_display('entity_test', 'entity_test', 'default');
265 $this->assertFalse($display->getComponent($field_name));