You are here

public function UniqueFieldConstraintTest::testEntityWithStringId in Drupal 8

Same name and namespace in other branches
  1. 9 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 32

Class

UniqueFieldConstraintTest
Tests the unique field value validation constraint.

Namespace

Drupal\KernelTests\Core\Validation

Code

public function testEntityWithStringId() {
  $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);
}