function TypedConfigTest::testNotBlankInteractionWithNotNull

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

Tests the behavior of `NotBlank` on required data.

@testWith ["", false, "This value should not be blank."] ["", true, "This value should not be blank."] [null, false, "This value should not be blank."] [null, true, "This value should not be null."]

See also

\Drupal\Core\TypedData\DataDefinition::getConstraints()

\Drupal\Core\TypedData\DataDefinitionInterface::isRequired()

\Drupal\Core\Validation\Plugin\Validation\Constraint\NotNullConstraint

\Symfony\Component\Validator\Constraints\NotBlank::$allowNull

File

core/tests/Drupal/KernelTests/Config/TypedConfigTest.php, line 129

Class

TypedConfigTest
Tests config validation mechanism.

Namespace

Drupal\KernelTests\Config

Code

public function testNotBlankInteractionWithNotNull(?string $value, bool $is_required, string $expected_message) : void {
    \Drupal::configFactory()->getEditable('config_test.validation')
        ->set('string__not_blank', $value)
        ->save();
    $typed_config = \Drupal::service('config.typed')->get('config_test.validation');
    $typed_config->get('string__not_blank')
        ->getDataDefinition()
        ->setRequired($is_required);
    $result = $typed_config->validate();
    // Expect 1 validation error message: the one from `NotBlank` or `NotNull`.
    $this->assertCount(1, $result);
    $this->assertSame('string__not_blank', $result->get(0)
        ->getPropertyPath());
    $this->assertEquals($expected_message, $result->get(0)
        ->getMessage());
}

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