function UniqueFieldConstraintTest::testEntityWithStringId

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringId()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringId()
  3. 11.x core/tests/Drupal/KernelTests/Core/Validation/UniqueFieldConstraintTest.php \Drupal\KernelTests\Core\Validation\UniqueFieldConstraintTest::testEntityWithStringId()

Tests cases where the validation passes for entities with string IDs.

@covers ::validate

File

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

Class

UniqueFieldConstraintTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testEntityWithStringId() : void {
  $this->installEntitySchema('entity_test_string_id');
  EntityTestStringId::create([
    'id' => 'foo',
    'name' => $this->randomString(),
  ])
    ->save();
  // Reload the entity.
  $entity = EntityTestStringId::load('foo');
  // Check that an existing entity validates when the value is preserved.
  $violations = $entity->name
    ->validate();
  $this->assertCount(0, $violations);
  // Create a new entity with a different ID and a different field value.
  EntityTestStringId::create([
    'id' => 'bar',
    'name' => $this->randomString(),
  ]);
  // Check that a new entity with a different field value validates.
  $violations = $entity->name
    ->validate();
  $this->assertCount(0, $violations);
}

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