3 namespace Drupal\Tests\field\Kernel;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Extension\ExtensionDiscovery;
7 use Drupal\Core\Field\BaseFieldDefinition;
8 use Drupal\entity_test\Entity\EntityTest;
11 * Tests the field type manager.
15 class FieldTypePluginManagerTest extends FieldKernelTestBase {
18 * Tests the default settings convenience methods.
20 public function testDefaultSettings() {
21 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
22 foreach (['test_field', 'shape', 'hidden_test_field'] as $type) {
23 $definition = $field_type_manager->getDefinition($type);
24 $this->assertIdentical($field_type_manager->getDefaultStorageSettings($type), $definition['class']::defaultStorageSettings(), format_string("%type storage settings were returned", ['%type' => $type]));
25 $this->assertIdentical($field_type_manager->getDefaultFieldSettings($type), $definition['class']::defaultFieldSettings(), format_string(" %type field settings were returned", ['%type' => $type]));
30 * Tests creation of field item instances.
32 public function testCreateInstance() {
33 /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
34 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
35 foreach (['test_field', 'shape', 'hidden_test_field'] as $type) {
36 $definition = $field_type_manager->getDefinition($type);
38 $class = $definition['class'];
39 $field_name = 'field_' . $type;
41 $field_definition = BaseFieldDefinition::create($type);
44 'field_definition' => $field_definition,
45 'name' => $field_name,
49 $instance = $field_type_manager->createInstance($type, $configuration);
51 $this->assertTrue($instance instanceof $class, new FormattableMarkup('Created a @class instance', ['@class' => $class]));
52 $this->assertEqual($field_name, $instance->getName(), new FormattableMarkup('Instance name is @name', ['@name' => $field_name]));
57 * Tests creation of field item instances.
59 public function testCreateInstanceWithConfig() {
60 /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
61 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
63 $definition = $field_type_manager->getDefinition($type);
65 $class = $definition['class'];
66 $field_name = 'field_' . $type;
68 $field_definition = BaseFieldDefinition::create($type)
70 ->setDefaultValue(8675309);
73 'field_definition' => $field_definition,
74 'name' => $field_name,
78 $entity = EntityTest::create();
80 $instance = $field_type_manager->createInstance($type, $configuration);
82 $this->assertTrue($instance instanceof $class, new FormattableMarkup('Created a @class instance', ['@class' => $class]));
83 $this->assertEqual($field_name, $instance->getName(), new FormattableMarkup('Instance name is @name', ['@name' => $field_name]));
84 $this->assertEqual($instance->getFieldDefinition()->getLabel(), 'Jenny', 'Instance label is Jenny');
85 $this->assertEqual($instance->getFieldDefinition()->getDefaultValue($entity), [['value' => 8675309]], 'Instance default_value is 8675309');
89 * Tests all field items provide an existing main property.
91 public function testMainProperty() {
92 // Let's enable all Drupal modules in Drupal core, so we test any field
94 $this->enableAllCoreModules();
96 /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
97 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
98 foreach ($field_type_manager->getDefinitions() as $plugin_id => $definition) {
99 $class = $definition['class'];
100 $property = $class::mainPropertyName();
101 $storage_definition = BaseFieldDefinition::create($plugin_id);
102 $property_definitions = $class::propertyDefinitions($storage_definition);
103 $properties = implode(', ', array_keys($property_definitions));
104 if (!empty($property_definitions)) {
105 $message = sprintf("%s property %s found in %s", $plugin_id, $property, $properties);
106 $this->assertArrayHasKey($property, $class::propertyDefinitions($storage_definition), $message);
112 * Enable all core modules.
114 protected function enableAllCoreModules() {
115 $listing = new ExtensionDiscovery($this->root);
116 $module_list = $listing->scan('module', FALSE);
117 /** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
118 $module_handler = $this->container->get('module_handler');
119 $module_list = array_filter(array_keys($module_list), function ($module) use ($module_handler, $module_list) {
120 return !$module_handler->moduleExists($module) && substr($module_list[$module]->getPath(), 0, 4) === 'core';
122 $this->enableModules($module_list);