function ExtensionNameConstraintTest::testValidation

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php \Drupal\KernelTests\Core\Extension\ExtensionNameConstraintTest::testValidation()

Tests the ExtensionName constraint.

File

core/tests/Drupal/KernelTests/Core/Extension/ExtensionNameConstraintTest.php, line 27

Class

ExtensionNameConstraintTest
Tests the ExtensionName constraint.

Namespace

Drupal\KernelTests\Core\Extension

Code

public function testValidation() : void {
    // Create a data definition that specifies the value must be a string with
    // the name of a valid extension.
    $definition = DataDefinition::create('string')->addConstraint('ExtensionName');
    
    /** @var \Drupal\Core\TypedData\TypedDataManagerInterface $typed_data */
    $typed_data = $this->container
        ->get('typed_data_manager');
    $data = $typed_data->create($definition, 'user');
    $this->assertCount(0, $data->validate());
    $data->setValue('invalid-name');
    $violations = $data->validate();
    $this->assertCount(1, $violations);
    $this->assertSame('This value is not valid.', (string) $violations->get(0)
        ->getMessage());
}

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