Projet

Général

Profil

Paste
Télécharger (15,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformSubmissionTestCase.test @ 7b2d1845

1
<?php
2

    
3
/**
4
 * Webform module submission tests.
5
 */
6
class WebformSubmissionTestCase extends WebformTestCase {
7

    
8
  /**
9
   * {@inheritdoc}
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => t('Webform submission'),
14
      'description' => t('Submits a sample webform and checks the database integrity.'),
15
      'group' => t('Webform'),
16
    );
17
  }
18

    
19
  /**
20
   * Test sending a submission and check database integrity.
21
   */
22
  public function testWebformSubmission() {
23
    $this->drupalLogin($this->webform_users['admin']);
24
    $this->webformReset();
25
    $this->webformSubmissionExecute('sample');
26

    
27
    $loggedInUser = $this->loggedInUser;
28

    
29
    $this->drupalLogout();
30

    
31
    // Test webform_get_submission_count().
32
    // Counting the anonymous submission doesn't work because
33
    // $_SESSION['webform_submission'] is not populated in testing.
34
    $this->webformSubmissionExecute('sample');
35

    
36
    $count = webform_get_submission_count($this->webformForm()->nid);
37
    $this->assertIdentical((int) $count, 2, 'webform_get_submission_count() counts 2 total submissions.');
38

    
39
    $count = webform_get_submission_count($this->webformForm()->nid, $loggedInUser->uid);
40
    $this->assertIdentical((int) $count, 1, 'webform_get_submission_count() counts 1 submission from loggedInUser.');
41

    
42
    // Test _webform_submission_prepare_mail().
43
    $node = node_load($this->webformForm()->nid);
44
    $submission = webform_get_submissions($node->nid);
45
    $submission = array_pop($submission);
46
    $email = array(
47
      'status' => TRUE,
48
      'html' => FALSE,
49
      'template' => 'default',
50
      'from_address' => 'Test From',
51
      'from_name' => 'from@example.com',
52
      'subject' => 'Test Subject',
53
      'email' => 'to@example.com',
54
    );
55
    variable_set('webform_email_replyto', TRUE);
56
    variable_set('webform_email_address_format', 'long');
57
    variable_set('webform_default_from_name', 'Default "From" Name');
58
    variable_set('webform_default_from_address', 'default-from@example.com');
59
    $prepared_email = _webform_submission_prepare_mail($node, $submission, $email);
60
    $this->assertIdentical($prepared_email['mail_params']['email']['from'], '"from@example.com via Default \'From\' Name" <default-from@example.com>', 'From address is correctly set in _webform_submission_prepare_mail().');
61
  }
62

    
63
  /**
64
   * Test that the correct submissions are offered for download.
65
   */
66
  public function testWebformSubmissionDownload() {
67
    $this->drupalLogin($this->webform_users['userAccess']);
68
    $this->webformReset();
69

    
70
    // Create Webform which allows drafts and submit a draft.
71
    $webform_settings = array(
72
      'allow_draft' => '1',
73
    );
74
    $this->webformSubmissionExecute('sample', TRUE, $webform_settings);
75

    
76
    $nid = $this->webformForm()->nid;
77

    
78
    // This submit should complete the draft.
79
    $this->webformSubmissionExecute('sample');
80
    // Create another draft.
81
    $this->webformSubmissionExecute('sample', TRUE);
82

    
83
    // Create finished and draft submission as another user.
84
    $this->drupalLogin($this->webform_users['editor']);
85
    $this->webformSubmissionExecute('sample');
86
    $this->webformSubmissionExecute('sample', TRUE);
87

    
88
    // Create finished submission as anonymous.
89
    $this->drupalLogout();
90
    $this->webformSubmissionExecute('sample');
91
    $this->drupalLogin($this->webform_users['editor']);
92

    
93
    // Test webform_download_sids().
94
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'finished'), (int) $this->webform_users['editor']->uid);
95
    $this->assertIdentical($sids, array('1', '3', '5'), 'webform_download_sids() shows correct finished submissions.');
96
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'draft'), (int) $this->webform_users['editor']->uid);
97
    $this->assertIdentical($sids, array('2', '4'), 'webform_download_sids() shows correct draft submissions.');
98

    
99
    // The timestamp of the Webform results download simulated by the call to
100
    // webform_update_webform_last_download() needs to be after the timestamps
101
    // of the submissions made above. This ensures that the call to time() will
102
    // be after the submission timestamps.
103
    sleep(1);
104

    
105
    // Record that submissions so-far have been downloaded.
106
    // Parameter $sid is not used, so it's value doesn't matter.
107
    webform_update_webform_last_download($this->webformForm(), (int) $this->webform_users['editor']->uid, 0, time());
108

    
109
    // Following the simulated download, there are none to download.
110
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'finished'), (int) $this->webform_users['editor']->uid);
111
    $this->assertIdentical($sids, array(), 'webform_download_sids() shows correct finished submissions.');
112
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'draft'), (int) $this->webform_users['editor']->uid);
113
    $this->assertIdentical($sids, array(), 'webform_download_sids() shows correct draft submissions.');
114

    
115
    // Finish the draft, add a new draft, and re-check submission lists.
116
    $this->webformSubmissionExecute('sample');
117
    $this->webformSubmissionExecute('sample', TRUE);
118
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'finished'), (int) $this->webform_users['editor']->uid);
119
    $this->assertIdentical($sids, array('4'), 'webform_download_sids() shows correct finished submissions.');
120
    $sids = webform_download_sids($nid, array('range_type' => 'new', 'completion_type' => 'draft'), (int) $this->webform_users['editor']->uid);
121
    $this->assertIdentical($sids, array('6'), 'webform_download_sids() shows correct draft submissions.');
122
  }
123

    
124
  /**
125
   * Test Webform submission utility functions.
126
   *
127
   * Test webform_update_webform_last_download() and
128
   * webform_download_last_download_info().
129
   */
130
  public function testWebformSubmissionFunctions() {
131
    $node = (object) array('nid' => 1);
132
    $uid = 1;
133

    
134
    $result = webform_update_webform_last_download($node, $uid, 10, 1000);
135
    $this->assertIdentical($result, MergeQuery::STATUS_INSERT, 'webform_update_webform_last_download() reports successful insert.');
136
    $result = webform_download_last_download_info($node->nid, $uid);
137
    $test = array(
138
      'nid' => '1',
139
      'uid' => '1',
140
      'sid' => '10',
141
      'requested' => '1000',
142
      'serial' => NULL,
143
    );
144
    $this->assertIdentical($result, $test, 'webform_download_last_download_info() returned correct result.');
145

    
146
    $result = webform_update_webform_last_download($node, $uid, 20, 2000);
147
    $this->assertIdentical($result, MergeQuery::STATUS_UPDATE, 'webform_update_webform_last_download() reports successful update.');
148
    $result = webform_download_last_download_info($node->nid, $uid);
149
    $test = array(
150
      'nid' => '1',
151
      'uid' => '1',
152
      'sid' => '20',
153
      'requested' => '2000',
154
      'serial' => NULL,
155
    );
156
    $this->assertIdentical($result, $test, 'webform_download_last_download_info() returned correct result.');
157
  }
158

    
159
  /**
160
   * Test a submission that uses default values, and check database integrity.
161
   */
162
  public function testWebformSubmissionDefault() {
163
    $this->drupalLogin($this->webform_users['admin']);
164
    $this->webformReset();
165
    $this->webformSubmissionExecute('default');
166
    $this->drupalLogout();
167
  }
168

    
169
  /**
170
   * Test validation errors on each component that has specialized validation.
171
   */
172
  public function testWebformSubmissionValidate() {
173
    $this->drupalLogin($this->webform_users['admin']);
174
    $this->webformReset();
175
    $this->webformSubmissionValidateExecute();
176
    $this->drupalLogout();
177
  }
178

    
179
  /**
180
   * Test that required fields with no default value can't be submitted as-is.
181
   */
182
  public function testWebformSubmissionRequiredComponents() {
183
    $this->drupalLogin($this->webform_users['admin']);
184
    $this->webformReset();
185

    
186
    // Create the Webform test node, and set all components to be required
187
    // with no default value.
188
    $node = $this->webformForm();
189
    $node = node_load($node->nid);
190
    foreach ($node->webform['components'] as &$component) {
191
      $component['value'] = '';
192
      $component['required'] = '1';
193
    }
194
    node_save($node);
195

    
196
    // Submit the webform with no data. We should get a message that all the
197
    // components are required. The exceptions are hidden fields, which can't be
198
    // made required, and date fields, which default to the current date when no
199
    // default value is provided; therefore, we don't expect a message for
200
    // those.
201
    $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
202
    foreach ($node->webform['components'] as $component) {
203
      if ($component['type'] != 'hidden' && $component['type'] != 'date') {
204
        $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
205
      }
206
    }
207

    
208
    $this->drupalLogout();
209
  }
210

    
211
  /**
212
   * Test length validation.
213
   */
214
  public function testWebformSubmissionComponentLength() {
215
    $this->drupalLogin($this->webform_users['admin']);
216
    $this->webformReset();
217

    
218
    // Create the Webform test node.
219
    $node = $this->webformForm();
220
    $node = node_load($node->nid);
221

    
222
    // Get the cid of the textfield component.
223
    foreach ($node->webform['components'] as &$component) {
224
      if ($component['form_key'] === 'textfield') {
225
        $textfield_cid = $component['cid'];
226
        break;
227
      }
228
    }
229

    
230
    // Set length validation rules.
231
    $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
232
    $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
233

    
234
    node_save($node);
235

    
236
    // Text value that is too long.
237
    $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123456'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
238
    $this->assertRaw(t('!name cannot be longer than %max characters but is currently %length characters long.', array('!name' => $node->webform['components'][$textfield_cid]['name'], '%max' => 5, '%length' => 6)));
239

    
240
    // Text value that is too short.
241
    $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
242
    $this->assertRaw(t('!name cannot be shorter than %min characters but is currently %length characters long.', array('!name' => $node->webform['components'][$textfield_cid]['name'], '%min' => 4, '%length' => 3)));
243

    
244
    // Test value that meets validation rules has no error message.
245
    $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '12345'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
246
    $this->assertNoPattern('/ cannot be (longer|shorter) than /');
247
  }
248

    
249
  /**
250
   * Test webform_submission_data() function.
251
   *
252
   * Do not save components that do not collect data, for example, markup.
253
   */
254
  public function testPlainComponentsSubmission() {
255
    $node = (object) [
256
      'webform' => [
257
        'components' => [
258
          ['type' => 'date'],
259
          ['type' => 'email'],
260
          ['type' => 'grid'],
261
          ['type' => 'hidden'],
262
          ['type' => 'number'],
263
          ['type' => 'select'],
264
          ['type' => 'textarea'],
265
          ['type' => 'textfield'],
266
          ['type' => 'time'],
267
          ['type' => 'fieldset'],
268
          ['type' => 'markup'],
269
          ['type' => 'pagebreak'],
270
        ],
271
      ],
272
    ];
273
    $submitted = [
274
      'date',
275
      'email',
276
      ['value' => 'grid'],
277
      'hidden',
278
      'number',
279
      'select',
280
      'textarea',
281
      'textfield',
282
      'time',
283
      'fieldset',
284
      'markup',
285
      'pagebreak',
286
    ];
287
    $test_data = webform_submission_data($node, $submitted);
288
    $sample_data = [
289
      ['date'],
290
      ['email'],
291
      ['value' => 'grid'],
292
      ['hidden'],
293
      ['number'],
294
      ['select'],
295
      ['textarea'],
296
      ['textfield'],
297
      ['time'],
298
    ];
299
    $this->assertIdentical($test_data, $sample_data, 'webform_submission_data() generates correct data array.');
300
  }
301

    
302
  /**
303
   * Execute the submission test.
304
   *
305
   * @param string $value_type
306
   *   The values to be submitted to the webform. Either "sample" or "default".
307
   * @param bool $save_draft
308
   *   Whether to save a draft or a final submission.
309
   * @param array $webform_settings
310
   *   Settings to use for this form. Any unspecific settings will be default.
311
   */
312
  public function webformSubmissionExecute($value_type = 'sample', $save_draft = FALSE, array $webform_settings = array()) {
313
    $path = drupal_get_path('module', 'webform');
314
    module_load_include('inc', 'webform', 'includes/webform.submissions');
315

    
316
    // Create a new Webform test node.
317
    $node = $this->webformForm($webform_settings);
318
    $submission_values = $value_type == 'sample' ? $this->webformPost() : array();
319

    
320
    // Visit the node page with the "foo=bar" query, to test
321
    // [current-page:query:?] default values.
322
    $this->drupalGet('node/' . $node->nid, array('query' => array('foo' => 'bar')));
323
    $this->assertText($node->title, t('Webform node created and accessible at !url', array('!url' => 'node/' . $node->nid)), t('Webform'));
324

    
325
    // Submit our test data.
326
    $this->drupalPost(NULL, $submission_values, $save_draft ? 'Save Draft' : 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
327

    
328
    if ($save_draft) {
329
      $this->assertText(t('Submission saved. You may return to this form later and it will restore the current values.'), t('Save draft message displayed.'), t('Webform'));
330
      return;
331
    }
332

    
333
    // Confirm that the submission has been created.
334
    $this->assertText($node->webform['confirmation'], t('Confirmation message "@confirmation" received.', array('@confirmation' => $node->webform['confirmation'])), t('Webform'));
335

    
336
    // Get the SID of the new submission.
337
    $matches = array();
338
    preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
339
    $sid = $matches[1];
340

    
341
    // Pull in the database submission and check the values.
342
    drupal_static_reset('webform_get_submission');
343
    $actual_submission = webform_get_submission($node->nid, $sid);
344

    
345
    $component_info = $this->webformComponents();
346
    foreach ($node->webform['components'] as $cid => $component) {
347
      $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
348
      $actual_value = $actual_submission->data[$cid];
349
      $result = $this->assertEqual($stable_value, $actual_value, t('Component @form_key data integrity check when using @type values.', array('@form_key' => $component['form_key'], '@type' => $value_type)), t('Webform'));
350
      if (!$result || $result === 'fail') {
351
        $this->fail(t("Expected !expected\n\nRecieved !recieved", array('!expected' => print_r($stable_value, TRUE), '!recieved' => print_r($actual_value, TRUE))), t('Webform'));
352
      }
353
    }
354
  }
355

    
356
  /**
357
   * Execute a validation check for a single component.
358
   */
359
  public function webformSubmissionValidateExecute() {
360
    $path = drupal_get_path('module', 'webform');
361
    module_load_include('inc', 'webform', 'includes/webform.submissions');
362

    
363
    // Create a new Webform test node.
364
    $node = $this->webformForm();
365

    
366
    // Visit the node page.
367
    $this->drupalGet('node/' . $node->nid);
368

    
369
    foreach ($this->webformComponents() as $key => $component_info) {
370
      if (isset($component_info['error values'])) {
371
        foreach ($component_info['error values'] as $value => $error_message) {
372
          $submission_values = array();
373
          $submission_values["submitted[$key]"] = $value;
374

    
375
          // Submit our test data.
376
          $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
377

    
378
          // Confirm that the validation error occurred and the submission did
379
          // not save.
380
          $this->assertRaw($error_message, t('Validation message properly thrown: "%message".', array('%message' => $error_message)), t('Webform'));
381

    
382
          $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
383
        }
384
      }
385
    }
386
  }
387

    
388
}