View source
<?php
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase;
use Drupal\language\Entity\ConfigurableLanguage;
class EntityValidationTest extends EntityKernelTestBase {
public static $modules = [
'filter',
'text',
'language',
];
protected $entityName;
protected $entityUser;
protected $entityFieldText;
protected function setUp() {
parent::setUp();
ConfigurableLanguage::createFromLangcode('de')
->save();
$this
->installEntitySchema('entity_test_mul');
$this
->installEntitySchema('entity_test_mul_langcode_key');
$this
->installEntitySchema('entity_test_mul_changed');
$this
->installEntitySchema('entity_test_rev');
$this
->installEntitySchema('entity_test_mulrev');
$this
->installEntitySchema('entity_test_mulrev_changed');
module_load_install('entity_test');
entity_test_install();
$this
->installConfig([
'system',
'filter',
]);
}
protected function createTestEntity($entity_type) {
$this->entityName = $this
->randomMachineName();
$this->entityUser = $this
->createUser();
$entity = $this->container
->get('entity_type.manager')
->getStorage($entity_type)
->create();
$entity->user_id->target_id = $this->entityUser
->id();
$entity->name->value = $this->entityName;
if ($entity
->hasField('field_test_text')) {
$this->entityFieldText = $this
->randomMachineName();
$entity->field_test_text->value = $this->entityFieldText;
}
return $entity;
}
public function testValidation() {
\Drupal::service('plugin.cache_clearer');
$plugin_cache_clearer = \Drupal::service('drupal.proxy_original_service.plugin.cache_clearer');
$get_cached_discoveries = function () {
return $this->cachedDiscoveries;
};
$get_cached_discoveries = $get_cached_discoveries
->bindTo($plugin_cache_clearer, $plugin_cache_clearer);
$cached_discoveries = $get_cached_discoveries();
$cached_discovery_classes = [];
foreach ($cached_discoveries as $cached_discovery) {
$cached_discovery_classes[] = get_class($cached_discovery);
}
$this
->assertContains('Drupal\\Core\\Validation\\ConstraintManager', $cached_discovery_classes);
foreach (entity_test_entity_types() as $entity_type) {
$this
->checkValidation($entity_type);
}
}
protected function checkValidation($entity_type) {
$entity = $this
->createTestEntity($entity_type);
$violations = $entity
->validate();
$this
->assertEqual($violations
->count(), 0, 'Validation passes.');
$test_entity = clone $entity;
$test_entity->id->value = -1;
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('%name: The integer must be larger or equal to %min.', [
'%name' => 'ID',
'%min' => 0,
]));
$test_entity = clone $entity;
$test_entity->uuid->value = $this
->randomString(129);
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('%name: may not be longer than @max characters.', [
'%name' => 'UUID',
'@max' => 128,
]));
$test_entity = clone $entity;
$langcode_key = $this->entityTypeManager
->getDefinition($entity_type)
->getKey('langcode');
$test_entity->{$langcode_key}->value = $this
->randomString(13);
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 2, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('This value is too long. It should have %limit characters or less.', [
'%limit' => '12',
]));
$this
->assertEqual($violations[1]
->getMessage(), t('The value you selected is not a valid choice.'));
$test_entity = clone $entity;
$test_entity->type->value = NULL;
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('This value should not be null.'));
$test_entity = clone $entity;
$test_entity->name->value = $this
->randomString(33);
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('%name: may not be longer than @max characters.', [
'%name' => 'Name',
'@max' => 32,
]));
$violation = $violations[0];
$this
->assertEqual($violation
->getRoot()
->getValue(), $test_entity, 'Violation root is entity.');
$this
->assertEqual($violation
->getPropertyPath(), 'name.0.value', 'Violation property path is correct.');
$this
->assertEqual($violation
->getInvalidValue(), $test_entity->name->value, 'Violation contains invalid value.');
$test_entity = clone $entity;
$test_entity
->set('user_id', 9999);
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('The referenced entity (%type: %id) does not exist.', [
'%type' => 'user',
'%id' => 9999,
]));
$test_entity = clone $entity;
$test_entity->field_test_text->format = $this
->randomString(33);
$violations = $test_entity
->validate();
$this
->assertEqual($violations
->count(), 1, 'Validation failed.');
$this
->assertEqual($violations[0]
->getMessage(), t('The value you selected is not a valid choice.'));
$violation = $violations[0];
$this
->assertEqual($violation
->getRoot()
->getValue(), $test_entity, 'Violation root is entity.');
$this
->assertEqual($violation
->getPropertyPath(), 'field_test_text.0.format', 'Violation property path is correct.');
$this
->assertEqual($violation
->getInvalidValue(), $test_entity->field_test_text->format, 'Violation contains invalid value.');
}
public function testCompositeConstraintValidation() {
$entity = $this
->createTestEntity('entity_test_composite_constraint');
$violations = $entity
->validate();
$this
->assertEqual($violations
->count(), 0);
$entity->name->value = 'test';
$entity->type->value = 'test2';
$violations = $entity
->validate();
$this
->assertEqual($violations
->count(), 1);
$constraint = $violations[0]
->getConstraint();
$this
->assertInstanceOf(CompositeConstraintBase::class, $constraint);
$this
->assertEqual('type', $violations[0]
->getPropertyPath());
$this
->assertEqual($constraint
->coversFields(), [
'name',
'type',
], 'Information about covered fields can be retrieved.');
}
public function testEntityChangedConstraintOnConcurrentMultilingualEditing() {
$this
->installEntitySchema('entity_test_mulrev_changed');
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_mulrev_changed');
$entity = $this
->createTestEntity('entity_test_mulrev_changed');
$entity
->save();
$entity
->setChangedTime($entity
->getChangedTime() - 1);
$violations = $entity
->validate();
$this
->assertEquals(1, $violations
->count());
$this
->assertEqual($violations[0]
->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
$entity = $storage
->loadUnchanged($entity
->id());
$translation = $entity
->addTranslation('de');
$entity
->save();
$this
->assertGreaterThan($entity
->getChangedTime(), $translation
->getChangedTime());
$original_entity_time = $entity
->getChangedTime();
$entity
->set('not_translatable', $this
->randomString());
$entity
->save();
$entity
->setChangedTime($original_entity_time);
$violations = $entity
->validate();
$this
->assertEquals(1, $violations
->count());
$this
->assertEqual($violations[0]
->getMessage(), 'The content has either been modified by another user, or you have already submitted modifications. As a result, your changes cannot be saved.');
}
}