You are here

public function UniqueFieldConstraintTest::testEntityWithStringIdWithViolation in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringIdWithViolation()

Tests cases when validation raises violations for entities with string IDs.

@covers ::validate

@dataProvider providerTestEntityWithStringIdWithViolation

Parameters

string|int|null $id: The entity ID.

File

core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php, line 68

Class

UniqueFieldConstraintTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testEntityWithStringIdWithViolation($id) {
  $this
    ->installEntitySchema('entity_test_string_id');
  $value = $this
    ->randomString();
  EntityTestStringId::create([
    'id' => 'first_entity',
    'name' => $value,
  ])
    ->save();
  $entity = EntityTestStringId::create([
    'id' => $id,
    'name' => $value,
  ]);

  /** @var \Symfony\Component\Validator\ConstraintViolationList $violations */
  $violations = $entity
    ->get('name')
    ->validate();
  $message = new FormattableMarkup('A @entity_type with @field_name %value already exists.', [
    '%value' => $value,
    '@entity_type' => $entity
      ->getEntityType()
      ->getSingularLabel(),
    '@field_name' => 'name',
  ]);

  // Check that the validation has created the appropriate violation.
  $this
    ->assertCount(1, $violations);
  $this
    ->assertEquals($message, $violations[0]
    ->getMessage());
}