function UserValidationTest::assertLengthViolation

Same name in other branches
  1. 9 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::assertLengthViolation()
  2. 8.9.x core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::assertLengthViolation()
  3. 10 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.

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 206

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(sprintf('%s: may not be longer than %s characters.', $field_label, $length), $violations[$expected_index]->getMessage());
}

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