function CommentValidationTest::assertLengthViolation

Same name in other branches
  1. 8.9.x core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::assertLengthViolation()
  2. 10 core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::assertLengthViolation()
  3. 11.x core/modules/comment/tests/src/Kernel/CommentValidationTest.php \Drupal\Tests\comment\Kernel\CommentValidationTest::assertLengthViolation()

Verifies that a length violation exists for the given field.

@internal

Parameters

\Drupal\comment\CommentInterface $comment: The comment object to validate.

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

int $length: Number of characters that was exceeded.

1 call to CommentValidationTest::assertLengthViolation()
CommentValidationTest::testValidation in core/modules/comment/tests/src/Kernel/CommentValidationTest.php
Tests the comment validation constraints.

File

core/modules/comment/tests/src/Kernel/CommentValidationTest.php, line 195

Class

CommentValidationTest
Tests comment validation constraints.

Namespace

Drupal\Tests\comment\Kernel

Code

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

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