function UniqueValuesConstraintValidatorTest::testValidationMultiple

Same name in other branches
  1. 11.x core/tests/Drupal/KernelTests/Core/Validation/UniqueValuesConstraintValidatorTest.php \Drupal\KernelTests\Core\Validation\UniqueValuesConstraintValidatorTest::testValidationMultiple()

Tests the UniqueField validation constraint validator for multiple violations.

Case 4. Try to add multiple existing values for unique field in the same entity.

@covers ::validate

Throws

\Drupal\Core\Entity\EntityStorageException

File

core/tests/Drupal/KernelTests/Core/Validation/UniqueValuesConstraintValidatorTest.php, line 241

Class

UniqueValuesConstraintValidatorTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testValidationMultiple() : void {
    // Create entity with two different values for the testing field.
    $definition = [
        'user_id' => 0,
        'field_test_text' => [
            'multi0',
            'multi1',
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    $entity->save();
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    // Create new entity with three identical values in unique field.
    $definition = [
        'user_id' => 0,
        'field_test_text' => [
            'multi2',
            'multi2',
            'multi2',
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(2, $violations);
    $this->assertEquals('field_test_text.1', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_field_test %s already exists.', $definition['field_test_text'][1]), $violations[0]->getMessage());
    $this->assertEquals('field_test_text.2', $violations[1]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_field_test %s already exists.', $definition['field_test_text'][2]), $violations[1]->getMessage());
    // Create new entity with two identical values and one existing value in unique field.
    $definition = [
        'user_id' => 0,
        'field_test_text' => [
            'multi3',
            'multi1',
            'multi3',
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(2, $violations);
    $this->assertEquals('field_test_text.1', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_field_test %s already exists.', $definition['field_test_text'][1]), $violations[0]->getMessage());
    $this->assertEquals('field_test_text.2', $violations[1]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_field_test %s already exists.', $definition['field_test_text'][2]), $violations[1]->getMessage());
}

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