diff --git a/core/modules/simpletest/tests/form.test b/core/modules/simpletest/tests/form.test index 49933c3..754ce72 100644 --- a/core/modules/simpletest/tests/form.test +++ b/core/modules/simpletest/tests/form.test @@ -122,11 +122,11 @@ class FormsTestCase extends DrupalWebTestCase { } /** - * Tests #required checkboxes and radios. + * Tests validation for required checkbox, select, and radio elements. * - * Programmatically submits a test form containing several types of input - * elements. The form is submitted twice, once with values and once without, - * and each is checked for the proper #required error messages. + * 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() */ @@ -134,18 +134,38 @@ class FormsTestCase extends DrupalWebTestCase { $form = $form_state = array(); $form = form_test_validate_required_form($form, $form_state); - // Verify each error message shows when required value not selected. + // 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) { - $this->assertText(t('!name field is required.', array('!name' => $form[$key]['#title']))); + $expected[] = t('!name field is required.', array('!name' => $form[$key]['#title'])); } - // The unhelpful catch-all generic error should not appear. - $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); + // 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("Invalid error message: {$error[0]}"); + } + // Remove the expected message from the list once it is found. + else { + unset($expected[$expected_key]); + } + } - // Verify that no error appears with valid values. + // Fail if any expected messages were not found. + foreach ($expected as $not_found) { + $this->fail("Error message not shown: $not_found"); + } + + // Submit again with required fields set and verify that there are no + // error messages. $edit = array( 'textfield' => $this->randomString(), 'checkboxes[foo]' => TRUE, @@ -153,11 +173,7 @@ class FormsTestCase extends DrupalWebTestCase { 'radios' => 'bar', ); $this->drupalPost('form-test/validate-required', $edit, 'Submit'); - - foreach (array('textfield', 'checkboxes', 'select', 'radios') as $key) { - $this->assertNoText(t('!name field is required.', array('!name' => $form[$key]['#title']))); - } - $this->assertNoText(t('An illegal choice has been detected. Please contact the site administrator.')); + $this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed when all required fields are filled.'); } /**