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. 10 core/modules/user/tests/src/Kernel/UserValidationTest.php \Drupal\Tests\user\Kernel\UserValidationTest::assertLengthViolation()
  3. 11.x 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 200

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->assertEqual(count($violations), $count, "Violation found when {$field_name} is too long.");
    $this->assertEqual($violations[$expected_index]->getPropertyPath(), "{$field_name}.0.value");
    $field_label = $entity->get($field_name)
        ->getFieldDefinition()
        ->getLabel();
    $this->assertEqual($violations[$expected_index]->getMessage(), t('%name: may not be longer than @max characters.', [
        '%name' => $field_label,
        '@max' => $length,
    ]));
}

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