function UniqueValuesConstraintValidatorTest::testValidationReference

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

Tests the UniqueField validation constraint validator for entity reference fields.

Case 2. Try to create another entity with existing reference for unique field.

@covers ::validate

Throws

\Drupal\Core\Entity\EntityStorageException

File

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

Class

UniqueValuesConstraintValidatorTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testValidationReference() : void {
    $users = [];
    for ($i = 0; $i <= 5; $i++) {
        $users[$i] = $this->createUser();
    }
    // Create new entity with two identical references.
    $definition = [
        'user_id' => 0,
        'field_test_reference' => [
            $users[0]->id(),
            $users[0]->id(),
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('field_test_reference.1', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_reference_test %s already exists.', $definition['field_test_reference'][1]), $violations[0]->getMessage());
    // Create entity with two references for the testing field.
    $definition = [
        'user_id' => 0,
        'field_test_reference' => [
            $users[1]->id(),
            $users[2]->id(),
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    $entity->save();
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    // Create another entity with two references for the testing field.
    $definition = [
        'user_id' => 0,
        'field_test_reference' => [
            $users[3]->id(),
            $users[4]->id(),
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    $entity->save();
    $violations = $entity->validate();
    $this->assertCount(0, $violations);
    // Create another entity with two references, but one reference is existing.
    $definition = [
        'user_id' => 0,
        'field_test_reference' => [
            $users[5]->id(),
            $users[1]->id(),
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('field_test_reference.1', $violations[0]->getPropertyPath());
    $this->assertEquals(sprintf('A unique field entity with unique_reference_test %s already exists.', $definition['field_test_reference'][1]), $violations[0]->getMessage());
}

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