You are here

public function FieldDefinitionIntegrityTest::testFieldPluginDefinitionAvailability in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php \Drupal\Tests\field\Kernel\FieldDefinitionIntegrityTest::testFieldPluginDefinitionAvailability()

Tests to load field plugin definitions used in core's existing entities.

File

core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php, line 93

Class

FieldDefinitionIntegrityTest
Tests the integrity of field API plugin definitions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldPluginDefinitionAvailability() {
  $this
    ->enableModules($this
    ->modulesWithSubdirectory('src' . DIRECTORY_SEPARATOR . 'Entity'));

  /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
  $field_formatter_manager = $this->container
    ->get('plugin.manager.field.formatter');

  /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
  $field_widget_manager = $this->container
    ->get('plugin.manager.field.widget');

  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager */
  $entity_field_manager = $this->container
    ->get('entity_field.manager');

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = $this->container
    ->get('entity_type.manager');

  /** @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;
  });
  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');
    }
  }
}