- Patch #606934 by matt2000, johanneshahn, yched: fixed bug with boolean fields, and added more field type tests.
parent
a5beea1d12
commit
02e70401ec
|
@ -224,19 +224,30 @@ function list_allowed_values_list($string_values, $position_keys = FALSE) {
|
|||
function list_allowed_values_validate($element, &$form_state) {
|
||||
$values = list_allowed_values_list($element['#value'], $element['#list_field_type'] == 'list');
|
||||
$field_type = $element['#list_field_type'];
|
||||
|
||||
// Check that keys are valid for the field type.
|
||||
foreach ($values as $key => $value) {
|
||||
if ($field_type == 'list_number' && !is_numeric($key)) {
|
||||
form_error($element, t('The entered available values are not valid. Each key must be a valid integer or decimal.'));
|
||||
form_error($element, t('Allowed values list: each key must be a valid integer or decimal.'));
|
||||
break;
|
||||
}
|
||||
elseif ($field_type == 'list_text' && strlen($key) > 255) {
|
||||
form_error($element, t('The entered available values are not valid. Each key must be a string less than 255 characters.'));
|
||||
form_error($element, t('Allowed values list: each key must be a string less than 255 characters.'));
|
||||
break;
|
||||
}
|
||||
elseif ($field_type == 'list' && (!preg_match('/^-?\d+$/', $key))) {
|
||||
form_error($element, t('The entered available values are not valid. All specified keys must be integers.'));
|
||||
elseif ($field_type == 'list' && !preg_match('/^-?\d+$/', $key)) {
|
||||
form_error($element, t('Allowed values list: keys must be integers.'));
|
||||
break;
|
||||
}
|
||||
elseif ($field_type == 'list_boolean' && !in_array($key, array('0', '1'))) {
|
||||
form_error($element, t('Allowed values list: keys must be either 0 or 1.'));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Check that boolean fields get two values.
|
||||
if ($field_type == 'list_boolean' && count($values) != 2) {
|
||||
form_error($element, t('Allowed values list: two values are required.'));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -85,5 +85,89 @@ class ListFieldTestCase extends DrupalWebTestCase {
|
|||
$this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE][2]), t('Option 2 exists'));
|
||||
$this->assertTrue(!empty($form['card_1'][FIELD_LANGUAGE_NONE][3]), t('Option 3 exists'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* List module UI tests.
|
||||
*/
|
||||
class ListFieldUITestCase extends DrupalWebTestCase {
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'List field UI tests',
|
||||
'description' => 'Test the List field UI functionality.',
|
||||
'group' => 'Field types',
|
||||
);
|
||||
}
|
||||
|
||||
function setUp() {
|
||||
FieldUITestCase::setUp();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that allowed values are properly validated in the UI.
|
||||
*/
|
||||
function testAllowedValues() {
|
||||
$element_name = "field[settings][allowed_values]";
|
||||
|
||||
//Test 'List' field type.
|
||||
$admin_path = $this->createListFieldAndEdit('list');
|
||||
//Check that non-integer keys are rejected.
|
||||
$edit = array($element_name => "1.1|one\n");
|
||||
$this->drupalPost($admin_path, $edit, t('Save settings'));
|
||||
$this->assertText("keys must be integers", t('Form vaildation failed.'));
|
||||
|
||||
// Test 'List (number)' field type.
|
||||
$admin_path = $this->createListFieldAndEdit('list_number');
|
||||
//Check that non-numeric keys are rejected.
|
||||
$edit = array($element_name => "1|one\nB|two");
|
||||
$this->drupalPost($admin_path, $edit, t('Save settings'));
|
||||
$this->assertText("each key must be a valid integer or decimal", t('Form vaildation failed.'));
|
||||
|
||||
//Test 'List (text)' field type.
|
||||
$admin_path = $this->createListFieldAndEdit('list_text');
|
||||
//Check that over long keys are rejected.
|
||||
$edit = array($element_name => "1|one\n" . $this->randomName(255) . "|two");
|
||||
$this->drupalPost($admin_path, $edit, t('Save settings'));
|
||||
$this->assertText("each key must be a string less than 255 characters", t('Form vaildation failed.'));
|
||||
|
||||
// Test 'List (boolean)' field type.
|
||||
$admin_path = $this->createListFieldAndEdit('list_boolean');
|
||||
// Check that invalid option keys are rejected.
|
||||
$edit = array($element_name => "1|one\n2|two");
|
||||
$this->drupalPost($admin_path, $edit, t('Save settings'));
|
||||
$this->assertText("keys must be either 0 or 1", t('Form vaildation failed.'));
|
||||
|
||||
//Check that missing option causes failure.
|
||||
$edit = array($element_name => "1|one");
|
||||
$this->drupalPost($admin_path, $edit, t('Save settings'));
|
||||
$this->assertText("two values are required", t('Form vaildation failed.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to create list field of a given type and get the edit page.
|
||||
*
|
||||
* @param string $type
|
||||
* 'list', 'list_boolean', 'list_number', or 'list_text'
|
||||
*/
|
||||
private function createListFieldAndEdit($type) {
|
||||
// Create a test field and instance.
|
||||
$field_name = 'test_' . $type;
|
||||
$field = array(
|
||||
'field_name' => $field_name,
|
||||
'type' => $type,
|
||||
);
|
||||
field_create_field($field);
|
||||
$instance = array(
|
||||
'field_name' => $field_name,
|
||||
'object_type' => 'node',
|
||||
'bundle' => $this->type,
|
||||
);
|
||||
field_create_instance($instance);
|
||||
|
||||
$admin_path = 'admin/structure/types/manage/' . $this->hyphen_type . '/fields/' . $field_name;
|
||||
return $admin_path;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue