Projet

Général

Profil

Paste
Télécharger (27,4 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / webform.test @ 13755f8d

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() {
17
    // Enable Webform.
18
    parent::setUp('webform', 'profile');
19

    
20
    // Create a profile field to test %profile tokens.
21
    db_query("INSERT INTO {profile_field} (title, name, explanation, category, type, weight, required, register, visibility, autocomplete, options, page) VALUES ('Gender', 'profile_gender', '', 'Profile', 'textfield', 0, 0, 0, 2, 0, '', '')");
22

    
23
    // Create a normal user that can view their own submissions.
24
    $permissions['userAccess'] = array(
25
      'access content',
26
      'access own webform submissions',
27
    );
28

    
29
    // Create a normal user than can edit their own submissions.
30
    $permissions['userEdit'] = array(
31
      'access content',
32
      'edit own webform submissions',
33
    );
34

    
35
    // Create a webform editor to test creating and editing own content.
36
    $permissions['editor'] = array(
37
      'access content',
38
      'create webform content',
39
      'edit own webform content',
40
      'access all webform results',
41
    );
42

    
43
    // Create a webform admin that will do all node creation.
44
    $permissions['admin'] = array(
45
      'access content',
46
      'administer nodes',
47
      'create webform content',
48
      'edit any webform content',
49
      'access all webform results',
50
      'edit all webform submissions',
51
      'delete all webform submissions',
52
    );
53

    
54
    foreach ($permissions as $user_key => $role_permissions) {
55
      $this->webform_users[$user_key] = $this->drupalCreateUser($role_permissions);
56
      $profile = array('profile_gender' => 'Female');
57
      $this->webform_users[$user_key]->profile_gender = 'Female';
58
      profile_save_profile($profile, $this->webform_users[$user_key], 'Profile');
59
    }
60
  }
61

    
62
  /**
63
   * Implemenation of tearDown().
64
   */
65
  function tearDown() {
66
    // Delete the webform admin and any created nodes.
67
    foreach ($this->webform_users as $account) {
68
      $uid = $account->uid;
69
      $result = db_select('node')
70
        ->fields('node')
71
        ->condition('uid', $uid)
72
        ->execute();
73
      foreach ($result as $node) {
74
        node_delete($node->nid);
75
      }
76
      user_cancel(array(), $uid, 'user_cancel_delete');
77
    }
78

    
79
    parent::tearDown();
80
  }
81

    
82
  /**
83
   *
84
   */
85
  function webformReset() {
86
    $this->_webform_node = NULL;
87
    $this->_webform_components = NULL;
88
  }
89

    
90
  /**
91
   * Provide a list of components to test throughout the suite.
92
   *
93
   * Each component provides:
94
   *   - A default configuration for the component.
95
   *   - Values to try setting via POST
96
   *   - Values that should match the database storage when set via POST
97
   *   - Values that should match the database storage when using the default values.
98
   *
99
   * @return array
100
   *   An array of each component settings.
101
   */
102
  function testWebformComponents() {
103
    if (isset($this->_webform_components)) {
104
      return $this->_webform_components;
105
    }
106

    
107
    $this->_webform_components = array(
108
      // Test date components.
109
      'date' => array(
110
        'component' => array(
111
          'form_key' => 'date',
112
          'name' => 'Date',
113
          'type' => 'date',
114
          'value' => '19 Nov 1978',
115
          'extra' => array(
116
            'timezone' => 'site',
117
            'start_date' => '-100 years',
118
            'end_date' => '+2 years',
119
          ),
120
          'mandatory' => '0',
121
          'pid' => '0',
122
          'weight' => '-15',
123
        ),
124
        'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
125
        'database values' => array('1982-09-30'),
126
        'database default values' => array('1978-11-19'),
127
      ),
128

    
129
      // Test grid components.
130
      'grid' => array(
131
        'component' => array(
132
          'form_key' => 'grid',
133
          'name' => 'Grid',
134
          'type' => 'grid',
135
          'value' => '',
136
          'extra' => array(
137
            'questions' => "0|Ålphå\n1|ıé†å\n2|Î鬆å", // Left side
138
            'options' => "0|øne\n1|twö\n2|ǼBƇ\n3|€Euro", // Top
139
          ),
140
          'mandatory' => '0',
141
          'pid' => '2',
142
          'weight' => '-19',
143
        ),
144
        'sample values' => array('0' => '0', '1' => '1', '2' => '2'),
145
        'database values' => array('0' => '0', '1' => '1', '2' => '2'),
146
        'database default values' => array('', '', ''),
147
      ),
148
      'grid_keyed' => array(
149
        'component' => array(
150
          'form_key' => 'grid_keyed',
151
          'name' => 'Grid Keyed',
152
          'type' => 'grid',
153
          'value' => '',
154
          'extra' => array(
155
            'questions' => "one|What's your option?\ntwo|Agåin?\nthree|One more time!", // Left side.
156
            'options' => "one|Option one\ntwo|Option 2\nthree| Three is me", // Top
157
          ),
158
          'mandatory' => '0',
159
          'pid' => '0',
160
          'weight' => '-15',
161
        ),
162
        'sample values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
163
        'database values' => array('one' => 'one', 'two' => 'two', 'three' => 'three'),
164
        'database default values' => array('one' => '', 'two' => '', 'three' => ''),
165
      ),
166

    
167
      // Test select components.
168
      'checkboxes' => array(
169
        'component' => array(
170
          'form_key' => 'checkboxes',
171
          'name' => 'Checkboxes',
172
          'type' => 'select',
173
          'value' => 'two',
174
          'extra' => array(
175
            'items' => "one|one\ntwo|two\nthree|three",
176
            'multiple' => 1,
177
          ),
178
          'mandatory' => '0',
179
          'pid' => '0',
180
          'weight' => '-15',
181
        ),
182
        'sample values' => array('one' => TRUE, 'two' => FALSE, 'three' => TRUE),
183
        'database values' => array('one', 'three'),
184
        'database default values' => array('two'),
185
      ),
186
      'checkboxes_zero' => array(
187
        'component' => array(
188
          'form_key' => 'checkboxes_zero',
189
          'name' => 'Checkboxes zero',
190
          'type' => 'select',
191
          'value' => '0',
192
          'extra' => array(
193
            'items' => "0|zero\n1|one\n2|two",
194
            'multiple' => 1,
195
          ),
196
          'mandatory' => '1',
197
          'pid' => '0',
198
          'weight' => '-9',
199
        ),
200
        'sample values' => array('0' => TRUE),
201
        'database values' => array('0'),
202
        'database default values' => array('0'),
203
      ),
204
      'radios' => array(
205
        'component' => array(
206
          'form_key' => 'radios',
207
          'name' => 'Radios',
208
          'type' => 'select',
209
          'value' => 'two',
210
          'extra' => array(
211
            'items' => "one|one\ntwo|two\nthree|three",
212
          ),
213
          'mandatory' => '1',
214
          'pid' => '0',
215
          'weight' => '-9',
216
        ),
217
        'sample values' => 'one',
218
        'database values' => array('one'),
219
        'database default values' => array('two'),
220
      ),
221
      'radios_zero' => array(
222
        'component' => array(
223
          'form_key' => 'radios_zero',
224
          'name' => 'Radios zero',
225
          'type' => 'select',
226
          'value' => '0',
227
          'extra' => array(
228
            'items' => "0|zero\n1|one\n2|two",
229
          ),
230
          'mandatory' => '1',
231
          'pid' => '0',
232
          'weight' => '-9',
233
        ),
234
        'sample values' => '0',
235
        'database values' => array('0'),
236
        'database default values' => array('0'),
237
      ),
238
      'select' => array(
239
        'component' => array(
240
          'form_key' => 'select',
241
          'name' => 'Select',
242
          'type' => 'select',
243
          'value' => 'one',
244
          'extra' => array(
245
            'description' => 'Description here',
246
            'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
247
            'aslist' => 1,
248
          ),
249
          'mandatory' => '1',
250
          'pid' => '0',
251
          'weight' => '-15',
252
        ),
253
        'sample values' => 'two',
254
        'database values' => array('two'),
255
        'database default values' => array('one'),
256
      ),
257
      'select_zero' => array(
258
        'component' => array(
259
          'form_key' => 'select_zero',
260
          'name' => 'Select zero',
261
          'type' => 'select',
262
          'value' => '0',
263
          'extra' => array(
264
            'description' => 'Tests saving zero as a value.',
265
            'items' => "0|zero\n1|one\n2|two",
266
            'aslist' => 1,
267
          ),
268
          'mandatory' => '1',
269
          'pid' => '0',
270
          'weight' => '-15',
271
        ),
272
        'sample values' => '0',
273
        'database values' => array('0'),
274
        'database default values' => array('0'),
275
      ),
276
      'select_no_default' => array(
277
        'component' => array(
278
          'form_key' => 'select_no_default',
279
          'name' => 'Select no default',
280
          'type' => 'select',
281
          'value' => '',
282
          'extra' => array(
283
            'description' => 'Description here',
284
            'items' => "one|one\ntwo|two\nthree|three\nfour|four\nfive|five\nsix|six",
285
            'aslist' => 1,
286
          ),
287
          'mandatory' => '0',
288
          'pid' => '0',
289
          'weight' => '-15',
290
        ),
291
        'sample values' => 'two',
292
        'database values' => array('two'),
293
        'database default values' => array(''),
294
      ),
295
      'select_no_default_zero' => array(
296
        'component' => array(
297
          'form_key' => 'select_no_default_zero',
298
          'name' => 'Select no default zero',
299
          'type' => 'select',
300
          'value' => '',
301
          'extra' => array(
302
            'description' => 'Tests saving zero as a value.',
303
            'items' => "0|zero\n1|one\n2|two",
304
            'aslist' => 1,
305
          ),
306
          'mandatory' => '0',
307
          'pid' => '0',
308
          'weight' => '-15',
309
        ),
310
        'sample values' => '0',
311
        'database values' => array('0'),
312
        'database default values' => array(''),
313
      ),
314
      'select_optgroup' => array(
315
        'component' => array(
316
          'form_key' => 'select_optgroup',
317
          'name' => 'Select Optgroup',
318
          'type' => 'select',
319
          'value' => 'option 1-2',
320
          'extra' => array(
321
            'description' => 'Tests saving zero as a value.',
322
            '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",
323
            'aslist' => 1,
324
          ),
325
          'mandatory' => '1',
326
          'pid' => '0',
327
          'weight' => '-15',
328
        ),
329
        'sample values' => 'option 2-2',
330
        'database values' => array('option 2-2'),
331
        'database default values' => array('option 1-2'),
332
      ),
333
      'select_email' => array(
334
        'component' => array(
335
          'form_key' => 'select_email',
336
          'name' => 'Select e-mails',
337
          'type' => 'select',
338
          'value' => 'nate@localhost.localhost',
339
          'extra' => array(
340
            'items' => "nate@localhost.localhost|one\nadmin@localhost.localhost|two",
341
          ),
342
          'mandatory' => '0',
343
          'pid' => '2',
344
          'weight' => '-17',
345
        ),
346
        'sample values' => 'admin@localhost.localhost',
347
        'database values' => array('admin@localhost.localhost'),
348
        'database default values' => array('nate@localhost.localhost'),
349
      ),
350
      'select_multiple' => array(
351
        'component' => array(
352
          'form_key' => 'select_multiple',
353
          'name' => 'Select Multiple',
354
          'type' => 'select',
355
          'value' => 'one,two',
356
          'extra' => array(
357
            'items' => "one|one\ntwo|two\nthree|three",
358
            'multiple' => 1,
359
            'aslist' => 1,
360
          ),
361
          'mandatory' => '0',
362
          'pid' => '0',
363
          'weight' => '-10',
364
        ),
365
        // TODO: I'd like to test a value, but SimpleTest can't set multiple values.
366
        'sample values' => NULL,
367
        'database values' => array('one', 'two'),
368
        'database default values' => array('one', 'two'),
369
      ),
370

    
371
      // Test date components.
372
      'date_textfield' => array(
373
        'component' => array(
374
          'form_key' => 'date_textfield',
375
          'name' => 'Date Textfield',
376
          'type' => 'date',
377
          'value' => 'Nov 19 1978',
378
          'extra' => array(
379
            'timezone' => 'site',
380
            'start_date' => '-100 years',
381
            'end_date' => '+2 years',
382
            'year_textfield' => 1,
383
          ),
384
          'mandatory' => '1',
385
          'pid' => '0',
386
          'weight' => '-7',
387
        ),
388
        'sample values' => array('day' => '30', 'month' => '9', 'year' => '1982'),
389
        'database values' => array('1982-09-30'),
390
        'database default values' => array('1978-11-19'),
391
      ),
392

    
393
      // Test email components.
394
      'email' => array(
395
        'component' => array(
396
          'form_key' => 'email',
397
          'name' => 'E-mail',
398
          'type' => 'email',
399
          'value' => '%useremail',
400
          'mandatory' => '0',
401
          'extra' => array(
402
            // SimpleTest does not support type="email" input fields.
403
            'attributes' => array('type' => 'text'),
404
          ),
405
          'pid' => '0',
406
          'weight' => '-5',
407
        ),
408
        'sample values' => 'admin@localhost.localhost',
409
        'database values' => array('admin@localhost.localhost'),
410
        'database default values' => array($this->webform_users['admin']->mail),
411
      ),
412

    
413
      // Test hidden components.
414
      'hidden' => array(
415
        'component' => array(
416
          'form_key' => 'hidden',
417
          'name' => 'Hidden',
418
          'type' => 'hidden',
419
          'value' => 'default hidden value',
420
          'mandatory' => '1',
421
          'pid' => '0',
422
          'weight' => '-4',
423
        ),
424
        'sample values' => NULL,
425
        'database values' => array('default hidden value'),
426
        'database default values' => array('default hidden value'),
427
      ),
428

    
429
      // Test textarea components.
430
      'textarea' => array(
431
        'component' => array(
432
          'form_key' => 'textarea',
433
          'name' => 'Textarea',
434
          'type' => 'textarea',
435
          'value' => 'sample textarea default value',
436
          'extra' => array(),
437
          'mandatory' => '0',
438
          'pid' => '0',
439
          'weight' => '15',
440
        ),
441
        'sample values' => 'sample textarea value',
442
        'database values' => array('sample textarea value'),
443
        'database default values' => array('sample textarea default value'),
444
      ),
445

    
446
      // Test textfield components.
447
      'textfield_disabled' => array(
448
        'component' => array(
449
          'form_key' => 'textfield_disabled',
450
          'name' => 'Textfield Disabled',
451
          'type' => 'textfield',
452
          'value' => '%get[foo]',
453
          'extra' => array(
454
            'disabled' => 1,
455
          ),
456
          'mandatory' => '0',
457
          'pid' => '0',
458
          'weight' => '-15',
459
        ),
460
        'sample values' => NULL,
461
        'database values' => array('bar'),
462
        'database default values' => array('bar'),
463
      ),
464
      'textfield_profile' => array(
465
        'component' => array(
466
          'form_key' => 'textfield_profile',
467
          'name' => 'Textfield Profile',
468
          'type' => 'textfield',
469
          'value' => '%profile[profile_gender]',
470
          'extra' => array(
471
            'width' => '20',
472
          ),
473
          'mandatory' => '0',
474
          'pid' => '0',
475
          'weight' => '-6',
476
        ),
477
        'sample values' => 'Female',
478
        'database values' => array('Female'),
479
        'database default values' => array($this->webform_users['admin']->profile_gender),
480
      ),
481

    
482
      // Test time components.
483
      'time' => array(
484
        'component' => array(
485
          'form_key' => 'time',
486
          'name' => 'Time',
487
          'type' => 'time',
488
          'value' => '10:30pm',
489
          'extra' => array(
490
            'timezone' => 'site',
491
            'hourformat' => '12-hour',
492
          ),
493
          'mandatory' => '0',
494
          'pid' => '0',
495
          'weight' => '16',
496
        ),
497
        'sample values' => array('hour' => '5', 'minute' => '0', 'ampm' => 'am'),
498
        'database values' => array('05:00:00'),
499
        'database default values' => array('22:30:00'),
500
      ),
501
      'time_24h' => array(
502
        'component' => array(
503
          'form_key' => 'time_24h',
504
          'name' => 'Time 24H',
505
          'type' => 'time',
506
          'value' => '10:30pm',
507
          'extra' => array(
508
            'timezone' => 'site',
509
            'hourformat' => '24-hour',
510
          ),
511
          'mandatory' => '0',
512
          'pid' => '0',
513
          'weight' => '17',
514
        ),
515
        'sample values' => array('hour' => '5', 'minute' => '0'),
516
        'database values' => array('05:00:00'),
517
        'database default values' => array('22:30:00'),
518
      ),
519

    
520
      // Test number components.
521
      'integer' => array(
522
        'component' => array(
523
          'form_key' => 'integer',
524
          'name' => 'Integer',
525
          'type' => 'number',
526
          'value' => '1',
527
          'extra' => array(
528
            'type' => 'textfield',
529
            'integer' => 1,
530
            'max' => '100',
531
            // SimpleTest does not support type="number" input fields.
532
            'attributes' => array('type' => 'text'),
533
          ),
534
          'mandatory' => '0',
535
          'pid' => '0',
536
          'weight' => '18',
537
        ),
538
        'sample values' => '2',
539
        'database values' => array('2'),
540
        'database default values' => array('1'),
541
        'error values' => array(
542
          '1.5' => t('%name field value of @value must be an integer.', array('%name' => 'Integer', '@value' => '1.5')),
543
          '101' => t('%name field value must be less than @max.', array('%name' => 'Integer', '@max' => '100')),
544
        ),
545
      ),
546
      'integer_range' => array(
547
        'component' => array(
548
          'form_key' => 'integer_range',
549
          'name' => 'Integer Range',
550
          'type' => 'number',
551
          'value' => '50',
552
          'extra' => array(
553
            'type' => 'select',
554
            'min' => '10',
555
            'max' => '50',
556
            'step' => 5,
557
            'integer' => 1,
558
          ),
559
          'mandatory' => '0',
560
          'pid' => '0',
561
          'weight' => '19',
562
        ),
563
        'sample values' => '10',
564
        'database values' => array('10'),
565
        'database default values' => array('50'),
566
      ),
567
      'decimal_positive' => array(
568
        'component' => array(
569
          'form_key' => 'decimal_positive',
570
          'name' => 'Decimal positive',
571
          'type' => 'number',
572
          'value' => '1',
573
          'extra' => array(
574
            'type' => 'textfield',
575
            'field_prefix' => '$',
576
            'field_suffix' => 'lbs',
577
            'min' => '0',
578
            'decimals' => '2',
579
            'point' => '.',
580
            'separator' => ',',
581
            // SimpleTest does not support type="number" input fields.
582
            'attributes' => array('type' => 'text'),
583
          ),
584
          'mandatory' => '0',
585
          'pid' => '0',
586
          'weight' => '20',
587
        ),
588
        'sample values' => '2.00',
589
        'database values' => array('2.00'),
590
        'database default values' => array('1'),
591
        'error values' => array(
592
          '-1' => t('%name field value must be greater than @min.', array('%name' => 'Decimal positive', '@min' => '0')),
593
        ),
594
      ),
595
      'decimal_range' => array(
596
        'component' => array(
597
          'form_key' => 'decimal_range',
598
          'name' => 'Decimal range',
599
          'type' => 'number',
600
          'value' => '1',
601
          'extra' => array(
602
            'type' => 'textfield',
603
            'field_prefix' => '$',
604
            'field_suffix' => 'lbs',
605
            'min' => '1',
606
            'max' => '12',
607
            'step' => '1.5',
608
            // SimpleTest does not support type="number" input fields.
609
            'attributes' => array('type' => 'text'),
610
          ),
611
          'mandatory' => '0',
612
          'pid' => '0',
613
          'weight' => '21',
614
        ),
615
        'sample values' => '11.5',
616
        'database values' => array('11.5'),
617
        'database default values' => array('1'),
618
        'error values' => array(
619
          '2' => t('%name field value must be @start plus a multiple of @step.', array('%name' => 'Decimal range', '@start' => '1', '@step' => '1.5')),
620
          '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')),
621
        ),
622
      ),
623
      'decimal_range_select' => array(
624
        'component' => array(
625
          'form_key' => 'decimal_range_select',
626
          'name' => 'Decimal range select',
627
          'type' => 'number',
628
          'value' => '1',
629
          'extra' => array(
630
            'type' => 'select',
631
            'field_prefix' => '$',
632
            'field_suffix' => 'lbs',
633
            'min' => '1',
634
            'max' => '12',
635
            'step' => '1.5',
636
          ),
637
          'mandatory' => '0',
638
          'pid' => '0',
639
          'weight' => '21',
640
        ),
641
        'sample values' => '11.5',
642
        'database values' => array('11.5'),
643
        'database default values' => array('1'),
644
      ),
645
    );
646

    
647
    return $this->_webform_components;
648
  }
649

    
650
  function testWebformForm() {
651
    if (isset($this->_webform_node)) {
652
      return $this->_webform_node;
653
    }
654

    
655
    $settings = array(
656
     'type' => 'webform',
657
     'language'  => LANGUAGE_NONE,
658
     'uid' => '1',
659
     'status' => '1',
660
     'promote' => '1',
661
     'moderate' => '0',
662
     'sticky' => '0',
663
     'tnid' => '0',
664
     'translate' => '0',
665
     'title' => 'Test Webform',
666
     'body' => array(LANGUAGE_NONE => array(array('value' => 'Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla. Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat urna, ut fermentum neque mi egestas dolor.'))),
667
     'teaser' => array(LANGUAGE_NONE => array(array('value' => 'Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla.'))),
668
     'log' => '',
669
     'format' => '1',
670
     'webform' => array(
671
        'confirmation' => 'Thanks!',
672
        'confirmation_format' => filter_default_format(),
673
        'redirect_url' => '<confirmation>',
674
        'teaser' => '0',
675
        'allow_draft' => '1',
676
        'submit_text' => '',
677
        'submit_limit' => '-1',
678
        'submit_interval' => '-1',
679
        'submit_notice' => '1',
680
        'roles' => array('1', '2'),
681
        'components' => array(),
682
        'emails' => array(),
683
      ),
684
    );
685

    
686
    $cid = 0;
687
    foreach ($this->testWebformComponents() as $key => $component_info) {
688
      $cid++;
689
      $settings['webform']['components'][$cid] = $component_info['component'];
690
      $settings['webform']['components'][$cid]['cid'] = $cid;
691
      $settings['webform']['components'][$cid]['pid'] = 0;
692
    }
693

    
694
    $this->_webform_node = $this->drupalCreateNode($settings);
695

    
696
    return $this->_webform_node;
697
  }
698

    
699
  /**
700
   * Generate a list of all values that would result in a valid submission.
701
   */
702
  function testWebformPost() {
703
    $edit = array();
704
    foreach ($this->testWebformComponents() as $key => $component_info) {
705
      if (is_array($component_info['sample values'])) {
706
        foreach ($component_info['sample values'] as $subkey => $value) {
707
          $edit["submitted[$key][$subkey]"] = $value;
708
        }
709
      }
710
      elseif ($component_info['sample values'] != NULL) {
711
        $value = $component_info['sample values'];
712
        // Multiple selects have a funky extra empty bracket in the name.
713
        $extra = $key == 'select_multiple' ? '[]' : '';
714
        $edit["submitted[$key]$extra"] = $value;
715
      }
716
    }
717
    return $edit;
718
  }
719

    
720
  /**
721
   * Utility function to print out the current page being tested.
722
   */
723
  function webformPrintPage() {
724
    $this->verbose($this->drupalGetContent());
725
  }
726
}
727

    
728
/**
729
 * Test general functionality of Webform.
730
 */
731
class WebformGeneralTestCase extends WebformTestCase {
732
  /**
733
   * Implements getInfo().
734
   */
735
  public static function getInfo() {
736
    return array(
737
      'name' => t('Webform'),
738
      'description' => t('Checks global Webform settings and content types.'),
739
      'group' => t('Webform'),
740
    );
741
  }
742

    
743
  /**
744
   * Test creating a new Webform node.
745
   */
746
  function testWebformCreate() {
747
    $settings = array(
748
      'title' => 'Test webform, no components',
749
      'type' => 'webform',
750
    );
751
    $node = $this->drupalCreateNode($settings);
752

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

    
757
    // Make a change to the node, ensure that the record stays intact.
758
    $node->title .= '!';
759
    node_save($node);
760
    $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record still in the database after modifying webform node.'));
761
  }
762

    
763
  /**
764
   * Test webform-enabling a different node type and testing behavior.
765
   */
766
  function testWebformCreateNewType() {
767
    // Enable webforms on the page content type.
768
    variable_set('webform_node_types', array('webform', 'page'));
769

    
770
    $settings = array(
771
      'title' => 'Test webform-enabled page',
772
      'type' => 'page',
773
    );
774
    $node = $this->drupalCreateNode($settings);
775

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

    
781
    // Make a change to the node, ensure that the record stays empty.
782
    $node->title .= '!';
783
    node_save($node);
784
    $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record still not in the database after modifying page node.'));
785

    
786
    // Add a new component to the node and check that a record is made in the
787
    // webform table.
788
    $components = $this->testWebformComponents();
789
    $textarea = $components['textarea'];
790
    $textarea['type'] = 'textarea';
791
    $textarea['form_key'] = 'textarea';
792
    $textarea['cid'] = 1;
793
    $textarea['pid'] = 0;
794
    $textarea = array_merge(webform_component_invoke('textarea', 'defaults'), $textarea);
795
    $node->webform['components'][1] = $textarea;
796
    node_save($node);
797
    $this->assertTrue($this->webformRecordExists($node->nid), t('Webform record now exists after adding a new component.'));
798

    
799
    // Remove the new component and ensure that the record is deleted.
800
    $node->webform['components'] = array();
801
    node_save($node);
802
    $this->assertFalse($this->webformRecordExists($node->nid), t('Webform record deleted after deleting last component.'));
803
  }
804

    
805
  function webformRecordExists($nid) {
806
    return (bool) db_query("SELECT nid FROM {webform} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
807
  }
808
}