Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / tests / webform.test @ e9f59ce0

1
<?php
2

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

    
8
class WebformTestCase extends DrupalWebTestCase {
9
  private $_webform_node;
10
  private $_webform_components;
11
  public $webform_users;
12

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

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

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

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

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

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

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

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

    
100
    parent::tearDown();
101
  }
102

    
103
  /**
104
   *
105
   */
106
  function webformReset() {
107
    $this->_webform_node = NULL;
108
    $this->_webform_components = NULL;
109
  }
110

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

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

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

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

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

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

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

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

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

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

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

    
990
    return $this->_webform_components;
991
  }
992

    
993
  function testWebformForm() {
994
    if (isset($this->_webform_node)) {
995
      return $this->_webform_node;
996
    }
997

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

    
1016
    $cid = 0;
1017
    foreach ($this->testWebformComponents() as $key => $component_info) {
1018
      $cid++;
1019
      $settings['webform']['components'][$cid] = $component_info['component'];
1020
      $settings['webform']['components'][$cid]['cid'] = $cid;
1021
      $settings['webform']['components'][$cid]['pid'] = 0;
1022
    }
1023

    
1024
    $this->_webform_node = $this->drupalCreateNode($settings);
1025

    
1026
    return $this->_webform_node;
1027
  }
1028

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

    
1039
    if (empty($input_values)) {
1040
      $input_values = array();
1041
      foreach ($this->testWebformComponents() as $key => $component_info) {
1042
        $input_values[$key] = $component_info['sample values'];
1043
      }
1044
    }
1045

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

    
1062
  /**
1063
   * Utility function to print out the current page being tested.
1064
   */
1065
  function webformPrintPage() {
1066
    $this->verbose($this->drupalGetContent());
1067
  }
1068
}
1069

    
1070
/**
1071
 * Test general functionality of Webform.
1072
 */
1073
class WebformGeneralTestCase extends WebformTestCase {
1074
  /**
1075
   * Implements getInfo().
1076
   */
1077
  public static function getInfo() {
1078
    return array(
1079
      'name' => t('Webform'),
1080
      'description' => t('Checks global Webform settings and content types.'),
1081
      'group' => t('Webform'),
1082
    );
1083
  }
1084

    
1085
  /**
1086
   * Test creating a new Webform node.
1087
   */
1088
  function testWebformCreate() {
1089
    $settings = array(
1090
      'title' => 'Test webform, no components',
1091
      'type' => 'webform',
1092
    );
1093
    $node = $this->drupalCreateNode($settings);
1094

    
1095
    // Because this is a "webform" type node, it should have an entry in the
1096
    // database even though it's using the default settings.
1097
    $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record made in the database for the new webform node.'));
1098

    
1099
    // Make a change to the node, ensure that the record stays intact.
1100
    $node->title .= '!';
1101
    node_save($node);
1102
    $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record still in the database after modifying webform node.'));
1103
  }
1104

    
1105
  /**
1106
   * Test webform-enabling a different node type and testing behavior.
1107
   */
1108
  function testWebformCreateNewType() {
1109
    // Enable webforms on the page content type.
1110
    variable_set('webform_node_webform', TRUE);
1111
    variable_set('webform_node_page', TRUE);
1112

    
1113
    $settings = array(
1114
      'title' => 'Test webform-enabled page',
1115
      'type' => 'page',
1116
    );
1117
    $node = $this->drupalCreateNode($settings);
1118

    
1119
    // Because this is a webform-enabled type node but does not yet have any
1120
    // components, it should not have an entry in the database because it is
1121
    // using the default settings.
1122
    $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record not in the database for the new page node.'));
1123

    
1124
    // Make a change to the node, ensure that the record stays empty.
1125
    $node->title .= '!';
1126
    node_save($node);
1127
    $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record still not in the database after modifying page node.'));
1128

    
1129
    // Add a new component to the node and check that a record is made in the
1130
    // webform table.
1131
    $components = $this->testWebformComponents();
1132
    $textarea = $components['textarea'];
1133
    $textarea['type'] = 'textarea';
1134
    $textarea['form_key'] = 'textarea';
1135
    $textarea['cid'] = 1;
1136
    $textarea['pid'] = 0;
1137
    $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
1138
    $node->webform['components'][1] = $textarea;
1139
    node_save($node);
1140
    $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record now exists after adding a new component.'));
1141

    
1142
    // Remove the new component and ensure that the record is deleted.
1143
    $node->webform['components'] = array();
1144
    node_save($node);
1145
    $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record deleted after deleting last component.'));
1146
  }
1147

    
1148
  function webformRecordExists($nid) {
1149
    return (bool) db_query("SELECT nid FROM {webform} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
1150
  }
1151
}