You are here

protected function UserValidationTest::assertLengthViolation in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::assertLengthViolation()
  2. 9 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::assertLengthViolation()

Verifies that a length violation exists for the given field.

@internal

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity object to validate.

string $field_name: The field that violates the maximum length.

int $length: Number of characters that was exceeded.

int $count: (optional) The number of expected violations. Defaults to 1.

int $expected_index: (optional) The index at which to expect the violation. Defaults to 0.

File

core/modules/user/tests/src/Kernel/UserValidationTest.php, line 204

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

protected function assertLengthViolation(EntityInterface $entity, string $field_name, int $length, int $count = 1, int $expected_index = 0) : void {
  $violations = $entity
    ->validate();
  $this
    ->assertCount($count, $violations, "Violation found when {$field_name} is too long.");
  $this
    ->assertEquals("{$field_name}.0.value", $violations[$expected_index]
    ->getPropertyPath());
  $field_label = $entity
    ->get($field_name)
    ->getFieldDefinition()
    ->getLabel();
  $this
    ->assertEquals(t('%name: may not be longer than @max characters.', [
    '%name' => $field_label,
    '@max' => $length,
  ]), $violations[$expected_index]
    ->getMessage());
}