comparison core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\Core\Extension\Extension;
6 use Drupal\KernelTests\KernelTestBase;
7
8 /**
9 * Tests the integrity of field API plugin definitions.
10 *
11 * @group field
12 */
13 class FieldDefinitionIntegrityTest extends KernelTestBase {
14
15 /**
16 * @var array
17 */
18 public static $modules = ['system'];
19
20 /**
21 * Tests the integrity of field plugin definitions.
22 */
23 public function testFieldPluginDefinitionIntegrity() {
24
25 // Enable all core modules that provide field plugins.
26 $modules = system_rebuild_module_data();
27 $modules = array_filter($modules, function (Extension $module) {
28 // Filter contrib, hidden, already enabled modules and modules in the
29 // Testing package.
30 if ($module->origin === 'core'
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
41 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
42 $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
43
44 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
45 $field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');
46
47 /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
48 $field_widget_manager = \Drupal::service('plugin.manager.field.widget');
49
50 // Load the IDs of all available field type plugins.
51 $available_field_type_ids = [];
52 foreach ($field_type_manager->getDefinitions() as $definition) {
53 $available_field_type_ids[] = $definition['id'];
54 }
55
56 // Load the IDs of all available field widget plugins.
57 $available_field_widget_ids = [];
58 foreach ($field_widget_manager->getDefinitions() as $definition) {
59 $available_field_widget_ids[] = $definition['id'];
60 }
61
62 // Load the IDs of all available field formatter plugins.
63 $available_field_formatter_ids = [];
64 foreach ($field_formatter_manager->getDefinitions() as $definition) {
65 $available_field_formatter_ids[] = $definition['id'];
66 }
67
68 // Test the field type plugins.
69 foreach ($field_type_manager->getDefinitions() as $definition) {
70 // Test default field widgets.
71 if (isset($definition['default_widget'])) {
72 if (in_array($definition['default_widget'], $available_field_widget_ids)) {
73 $this->pass(sprintf('Field type %s uses an existing field widget by default.', $definition['id']));
74 }
75 else {
76 $this->fail(sprintf('Field type %s uses a non-existent field widget by default: %s', $definition['id'], $definition['default_widget']));
77 }
78 }
79
80 // Test default field formatters.
81 if (isset($definition['default_formatter'])) {
82 if (in_array($definition['default_formatter'], $available_field_formatter_ids)) {
83 $this->pass(sprintf('Field type %s uses an existing field formatter by default.', $definition['id']));
84 }
85 else {
86 $this->fail(sprintf('Field type %s uses a non-existent field formatter by default: %s', $definition['id'], $definition['default_formatter']));
87 }
88 }
89 }
90
91 // Test the field widget plugins.
92 foreach ($field_widget_manager->getDefinitions() as $definition) {
93 $missing_field_type_ids = array_diff($definition['field_types'], $available_field_type_ids);
94 if ($missing_field_type_ids) {
95 $this->fail(sprintf('Field widget %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
96 }
97 else {
98 $this->pass(sprintf('Field widget %s integrates with existing field types.', $definition['id']));
99 }
100 }
101
102 // Test the field formatter plugins.
103 foreach ($field_formatter_manager->getDefinitions() as $definition) {
104 $missing_field_type_ids = array_diff($definition['field_types'], $available_field_type_ids);
105 if ($missing_field_type_ids) {
106 $this->fail(sprintf('Field formatter %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
107 }
108 else {
109 $this->pass(sprintf('Field formatter %s integrates with existing field types.', $definition['id']));
110 }
111 }
112 }
113
114 }