Mercurial > hg > isophonics-drupal-site
comparison core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php @ 12:7a779792577d
Update Drupal core to v8.4.5 (via Composer)
author | Chris Cannam |
---|---|
date | Fri, 23 Feb 2018 15:52:07 +0000 |
parents | 4c8ae668cc8c |
children |
comparison
equal
deleted
inserted
replaced
11:bfffd8d7479a | 12:7a779792577d |
---|---|
1 <?php | 1 <?php |
2 | 2 |
3 namespace Drupal\Tests\field\Kernel; | 3 namespace Drupal\Tests\field\Kernel; |
4 | 4 |
5 use Drupal\Component\Plugin\Discovery\DiscoveryInterface; | |
6 use Drupal\Component\Plugin\Exception\PluginNotFoundException; | |
7 use Drupal\Component\Utility\NestedArray; | |
8 use Drupal\Core\Entity\ContentEntityTypeInterface; | |
9 use Drupal\Core\Entity\EntityTypeInterface; | |
5 use Drupal\Core\Extension\Extension; | 10 use Drupal\Core\Extension\Extension; |
11 use Drupal\Core\Field\BaseFieldDefinition; | |
6 use Drupal\KernelTests\KernelTestBase; | 12 use Drupal\KernelTests\KernelTestBase; |
7 | 13 |
8 /** | 14 /** |
9 * Tests the integrity of field API plugin definitions. | 15 * Tests the integrity of field API plugin definitions. |
10 * | 16 * |
19 | 25 |
20 /** | 26 /** |
21 * Tests the integrity of field plugin definitions. | 27 * Tests the integrity of field plugin definitions. |
22 */ | 28 */ |
23 public function testFieldPluginDefinitionIntegrity() { | 29 public function testFieldPluginDefinitionIntegrity() { |
24 | 30 // Enable all core modules that provide field plugins, and their |
25 // Enable all core modules that provide field plugins. | 31 // dependencies. |
26 $modules = system_rebuild_module_data(); | 32 $this->enableModules( |
27 $modules = array_filter($modules, function (Extension $module) { | 33 $this->modulesWithSubdirectory( |
28 // Filter contrib, hidden, already enabled modules and modules in the | 34 'src' . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . 'Field' |
29 // Testing package. | 35 ) |
30 if ($module->origin === 'core' | 36 ); |
31 && empty($module->info['hidden']) | |
32 && $module->status == FALSE | |
33 && $module->info['package'] !== 'Testing' | |
34 && is_readable($module->getPath() . '/src/Plugin/Field')) { | |
35 return TRUE; | |
36 } | |
37 return FALSE; | |
38 }); | |
39 $this->enableModules(array_keys($modules)); | |
40 | 37 |
41 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ | 38 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ |
42 $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); | 39 $field_type_manager = \Drupal::service('plugin.manager.field.field_type'); |
43 | 40 |
44 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ | 41 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ |
106 $this->fail(sprintf('Field formatter %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids))); | 103 $this->fail(sprintf('Field formatter %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids))); |
107 } | 104 } |
108 else { | 105 else { |
109 $this->pass(sprintf('Field formatter %s integrates with existing field types.', $definition['id'])); | 106 $this->pass(sprintf('Field formatter %s integrates with existing field types.', $definition['id'])); |
110 } | 107 } |
111 } | 108 |
109 } | |
110 } | |
111 | |
112 /** | |
113 * Tests to load field plugin definitions used in core's existing entities. | |
114 */ | |
115 public function testFieldPluginDefinitionAvailability() { | |
116 $this->enableModules( | |
117 $this->modulesWithSubdirectory('src' . DIRECTORY_SEPARATOR . 'Entity') | |
118 ); | |
119 | |
120 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ | |
121 $field_formatter_manager = $this->container->get('plugin.manager.field.formatter'); | |
122 | |
123 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */ | |
124 $field_widget_manager = $this->container->get('plugin.manager.field.widget'); | |
125 | |
126 /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */ | |
127 $entity_field_manager = $this->container->get('entity_field.manager'); | |
128 | |
129 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */ | |
130 $entity_type_manager = $this->container->get('entity_type.manager'); | |
131 | |
132 /** @var \Drupal\Core\Field\BaseFieldDefinition[][] $field_definitions */ | |
133 $field_definitions = []; | |
134 | |
135 /** @var \Drupal\Core\Entity\EntityTypeInterface[] $content_entity_types */ | |
136 $content_entity_types = array_filter($entity_type_manager->getDefinitions(), function (EntityTypeInterface $entity_type) { | |
137 return $entity_type instanceof ContentEntityTypeInterface; | |
138 }); | |
139 | |
140 foreach ($content_entity_types as $entity_type_id => $entity_type_definition) { | |
141 $field_definitions[$entity_type_id] = $entity_field_manager->getBaseFieldDefinitions($entity_type_id); | |
142 } | |
143 | |
144 foreach ($field_definitions as $entity_type_id => $definitions) { | |
145 foreach ($definitions as $field_id => $field_definition) { | |
146 $this->checkDisplayOption($entity_type_id, $field_id, $field_definition, $field_formatter_manager, 'view'); | |
147 $this->checkDisplayOption($entity_type_id, $field_id, $field_definition, $field_widget_manager, 'form'); | |
148 } | |
149 } | |
150 } | |
151 | |
152 /** | |
153 * Helper method that tries to load plugin definitions. | |
154 * | |
155 * @param string $entity_type_id | |
156 * Id of entity type. Required by message. | |
157 * @param string $field_id | |
158 * Id of field. Required by message. | |
159 * @param \Drupal\Core\Field\BaseFieldDefinition $field_definition | |
160 * Field definition that provide display options. | |
161 * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $plugin_manager | |
162 * Plugin manager that will try to provide plugin definition. | |
163 * @param string $display_context | |
164 * Defines which display options should be loaded. | |
165 */ | |
166 protected function checkDisplayOption($entity_type_id, $field_id, BaseFieldDefinition $field_definition, DiscoveryInterface $plugin_manager, $display_context) { | |
167 $display_options = $field_definition->getDisplayOptions($display_context); | |
168 if (!empty($display_options['type'])) { | |
169 try { | |
170 $plugin_manager->getDefinition($display_options['type']); | |
171 } | |
172 catch (PluginNotFoundException $e) { | |
173 $this->fail(sprintf( | |
174 'PluginNotFoundException here for "%s" field %s display options of "%s" entity type. Original message: %s', | |
175 $field_id, | |
176 $display_context, | |
177 $entity_type_id, | |
178 $e->getMessage() | |
179 )); | |
180 } | |
181 } | |
182 } | |
183 | |
184 /** | |
185 * Find modules with a specified subdirectory. | |
186 * | |
187 * @param string $subdirectory | |
188 * The required path, relative to the module directory. | |
189 * | |
190 * @return string[] | |
191 * A list of module names satisfying these criteria: | |
192 * - provided by core | |
193 * - not hidden | |
194 * - not already enabled | |
195 * - not in the Testing package | |
196 * - containing the required $subdirectory | |
197 * and all modules required by any of these modules. | |
198 */ | |
199 protected function modulesWithSubdirectory($subdirectory) { | |
200 $modules = system_rebuild_module_data(); | |
201 $modules = array_filter($modules, function (Extension $module) use ($subdirectory) { | |
202 // Filter contrib, hidden, already enabled modules and modules in the | |
203 // Testing package. | |
204 return ($module->origin === 'core' | |
205 && empty($module->info['hidden']) | |
206 && $module->status == FALSE | |
207 && $module->info['package'] !== 'Testing' | |
208 && is_readable($module->getPath() . DIRECTORY_SEPARATOR . $subdirectory)); | |
209 }); | |
210 // Gather the dependencies of the modules. | |
211 $dependencies = NestedArray::mergeDeepArray(array_map(function (Extension $module) { | |
212 return array_keys($module->requires); | |
213 }, $modules)); | |
214 | |
215 return array_unique(NestedArray::mergeDeep(array_keys($modules), $dependencies)); | |
112 } | 216 } |
113 | 217 |
114 } | 218 } |