EntityTypeConstraintsTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Drupal\KernelTests\Core\Entity;
  3. /**
  4. * Tests entity level validation constraints.
  5. *
  6. * @group Entity
  7. */
  8. class EntityTypeConstraintsTest extends EntityKernelTestBase {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. protected function setUp() {
  13. parent::setUp();
  14. $this->installEntitySchema('entity_test_constraints');
  15. }
  16. /**
  17. * Tests defining entity constraints via entity type annotations and hooks.
  18. */
  19. public function testConstraintDefinition() {
  20. // Test reading the annotation. There should be two constraints, the defined
  21. // constraint and the automatically added EntityChanged constraint.
  22. $entity_type = $this->entityManager->getDefinition('entity_test_constraints');
  23. $default_constraints = ['NotNull' => [], 'EntityChanged' => NULL];
  24. $this->assertEqual($default_constraints, $entity_type->getConstraints());
  25. // Enable our test module and test extending constraints.
  26. $this->enableModules(['entity_test_constraints']);
  27. $this->container->get('module_handler')->resetImplementations();
  28. $extra_constraints = ['Test' => []];
  29. $this->state->set('entity_test_constraints.build', $extra_constraints);
  30. // Re-fetch the entity manager from the new container built after the new
  31. // modules were enabled.
  32. $this->entityManager = $this->container->get('entity.manager');
  33. $this->entityManager->clearCachedDefinitions();
  34. $entity_type = $this->entityManager->getDefinition('entity_test_constraints');
  35. $this->assertEqual($default_constraints + $extra_constraints, $entity_type->getConstraints());
  36. // Test altering constraints.
  37. $altered_constraints = ['Test' => ['some_setting' => TRUE]];
  38. $this->state->set('entity_test_constraints.alter', $altered_constraints);
  39. // Clear the cache in state instance in the Drupal container, so it can pick
  40. // up the modified value.
  41. \Drupal::state()->resetCache();
  42. $this->entityManager->clearCachedDefinitions();
  43. $entity_type = $this->entityManager->getDefinition('entity_test_constraints');
  44. $this->assertEqual($altered_constraints, $entity_type->getConstraints());
  45. }
  46. /**
  47. * Tests entity constraints are validated.
  48. */
  49. public function testConstraintValidation() {
  50. $entity = $this->entityManager->getStorage('entity_test_constraints')->create();
  51. $entity->user_id->target_id = 0;
  52. $violations = $entity->validate();
  53. $this->assertEqual($violations->count(), 0, 'Validation passed.');
  54. $entity->save();
  55. $entity->changed->value = REQUEST_TIME - 86400;
  56. $violations = $entity->validate();
  57. $this->assertEqual($violations->count(), 1, 'Validation failed.');
  58. $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.'));
  59. }
  60. }