You are here

public function EntityTypeConstraintsTest::testConstraintValidation in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php \Drupal\KernelTests\Core\Entity\EntityTypeConstraintsTest::testConstraintValidation()
  2. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php \Drupal\KernelTests\Core\Entity\EntityTypeConstraintsTest::testConstraintValidation()

Tests entity constraints are validated.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityTypeConstraintsTest.php, line 61

Class

EntityTypeConstraintsTest
Tests entity level validation constraints.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testConstraintValidation() {
  $entity = $this->entityTypeManager
    ->getStorage('entity_test_constraints')
    ->create();
  $entity->user_id->target_id = 0;
  $violations = $entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 0, 'Validation passed.');
  $entity
    ->save();
  $entity->changed->value = REQUEST_TIME - 86400;
  $violations = $entity
    ->validate();
  $this
    ->assertEqual($violations
    ->count(), 1, 'Validation failed.');
  $this
    ->assertEqual($violations[0]
    ->getMessage(), t('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.'));
}