function ConfigEntityValidationTestBase::getRequiredPropertyKeys

Same name and namespace in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase::getRequiredPropertyKeys()

Determines the config entity mapping properties with required keys.

This refers only to the top-level properties of the config entity which are expected to be mappings, and of those mappings, only the ones which have required keys.

Return value

string[] An array of key-value pairs, with:

  • keys: names of the config entity properties which are mappings that contain required keys.
  • values: the corresponding expected validation error message.
1 call to ConfigEntityValidationTestBase::getRequiredPropertyKeys()
ConfigEntityValidationTestBase::testRequiredPropertyKeysMissing in core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php
A property that is required must have a value (i.e. not NULL).

File

core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php, line 642

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

protected function getRequiredPropertyKeys() : array {
    // If a config entity type is not fully validatable, no mapping property
    // keys are required.
    if (!$this->isFullyValidatable()) {
        return [];
    }
    $config_entity_properties = array_keys($this->entity
        ->getEntityType()
        ->getPropertiesToExport());
    // Otherwise, all mapping property keys are required except for those marked
    // optional. Rather than inspecting config schema, require authors of tests
    // to explicitly list optional properties in a `propertiesWithRequiredKeys`
    // property on this class.
    // @see \Drupal\KernelTests\Config\Schema\MappingTest::testMappingInterpretation()
    $class = static::class;
    $properties_with_required_keys = [];
    while ($class) {
        if (property_exists($class, 'propertiesWithRequiredKeys')) {
            $properties_with_required_keys += $class::$propertiesWithRequiredKeys;
        }
        $class = get_parent_class($class);
    }
    // Guide developers when $propertiesWithRequiredKeys does not contain
    // sensible values.
    if (!empty(array_diff(array_keys($properties_with_required_keys), $config_entity_properties))) {
        throw new \LogicException(sprintf('The %s test class lists %s in $propertiesWithRequiredKeys but it is not a property of the %s config entity type.', get_called_class(), implode(', ', array_diff(array_keys($properties_with_required_keys), $config_entity_properties)), $this->entity
            ->getEntityTypeId()));
    }
    return $properties_with_required_keys;
}

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