function CommentValidationTest::assertLengthViolation

Same name in other branches
  1. 9 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.

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 193

Class

CommentValidationTest
Tests comment validation constraints.

Namespace

Drupal\Tests\comment\Kernel

Code

protected function assertLengthViolation(CommentInterface $comment, $field_name, $length) {
    $violations = $comment->validate();
    $this->assertCount(1, $violations, "Violation found when {$field_name} is too long.");
    $this->assertEqual($violations[0]->getPropertyPath(), "{$field_name}.0.value");
    $field_label = $comment->get($field_name)
        ->getFieldDefinition()
        ->getLabel();
    $this->assertEqual($violations[0]->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.