function ConfigEntityValidationTestBase::getPropertiesWithOptionalValues

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

Determines the config entity properties with optional values.

Return value

string[] The config entity properties whose values are optional.

1 call to ConfigEntityValidationTestBase::getPropertiesWithOptionalValues()
ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing 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 685

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

protected function getPropertiesWithOptionalValues() : array {
    $config_entity_properties = array_keys($this->entity
        ->getEntityType()
        ->getPropertiesToExport());
    // If a config entity type is not fully validatable, all properties are
    // optional, with the exception of `type: langcode` and
    // `type: required_label`.
    if (!$this->isFullyValidatable()) {
        return array_diff($config_entity_properties, [
            // @see `type: langcode`
            // @see \Symfony\Component\Validator\Constraints\NotNull
'langcode',
            'default_langcode',
            // @see `type: required_label`
            // @see \Symfony\Component\Validator\Constraints\NotBlank
$this->entity
                ->getEntityType()
                ->getKey('label'),
        ]);
    }
    // Otherwise, all properties are required except for those marked
    // optional. Rather than inspecting config schema, require authors of tests
    // to explicitly list optional properties in a
    // `propertiesWithOptionalValues` property on this class.
    $class = static::class;
    $optional_properties = [];
    while ($class) {
        if (property_exists($class, 'propertiesWithOptionalValues')) {
            $optional_properties = array_merge($optional_properties, $class::$propertiesWithOptionalValues);
        }
        $class = get_parent_class($class);
    }
    $optional_properties = array_unique($optional_properties);
    // Guide developers when $optionalProperties does not contain sensible
    // values.
    $non_existing_properties = array_diff($optional_properties, $config_entity_properties);
    if ($non_existing_properties) {
        throw new \LogicException(sprintf('The %s test class lists %s in $optionalProperties but it is not a property of the %s config entity type.', static::class, implode(', ', $non_existing_properties), $this->entity
            ->getEntityTypeId()));
    }
    return $optional_properties;
}

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