diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-07-19 19:44:03 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-07-19 19:44:03 +0000 |
commit | 1c94c89e388db7783d5571a4c0cf9396c49dd8c5 (patch) | |
tree | 2df13e559f0e8a045f37e5621984cfbe3156b233 /includes/tests | |
parent | a5f9b7b0123a2be0d50e6294db35029a9e9b5ccc (diff) | |
download | brdo-1c94c89e388db7783d5571a4c0cf9396c49dd8c5.tar.gz brdo-1c94c89e388db7783d5571a4c0cf9396c49dd8c5.tar.bz2 |
- Patch #231302 by pwolanin: test required field validation in form API.
Diffstat (limited to 'includes/tests')
-rw-r--r-- | includes/tests/form.test | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/includes/tests/form.test b/includes/tests/form.test new file mode 100644 index 000000000..70cb131cb --- /dev/null +++ b/includes/tests/form.test @@ -0,0 +1,74 @@ +<?php +// $Id$ + +/** + * @file + * Unit tests for the Drupal Form API. + */ + +class FormsTestCase extends DrupalWebTestCase { + + function getInfo() { + return array( + 'name' => t('Required field validation'), + 'description' => t('Carriage returns, tabs, and spaces are not valid content for a required field.'), + 'group' => t('Form API'), + ); + } + + /** + * Check several empty values for required forms elements. + * + * 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()); + + $elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield', '#required' => TRUE); + $elements['textfield']['empty_values'] = $empty_strings; + + $elements['password']['element'] = array('#title' => $this->randomName(), '#type' => 'password', '#required' => TRUE); + $elements['password']['empty_values'] = $empty_strings; + + $elements['password_confirm']['element'] = array('#title' => $this->randomName(), '#type' => 'password_confirm', '#required' => TRUE); + $elements['password_confirm']['empty_values'] = $empty_strings; + + $elements['textarea']['element'] = array('#title' => $this->randomName(), '#type' => 'textarea', '#required' => TRUE); + $elements['textarea']['empty_values'] = $empty_strings; + + $elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#required' => TRUE, '#options' => array($this->randomName(), $this->randomName(), $this->randomName())); + $elements['radios']['empty_values'] = $empty_arrays; + + $elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#required' => TRUE,'#options' => array($this->randomName(), $this->randomName(), $this->randomName())); + $elements['checkboxes']['empty_values'] = $empty_arrays; + + $elements['select']['element'] = array('#title' => $this->randomName(), '#type' => 'select', '#required' => TRUE, '#options' => array($this->randomName(), $this->randomName(), $this->randomName())); + $elements['select']['empty_values'] = $empty_strings; + + $elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file', '#required' => TRUE); + $elements['file']['empty_values'] = $empty_strings; + + // Go through all the elements and all the empty values for them + foreach ($elements as $type => $data) { + foreach ($data['empty_values'] as $key => $empty) { + $form_id = $this->randomName(); + $form = $form_state = array(); + $form['op'] = array('#type' => 'submit', '#value' => t('Submit')); + $element = $data['element']['#title']; + $form[$element] = $data['element']; + $form_state['values'][$element] = $empty; + $form['#post'] = $form_state['values']; + $form['#post']['form_id'] = $form_id; + drupal_prepare_form($form_id, $form, $form_state); + drupal_process_form($form_id, $form, $form_state); + $errors = form_get_errors(); + $this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'"); + } + } + // Clear the expected form error messages so they don't appear as exceptions. + drupal_get_messages(); + } +} |