Projet

Général

Profil

Paste
Télécharger (34 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformTestCase.test @ 8c72e82a

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module tests.
6
 */
7
class WebformTestCase extends DrupalWebTestCase {
8
  private $_webform_node;
9
  private $_webform_components;
10
  public $webform_users;
11

    
12
  /**
13
   * {@inheritdoc}
14
   */
15
  function setUp($added_modules = array()) {
16
    // Enable Webform and Token module if available.
17
    if (module_exists('token')) {
18
      $modules = array('webform', 'token');
19
    }
20
    else {
21
      $modules = array('webform');
22
    }
23
    parent::setUp(array_merge($modules, $added_modules));
24

    
25
    // Create a profile field to test [user:?] tokens.
26
    $field = array(
27
      'field_name' => 'gender',
28
      'type' => 'text',
29
      'cardinality' => 1,
30
    );
31
    $instance = array(
32
      'field_name' => 'gender',
33
      'entity_type' => 'user',
34
      'bundle' => 'user',
35
      'label' => 'Gender',
36
      'widget' => array(
37
        'type' => 'text_textfield',
38
        'label' => 'Gender',
39
      ),
40
    );
41
    field_create_field($field);
42
    field_create_instance($instance);
43

    
44
    // Create a normal user that can view their own submissions.
45
    $permissions['userAccess'] = array(
46
      'access content',
47
      'access own webform submissions',
48
    );
49

    
50
    // Create a normal user than can edit their own submissions.
51
    $permissions['userEdit'] = array(
52
      'access content',
53
      'edit own webform submissions',
54
    );
55

    
56
    // Create a webform editor to test creating and editing own content.
57
    $permissions['editor'] = array(
58
      'access content',
59
      'create webform content',
60
      'edit own webform content',
61
      'access all webform results',
62
    );
63

    
64
    // Create a webform admin that will do all node creation.
65
    $permissions['admin'] = array(
66
      'access content',
67
      'administer nodes',
68
      'create webform content',
69
      'edit any webform content',
70
      'access all webform results',
71
      'edit all webform submissions',
72
      'delete all webform submissions',
73
    );
74

    
75
    foreach ($permissions as $user_key => $role_permissions) {
76
      $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
77
      $this->webform_users[$user_key]->gender = array(LANGUAGE_NONE => array(array('value' => 'Female')));
78
      user_save($this->webform_users[$user_key]);
79
    }
80
  }
81

    
82
  /**
83
   * {@inheritdoc}
84
   */
85
  function tearDown() {
86
    // Delete the webform admin and any created nodes.
87
    foreach ($this->webform_users as $account) {
88
      $uid = $account->uid;
89
      $result = db_select('node')
90
        ->fields('node')
91
        ->condition('uid', $uid)
92
        ->execute();
93
      foreach ($result as $node) {
94
        node_delete($node->nid);
95
      }
96
      user_cancel(array(), $uid, 'user_cancel_delete');
97
    }
98

    
99
    parent::tearDown();
100
  }
101

    
102
  function webformReset() {
103
    $this->_webform_node = NULL;
104
    $this->_webform_components = NULL;
105
  }
106

    
107
  /**
108
   * Provide a list of components to test throughout the suite.
109
   *
110
   * Each component provides:
111
   *   - A default configuration for the component.
112
   *   - Values to try setting via POST
113
   *   - Values that should match the database storage when set via POST
114
   *   - Values that should match the database storage when using the default values.
115
   *
116
   * @return array
117
   *   An array of each component settings.
118
   */
119
  function webformComponents() {
120
    if (isset($this->_webform_components)) {
121
      return $this->_webform_components;
122
    }
123

    
124
    $this->_webform_components = array(
125
      // Test date components.
126
      'date' => array(
127
        'component' => array(
128
          'form_key' => 'date',
129
          'name' => 'Date',
130
          'type' => 'date',
131
          'value' => '19 Nov 1978',
132
          'extra' => array(
133
            'timezone' => 'site',
134
            'start_date' => '-100 years',
135
            'end_date' => '+2 years',
136
          ),
137
          'required' => '0',
138
          'pid' => '0',
139
          'weight' => '-15',
140
        ),
141
        'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
142
        'database values' => array('1982-09-30'),
143
        'database default values' => array('1978-11-19'),
144
        // Conditionals match against the 'sample values'.
145
        'match conditional values' => array(
146
          'equal' => '1982/9/30',
147
          'before' => '1982/10/1',
148
          'before_equal' => '1982/9/30',
149
          'after' => '1982/9/29',
150
          'after_equal' => '1982/9/29',
151
        ),
152
        'mismatch conditional values' => array(
153
          'equal' => '1981/9/30',
154
          'before' => '1982/9/30',
155
          'before_equal' => '1982/9/29',
156
          'after' => '1982/9/30',
157
          'after_equal' => '1982/10/1',
158
        ),
159
      ),
160

    
161
      // Test grid components.
162
      'grid' => array(
163
        'component' => array(
164
          'form_key' => 'grid',
165
          'name' => 'Grid',
166
          'type' => 'grid',
167
          'value' => '',
168
          'extra' => array(
169
            'questions' => "0|Ålphå\n1|ıé†å\n2|Î鬆å", // Left side
170
            'options' => "0|øne\n1|twö\n2|ǼBƇ\n3|€Euro", // Top
171
          ),
172
          'required' => '0',
173
          'pid' => '2',
174
          'weight' => '-19',
175
        ),
176
        'sample values' => array('0' => '0', '1' => '1', '2' => '2'),
177
        'database values' => array('0' => '0', '1' => '1', '2' => '2'),
178
        'database default values' => array('', '', ''),
179
      ),
180
      'grid_keyed' => array(
181
        'component' => array(
182
          'form_key' => 'grid_keyed',
183
          'name' => 'Grid Keyed',
184
          'type' => 'grid',
185
          'value' => '',
186
          'extra' => array(
187
            'questions' => "one|What's your option?\ntwo|Agåin?\nthree|One more time!", // Left side.
188
            'options' => "one|Option one\ntwo|Option 2\nthree| Three is me", // Top
189
          ),
190
          'required' => '0',
191
          'pid' => '0',
192
          'weight' => '-15',
193
        ),
194
        'sample values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
195
        'database values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
196
        'database default values' => array('one' => '', 'two' => '', 'three' => ''),
197
      ),
198

    
199
      // Test select components.
200
      'checkboxes' => array(
201
        'component' => array(
202
          'form_key' => 'checkboxes',
203
          'name' => 'Checkboxes',
204
          'type' => 'select',
205
          'value' => 'two',
206
          'extra' => array(
207
            'items' => "one|one\ntwo|two\nthree|three",
208
            'multiple' => 1,
209
          ),
210
          'required' => '0',
211
          'pid' => '0',
212
          'weight' => '-15',
213
        ),
214
        'sample values' => array('one' => TRUE, 'two' => FALSE, 'three' => TRUE),
215
        'database values' => array('one', 'three'),
216
        'database default values' => array('two'),
217
        // Conditionals match against the 'sample values'.
218
        'match conditional values' => array(
219
          'equal' => array('one', 'three'), // ANDed together match.
220
          'not_equal' => array('two'),
221
        ),
222
        'mismatch conditional values' => array(
223
          'equal' => array('one', 'two'),
224
          'not_equal' => array('two', 'three'),
225
        ),
226
      ),
227
      'checkboxes_zero' => array(
228
        'component' => array(
229
          'form_key' => 'checkboxes_zero',
230
          'name' => 'Checkboxes zero',
231
          'type' => 'select',
232
          'value' => '0',
233
          'extra' => array(
234
            'items' => "0|zero\n1|one\n2|two",
235
            'multiple' => 1,
236
          ),
237
          'required' => '1',
238
          'pid' => '0',
239
          'weight' => '-9',
240
        ),
241
        'sample values' => array('0' => TRUE),
242
        'database values' => array('0'),
243
        'database default values' => array('0'),
244
        // Conditionals match against the 'sample values'.
245
        'match conditional values' => array(
246
          'equal' => '0',
247
          'not_equal' => '1',
248
        ),
249
        'mismatch conditional values' => array(
250
          'equal' => '1',
251
          'not_equal' => '0',
252
        ),
253
      ),
254
      'radios' => array(
255
        'component' => array(
256
          'form_key' => 'radios',
257
          'name' => 'Radios',
258
          'type' => 'select',
259
          'value' => 'two',
260
          'extra' => array(
261
            'items' => "one|one\ntwo|two\nthree|three",
262
          ),
263
          'required' => '1',
264
          'pid' => '0',
265
          'weight' => '-9',
266
        ),
267
        'sample values' => 'one',
268
        'database values' => array('one'),
269
        'database default values' => array('two'),
270
        // Conditionals match against the 'sample values'.
271
        'match conditional values' => array(
272
          'equal' => 'one',
273
          'not_equal' => 'two',
274
        ),
275
        'mismatch conditional values' => array(
276
          'equal' => 'two',
277
          'not_equal' => 'one',
278
        ),
279
      ),
280
      'radios_zero' => array(
281
        'component' => array(
282
          'form_key' => 'radios_zero',
283
          'name' => 'Radios zero',
284
          'type' => 'select',
285
          'value' => '0',
286
          'extra' => array(
287
            'items' => "0|zero\n1|one\n2|two",
288
          ),
289
          'required' => '1',
290
          'pid' => '0',
291
          'weight' => '-9',
292
        ),
293
        'sample values' => '0',
294
        'database values' => array('0'),
295
        'database default values' => array('0'),
296
        // Conditionals match against the 'sample values'.
297
        'match conditional values' => array(
298
          'equal' => '0',
299
          'not_equal' => '1',
300
        ),
301
        'mismatch conditional values' => array(
302
          'equal' => '1',
303
          'not_equal' => '0',
304
        ),
305
      ),
306
      'radios_relative' => array(
307
        'component' => array(
308
          'form_key' => 'radios_relative',
309
          'name' => 'Radios relative',
310
          'type' => 'select',
311
          'value' => 'one',
312
          'extra' => array(
313
            'items' => "zero|Zero\none|One\ntwo|Two\nthree|Three\n",
314
          ),
315
          'required' => '1',
316
          'pid' => '0',
317
          'weight' => '-9',
318
        ),
319
        'sample values' => 'one',
320
        'database values' => array('one'),
321
        'database default values' => array('one'),
322
        // Conditionals match against the 'sample values'.
323
        'match conditional values' => array(
324
          'equal' => 'one',
325
          'not_equal' => 'zero',
326
          'less_than' => 'two',
327
          'less_than_equal' => 'one',
328
          'greater_than' => 'zero',
329
          'greater_than_equal' => 'zero',
330
        ),
331
        'mismatch conditional values' => array(
332
          'equal' => 'zero',
333
          'not_equal' => 'one',
334
          'less_than' => 'one',
335
          'less_than_equal' => 'zero',
336
          'greater_than' => 'two',
337
          'greater_than_equal' => 'two',
338
        ),
339
      ),
340
      'select' => array(
341
        'component' => array(
342
          'form_key' => 'select',
343
          'name' => 'Select',
344
          'type' => 'select',
345
          'value' => 'one',
346
          'extra' => array(
347
            'description' => 'Description here',
348
            'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
349
            'aslist' => 1,
350
          ),
351
          'required' => '1',
352
          'pid' => '0',
353
          'weight' => '-15',
354
        ),
355
        'sample values' => 'two',
356
        'database values' => array('two'),
357
        'database default values' => array('one'),
358
        // Conditionals match against the 'sample values'.
359
        'match conditional values' => array(
360
          'equal' => 'two',
361
          'not_equal' => 'one',
362
        ),
363
        'mismatch conditional values' => array(
364
          'equal' => 'one',
365
          'not_equal' => 'two',
366
        ),
367
      ),
368
      'select_zero' => array(
369
        'component' => array(
370
          'form_key' => 'select_zero',
371
          'name' => 'Select zero',
372
          'type' => 'select',
373
          'value' => '0',
374
          'extra' => array(
375
            'description' => 'Tests saving zero as a value.',
376
            'items' => "0|zero\n1|one\n2|two",
377
            'aslist' => 1,
378
          ),
379
          'required' => '1',
380
          'pid' => '0',
381
          'weight' => '-15',
382
        ),
383
        'sample values' => '0',
384
        'database values' => array('0'),
385
        'database default values' => array('0'),
386
        // Conditionals match against the 'sample values'.
387
        'match conditional values' => array(
388
          'equal' => '0',
389
          'not_equal' => '1',
390
        ),
391
        'mismatch conditional values' => array(
392
          'equal' => '1',
393
          'not_equal' => '0',
394
        ),
395
      ),
396
      'select_no_default' => array(
397
        'component' => array(
398
          'form_key' => 'select_no_default',
399
          'name' => 'Select no default',
400
          'type' => 'select',
401
          'value' => '',
402
          'extra' => array(
403
            'description' => 'Description here',
404
            'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
405
            'aslist' => 1,
406
          ),
407
          'required' => '0',
408
          'pid' => '0',
409
          'weight' => '-15',
410
        ),
411
        'sample values' => 'two',
412
        'database values' => array('two'),
413
        'database default values' => array(''),
414
        // Conditionals match against the 'sample values'.
415
        'match conditional values' => array(
416
          'equal' => 'two',
417
          'not_equal' => '',
418
        ),
419
        'mismatch conditional values' => array(
420
          'equal' => '',
421
          'not_equal' => 'two',
422
        ),
423
      ),
424
      'select_no_default_zero' => array(
425
        'component' => array(
426
          'form_key' => 'select_no_default_zero',
427
          'name' => 'Select no default zero',
428
          'type' => 'select',
429
          'value' => '',
430
          'extra' => array(
431
            'description' => 'Tests saving zero as a value.',
432
            'items' => "0|zero\n1|one\n2|two",
433
            'aslist' => 1,
434
          ),
435
          'required' => '0',
436
          'pid' => '0',
437
          'weight' => '-15',
438
        ),
439
        'sample values' => '0',
440
        'database values' => array('0'),
441
        'database default values' => array(''),
442
        // Conditionals match against the 'sample values'.
443
        'match conditional values' => array(
444
          'equal' => '0',
445
          'not_equal' => '',
446
        ),
447
        'mismatch conditional values' => array(
448
          'equal' => '',
449
          'not_equal' => '0',
450
        ),
451
      ),
452
      'select_optgroup' => array(
453
        'component' => array(
454
          'form_key' => 'select_optgroup',
455
          'name' => 'Select Optgroup',
456
          'type' => 'select',
457
          'value' => 'option 1-2',
458
          'extra' => array(
459
            'description' => 'Tests saving zero as a value.',
460
            'items' => "<Group 1>\noption 1-1|option 1-1\noption 1-2|option 1-2\noption 1-3|option 1-3\n<Group 2>\noption 2-1|option 2-1\noption 2-2|option 2-2\noption 2-3|option 2-3",
461
            'aslist' => 1,
462
          ),
463
          'required' => '1',
464
          'pid' => '0',
465
          'weight' => '-15',
466
        ),
467
        'sample values' => 'option 2-2',
468
        'database values' => array('option 2-2'),
469
        'database default values' => array('option 1-2'),
470
      ),
471
      'select_email' => array(
472
        'component' => array(
473
          'form_key' => 'select_email',
474
          'name' => 'Select e-mails',
475
          'type' => 'select',
476
          'value' => 'nate@localhost.localhost',
477
          'extra' => array(
478
            'items' => "nate@localhost.localhost|one\nadmin@localhost.localhost|two",
479
          ),
480
          'required' => '0',
481
          'pid' => '2',
482
          'weight' => '-17',
483
        ),
484
        'sample values' => 'admin@localhost.localhost',
485
        'database values' => array('admin@localhost.localhost'),
486
        'database default values' => array('nate@localhost.localhost'),
487
      ),
488
      'select_multiple' => array(
489
        'component' => array(
490
          'form_key' => 'select_multiple',
491
          'name' => 'Select Multiple',
492
          'type' => 'select',
493
          'value' => 'one,two',
494
          'extra' => array(
495
            'items' => "one|one\ntwo|two\nthree|three",
496
            'multiple' => 1,
497
            'aslist' => 1,
498
          ),
499
          'required' => '0',
500
          'pid' => '0',
501
          'weight' => '-10',
502
        ),
503
        // @todo: I'd like to test a value, but SimpleTest can't set multiple values.
504
        'sample values' => NULL,
505
        'database values' => array('one', 'two'),
506
        'database default values' => array('one', 'two'),
507
      ),
508
      'select_relative' => array(
509
        'component' => array(
510
          'form_key' => 'select_relative',
511
          'name' => 'Select relative',
512
          'type' => 'select',
513
          'value' => 'one',
514
          'extra' => array(
515
            'items' => "zero|Zero\none|One\ntwo|Two\nthree|Three\n",
516
            'aslist' => 1,
517
          ),
518
          'required' => '1',
519
          'pid' => '0',
520
          'weight' => '-9',
521
        ),
522
        'sample values' => 'one',
523
        'database values' => array('one'),
524
        'database default values' => array('one'),
525
        // Conditionals match against the 'sample values'.
526
        'match conditional values' => array(
527
          'equal' => 'one',
528
          'not_equal' => 'zero',
529
          'less_than' => 'two',
530
          'less_than_equal' => 'one',
531
          'greater_than' => 'zero',
532
          'greater_than_equal' => 'zero',
533
        ),
534
        'mismatch conditional values' => array(
535
          'equal' => 'zero',
536
          'not_equal' => 'one',
537
          'less_than' => 'one',
538
          'less_than_equal' => 'zero',
539
          'greater_than' => 'two',
540
          'greater_than_equal' => 'two',
541
        ),
542
      ),
543

    
544
      // Test date components.
545
      'date_textfield' => array(
546
        'component' => array(
547
          'form_key' => 'date_textfield',
548
          'name' => 'Date Textfield',
549
          'type' => 'date',
550
          'value' => 'Nov 19 1978',
551
          'extra' => array(
552
            'timezone' => 'site',
553
            'start_date' => '-100 years',
554
            'end_date' => '+2 years',
555
            'year_textfield' => 1,
556
          ),
557
          'required' => '1',
558
          'pid' => '0',
559
          'weight' => '-7',
560
        ),
561
        'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
562
        'database values' => array('1982-09-30'),
563
        'database default values' => array('1978-11-19'),
564
        // Conditionals match against the 'sample values'.
565
        'match conditional values' => array(
566
          'equal' => '1982/9/30',
567
          'before' => '1982/10/1',
568
          'before_equal' => '1982/9/30',
569
          'after' => '1982/9/29',
570
          'after_equal' => '1982/9/29',
571
        ),
572
        'mismatch conditional values' => array(
573
          'equal' => '1981/9/30',
574
          'before' => '1982/9/30',
575
          'before_equal' => '1982/9/29',
576
          'after' => '1982/9/30',
577
          'after_equal' => '1982/10/1',
578
        ),
579
      ),
580

    
581
      // Test email components.
582
      'email' => array(
583
        'component' => array(
584
          'form_key' => 'email',
585
          'name' => 'E-mail',
586
          'type' => 'email',
587
          'value' => '[current-user:mail]',
588
          'required' => '0',
589
          'extra' => array(
590
            // SimpleTest does not support type="email" input fields.
591
            'attributes' => array('type' => 'text'),
592
          ),
593
          'pid' => '0',
594
          'weight' => '-5',
595
        ),
596
        'sample values' => 'admin@localhost.localhost',
597
        'database values' => array('admin@localhost.localhost'),
598
        'database default values' => array($this->webform_users['admin']->mail),
599
        // Conditionals match against the 'sample values'.
600
        'match conditional values' => array(
601
          'equal' => 'admin@localhost.localhost',
602
          'not_equal' => '',
603
          'contains' => 'admin',
604
          'does_not_contain' => 'foo',
605
          'begins_with' => 'admin',
606
          'ends_with' => 'localhost',
607
          'not_empty' => TRUE,
608
        ),
609
        'mismatch conditional values' => array(
610
          'equal' => 'foo@localhost.localhost',
611
          'not_equal' => 'admin@localhost.localhost',
612
          'contains' => 'foo',
613
          'does_not_contain' => 'admin',
614
          'begins_with' => 'localhost',
615
          'ends_with' => 'admin',
616
          'empty' => TRUE,
617
        ),
618
      ),
619

    
620
      // Test hidden components.
621
      'hidden' => array(
622
        'component' => array(
623
          'form_key' => 'hidden',
624
          'name' => 'Hidden',
625
          'type' => 'hidden',
626
          'value' => 'default hidden value',
627
          'required' => '1',
628
          'pid' => '0',
629
          'weight' => '-4',
630
        ),
631
        'sample values' => NULL,
632
        'database values' => array('default hidden value'),
633
        'database default values' => array('default hidden value'),
634
        // Conditionals match against the 'sample values'.
635
        'match conditional values' => array(
636
          'equal' => 'default hidden value',
637
          'not_equal' => '',
638
          'contains' => 'hidden',
639
          'does_not_contain' => 'foo',
640
          'begins_with' => 'default',
641
          'ends_with' => 'value',
642
          'not_empty' => TRUE,
643
        ),
644
        'mismatch conditional values' => array(
645
          'equal' => '',
646
          'not_equal' => 'default hidden value',
647
          'contains' => 'foo',
648
          'does_not_contain' => 'hidden',
649
          'begins_with' => 'value',
650
          'ends_with' => 'default',
651
          'empty' => TRUE,
652
        ),
653
      ),
654

    
655
      // Test textarea components.
656
      'textarea' => array(
657
        'component' => array(
658
          'form_key' => 'textarea',
659
          'name' => 'Textarea',
660
          'type' => 'textarea',
661
          'value' => 'sample textarea default value',
662
          'extra' => array(),
663
          'required' => '0',
664
          'pid' => '0',
665
          'weight' => '15',
666
        ),
667
        'sample values' => 'sample textarea value',
668
        'database values' => array('sample textarea value'),
669
        'database default values' => array('sample textarea default value'),
670
        // Conditionals match against the 'sample values'.
671
        'match conditional values' => array(
672
          'equal' => 'sample textarea value',
673
          'not_equal' => '',
674
          'contains' => 'sample',
675
          'does_not_contain' => 'foo',
676
          'begins_with' => 'sample',
677
          'ends_with' => 'value',
678
          'not_empty' => TRUE,
679
        ),
680
        'mismatch conditional values' => array(
681
          'equal' => '',
682
          'not_equal' => 'sample textarea value',
683
          'contains' => 'foo',
684
          'does_not_contain' => 'sample',
685
          'begins_with' => 'value',
686
          'ends_with' => 'sample',
687
          'empty' => TRUE,
688
        ),
689
      ),
690

    
691
      // Test textfield components.
692
      'textfield' => array(
693
        'component' => array(
694
          'form_key' => 'textfield',
695
          'name' => 'Textfield',
696
          'type' => 'textfield',
697
          'value' => '',
698
          'required' => '0',
699
          'pid' => '0',
700
          'weight' => '-14',
701
        ),
702
        'sample values' => '',
703
        'database values' => array(''),
704
        'database default values' => array(''),
705
      ),
706
      'textfield_disabled' => array(
707
        'component' => array(
708
          'form_key' => 'textfield_disabled',
709
          'name' => 'Textfield Disabled',
710
          'type' => 'textfield',
711
          'value' => '[current-page:query:foo]',
712
          'extra' => array(
713
            'disabled' => 1,
714
          ),
715
          'required' => '0',
716
          'pid' => '0',
717
          'weight' => '-15',
718
        ),
719
        // Manually hard-code the input if token is not available.
720
        // @todo: Update after http://drupal.org/node/1347790 is finished.
721
        'sample values' => module_exists('token') ? NULL : 'bar',
722
        'database values' => array('bar'),
723
        'database default values' => module_exists('token') ? array('bar') : array(''),
724
      ),
725
      'textfield_profile' => array(
726
        'component' => array(
727
          'form_key' => 'textfield_profile',
728
          'name' => 'Textfield Profile',
729
          'type' => 'textfield',
730
          'value' => '[current-user:gender]',
731
          'extra' => array(
732
            'width' => '20',
733
          ),
734
          'required' => '0',
735
          'pid' => '0',
736
          'weight' => '-6',
737
        ),
738
        'sample values' => 'Female',
739
        'database values' => array('Female'),
740
        // The default value will be blank if token does not exist.
741
        // @todo: Update after http://drupal.org/node/1347790 is finished.
742
        'database default values' => module_exists('token') ? array($this->webform_users['admin']->gender[LANGUAGE_NONE][0]['value']) : array(''),
743
      ),
744

    
745
      // Test time components.
746
      'time' => array(
747
        'component' => array(
748
          'form_key' => 'time',
749
          'name' => 'Time',
750
          'type' => 'time',
751
          'value' => '10:30pm',
752
          'extra' => array(
753
            'timezone' => 'site',
754
            'hourformat' => '12-hour',
755
          ),
756
          'required' => '0',
757
          'pid' => '0',
758
          'weight' => '16',
759
        ),
760
        'sample values' => array('hour' => '12', 'minute' => '0', 'ampm' => 'pm'),
761
        'database values' => array('12:00:00'),
762
        'database default values' => array('22:30:00'),
763
        // Conditionals match against the 'sample values'.
764
        'match conditional values' => array(
765
          'equal' => '12:00pm',
766
          'before' => '1:00pm',
767
          'before_equal' => '12:00pm',
768
          'after' => '11:00am',
769
          'after_equal' => '11:00am',
770
        ),
771
        'mismatch conditional values' => array(
772
          'equal' => '12:00am',
773
          'before' => '12:00pm',
774
          'before_equal' => '11:00am',
775
          'after' => '12:00pm',
776
        ),
777
      ),
778
      'time_24h' => array(
779
        'component' => array(
780
          'form_key' => 'time_24h',
781
          'name' => 'Time 24H',
782
          'type' => 'time',
783
          'value' => '10:30pm',
784
          'extra' => array(
785
            'timezone' => 'site',
786
            'hourformat' => '24-hour',
787
          ),
788
          'required' => '0',
789
          'pid' => '0',
790
          'weight' => '17',
791
        ),
792
        'sample values' => array('hour' => '5', 'minute' => '0'),
793
        'database values' => array('05:00:00'),
794
        'database default values' => array('22:30:00'),
795
        // Conditionals match against the 'sample values'.
796
        'match conditional values' => array(
797
          'equal' => '5:00',
798
          'before' => '24:00',
799
          'before_equal' => '5:00',
800
          'after' => '00:00',
801
          'after_equal' => '00:00',
802
        ),
803
        'mismatch conditional values' => array(
804
          'equal' => '5:01',
805
          'before' => '5:00',
806
          'before_equal' => '4:59',
807
          'after' => '5:00',
808
          'after_equal' => '5:01',
809
        ),
810
      ),
811

    
812
      // Test number components.
813
      'integer' => array(
814
        'component' => array(
815
          'form_key' => 'integer',
816
          'name' => 'Integer',
817
          'type' => 'number',
818
          'value' => '1',
819
          'extra' => array(
820
            'type' => 'textfield',
821
            'integer' => 1,
822
            'max' => '100',
823
            // SimpleTest does not support type="number" input fields.
824
            'attributes' => array('type' => 'text'),
825
          ),
826
          'required' => '0',
827
          'pid' => '0',
828
          'weight' => '18',
829
        ),
830
        'sample values' => '2',
831
        'database values' => array('2'),
832
        'database default values' => array('1'),
833
        // Conditionals match against the 'sample values'.
834
        'match conditional values' => array(
835
          'equal' => '2',
836
          'not_equal' => '0',
837
          'less_than' => '3',
838
          'less_than_equal' => '2',
839
          'greater_than' => '1',
840
          'greater_than_equal' => '1',
841
          'not_empty' => TRUE,
842
        ),
843
        'mismatch conditional values' => array(
844
          'equal' => '0',
845
          'not_equal' => '2',
846
          'less_than' => '2',
847
          'less_than_equal' => '1',
848
          'greater_than' => '2',
849
          'greater_than_equal' => '3',
850
          'empty' => TRUE,
851
        ),
852
        'error values' => array(
853
          '1.5' => t('!name field value of @value must be an integer.', array('!name' => 'Integer', '@value' => '1.5')),
854
          '101' => t('!name field value must be less than @max.', array('!name' => 'Integer', '@max' => '100')),
855
        ),
856
      ),
857
      'integer_range' => array(
858
        'component' => array(
859
          'form_key' => 'integer_range',
860
          'name' => 'Integer Range',
861
          'type' => 'number',
862
          'value' => '50',
863
          'extra' => array(
864
            'type' => 'select',
865
            'min' => '10',
866
            'max' => '50',
867
            'step' => 5,
868
            'integer' => 1,
869
          ),
870
          'required' => '0',
871
          'pid' => '0',
872
          'weight' => '19',
873
        ),
874
        'sample values' => '10',
875
        'database values' => array('10'),
876
        'database default values' => array('50'),
877
      ),
878
      'decimal_positive' => array(
879
        'component' => array(
880
          'form_key' => 'decimal_positive',
881
          'name' => 'Decimal positive',
882
          'type' => 'number',
883
          'value' => '1',
884
          'extra' => array(
885
            'type' => 'textfield',
886
            'field_prefix' => '$',
887
            'field_suffix' => 'lbs',
888
            'min' => '0',
889
            'decimals' => '2',
890
            'point' => '.',
891
            'separator' => ',',
892
            // SimpleTest does not support type="number" input fields.
893
            'attributes' => array('type' => 'text'),
894
          ),
895
          'required' => '0',
896
          'pid' => '0',
897
          'weight' => '20',
898
        ),
899
        'sample values' => '2.00',
900
        'database values' => array('2.00'),
901
        'database default values' => array('1'),
902
        // Conditionals match against the 'sample values'.
903
        'match conditional values' => array(
904
          'equal' => '2',
905
          'not_equal' => '0',
906
          'less_than' => '3.000',
907
          'greater_than' => '1.000',
908
          'not_empty' => TRUE,
909
        ),
910
        'mismatch conditional values' => array(
911
          'equal' => '0',
912
          'not_equal' => '2',
913
          'less_than' => '2.0',
914
          'greater_than' => '2.00',
915
          'empty' => TRUE,
916
        ),
917
        'error values' => array(
918
          '-1' => t('!name field value must be greater than @min.', array('!name' => 'Decimal positive', '@min' => '0')),
919
        ),
920
      ),
921
      'decimal_range' => array(
922
        'component' => array(
923
          'form_key' => 'decimal_range',
924
          'name' => 'Decimal range',
925
          'type' => 'number',
926
          'value' => '1',
927
          'extra' => array(
928
            'type' => 'textfield',
929
            'field_prefix' => '$',
930
            'field_suffix' => 'lbs',
931
            'min' => '1',
932
            'max' => '12',
933
            'step' => '1.5',
934
            // SimpleTest does not support type="number" input fields.
935
            'attributes' => array('type' => 'text'),
936
          ),
937
          'required' => '0',
938
          'pid' => '0',
939
          'weight' => '21',
940
        ),
941
        'sample values' => '11.5',
942
        'database values' => array('11.5'),
943
        'database default values' => array('1'),
944
        'error values' => array(
945
          '2' => t('!name field value must be @start plus a multiple of @step.', array('!name' => 'Decimal range', '@start' => '1', '@step' => '1.5')),
946
          '13' => t('!name field value of @value should be in the range @min to @max.', array('!name' => 'Decimal range', '@value' => '13', '@min' => '1', '@max' => '12')),
947
        ),
948
      ),
949
      'decimal_range_select' => array(
950
        'component' => array(
951
          'form_key' => 'decimal_range_select',
952
          'name' => 'Decimal range select',
953
          'type' => 'number',
954
          'value' => '1',
955
          'extra' => array(
956
            'type' => 'select',
957
            'field_prefix' => '$',
958
            'field_suffix' => 'lbs',
959
            'min' => '1',
960
            'max' => '12',
961
            'step' => '1.5',
962
          ),
963
          'required' => '0',
964
          'pid' => '0',
965
          'weight' => '21',
966
        ),
967
        'sample values' => '10',
968
        'database values' => array('10'),
969
        'database default values' => array('1'),
970
        // Conditionals match against the 'sample values'.
971
        'match conditional values' => array(
972
          'equal' => '10',
973
          'not_equal' => '2.5',
974
          'less_than' => '11.5',
975
          'greater_than' => '1',
976
        ),
977
        'mismatch conditional values' => array(
978
          'equal' => '2.5',
979
          'not_equal' => '10',
980
          'less_than' => '10',
981
          'greater_than' => '11.5',
982
        ),
983
      ),
984
    );
985

    
986
    return $this->_webform_components;
987
  }
988

    
989
  function webformForm() {
990
    if (isset($this->_webform_node)) {
991
      return $this->_webform_node;
992
    }
993

    
994
    $settings = array(
995
     'type' => 'webform',
996
     'language'  => LANGUAGE_NONE,
997
     'uid' => '1',
998
     'status' => '1',
999
     'promote' => '1',
1000
     'moderate' => '0',
1001
     'sticky' => '0',
1002
     'tnid' => '0',
1003
     'translate' => '0',
1004
     'title' => 'Test Webform',
1005
     'log' => '',
1006
     'format' => '1',
1007
     'webform' => array(
1008
        'confirmation' => 'Thanks!',
1009
      ) + webform_node_defaults(),
1010
    );
1011

    
1012
    $cid = 0;
1013
    foreach ($this->webformComponents() as $key => $component_info) {
1014
      $cid++;
1015
      $settings['webform']['components'][$cid] = $component_info['component'];
1016
      $settings['webform']['components'][$cid]['cid'] = $cid;
1017
      $settings['webform']['components'][$cid]['pid'] = 0;
1018
    }
1019

    
1020
    $this->_webform_node = $this->drupalCreateNode($settings);
1021

    
1022
    return $this->_webform_node;
1023
  }
1024

    
1025
  /**
1026
   * Generate a list of all values that would result in a valid submission.
1027
   *
1028
   * @param $input_values
1029
   *   An array of input values keyed by the component form key. If none
1030
   *   are specified, the defaults will be pulled from webformComponents().
1031
   */
1032
  function webformPost($input_values = NULL) {
1033
    $edit = array();
1034

    
1035
    if (empty($input_values)) {
1036
      $input_values = array();
1037
      foreach ($this->webformComponents() as $key => $component_info) {
1038
        $input_values[$key] = $component_info['sample values'];
1039
      }
1040
    }
1041

    
1042
    foreach ($input_values as $key => $values) {
1043
      if (is_array($values)) {
1044
        foreach ($values as $subkey => $value) {
1045
          $edit["submitted[$key][$subkey]"] = $value;
1046
        }
1047
      }
1048
      elseif ($values != NULL) {
1049
        $value = $values;
1050
        // Multiple selects have a funky extra empty bracket in the name.
1051
        $extra = $key == 'select_multiple' ? '[]' : '';
1052
        $edit["submitted[$key]$extra"] = $value;
1053
      }
1054
    }
1055
    return $edit;
1056
  }
1057

    
1058
  /**
1059
   * Utility function to print out the current page being tested.
1060
   */
1061
  function webformPrintPage() {
1062
    $this->verbose($this->drupalGetContent());
1063
  }
1064
}