core/includes/form.inc | 6 +++- core/modules/simpletest/tests/form.test | 9 ++++--- core/modules/simpletest/tests/form_test.module | 25 +++++++++++++++++------ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index 9e99426..139a663 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -2258,8 +2258,10 @@ function form_type_radios_value(&$element, $input = FALSE) { if ($input !== FALSE) { // Check if the user submission had no value for this element. if (!isset($input)) { - // Flag as a garbage value so that form builder will not automatically - // replace the NULL value with an empty string. + // Flag as a garbage value. If the value is set to NULL and + // #has_garbage_value is not set then the form builder will automatically + // attempt to use default value or '' (an empty string). An empty string + // will fail validation because it is not in the allowed values list. $element['#has_garbage_value'] = TRUE; // There was a user submission so validation is a must. This way, the // required checker will produce an error as necessary. diff --git a/core/modules/simpletest/tests/form.test b/core/modules/simpletest/tests/form.test index c79a469..57b2616 100644 --- a/core/modules/simpletest/tests/form.test +++ b/core/modules/simpletest/tests/form.test @@ -136,7 +136,7 @@ class FormsTestCase extends DrupalWebTestCase { // Attempt to submit the form with no required fields set. $edit = array(); - $this->drupalPost('form-test/validate-required', $edit, 'Submit'); + $this->drupalPost('form-test/validate-required', $edit, t('Submit')); // The only error messages that should appear are the relevant 'required' // messages for each field. @@ -151,7 +151,7 @@ class FormsTestCase extends DrupalWebTestCase { $expected_key = array_search($error[0], $expected); // If the error message is not one of the expected messages, fail. if ($expected_key === FALSE) { - $this->fail("Invalid error message: {$error[0]}"); + $this->fail(format_string("Invalid error message: @error", array('@error' => $error[0]))); } // Remove the expected message from the list once it is found. else { @@ -161,7 +161,7 @@ class FormsTestCase extends DrupalWebTestCase { // Fail if any expected messages were not found. foreach ($expected as $not_found) { - $this->fail("Error message not shown: $not_found"); + $this->fail(format_string("Error message not shown: @error", array('@error' => $not_found))); } // Submit again with required fields set and verify that there are no @@ -172,8 +172,9 @@ class FormsTestCase extends DrupalWebTestCase { 'select' => 'foo', 'radios' => 'bar', ); - $this->drupalPost('form-test/validate-required', $edit, 'Submit'); + $this->drupalPost('form-test/validate-required', $edit, t('Submit')); $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed when all required fields are filled.'); + $this->assertRaw("The form_test_validate_required_form form was submitted successfully.", 'Validation form submitted successfully.'); } /** diff --git a/core/modules/simpletest/tests/form_test.module b/core/modules/simpletest/tests/form_test.module index 2173bcb..01db063 100644 --- a/core/modules/simpletest/tests/form_test.module +++ b/core/modules/simpletest/tests/form_test.module @@ -27,7 +27,8 @@ function form_test_menu() { 'title' => 'Form #required validation', 'page callback' => 'drupal_get_form', 'page arguments' => array('form_test_validate_required_form'), - 'access callback' => TRUE, + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, ); $items['form-test/limit-validation-errors'] = array( 'title' => 'Form validation with some error suppression', @@ -357,38 +358,48 @@ function form_test_validate_form_validate(&$form, &$form_state) { function form_test_validate_required_form($form, &$form_state) { $form['textfield'] = array( '#type' => 'textfield', - '#title' => 'Textfield', + '#title' => t('Textfield'), '#required' => TRUE, ); $form['checkboxes'] = array( '#type' => 'checkboxes', - '#title' => 'Checkboxes', + '#title' => t('Checkboxes'), '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, ); $form['select'] = array( '#type' => 'select', - '#title' => 'Select', + '#title' => t('Select'), '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, ); $form['radios'] = array( '#type' => 'radios', - '#title' => 'Radios', + '#title' => t('Radios'), '#options' => drupal_map_assoc(array('foo', 'bar')), '#required' => TRUE, ); $form['radios_optional'] = array( '#type' => 'radios', - '#title' => 'Radios (optional)', + '#title' => t('Radios (optional)'), '#options' => drupal_map_assoc(array('foo', 'bar')), ); $form['actions'] = array('#type' => 'actions'); - $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Submit')); return $form; } /** + * Form submit handler to test for proper submission. + * + * This message is checked for in the tests to confirm that this for + * successfully submitted without any errors. + */ +function form_test_validate_required_form_submit($form, &$form_state) { + drupal_set_message("The form_test_validate_required_form form was submitted successfully."); +} + +/** * Builds a simple form with a button triggering partial validation. */ function form_test_limit_validation_errors_form($form, &$form_state) {