summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDries Buytaert2009-11-24 05:27:31 (GMT)
committer Dries Buytaert2009-11-24 05:27:31 (GMT)
commit02e70401ecb1c4a76d60bf44fc66090e1f0c10d1 (patch)
tree10b6c11f6f4e6decdba2aafdc1a673540c9d7ce3
parenta5beea1d124f57d87d4d8938b2e5ba8ade0755f3 (diff)
- Patch #606934 by matt2000, johanneshahn, yched: fixed bug with boolean fields, and added more field type tests.
-rw-r--r--modules/field/modules/list/list.module19
-rw-r--r--modules/field/modules/list/list.test84
2 files changed, 99 insertions, 4 deletions
diff --git a/modules/field/modules/list/list.module b/modules/field/modules/list/list.module
index b569f8c..64d8441 100644
--- a/modules/field/modules/list/list.module
+++ b/modules/field/modules/list/list.module
@@ -224,20 +224,31 @@ 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('Allowed values list: keys must be integers.'));
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_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.'));
+ }
}
/**
diff --git a/modules/field/modules/list/list.test b/modules/field/modules/list/list.test
index 7f8a1dc..de9c29e 100644
--- a/modules/field/modules/list/list.test
+++ b/modules/field/modules/list/list.test
@@ -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;
+ }
+
}