function FieldDefinitionIntegrityTest::testFieldPluginDefinitionIntegrity

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php \Drupal\Tests\field\Kernel\FieldDefinitionIntegrityTest::testFieldPluginDefinitionIntegrity()
  2. 8.9.x core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php \Drupal\Tests\field\Kernel\FieldDefinitionIntegrityTest::testFieldPluginDefinitionIntegrity()
  3. 10 core/modules/field/tests/src/Kernel/FieldDefinitionIntegrityTest.php \Drupal\Tests\field\Kernel\FieldDefinitionIntegrityTest::testFieldPluginDefinitionIntegrity()

Tests the integrity of field plugin definitions.

File

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

Class

FieldDefinitionIntegrityTest
Tests the integrity of field API plugin definitions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testFieldPluginDefinitionIntegrity() : void {
    // Enable all core modules that provide field plugins, and their
    // dependencies.
    $this->enableModules($this->modulesWithSubdirectory('src' . DIRECTORY_SEPARATOR . 'Plugin' . DIRECTORY_SEPARATOR . 'Field'));
    
    /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    
    /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
    $field_formatter_manager = \Drupal::service('plugin.manager.field.formatter');
    
    /** @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface $field_type_manager */
    $field_widget_manager = \Drupal::service('plugin.manager.field.widget');
    // Load the IDs of all available field type plugins.
    $available_field_type_ids = [];
    foreach ($field_type_manager->getDefinitions() as $definition) {
        $available_field_type_ids[] = $definition['id'];
    }
    // Load the IDs of all available field widget plugins.
    $available_field_widget_ids = [];
    foreach ($field_widget_manager->getDefinitions() as $definition) {
        $available_field_widget_ids[] = $definition['id'];
    }
    // Load the IDs of all available field formatter plugins.
    $available_field_formatter_ids = [];
    foreach ($field_formatter_manager->getDefinitions() as $definition) {
        $available_field_formatter_ids[] = $definition['id'];
    }
    // Test the field type plugins.
    foreach ($field_type_manager->getDefinitions() as $definition) {
        // Test default field widgets.
        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']));
        }
        // Test default field formatters.
        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']));
        }
    }
    // Test the field widget plugins.
    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)));
    }
    // Test the field formatter plugins.
    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)));
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.