function 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.'));
}

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