1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Unit tests for the Drupal Form API.
|
6
|
*/
|
7
|
|
8
|
class FormsTestCase extends DrupalWebTestCase {
|
9
|
|
10
|
public static function getInfo() {
|
11
|
return array(
|
12
|
'name' => 'Form element validation',
|
13
|
'description' => 'Tests various form element validation mechanisms.',
|
14
|
'group' => 'Form API',
|
15
|
);
|
16
|
}
|
17
|
|
18
|
function setUp() {
|
19
|
parent::setUp('form_test');
|
20
|
}
|
21
|
|
22
|
/**
|
23
|
* Check several empty values for required forms elements.
|
24
|
*
|
25
|
* Carriage returns, tabs, spaces, and unchecked checkbox elements are not
|
26
|
* valid content for a required field.
|
27
|
*
|
28
|
* If the form field is found in form_get_errors() then the test pass.
|
29
|
*/
|
30
|
function testRequiredFields() {
|
31
|
// Originates from http://drupal.org/node/117748
|
32
|
// Sets of empty strings and arrays.
|
33
|
$empty_strings = array('""' => "", '"\n"' => "\n", '" "' => " ", '"\t"' => "\t", '" \n\t "' => " \n\t ", '"\n\n\n\n\n"' => "\n\n\n\n\n");
|
34
|
$empty_arrays = array('array()' => array());
|
35
|
$empty_checkbox = array(NULL);
|
36
|
|
37
|
$elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield');
|
38
|
$elements['textfield']['empty_values'] = $empty_strings;
|
39
|
|
40
|
$elements['password']['element'] = array('#title' => $this->randomName(), '#type' => 'password');
|
41
|
$elements['password']['empty_values'] = $empty_strings;
|
42
|
|
43
|
$elements['password_confirm']['element'] = array('#title' => $this->randomName(), '#type' => 'password_confirm');
|
44
|
// Provide empty values for both password fields.
|
45
|
foreach ($empty_strings as $key => $value) {
|
46
|
$elements['password_confirm']['empty_values'][$key] = array('pass1' => $value, 'pass2' => $value);
|
47
|
}
|
48
|
|
49
|
$elements['textarea']['element'] = array('#title' => $this->randomName(), '#type' => 'textarea');
|
50
|
$elements['textarea']['empty_values'] = $empty_strings;
|
51
|
|
52
|
$elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#options' => array('' => t('None'), $this->randomName(), $this->randomName(), $this->randomName()));
|
53
|
$elements['radios']['empty_values'] = $empty_arrays;
|
54
|
|
55
|
$elements['checkbox']['element'] = array('#title' => $this->randomName(), '#type' => 'checkbox', '#required' => TRUE);
|
56
|
$elements['checkbox']['empty_values'] = $empty_checkbox;
|
57
|
|
58
|
$elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
|
59
|
$elements['checkboxes']['empty_values'] = $empty_arrays;
|
60
|
|
61
|
$elements['select']['element'] = array('#title' => $this->randomName(), '#type' => 'select', '#options' => array('' => t('None'), $this->randomName(), $this->randomName(), $this->randomName()));
|
62
|
$elements['select']['empty_values'] = $empty_strings;
|
63
|
|
64
|
$elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file');
|
65
|
$elements['file']['empty_values'] = $empty_strings;
|
66
|
|
67
|
// Regular expression to find the expected marker on required elements.
|
68
|
$required_marker_preg = '@<label.*<span class="form-required" title="This field is required\.">\*</span></label>@';
|
69
|
|
70
|
// Go through all the elements and all the empty values for them.
|
71
|
foreach ($elements as $type => $data) {
|
72
|
foreach ($data['empty_values'] as $key => $empty) {
|
73
|
foreach (array(TRUE, FALSE) as $required) {
|
74
|
$form_id = $this->randomName();
|
75
|
$form = array();
|
76
|
$form_state = form_state_defaults();
|
77
|
form_clear_error();
|
78
|
$form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
|
79
|
$element = $data['element']['#title'];
|
80
|
$form[$element] = $data['element'];
|
81
|
$form[$element]['#required'] = $required;
|
82
|
$form_state['input'][$element] = $empty;
|
83
|
$form_state['input']['form_id'] = $form_id;
|
84
|
$form_state['method'] = 'post';
|
85
|
|
86
|
// The form token CSRF protection should not interfere with this test,
|
87
|
// so we bypass it by marking this test form as programmed.
|
88
|
$form_state['programmed'] = TRUE;
|
89
|
drupal_prepare_form($form_id, $form, $form_state);
|
90
|
drupal_process_form($form_id, $form, $form_state);
|
91
|
$errors = form_get_errors();
|
92
|
// Form elements of type 'radios' throw all sorts of PHP notices
|
93
|
// when you try to render them like this, so we ignore those for
|
94
|
// testing the required marker.
|
95
|
// @todo Fix this work-around (http://drupal.org/node/588438).
|
96
|
$form_output = ($type == 'radios') ? '' : drupal_render($form);
|
97
|
if ($required) {
|
98
|
// Make sure we have a form error for this element.
|
99
|
$this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'");
|
100
|
if (!empty($form_output)) {
|
101
|
// Make sure the form element is marked as required.
|
102
|
$this->assertTrue(preg_match($required_marker_preg, $form_output), "Required '$type' field is marked as required");
|
103
|
}
|
104
|
}
|
105
|
else {
|
106
|
if (!empty($form_output)) {
|
107
|
// Make sure the form element is *not* marked as required.
|
108
|
$this->assertFalse(preg_match($required_marker_preg, $form_output), "Optional '$type' field is not marked as required");
|
109
|
}
|
110
|
if ($type == 'select') {
|
111
|
// Select elements are going to have validation errors with empty
|
112
|
// input, since those are illegal choices. Just make sure the
|
113
|
// error is not "field is required".
|
114
|
$this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element");
|
115
|
}
|
116
|
else {
|
117
|
// Make sure there is *no* form error for this element.
|
118
|
$this->assertTrue(empty($errors[$element]), "Optional '$type' field '$element' has no errors with empty input");
|
119
|
}
|
120
|
}
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
// Clear the expected form error messages so they don't appear as exceptions.
|
125
|
drupal_get_messages();
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Tests validation for required checkbox, select, and radio elements.
|
130
|
*
|
131
|
* Submits a test form containing several types of form elements. The form
|
132
|
* is submitted twice, first without values for required fields and then
|
133
|
* with values. Each submission is checked for relevant error messages.
|
134
|
*
|
135
|
* @see form_test_validate_required_form()
|
136
|
*/
|
137
|
function testRequiredCheckboxesRadio() {
|
138
|
$form = $form_state = array();
|
139
|
$form = form_test_validate_required_form($form, $form_state);
|
140
|
|
141
|
// Attempt to submit the form with no required fields set.
|
142
|
$edit = array();
|
143
|
$this->drupalPost('form-test/validate-required', $edit, 'Submit');
|
144
|
|
145
|
// The only error messages that should appear are the relevant 'required'
|
146
|
// messages for each field.
|
147
|
$expected = array();
|
148
|
foreach (array('textfield', 'checkboxes', 'select', 'radios') as $key) {
|
149
|
$expected[] = t('!name field is required.', array('!name' => $form[$key]['#title']));
|
150
|
}
|
151
|
|
152
|
// Check the page for error messages.
|
153
|
$errors = $this->xpath('//div[contains(@class, "error")]//li');
|
154
|
foreach ($errors as $error) {
|
155
|
$expected_key = array_search($error[0], $expected);
|
156
|
// If the error message is not one of the expected messages, fail.
|
157
|
if ($expected_key === FALSE) {
|
158
|
$this->fail(format_string("Unexpected error message: @error", array('@error' => $error[0])));
|
159
|
}
|
160
|
// Remove the expected message from the list once it is found.
|
161
|
else {
|
162
|
unset($expected[$expected_key]);
|
163
|
}
|
164
|
}
|
165
|
|
166
|
// Fail if any expected messages were not found.
|
167
|
foreach ($expected as $not_found) {
|
168
|
$this->fail(format_string("Found error message: @error", array('@error' => $not_found)));
|
169
|
}
|
170
|
|
171
|
// Verify that input elements are still empty.
|
172
|
$this->assertFieldByName('textfield', '');
|
173
|
$this->assertNoFieldChecked('edit-checkboxes-foo');
|
174
|
$this->assertNoFieldChecked('edit-checkboxes-bar');
|
175
|
$this->assertOptionSelected('edit-select', '');
|
176
|
$this->assertNoFieldChecked('edit-radios-foo');
|
177
|
$this->assertNoFieldChecked('edit-radios-bar');
|
178
|
$this->assertNoFieldChecked('edit-radios-optional-foo');
|
179
|
$this->assertNoFieldChecked('edit-radios-optional-bar');
|
180
|
$this->assertNoFieldChecked('edit-radios-optional-default-value-false-foo');
|
181
|
$this->assertNoFieldChecked('edit-radios-optional-default-value-false-bar');
|
182
|
|
183
|
// Submit again with required fields set and verify that there are no
|
184
|
// error messages.
|
185
|
$edit = array(
|
186
|
'textfield' => $this->randomString(),
|
187
|
'checkboxes[foo]' => TRUE,
|
188
|
'select' => 'foo',
|
189
|
'radios' => 'bar',
|
190
|
);
|
191
|
$this->drupalPost(NULL, $edit, 'Submit');
|
192
|
$this->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed when all required fields are filled.');
|
193
|
$this->assertRaw("The form_test_validate_required_form form was submitted successfully.", 'Validation form submitted successfully.');
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Tests validation for required textfield element without title.
|
198
|
*
|
199
|
* Submits a test form containing a textfield form elements without title.
|
200
|
* The form is submitted twice, first without value for the required field
|
201
|
* and then with value. Each submission is checked for relevant error
|
202
|
* messages.
|
203
|
*
|
204
|
* @see form_test_validate_required_form_no_title()
|
205
|
*/
|
206
|
function testRequiredTextfieldNoTitle() {
|
207
|
$form = $form_state = array();
|
208
|
$form = form_test_validate_required_form_no_title($form, $form_state);
|
209
|
|
210
|
// Attempt to submit the form with no required field set.
|
211
|
$edit = array();
|
212
|
$this->drupalPost('form-test/validate-required-no-title', $edit, 'Submit');
|
213
|
$this->assertNoRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');
|
214
|
|
215
|
// Check the page for the error class on the textfield.
|
216
|
$this->assertFieldByXPath('//input[contains(@class, "error")]', FALSE, 'Error input form element class found.');
|
217
|
|
218
|
// Submit again with required fields set and verify that there are no
|
219
|
// error messages.
|
220
|
$edit = array(
|
221
|
'textfield' => $this->randomString(),
|
222
|
);
|
223
|
$this->drupalPost(NULL, $edit, 'Submit');
|
224
|
$this->assertNoFieldByXpath('//input[contains(@class, "error")]', FALSE, 'No error input form element class found.');
|
225
|
$this->assertRaw("The form_test_validate_required_form_no_title form was submitted successfully.", 'Validation form submitted successfully.');
|
226
|
}
|
227
|
|
228
|
/**
|
229
|
* Test default value handling for checkboxes.
|
230
|
*
|
231
|
* @see _form_test_checkbox()
|
232
|
*/
|
233
|
function testCheckboxProcessing() {
|
234
|
// First, try to submit without the required checkbox.
|
235
|
$edit = array();
|
236
|
$this->drupalPost('form-test/checkbox', $edit, t('Submit'));
|
237
|
$this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), 'A required checkbox is actually mandatory');
|
238
|
|
239
|
// Now try to submit the form correctly.
|
240
|
$values = drupal_json_decode($this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit')));
|
241
|
$expected_values = array(
|
242
|
'disabled_checkbox_on' => 'disabled_checkbox_on',
|
243
|
'disabled_checkbox_off' => '',
|
244
|
'checkbox_on' => 'checkbox_on',
|
245
|
'checkbox_off' => '',
|
246
|
'zero_checkbox_on' => '0',
|
247
|
'zero_checkbox_off' => '',
|
248
|
);
|
249
|
foreach ($expected_values as $widget => $expected_value) {
|
250
|
$this->assertEqual($values[$widget], $expected_value, format_string('Checkbox %widget returns expected value (expected: %expected, got: %value)', array(
|
251
|
'%widget' => var_export($widget, TRUE),
|
252
|
'%expected' => var_export($expected_value, TRUE),
|
253
|
'%value' => var_export($values[$widget], TRUE),
|
254
|
)));
|
255
|
}
|
256
|
}
|
257
|
|
258
|
/**
|
259
|
* Tests validation of #type 'select' elements.
|
260
|
*/
|
261
|
function testSelect() {
|
262
|
$form = $form_state = array();
|
263
|
$form = form_test_select($form, $form_state);
|
264
|
$error = '!name field is required.';
|
265
|
$this->drupalGet('form-test/select');
|
266
|
|
267
|
// Posting without any values should throw validation errors.
|
268
|
$this->drupalPost(NULL, array(), 'Submit');
|
269
|
$this->assertNoText(t($error, array('!name' => $form['select']['#title'])));
|
270
|
$this->assertNoText(t($error, array('!name' => $form['select_required']['#title'])));
|
271
|
$this->assertNoText(t($error, array('!name' => $form['select_optional']['#title'])));
|
272
|
$this->assertNoText(t($error, array('!name' => $form['empty_value']['#title'])));
|
273
|
$this->assertNoText(t($error, array('!name' => $form['empty_value_one']['#title'])));
|
274
|
$this->assertText(t($error, array('!name' => $form['no_default']['#title'])));
|
275
|
$this->assertNoText(t($error, array('!name' => $form['no_default_optional']['#title'])));
|
276
|
$this->assertText(t($error, array('!name' => $form['no_default_empty_option']['#title'])));
|
277
|
$this->assertNoText(t($error, array('!name' => $form['no_default_empty_option_optional']['#title'])));
|
278
|
$this->assertText(t($error, array('!name' => $form['no_default_empty_value']['#title'])));
|
279
|
$this->assertText(t($error, array('!name' => $form['no_default_empty_value_one']['#title'])));
|
280
|
$this->assertNoText(t($error, array('!name' => $form['no_default_empty_value_optional']['#title'])));
|
281
|
$this->assertNoText(t($error, array('!name' => $form['multiple']['#title'])));
|
282
|
$this->assertNoText(t($error, array('!name' => $form['multiple_no_default']['#title'])));
|
283
|
$this->assertText(t($error, array('!name' => $form['multiple_no_default_required']['#title'])));
|
284
|
|
285
|
// Post values for required fields.
|
286
|
$edit = array(
|
287
|
'no_default' => 'three',
|
288
|
'no_default_empty_option' => 'three',
|
289
|
'no_default_empty_value' => 'three',
|
290
|
'no_default_empty_value_one' => 'three',
|
291
|
'multiple_no_default_required[]' => 'three',
|
292
|
);
|
293
|
$this->drupalPost(NULL, $edit, 'Submit');
|
294
|
$values = drupal_json_decode($this->drupalGetContent());
|
295
|
|
296
|
// Verify expected values.
|
297
|
$expected = array(
|
298
|
'select' => 'one',
|
299
|
'empty_value' => 'one',
|
300
|
'empty_value_one' => 'one',
|
301
|
'no_default' => 'three',
|
302
|
'no_default_optional' => 'one',
|
303
|
'no_default_optional_empty_value' => '',
|
304
|
'no_default_empty_option' => 'three',
|
305
|
'no_default_empty_option_optional' => '',
|
306
|
'no_default_empty_value' => 'three',
|
307
|
'no_default_empty_value_one' => 'three',
|
308
|
'no_default_empty_value_optional' => 0,
|
309
|
'multiple' => array('two' => 'two'),
|
310
|
'multiple_no_default' => array(),
|
311
|
'multiple_no_default_required' => array('three' => 'three'),
|
312
|
);
|
313
|
foreach ($expected as $key => $value) {
|
314
|
$this->assertIdentical($values[$key], $value, format_string('@name: @actual is equal to @expected.', array(
|
315
|
'@name' => $key,
|
316
|
'@actual' => var_export($values[$key], TRUE),
|
317
|
'@expected' => var_export($value, TRUE),
|
318
|
)));
|
319
|
}
|
320
|
}
|
321
|
|
322
|
/**
|
323
|
* Test handling of disabled elements.
|
324
|
*
|
325
|
* @see _form_test_disabled_elements()
|
326
|
*/
|
327
|
function testDisabledElements() {
|
328
|
// Get the raw form in its original state.
|
329
|
$form_state = array();
|
330
|
$form = _form_test_disabled_elements(array(), $form_state);
|
331
|
|
332
|
// Build a submission that tries to hijack the form by submitting input for
|
333
|
// elements that are disabled.
|
334
|
$edit = array();
|
335
|
foreach (element_children($form) as $key) {
|
336
|
if (isset($form[$key]['#test_hijack_value'])) {
|
337
|
if (is_array($form[$key]['#test_hijack_value'])) {
|
338
|
foreach ($form[$key]['#test_hijack_value'] as $subkey => $value) {
|
339
|
$edit[$key . '[' . $subkey . ']'] = $value;
|
340
|
}
|
341
|
}
|
342
|
else {
|
343
|
$edit[$key] = $form[$key]['#test_hijack_value'];
|
344
|
}
|
345
|
}
|
346
|
}
|
347
|
|
348
|
// Submit the form with no input, as the browser does for disabled elements,
|
349
|
// and fetch the $form_state['values'] that is passed to the submit handler.
|
350
|
$this->drupalPost('form-test/disabled-elements', array(), t('Submit'));
|
351
|
$returned_values['normal'] = drupal_json_decode($this->content);
|
352
|
|
353
|
// Do the same with input, as could happen if JavaScript un-disables an
|
354
|
// element. drupalPost() emulates a browser by not submitting input for
|
355
|
// disabled elements, so we need to un-disable those elements first.
|
356
|
$this->drupalGet('form-test/disabled-elements');
|
357
|
$disabled_elements = array();
|
358
|
foreach ($this->xpath('//*[@disabled]') as $element) {
|
359
|
$disabled_elements[] = (string) $element['name'];
|
360
|
unset($element['disabled']);
|
361
|
}
|
362
|
|
363
|
// All the elements should be marked as disabled, including the ones below
|
364
|
// the disabled container.
|
365
|
$this->assertEqual(count($disabled_elements), 32, 'The correct elements have the disabled property in the HTML code.');
|
366
|
|
367
|
$this->drupalPost(NULL, $edit, t('Submit'));
|
368
|
$returned_values['hijacked'] = drupal_json_decode($this->content);
|
369
|
|
370
|
// Ensure that the returned values match the form's default values in both
|
371
|
// cases.
|
372
|
foreach ($returned_values as $type => $values) {
|
373
|
$this->assertFormValuesDefault($values, $form);
|
374
|
}
|
375
|
}
|
376
|
|
377
|
/**
|
378
|
* Assert that the values submitted to a form matches the default values of the elements.
|
379
|
*/
|
380
|
function assertFormValuesDefault($values, $form) {
|
381
|
foreach (element_children($form) as $key) {
|
382
|
if (isset($form[$key]['#default_value'])) {
|
383
|
if (isset($form[$key]['#expected_value'])) {
|
384
|
$expected_value = $form[$key]['#expected_value'];
|
385
|
}
|
386
|
else {
|
387
|
$expected_value = $form[$key]['#default_value'];
|
388
|
}
|
389
|
|
390
|
if ($key == 'checkboxes_multiple') {
|
391
|
// Checkboxes values are not filtered out.
|
392
|
$values[$key] = array_filter($values[$key]);
|
393
|
}
|
394
|
$this->assertIdentical($expected_value, $values[$key], format_string('Default value for %type: expected %expected, returned %returned.', array('%type' => $key, '%expected' => var_export($expected_value, TRUE), '%returned' => var_export($values[$key], TRUE))));
|
395
|
}
|
396
|
|
397
|
// Recurse children.
|
398
|
$this->assertFormValuesDefault($values, $form[$key]);
|
399
|
}
|
400
|
}
|
401
|
|
402
|
/**
|
403
|
* Verify markup for disabled form elements.
|
404
|
*
|
405
|
* @see _form_test_disabled_elements()
|
406
|
*/
|
407
|
function testDisabledMarkup() {
|
408
|
$this->drupalGet('form-test/disabled-elements');
|
409
|
$form_state = array();
|
410
|
$form = _form_test_disabled_elements(array(), $form_state);
|
411
|
$type_map = array(
|
412
|
'textarea' => 'textarea',
|
413
|
'select' => 'select',
|
414
|
'weight' => 'select',
|
415
|
'date' => 'select',
|
416
|
);
|
417
|
|
418
|
foreach ($form as $name => $item) {
|
419
|
// Skip special #types.
|
420
|
if (!isset($item['#type']) || in_array($item['#type'], array('hidden', 'text_format'))) {
|
421
|
continue;
|
422
|
}
|
423
|
// Setup XPath and CSS class depending on #type.
|
424
|
if (in_array($item['#type'], array('image_button', 'button', 'submit'))) {
|
425
|
$path = "//!type[contains(@class, :div-class) and @value=:value]";
|
426
|
$class = 'form-button-disabled';
|
427
|
}
|
428
|
else {
|
429
|
// starts-with() required for checkboxes.
|
430
|
$path = "//div[contains(@class, :div-class)]/descendant::!type[starts-with(@name, :name)]";
|
431
|
$class = 'form-disabled';
|
432
|
}
|
433
|
// Replace DOM element name in $path according to #type.
|
434
|
$type = 'input';
|
435
|
if (isset($type_map[$item['#type']])) {
|
436
|
$type = $type_map[$item['#type']];
|
437
|
}
|
438
|
$path = strtr($path, array('!type' => $type));
|
439
|
// Verify that the element exists.
|
440
|
$element = $this->xpath($path, array(
|
441
|
':name' => check_plain($name),
|
442
|
':div-class' => $class,
|
443
|
':value' => isset($item['#value']) ? $item['#value'] : '',
|
444
|
));
|
445
|
$this->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array('%type' => $item['#type'])));
|
446
|
}
|
447
|
|
448
|
// Verify special element #type text-format.
|
449
|
$element = $this->xpath('//div[contains(@class, :div-class)]/descendant::textarea[@name=:name]', array(
|
450
|
':name' => 'text_format[value]',
|
451
|
':div-class' => 'form-disabled',
|
452
|
));
|
453
|
$this->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array('%type' => 'text_format[value]')));
|
454
|
$element = $this->xpath('//div[contains(@class, :div-class)]/descendant::select[@name=:name]', array(
|
455
|
':name' => 'text_format[format]',
|
456
|
':div-class' => 'form-disabled',
|
457
|
));
|
458
|
$this->assertTrue(isset($element[0]), format_string('Disabled form element class found for #type %type.', array('%type' => 'text_format[format]')));
|
459
|
}
|
460
|
|
461
|
/**
|
462
|
* Test Form API protections against input forgery.
|
463
|
*
|
464
|
* @see _form_test_input_forgery()
|
465
|
*/
|
466
|
function testInputForgery() {
|
467
|
$this->drupalGet('form-test/input-forgery');
|
468
|
$checkbox = $this->xpath('//input[@name="checkboxes[two]"]');
|
469
|
$checkbox[0]['value'] = 'FORGERY';
|
470
|
$this->drupalPost(NULL, array('checkboxes[one]' => TRUE, 'checkboxes[two]' => TRUE), t('Submit'));
|
471
|
$this->assertText('An illegal choice has been detected.', 'Input forgery was detected.');
|
472
|
}
|
473
|
}
|
474
|
|
475
|
/**
|
476
|
* Tests building and processing of core form elements.
|
477
|
*/
|
478
|
class FormElementTestCase extends DrupalWebTestCase {
|
479
|
protected $profile = 'testing';
|
480
|
|
481
|
public static function getInfo() {
|
482
|
return array(
|
483
|
'name' => 'Element processing',
|
484
|
'description' => 'Tests building and processing of core form elements.',
|
485
|
'group' => 'Form API',
|
486
|
);
|
487
|
}
|
488
|
|
489
|
function setUp() {
|
490
|
parent::setUp(array('form_test'));
|
491
|
}
|
492
|
|
493
|
/**
|
494
|
* Tests expansion of #options for #type checkboxes and radios.
|
495
|
*/
|
496
|
function testOptions() {
|
497
|
$this->drupalGet('form-test/checkboxes-radios');
|
498
|
|
499
|
// Verify that all options appear in their defined order.
|
500
|
foreach (array('checkbox', 'radio') as $type) {
|
501
|
$elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
|
502
|
$expected_values = array('0', 'foo', '1', 'bar', '>');
|
503
|
foreach ($elements as $element) {
|
504
|
$expected = array_shift($expected_values);
|
505
|
$this->assertIdentical((string) $element['value'], $expected);
|
506
|
}
|
507
|
}
|
508
|
|
509
|
// Enable customized option sub-elements.
|
510
|
$this->drupalGet('form-test/checkboxes-radios/customize');
|
511
|
|
512
|
// Verify that all options appear in their defined order, taking a custom
|
513
|
// #weight into account.
|
514
|
foreach (array('checkbox', 'radio') as $type) {
|
515
|
$elements = $this->xpath('//input[@type=:type]', array(':type' => $type));
|
516
|
$expected_values = array('0', 'foo', 'bar', '>', '1');
|
517
|
foreach ($elements as $element) {
|
518
|
$expected = array_shift($expected_values);
|
519
|
$this->assertIdentical((string) $element['value'], $expected);
|
520
|
}
|
521
|
}
|
522
|
// Verify that custom #description properties are output.
|
523
|
foreach (array('checkboxes', 'radios') as $type) {
|
524
|
$elements = $this->xpath('//input[@id=:id]/following-sibling::div[@class=:class]', array(
|
525
|
':id' => 'edit-' . $type . '-foo',
|
526
|
':class' => 'description',
|
527
|
));
|
528
|
$this->assertTrue(count($elements), format_string('Custom %type option description found.', array(
|
529
|
'%type' => $type,
|
530
|
)));
|
531
|
}
|
532
|
}
|
533
|
}
|
534
|
|
535
|
/**
|
536
|
* Test form alter hooks.
|
537
|
*/
|
538
|
class FormAlterTestCase extends DrupalWebTestCase {
|
539
|
public static function getInfo() {
|
540
|
return array(
|
541
|
'name' => 'Form alter hooks',
|
542
|
'description' => 'Tests hook_form_alter() and hook_form_FORM_ID_alter().',
|
543
|
'group' => 'Form API',
|
544
|
);
|
545
|
}
|
546
|
|
547
|
function setUp() {
|
548
|
parent::setUp('form_test');
|
549
|
}
|
550
|
|
551
|
/**
|
552
|
* Tests execution order of hook_form_alter() and hook_form_FORM_ID_alter().
|
553
|
*/
|
554
|
function testExecutionOrder() {
|
555
|
$this->drupalGet('form-test/alter');
|
556
|
// Ensure that the order is first by module, then for a given module, the
|
557
|
// id-specific one after the generic one.
|
558
|
$expected = array(
|
559
|
'block_form_form_test_alter_form_alter() executed.',
|
560
|
'form_test_form_alter() executed.',
|
561
|
'form_test_form_form_test_alter_form_alter() executed.',
|
562
|
'system_form_form_test_alter_form_alter() executed.',
|
563
|
);
|
564
|
$content = preg_replace('/\s+/', ' ', filter_xss($this->content, array()));
|
565
|
$this->assert(strpos($content, implode(' ', $expected)) !== FALSE, 'Form alter hooks executed in the expected order.');
|
566
|
}
|
567
|
}
|
568
|
|
569
|
/**
|
570
|
* Test form validation handlers.
|
571
|
*/
|
572
|
class FormValidationTestCase extends DrupalWebTestCase {
|
573
|
public static function getInfo() {
|
574
|
return array(
|
575
|
'name' => 'Form validation handlers',
|
576
|
'description' => 'Tests form processing and alteration via form validation handlers.',
|
577
|
'group' => 'Form API',
|
578
|
);
|
579
|
}
|
580
|
|
581
|
function setUp() {
|
582
|
parent::setUp('form_test');
|
583
|
}
|
584
|
|
585
|
/**
|
586
|
* Tests form alterations by #element_validate, #validate, and form_set_value().
|
587
|
*/
|
588
|
function testValidate() {
|
589
|
$this->drupalGet('form-test/validate');
|
590
|
// Verify that #element_validate handlers can alter the form and submitted
|
591
|
// form values.
|
592
|
$edit = array(
|
593
|
'name' => 'element_validate',
|
594
|
);
|
595
|
$this->drupalPost(NULL, $edit, 'Save');
|
596
|
$this->assertFieldByName('name', '#value changed by #element_validate', 'Form element #value was altered.');
|
597
|
$this->assertText('Name value: value changed by form_set_value() in #element_validate', 'Form element value in $form_state was altered.');
|
598
|
|
599
|
// Verify that #validate handlers can alter the form and submitted
|
600
|
// form values.
|
601
|
$edit = array(
|
602
|
'name' => 'validate',
|
603
|
);
|
604
|
$this->drupalPost(NULL, $edit, 'Save');
|
605
|
$this->assertFieldByName('name', '#value changed by #validate', 'Form element #value was altered.');
|
606
|
$this->assertText('Name value: value changed by form_set_value() in #validate', 'Form element value in $form_state was altered.');
|
607
|
|
608
|
// Verify that #element_validate handlers can make form elements
|
609
|
// inaccessible, but values persist.
|
610
|
$edit = array(
|
611
|
'name' => 'element_validate_access',
|
612
|
);
|
613
|
$this->drupalPost(NULL, $edit, 'Save');
|
614
|
$this->assertNoFieldByName('name', 'Form element was hidden.');
|
615
|
$this->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
|
616
|
|
617
|
// Verify that value for inaccessible form element persists.
|
618
|
$this->drupalPost(NULL, array(), 'Save');
|
619
|
$this->assertNoFieldByName('name', 'Form element was hidden.');
|
620
|
$this->assertText('Name value: element_validate_access', 'Value for inaccessible form element exists.');
|
621
|
|
622
|
// Verify that #validate handlers don't run if the CSRF token is invalid.
|
623
|
$this->drupalLogin($this->drupalCreateUser());
|
624
|
$this->drupalGet('form-test/validate');
|
625
|
$edit = array(
|
626
|
'name' => 'validate',
|
627
|
'form_token' => 'invalid token'
|
628
|
);
|
629
|
$this->drupalPost(NULL, $edit, 'Save');
|
630
|
$this->assertNoFieldByName('name', '#value changed by #validate', 'Form element #value was not altered.');
|
631
|
$this->assertNoText('Name value: value changed by form_set_value() in #validate', 'Form element value in $form_state was not altered.');
|
632
|
$this->assertText('The form has become outdated. Copy any unsaved work in the form below');
|
633
|
}
|
634
|
|
635
|
/**
|
636
|
* Tests partial form validation through #limit_validation_errors.
|
637
|
*/
|
638
|
function testValidateLimitErrors() {
|
639
|
$edit = array(
|
640
|
'test' => 'invalid',
|
641
|
'test_numeric_index[0]' => 'invalid',
|
642
|
'test_substring[foo]' => 'invalid',
|
643
|
);
|
644
|
$path = 'form-test/limit-validation-errors';
|
645
|
|
646
|
// Submit the form by pressing the 'Partial validate' button (uses
|
647
|
// #limit_validation_errors) and ensure that the title field is not
|
648
|
// validated, but the #element_validate handler for the 'test' field
|
649
|
// is triggered.
|
650
|
$this->drupalPost($path, $edit, t('Partial validate'));
|
651
|
$this->assertNoText(t('!name field is required.', array('!name' => 'Title')));
|
652
|
$this->assertText('Test element is invalid');
|
653
|
|
654
|
// Edge case of #limit_validation_errors containing numeric indexes: same
|
655
|
// thing with the 'Partial validate (numeric index)' button and the
|
656
|
// 'test_numeric_index' field.
|
657
|
$this->drupalPost($path, $edit, t('Partial validate (numeric index)'));
|
658
|
$this->assertNoText(t('!name field is required.', array('!name' => 'Title')));
|
659
|
$this->assertText('Test (numeric index) element is invalid');
|
660
|
|
661
|
// Ensure something like 'foobar' isn't considered "inside" 'foo'.
|
662
|
$this->drupalPost($path, $edit, t('Partial validate (substring)'));
|
663
|
$this->assertNoText(t('!name field is required.', array('!name' => 'Title')));
|
664
|
$this->assertText('Test (substring) foo element is invalid');
|
665
|
|
666
|
// Ensure not validated values are not available to submit handlers.
|
667
|
$this->drupalPost($path, array('title' => '', 'test' => 'valid'), t('Partial validate'));
|
668
|
$this->assertText('Only validated values appear in the form values.');
|
669
|
|
670
|
// Now test full form validation and ensure that the #element_validate
|
671
|
// handler is still triggered.
|
672
|
$this->drupalPost($path, $edit, t('Full validate'));
|
673
|
$this->assertText(t('!name field is required.', array('!name' => 'Title')));
|
674
|
$this->assertText('Test element is invalid');
|
675
|
}
|
676
|
|
677
|
/**
|
678
|
* Tests error border of multiple fields with same name in a page.
|
679
|
*/
|
680
|
function testMultiFormSameNameErrorClass() {
|
681
|
$this->drupalGet('form-test/double-form');
|
682
|
$edit = array();
|
683
|
$this->drupalPost(NULL, $edit, t('Save'));
|
684
|
$this->assertFieldByXpath('//input[@id="edit-name" and contains(@class, "error")]', NULL, 'Error input form element class found for first element.');
|
685
|
$this->assertNoFieldByXpath('//input[@id="edit-name--2" and contains(@class, "error")]', NULL, 'No error input form element class found for second element.');
|
686
|
}
|
687
|
}
|
688
|
|
689
|
/**
|
690
|
* Test form element labels, required markers and associated output.
|
691
|
*/
|
692
|
class FormsElementsLabelsTestCase extends DrupalWebTestCase {
|
693
|
|
694
|
public static function getInfo() {
|
695
|
return array(
|
696
|
'name' => 'Form element and label output test',
|
697
|
'description' => 'Test form element labels, required markers and associated output.',
|
698
|
'group' => 'Form API',
|
699
|
);
|
700
|
}
|
701
|
|
702
|
function setUp() {
|
703
|
parent::setUp('form_test');
|
704
|
}
|
705
|
|
706
|
/**
|
707
|
* Test form elements, labels, title attibutes and required marks output
|
708
|
* correctly and have the correct label option class if needed.
|
709
|
*/
|
710
|
function testFormLabels() {
|
711
|
$this->drupalGet('form_test/form-labels');
|
712
|
|
713
|
// Check that the checkbox/radio processing is not interfering with
|
714
|
// basic placement.
|
715
|
$elements = $this->xpath('//input[@id="edit-form-checkboxes-test-third-checkbox"]/following-sibling::label[@for="edit-form-checkboxes-test-third-checkbox" and @class="option"]');
|
716
|
$this->assertTrue(isset($elements[0]), "Label follows field and label option class correct for regular checkboxes.");
|
717
|
|
718
|
// Make sure the label is rendered for checkboxes.
|
719
|
$elements = $this->xpath('//input[@id="edit-form-checkboxes-test-0"]/following-sibling::label[@for="edit-form-checkboxes-test-0" and @class="option"]');
|
720
|
$this->assertTrue(isset($elements[0]), "Label 0 found checkbox.");
|
721
|
|
722
|
$elements = $this->xpath('//input[@id="edit-form-radios-test-second-radio"]/following-sibling::label[@for="edit-form-radios-test-second-radio" and @class="option"]');
|
723
|
$this->assertTrue(isset($elements[0]), "Label follows field and label option class correct for regular radios.");
|
724
|
|
725
|
// Make sure the label is rendered for radios.
|
726
|
$elements = $this->xpath('//input[@id="edit-form-radios-test-0"]/following-sibling::label[@for="edit-form-radios-test-0" and @class="option"]');
|
727
|
$this->assertTrue(isset($elements[0]), "Label 0 found radios.");
|
728
|
|
729
|
// Exercise various defaults for checkboxes and modifications to ensure
|
730
|
// appropriate override and correct behavior.
|
731
|
$elements = $this->xpath('//input[@id="edit-form-checkbox-test"]/following-sibling::label[@for="edit-form-checkbox-test" and @class="option"]');
|
732
|
$this->assertTrue(isset($elements[0]), "Label follows field and label option class correct for a checkbox by default.");
|
733
|
|
734
|
// Exercise various defaults for textboxes and modifications to ensure
|
735
|
// appropriate override and correct behavior.
|
736
|
$elements = $this->xpath('//label[@for="edit-form-textfield-test-title-and-required"]/child::span[@class="form-required"]/parent::*/following-sibling::input[@id="edit-form-textfield-test-title-and-required"]');
|
737
|
$this->assertTrue(isset($elements[0]), "Label precedes textfield, with required marker inside label.");
|
738
|
|
739
|
$elements = $this->xpath('//input[@id="edit-form-textfield-test-no-title-required"]/preceding-sibling::label[@for="edit-form-textfield-test-no-title-required"]/span[@class="form-required"]');
|
740
|
$this->assertTrue(isset($elements[0]), "Label tag with required marker precedes required textfield with no title.");
|
741
|
|
742
|
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title-invisible"]/preceding-sibling::label[@for="edit-form-textfield-test-title-invisible" and @class="element-invisible"]');
|
743
|
$this->assertTrue(isset($elements[0]), "Label preceding field and label class is element-invisible.");
|
744
|
|
745
|
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title"]/preceding-sibling::span[@class="form-required"]');
|
746
|
$this->assertFalse(isset($elements[0]), "No required marker on non-required field.");
|
747
|
|
748
|
$elements = $this->xpath('//input[@id="edit-form-textfield-test-title-after"]/following-sibling::label[@for="edit-form-textfield-test-title-after" and @class="option"]');
|
749
|
$this->assertTrue(isset($elements[0]), "Label after field and label option class correct for text field.");
|
750
|
|
751
|
$elements = $this->xpath('//label[@for="edit-form-textfield-test-title-no-show"]');
|
752
|
$this->assertFalse(isset($elements[0]), "No label tag when title set not to display.");
|
753
|
|
754
|
// Check #field_prefix and #field_suffix placement.
|
755
|
$elements = $this->xpath('//span[@class="field-prefix"]/following-sibling::div[@id="edit-form-radios-test"]');
|
756
|
$this->assertTrue(isset($elements[0]), "Properly placed the #field_prefix element after the label and before the field.");
|
757
|
|
758
|
$elements = $this->xpath('//span[@class="field-suffix"]/preceding-sibling::div[@id="edit-form-radios-test"]');
|
759
|
$this->assertTrue(isset($elements[0]), "Properly places the #field_suffix element immediately after the form field.");
|
760
|
|
761
|
// Check #prefix and #suffix placement.
|
762
|
$elements = $this->xpath('//div[@id="form-test-textfield-title-prefix"]/following-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
|
763
|
$this->assertTrue(isset($elements[0]), "Properly places the #prefix element before the form item.");
|
764
|
|
765
|
$elements = $this->xpath('//div[@id="form-test-textfield-title-suffix"]/preceding-sibling::div[contains(@class, \'form-item-form-textfield-test-title\')]');
|
766
|
$this->assertTrue(isset($elements[0]), "Properly places the #suffix element before the form item.");
|
767
|
|
768
|
// Check title attribute for radios and checkboxes.
|
769
|
$elements = $this->xpath('//div[@id="edit-form-checkboxes-title-attribute"]');
|
770
|
$this->assertEqual($elements[0]['title'], 'Checkboxes test' . ' (' . t('Required') . ')', 'Title attribute found.');
|
771
|
$elements = $this->xpath('//div[@id="edit-form-radios-title-attribute"]');
|
772
|
$this->assertEqual($elements[0]['title'], 'Radios test' . ' (' . t('Required') . ')', 'Title attribute found.');
|
773
|
}
|
774
|
}
|
775
|
|
776
|
/**
|
777
|
* Test the tableselect form element for expected behavior.
|
778
|
*/
|
779
|
class FormsElementsTableSelectFunctionalTest extends DrupalWebTestCase {
|
780
|
|
781
|
public static function getInfo() {
|
782
|
return array(
|
783
|
'name' => 'Tableselect form element type test',
|
784
|
'description' => 'Test the tableselect element for expected behavior',
|
785
|
'group' => 'Form API',
|
786
|
);
|
787
|
}
|
788
|
|
789
|
function setUp() {
|
790
|
parent::setUp('form_test');
|
791
|
}
|
792
|
|
793
|
|
794
|
/**
|
795
|
* Test the display of checkboxes when #multiple is TRUE.
|
796
|
*/
|
797
|
function testMultipleTrue() {
|
798
|
|
799
|
$this->drupalGet('form_test/tableselect/multiple-true');
|
800
|
|
801
|
$this->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
|
802
|
|
803
|
// Test for the presence of the Select all rows tableheader.
|
804
|
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Presence of the "Select all" checkbox.');
|
805
|
|
806
|
$rows = array('row1', 'row2', 'row3');
|
807
|
foreach ($rows as $row) {
|
808
|
$this->assertFieldByXPath('//input[@type="checkbox"]', $row, format_string('Checkbox for value @row.', array('@row' => $row)));
|
809
|
}
|
810
|
}
|
811
|
|
812
|
/**
|
813
|
* Test the display of radios when #multiple is FALSE.
|
814
|
*/
|
815
|
function testMultipleFalse() {
|
816
|
$this->drupalGet('form_test/tableselect/multiple-false');
|
817
|
|
818
|
$this->assertNoText(t('Empty text.'), 'Empty text should not be displayed.');
|
819
|
|
820
|
// Test for the absence of the Select all rows tableheader.
|
821
|
$this->assertNoFieldByXPath('//th[@class="select-all"]', '', 'Absence of the "Select all" checkbox.');
|
822
|
|
823
|
$rows = array('row1', 'row2', 'row3');
|
824
|
foreach ($rows as $row) {
|
825
|
$this->assertFieldByXPath('//input[@type="radio"]', $row, format_string('Radio button for value @row.', array('@row' => $row)));
|
826
|
}
|
827
|
}
|
828
|
|
829
|
/**
|
830
|
* Test the display of the #empty text when #options is an empty array.
|
831
|
*/
|
832
|
function testEmptyText() {
|
833
|
$this->drupalGet('form_test/tableselect/empty-text');
|
834
|
$this->assertText(t('Empty text.'), 'Empty text should be displayed.');
|
835
|
}
|
836
|
|
837
|
/**
|
838
|
* Test the submission of single and multiple values when #multiple is TRUE.
|
839
|
*/
|
840
|
function testMultipleTrueSubmit() {
|
841
|
|
842
|
// Test a submission with one checkbox checked.
|
843
|
$edit = array();
|
844
|
$edit['tableselect[row1]'] = TRUE;
|
845
|
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
|
846
|
|
847
|
$this->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1');
|
848
|
$this->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
|
849
|
$this->assertText(t('Submitted: row3 = 0'), 'Unchecked checkbox row3.');
|
850
|
|
851
|
// Test a submission with multiple checkboxes checked.
|
852
|
$edit['tableselect[row1]'] = TRUE;
|
853
|
$edit['tableselect[row3]'] = TRUE;
|
854
|
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
|
855
|
|
856
|
$this->assertText(t('Submitted: row1 = row1'), 'Checked checkbox row1.');
|
857
|
$this->assertText(t('Submitted: row2 = 0'), 'Unchecked checkbox row2.');
|
858
|
$this->assertText(t('Submitted: row3 = row3'), 'Checked checkbox row3.');
|
859
|
|
860
|
}
|
861
|
|
862
|
/**
|
863
|
* Test submission of values when #multiple is FALSE.
|
864
|
*/
|
865
|
function testMultipleFalseSubmit() {
|
866
|
$edit['tableselect'] = 'row1';
|
867
|
$this->drupalPost('form_test/tableselect/multiple-false', $edit, 'Submit');
|
868
|
$this->assertText(t('Submitted: row1'), 'Selected radio button');
|
869
|
}
|
870
|
|
871
|
/**
|
872
|
* Test the #js_select property.
|
873
|
*/
|
874
|
function testAdvancedSelect() {
|
875
|
// When #multiple = TRUE a Select all checkbox should be displayed by default.
|
876
|
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
|
877
|
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, 'Display a "Select all" checkbox by default when #multiple is TRUE.');
|
878
|
|
879
|
// When #js_select is set to FALSE, a "Select all" checkbox should not be displayed.
|
880
|
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
|
881
|
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #js_select is FALSE.');
|
882
|
|
883
|
// A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select.
|
884
|
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
|
885
|
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE.');
|
886
|
|
887
|
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
|
888
|
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, 'Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.');
|
889
|
}
|
890
|
|
891
|
|
892
|
/**
|
893
|
* Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
|
894
|
*/
|
895
|
function testMultipleTrueOptionchecker() {
|
896
|
|
897
|
list($header, $options) = _form_test_tableselect_get_data();
|
898
|
|
899
|
$form['tableselect'] = array(
|
900
|
'#type' => 'tableselect',
|
901
|
'#header' => $header,
|
902
|
'#options' => $options,
|
903
|
);
|
904
|
|
905
|
// Test with a valid value.
|
906
|
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => array('row1' => 'row1')));
|
907
|
$this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for checkboxes.');
|
908
|
|
909
|
// Test with an invalid value.
|
910
|
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => array('non_existing_value' => 'non_existing_value')));
|
911
|
$this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for checkboxes.');
|
912
|
|
913
|
}
|
914
|
|
915
|
|
916
|
/**
|
917
|
* Test the whether the option checker gives an error on invalid tableselect values for radios.
|
918
|
*/
|
919
|
function testMultipleFalseOptionchecker() {
|
920
|
|
921
|
list($header, $options) = _form_test_tableselect_get_data();
|
922
|
|
923
|
$form['tableselect'] = array(
|
924
|
'#type' => 'tableselect',
|
925
|
'#header' => $header,
|
926
|
'#options' => $options,
|
927
|
'#multiple' => FALSE,
|
928
|
);
|
929
|
|
930
|
// Test with a valid value.
|
931
|
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1'));
|
932
|
$this->assertFalse(isset($errors['tableselect']), 'Option checker allows valid values for radio buttons.');
|
933
|
|
934
|
// Test with an invalid value.
|
935
|
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value'));
|
936
|
$this->assertTrue(isset($errors['tableselect']), 'Option checker disallows invalid values for radio buttons.');
|
937
|
}
|
938
|
|
939
|
|
940
|
/**
|
941
|
* Helper function for the option check test to submit a form while collecting errors.
|
942
|
*
|
943
|
* @param $form_element
|
944
|
* A form element to test.
|
945
|
* @param $edit
|
946
|
* An array containing post data.
|
947
|
*
|
948
|
* @return
|
949
|
* An array containing the processed form, the form_state and any errors.
|
950
|
*/
|
951
|
private function formSubmitHelper($form, $edit) {
|
952
|
$form_id = $this->randomName();
|
953
|
$form_state = form_state_defaults();
|
954
|
|
955
|
$form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
|
956
|
|
957
|
$form_state['input'] = $edit;
|
958
|
$form_state['input']['form_id'] = $form_id;
|
959
|
|
960
|
// The form token CSRF protection should not interfere with this test,
|
961
|
// so we bypass it by marking this test form as programmed.
|
962
|
$form_state['programmed'] = TRUE;
|
963
|
|
964
|
drupal_prepare_form($form_id, $form, $form_state);
|
965
|
|
966
|
drupal_process_form($form_id, $form, $form_state);
|
967
|
|
968
|
$errors = form_get_errors();
|
969
|
|
970
|
// Clear errors and messages.
|
971
|
drupal_get_messages();
|
972
|
form_clear_error();
|
973
|
|
974
|
// Return the processed form together with form_state and errors
|
975
|
// to allow the caller lowlevel access to the form.
|
976
|
return array($form, $form_state, $errors);
|
977
|
}
|
978
|
|
979
|
}
|
980
|
|
981
|
/**
|
982
|
* Test the vertical_tabs form element for expected behavior.
|
983
|
*/
|
984
|
class FormsElementsVerticalTabsFunctionalTest extends DrupalWebTestCase {
|
985
|
|
986
|
public static function getInfo() {
|
987
|
return array(
|
988
|
'name' => 'Vertical tabs form element type test',
|
989
|
'description' => 'Test the vertical_tabs element for expected behavior',
|
990
|
'group' => 'Form API',
|
991
|
);
|
992
|
}
|
993
|
|
994
|
function setUp() {
|
995
|
parent::setUp('form_test');
|
996
|
}
|
997
|
|
998
|
/**
|
999
|
* Ensures that vertical-tabs.js is included before collapse.js.
|
1000
|
*
|
1001
|
* Otherwise, collapse.js adds "SHOW" or "HIDE" labels to the tabs.
|
1002
|
*/
|
1003
|
function testJavaScriptOrdering() {
|
1004
|
$this->drupalGet('form_test/vertical-tabs');
|
1005
|
$position1 = strpos($this->content, 'misc/vertical-tabs.js');
|
1006
|
$position2 = strpos($this->content, 'misc/collapse.js');
|
1007
|
$this->assertTrue($position1 !== FALSE && $position2 !== FALSE && $position1 < $position2, 'vertical-tabs.js is included before collapse.js');
|
1008
|
}
|
1009
|
}
|
1010
|
|
1011
|
/**
|
1012
|
* Test the form storage on a multistep form.
|
1013
|
*
|
1014
|
* The tested form puts data into the storage during the initial form
|
1015
|
* construction. These tests verify that there are no duplicate form
|
1016
|
* constructions, with and without manual form caching activiated. Furthermore
|
1017
|
* when a validation error occurs, it makes sure that changed form element
|
1018
|
* values aren't lost due to a wrong form rebuild.
|
1019
|
*/
|
1020
|
class FormsFormStorageTestCase extends DrupalWebTestCase {
|
1021
|
|
1022
|
public static function getInfo() {
|
1023
|
return array(
|
1024
|
'name' => 'Multistep form using form storage',
|
1025
|
'description' => 'Tests a multistep form using form storage and makes sure validation and caching works right.',
|
1026
|
'group' => 'Form API',
|
1027
|
);
|
1028
|
}
|
1029
|
|
1030
|
function setUp() {
|
1031
|
parent::setUp('form_test');
|
1032
|
|
1033
|
$this->web_user = $this->drupalCreateUser(array('access content'));
|
1034
|
$this->drupalLogin($this->web_user);
|
1035
|
}
|
1036
|
|
1037
|
/**
|
1038
|
* Tests using the form in a usual way.
|
1039
|
*/
|
1040
|
function testForm() {
|
1041
|
$this->drupalGet('form_test/form-storage');
|
1042
|
$this->assertText('Form constructions: 1');
|
1043
|
|
1044
|
$edit = array('title' => 'new', 'value' => 'value_is_set');
|
1045
|
|
1046
|
// Use form rebuilding triggered by a submit button.
|
1047
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1048
|
$this->assertText('Form constructions: 2');
|
1049
|
$this->assertText('Form constructions: 3');
|
1050
|
|
1051
|
// Reset the form to the values of the storage, using a form rebuild
|
1052
|
// triggered by button of type button.
|
1053
|
$this->drupalPost(NULL, array('title' => 'changed'), 'Reset');
|
1054
|
$this->assertFieldByName('title', 'new', 'Values have been resetted.');
|
1055
|
// After rebuilding, the form has been cached.
|
1056
|
$this->assertText('Form constructions: 4');
|
1057
|
|
1058
|
$this->drupalPost(NULL, $edit, 'Save');
|
1059
|
$this->assertText('Form constructions: 4');
|
1060
|
$this->assertText('Title: new', 'The form storage has stored the values.');
|
1061
|
}
|
1062
|
|
1063
|
/**
|
1064
|
* Tests using the form with an activated $form_state['cache'] property.
|
1065
|
*/
|
1066
|
function testFormCached() {
|
1067
|
$this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1)));
|
1068
|
$this->assertText('Form constructions: 1');
|
1069
|
|
1070
|
$edit = array('title' => 'new', 'value' => 'value_is_set');
|
1071
|
|
1072
|
// Use form rebuilding triggered by a submit button.
|
1073
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1074
|
$this->assertText('Form constructions: 2');
|
1075
|
|
1076
|
// Reset the form to the values of the storage, using a form rebuild
|
1077
|
// triggered by button of type button.
|
1078
|
$this->drupalPost(NULL, array('title' => 'changed'), 'Reset');
|
1079
|
$this->assertFieldByName('title', 'new', 'Values have been resetted.');
|
1080
|
$this->assertText('Form constructions: 3');
|
1081
|
|
1082
|
$this->drupalPost(NULL, $edit, 'Save');
|
1083
|
$this->assertText('Form constructions: 3');
|
1084
|
$this->assertText('Title: new', 'The form storage has stored the values.');
|
1085
|
}
|
1086
|
|
1087
|
/**
|
1088
|
* Tests validation when form storage is used.
|
1089
|
*/
|
1090
|
function testValidation() {
|
1091
|
$this->drupalPost('form_test/form-storage', array('title' => '', 'value' => 'value_is_set'), 'Continue submit');
|
1092
|
$this->assertPattern('/value_is_set/', 'The input values have been kept.');
|
1093
|
}
|
1094
|
|
1095
|
/**
|
1096
|
* Tests updating cached form storage during form validation.
|
1097
|
*
|
1098
|
* If form caching is enabled and a form stores data in the form storage, then
|
1099
|
* the form storage also has to be updated in case of a validation error in
|
1100
|
* the form. This test re-uses the existing form for multi-step tests, but
|
1101
|
* triggers a special #element_validate handler to update the form storage
|
1102
|
* during form validation, while another, required element in the form
|
1103
|
* triggers a form validation error.
|
1104
|
*/
|
1105
|
function testCachedFormStorageValidation() {
|
1106
|
// Request the form with 'cache' query parameter to enable form caching.
|
1107
|
$this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1)));
|
1108
|
|
1109
|
// Skip step 1 of the multi-step form, since the first step copies over
|
1110
|
// 'title' into form storage, but we want to verify that changes in the form
|
1111
|
// storage are updated in the cache during form validation.
|
1112
|
$edit = array('title' => 'foo');
|
1113
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1114
|
|
1115
|
// In step 2, trigger a validation error for the required 'title' field, and
|
1116
|
// post the special 'change_title' value for the 'value' field, which
|
1117
|
// conditionally invokes the #element_validate handler to update the form
|
1118
|
// storage.
|
1119
|
$edit = array('title' => '', 'value' => 'change_title');
|
1120
|
$this->drupalPost(NULL, $edit, 'Save');
|
1121
|
|
1122
|
// At this point, the form storage should contain updated values, but we do
|
1123
|
// not see them, because the form has not been rebuilt yet due to the
|
1124
|
// validation error. Post again and verify that the rebuilt form contains
|
1125
|
// the values of the updated form storage.
|
1126
|
$this->drupalPost(NULL, array('title' => 'foo', 'value' => 'bar'), 'Save');
|
1127
|
$this->assertText("The thing has been changed.", 'The altered form storage value was updated in cache and taken over.');
|
1128
|
}
|
1129
|
|
1130
|
/**
|
1131
|
* Tests a form using form state without using 'storage' to pass data from the
|
1132
|
* constructor to a submit handler. The data has to persist even when caching
|
1133
|
* gets activated, what may happen when a modules alter the form and adds
|
1134
|
* #ajax properties.
|
1135
|
*/
|
1136
|
function testFormStatePersist() {
|
1137
|
// Test the form one time with caching activated and one time without.
|
1138
|
$run_options = array(
|
1139
|
array(),
|
1140
|
array('query' => array('cache' => 1)),
|
1141
|
);
|
1142
|
foreach ($run_options as $options) {
|
1143
|
$this->drupalPost('form-test/state-persist', array(), t('Submit'), $options);
|
1144
|
// The submit handler outputs the value in $form_state, assert it's there.
|
1145
|
$this->assertText('State persisted.');
|
1146
|
|
1147
|
// Test it again, but first trigger a validation error, then test.
|
1148
|
$this->drupalPost('form-test/state-persist', array('title' => ''), t('Submit'), $options);
|
1149
|
$this->assertText(t('!name field is required.', array('!name' => 'title')));
|
1150
|
// Submit the form again triggering no validation error.
|
1151
|
$this->drupalPost(NULL, array('title' => 'foo'), t('Submit'), $options);
|
1152
|
$this->assertText('State persisted.');
|
1153
|
|
1154
|
// Now post to the rebuilt form and verify it's still there afterwards.
|
1155
|
$this->drupalPost(NULL, array('title' => 'bar'), t('Submit'), $options);
|
1156
|
$this->assertText('State persisted.');
|
1157
|
}
|
1158
|
}
|
1159
|
|
1160
|
/**
|
1161
|
* Verify that the form build-id remains the same when validation errors
|
1162
|
* occur on a mutable form.
|
1163
|
*/
|
1164
|
function testMutableForm() {
|
1165
|
// Request the form with 'cache' query parameter to enable form caching.
|
1166
|
$this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1)));
|
1167
|
$buildIdFields = $this->xpath('//input[@name="form_build_id"]');
|
1168
|
$this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
|
1169
|
$buildId = (string) $buildIdFields[0]['value'];
|
1170
|
|
1171
|
// Trigger validation error by submitting an empty title.
|
1172
|
$edit = array('title' => '');
|
1173
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1174
|
|
1175
|
// Verify that the build-id did not change.
|
1176
|
$this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails');
|
1177
|
}
|
1178
|
|
1179
|
/**
|
1180
|
* Verifies that form build-id is regenerated when loading an immutable form
|
1181
|
* from the cache.
|
1182
|
*/
|
1183
|
function testImmutableForm() {
|
1184
|
// Request the form with 'cache' query parameter to enable form caching.
|
1185
|
$this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1, 'immutable' => 1)));
|
1186
|
$buildIdFields = $this->xpath('//input[@name="form_build_id"]');
|
1187
|
$this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
|
1188
|
$buildId = (string) $buildIdFields[0]['value'];
|
1189
|
|
1190
|
// Trigger validation error by submitting an empty title.
|
1191
|
$edit = array('title' => '');
|
1192
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1193
|
|
1194
|
// Verify that the build-id did change.
|
1195
|
$this->assertNoFieldByName('form_build_id', $buildId, 'Build id changes when form validation fails');
|
1196
|
|
1197
|
// Retrieve the new build-id.
|
1198
|
$buildIdFields = $this->xpath('//input[@name="form_build_id"]');
|
1199
|
$this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
|
1200
|
$buildId = (string) $buildIdFields[0]['value'];
|
1201
|
|
1202
|
// Trigger validation error by again submitting an empty title.
|
1203
|
$edit = array('title' => '');
|
1204
|
$this->drupalPost(NULL, $edit, 'Continue submit');
|
1205
|
|
1206
|
// Verify that the build-id does not change the second time.
|
1207
|
$this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails subsequently');
|
1208
|
}
|
1209
|
|
1210
|
/**
|
1211
|
* Verify that existing contrib code cannot overwrite immutable form state.
|
1212
|
*/
|
1213
|
public function testImmutableFormLegacyProtection() {
|
1214
|
$this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1, 'immutable' => 1)));
|
1215
|
$build_id_fields = $this->xpath('//input[@name="form_build_id"]');
|
1216
|
$this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
|
1217
|
$build_id = (string) $build_id_fields[0]['value'];
|
1218
|
|
1219
|
// Try to poison the form cache.
|
1220
|
$original = $this->drupalGetAJAX('form_test/form-storage-legacy/' . $build_id);
|
1221
|
$this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
|
1222
|
$this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
|
1223
|
|
1224
|
// Assert that a watchdog message was logged by form_set_cache.
|
1225
|
$status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, array(':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.'));
|
1226
|
$this->assert($status, 'A watchdog message was logged by form_set_cache');
|
1227
|
|
1228
|
// Ensure that the form state was not poisoned by the preceeding call.
|
1229
|
$original = $this->drupalGetAJAX('form_test/form-storage-legacy/' . $build_id);
|
1230
|
$this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
|
1231
|
$this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
|
1232
|
$this->assert(empty($original['form']['#poisoned']), 'Original form structure was preserved');
|
1233
|
$this->assert(empty($original['form_state']['poisoned']), 'Original form state was preserved');
|
1234
|
}
|
1235
|
}
|
1236
|
|
1237
|
/**
|
1238
|
* Test the form storage when page caching for anonymous users is turned on.
|
1239
|
*/
|
1240
|
class FormsFormStoragePageCacheTestCase extends DrupalWebTestCase {
|
1241
|
protected $profile = 'testing';
|
1242
|
|
1243
|
public static function getInfo() {
|
1244
|
return array(
|
1245
|
'name' => 'Forms using form storage on cached pages',
|
1246
|
'description' => 'Tests a form using form storage and makes sure validation and caching works when page caching for anonymous users is turned on.',
|
1247
|
'group' => 'Form API',
|
1248
|
);
|
1249
|
}
|
1250
|
|
1251
|
public function setUp() {
|
1252
|
parent::setUp('form_test');
|
1253
|
|
1254
|
variable_set('cache', TRUE);
|
1255
|
}
|
1256
|
|
1257
|
/**
|
1258
|
* Return the build id of the current form.
|
1259
|
*/
|
1260
|
protected function getFormBuildId() {
|
1261
|
$build_id_fields = $this->xpath('//input[@name="form_build_id"]');
|
1262
|
$this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
|
1263
|
return (string) $build_id_fields[0]['value'];
|
1264
|
}
|
1265
|
|
1266
|
/**
|
1267
|
* Build-id is regenerated when validating cached form.
|
1268
|
*/
|
1269
|
public function testValidateFormStorageOnCachedPage() {
|
1270
|
$this->drupalGet('form_test/form-storage-page-cache');
|
1271
|
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
|
1272
|
$this->assertText('No old build id', 'No old build id on the page');
|
1273
|
$build_id_initial = $this->getFormBuildId();
|
1274
|
|
1275
|
// Trigger validation error by submitting an empty title.
|
1276
|
$edit = array('title' => '');
|
1277
|
$this->drupalPost(NULL, $edit, 'Save');
|
1278
|
$this->assertText($build_id_initial, 'Old build id on the page');
|
1279
|
$build_id_first_validation = $this->getFormBuildId();
|
1280
|
$this->assertNotEqual($build_id_initial, $build_id_first_validation, 'Build id changes when form validation fails');
|
1281
|
|
1282
|
// Trigger validation error by again submitting an empty title.
|
1283
|
$edit = array('title' => '');
|
1284
|
$this->drupalPost(NULL, $edit, 'Save');
|
1285
|
$this->assertText('No old build id', 'No old build id on the page');
|
1286
|
$build_id_second_validation = $this->getFormBuildId();
|
1287
|
$this->assertEqual($build_id_first_validation, $build_id_second_validation, 'Build id remains the same when form validation fails subsequently');
|
1288
|
|
1289
|
// Repeat the test sequence but this time with a page loaded from the cache.
|
1290
|
$this->drupalGet('form_test/form-storage-page-cache');
|
1291
|
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
|
1292
|
$this->assertText('No old build id', 'No old build id on the page');
|
1293
|
$build_id_from_cache_initial = $this->getFormBuildId();
|
1294
|
$this->assertEqual($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
|
1295
|
|
1296
|
// Trigger validation error by submitting an empty title.
|
1297
|
$edit = array('title' => '');
|
1298
|
$this->drupalPost(NULL, $edit, 'Save');
|
1299
|
$this->assertText($build_id_initial, 'Old build id is initial build id');
|
1300
|
$build_id_from_cache_first_validation = $this->getFormBuildId();
|
1301
|
$this->assertNotEqual($build_id_initial, $build_id_from_cache_first_validation, 'Build id changes when form validation fails');
|
1302
|
$this->assertNotEqual($build_id_first_validation, $build_id_from_cache_first_validation, 'Build id from first user is not reused');
|
1303
|
|
1304
|
// Trigger validation error by again submitting an empty title.
|
1305
|
$edit = array('title' => '');
|
1306
|
$this->drupalPost(NULL, $edit, 'Save');
|
1307
|
$this->assertText('No old build id', 'No old build id on the page');
|
1308
|
$build_id_from_cache_second_validation = $this->getFormBuildId();
|
1309
|
$this->assertEqual($build_id_from_cache_first_validation, $build_id_from_cache_second_validation, 'Build id remains the same when form validation fails subsequently');
|
1310
|
}
|
1311
|
|
1312
|
/**
|
1313
|
* Build-id is regenerated when rebuilding cached form.
|
1314
|
*/
|
1315
|
public function testRebuildFormStorageOnCachedPage() {
|
1316
|
$this->drupalGet('form_test/form-storage-page-cache');
|
1317
|
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
|
1318
|
$this->assertText('No old build id', 'No old build id on the page');
|
1319
|
$build_id_initial = $this->getFormBuildId();
|
1320
|
|
1321
|
// Trigger rebuild, should regenerate build id.
|
1322
|
$edit = array('title' => 'something');
|
1323
|
$this->drupalPost(NULL, $edit, 'Rebuild');
|
1324
|
$this->assertText($build_id_initial, 'Initial build id as old build id on the page');
|
1325
|
$build_id_first_rebuild = $this->getFormBuildId();
|
1326
|
$this->assertNotEqual($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.');
|
1327
|
|
1328
|
// Trigger subsequent rebuild, should regenerate the build id again.
|
1329
|
$edit = array('title' => 'something');
|
1330
|
$this->drupalPost(NULL, $edit, 'Rebuild');
|
1331
|
$this->assertText($build_id_first_rebuild, 'First build id as old build id on the page');
|
1332
|
$build_id_second_rebuild = $this->getFormBuildId();
|
1333
|
$this->assertNotEqual($build_id_first_rebuild, $build_id_second_rebuild, 'Build id changes on second rebuild.');
|
1334
|
}
|
1335
|
}
|
1336
|
|
1337
|
/**
|
1338
|
* Test wrapper form callbacks.
|
1339
|
*/
|
1340
|
class FormsFormWrapperTestCase extends DrupalWebTestCase {
|
1341
|
public static function getInfo() {
|
1342
|
return array(
|
1343
|
'name' => 'Form wrapper callback',
|
1344
|
'description' => 'Tests form wrapper callbacks to pass a prebuilt form to form builder functions.',
|
1345
|
'group' => 'Form API',
|
1346
|
);
|
1347
|
}
|
1348
|
|
1349
|
function setUp() {
|
1350
|
parent::setUp('form_test');
|
1351
|
}
|
1352
|
|
1353
|
/**
|
1354
|
* Tests using the form in a usual way.
|
1355
|
*/
|
1356
|
function testWrapperCallback() {
|
1357
|
$this->drupalGet('form_test/wrapper-callback');
|
1358
|
$this->assertText('Form wrapper callback element output.', 'The form contains form wrapper elements.');
|
1359
|
$this->assertText('Form builder element output.', 'The form contains form builder elements.');
|
1360
|
}
|
1361
|
}
|
1362
|
|
1363
|
/**
|
1364
|
* Test $form_state clearance.
|
1365
|
*/
|
1366
|
class FormStateValuesCleanTestCase extends DrupalWebTestCase {
|
1367
|
public static function getInfo() {
|
1368
|
return array(
|
1369
|
'name' => 'Form state values clearance',
|
1370
|
'description' => 'Test proper removal of submitted form values using form_state_values_clean().',
|
1371
|
'group' => 'Form API',
|
1372
|
);
|
1373
|
}
|
1374
|
|
1375
|
function setUp() {
|
1376
|
parent::setUp('form_test');
|
1377
|
}
|
1378
|
|
1379
|
/**
|
1380
|
* Tests form_state_values_clean().
|
1381
|
*/
|
1382
|
function testFormStateValuesClean() {
|
1383
|
$values = drupal_json_decode($this->drupalPost('form_test/form-state-values-clean', array(), t('Submit')));
|
1384
|
|
1385
|
// Setup the expected result.
|
1386
|
$result = array(
|
1387
|
'beer' => 1000,
|
1388
|
'baz' => array('beer' => 2000),
|
1389
|
);
|
1390
|
|
1391
|
// Verify that all internal Form API elements were removed.
|
1392
|
$this->assertFalse(isset($values['form_id']), format_string('%element was removed.', array('%element' => 'form_id')));
|
1393
|
$this->assertFalse(isset($values['form_token']), format_string('%element was removed.', array('%element' => 'form_token')));
|
1394
|
$this->assertFalse(isset($values['form_build_id']), format_string('%element was removed.', array('%element' => 'form_build_id')));
|
1395
|
$this->assertFalse(isset($values['op']), format_string('%element was removed.', array('%element' => 'op')));
|
1396
|
|
1397
|
// Verify that all buttons were removed.
|
1398
|
$this->assertFalse(isset($values['foo']), format_string('%element was removed.', array('%element' => 'foo')));
|
1399
|
$this->assertFalse(isset($values['bar']), format_string('%element was removed.', array('%element' => 'bar')));
|
1400
|
$this->assertFalse(isset($values['baz']['foo']), format_string('%element was removed.', array('%element' => 'foo')));
|
1401
|
$this->assertFalse(isset($values['baz']['baz']), format_string('%element was removed.', array('%element' => 'baz')));
|
1402
|
|
1403
|
// Verify that nested form value still exists.
|
1404
|
$this->assertTrue(isset($values['baz']['beer']), 'Nested form value still exists.');
|
1405
|
|
1406
|
// Verify that actual form values equal resulting form values.
|
1407
|
$this->assertEqual($values, $result, 'Expected form values equal actual form values.');
|
1408
|
}
|
1409
|
}
|
1410
|
|
1411
|
/**
|
1412
|
* Tests $form_state clearance with form elements having buttons.
|
1413
|
*/
|
1414
|
class FormStateValuesCleanAdvancedTestCase extends DrupalWebTestCase {
|
1415
|
/**
|
1416
|
* An image file path for uploading.
|
1417
|
*/
|
1418
|
protected $image;
|
1419
|
|
1420
|
public static function getInfo() {
|
1421
|
return array(
|
1422
|
'name' => 'Form state values clearance (advanced)',
|
1423
|
'description' => 'Test proper removal of submitted form values using form_state_values_clean() when having forms with elements containing buttons like "managed_file".',
|
1424
|
'group' => 'Form API',
|
1425
|
);
|
1426
|
}
|
1427
|
|
1428
|
function setUp() {
|
1429
|
parent::setUp('form_test');
|
1430
|
}
|
1431
|
|
1432
|
/**
|
1433
|
* Tests form_state_values_clean().
|
1434
|
*/
|
1435
|
function testFormStateValuesCleanAdvanced() {
|
1436
|
|
1437
|
// Get an image for uploading.
|
1438
|
$image_files = $this->drupalGetTestFiles('image');
|
1439
|
$this->image = current($image_files);
|
1440
|
|
1441
|
// Check if the physical file is there.
|
1442
|
$this->assertTrue(is_file($this->image->uri), "The image file we're going to upload exists.");
|
1443
|
|
1444
|
// "Browse" for the desired file.
|
1445
|
$edit = array('files[image]' => drupal_realpath($this->image->uri));
|
1446
|
|
1447
|
// Post the form.
|
1448
|
$this->drupalPost('form_test/form-state-values-clean-advanced', $edit, t('Submit'));
|
1449
|
|
1450
|
// Expecting a 200 HTTP code.
|
1451
|
$this->assertResponse(200, 'Received a 200 response for posted test file.');
|
1452
|
$this->assertRaw(t('You WIN!'), 'Found the success message.');
|
1453
|
}
|
1454
|
}
|
1455
|
|
1456
|
/**
|
1457
|
* Tests form rebuilding.
|
1458
|
*
|
1459
|
* @todo Add tests for other aspects of form rebuilding.
|
1460
|
*/
|
1461
|
class FormsRebuildTestCase extends DrupalWebTestCase {
|
1462
|
public static function getInfo() {
|
1463
|
return array(
|
1464
|
'name' => 'Form rebuilding',
|
1465
|
'description' => 'Tests functionality of drupal_rebuild_form().',
|
1466
|
'group' => 'Form API',
|
1467
|
);
|
1468
|
}
|
1469
|
|
1470
|
function setUp() {
|
1471
|
parent::setUp('form_test');
|
1472
|
|
1473
|
$this->web_user = $this->drupalCreateUser(array('access content'));
|
1474
|
$this->drupalLogin($this->web_user);
|
1475
|
}
|
1476
|
|
1477
|
/**
|
1478
|
* Tests preservation of values.
|
1479
|
*/
|
1480
|
function testRebuildPreservesValues() {
|
1481
|
$edit = array(
|
1482
|
'checkbox_1_default_off' => TRUE,
|
1483
|
'checkbox_1_default_on' => FALSE,
|
1484
|
'text_1' => 'foo',
|
1485
|
);
|
1486
|
$this->drupalPost('form-test/form-rebuild-preserve-values', $edit, 'Add more');
|
1487
|
|
1488
|
// Verify that initial elements retained their submitted values.
|
1489
|
$this->assertFieldChecked('edit-checkbox-1-default-off', 'A submitted checked checkbox retained its checked state during a rebuild.');
|
1490
|
$this->assertNoFieldChecked('edit-checkbox-1-default-on', 'A submitted unchecked checkbox retained its unchecked state during a rebuild.');
|
1491
|
$this->assertFieldById('edit-text-1', 'foo', 'A textfield retained its submitted value during a rebuild.');
|
1492
|
|
1493
|
// Verify that newly added elements were initialized with their default values.
|
1494
|
$this->assertFieldChecked('edit-checkbox-2-default-on', 'A newly added checkbox was initialized with a default checked state.');
|
1495
|
$this->assertNoFieldChecked('edit-checkbox-2-default-off', 'A newly added checkbox was initialized with a default unchecked state.');
|
1496
|
$this->assertFieldById('edit-text-2', 'DEFAULT 2', 'A newly added textfield was initialized with its default value.');
|
1497
|
}
|
1498
|
|
1499
|
/**
|
1500
|
* Tests that a form's action is retained after an Ajax submission.
|
1501
|
*
|
1502
|
* The 'action' attribute of a form should not change after an Ajax submission
|
1503
|
* followed by a non-Ajax submission, which triggers a validation error.
|
1504
|
*/
|
1505
|
function testPreserveFormActionAfterAJAX() {
|
1506
|
// Create a multi-valued field for 'page' nodes to use for Ajax testing.
|
1507
|
$field_name = 'field_ajax_test';
|
1508
|
$field = array(
|
1509
|
'field_name' => $field_name,
|
1510
|
'type' => 'text',
|
1511
|
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
|
1512
|
);
|
1513
|
field_create_field($field);
|
1514
|
$instance = array(
|
1515
|
'field_name' => $field_name,
|
1516
|
'entity_type' => 'node',
|
1517
|
'bundle' => 'page',
|
1518
|
);
|
1519
|
field_create_instance($instance);
|
1520
|
|
1521
|
// Log in a user who can create 'page' nodes.
|
1522
|
$this->web_user = $this->drupalCreateUser(array('create page content'));
|
1523
|
$this->drupalLogin($this->web_user);
|
1524
|
|
1525
|
// Get the form for adding a 'page' node. Submit an "add another item" Ajax
|
1526
|
// submission and verify it worked by ensuring the updated page has two text
|
1527
|
// field items in the field for which we just added an item.
|
1528
|
$this->drupalGet('node/add/page');
|
1529
|
$this->drupalPostAJAX(NULL, array(), array('field_ajax_test_add_more' => t('Add another item')), 'system/ajax', array(), array(), 'page-node-form');
|
1530
|
$this->assert(count($this->xpath('//div[contains(@class, "field-name-field-ajax-test")]//input[@type="text"]')) == 2, 'AJAX submission succeeded.');
|
1531
|
|
1532
|
// Submit the form with the non-Ajax "Save" button, leaving the title field
|
1533
|
// blank to trigger a validation error, and ensure that a validation error
|
1534
|
// occurred, because this test is for testing what happens when a form is
|
1535
|
// re-rendered without being re-built, which is what happens when there's
|
1536
|
// a validation error.
|
1537
|
$this->drupalPost(NULL, array(), t('Save'));
|
1538
|
$this->assertText('Title field is required.', 'Non-AJAX submission correctly triggered a validation error.');
|
1539
|
|
1540
|
// Ensure that the form contains two items in the multi-valued field, so we
|
1541
|
// know we're testing a form that was correctly retrieved from cache.
|
1542
|
$this->assert(count($this->xpath('//form[contains(@id, "page-node-form")]//div[contains(@class, "form-item-field-ajax-test")]//input[@type="text"]')) == 2, 'Form retained its state from cache.');
|
1543
|
|
1544
|
// Ensure that the form's action is correct.
|
1545
|
$forms = $this->xpath('//form[contains(@class, "node-page-form")]');
|
1546
|
$this->assert(count($forms) == 1 && $forms[0]['action'] == url('node/add/page'), 'Re-rendered form contains the correct action value.');
|
1547
|
}
|
1548
|
}
|
1549
|
|
1550
|
/**
|
1551
|
* Tests form redirection.
|
1552
|
*/
|
1553
|
class FormsRedirectTestCase extends DrupalWebTestCase {
|
1554
|
|
1555
|
public static function getInfo() {
|
1556
|
return array(
|
1557
|
'name' => 'Form redirecting',
|
1558
|
'description' => 'Tests functionality of drupal_redirect_form().',
|
1559
|
'group' => 'Form API',
|
1560
|
);
|
1561
|
}
|
1562
|
|
1563
|
function setUp() {
|
1564
|
parent::setUp(array('form_test'));
|
1565
|
}
|
1566
|
|
1567
|
/**
|
1568
|
* Tests form redirection.
|
1569
|
*/
|
1570
|
function testRedirect() {
|
1571
|
$path = 'form-test/redirect';
|
1572
|
$options = array('query' => array('foo' => 'bar'));
|
1573
|
$options['absolute'] = TRUE;
|
1574
|
|
1575
|
// Test basic redirection.
|
1576
|
$edit = array(
|
1577
|
'redirection' => TRUE,
|
1578
|
'destination' => $this->randomName(),
|
1579
|
);
|
1580
|
$this->drupalPost($path, $edit, t('Submit'));
|
1581
|
$this->assertUrl($edit['destination'], array(), 'Basic redirection works.');
|
1582
|
|
1583
|
|
1584
|
// Test without redirection.
|
1585
|
$edit = array(
|
1586
|
'redirection' => FALSE,
|
1587
|
);
|
1588
|
$this->drupalPost($path, $edit, t('Submit'));
|
1589
|
$this->assertUrl($path, array(), 'When redirect is set to FALSE, there should be no redirection.');
|
1590
|
|
1591
|
// Test redirection with query parameters.
|
1592
|
$edit = array(
|
1593
|
'redirection' => TRUE,
|
1594
|
'destination' => $this->randomName(),
|
1595
|
);
|
1596
|
$this->drupalPost($path, $edit, t('Submit'), $options);
|
1597
|
$this->assertUrl($edit['destination'], array(), 'Redirection with query parameters works.');
|
1598
|
|
1599
|
// Test without redirection but with query parameters.
|
1600
|
$edit = array(
|
1601
|
'redirection' => FALSE,
|
1602
|
);
|
1603
|
$this->drupalPost($path, $edit, t('Submit'), $options);
|
1604
|
$this->assertUrl($path, $options, 'When redirect is set to FALSE, there should be no redirection, and the query parameters should be passed along.');
|
1605
|
|
1606
|
// Test redirection back to the original path.
|
1607
|
$edit = array(
|
1608
|
'redirection' => TRUE,
|
1609
|
'destination' => '',
|
1610
|
);
|
1611
|
$this->drupalPost($path, $edit, t('Submit'));
|
1612
|
$this->assertUrl($path, array(), 'When using an empty redirection string, there should be no redirection.');
|
1613
|
|
1614
|
// Test redirection back to the original path with query parameters.
|
1615
|
$edit = array(
|
1616
|
'redirection' => TRUE,
|
1617
|
'destination' => '',
|
1618
|
);
|
1619
|
$this->drupalPost($path, $edit, t('Submit'), $options);
|
1620
|
$this->assertUrl($path, $options, 'When using an empty redirection string, there should be no redirection, and the query parameters should be passed along.');
|
1621
|
}
|
1622
|
|
1623
|
}
|
1624
|
|
1625
|
/**
|
1626
|
* Test the programmatic form submission behavior.
|
1627
|
*/
|
1628
|
class FormsProgrammaticTestCase extends DrupalWebTestCase {
|
1629
|
|
1630
|
public static function getInfo() {
|
1631
|
return array(
|
1632
|
'name' => 'Programmatic form submissions',
|
1633
|
'description' => 'Test the programmatic form submission behavior.',
|
1634
|
'group' => 'Form API',
|
1635
|
);
|
1636
|
}
|
1637
|
|
1638
|
function setUp() {
|
1639
|
parent::setUp('form_test');
|
1640
|
}
|
1641
|
|
1642
|
/**
|
1643
|
* Test the programmatic form submission workflow.
|
1644
|
*/
|
1645
|
function testSubmissionWorkflow() {
|
1646
|
// Backup the current batch status and reset it to avoid conflicts while
|
1647
|
// processing the dummy form submit handler.
|
1648
|
$current_batch = $batch =& batch_get();
|
1649
|
$batch = array();
|
1650
|
|
1651
|
// Test that a programmatic form submission is rejected when a required
|
1652
|
// textfield is omitted and correctly processed when it is provided.
|
1653
|
$this->submitForm(array(), FALSE);
|
1654
|
$this->submitForm(array('textfield' => 'test 1'), TRUE);
|
1655
|
$this->submitForm(array(), FALSE);
|
1656
|
$this->submitForm(array('textfield' => 'test 2'), TRUE);
|
1657
|
|
1658
|
// Test that a programmatic form submission can turn on and off checkboxes
|
1659
|
// which are, by default, checked.
|
1660
|
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => 1, 2 => 2)), TRUE);
|
1661
|
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => 1, 2 => NULL)), TRUE);
|
1662
|
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => 2)), TRUE);
|
1663
|
$this->submitForm(array('textfield' => 'dummy value', 'checkboxes' => array(1 => NULL, 2 => NULL)), TRUE);
|
1664
|
|
1665
|
// Test that a programmatic form submission can successfully submit values
|
1666
|
// even for fields where the #access property is FALSE.
|
1667
|
$this->submitForm(array('textfield' => 'dummy value', 'textfield_no_access' => 'test value'), TRUE);
|
1668
|
// Test that #access is respected for programmatic form submissions when
|
1669
|
// requested to do so.
|
1670
|
$submitted_values = array('textfield' => 'dummy value', 'textfield_no_access' => 'test value');
|
1671
|
$expected_values = array('textfield' => 'dummy value', 'textfield_no_access' => 'default value');
|
1672
|
$form_state = array('programmed_bypass_access_check' => FALSE);
|
1673
|
$this->submitForm($submitted_values, TRUE, $expected_values, $form_state);
|
1674
|
|
1675
|
// Test that a programmatic form submission can correctly click a button
|
1676
|
// that limits validation errors based on user input. Since we do not
|
1677
|
// submit any values for "textfield" here and the textfield is required, we
|
1678
|
// only expect form validation to pass when validation is limited to a
|
1679
|
// different field.
|
1680
|
$this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'all'), FALSE);
|
1681
|
$this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'textfield'), FALSE);
|
1682
|
$this->submitForm(array('op' => 'Submit with limited validation', 'field_to_validate' => 'field_to_validate'), TRUE);
|
1683
|
|
1684
|
// Restore the current batch status.
|
1685
|
$batch = $current_batch;
|
1686
|
}
|
1687
|
|
1688
|
/**
|
1689
|
* Helper function used to programmatically submit the form defined in
|
1690
|
* form_test.module with the given values.
|
1691
|
*
|
1692
|
* @param $values
|
1693
|
* An array of field values to be submitted.
|
1694
|
* @param $valid_input
|
1695
|
* A boolean indicating whether or not the form submission is expected to
|
1696
|
* be valid.
|
1697
|
* @param $expected_values
|
1698
|
* (Optional) An array of field values that are expected to be stored by
|
1699
|
* the form submit handler. If not set, the submitted $values are assumed
|
1700
|
* to also be the expected stored values.
|
1701
|
* @param $form_state
|
1702
|
* (Optional) A keyed array containing the state of the form, to be sent in
|
1703
|
* the call to drupal_form_submit(). The $values parameter is added to
|
1704
|
* $form_state['values'] by default, if it is not already set.
|
1705
|
*/
|
1706
|
private function submitForm($values, $valid_input, $expected_values = NULL, $form_state = array()) {
|
1707
|
// Programmatically submit the given values.
|
1708
|
$form_state += array('values' => $values);
|
1709
|
drupal_form_submit('form_test_programmatic_form', $form_state);
|
1710
|
|
1711
|
// Check that the form returns an error when expected, and vice versa.
|
1712
|
$errors = form_get_errors();
|
1713
|
$valid_form = empty($errors);
|
1714
|
$args = array(
|
1715
|
'%values' => print_r($values, TRUE),
|
1716
|
'%errors' => $valid_form ? t('None') : implode(' ', $errors),
|
1717
|
);
|
1718
|
$this->assertTrue($valid_input == $valid_form, format_string('Input values: %values<br/>Validation handler errors: %errors', $args));
|
1719
|
|
1720
|
// We check submitted values only if we have a valid input.
|
1721
|
if ($valid_input) {
|
1722
|
// By fetching the values from $form_state['storage'] we ensure that the
|
1723
|
// submission handler was properly executed.
|
1724
|
$stored_values = $form_state['storage']['programmatic_form_submit'];
|
1725
|
if (!isset($expected_values)) {
|
1726
|
$expected_values = $values;
|
1727
|
}
|
1728
|
foreach ($expected_values as $key => $value) {
|
1729
|
$this->assertTrue(isset($stored_values[$key]) && $stored_values[$key] == $value, format_string('Submission handler correctly executed: %stored_key is %stored_value', array('%stored_key' => $key, '%stored_value' => print_r($value, TRUE))));
|
1730
|
}
|
1731
|
}
|
1732
|
}
|
1733
|
}
|
1734
|
|
1735
|
/**
|
1736
|
* Test that FAPI correctly determines $form_state['triggering_element'].
|
1737
|
*/
|
1738
|
class FormsTriggeringElementTestCase extends DrupalWebTestCase {
|
1739
|
|
1740
|
public static function getInfo() {
|
1741
|
return array(
|
1742
|
'name' => 'Form triggering element determination',
|
1743
|
'description' => 'Test the determination of $form_state[\'triggering_element\'].',
|
1744
|
'group' => 'Form API',
|
1745
|
);
|
1746
|
}
|
1747
|
|
1748
|
function setUp() {
|
1749
|
parent::setUp('form_test');
|
1750
|
}
|
1751
|
|
1752
|
/**
|
1753
|
* Test the determination of $form_state['triggering_element'] when no button
|
1754
|
* information is included in the POST data, as is sometimes the case when
|
1755
|
* the ENTER key is pressed in a textfield in Internet Explorer.
|
1756
|
*/
|
1757
|
function testNoButtonInfoInPost() {
|
1758
|
$path = 'form-test/clicked-button';
|
1759
|
$edit = array();
|
1760
|
$form_html_id = 'form-test-clicked-button';
|
1761
|
|
1762
|
// Ensure submitting a form with no buttons results in no
|
1763
|
// $form_state['triggering_element'] and the form submit handler not
|
1764
|
// running.
|
1765
|
$this->drupalPost($path, $edit, NULL, array(), array(), $form_html_id);
|
1766
|
$this->assertText('There is no clicked button.', '$form_state[\'triggering_element\'] set to NULL.');
|
1767
|
$this->assertNoText('Submit handler for form_test_clicked_button executed.', 'Form submit handler did not execute.');
|
1768
|
|
1769
|
// Ensure submitting a form with one or more submit buttons results in
|
1770
|
// $form_state['triggering_element'] being set to the first one the user has
|
1771
|
// access to. An argument with 'r' in it indicates a restricted
|
1772
|
// (#access=FALSE) button.
|
1773
|
$this->drupalPost($path . '/s', $edit, NULL, array(), array(), $form_html_id);
|
1774
|
$this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to only button.');
|
1775
|
$this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
|
1776
|
|
1777
|
$this->drupalPost($path . '/s/s', $edit, NULL, array(), array(), $form_html_id);
|
1778
|
$this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
|
1779
|
$this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
|
1780
|
|
1781
|
$this->drupalPost($path . '/rs/s', $edit, NULL, array(), array(), $form_html_id);
|
1782
|
$this->assertText('The clicked button is button2.', '$form_state[\'triggering_element\'] set to first available button.');
|
1783
|
$this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
|
1784
|
|
1785
|
// Ensure submitting a form with buttons of different types results in
|
1786
|
// $form_state['triggering_element'] being set to the first button,
|
1787
|
// regardless of type. For the FAPI 'button' type, this should result in the
|
1788
|
// submit handler not executing. The types are 's'(ubmit), 'b'(utton), and
|
1789
|
// 'i'(mage_button).
|
1790
|
$this->drupalPost($path . '/s/b/i', $edit, NULL, array(), array(), $form_html_id);
|
1791
|
$this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
|
1792
|
$this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
|
1793
|
|
1794
|
$this->drupalPost($path . '/b/s/i', $edit, NULL, array(), array(), $form_html_id);
|
1795
|
$this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
|
1796
|
$this->assertNoText('Submit handler for form_test_clicked_button executed.', 'Form submit handler did not execute.');
|
1797
|
|
1798
|
$this->drupalPost($path . '/i/s/b', $edit, NULL, array(), array(), $form_html_id);
|
1799
|
$this->assertText('The clicked button is button1.', '$form_state[\'triggering_element\'] set to first button.');
|
1800
|
$this->assertText('Submit handler for form_test_clicked_button executed.', 'Form submit handler executed.');
|
1801
|
}
|
1802
|
|
1803
|
/**
|
1804
|
* Test that $form_state['triggering_element'] does not get set to a button
|
1805
|
* with #access=FALSE.
|
1806
|
*/
|
1807
|
function testAttemptAccessControlBypass() {
|
1808
|
$path = 'form-test/clicked-button';
|
1809
|
$form_html_id = 'form-test-clicked-button';
|
1810
|
|
1811
|
// Retrieve a form where 'button1' has #access=FALSE and 'button2' doesn't.
|
1812
|
$this->drupalGet($path . '/rs/s');
|
1813
|
|
1814
|
// Submit the form with 'button1=button1' in the POST data, which someone
|
1815
|
// trying to get around security safeguards could easily do. We have to do
|
1816
|
// a little trickery here, to work around the safeguards in drupalPost(): by
|
1817
|
// renaming the text field that is in the form to 'button1', we can get the
|
1818
|
// data we want into $_POST.
|
1819
|
$elements = $this->xpath('//form[@id="' . $form_html_id . '"]//input[@name="text"]');
|
1820
|
$elements[0]['name'] = 'button1';
|
1821
|
$this->drupalPost(NULL, array('button1' => 'button1'), NULL, array(), array(), $form_html_id);
|
1822
|
|
1823
|
// Ensure that $form_state['triggering_element'] was not set to the
|
1824
|
// restricted button. Do this with both a negative and positive assertion,
|
1825
|
// because negative assertions alone can be brittle. See
|
1826
|
// testNoButtonInfoInPost() for why the triggering element gets set to
|
1827
|
// 'button2'.
|
1828
|
$this->assertNoText('The clicked button is button1.', '$form_state[\'triggering_element\'] not set to a restricted button.');
|
1829
|
$this->assertText('The clicked button is button2.', '$form_state[\'triggering_element\'] not set to a restricted button.');
|
1830
|
}
|
1831
|
}
|
1832
|
|
1833
|
/**
|
1834
|
* Tests rebuilding of arbitrary forms by altering them.
|
1835
|
*/
|
1836
|
class FormsArbitraryRebuildTestCase extends DrupalWebTestCase {
|
1837
|
public static function getInfo() {
|
1838
|
return array(
|
1839
|
'name' => 'Rebuild arbitrary forms',
|
1840
|
'description' => 'Tests altering forms to be rebuilt so there are multiple steps.',
|
1841
|
'group' => 'Form API',
|
1842
|
);
|
1843
|
}
|
1844
|
|
1845
|
function setUp() {
|
1846
|
parent::setUp('form_test');
|
1847
|
// Auto-create a field for testing.
|
1848
|
$field = array(
|
1849
|
'field_name' => 'test_multiple',
|
1850
|
'type' => 'text',
|
1851
|
'cardinality' => -1,
|
1852
|
'translatable' => FALSE,
|
1853
|
);
|
1854
|
field_create_field($field);
|
1855
|
|
1856
|
$instance = array(
|
1857
|
'entity_type' => 'node',
|
1858
|
'field_name' => 'test_multiple',
|
1859
|
'bundle' => 'page',
|
1860
|
'label' => 'Test a multiple valued field',
|
1861
|
'widget' => array(
|
1862
|
'type' => 'text_textfield',
|
1863
|
'weight' => 0,
|
1864
|
),
|
1865
|
);
|
1866
|
field_create_instance($instance);
|
1867
|
variable_set('user_register', USER_REGISTER_VISITORS);
|
1868
|
}
|
1869
|
|
1870
|
/**
|
1871
|
* Tests a basic rebuild with the user registration form.
|
1872
|
*/
|
1873
|
function testUserRegistrationRebuild() {
|
1874
|
$edit = array(
|
1875
|
'name' => 'foo',
|
1876
|
'mail' => 'bar@example.com',
|
1877
|
);
|
1878
|
$this->drupalPost('user/register', $edit, 'Rebuild');
|
1879
|
$this->assertText('Form rebuilt.');
|
1880
|
$this->assertFieldByName('name', 'foo', 'Entered user name has been kept.');
|
1881
|
$this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.');
|
1882
|
}
|
1883
|
|
1884
|
/**
|
1885
|
* Tests a rebuild caused by a multiple value field.
|
1886
|
*/
|
1887
|
function testUserRegistrationMultipleField() {
|
1888
|
$edit = array(
|
1889
|
'name' => 'foo',
|
1890
|
'mail' => 'bar@example.com',
|
1891
|
);
|
1892
|
$this->drupalPost('user/register', $edit, t('Add another item'), array('query' => array('field' => TRUE)));
|
1893
|
$this->assertText('Test a multiple valued field', 'Form has been rebuilt.');
|
1894
|
$this->assertFieldByName('name', 'foo', 'Entered user name has been kept.');
|
1895
|
$this->assertFieldByName('mail', 'bar@example.com', 'Entered mail address has been kept.');
|
1896
|
}
|
1897
|
}
|
1898
|
|
1899
|
/**
|
1900
|
* Tests form API file inclusion.
|
1901
|
*/
|
1902
|
class FormsFileInclusionTestCase extends DrupalWebTestCase {
|
1903
|
|
1904
|
public static function getInfo() {
|
1905
|
return array(
|
1906
|
'name' => 'Form API file inclusion',
|
1907
|
'description' => 'Tests form API file inclusion.',
|
1908
|
'group' => 'Form API',
|
1909
|
);
|
1910
|
}
|
1911
|
|
1912
|
function setUp() {
|
1913
|
parent::setUp('form_test');
|
1914
|
}
|
1915
|
|
1916
|
/**
|
1917
|
* Tests loading an include specified in hook_menu().
|
1918
|
*/
|
1919
|
function testLoadMenuInclude() {
|
1920
|
$this->drupalPostAJAX('form-test/load-include-menu', array(), array('op' => t('Save')), 'system/ajax', array(), array(), 'form-test-load-include-menu');
|
1921
|
$this->assertText('Submit callback called.');
|
1922
|
}
|
1923
|
|
1924
|
/**
|
1925
|
* Tests loading a custom specified inlcude.
|
1926
|
*/
|
1927
|
function testLoadCustomInclude() {
|
1928
|
$this->drupalPost('form-test/load-include-custom', array(), t('Save'));
|
1929
|
$this->assertText('Submit callback called.');
|
1930
|
}
|
1931
|
}
|
1932
|
|
1933
|
/**
|
1934
|
* Tests checkbox element.
|
1935
|
*/
|
1936
|
class FormCheckboxTestCase extends DrupalWebTestCase {
|
1937
|
|
1938
|
public static function getInfo() {
|
1939
|
return array(
|
1940
|
'name' => 'Form API checkbox',
|
1941
|
'description' => 'Tests form API checkbox handling of various combinations of #default_value and #return_value.',
|
1942
|
'group' => 'Form API',
|
1943
|
);
|
1944
|
}
|
1945
|
|
1946
|
function setUp() {
|
1947
|
parent::setUp('form_test');
|
1948
|
}
|
1949
|
|
1950
|
function testFormCheckbox() {
|
1951
|
// Ensure that the checked state is determined and rendered correctly for
|
1952
|
// tricky combinations of default and return values.
|
1953
|
foreach (array(FALSE, NULL, TRUE, 0, '0', '', 1, '1', 'foobar', '1foobar') as $default_value) {
|
1954
|
// Only values that can be used for array indeces are supported for
|
1955
|
// #return_value, with the exception of integer 0, which is not supported.
|
1956
|
// @see form_process_checkbox().
|
1957
|
foreach (array('0', '', 1, '1', 'foobar', '1foobar') as $return_value) {
|
1958
|
$form_array = drupal_get_form('form_test_checkbox_type_juggling', $default_value, $return_value);
|
1959
|
$form = drupal_render($form_array);
|
1960
|
if ($default_value === TRUE) {
|
1961
|
$checked = TRUE;
|
1962
|
}
|
1963
|
elseif ($return_value === '0') {
|
1964
|
$checked = ($default_value === '0');
|
1965
|
}
|
1966
|
elseif ($return_value === '') {
|
1967
|
$checked = ($default_value === '');
|
1968
|
}
|
1969
|
elseif ($return_value === 1 || $return_value === '1') {
|
1970
|
$checked = ($default_value === 1 || $default_value === '1');
|
1971
|
}
|
1972
|
elseif ($return_value === 'foobar') {
|
1973
|
$checked = ($default_value === 'foobar');
|
1974
|
}
|
1975
|
elseif ($return_value === '1foobar') {
|
1976
|
$checked = ($default_value === '1foobar');
|
1977
|
}
|
1978
|
$checked_in_html = strpos($form, 'checked') !== FALSE;
|
1979
|
$message = format_string('#default_value is %default_value #return_value is %return_value.', array('%default_value' => var_export($default_value, TRUE), '%return_value' => var_export($return_value, TRUE)));
|
1980
|
$this->assertIdentical($checked, $checked_in_html, $message);
|
1981
|
}
|
1982
|
}
|
1983
|
|
1984
|
// Ensure that $form_state['values'] is populated correctly for a checkboxes
|
1985
|
// group that includes a 0-indexed array of options.
|
1986
|
$results = json_decode($this->drupalPost('form-test/checkboxes-zero', array(), 'Save'));
|
1987
|
$this->assertIdentical($results->checkbox_off, array(0, 0, 0), 'All three in checkbox_off are zeroes: off.');
|
1988
|
$this->assertIdentical($results->checkbox_zero_default, array('0', 0, 0), 'The first choice is on in checkbox_zero_default');
|
1989
|
$this->assertIdentical($results->checkbox_string_zero_default, array('0', 0, 0), 'The first choice is on in checkbox_string_zero_default');
|
1990
|
$edit = array('checkbox_off[0]' => '0');
|
1991
|
$results = json_decode($this->drupalPost('form-test/checkboxes-zero', $edit, 'Save'));
|
1992
|
$this->assertIdentical($results->checkbox_off, array('0', 0, 0), 'The first choice is on in checkbox_off but the rest is not');
|
1993
|
|
1994
|
// Ensure that each checkbox is rendered correctly for a checkboxes group
|
1995
|
// that includes a 0-indexed array of options.
|
1996
|
$this->drupalPost('form-test/checkboxes-zero/0', array(), 'Save');
|
1997
|
$checkboxes = $this->xpath('//input[@type="checkbox"]');
|
1998
|
foreach ($checkboxes as $checkbox) {
|
1999
|
$checked = isset($checkbox['checked']);
|
2000
|
$name = (string) $checkbox['name'];
|
2001
|
$this->assertIdentical($checked, $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', array('%name' => $name)));
|
2002
|
}
|
2003
|
$edit = array('checkbox_off[0]' => '0');
|
2004
|
$this->drupalPost('form-test/checkboxes-zero/0', $edit, 'Save');
|
2005
|
$checkboxes = $this->xpath('//input[@type="checkbox"]');
|
2006
|
foreach ($checkboxes as $checkbox) {
|
2007
|
$checked = isset($checkbox['checked']);
|
2008
|
$name = (string) $checkbox['name'];
|
2009
|
$this->assertIdentical($checked, $name == 'checkbox_off[0]' || $name == 'checkbox_zero_default[0]' || $name == 'checkbox_string_zero_default[0]', format_string('Checkbox %name correctly checked', array('%name' => $name)));
|
2010
|
}
|
2011
|
}
|
2012
|
}
|
2013
|
|
2014
|
/**
|
2015
|
* Tests uniqueness of generated HTML IDs.
|
2016
|
*/
|
2017
|
class HTMLIdTestCase extends DrupalWebTestCase {
|
2018
|
|
2019
|
public static function getInfo() {
|
2020
|
return array(
|
2021
|
'name' => 'Unique HTML IDs',
|
2022
|
'description' => 'Tests functionality of drupal_html_id().',
|
2023
|
'group' => 'Form API',
|
2024
|
);
|
2025
|
}
|
2026
|
|
2027
|
function setUp() {
|
2028
|
parent::setUp('form_test');
|
2029
|
}
|
2030
|
|
2031
|
/**
|
2032
|
* Tests that HTML IDs do not get duplicated when form validation fails.
|
2033
|
*/
|
2034
|
function testHTMLId() {
|
2035
|
$this->drupalGet('form-test/double-form');
|
2036
|
$this->assertNoDuplicateIds('There are no duplicate IDs');
|
2037
|
|
2038
|
// Submit second form with empty title.
|
2039
|
$edit = array();
|
2040
|
$this->drupalPost(NULL, $edit, 'Save', array(), array(), 'form-test-html-id--2');
|
2041
|
$this->assertNoDuplicateIds('There are no duplicate IDs');
|
2042
|
}
|
2043
|
}
|