You are here

public function JsonItemTest::testCharacterLimit in JSON Field 8

Test character limit constraints.

@dataProvider providerTestCharacterLimit

File

tests/src/Kernel/JsonItemTest.php, line 71

Class

JsonItemTest
@coversDefaultClass \Drupal\json_field\Plugin\Field\FieldType\JSONItem

Namespace

Drupal\Tests\json_field\Kernel

Code

public function testCharacterLimit($size, $limit) {
  $storage = [
    'settings' => [
      'size' => $size,
    ],
  ];
  $this
    ->createTestField($storage);
  $entity = EntityTest::create([
    // Valid JSON 1 character larger than $limit.
    'test_json_field' => '"' . str_repeat('x', $limit - 1) . '"',
  ]);
  $constraint_list = $entity
    ->validate()
    ->getByField('test_json_field');
  $this
    ->assertEquals(1, $constraint_list
    ->count());

  /** @var \Symfony\Component\Validator\ConstraintViolation $violation */
  $violation = $constraint_list
    ->get(0);
  $this
    ->assertTrue($violation
    ->getConstraint() instanceof LengthConstraint);
}