form.test 4.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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();
  }
}
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

/**
 * Test form type functions for expected behavior.
 */
class FormsTestTypeCase extends DrupalWebTestCase {
  function getInfo() {
    return array(
      'name' => t('Form type-specific tests'),
      'description' => t('Test form type functions for expected behavior.'),
      'group' => t('Form API'),
    );
  }

  /**
   * Test form_type_checkbox_value() function for expected behavior.
   */
  function testFormCheckboxValue() {
    $form['#return_value'] = $return_value = $this->randomName();
    $form['#default_value'] = $default_value = $this->randomName();
    // Element is disabled , and $edit is not empty.
    $form['#disabled'] = TRUE;
    $edit = array(1);
    $this->assertEqual(form_type_checkbox_value($form, $edit), $default_value, t('form_type_checkbox_value() returns the default value when #disabled is set.'));

    // Element is not disabled, $edit is not empty.
    unset($form['#disabled']);
    $this->assertEqual(form_type_checkbox_value($form, $edit), $return_value, t('form_type_checkbox_value() returns the return value when #disabled is not set.'));

    // Element is not disabled, $edit is empty.
    $edit = array();
    $this->assertIdentical(form_type_checkbox_value($form, $edit), 0, t('form_type_checkbox_value() returns 0 when #disabled is not set, and $edit is empty.'));

    // $edit is FALSE.
    $edit = FALSE;
    $this->assertNull(form_type_checkbox_value($form, $edit), t('form_type_checkbox_value() returns NULL when $edit is FALSE'));
  }
}