View source
<?php
namespace Drupal\system\Tests\Entity;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormState;
use Drupal\entity_test\Entity\EntityTestCompositeConstraint;
use Drupal\simpletest\KernelTestBase;
use Drupal\system\Tests\TypedData;
class FieldWidgetConstraintValidatorTest extends KernelTestBase {
public static $modules = array(
'entity_test',
'field',
'user',
'system',
);
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'router',
'key_value',
]);
$this->container
->get('router.builder')
->rebuild();
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test_composite_constraint');
}
public function testValidation() {
$entity_type = 'entity_test_constraint_violation';
$entity = entity_create($entity_type, array(
'id' => 1,
'revision_id' => 1,
));
$display = entity_get_form_display($entity_type, $entity_type, 'default');
$form = array();
$form_state = new FormState();
$display
->buildForm($entity, $form, $form_state);
$form_state
->setFormObject(\Drupal::entityManager()
->getFormObject($entity_type, 'default'));
\Drupal::formBuilder()
->prepareForm('field_test_entity_form', $form, $form_state);
\Drupal::formBuilder()
->processForm('field_test_entity_form', $form, $form_state);
$form_state
->getFormObject()
->setEntity($entity)
->setFormDisplay($display, $form_state);
$entity = $form_state
->getFormObject()
->buildEntity($form, $form_state);
$display
->validateFormValues($entity, $form, $form_state);
$errors = $form_state
->getErrors();
$this
->assertEqual($errors['name'], 'Widget constraint has failed.', 'Constraint violation is generated correctly');
}
protected function getErrorsForEntity(EntityInterface $entity, $hidden_fields = []) {
$entity_type_id = 'entity_test_composite_constraint';
$display = entity_get_form_display($entity_type_id, $entity_type_id, 'default');
foreach ($hidden_fields as $hidden_field) {
$display
->removeComponent($hidden_field);
}
$form = [];
$form_state = new FormState();
$display
->buildForm($entity, $form, $form_state);
$form_state
->setFormObject(\Drupal::entityManager()
->getFormObject($entity_type_id, 'default'));
\Drupal::formBuilder()
->prepareForm('field_test_entity_form', $form, $form_state);
\Drupal::formBuilder()
->processForm('field_test_entity_form', $form, $form_state);
$form_object = $form_state
->getFormObject();
$form_object
->setEntity($entity)
->setFormDisplay($display, $form_state)
->validateForm($form, $form_state);
return $form_state
->getErrors();
}
public function testValidationWithCompositeConstraint() {
$entity = EntityTestCompositeConstraint::create([
'name' => 'valid-value',
]);
$entity
->save();
$errors = $this
->getErrorsForEntity($entity);
$this
->assertFalse(isset($errors['name']));
$this
->assertFalse(isset($errors['type']));
$entity = EntityTestCompositeConstraint::create([
'name' => 'failure-field-name',
]);
$errors = $this
->getErrorsForEntity($entity);
$this
->assertTrue(isset($errors['name']));
$this
->assertFalse(isset($errors['type']));
$entity = EntityTestCompositeConstraint::create([
'name' => 'failure-field-name',
]);
$errors = $this
->getErrorsForEntity($entity, [
'type',
]);
$this
->assertTrue(isset($errors['name']));
$this
->assertFalse(isset($errors['type']));
$entity = EntityTestCompositeConstraint::create([
'name' => 'failure-field-name',
]);
$errors = $this
->getErrorsForEntity($entity, [
'name',
]);
$this
->assertFalse(isset($errors['name']));
$this
->assertTrue(isset($errors['type']));
$this
->assertEqual($errors['type'], SafeMarkup::format('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', [
'%field_name' => 'name',
]));
}
public function testEntityLevelConstraintValidation() {
$entity = EntityTestCompositeConstraint::create([
'name' => 'entity-level-violation',
]);
$entity
->save();
$errors = $this
->getErrorsForEntity($entity);
$this
->assertEqual($errors[''], 'Entity level validation');
}
}