FieldDefinitionIntegrityTest.php in Zircon Profile 8
File
core/modules/field/src/Tests/FieldDefinitionIntegrityTest.php
View source
<?php
namespace Drupal\field\Tests;
use Drupal\Core\Extension\Extension;
use Drupal\simpletest\KernelTestBase;
class FieldDefinitionIntegrityTest extends KernelTestBase {
protected function setUp() {
parent::setUp();
$modules = system_rebuild_module_data();
$modules = array_filter($modules, function (Extension $module) {
if ($module->origin === 'core' && empty($module->info['hidden']) && $module->status == FALSE && $module->info['package'] !== 'Testing' && is_readable($module
->getPath() . '/src/Plugin/Field')) {
return TRUE;
}
return FALSE;
});
$this
->enableModules(array_keys($modules));
}
public function testFieldPluginDefinitionIntegrity() {
$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'])) {
if (in_array($definition['default_widget'], $available_field_widget_ids)) {
$this
->pass(sprintf('Field type %s uses an existing field widget by default.', $definition['id']));
}
else {
$this
->fail(sprintf('Field type %s uses a non-existent field widget by default: %s', $definition['id'], $definition['default_widget']));
}
}
if (isset($definition['default_formatter'])) {
if (in_array($definition['default_formatter'], $available_field_formatter_ids)) {
$this
->pass(sprintf('Field type %s uses an existing field formatter by default.', $definition['id']));
}
else {
$this
->fail(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);
if ($missing_field_type_ids) {
$this
->fail(sprintf('Field widget %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
}
else {
$this
->pass(sprintf('Field widget %s integrates with existing field types.', $definition['id']));
}
}
foreach ($field_formatter_manager
->getDefinitions() as $definition) {
$missing_field_type_ids = array_diff($definition['field_types'], $available_field_type_ids);
if ($missing_field_type_ids) {
$this
->fail(sprintf('Field formatter %s integrates with non-existent field types: %s', $definition['id'], implode(', ', $missing_field_type_ids)));
}
else {
$this
->pass(sprintf('Field formatter %s integrates with existing field types.', $definition['id']));
}
}
}
}