function ConfigEntityValidationTestBase::testRequiredPropertyValuesMissing

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

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::testRequiredPropertyValuesMissing()
EditorValidationTest::testRequiredPropertyValuesMissing in core/modules/editor/tests/src/Kernel/EditorValidationTest.php
A property that is required must have a value (i.e. not NULL).
FieldConfigValidationTest::testRequiredPropertyValuesMissing 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::testRequiredPropertyValuesMissing()
EditorValidationTest::testRequiredPropertyValuesMissing in core/modules/editor/tests/src/Kernel/EditorValidationTest.php
A property that is required must have a value (i.e. not NULL).
FieldConfigValidationTest::testRequiredPropertyValuesMissing 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 545

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testRequiredPropertyValuesMissing(?array $additional_expected_validation_errors_when_missing = NULL) : void {
  $config_entity_properties = array_keys($this->entity
    ->getEntityType()
    ->getPropertiesToExport());
  // Guide developers when $additional_expected_validation_errors_when_missing
  // does not contain sensible values.
  $non_existing_properties = array_diff(array_keys($additional_expected_validation_errors_when_missing ?? []), $config_entity_properties);
  if ($non_existing_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.', __METHOD__, implode(', ', $non_existing_properties), $this->entity
      ->getEntityTypeId()));
  }
  $properties_with_optional_values = $this->getPropertiesWithOptionalValues();
  // Get the config entity properties that are immutable.
  // @see ::testImmutableProperties()
  $immutable_properties = $this->entity
    ->getEntityType()
    ->getConstraints()['ImmutableProperties'];
  // Config entity properties containing plugin collections are special cases:
  // setting them to NULL would cause them to get out of sync with the plugin
  // collection.
  // @see \Drupal\Core\Config\Entity\ConfigEntityBase::set()
  // @see \Drupal\Core\Config\Entity\ConfigEntityBase::preSave()
  $plugin_collection_properties = $this->entity instanceof EntityWithPluginCollectionInterface ? array_keys($this->entity
    ->getPluginCollections()) : [];
  // To test properties with missing required values, $this->entity must be
  // modified to be able to use ::assertValidationErrors(). To allow restoring
  // $this->entity to its original value for each tested property, a clone of
  // the original entity is needed.
  $original_entity = clone $this->entity;
  foreach ($config_entity_properties as $property) {
    // Do not try to set immutable properties to NULL: their immutability is
    // already tested.
    // @see ::testImmutableProperties()
    if (in_array($property, $immutable_properties, TRUE)) {
      continue;
    }
    // Do not try to set plugin collection properties to NULL.
    if (in_array($property, $plugin_collection_properties, TRUE)) {
      continue;
    }
    $this->entity = clone $original_entity;
    $this->entity
      ->set($property, NULL);
    $expected_validation_errors = in_array($property, $properties_with_optional_values, TRUE) ? [] : [
      $property => 'This value should not be null.',
    ];
    // @see `type: required_label`
    // @see \Symfony\Component\Validator\Constraints\NotBlank
    if (!$this->isFullyValidatable() && $this->entity
      ->getEntityType()
      ->getKey('label') == $property) {
      $expected_validation_errors = [
        $property => 'This value should not be blank.',
      ];
    }
    $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.