function FieldValidationTest::testFieldConstraints in Zircon Profile 8.0
Same name and namespace in other branches
- 8 core/modules/field/src/Tests/FieldValidationTest.php \Drupal\field\Tests\FieldValidationTest::testFieldConstraints()
Tests that constraints defined by the field type are validated.
File
- core/
modules/ field/ src/ Tests/ FieldValidationTest.php, line 70 - Contains \Drupal\field\Tests\FieldValidationTest.
Class
- FieldValidationTest
- Tests field validation.
Namespace
Drupal\field\TestsCode
function testFieldConstraints() {
$cardinality = $this->fieldTestData->field_storage
->getCardinality();
$entity = $this->entity;
// The test is only valid if the field cardinality is greater than 2.
$this
->assertTrue($cardinality >= 2);
// Set up values for the field.
$expected_violations = array();
for ($delta = 0; $delta < $cardinality; $delta++) {
// All deltas except '1' have incorrect values.
if ($delta == 1) {
$value = 1;
}
else {
$value = -1;
$expected_violations[$delta . '.value'][] = t('%name does not accept the value -1.', array(
'%name' => $this->fieldTestData->field
->getLabel(),
));
}
$entity->{$this->fieldTestData->field_name}[] = $value;
}
// Validate the field.
$violations = $entity->{$this->fieldTestData->field_name}
->validate();
// Check that the expected constraint violations are reported.
$violations_by_path = array();
foreach ($violations as $violation) {
$violations_by_path[$violation
->getPropertyPath()][] = $violation
->getMessage();
}
$this
->assertEqual($violations_by_path, $expected_violations);
}