'Form element validation', 'description' => 'Tests various form element validation mechanisms.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Check several empty values for required forms elements. * * Carriage returns, tabs, spaces, and unchecked checkbox elements are not * valid content for a required field. * * If the form field is found in form_get_errors() then the test pass. */ function testRequiredFields() { // Originates from http://drupal.org/node/117748 // Sets of empty strings and arrays. $empty_strings = array('""' => "", '"\n"' => "\n", '" "' => " ", '"\t"' => "\t", '" \n\t "' => " \n\t ", '"\n\n\n\n\n"' => "\n\n\n\n\n"); $empty_arrays = array('array()' => array()); $empty_checkbox = array(NULL); $elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield'); $elements['textfield']['empty_values'] = $empty_strings; $elements['password']['element'] = array('#title' => $this->randomName(), '#type' => 'password'); $elements['password']['empty_values'] = $empty_strings; $elements['password_confirm']['element'] = array('#title' => $this->randomName(), '#type' => 'password_confirm'); $elements['password_confirm']['empty_values'] = $empty_strings; $elements['textarea']['element'] = array('#title' => $this->randomName(), '#type' => 'textarea'); $elements['textarea']['empty_values'] = $empty_strings; $elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#options' => array($this->randomName(), $this->randomName(), $this->randomName())); $elements['radios']['empty_values'] = $empty_arrays; $elements['checkbox']['element'] = array('#title' => $this->randomName(), '#type' => 'checkbox', '#required' => TRUE, '#title' => $this->randomName()); $elements['checkbox']['empty_values'] = $empty_checkbox; $elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#options' => array($this->randomName(), $this->randomName(), $this->randomName())); $elements['checkboxes']['empty_values'] = $empty_arrays; $elements['select']['element'] = array('#title' => $this->randomName(), '#type' => 'select', '#options' => array($this->randomName(), $this->randomName(), $this->randomName())); $elements['select']['empty_values'] = $empty_strings; $elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file'); $elements['file']['empty_values'] = $empty_strings; // Regular expression to find the expected marker on required elements. $required_marker_preg = '@\*@'; // Go through all the elements and all the empty values for them. foreach ($elements as $type => $data) { foreach ($data['empty_values'] as $key => $empty) { foreach (array(TRUE, FALSE) as $required) { $form_id = $this->randomName(); $form = array(); $form_state = form_state_defaults(); form_clear_error(); $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); $element = $data['element']['#title']; $form[$element] = $data['element']; $form[$element]['#required'] = $required; $form_state['values'][$element] = $empty; $form_state['input'] = $form_state['values']; $form_state['input']['form_id'] = $form_id; $form_state['method'] = 'post'; drupal_prepare_form($form_id, $form, $form_state); drupal_process_form($form_id, $form, $form_state); $errors = form_get_errors(); // Form elements of type 'radios' throw all sorts of PHP notices // when you try to render them like this, so we ignore those for // testing the required marker. // @todo Fix this work-around (http://drupal.org/node/588438). $form_output = ($type == 'radios') ? '' : drupal_render($form); if ($required) { // Make sure we have a form error for this element. $this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'"); if (!empty($form_output)) { // Make sure the form element is marked as required. $this->assertTrue(preg_match($required_marker_preg, $form_output), "Required '$type' field is marked as required"); } } else { if (!empty($form_output)) { // Make sure the form element is *not* marked as required. $this->assertFalse(preg_match($required_marker_preg, $form_output), "Optional '$type' field is not marked as required"); } if ($type == 'select') { // Select elements are going to have validation errors with empty // input, since those are illegal choices. Just make sure the // error is not "field is required". $this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element"); } else { // Make sure there is *no* form error for this element. $this->assertTrue(empty($errors[$element]), "Optional '$type' field '$element' has no errors with empty input"); } } } } } // Clear the expected form error messages so they don't appear as exceptions. drupal_get_messages(); } /** * Test default value handling for checkboxes. * * @see _form_test_checkbox() */ function testCheckboxProcessing() { // First, try to submit without the required checkbox. $edit = array(); $this->drupalPost('form-test/checkbox', $edit, t('Submit')); $this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), t('A required checkbox is actually mandatory')); // Now try to submit the form correctly. $values = drupal_json_decode($this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit'))); $expected_values = array( 'disabled_checkbox_on' => 'disabled_checkbox_on', 'disabled_checkbox_off' => '', 'checkbox_on' => 'checkbox_on', 'checkbox_off' => '', 'zero_checkbox_on' => '0', 'zero_checkbox_off' => '', ); foreach ($expected_values as $widget => $expected_value) { $this->assertEqual($values[$widget], $expected_value, t('Checkbox %widget returns expected value (expected: %expected, got: %value)', array( '%widget' => var_export($widget, TRUE), '%expected' => var_export($expected_value, TRUE), '%value' => var_export($values[$widget], TRUE), ))); } } /** * Test handling of disabled elements. * * @see _form_test_disabled_elements() */ function testDisabledElements() { // Get the raw form in its original state. $form_state = array(); $form = _form_test_disabled_elements(array(), $form_state); // Build a submission that tries to hijack the form by submitting input for // elements that are disabled. $edit = array(); foreach (element_children($form) as $key) { if (isset($form[$key]['#test_hijack_value'])) { if (is_array($form[$key]['#test_hijack_value'])) { foreach ($form[$key]['#test_hijack_value'] as $subkey => $value) { $edit[$key . '[' . $subkey . ']'] = $value; } } else { $edit[$key] = $form[$key]['#test_hijack_value']; } } } // Submit the form with no input, as the browser does for disabled elements, // and fetch the $form_state['values'] that is passed to the submit handler. $this->drupalPost('form-test/disabled-elements', array(), t('Submit')); $returned_values['normal'] = drupal_json_decode($this->content); // Do the same with input, as could happen if JavaScript un-disables an // element. drupalPost() emulates a browser by not submitting input for // disabled elements, so we need to un-disable those elements first. $this->drupalGet('form-test/disabled-elements'); foreach ($this->xpath('//*[@disabled]') as $element) { unset($element['disabled']); } $this->drupalPost(NULL, $edit, t('Submit')); $returned_values['hijacked'] = drupal_json_decode($this->content); // Ensure that the returned values match the form's default values in both // cases. foreach ($returned_values as $type => $values) { foreach (element_children($form) as $key) { if (isset($form[$key]['#default_value'])) { $expected_value = $form[$key]['#default_value']; if ($key == 'checkboxes_multiple') { // Checkboxes values are not filtered out. $values[$key] = array_filter($values[$key]); } $this->assertIdentical($expected_value, $values[$key], t('Default value for %type: expected %expected, returned %returned.', array('%type' => $key, '%expected' => var_export($expected_value, TRUE), '%returned' => var_export($values[$key], TRUE)))); } } } } } /** * Test form validation handlers. */ class FormValidationTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form validation handlers', 'description' => 'Tests form processing and alteration via form validation handlers.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Tests form alterations by #element_validate, #validate, and form_set_value(). */ function testValidate() { $this->drupalGet('form-test/validate'); // Verify that #element_validate handlers can alter the form and submitted // form values. $edit = array( 'name' => 'element_validate', ); $this->drupalPost(NULL, $edit, 'Save'); $this->assertFieldByName('name', '#value changed by #element_validate', t('Form element #value was altered.')); $this->assertText('Name value: value changed by form_set_value() in #element_validate', t('Form element value in $form_state was altered.')); // Verify that #validate handlers can alter the form and submitted // form values. $edit = array( 'name' => 'validate', ); $this->drupalPost(NULL, $edit, 'Save'); $this->assertFieldByName('name', '#value changed by #validate', t('Form element #value was altered.')); $this->assertText('Name value: value changed by form_set_value() in #validate', t('Form element value in $form_state was altered.')); // Verify that #element_validate handlers can make form elements // inaccessible, but values persist. $edit = array( 'name' => 'element_validate_access', ); $this->drupalPost(NULL, $edit, 'Save'); $this->assertNoFieldByName('name', t('Form element was hidden.')); $this->assertText('Name value: element_validate_access', t('Value for inaccessible form element exists.')); // Verify that value for inaccessible form element persists. $this->drupalPost(NULL, array(), 'Save'); $this->assertNoFieldByName('name', t('Form element was hidden.')); $this->assertText('Name value: element_validate_access', t('Value for inaccessible form element exists.')); } /** * Tests partial form validation through #limit_validation_errors. */ function testValidateLimitErrors() { $edit = array('test' => 'invalid'); $path = 'form-test/limit-validation-errors'; // Submit the form by pressing the button with #limit_validation_errors and // ensure that the title field is not validated, but the #element_validate // handler for the 'test' field is triggered. $this->drupalPost($path, $edit, t('Partial validate')); $this->assertNoText(t('!name field is required.', array('!name' => 'Title'))); $this->assertText('Test element is invalid'); // Now test full form validation and ensure that the #element_validate // handler is still triggered. $this->drupalPost($path, $edit, t('Full validate')); $this->assertText(t('!name field is required.', array('!name' => 'Title'))); $this->assertText('Test element is invalid'); } } /** * Test form element labels, required markers and associated output. */ class FormsElementsLabelsTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form element and label output test', 'description' => 'Test form element labels, required markers and associated output.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Test form elements, labels, title attibutes and required marks output * correctly and have the correct label option class if needed. */ function testFormLabels() { $this->drupalGet('form_test/form-labels'); // Check that the checkbox/radio processing is not interfering with // basic placement. $elements = $this->xpath('//input[@id="edit-form-checkboxes-test-third-checkbox"]/following-sibling::label[@for="edit-form-checkboxes-test-third-checkbox" and @class="option"]'); $this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for regular checkboxes.")); $elements = $this->xpath('//input[@id="edit-form-radios-test-second-radio"]/following-sibling::label[@for="edit-form-radios-test-second-radio" and @class="option"]'); $this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for regular radios.")); // Exercise various defaults for checkboxes and modifications to ensure // appropriate override and correct behaviour. $elements = $this->xpath('//input[@id="edit-form-checkbox-test"]/following-sibling::label[@for="edit-form-checkbox-test" and @class="option"]'); $this->assertTrue(isset($elements[0]), t("Label follows field and label option class correct for a checkbox by default.")); // Exercise various defaults for textboxes and modifications to ensure // appropriate override and correct behaviour. $elements = $this->xpath('//label[@for="edit-form-textfield-test-title-and-required"]/child::span[@class="form-required"]/parent::*/following-sibling::input[@id="edit-form-textfield-test-title-and-required"]'); $this->assertTrue(isset($elements[0]), t("Label preceeds textfield, with required marker inside label.")); $elements = $this->xpath('//input[@id="edit-form-textfield-test-no-title-required"]/preceding-sibling::label[@for="edit-form-textfield-test-no-title-required"]/span[@class="form-required"]'); $this->assertTrue(isset($elements[0]), t("Label tag with required marker preceeds required textfield with no title.")); $elements = $this->xpath('//input[@id="edit-form-textfield-test-title"]/preceding-sibling::span[@class="form-required"]'); $this->assertFalse(isset($elements[0]), t("No required marker on non-required field.")); $elements = $this->xpath('//input[@id="edit-form-textfield-test-title-after"]/following-sibling::label[@for="edit-form-textfield-test-title-after" and @class="option"]'); $this->assertTrue(isset($elements[0]), t("Label after field and label option class correct for text field.")); $elements = $this->xpath('//input[@id="edit-form-textfield-test-title-invisible"]/following-sibling::label[@for="edit-form-textfield-test-title-invisible" and @class="element-invisible"]'); $this->assertTrue(isset($elements[0]), t("Label after field and label class is element-invisible.")); $elements = $this->xpath('//label[@for="edit-form-textfield-test-title-no-show"]'); $this->assertFalse(isset($elements[0]), t("No label tag when title set not to display.")); } } /** * Test the tableselect form element for expected behavior. */ class FormsElementsTableSelectFunctionalTest extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Tableselect form element type test', 'description' => 'Test the tableselect element for expected behavior', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Test the display of checkboxes when #multiple is TRUE. */ function testMultipleTrue() { $this->drupalGet('form_test/tableselect/multiple-true'); $this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.')); // Test for the presence of the Select all rows tableheader. $this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Presence of the "Select all" checkbox.')); $rows = array('row1', 'row2', 'row3'); foreach ($rows as $row) { $this->assertFieldByXPath('//input[@type="checkbox"]', $row, t('Checkbox for value @row.', array('@row' => $row))); } } /** * Test the display of radios when #multiple is FALSE. */ function testMultipleFalse() { $this->drupalGet('form_test/tableselect/multiple-false'); $this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.')); // Test for the absence of the Select all rows tableheader. $this->assertNoFieldByXPath('//th[@class="select-all"]', '', t('Absence of the "Select all" checkbox.')); $rows = array('row1', 'row2', 'row3'); foreach ($rows as $row) { $this->assertFieldByXPath('//input[@type="radio"]', $row, t('Radio button for value @row.', array('@row' => $row))); } } /** * Test the display of the #empty text when #options is an empty array. */ function testEmptyText() { $this->drupalGet('form_test/tableselect/empty-text'); $this->assertText(t('Empty text.'), t('Empty text should be displayed.')); } /** * Test the submission of single and multiple values when #multiple is TRUE. */ function testMultipleTrueSubmit() { // Test a submission with one checkbox checked. $edit = array(); $edit['tableselect[row1]'] = TRUE; $this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit'); $this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1')); $this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.')); $this->assertText(t('Submitted: row3 = 0'), t('Unchecked checkbox row3.')); // Test a submission with multiple checkboxes checked. $edit['tableselect[row1]'] = TRUE; $edit['tableselect[row3]'] = TRUE; $this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit'); $this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1.')); $this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.')); $this->assertText(t('Submitted: row3 = row3'), t('Checked checkbox row3.')); } /** * Test submission of values when #multiple is FALSE. */ function testMultipleFalseSubmit() { $edit['tableselect'] = 'row1'; $this->drupalPost('form_test/tableselect/multiple-false', $edit, 'Submit'); $this->assertText(t('Submitted: row1'), t('Selected radio button')); } /** * Test the #js_select property. */ function testAdvancedSelect() { // When #multiple = TRUE a Select all checkbox should be displayed by default. $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default'); $this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Display a "Select all" checkbox by default when #multiple is TRUE.')); // When #js_select is set to FALSE, a "Select all" checkbox should not be displayed. $this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select'); $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #js_select is FALSE.')); // A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select. $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default'); $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE.')); $this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select'); $this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.')); } /** * Test the whether the option checker gives an error on invalid tableselect values for checkboxes. */ function testMultipleTrueOptionchecker() { list($header, $options) = _form_test_tableselect_get_data(); $form['tableselect'] = array( '#type' => 'tableselect', '#header' => $header, '#options' => $options, ); // Test with a valid value. list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1')); $this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for checkboxes.')); // Test with an invalid value. list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value')); $this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for checkboxes.')); } /** * Test the whether the option checker gives an error on invalid tableselect values for radios. */ function testMultipleFalseOptionchecker() { list($header, $options) = _form_test_tableselect_get_data(); $form['tableselect'] = array( '#type' => 'tableselect', '#header' => $header, '#options' => $options, '#multiple' => FALSE, ); // Test with a valid value. list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1')); $this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for radio buttons.')); // Test with an invalid value. list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value')); $this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for radio buttons.')); } /** * Helper function for the option check test to submit a form while collecting errors. * * @param $form_element * A form element to test. * @param $edit * An array containing post data. * * @return * An array containing the processed form, the form_state and any errors. */ private function formSubmitHelper($form, $edit) { $form_id = $this->randomName(); $form_state = form_state_defaults(); $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); $form_state['input'] = $edit; $form_state['input']['form_id'] = $form_id; drupal_prepare_form($form_id, $form, $form_state); drupal_process_form($form_id, $form, $form_state); $errors = form_get_errors(); // Clear errors and messages. drupal_get_messages(); form_clear_error(); // Return the processed form together with form_state and errors // to allow the caller lowlevel access to the form. return array($form, $form_state, $errors); } } /** * Test the form storage on a multistep form. * * The tested form puts data into the storage during the initial form * construction. These tests verify that there are no duplicate form * constructions, with and without manual form caching activiated. Furthermore * when a validation error occurs, it makes sure that changed form element * values aren't lost due to a wrong form rebuild. */ class FormsFormStorageTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Multistep form using form storage', 'description' => 'Tests a multistep form using form storage and makes sure validation and caching works right.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); $this->web_user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($this->web_user); } /** * Tests using the form in a usual way. */ function testForm() { $this->drupalGet('form_test/form-storage'); $this->assertText('Form constructions: 1'); $edit = array('title' => 'new', 'value' => 'value_is_set'); // Reload the form, but don't rebuild. $this->drupalPost(NULL, $edit, 'Reload'); $this->assertText('Form constructions: 2'); // Now use form rebuilding triggered by a submit button. $this->drupalPost(NULL, $edit, 'Continue submit'); $this->assertText('Form constructions: 3'); $this->assertText('Form constructions: 4'); // Reset the form to the values of the storage, using a form rebuild // triggered by button of type button. $this->drupalPost(NULL, array('title' => 'changed'), 'Reset'); $this->assertFieldByName('title', 'new', 'Values have been resetted.'); // After rebuilding, the form has been cached. $this->assertText('Form constructions: 5'); $this->drupalPost(NULL, $edit, 'Save'); $this->assertText('Form constructions: 5'); $this->assertText('Title: new', t('The form storage has stored the values.')); } /** * Tests using the form with an activated $form_state['cache'] property. */ function testFormCached() { $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1))); $this->assertText('Form constructions: 1'); $edit = array('title' => 'new', 'value' => 'value_is_set'); // Reload the form, but don't rebuild. $this->drupalPost(NULL, $edit, 'Reload'); $this->assertNoText('Form constructions'); // Now use form rebuilding triggered by a submit button. $this->drupalPost(NULL, $edit, 'Continue submit'); $this->assertText('Form constructions: 2'); // Reset the form to the values of the storage, using a form rebuild // triggered by button of type button. $this->drupalPost(NULL, array('title' => 'changed'), 'Reset'); $this->assertFieldByName('title', 'new', 'Values have been resetted.'); $this->assertText('Form constructions: 3'); $this->drupalPost(NULL, $edit, 'Save'); $this->assertText('Form constructions: 3'); $this->assertText('Title: new', t('The form storage has stored the values.')); } /** * Tests validation when form storage is used. */ function testValidation() { $this->drupalPost('form_test/form-storage', array('title' => '', 'value' => 'value_is_set'), 'Continue submit'); $this->assertPattern('/value_is_set/', t('The input values have been kept.')); } /** * Tests updating cached form storage during form validation. * * If form caching is enabled and a form stores data in the form storage, then * the form storage also has to be updated in case of a validation error in * the form. This test re-uses the existing form for multi-step tests, but * triggers a special #element_validate handler to update the form storage * during form validation, while another, required element in the form * triggers a form validation error. */ function testCachedFormStorageValidation() { // Request the form with 'cache' query parameter to enable form caching. $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1))); // Skip step 1 of the multi-step form, since the first step copies over // 'title' into form storage, but we want to verify that changes in the form // storage are updated in the cache during form validation. $edit = array('title' => 'foo'); $this->drupalPost(NULL, $edit, 'Continue submit'); // In step 2, trigger a validation error for the required 'title' field, and // post the special 'change_title' value for the 'value' field, which // conditionally invokes the #element_validate handler to update the form // storage. $edit = array('title' => '', 'value' => 'change_title'); $this->drupalPost(NULL, $edit, 'Save'); // At this point, the form storage should contain updated values, but we do // not see them, because the form has not been rebuilt yet due to the // validation error. Post again and verify that the rebuilt form contains // the values of the updated form storage. $this->drupalPost(NULL, array('title' => 'foo', 'value' => 'bar'), 'Save'); $this->assertText("The thing has been changed.", 'The altered form storage value was updated in cache and taken over.'); } /** * Tests a form using form state without using 'storage' to pass data from the * constructor to a submit handler. The data has to persist even when caching * gets activated, what may happen when a modules alter the form and adds * #ajax properties. */ function testFormStatePersist() { // Test the form one time with caching activated and one time without. $run_options = array( array(), array('query' => array('cache' => 1)), ); foreach($run_options as $options) { $this->drupalPost('form-test/state-persist', array(), t('Submit'), $options); // The submit handler outputs the value in $form_state, assert it's there. $this->assertText('State persisted.'); // Test it again, but first trigger a validation error, then test. $this->drupalPost('form-test/state-persist', array('title' => ''), t('Submit'), $options); $this->assertText(t('!name field is required.', array('!name' => 'title'))); // Submit the form again triggering no validation error. $this->drupalPost(NULL, array('title' => 'foo'), t('Submit'), $options); $this->assertText('State persisted.'); // Now post to the rebuilt form and verify it's still there afterwards. $this->drupalPost(NULL, array('title' => 'bar'), t('Submit'), $options); $this->assertText('State persisted.'); } } } /** * Test wrapper form callbacks. */ class FormsFormWrapperTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form wrapper callback', 'description' => 'Tests form wrapper callbacks to pass a prebuilt form to form builder functions.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Tests using the form in a usual way. */ function testWrapperCallback() { $this->drupalGet('form_test/wrapper-callback'); $this->assertText('Form wrapper callback element output.', t('The form contains form wrapper elements.')); $this->assertText('Form builder element output.', t('The form contains form builder elements.')); } } /** * Test $form_state clearance. */ class FormStateValuesCleanTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form state values clearance', 'description' => 'Test proper removal of submitted form values using form_state_values_clean().', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Tests form_state_values_clean(). */ function testFormStateValuesClean() { $values = drupal_json_decode($this->drupalPost('form_test/form-state-values-clean', array(), t('Submit'))); // Setup the expected result. $result = array( 'beer' => 1000, 'baz' => array('beer' => 2000), ); // Verify that all internal Form API elements were removed. $this->assertFalse(isset($values['form_id']), t('%element was removed.', array('%element' => 'form_id'))); $this->assertFalse(isset($values['form_token']), t('%element was removed.', array('%element' => 'form_token'))); $this->assertFalse(isset($values['form_build_id']), t('%element was removed.', array('%element' => 'form_build_id'))); $this->assertFalse(isset($values['op']), t('%element was removed.', array('%element' => 'op'))); // Verify that all buttons were removed. $this->assertFalse(isset($values['foo']), t('%element was removed.', array('%element' => 'foo'))); $this->assertFalse(isset($values['bar']), t('%element was removed.', array('%element' => 'bar'))); $this->assertFalse(isset($values['baz']['foo']), t('%element was removed.', array('%element' => 'foo'))); $this->assertFalse(isset($values['baz']['baz']), t('%element was removed.', array('%element' => 'baz'))); // Verify that nested form value still exists. $this->assertTrue(isset($values['baz']['beer']), t('Nested form value still exists.')); // Verify that actual form values equal resulting form values. $this->assertEqual($values, $result, t('Expected form values equal actual form values.')); } } /** * Tests form rebuilding. * * @todo Add tests for other aspects of form rebuilding. */ class FormsRebuildTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form rebuilding', 'description' => 'Tests functionality of drupal_rebuild_form().', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); $this->web_user = $this->drupalCreateUser(array('access content')); $this->drupalLogin($this->web_user); } /** * Tests preservation of values. */ function testRebuildPreservesValues() { $edit = array( 'checkbox_1_default_off' => TRUE, 'checkbox_1_default_on' => FALSE, 'text_1' => 'foo', ); $this->drupalPost('form-test/form-rebuild-preserve-values', $edit, 'Add more'); // Verify that initial elements retained their submitted values. $this->assertFieldChecked('edit-checkbox-1-default-off', t('A submitted checked checkbox retained its checked state during a rebuild.')); $this->assertNoFieldChecked('edit-checkbox-1-default-on', t('A submitted unchecked checkbox retained its unchecked state during a rebuild.')); $this->assertFieldById('edit-text-1', 'foo', t('A textfield retained its submitted value during a rebuild.')); // Verify that newly added elements were initialized with their default values. $this->assertFieldChecked('edit-checkbox-2-default-on', t('A newly added checkbox was initialized with a default checked state.')); $this->assertNoFieldChecked('edit-checkbox-2-default-off', t('A newly added checkbox was initialized with a default unchecked state.')); $this->assertFieldById('edit-text-2', 'DEFAULT 2', t('A newly added textfield was initialized with its default value.')); } /** * Tests that a form's action is retained after an AJAX submission. * * The 'action' attribute of a form should not change after an AJAX submission * followed by a non-AJAX submission, which triggers a validation error. */ function testPreserveFormActionAfterAJAX() { // Create a multi-valued field for 'page' nodes to use for AJAX testing. $field_name = 'field_ajax_test'; $field = array( 'field_name' => $field_name, 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED, ); field_create_field($field); $instance = array( 'field_name' => $field_name, 'entity_type' => 'node', 'bundle' => 'page', ); field_create_instance($instance); // Log in a user who can create 'page' nodes. $this->web_user = $this->drupalCreateUser(array('create page content')); $this->drupalLogin($this->web_user); // Get the form for adding a 'page' node. Submit an "add another item" AJAX // submission and verify it worked by ensuring the updated page has two text // field items in the field for which we just added an item. $this->drupalGet('node/add/page'); preg_match('/jQuery\.extend\(Drupal\.settings, (.*?)\);/', $this->content, $matches); $settings = drupal_json_decode($matches[1]); $button = $this->xpath('//input[@name="field_ajax_test_add_more"]'); $button_id = (string) $button[0]['id']; $this->drupalPostAJAX(NULL, array(), array('field_ajax_test_add_more' => t('Add another item')), 'system/ajax', array(), array(), 'page-node-form', $settings['ajax'][$button_id]); $this->assert(count($this->xpath('//div[contains(@class, "field-name-field-ajax-test")]//input[@type="text"]')) == 2, t('AJAX submission succeeded.')); // Submit the form with the non-AJAX "Save" button, leaving the title field // blank to trigger a validation error, and ensure that a validation error // occurred, because this test is for testing what happens when a form is // re-rendered without being re-built, which is what happens when there's // a validation error. $this->drupalPost(NULL, array(), t('Save')); $this->assertText('Title field is required.', t('Non-AJAX submission correctly triggered a validation error.')); // Ensure that the form's action is correct. $forms = $this->xpath('//form[contains(@class, "node-page-form")]'); $this->assert(count($forms) == 1 && $forms[0]['action'] == url('node/add/page'), t('Re-rendered form contains the correct action value.')); } } /** * Test the programmatic form submission behavior. */ class FormsProgrammaticTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Programmatic form submissions', 'description' => 'Test the programmatic form submission behavior.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Test the programmatic form submission workflow. */ function testSubmissionWorkflow() { // Backup the current batch status and reset it to avoid conflicts while // processing the dummy form submit handler. $current_batch = $batch =& batch_get(); $batch = array(); $this->submitForm(); $this->submitForm('test 1'); $this->submitForm(); $this->submitForm('test 2'); // Restore the current batch status. $batch = $current_batch; } /** * Helper function used to programmatically submit the form defined in * form_test.module with the given value. * * @param string $value * The field value to be submitted. */ private function submitForm($value = NULL) { // Programmatically submit the given value. $form_state = array('values' => array('submitted_field' => $value)); drupal_form_submit('form_test_programmatic_form', $form_state); $errors = form_get_errors(); $valid_form = empty($errors); $valid_input = !empty($value); // If no value was passed the form should return an error and viceversa. $args = array('%value' => $value, '%errors' => $valid_form ? '' : implode(' ', $errors)); $this->assertTrue($valid_input == $valid_form, t('Input value: %value
Validation handler errors: %errors', $args)); // We check submitted values only if we have a valid input. if ($valid_input) { // By fetching the value from $form_state['storage'] we ensure that the // submission handler was properly executed. $submitted_value = $form_state['storage']['programmatic_form_submit']; $this->assertTrue($submitted_value == $value, t('Submission handler correctly executed: %submitted_value', array('%submitted_value' => $submitted_value))); } } } /** * Test that FAPI correctly determines $form_state['clicked_button']. */ class FormsClickedButtonTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Form clicked button determination', 'description' => 'Test the determination of $form_state[\'clicked_button\'].', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); } /** * Test the determination of $form_state['clicked_button'] when no button * information is included in the POST data, as is sometimes the case when * the ENTER key is pressed in a textfield in Internet Explorer. */ function testNoButtonInfoInPost() { $path = 'form-test/clicked-button'; $edit = array(); $form_id = 'form-test-clicked-button'; // Ensure submitting a form with no buttons results in no // $form_state['clicked_button'] and the form submit handler not running. drupal_static_reset('drupal_html_id'); $this->drupalPost($path, $edit, NULL, array(), array(), $form_id); $this->assertText('There is no clicked button.', t('$form_state[\'clicked_button\'] set to NULL.')); $this->assertNoText('Submit handler for form_test_clicked_button executed.', t('Form submit handler did not execute.')); // Ensure submitting a form with one or more submit buttons results in // $form_state['clicked_button'] being set to the first one the user has // access to. An argument with 'r' in it indicates a restricted // (#access=FALSE) button. drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/s', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button1.', t('$form_state[\'clicked_button\'] set to only button.')); $this->assertText('Submit handler for form_test_clicked_button executed.', t('Form submit handler executed.')); drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/s/s', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button1.', t('$form_state[\'clicked_button\'] set to first button.')); $this->assertText('Submit handler for form_test_clicked_button executed.', t('Form submit handler executed.')); drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/rs/s', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button2.', t('$form_state[\'clicked_button\'] set to first available button.')); $this->assertText('Submit handler for form_test_clicked_button executed.', t('Form submit handler executed.')); // Ensure submitting a form with buttons of different types results in // $form_state['clicked_button'] being set to the first button, regardless // of type. For the FAPI 'button' type, this should result in the submit // handler not executing. The types are 's'(ubmit), 'b'(utton), and // 'i'(mage_button). drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/s/b/i', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button1.', t('$form_state[\'clicked_button\'] set to first button.')); $this->assertText('Submit handler for form_test_clicked_button executed.', t('Form submit handler executed.')); drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/b/s/i', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button1.', t('$form_state[\'clicked_button\'] set to first button.')); $this->assertNoText('Submit handler for form_test_clicked_button executed.', t('Form submit handler did not execute.')); drupal_static_reset('drupal_html_id'); $this->drupalPost($path . '/i/s/b', $edit, NULL, array(), array(), $form_id); $this->assertText('The clicked button is button1.', t('$form_state[\'clicked_button\'] set to first button.')); $this->assertText('Submit handler for form_test_clicked_button executed.', t('Form submit handler executed.')); } } /** * Tests rebuilding of arbitrary forms by altering them. */ class FormsArbitraryRebuildTestCase extends DrupalWebTestCase { public static function getInfo() { return array( 'name' => 'Rebuild arbitrary forms', 'description' => 'Tests altering forms to be rebuilt so there are multiple steps.', 'group' => 'Form API', ); } function setUp() { parent::setUp('form_test'); // Auto-create a field for testing. $field = array( 'field_name' => 'test_multiple', 'type' => 'text', 'cardinality' => -1, 'translatable' => FALSE, ); field_create_field($field); $instance = array( 'entity_type' => 'node', 'field_name' => 'test_multiple', 'bundle' => 'page', 'label' => 'Test a multiple valued field', 'widget' => array( 'type' => 'text_textfield', 'weight' => 0, ), ); field_create_instance($instance); } /** * Tests a basic rebuild with the user registration form. */ function testUserRegistrationRebuild() { $edit = array( 'name' => 'foo', 'mail' => 'bar@example.com', ); $this->drupalPost('user/register', $edit, 'Rebuild'); $this->assertText('Form rebuilt.'); $this->assertFieldByName('name', 'foo', 'Entered user name has been kept.'); $this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.'); } /** * Tests a rebuild caused by a multiple value field. */ function testUserRegistrationMultipleField() { $edit = array( 'name' => 'foo', 'mail' => 'bar@example.com', ); $this->drupalPost('user/register', $edit, t('Add another item'), array('query' => array('field' => TRUE))); $this->assertText('Test a multiple valued field', 'Form has been rebuilt.'); $this->assertFieldByName('name', 'foo', 'Entered user name has been kept.'); $this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.'); } }