EntityTypeConstraintsTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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->entityTypeManager->getDefinition('entity_test_constraints');
  23. $default_constraints = [
  24. 'NotNull' => [],
  25. 'EntityChanged' => NULL,
  26. 'EntityUntranslatableFields' => NULL,
  27. ];
  28. $this->assertEqual($default_constraints, $entity_type->getConstraints());
  29. // Enable our test module and test extending constraints.
  30. $this->enableModules(['entity_test_constraints']);
  31. $this->container->get('module_handler')->resetImplementations();
  32. $extra_constraints = ['Test' => []];
  33. $this->state->set('entity_test_constraints.build', $extra_constraints);
  34. // Re-fetch the entity manager from the new container built after the new
  35. // modules were enabled.
  36. $this->entityTypeManager = $this->container->get('entity_type.manager');
  37. $this->entityTypeManager->clearCachedDefinitions();
  38. $entity_type = $this->entityTypeManager->getDefinition('entity_test_constraints');
  39. $this->assertEqual($default_constraints + $extra_constraints, $entity_type->getConstraints());
  40. // Test altering constraints.
  41. $altered_constraints = ['Test' => ['some_setting' => TRUE]];
  42. $this->state->set('entity_test_constraints.alter', $altered_constraints);
  43. // Clear the cache in state instance in the Drupal container, so it can pick
  44. // up the modified value.
  45. \Drupal::state()->resetCache();
  46. $this->entityTypeManager->clearCachedDefinitions();
  47. $entity_type = $this->entityTypeManager->getDefinition('entity_test_constraints');
  48. $this->assertEqual($altered_constraints, $entity_type->getConstraints());
  49. }
  50. /**
  51. * Tests entity constraints are validated.
  52. */
  53. public function testConstraintValidation() {
  54. $entity = $this->entityTypeManager->getStorage('entity_test_constraints')->create();
  55. $entity->user_id->target_id = 0;
  56. $violations = $entity->validate();
  57. $this->assertEqual($violations->count(), 0, 'Validation passed.');
  58. $entity->save();
  59. $entity->changed->value = REQUEST_TIME - 86400;
  60. $violations = $entity->validate();
  61. $this->assertEqual($violations->count(), 1, 'Validation failed.');
  62. $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.'));
  63. }
  64. }