Commit 90350182 authored by webchick's avatar webchick

#260934 by plach, ShawnClark, et al: Fixed Static caching: cannot call...

#260934 by plach, ShawnClark, et al: Fixed Static caching: cannot call drupal_validate_form() on the same form more than once.
parent 3205a267
......@@ -481,6 +481,10 @@ function drupal_form_submit($form_id, &$form_state) {
// Programmed forms are always submitted.
$form_state['submitted'] = TRUE;
// Reset form validation.
$form_state['must_validate'] = TRUE;
form_clear_error();
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
}
......
......@@ -784,3 +784,68 @@ class FormsRebuildTestCase extends DrupalWebTestCase {
$this->assertFieldById('edit-text-2', 'DEFAULT 2', t('A newly added textfield was initialized with its default value.'));
}
}
/**
* Test the programmatic form submission behavior.
*/
class FormsProgrammaticTestCase extends DrupalWebTestCase {
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<br/>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)));
}
}
}
......@@ -888,3 +888,37 @@ function form_test_form_form_test_state_persist_alter(&$form, &$form_state) {
$form_state['cache'] = TRUE;
}
}
/**
* Form builder to test programmatic form submissions.
*/
function form_test_programmatic_form($form, &$form_state) {
$form['submitted_field'] = array(
'#title' => 'Submitted',
'#type' => 'textfield',
);
return $form;
}
/**
* Form validation handler for programmatic form submissions.
*
* To test that the validation handler is correctly executed, the field value is
* explicitly required here.
*/
function form_test_programmatic_form_validate($form, &$form_state) {
if (empty($form_state['values']['submitted_field'])) {
form_set_error('submitted_field', t('Submitted field is required.'));
}
}
/**
* Form submit handler for programmatic form submissions.
*
* To test that the submission handler is correctly executed, we store the
* submitted value in a place we can access from the caller context.
*/
function form_test_programmatic_form_submit($form, &$form_state) {
$form_state['storage']['programmatic_form_submit'] = $form_state['values']['submitted_field'];
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment