3 namespace Drupal\config_test;
5 use Symfony\Component\Validator\Context\ExecutionContextInterface;
8 * Provides a collection of validation callbacks for testing purposes.
10 class ConfigValidation {
15 * @param string $string
16 * The string to validate.
17 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
18 * The validation execution context.
20 public static function validateLlama($string, ExecutionContextInterface $context) {
21 if (!in_array($string, ['llama', 'alpaca', 'guanaco', 'vicuña'], TRUE)) {
22 $context->addViolation('no valid llama');
29 * @param string $string
30 * The string to validate.
31 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
32 * The validation execution context.
34 public static function validateCats($string, ExecutionContextInterface $context) {
35 if (!in_array($string, ['kitten', 'cats', 'nyans'])) {
36 $context->addViolation('no valid cat');
44 * The integer to validate.
45 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
46 * The validation execution context.
48 public static function validateCatCount($count, ExecutionContextInterface $context) {
50 $context->addViolation('no enough cats');
57 * @param string $string
58 * The string to validate.
59 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
60 * The validation execution context.
62 public static function validateGiraffes($string, ExecutionContextInterface $context) {
63 if (strpos($string, 'hum') !== 0) {
64 $context->addViolation('Giraffes just hum');
69 * Validates a mapping.
71 * @param array $mapping
72 * The data to validate.
73 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
74 * The validation execution context.
76 public static function validateMapping($mapping, ExecutionContextInterface $context) {
77 // Ensure we are validating the entire mapping by diffing against all the
79 $mapping_schema = \Drupal::service('config.typed')->get('config_test.validation')->getValue();
80 if ($diff = array_diff_key($mapping, $mapping_schema)) {
81 $context->addViolation('Unexpected keys: ' . implode(', ', array_keys($diff)));
86 * Validates a sequence.
88 * @param array $sequence
89 * The data to validate.
90 * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
91 * The validation execution context.
93 public static function validateSequence($sequence, ExecutionContextInterface $context) {
94 if (isset($sequence['invalid-key'])) {
95 $context->addViolation('Invalid giraffe key.');