function EntityTypeConstraintsTest::testConstraintValidation

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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->assertEquals(0, $violations->count(), 'Validation passed.');
  $entity->save();
  $entity->changed->value = REQUEST_TIME - 86400;
  $violations = $entity->validate();
  $this->assertEquals(1, $violations->count(), 'Validation failed.');
  $this->assertEquals('The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.', $violations[0]->getMessage());
}

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