function UniqueFieldConstraintTest::testEntityWithStringIdWithViolation

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

@covers ::validate

@dataProvider providerTestEntityWithStringIdWithViolation

Parameters

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

File

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

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 = 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());
}

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