function RoleExistsConstraintValidatorTest::testRoleExists

Tests when the constraint's entityTypeId value is not valid.

File

core/modules/user/tests/src/Kernel/Plugin/Validation/Constraint/RoleExistsConstraintValidatorTest.php, line 43

Class

RoleExistsConstraintValidatorTest
@group user @group Validation

Namespace

Drupal\Tests\user\Kernel\Plugin\Validation\Constraint

Code

public function testRoleExists() : void {
    // Validation error when role does not exist.
    $definition = DataDefinition::create('string')->addConstraint('RoleExists');
    $violations = $this->container
        ->get('typed_data_manager')
        ->create($definition, 'test_role')
        ->validate();
    $this->assertEquals('The role with id \'test_role\' does not exist.', $violations->get(0)
        ->getMessage());
    $this->assertCount(1, $violations);
    // Validation success when role exists.
    Role::create([
        'id' => 'test_role',
        'label' => 'Test role',
    ])->save();
    $definition = DataDefinition::create('string')->addConstraint('RoleExists');
    $violations = $this->container
        ->get('typed_data_manager')
        ->create($definition, 'test_role')
        ->validate();
    $this->assertCount(0, $violations);
}

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