diff options
author | webchick <webchick@24967.no-reply.drupal.org> | 2012-03-13 09:41:32 -0700 |
---|---|---|
committer | webchick <webchick@24967.no-reply.drupal.org> | 2012-03-13 09:41:32 -0700 |
commit | 1569c4bde2de6097de2abd6f5f66d62f0cede3c5 (patch) | |
tree | cebbe6b6cd504d1e4116175072684f70e13526e9 /modules/simpletest | |
parent | c29e269956ca92d83c62ccef4fda6c571e802101 (diff) | |
download | brdo-1569c4bde2de6097de2abd6f5f66d62f0cede3c5.tar.gz brdo-1569c4bde2de6097de2abd6f5f66d62f0cede3c5.tar.bz2 |
Issue #811542 by bojanz, sun, mlncn, chx, pillarsdotnet, loganfsmyth, xjm: Fixed Regression: Required radios throw illegal choice error when none selected.
Diffstat (limited to 'modules/simpletest')
-rw-r--r-- | modules/simpletest/tests/form.test | 66 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 53 |
2 files changed, 119 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 13fdca201..ec7df2c33 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -122,6 +122,72 @@ class FormsTestCase extends DrupalWebTestCase { } /** + * Tests validation for required checkbox, select, and radio elements. + * + * Submits a test form containing several types of form elements. The form + * is submitted twice, first without values for required fields and then + * with values. Each submission is checked for relevant error messages. + * + * @see form_test_validate_required_form() + */ + function testRequiredCheckboxesRadio() { + $form = $form_state = array(); + $form = form_test_validate_required_form($form, $form_state); + + // Attempt to submit the form with no required fields set. + $edit = array(); + $this->drupalPost('form-test/validate-required', $edit, 'Submit'); + + // The only error messages that should appear are the relevant 'required' + // messages for each field. + $expected = array(); + foreach (array('textfield', 'checkboxes', 'select', 'radios') as $key) { + $expected[] = t('!name field is required.', array('!name' => $form[$key]['#title'])); + } + + // Check the page for error messages. + $errors = $this->xpath('//div[contains(@class, "error")]//li'); + foreach ($errors as $error) { + $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(format_string("Unexpected error message: @error", array('@error' => $error[0]))); + } + // Remove the expected message from the list once it is found. + else { + unset($expected[$expected_key]); + } + } + + // Fail if any expected messages were not found. + foreach ($expected as $not_found) { + $this->fail(format_string("Found error message: @error", array('@error' => $not_found))); + } + + // Verify that input elements are still empty. + $this->assertFieldByName('textfield', ''); + $this->assertNoFieldChecked('edit-checkboxes-foo'); + $this->assertNoFieldChecked('edit-checkboxes-bar'); + $this->assertOptionSelected('edit-select', ''); + $this->assertNoFieldChecked('edit-radios-foo'); + $this->assertNoFieldChecked('edit-radios-bar'); + $this->assertNoFieldChecked('edit-radios-optional-foo'); + $this->assertNoFieldChecked('edit-radios-optional-bar'); + + // Submit again with required fields set and verify that there are no + // error messages. + $edit = array( + 'textfield' => $this->randomString(), + 'checkboxes[foo]' => TRUE, + 'select' => 'foo', + 'radios' => 'bar', + ); + $this->drupalPost(NULL, $edit, '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.'); + } + + /** * Test default value handling for checkboxes. * * @see _form_test_checkbox() diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 0a748d2df..1c16c39c7 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -23,6 +23,13 @@ function form_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['form-test/validate-required'] = array( + 'title' => 'Form #required validation', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('form_test_validate_required_form'), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); $items['form-test/limit-validation-errors'] = array( 'title' => 'Form validation with some error suppression', 'page callback' => 'drupal_get_form', @@ -332,6 +339,52 @@ function form_test_validate_form_validate(&$form, &$form_state) { } /** + * Form constructor to test the #required property. + */ +function form_test_validate_required_form($form, &$form_state) { + $options = drupal_map_assoc(array('foo', 'bar')); + + $form['textfield'] = array( + '#type' => 'textfield', + '#title' => 'Textfield', + '#required' => TRUE, + ); + $form['checkboxes'] = array( + '#type' => 'checkboxes', + '#title' => 'Checkboxes', + '#options' => $options, + '#required' => TRUE, + ); + $form['select'] = array( + '#type' => 'select', + '#title' => 'Select', + '#options' => $options, + '#required' => TRUE, + ); + $form['radios'] = array( + '#type' => 'radios', + '#title' => 'Radios', + '#options' => $options, + '#required' => TRUE, + ); + $form['radios_optional'] = array( + '#type' => 'radios', + '#title' => 'Radios (optional)', + '#options' => $options, + ); + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Submit'); + return $form; +} + +/** + * Form submission handler for form_test_validate_required_form(). + */ +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) { |