1.20.x text.test | TextFieldTestCase::testTextFieldValidation() |
Test text field validation.
File
- modules/
field/ modules/ text/ tests/ text.test, line 26 - Tests for text.module.
Class
Code
function testTextFieldValidation() {
// Create a field with settings to validate.
$max_length = 3;
$this->field = array(
'field_name' => backdrop_strtolower($this->randomName()),
'type' => 'text',
'settings' => array(
'max_length' => $max_length,
)
);
field_create_field($this->field);
$this->instance = array(
'field_name' => $this->field['field_name'],
'entity_type' => 'test_entity',
'bundle' => 'test_bundle',
'widget' => array(
'type' => 'text_textfield',
),
'display' => array(
'default' => array(
'type' => 'text_default',
),
),
);
field_create_instance($this->instance);
// Test valid and invalid values with field_attach_validate().
$entity = field_test_create_entity();
$langcode = LANGUAGE_NONE;
for ($i = 0; $i <= $max_length + 2; $i++) {
$entity->{$this->field['field_name']}[$langcode][0]['value'] = str_repeat('x', $i);
try {
field_attach_validate('test_entity', $entity);
$this->assertTrue($i <= $max_length, "Length $i does not cause validation error when max_length is $max_length");
}
catch (FieldValidationException $e) {
$this->assertTrue($i > $max_length, "Length $i causes validation error when max_length is $max_length");
}
}
}