function ConfigEntityValidationTestBase::testRequiredPropertyKeysMissing

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

A property that is required must have a value (i.e. not NULL).

@todo Remove this optional parameter in https://www.drupal.org/project/drupal/issues/2820364#comment-15333069

Parameters

string[]|null $additional_expected_validation_errors_when_missing: Some required config entity properties have additional validation constraints that cause additional messages to appear. Keys must be config entity properties, values must be arrays as expected by ::assertValidationErrors().

Return value

void

2 calls to ConfigEntityValidationTestBase::testRequiredPropertyKeysMissing()
EditorValidationTest::testRequiredPropertyKeysMissing in core/modules/editor/tests/src/Kernel/EditorValidationTest.php
A property that is required must have a value (i.e. not NULL).
FieldConfigValidationTest::testRequiredPropertyKeysMissing in core/modules/field/tests/src/Kernel/Entity/FieldConfigValidationTest.php
A property that is required must have a value (i.e. not NULL).
2 methods override ConfigEntityValidationTestBase::testRequiredPropertyKeysMissing()
EditorValidationTest::testRequiredPropertyKeysMissing in core/modules/editor/tests/src/Kernel/EditorValidationTest.php
A property that is required must have a value (i.e. not NULL).
FieldConfigValidationTest::testRequiredPropertyKeysMissing in core/modules/field/tests/src/Kernel/Entity/FieldConfigValidationTest.php
A property that is required must have a value (i.e. not NULL).

File

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

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testRequiredPropertyKeysMissing(?array $additional_expected_validation_errors_when_missing = NULL) : void {
    $config_entity_properties = array_keys($this->entity
        ->getEntityType()
        ->getPropertiesToExport());
    if (!empty(array_diff(array_keys($additional_expected_validation_errors_when_missing ?? []), $config_entity_properties))) {
        throw new \LogicException(sprintf('The test %s lists `%s` in $additional_expected_validation_errors_when_missing but it is not a property of the `%s` config entity type.', get_called_class(), implode(', ', array_diff(array_keys($additional_expected_validation_errors_when_missing), $config_entity_properties)), $this->entity
            ->getEntityTypeId()));
    }
    $mapping_properties = array_keys(array_filter(ConfigEntityAdapter::createFromEntity($this->entity)
        ->getProperties(FALSE), fn(TypedDataInterface $v) => $v instanceof Mapping));
    $required_property_keys = $this->getRequiredPropertyKeys();
    if (!$this->isFullyValidatable()) {
        $this->assertEmpty($required_property_keys, 'No keys can be required when a config entity type is not fully validatable.');
    }
    $original_entity = clone $this->entity;
    foreach ($mapping_properties as $property) {
        $this->entity = clone $original_entity;
        $this->entity
            ->set($property, []);
        $expected_validation_errors = array_key_exists($property, $required_property_keys) ? [
            $property => $required_property_keys[$property],
        ] : [];
        $this->assertValidationErrors(($additional_expected_validation_errors_when_missing[$property] ?? []) + $expected_validation_errors);
    }
}

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