$entity_type = 'entity_test_constraint_violation';
$entity =
$this->container->
get('entity_type.manager'
) ->
getStorage($entity_type) ->
create(['id' => 1, 'revision_id' => 1
]);
$display = \Drupal::
service('entity_display.repository'
) ->
getFormDisplay($entity_type,
$entity_type);
$form =
[];
$form_state =
new FormState();
$display->
buildForm($entity,
$form,
$form_state);
// Pretend the form has been built.
$form_state->
setFormObject(\Drupal::
entityTypeManager()->
getFormObject($entity_type, 'default'
));
\Drupal::
formBuilder()->
prepareForm('field_test_entity_form',
$form,
$form_state);
\Drupal::
formBuilder()->
processForm('field_test_entity_form',
$form,
$form_state);
// Validate the field constraint.
$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->
assertEquals('Widget constraint has failed.',
$errors['name'
], 'Constraint violation at the field items list level is generated correctly'
);
$this->
assertEquals('Widget constraint has failed.',
$errors['test_field'
], 'Constraint violation at the field items list level is generated correctly for an advanced widget'
);
}