function UniqueValuesConstraintValidatorTest::testValidationCaseInsensitive

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

Tests the UniqueField validation constraint validator with regards to case-insensitivity.

Case 5. Try to create another entity with existing value for unique field with different capitalization.

@covers ::validate

Throws

\Drupal\Core\Entity\EntityStorageException

File

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

Class

UniqueValuesConstraintValidatorTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testValidationCaseInsensitive() : void {
    // Create entity with two values for the testing field.
    $definition = [
        'id' => (int) rand(0, getrandmax()),
        'user_id' => 0,
        'field_test_text' => [
            'text1',
            'text2',
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $entity->save();
    // Create another entity with two values for the testing field, one identical
    // to other value, but with different capitalization which should still trigger a validation error.
    $definition = [
        'id' => (int) rand(0, getrandmax()),
        'user_id' => 0,
        'field_test_text' => [
            'Text1',
            'text3',
        ],
    ];
    $entity = EntityTestUniqueConstraint::create($definition);
    $violations = $entity->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('field_test_text.0', $violations[0]->getPropertyPath());
}

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