diff -u b/core/modules/field/tests/src/Kernel/FieldDefinitionAvailabilityTest.php b/core/modules/field/tests/src/Kernel/FieldDefinitionAvailabilityTest.php --- b/core/modules/field/tests/src/Kernel/FieldDefinitionAvailabilityTest.php +++ b/core/modules/field/tests/src/Kernel/FieldDefinitionAvailabilityTest.php @@ -2,14 +2,16 @@ namespace Drupal\Tests\field\Kernel; +use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Entity\ContentEntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Extension\Extension; +use Drupal\Core\Field\BaseFieldDefinition; use Drupal\KernelTests\KernelTestBase; /** - * Tests the integrity of field API plugin definitions. + * Tests to load field plugin definitions used in core's existing entities. * * @group field */ @@ -18,10 +20,6 @@ /** * Modules to enable. * - * The test runner will merge the $modules lists from this class, the class - * it extends, and so on up the class hierarchy. It is not necessary to - * include modules in your list that a parent class has already declared. - * * @var array * * @see \Drupal\Tests\KernelTestBase::enableModules() @@ -30,7 +28,7 @@ public static $modules = ['system']; /** - * Tests the availability of field plugin definitions. + * Tests to load field plugin definitions used in core's existing entities. */ public function testFieldPluginDefinitionAvailability() { @@ -39,13 +37,10 @@ $modules = array_filter($modules, function (Extension $module) { // Filter contrib, hidden, already enabled modules and modules in the // Testing package. - if ($module->origin === 'core' + return ($module->origin === 'core' && empty($module->info['hidden']) && $module->status == FALSE - && $module->info['package'] !== 'Testing') { - return TRUE; - } - return FALSE; + && $module->info['package'] !== 'Testing'); }); $this->enableModules(array_keys($modules)); @@ -64,6 +59,7 @@ /** @var \Drupal\Core\Field\BaseFieldDefinition[][] $field_definitions */ $field_definitions = []; + /** @var \Drupal\Core\Entity\EntityTypeInterface[] $content_entity_types */ $content_entity_types = array_filter($entity_type_manager->getDefinitions(), function (EntityTypeInterface $entity_type) { return $entity_type instanceof ContentEntityTypeInterface; @@ -73,31 +69,44 @@ $field_definitions[$entity_type_id] = $entity_field_manager->getBaseFieldDefinitions($entity_type_id); } - $plugin_not_found = []; foreach ($field_definitions as $entity_type_id => $definitions) { foreach ($definitions as $field_id => $field_definition) { - $view_display_options = $field_definition->getDisplayOptions('view'); - if (!empty($view_display_options) && !empty($view_display_options['type'])) { - try { - $field_formatter_manager->getDefinition($view_display_options['type']); - } - catch (PluginNotFoundException $e) { - $plugin_not_found[] = "PluginNotFoundException here for \"{$entity_type_id}\" entity type in \"{$field_id}\" field view display options. Original message: {$e->getMessage()}"; - } - } - $form_display_options = $field_definition->getDisplayOptions('form'); - if (!empty($form_display_options) && !empty($form_display_options['type'])) { - try { - $field_widget_manager->getDefinition($form_display_options['type']); - } - catch (PluginNotFoundException $e) { - $plugin_not_found[] = "PluginNotFoundException here for \"{$entity_type_id}\" entity type in \"{$field_id}\" field form display options. Original message: {$e->getMessage()}"; - } - } + $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'); } } + } - $this->assertEmpty($plugin_not_found, implode(" ", $plugin_not_found)); + /** + * Helper method that tries to load plugin definitions. + * + * @param string $entity_type_id + * Id of entity type. Required by message. + * @param string $field_id + * Id of field. Required by message. + * @param \Drupal\Core\Field\BaseFieldDefinition $field_definition + * Field definition that provide display options. + * @param \Drupal\Component\Plugin\Discovery\DiscoveryInterface $plugin_manager + * Plugin manager that will try to provide plugin definition. + * @param string $display_context + * Defines which display options should be loaded. + */ + protected function checkDisplayOption($entity_type_id, $field_id, BaseFieldDefinition $field_definition, DiscoveryInterface $plugin_manager, $display_context) { + $form_display_options = $field_definition->getDisplayOptions($display_context); + if (!empty($form_display_options) && !empty($form_display_options['type'])) { + try { + $plugin_manager->getDefinition($form_display_options['type']); + } + catch (PluginNotFoundException $e) { + $this->fail(sprintf( + 'PluginNotFoundException here for "%s" field %s display options of "%s" entity type. Original message: %s', + $field_id, + $display_context, + $entity_type_id, + $e->getMessage() + )); + } + } } }