function ConfigEntityValidationTestBase::assertValidationErrors

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

Asserts a set of validation errors is raised when the entity is validated.

Parameters

array<string, string|string[]> $expected_messages: The expected validation error messages. Keys are property paths, values are the expected messages: a string if a single message is expected, an array of strings if multiple are expected.

41 calls to ConfigEntityValidationTestBase::assertValidationErrors()
ActionValidationTest::testInvalidPluginId in core/modules/system/tests/src/Kernel/Entity/ActionValidationTest.php
Tests that the action plugin ID is validated.
BaseFieldOverrideValidationTest::testFieldTypePluginIsValidated in core/tests/Drupal/KernelTests/Core/Entity/BaseFieldOverrideValidationTest.php
Tests that the field type plugin's existence is validated.
BaseFieldOverrideValidationTest::testTargetBundleMustExist in core/tests/Drupal/KernelTests/Core/Entity/BaseFieldOverrideValidationTest.php
Tests that the target bundle of the field is checked.
BlockValidationTest::testInvalidPluginId in core/modules/block/tests/src/Kernel/BlockValidationTest.php
Tests validating a block with an unknown plugin ID.
BlockValidationTest::testLabelValidation in core/modules/block/tests/src/Kernel/BlockValidationTest.php
Tests validation of config entity's label.

... See full list

File

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

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

protected function assertValidationErrors(array $expected_messages) : void {
    
    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
    $typed_data = $this->container
        ->get('typed_data_manager');
    $definition = $typed_data->createDataDefinition('entity:' . $this->entity
        ->getEntityTypeId());
    $violations = $typed_data->create($definition, $this->entity)
        ->validate();
    $actual_messages = [];
    foreach ($violations as $violation) {
        $property_path = $violation->getPropertyPath();
        if (!isset($actual_messages[$property_path])) {
            $actual_messages[$property_path] = (string) $violation->getMessage();
        }
        else {
            // Transform value from string to array.
            if (is_string($actual_messages[$property_path])) {
                $actual_messages[$property_path] = (array) $actual_messages[$violation->getPropertyPath()];
            }
            // And append.
            $actual_messages[$property_path][] = (string) $violation->getMessage();
        }
    }
    ksort($expected_messages);
    ksort($actual_messages);
    $this->assertSame($expected_messages, $actual_messages);
}

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