function AtLeastOneOfConstraintValidatorTest::testValidation
Tests the AllowedValues validation constraint validator.
For testing we define an integer with a set of allowed values.
Attributes
#[DataProvider('dataProvider')]
File
- 
              core/
tests/ Drupal/ KernelTests/ Core/ Validation/ AtLeastOneOfConstraintValidatorTest.php, line 45  
Class
- AtLeastOneOfConstraintValidatorTest
 - Tests AtLeastOneOf validation constraint with both valid and invalid values.
 
Namespace
Drupal\KernelTests\Core\ValidationCode
public function testValidation($type, $value, $at_least_one_of_constraints, $expectedViolations, $extra_constraints = []) : void {
  // Create a definition that specifies some AllowedValues.
  $definition = DataDefinition::create($type);
  if (count($extra_constraints) > 0) {
    foreach ($extra_constraints as $name => $settings) {
      $definition->addConstraint($name, $settings);
    }
  }
  $definition->addConstraint('AtLeastOneOf', [
    'constraints' => $at_least_one_of_constraints,
  ]);
  // Test the validation.
  $typed_data = $this->typedDataManager
    ->create($definition, $value);
  $violations = $typed_data->validate();
  $violationMessages = [];
  foreach ($violations as $violation) {
    $violationMessages[] = (string) $violation->getMessage();
  }
  $this->assertEquals($expectedViolations, $violationMessages, 'Validation passed for correct value.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.