1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
<?php
// $Id$
/**
* @file
* Unit tests for the Drupal Form API.
*/
class FormsTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Form element validation',
'description' => 'Tests various form element validation mechanisms.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Check several empty values for required forms elements.
*
* Carriage returns, tabs, spaces, and unchecked checkbox elements are not
* valid content for a required field.
*
* If the form field is found in form_get_errors() then the test pass.
*/
function testRequiredFields() {
// Originates from http://drupal.org/node/117748
// Sets of empty strings and arrays.
$empty_strings = array('""' => "", '"\n"' => "\n", '" "' => " ", '"\t"' => "\t", '" \n\t "' => " \n\t ", '"\n\n\n\n\n"' => "\n\n\n\n\n");
$empty_arrays = array('array()' => array());
$empty_checkbox = array(NULL);
$elements['textfield']['element'] = array('#title' => $this->randomName(), '#type' => 'textfield');
$elements['textfield']['empty_values'] = $empty_strings;
$elements['password']['element'] = array('#title' => $this->randomName(), '#type' => 'password');
$elements['password']['empty_values'] = $empty_strings;
$elements['password_confirm']['element'] = array('#title' => $this->randomName(), '#type' => 'password_confirm');
$elements['password_confirm']['empty_values'] = $empty_strings;
$elements['textarea']['element'] = array('#title' => $this->randomName(), '#type' => 'textarea');
$elements['textarea']['empty_values'] = $empty_strings;
$elements['radios']['element'] = array('#title' => $this->randomName(), '#type' => 'radios', '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
$elements['radios']['empty_values'] = $empty_arrays;
$elements['checkbox']['element'] = array('#title' => $this->randomName(), '#type' => 'checkbox', '#required' => TRUE, '#title' => $this->randomName());
$elements['checkbox']['empty_values'] = $empty_checkbox;
$elements['checkboxes']['element'] = array('#title' => $this->randomName(), '#type' => 'checkboxes', '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
$elements['checkboxes']['empty_values'] = $empty_arrays;
$elements['select']['element'] = array('#title' => $this->randomName(), '#type' => 'select', '#options' => array($this->randomName(), $this->randomName(), $this->randomName()));
$elements['select']['empty_values'] = $empty_strings;
$elements['file']['element'] = array('#title' => $this->randomName(), '#type' => 'file');
$elements['file']['empty_values'] = $empty_strings;
// Regular expression to find the expected marker on required elements.
$required_marker_preg = '@<label.*<span class="form-required" title="This field is required\.">\*</span></label>@';
// Go through all the elements and all the empty values for them.
foreach ($elements as $type => $data) {
foreach ($data['empty_values'] as $key => $empty) {
foreach (array(TRUE, FALSE) as $required) {
$form_id = $this->randomName();
$form = $form_state = array();
form_clear_error();
$form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
$element = $data['element']['#title'];
$form[$element] = $data['element'];
$form[$element]['#required'] = $required;
$form_state['values'][$element] = $empty;
$form_state['input'] = $form_state['values'];
$form_state['input']['form_id'] = $form_id;
$form_state['method'] = 'post';
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$errors = form_get_errors();
// Form elements of type 'radios' throw all sorts of PHP notices
// when you try to render them like this, so we ignore those for
// testing the required marker.
// @todo Fix this work-around (http://drupal.org/node/588438).
$form_output = ($type == 'radios') ? '' : drupal_render($form);
if ($required) {
// Make sure we have a form error for this element.
$this->assertTrue(isset($errors[$element]), "Check empty($key) '$type' field '$element'");
if (!empty($form_output)) {
// Make sure the form element is marked as required.
$this->assertTrue(preg_match($required_marker_preg, $form_output), "Required '$type' field is marked as required");
}
}
else {
if (!empty($form_output)) {
// Make sure the form element is *not* marked as required.
$this->assertFalse(preg_match($required_marker_preg, $form_output), "Optional '$type' field is not marked as required");
}
if ($type == 'select') {
// Select elements are going to have validation errors with empty
// input, since those are illegal choices. Just make sure the
// error is not "field is required".
$this->assertTrue((empty($errors[$element]) || strpos('field is required', $errors[$element]) === FALSE), "Optional '$type' field '$element' is not treated as a required element");
}
else {
// Make sure there is *no* form error for this element.
$this->assertTrue(empty($errors[$element]), "Optional '$type' field '$element' has no errors with empty input");
}
}
}
}
}
// Clear the expected form error messages so they don't appear as exceptions.
drupal_get_messages();
}
/**
* Test default value handling for checkboxes.
*
* @see _form_test_checkbox().
*/
function testCheckboxProcessing() {
// First, try to submit without the required checkbox.
$edit = array();
$this->drupalPost('form-test/checkbox', $edit, t('Submit'));
$this->assertRaw(t('!name field is required.', array('!name' => 'required_checkbox')), t('A required checkbox is actually mandatory'));
// Now try to submit the form correctly.
$this->drupalPost(NULL, array('required_checkbox' => 1), t('Submit'));
$values = json_decode($this->drupalGetContent(), TRUE);
$expected_values = array(
'disabled_checkbox_on' => 'disabled_checkbox_on',
'disabled_checkbox_off' => '',
'checkbox_on' => 'checkbox_on',
'checkbox_off' => '',
'zero_checkbox_on' => '0',
'zero_checkbox_off' => '',
);
foreach ($expected_values as $widget => $expected_value) {
$this->assertEqual($values[$widget], $expected_value, t('Checkbox %widget returns expected value (expected: %expected, got: %value)', array(
'%widget' => var_export($widget, TRUE),
'%expected' => var_export($expected_value, TRUE),
'%value' => var_export($values[$widget], TRUE),
)));
}
}
}
/**
* Test form validation handlers.
*/
class FormValidationTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Form validation handlers',
'description' => 'Tests form processing and alteration via form validation handlers.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Tests form alterations by #element_validate, #validate, and form_set_value().
*/
function testValidate() {
$this->drupalGet('form-test/validate');
// Verify that #element_validate handlers can alter the form and submitted
// form values.
$edit = array(
'name' => 'element_validate',
);
$this->drupalPost(NULL, $edit, 'Save');
$this->assertFieldByName('name', '#value changed by #element_validate', t('Form element #value was altered.'));
$this->assertText('Name value: value changed by form_set_value() in #element_validate', t('Form element value in $form_state was altered.'));
// Verify that #validate handlers can alter the form and submitted
// form values.
$edit = array(
'name' => 'validate',
);
$this->drupalPost(NULL, $edit, 'Save');
$this->assertFieldByName('name', '#value changed by #validate', t('Form element #value was altered.'));
$this->assertText('Name value: value changed by form_set_value() in #validate', t('Form element value in $form_state was altered.'));
// Verify that #element_validate handlers can make form elements
// inaccessible, but values persist.
$edit = array(
'name' => 'element_validate_access',
);
$this->drupalPost(NULL, $edit, 'Save');
$this->assertNoFieldByName('name', t('Form element was hidden.'));
$this->assertText('Name value: element_validate_access', t('Value for inaccessible form element exists.'));
// Verify that value for inaccessible form element persists.
$this->drupalPost(NULL, array(), 'Save');
$this->assertNoFieldByName('name', t('Form element was hidden.'));
$this->assertText('Name value: element_validate_access', t('Value for inaccessible form element exists.'));
}
}
/**
* Test the tableselect form element for expected behavior.
*/
class FormsElementsTableSelectFunctionalTest extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Tableselect form element type test',
'description' => 'Test the tableselect element for expected behavior',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Test the display of checkboxes when #multiple is TRUE.
*/
function testMultipleTrue() {
$this->drupalGet('form_test/tableselect/multiple-true');
$this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.'));
// Test for the presence of the Select all rows tableheader.
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Presence of the "Select all" checkbox.'));
$rows = array('row1', 'row2', 'row3');
foreach ($rows as $row) {
$this->assertFieldByXPath('//input[@type="checkbox"]', $row, t('Checkbox for value @row.', array('@row' => $row)));
}
}
/**
* Test the display of radios when #multiple is FALSE.
*/
function testMultipleFalse() {
$this->drupalGet('form_test/tableselect/multiple-false');
$this->assertNoText(t('Empty text.'), t('Empty text should not be displayed.'));
// Test for the absence of the Select all rows tableheader.
$this->assertNoFieldByXPath('//th[@class="select-all"]', '', t('Absence of the "Select all" checkbox.'));
$rows = array('row1', 'row2', 'row3');
foreach ($rows as $row) {
$this->assertFieldByXPath('//input[@type="radio"]', $row, t('Radio button for value @row.', array('@row' => $row)));
}
}
/**
* Test the display of the #empty text when #options is an empty array.
*/
function testEmptyText() {
$this->drupalGet('form_test/tableselect/empty-text');
$this->assertText(t('Empty text.'), t('Empty text should be displayed.'));
}
/**
* Test the submission of single and multiple values when #multiple is TRUE.
*/
function testMultipleTrueSubmit() {
// Test a submission with one checkbox checked.
$edit = array();
$edit['tableselect[row1]'] = TRUE;
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
$this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1'));
$this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.'));
$this->assertText(t('Submitted: row3 = 0'), t('Unchecked checkbox row3.'));
// Test a submission with multiple checkboxes checked.
$edit['tableselect[row1]'] = TRUE;
$edit['tableselect[row3]'] = TRUE;
$this->drupalPost('form_test/tableselect/multiple-true', $edit, 'Submit');
$this->assertText(t('Submitted: row1 = row1'), t('Checked checkbox row1.'));
$this->assertText(t('Submitted: row2 = 0'), t('Unchecked checkbox row2.'));
$this->assertText(t('Submitted: row3 = row3'), t('Checked checkbox row3.'));
}
/**
* Test submission of values when #multiple is FALSE.
*/
function testMultipleFalseSubmit() {
$edit['tableselect'] = 'row1';
$this->drupalPost('form_test/tableselect/multiple-false', $edit, 'Submit');
$this->assertText(t('Submitted: row1'), t('Selected radio button'));
}
/**
* Test the #js_select property.
*/
function testAdvancedSelect() {
// When #multiple = TRUE a Select all checkbox should be displayed by default.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-default');
$this->assertFieldByXPath('//th[@class="select-all"]', NULL, t('Display a "Select all" checkbox by default when #multiple is TRUE.'));
// When #js_select is set to FALSE, a "Select all" checkbox should not be displayed.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-true-no-advanced-select');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #js_select is FALSE.'));
// A "Select all" checkbox never makes sense when #multiple = FALSE, regardless of the value of #js_select.
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-default');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE.'));
$this->drupalGet('form_test/tableselect/advanced-select/multiple-false-advanced-select');
$this->assertNoFieldByXPath('//th[@class="select-all"]', NULL, t('Do not display a "Select all" checkbox when #multiple is FALSE, even when #js_select is TRUE.'));
}
/**
* Test the whether the option checker gives an error on invalid tableselect values for checkboxes.
*/
function testMultipleTrueOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
);
// Test with a valid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1'));
$this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for checkboxes.'));
// Test with an invalid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value'));
$this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for checkboxes.'));
}
/**
* Test the whether the option checker gives an error on invalid tableselect values for radios.
*/
function testMultipleFalseOptionchecker() {
list($header, $options) = _form_test_tableselect_get_data();
$form['tableselect'] = array(
'#type' => 'tableselect',
'#header' => $header,
'#options' => $options,
'#multiple' => FALSE,
);
// Test with a valid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'row1'));
$this->assertFalse(isset($errors['tableselect']), t('Option checker allows valid values for radio buttons.'));
// Test with an invalid value.
list($processed_form, $form_state, $errors) = $this->formSubmitHelper($form, array('tableselect' => 'non_existing_value'));
$this->assertTrue(isset($errors['tableselect']), t('Option checker disallows invalid values for radio buttons.'));
}
/**
* Helper function for the option check test to submit a form while collecting errors.
*
* @param $form_element
* A form element to test.
* @param $edit
* An array containing post data.
*
* @return
* An array containing the processed form, the form_state and any errors.
*/
private function formSubmitHelper($form, $edit) {
$form_id = $this->randomName();
$form_state = form_state_defaults();
$form['op'] = array('#type' => 'submit', '#value' => t('Submit'));
$form_state['input'] = $edit;
$form_state['input']['form_id'] = $form_id;
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
$errors = form_get_errors();
// Clear errors and messages.
drupal_get_messages();
form_clear_error();
// Return the processed form together with form_state and errors
// to allow the caller lowlevel access to the form.
return array($form, $form_state, $errors);
}
}
/**
* Test using drupal_form_submit in a batch.
*/
class FormAPITestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Drupal Execute and Batch API',
'description' => 'Tests the compatibility of drupal_form_submit and the Batch API',
'group' => 'Form API',
);
}
/**
* Check that we can run drupal_form_submit during a batch.
*/
function testDrupalFormSubmitInBatch() {
// Our test is going to modify the following variable.
variable_set('form_test_mock_submit', 'initial_state');
// This is a page that sets a batch, which calls drupal_form_submit, which
// modifies the variable we set up above.
$this->drupalGet('form_test/drupal_form_submit_batch_api');
// If the drupal_form_submit call executed correctly our test variable will be
// set to 'form_submitted'.
$this->assertEqual('form_submitted', variable_get('form_test_mock_submit', 'initial_state'), t('Check drupal_form_submit called submit handlers when running in a batch'));
// Clean our variable up.
variable_del('form_test_mock_submit');
}
function setUp() {
parent::setUp('form_test');
}
}
/**
* Test the form storage on a multistep form.
*
* The tested form puts data into the storage during the initial form
* construction. These tests verify that there are no duplicate form
* constructions, with and without manual form caching activiated. Furthermore
* when a validation error occurs, it makes sure that changed form element
* values aren't lost due to a wrong form rebuild.
*/
class FormsFormStorageTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Multistep form using form storage',
'description' => 'Tests a multistep form using form storage and makes sure validation and caching works right.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Tests using the form in a usual way.
*/
function testForm() {
$user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($user);
$this->drupalPost('form_test/form-storage', array('title' => 'new', 'value' => 'value_is_set'), 'Continue');
$this->assertText('Form constructions: 2', t('The form has been constructed two times till now.'));
$this->drupalPost(NULL, array(), 'Save');
$this->assertText('Form constructions: 3', t('The form has been constructed three times till now.'));
$this->assertText('Title: new', t('The form storage has stored the values.'));
}
/**
* Tests using the form with an activated $form_state['cache'] property.
*/
function testFormCached() {
$user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($user);
$this->drupalPost('form_test/form-storage', array('title' => 'new', 'value' => 'value_is_set'), 'Continue', array('query' => array('cache' => 1)));
$this->assertText('Form constructions: 1', t('The form has been constructed one time till now.'));
$this->drupalPost(NULL, array(), 'Save', array('query' => array('cache' => 1)));
$this->assertText('Form constructions: 2', t('The form has been constructed two times till now.'));
$this->assertText('Title: new', t('The form storage has stored the values.'));
}
/**
* Tests validation when form storage is used.
*/
function testValidation() {
$user = $this->drupalCreateUser(array('access content'));
$this->drupalLogin($user);
$this->drupalPost('form_test/form-storage', array('title' => '', 'value' => 'value_is_set'), 'Continue');
$this->assertPattern('/value_is_set/', t("The input values have been kept."));
}
}
/**
* Test wrapper form callbacks.
*/
class FormsFormWrapperTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Form wrapper callback',
'description' => 'Tests form wrapper callbacks to pass a prebuilt form to form builder functions.',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Tests using the form in a usual way.
*/
function testWrapperCallback() {
$this->drupalGet('form_test/wrapper-callback');
$this->assertText('Form wrapper callback element output.', t('The form contains form wrapper elements.'));
$this->assertText('Form builder element output.', t('The form contains form builder elements.'));
}
}
/**
* Test $form_state clearance.
*/
class FormStateValuesCleanTestCase extends DrupalWebTestCase {
public static function getInfo() {
return array(
'name' => 'Form state values clearance',
'description' => 'Test proper removal of submitted form values using form_state_values_clean().',
'group' => 'Form API',
);
}
function setUp() {
parent::setUp('form_test');
}
/**
* Tests form_state_values_clean().
*/
function testFormStateValuesClean() {
$this->drupalPost('form_test/form-state-values-clean', array(), t('Submit'));
$values = json_decode($this->content, TRUE);
// Setup the expected result.
$result = array(
'beer' => 1000,
'baz' => array('beer' => 2000),
);
// Verify that all internal Form API elements were removed.
$this->assertFalse(isset($values['form_id']), t('%element was removed.', array('%element' => 'form_id')));
$this->assertFalse(isset($values['form_token']), t('%element was removed.', array('%element' => 'form_token')));
$this->assertFalse(isset($values['form_build_id']), t('%element was removed.', array('%element' => 'form_build_id')));
$this->assertFalse(isset($values['op']), t('%element was removed.', array('%element' => 'op')));
// Verify that all buttons were removed.
$this->assertFalse(isset($values['foo']), t('%element was removed.', array('%element' => 'foo')));
$this->assertFalse(isset($values['bar']), t('%element was removed.', array('%element' => 'bar')));
$this->assertFalse(isset($values['baz']['foo']), t('%element was removed.', array('%element' => 'foo')));
$this->assertFalse(isset($values['baz']['baz']), t('%element was removed.', array('%element' => 'baz')));
// Verify that nested form value still exists.
$this->assertTrue(isset($values['baz']['beer']), t('Nested form value still exists.'));
// Verify that actual form values equal resulting form values.
$this->assertEqual($values, $result, t('Expected form values equal actual form values.'));
}
}
|