EntityTypeConstraintsTest.php in Zircon Profile 8.0
File
core/modules/system/src/Tests/Entity/EntityTypeConstraintsTest.php
View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\system\Tests\TypedData;
class EntityTypeConstraintsTest extends EntityUnitTestBase {
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('entity_test_constraints');
}
public function testConstraintDefinition() {
$entity_type = $this->entityManager
->getDefinition('entity_test_constraints');
$default_constraints = [
'NotNull' => [],
'EntityChanged' => NULL,
];
$this
->assertEqual($default_constraints, $entity_type
->getConstraints());
$this
->enableModules(array_merge(static::$modules, [
'entity_test_constraints',
]));
$this->container
->get('module_handler')
->resetImplementations();
$extra_constraints = [
'Test' => [],
];
$this->state
->set('entity_test_constraints.build', $extra_constraints);
$this->entityManager = $this->container
->get('entity.manager');
$this->entityManager
->clearCachedDefinitions();
$entity_type = $this->entityManager
->getDefinition('entity_test_constraints');
$this
->assertEqual($default_constraints + $extra_constraints, $entity_type
->getConstraints());
$altered_constraints = [
'Test' => [
'some_setting' => TRUE,
],
];
$this->state
->set('entity_test_constraints.alter', $altered_constraints);
\Drupal::state()
->resetCache();
$this->entityManager
->clearCachedDefinitions();
$entity_type = $this->entityManager
->getDefinition('entity_test_constraints');
$this
->assertEqual($altered_constraints, $entity_type
->getConstraints());
}
public function testConstraintValidation() {
$entity = $this->entityManager
->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.'));
}
}