Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module tests.
6
 */
7

    
8
/**
9
 *
10
 */
11
class WebformTestCase extends DrupalWebTestCase {
12
  private $_webform_node;
13
  private $_webform_components;
14
  public $webform_users;
15

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

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

    
48
    // Create a normal user that can view their own submissions.
49
    $permissions['userAccess'] = array(
50
      'access content',
51
      'access own webform submissions',
52
    );
53

    
54
    // Create a normal user than can edit their own submissions.
55
    $permissions['userEdit'] = array(
56
      'access content',
57
      'edit own webform submissions',
58
    );
59

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

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

    
79
    foreach ($permissions as $user_key => $role_permissions) {
80
      $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
81
      $this->webform_users[$user_key]->gender = array(LANGUAGE_NONE => array(array('value' => 'Female')));
82
      user_save($this->webform_users[$user_key]);
83
    }
84
  }
85

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

    
103
    parent::tearDown();
104
  }
105

    
106
  /**
107
   *
108
   */
109
  public function webformReset() {
110
    $this->_webform_node = NULL;
111
    $this->_webform_components = NULL;
112
  }
113

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

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

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

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

    
556
      // Test date components.
557
      'date_textfield' => array(
558
        'component' => array(
559
          'form_key' => 'date_textfield',
560
          'name' => 'Date Textfield',
561
          'type' => 'date',
562
          'value' => 'Nov 19 1978',
563
          'extra' => array(
564
            'timezone' => 'site',
565
            'start_date' => '-100 years',
566
            'end_date' => '+2 years',
567
            'year_textfield' => 1,
568
          ),
569
          'required' => '1',
570
          'pid' => '0',
571
          'weight' => '-7',
572
        ),
573
        'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
574
        'database values' => array('1982-09-30'),
575
        'database default values' => array('1978-11-19'),
576
        // Conditionals match against the 'sample values'.
577
        'match conditional values' => array(
578
          'equal' => '1982/9/30',
579
          'before' => '1982/10/1',
580
          'before_equal' => '1982/9/30',
581
          'after' => '1982/9/29',
582
          'after_equal' => '1982/9/29',
583
        ),
584
        'mismatch conditional values' => array(
585
          'equal' => '1981/9/30',
586
          'before' => '1982/9/30',
587
          'before_equal' => '1982/9/29',
588
          'after' => '1982/9/30',
589
          'after_equal' => '1982/10/1',
590
        ),
591
      ),
592

    
593
      // Test email components.
594
      'email' => array(
595
        'component' => array(
596
          'form_key' => 'email',
597
          'name' => 'E-mail',
598
          'type' => 'email',
599
          'value' => '[current-user:mail]',
600
          'required' => '0',
601
          'extra' => array(
602
            // SimpleTest does not support type="email" input fields.
603
            'attributes' => array('type' => 'text'),
604
          ),
605
          'pid' => '0',
606
          'weight' => '-5',
607
        ),
608
        'sample values' => 'admin@localhost.localhost',
609
        'database values' => array('admin@localhost.localhost'),
610
        'database default values' => array($this->webform_users['admin']->mail),
611
        // Conditionals match against the 'sample values'.
612
        'match conditional values' => array(
613
          'equal' => 'admin@localhost.localhost',
614
          'not_equal' => '',
615
          'contains' => 'admin',
616
          'does_not_contain' => 'foo',
617
          'begins_with' => 'admin',
618
          'ends_with' => 'localhost',
619
          'not_empty' => TRUE,
620
        ),
621
        'mismatch conditional values' => array(
622
          'equal' => 'foo@localhost.localhost',
623
          'not_equal' => 'admin@localhost.localhost',
624
          'contains' => 'foo',
625
          'does_not_contain' => 'admin',
626
          'begins_with' => 'localhost',
627
          'ends_with' => 'admin',
628
          'empty' => TRUE,
629
        ),
630
      ),
631

    
632
      // Test hidden components.
633
      'hidden' => array(
634
        'component' => array(
635
          'form_key' => 'hidden',
636
          'name' => 'Hidden',
637
          'type' => 'hidden',
638
          'value' => 'default hidden value',
639
          'required' => '1',
640
          'pid' => '0',
641
          'weight' => '-4',
642
        ),
643
        'sample values' => NULL,
644
        'database values' => array('default hidden value'),
645
        'database default values' => array('default hidden value'),
646
        // Conditionals match against the 'sample values'.
647
        'match conditional values' => array(
648
          'equal' => 'default hidden value',
649
          'not_equal' => '',
650
          'contains' => 'hidden',
651
          'does_not_contain' => 'foo',
652
          'begins_with' => 'default',
653
          'ends_with' => 'value',
654
          'not_empty' => TRUE,
655
        ),
656
        'mismatch conditional values' => array(
657
          'equal' => '',
658
          'not_equal' => 'default hidden value',
659
          'contains' => 'foo',
660
          'does_not_contain' => 'hidden',
661
          'begins_with' => 'value',
662
          'ends_with' => 'default',
663
          'empty' => TRUE,
664
        ),
665
      ),
666

    
667
      // Test textarea components.
668
      'textarea' => array(
669
        'component' => array(
670
          'form_key' => 'textarea',
671
          'name' => 'Textarea',
672
          'type' => 'textarea',
673
          'value' => 'sample textarea default value',
674
          'extra' => array(),
675
          'required' => '0',
676
          'pid' => '0',
677
          'weight' => '15',
678
        ),
679
        'sample values' => 'sample textarea value',
680
        'database values' => array('sample textarea value'),
681
        'database default values' => array('sample textarea default value'),
682
        // Conditionals match against the 'sample values'.
683
        'match conditional values' => array(
684
          'equal' => 'sample textarea value',
685
          'not_equal' => '',
686
          'contains' => 'sample',
687
          'does_not_contain' => 'foo',
688
          'begins_with' => 'sample',
689
          'ends_with' => 'value',
690
          'not_empty' => TRUE,
691
        ),
692
        'mismatch conditional values' => array(
693
          'equal' => '',
694
          'not_equal' => 'sample textarea value',
695
          'contains' => 'foo',
696
          'does_not_contain' => 'sample',
697
          'begins_with' => 'value',
698
          'ends_with' => 'sample',
699
          'empty' => TRUE,
700
        ),
701
      ),
702

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

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

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

    
998
    return $this->_webform_components;
999
  }
1000

    
1001
  /**
1002
   *
1003
   */
1004
  public function webformForm() {
1005
    if (isset($this->_webform_node)) {
1006
      return $this->_webform_node;
1007
    }
1008

    
1009
    $settings = array(
1010
      'type' => 'webform',
1011
      'language'  => LANGUAGE_NONE,
1012
      'uid' => '1',
1013
      'status' => '1',
1014
      'promote' => '1',
1015
      'moderate' => '0',
1016
      'sticky' => '0',
1017
      'tnid' => '0',
1018
      'translate' => '0',
1019
      'title' => 'Test Webform',
1020
      'log' => '',
1021
      'format' => '1',
1022
      'webform' => array(
1023
        'confirmation' => 'Thanks!',
1024
      ) + webform_node_defaults(),
1025
    );
1026

    
1027
    $cid = 0;
1028
    foreach ($this->webformComponents() as $key => $component_info) {
1029
      $cid++;
1030
      $settings['webform']['components'][$cid] = $component_info['component'];
1031
      $settings['webform']['components'][$cid]['cid'] = $cid;
1032
      $settings['webform']['components'][$cid]['pid'] = 0;
1033
    }
1034

    
1035
    $this->_webform_node = $this->drupalCreateNode($settings);
1036

    
1037
    return $this->_webform_node;
1038
  }
1039

    
1040
  /**
1041
   * Generate a list of all values that would result in a valid submission.
1042
   *
1043
   * @param $input_values
1044
   *   An array of input values keyed by the component form key. If none
1045
   *   are specified, the defaults will be pulled from webformComponents().
1046
   */
1047
  public function webformPost($input_values = NULL) {
1048
    $edit = array();
1049

    
1050
    if (empty($input_values)) {
1051
      $input_values = array();
1052
      foreach ($this->webformComponents() as $key => $component_info) {
1053
        $input_values[$key] = $component_info['sample values'];
1054
      }
1055
    }
1056

    
1057
    foreach ($input_values as $key => $values) {
1058
      if (is_array($values)) {
1059
        foreach ($values as $subkey => $value) {
1060
          $edit["submitted[$key][$subkey]"] = $value;
1061
        }
1062
      }
1063
      elseif ($values != NULL) {
1064
        $value = $values;
1065
        // Multiple selects have a funky extra empty bracket in the name.
1066
        $extra = $key == 'select_multiple' ? '[]' : '';
1067
        $edit["submitted[$key]$extra"] = $value;
1068
      }
1069
    }
1070
    return $edit;
1071
  }
1072

    
1073
  /**
1074
   * Utility function to print out the current page being tested.
1075
   */
1076
  public function webformPrintPage() {
1077
    $this->verbose($this->drupalGetContent());
1078
  }
1079

    
1080
}