You are here

protected function UserValidationTest::assertLengthViolation in Drupal 9

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

Verifies that a length violation exists for the given field.

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.

1 call to UserValidationTest::assertLengthViolation()
UserValidationTest::testValidation in core/modules/user/tests/src/Kernel/UserValidationTest.php
Runs entity validation checks.

File

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

Class

UserValidationTest
Verify that user validity checks behave as designed.

Namespace

Drupal\Tests\user\Kernel

Code

protected function assertLengthViolation(EntityInterface $entity, $field_name, $length, $count = 1, $expected_index = 0) {
  $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());
}