Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / FieldTypePluginManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
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;
9
10 /**
11  * Tests the field type manager.
12  *
13  * @group field
14  */
15 class FieldTypePluginManagerTest extends FieldKernelTestBase {
16
17   /**
18    * Tests the default settings convenience methods.
19    */
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]));
26     }
27   }
28
29   /**
30    * Tests creation of field item instances.
31    */
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);
37
38       $class = $definition['class'];
39       $field_name = 'field_' . $type;
40
41       $field_definition = BaseFieldDefinition::create($type);
42
43       $configuration = [
44         'field_definition' => $field_definition,
45         'name' => $field_name,
46         'parent' => NULL,
47       ];
48
49       $instance = $field_type_manager->createInstance($type, $configuration);
50
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]));
53     }
54   }
55
56   /**
57    * Tests creation of field item instances.
58    */
59   public function testCreateInstanceWithConfig() {
60     /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
61     $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
62     $type = 'test_field';
63     $definition = $field_type_manager->getDefinition($type);
64
65     $class = $definition['class'];
66     $field_name = 'field_' . $type;
67
68     $field_definition = BaseFieldDefinition::create($type)
69       ->setLabel('Jenny')
70       ->setDefaultValue(8675309);
71
72     $configuration = [
73       'field_definition' => $field_definition,
74       'name' => $field_name,
75       'parent' => NULL,
76     ];
77
78     $entity = EntityTest::create();
79
80     $instance = $field_type_manager->createInstance($type, $configuration);
81
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');
86   }
87
88   /**
89    * Tests all field items provide an existing main property.
90    */
91   public function testMainProperty() {
92     // Let's enable all Drupal modules in Drupal core, so we test any field
93     // type plugin.
94     $this->enableAllCoreModules();
95
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);
107       }
108     }
109   }
110
111   /**
112    * Enable all core modules.
113    */
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';
121     });
122     $this->enableModules($module_list);
123   }
124
125 }