function FieldTypePluginManagerTest::testMainProperty

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

Tests all field items provide an existing main property.

File

core/modules/field/tests/src/Kernel/FieldTypePluginManagerTest.php, line 94

Class

FieldTypePluginManagerTest
Tests the field type manager.

Namespace

Drupal\Tests\field\Kernel

Code

public function testMainProperty() {
    // Let's enable all Drupal modules in Drupal core, so we test any field
    // type plugin.
    $this->enableAllCoreModules();
    
    /** @var \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager */
    $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
    foreach ($field_type_manager->getDefinitions() as $plugin_id => $definition) {
        $class = $definition['class'];
        $property = $class::mainPropertyName();
        $storage_definition = BaseFieldDefinition::create($plugin_id);
        $property_definitions = $class::propertyDefinitions($storage_definition);
        $properties = implode(', ', array_keys($property_definitions));
        if (!empty($property_definitions)) {
            $message = sprintf("%s property %s found in %s", $plugin_id, $property, $properties);
            $this->assertArrayHasKey($property, $class::propertyDefinitions($storage_definition), $message);
        }
    }
}

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