View source
<?php
namespace Drupal\Tests\field\Kernel;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\KernelTests\KernelTestBase;
class FieldDefinitionIntegrityTest extends KernelTestBase {
protected static $modules = [
'system',
'path_alias',
];
public function testFieldPluginDefinitionIntegrity() {
$this
->enableModules($this
->modulesWithSubdirectory('src' . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . 'Field'));
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
$field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');
$field_widget_manager = \Drupal::service('plugin.manager.field.widget');
$available_field_type_ids = [];
foreach ($field_type_manager
->getDefinitions() as $definition) {
$available_field_type_ids[] = $definition['id'];
}
$available_field_widget_ids = [];
foreach ($field_widget_manager
->getDefinitions() as $definition) {
$available_field_widget_ids[] = $definition['id'];
}
$available_field_formatter_ids = [];
foreach ($field_formatter_manager
->getDefinitions() as $definition) {
$available_field_formatter_ids[] = $definition['id'];
}
foreach ($field_type_manager
->getDefinitions() as $definition) {
if (isset($definition['default_widget'])) {
$this
->assertContains($definition['default_widget'], $available_field_widget_ids, sprintf('Field type %s uses a non-existent field widget by default: %s', $definition['id'], $definition['default_widget']));
}
if (isset($definition['default_formatter'])) {
$this
->assertContains($definition['default_formatter'], $available_field_formatter_ids, sprintf('Field type %s uses a non-existent field formatter by default: %s', $definition['id'], $definition['default_formatter']));
}
}
foreach ($field_widget_manager
->getDefinitions() as $definition) {
$missing_field_type_ids = array_diff($definition['field_types'], $available_field_type_ids);
$this
->assertEmpty($missing_field_type_ids, sprintf('Field widget %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
}
foreach ($field_formatter_manager
->getDefinitions() as $definition) {
$missing_field_type_ids = array_diff($definition['field_types'], $available_field_type_ids);
$this
->assertEmpty($missing_field_type_ids, sprintf('Field formatter %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
}
}
public function testFieldPluginDefinitionAvailability() {
$this
->enableModules($this
->modulesWithSubdirectory('src' . DIRECTORY_SEPARATOR . 'Entity'));
$field_formatter_manager = $this->container
->get('plugin.manager.field.formatter');
$field_widget_manager = $this->container
->get('plugin.manager.field.widget');
$entity_field_manager = $this->container
->get('entity_field.manager');
$entity_type_manager = $this->container
->get('entity_type.manager');
$field_definitions = [];
$content_entity_types = array_filter($entity_type_manager
->getDefinitions(), function (EntityTypeInterface $entity_type) {
return $entity_type instanceof ContentEntityTypeInterface;
});
foreach ($content_entity_types as $entity_type_id => $entity_type_definition) {
$field_definitions[$entity_type_id] = $entity_field_manager
->getBaseFieldDefinitions($entity_type_id);
}
foreach ($field_definitions as $entity_type_id => $definitions) {
foreach ($definitions as $field_id => $field_definition) {
$this
->checkDisplayOption($entity_type_id, $field_id, $field_definition, $field_formatter_manager, 'view');
$this
->checkDisplayOption($entity_type_id, $field_id, $field_definition, $field_widget_manager, 'form');
}
}
}
protected function checkDisplayOption($entity_type_id, $field_id, BaseFieldDefinition $field_definition, DiscoveryInterface $plugin_manager, $display_context) {
$display_options = $field_definition
->getDisplayOptions($display_context);
if (!empty($display_options['type'])) {
$plugin = $plugin_manager
->getDefinition($display_options['type'], FALSE);
$this
->assertNotNull($plugin, sprintf('Plugin found for "%s" field %s display options of "%s" entity type.', $field_id, $display_context, $entity_type_id));
}
}
protected function modulesWithSubdirectory($subdirectory) {
$modules = \Drupal::service('extension.list.module')
->getList();
$modules = array_filter($modules, function (Extension $module) use ($subdirectory) {
return $module->origin === 'core' && empty($module->info['hidden']) && $module->status == FALSE && $module->info['package'] !== 'Testing' && is_readable($module
->getPath() . DIRECTORY_SEPARATOR . $subdirectory);
});
$dependencies = NestedArray::mergeDeepArray(array_map(function (Extension $module) {
return array_keys($module->requires);
}, $modules));
return array_unique(NestedArray::mergeDeep(array_keys($modules), $dependencies));
}
}