View source
<?php
namespace Drupal\field_ui\Tests;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityFormMode;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\KernelTestBase;
class EntityFormDisplayTest extends KernelTestBase {
public static $modules = [
'field_ui',
'field',
'entity_test',
'field_test',
'user',
'text',
];
protected function setUp() {
parent::setUp();
}
public function testEntityGetFromDisplay() {
$form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertTrue($form_display
->isNew());
$form_display
->setComponent('component_1', array(
'weight' => 10,
))
->save();
$form_display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertFalse($form_display
->isNew());
$this
->assertEqual($form_display
->id(), 'entity_test.entity_test.default');
$this
->assertEqual($form_display
->getComponent('component_1'), array(
'weight' => 10,
'settings' => array(),
'third_party_settings' => array(),
));
}
public function testFieldComponent() {
$field_name = 'test_field';
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'test_field',
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_storage' => $field_storage,
'bundle' => 'entity_test',
));
$field
->save();
$form_display = EntityFormDisplay::create(array(
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
));
$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 = array(
'weight' => 0,
'type' => $default_widget,
'settings' => $widget_settings,
'third_party_settings' => array(),
);
$this
->assertEqual($form_display
->getComponent($field_name), $expected);
$widget = $form_display
->getRenderer($field_name);
$this
->assertEqual($widget
->getPluginId(), $default_widget);
$this
->assertEqual($widget
->getSettings(), $widget_settings);
$random_value = $this
->randomString();
$widget->randomValue = $random_value;
$widget = $form_display
->getRenderer($field_name);
$this
->assertEqual($widget->randomValue, $random_value);
$form_display
->setComponent($field_name, array(
'type' => 'field_test_multiple',
));
$widget = $form_display
->getRenderer($field_name);
$this
->assertEqual($widget
->getPluginId(), 'test_field_widget');
$this
->assertFalse(isset($widget->randomValue));
$form_display
->setComponent($field_name, array(
'type' => 'unknown_widget',
));
$options = $form_display
->getComponent($field_name);
$this
->assertEqual($options['type'], 'unknown_widget');
$widget = $form_display
->getRenderer($field_name);
$this
->assertEqual($widget
->getPluginId(), $default_widget);
}
public function testBaseFieldComponent() {
$display = EntityFormDisplay::create(array(
'targetEntityType' => 'entity_test_base_field_display',
'bundle' => 'entity_test_base_field_display',
'mode' => 'default',
));
$formatter_settings = \Drupal::service('plugin.manager.field.widget')
->getDefaultSettings('text_textfield');
$expected = array(
'test_no_display' => NULL,
'test_display_configurable' => array(
'type' => 'text_textfield',
'settings' => $formatter_settings,
'third_party_settings' => array(),
'weight' => 10,
),
'test_display_non_configurable' => array(
'type' => 'text_textfield',
'settings' => $formatter_settings,
'third_party_settings' => array(),
'weight' => 11,
),
);
foreach ($expected as $field_name => $options) {
$this
->assertEqual($display
->getComponent($field_name), $options);
}
$display
->save();
$config = $this
->config('core.entity_form_display.' . $display
->id());
$data = $config
->get();
$this
->assertFalse(isset($data['content']['test_no_display']));
$this
->assertFalse(isset($data['hidden']['test_no_display']));
$this
->assertEqual($data['content']['test_display_configurable'], $expected['test_display_configurable']);
$this
->assertFalse(isset($data['content']['test_display_non_configurable']));
$this
->assertFalse(isset($data['hidden']['test_display_non_configurable']));
$display = entity_load('entity_form_display', $display
->id());
foreach ($expected as $field_name => $options) {
$this
->assertEqual($display
->getComponent($field_name), $options);
}
$data['content']['test_display_non_configurable'] = $expected['test_display_non_configurable'];
$data['content']['test_display_non_configurable']['weight']++;
$config
->setData($data)
->save();
$display = entity_load('entity_form_display', $display
->id());
foreach ($expected as $field_name => $options) {
$this
->assertEqual($display
->getComponent($field_name), $options);
}
}
public function testDeleteField() {
$field_name = 'test_field';
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'test_field',
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_storage' => $field_storage,
'bundle' => 'entity_test',
));
$field
->save();
EntityFormMode::create(array(
'id' => 'entity_test.compact',
'targetEntityType' => 'entity_test',
))
->save();
EntityFormDisplay::create(array(
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
))
->setComponent($field_name)
->save();
EntityFormDisplay::create(array(
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'compact',
))
->setComponent($field_name)
->save();
$display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertTrue($display
->getComponent($field_name));
$display = entity_get_form_display('entity_test', 'entity_test', 'compact');
$this
->assertTrue($display
->getComponent($field_name));
$field
->delete();
$display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertFalse($display
->getComponent($field_name));
$display = entity_get_form_display('entity_test', 'entity_test', 'compact');
$this
->assertFalse($display
->getComponent($field_name));
}
public function testOnDependencyRemoval() {
$this
->enableModules(array(
'field_plugins_test',
));
$field_name = 'test_field';
$field_storage = FieldStorageConfig::create(array(
'field_name' => $field_name,
'entity_type' => 'entity_test',
'type' => 'text',
));
$field_storage
->save();
$field = FieldConfig::create(array(
'field_storage' => $field_storage,
'bundle' => 'entity_test',
));
$field
->save();
EntityFormDisplay::create(array(
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
))
->setComponent($field_name, array(
'type' => 'field_plugins_test_text_widget',
))
->save();
$display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertEqual($display
->getComponent($field_name)['type'], 'field_plugins_test_text_widget');
\Drupal::service('config.manager')
->uninstall('module', 'field_plugins_test');
$display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertEqual($display
->getComponent($field_name)['type'], 'text_textfield');
\Drupal::service('config.manager')
->uninstall('module', 'text');
$display = entity_get_form_display('entity_test', 'entity_test', 'default');
$this
->assertFalse($display
->getComponent($field_name));
}
}