public function EntityFormDisplayTest::testFieldComponent in Drupal 10
Same name and namespace in other branches
- 8 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testFieldComponent()
- 9 core/modules/field_ui/tests/src/Kernel/EntityFormDisplayTest.php \Drupal\Tests\field_ui\Kernel\EntityFormDisplayTest::testFieldComponent()
Tests the behavior of a field component within an EntityFormDisplay object.
File
- core/
modules/ field_ui/ tests/ src/ Kernel/ EntityFormDisplayTest.php, line 64
Class
- EntityFormDisplayTest
- Tests the entity display configuration entities.
Namespace
Drupal\Tests\field_ui\KernelCode
public function testFieldComponent() {
// Create a field storage and a field.
$field_name = 'test_field';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'test_field',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'entity_test',
]);
$field
->save();
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
]);
// Check that providing no options results in default values being used.
$form_display
->setComponent($field_name);
$field_type_info = \Drupal::service('plugin.manager.field.field_type')
->getDefinition($field_storage
->getType());
$default_widget = $field_type_info['default_widget'];
$widget_settings = \Drupal::service('plugin.manager.field.widget')
->getDefaultSettings($default_widget);
$expected = [
'weight' => 3,
'type' => $default_widget,
'settings' => $widget_settings,
'third_party_settings' => [],
];
$this
->assertEquals($expected, $form_display
->getComponent($field_name));
// Check that the getWidget() method returns the correct widget plugin.
$widget = $form_display
->getRenderer($field_name);
$this
->assertEquals($default_widget, $widget
->getPluginId());
$this
->assertEquals($widget_settings, $widget
->getSettings());
// Check that the widget is statically persisted, by assigning an
// arbitrary property and reading it back.
$random_value = $this
->randomString();
$widget->randomValue = $random_value;
$widget = $form_display
->getRenderer($field_name);
$this
->assertEquals($random_value, $widget->randomValue);
// Check that changing the definition creates a new widget.
$form_display
->setComponent($field_name, [
'type' => 'field_test_multiple',
]);
$widget = $form_display
->getRenderer($field_name);
$this
->assertEquals('test_field_widget', $widget
->getPluginId());
$this
->assertFalse(isset($widget->randomValue));
// Check that specifying an unknown widget (e.g. case of a disabled module)
// gets stored as is in the display, but results in the default widget being
// used.
$form_display
->setComponent($field_name, [
'type' => 'unknown_widget',
]);
$options = $form_display
->getComponent($field_name);
$this
->assertEquals('unknown_widget', $options['type']);
$widget = $form_display
->getRenderer($field_name);
$this
->assertEquals($default_widget, $widget
->getPluginId());
}