summaryrefslogtreecommitdiff
path: root/modules/simpletest
diff options
context:
space:
mode:
authorDavid Rothstein <drothstein@gmail.com>2014-12-01 18:33:09 -0500
committerDavid Rothstein <drothstein@gmail.com>2014-12-01 18:33:09 -0500
commit8bbc2d2ea0bfb6cf12f5f6f3edf82cca6429d046 (patch)
tree54ca4e1dc49517a556107c237c77ee5355c648d0 /modules/simpletest
parentde8762b201863542b1867737997a45c7100b8f2f (diff)
downloadbrdo-8bbc2d2ea0bfb6cf12f5f6f3edf82cca6429d046.tar.gz
brdo-8bbc2d2ea0bfb6cf12f5f6f3edf82cca6429d046.tar.bz2
Issue #2380053 by klausi, pwolanin, tsphethean, sun, David_Rothstein: Posting an array as value of a form element is allowed even when a string is expected (and bypasses #maxlength constraints) - first step: text fields
Diffstat (limited to 'modules/simpletest')
-rw-r--r--modules/simpletest/tests/form.test58
1 files changed, 58 insertions, 0 deletions
diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test
index f90b854c7..0bf6c8c65 100644
--- a/modules/simpletest/tests/form.test
+++ b/modules/simpletest/tests/form.test
@@ -470,6 +470,64 @@ class FormsTestCase extends DrupalWebTestCase {
$this->drupalPost(NULL, array('checkboxes[one]' => TRUE, 'checkboxes[two]' => TRUE), t('Submit'));
$this->assertText('An illegal choice has been detected.', 'Input forgery was detected.');
}
+
+ /**
+ * Tests that submitted values are converted to scalar strings for textfields.
+ */
+ public function testTextfieldStringValue() {
+ // Check multivalued submissions.
+ $multivalue = array('evil' => 'multivalue', 'not so' => 'good');
+ $this->checkFormValue('textfield', $multivalue, '');
+ $this->checkFormValue('password', $multivalue, '');
+ $this->checkFormValue('textarea', $multivalue, '');
+ $this->checkFormValue('machine_name', $multivalue, '');
+ $this->checkFormValue('password_confirm', $multivalue, array('pass1' => '', 'pass2' => ''));
+ // Check integer submissions.
+ $integer = 5;
+ $string = '5';
+ $this->checkFormValue('textfield', $integer, $string);
+ $this->checkFormValue('password', $integer, $string);
+ $this->checkFormValue('textarea', $integer, $string);
+ $this->checkFormValue('machine_name', $integer, $string);
+ $this->checkFormValue('password_confirm', array('pass1' => $integer, 'pass2' => $integer), array('pass1' => $string, 'pass2' => $string));
+ // Check that invalid array keys are ignored for password confirm elements.
+ $this->checkFormValue('password_confirm', array('pass1' => 'test', 'pass2' => 'test', 'extra' => 'invalid'), array('pass1' => 'test', 'pass2' => 'test'));
+ }
+
+ /**
+ * Checks that a given form input value is sanitized to the expected result.
+ *
+ * @param string $element_type
+ * The form element type. Example: textfield.
+ * @param mixed $input_value
+ * The submitted user input value for the form element.
+ * @param mixed $expected_value
+ * The sanitized result value in the form state after calling
+ * form_builder().
+ */
+ protected function checkFormValue($element_type, $input_value, $expected_value) {
+ $form_id = $this->randomName();
+ $form = array();
+ $form_state = form_state_defaults();
+ $form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
+ $form[$element_type] = array(
+ '#type' => $element_type,
+ '#title' => 'test',
+ );
+
+ $form_state['input'][$element_type] = $input_value;
+ $form_state['input']['form_id'] = $form_id;
+ $form_state['method'] = 'post';
+ $form_state['values'] = array();
+ drupal_prepare_form($form_id, $form, $form_state);
+
+ // This is the main function we want to test: it is responsible for
+ // populating user supplied $form_state['input'] to sanitized
+ // $form_state['values'].
+ form_builder($form_id, $form, $form_state);
+
+ $this->assertIdentical($form_state['values'][$element_type], $expected_value, format_string('Form submission for the "@element_type" element type has been correctly sanitized.', array('@element_type' => $element_type)));
+ }
}
/**