summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorAngie Byron <webchick@24967.no-reply.drupal.org>2010-03-07 07:49:26 +0000
committerAngie Byron <webchick@24967.no-reply.drupal.org>2010-03-07 07:49:26 +0000
commit903501822d39940a605e6fce2ce8e08186fcd789 (patch)
tree1be694e46b99563869fdc214ce60b04cd42a8832 /modules
parent3205a26755ecf71b4d8503359c2ff1910254f322 (diff)
downloadbrdo-903501822d39940a605e6fce2ce8e08186fcd789.tar.gz
brdo-903501822d39940a605e6fce2ce8e08186fcd789.tar.bz2
#260934 by plach, ShawnClark, et al: Fixed Static caching: cannot call drupal_validate_form() on the same form more than once.
Diffstat (limited to 'modules')
-rw-r--r--modules/simpletest/tests/form.test65
-rw-r--r--modules/simpletest/tests/form_test.module34
2 files changed, 99 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test
index b7dec7b6a..82a9b9e08 100644
--- a/modules/simpletest/tests/form.test
+++ b/modules/simpletest/tests/form.test
@@ -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)));
+ }
+ }
+}
diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module
index 691657786..f944d709f 100644
--- a/modules/simpletest/tests/form_test.module
+++ b/modules/simpletest/tests/form_test.module
@@ -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'];
+}