// @see field_test_entity_bundle_field_info_alter()
\Drupal::
state()->
set('field_test_constraint',
$field_name);
// Clear the field definitions cache so the new constraints added by
// field_test_entity_bundle_field_info_alter() are taken into consideration.
\Drupal::
service('entity_field.manager'
)->
clearCachedFieldDefinitions();
// Test the newly added property constraints in the same request as when the
// caches were cleared. This will test the field definitions that are stored
// in the static cache of
// \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions().
$this->
doFieldPropertyConstraintsTests();
// In order to test a real-world scenario where the property constraints are
// only stored in the persistent cache of
// \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions(), we need to
// simulate a new request by removing the 'entity_field.manager' service,
// thus forcing it to be re-initialized without static caches.
\Drupal::
getContainer()->
set('entity_field.manager', NULL
);
// This will test the field definitions that are stored in the persistent
// cache by \Drupal\Core\Entity\EntityFieldManager::getFieldDefinitions().
$this->
doFieldPropertyConstraintsTests();
}