summaryrefslogtreecommitdiff
path: root/modules/simpletest/tests/form_test.module
blob: a1eee25b2468422ae1f11d0b1c5084dc7914c331 (plain)
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
// $Id$

/**
 * @file
 * Helper module for the form API tests.
 */

/**
 * Implementation of hook_menu().
 */
function form_test_menu() {
  $items = array();

  $items['form_test/tableselect/multiple-true'] = array(
    'title' => 'Tableselect checkboxes test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_multiple_true_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['form_test/tableselect/multiple-false'] = array(
    'title' => 'Tableselect radio button test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_multiple_false_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['form_test/tableselect/empty-text'] = array(
    'title' => 'Tableselect empty text test',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_empty_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  $items['form_test/tableselect/advanced-select'] = array(
    'title' => 'Tableselect js_select tests',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('_form_test_tableselect_js_select_form'),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Create a header and options array. Helper function for callbacks.
 */
function _form_test_tableselect_get_data() {
  $header = array(
    'one' => t('One'),
    'two' => t('Two'),
    'three' => t('Three'),
    'four' => t('Four'),
  );

  $options['row1'] = array(
    'one' => 'row1col1',
    'two' => t('row1col2'),
    'three' => t('row1col3'),
    'four' => t('row1col4'),
  );

  $options['row2'] = array(
    'one' => 'row2col1',
    'two' => t('row2col2'),
    'three' => t('row2col3'),
    'four' => t('row2col4'),
  );

  $options['row3'] = array(
    'one' => 'row3col1',
    'two' => t('row3col2'),
    'three' => t('row3col3'),
    'four' => t('row3col4'),
  );

  return array($header, $options);
}

/**
 * Build a form to test the tableselect element.
 *
 * @param $form_state
 *   The form_state
 * @param $element_properties
 *   An array of element properties for the tableselect element.
 *
 * @return
 *   A form with a tableselect element and a submit button.
 */
function _form_test_tableselect_form_builder($form_state, $element_properties) {
  $form = array();

  list($header, $options) = _form_test_tableselect_get_data();

  $form['tableselect'] = $element_properties;

  $form['tableselect'] += array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#multiple' => FALSE,
    '#empty' => t('Empty text.'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

/**
 * Test the tableselect #multiple = TRUE functionality.
 */
function _form_test_tableselect_multiple_true_form($form_state) {
  return _form_test_tableselect_form_builder($form_state, array('#multiple' => TRUE));
}

/**
 * Process the tableselect #multiple = TRUE submitted values.
 */
function _form_test_tableselect_multiple_true_form_submit($form, &$form_state) {
  $selected = $form_state['values']['tableselect'];
  foreach ($selected as $key => $value) {
    drupal_set_message(t('Submitted: @key = @value', array('@key' => $key, '@value' => $value)));
  }
}

/**
 * Test the tableselect #multiple = FALSE functionality.
 */
function _form_test_tableselect_multiple_false_form($form_state) {
  return _form_test_tableselect_form_builder($form_state, array('#multiple' => FALSE));
}

/**
 * Process the tableselect #multiple = FALSE submitted values.
 */
function _form_test_tableselect_multiple_false_form_submit($form, &$form_state) {
  drupal_set_message(t('Submitted: @value', array('@value' => $form_state['values']['tableselect'])));
}

/**
 * Test functionality of the tableselect #empty property.
 */
function _form_test_tableselect_empty_form($form_state) {
  return _form_test_tableselect_form_builder($form_state, array('#options' => array()));
}

/**
 * Test functionality of the tableselect #js_select property.
 */
function _form_test_tableselect_js_select_form($form_state, $action) {

  switch ($action) {

    case 'multiple-true-default':
      $options = array('#multiple' => TRUE);
      break;

    case 'multiple-false-default':
      $options = array('#multiple' => FALSE);
      break;

    case 'multiple-true-no-advanced-select':
      $options = array('#multiple' => TRUE, '#js_select' => FALSE);
      break;

    case 'multiple-false-advanced-select':
      $options = array('#multiple' => FALSE, '#js_select' => TRUE);
      break;
  }

  return _form_test_tableselect_form_builder($form_state, $options);
}