function UniqueFieldConstraintTest::testEntityWithStringIdWithViolation

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringIdWithViolation()
  2. 10 core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringIdWithViolation()
  3. 9 core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringIdWithViolation()
  4. 8.9.x 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.

@legacy-covers ::validate

Attributes

#[DataProvider('providerTestEntityWithStringIdWithViolation')]

Parameters

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

File

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

Class

UniqueFieldConstraintTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testEntityWithStringIdWithViolation($id) : void {
  $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 = 'A ' . $entity->getEntityType()
    ->getSingularLabel() . ' with Name ' . Html::escape($value) . ' already exists.';
  // Check that the validation has created the appropriate violation.
  $this->assertCount(1, $violations);
  $this->assertEquals($message, $violations[0]->getMessage());
}

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