Projet

Général

Profil

Paste
Télécharger (9,45 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformSubmissionTestCase.test @ 8d02775b

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
    $this->webformSubmissionExecute('sample');
33

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

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

    
40
    // Counting the anonymous submission doesn't work because
41
    // $_SESSION['webform_submission'] is not populated in testing.
42

    
43
    // Test _webform_submission_prepare_mail().
44
    $node = node_load($this->webformForm()->nid);
45
    $submission = webform_get_submissions($node->nid);
46
    $submission = array_pop($submission);
47
    $email = array(
48
      'status' => TRUE,
49
      'html' => FALSE,
50
      'template' => 'default',
51
      'from_address' => 'Test From',
52
      'from_name' => 'from@example.com',
53
      'subject' => 'Test Subject',
54
      'email' => 'to@example.com',
55
    );
56
    variable_set('webform_email_replyto', TRUE);
57
    variable_set('webform_email_address_format', 'long');
58
    variable_set('webform_default_from_name', 'Default "From" Name');
59
    variable_set('webform_default_from_address', 'default-from@example.com');
60
    $prepared_email = _webform_submission_prepare_mail($node, $submission, $email);
61
    $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().');
62
  }
63

    
64
  /**
65
   * Test a submission that uses default values, and check database integrity.
66
   */
67
  public function testWebformSubmissionDefault() {
68
    $this->drupalLogin($this->webform_users['admin']);
69
    $this->webformReset();
70
    $this->webformSubmissionExecute('default');
71
    $this->drupalLogout();
72
  }
73

    
74
  /**
75
   * Test validation errors on each component that has specialized validation.
76
   */
77
  public function testWebformSubmissionValidate() {
78
    $this->drupalLogin($this->webform_users['admin']);
79
    $this->webformReset();
80
    $this->webformSubmissionValidateExecute();
81
    $this->drupalLogout();
82
  }
83

    
84
  /**
85
   * Test that required fields with no default value can't be submitted as-is.
86
   */
87
  public function testWebformSubmissionRequiredComponents() {
88
    $this->drupalLogin($this->webform_users['admin']);
89
    $this->webformReset();
90

    
91
    // Create the Webform test node, and set all components to be required
92
    // with no default value.
93
    $node = $this->webformForm();
94
    $node = node_load($node->nid);
95
    foreach ($node->webform['components'] as &$component) {
96
      $component['value'] = '';
97
      $component['required'] = '1';
98
    }
99
    node_save($node);
100

    
101
    // Submit the webform with no data. We should get a message that all the
102
    // components are required. The exceptions are hidden fields, which can't be
103
    // made required, and date fields, which default to the current date when no
104
    // default value is provided; therefore, we don't expect a message for
105
    // those.
106
    $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
107
    foreach ($node->webform['components'] as $component) {
108
      if ($component['type'] != 'hidden' && $component['type'] != 'date') {
109
        $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
110
      }
111
    }
112

    
113
    $this->drupalLogout();
114
  }
115

    
116
  /**
117
   * Test length validation.
118
   */
119
  public function testWebformSubmissionComponentLength() {
120
    $this->drupalLogin($this->webform_users['admin']);
121
    $this->webformReset();
122

    
123
    // Create the Webform test node.
124
    $node = $this->webformForm();
125
    $node = node_load($node->nid);
126

    
127
    // Get the cid of the textfield component.
128
    foreach ($node->webform['components'] as &$component) {
129
      if ($component['form_key'] === 'textfield') {
130
        $textfield_cid = $component['cid'];
131
        break;
132
      }
133
    }
134

    
135
    // Set length validation rules.
136
    $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
137
    $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
138

    
139
    node_save($node);
140

    
141
    // Text value that is too long.
142
    $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123456'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
143
    $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)));
144

    
145
    // Text value that is too short.
146
    $this->drupalPost('node/' . $node->nid, array('submitted[textfield]' => '123'), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
147
    $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)));
148

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

    
154
  /**
155
   * Execute the submission test.
156
   *
157
   * @param string $value_type
158
   *   The values to be submitted to the webform. Either "sample" or "default".
159
   */
160
  public function webformSubmissionExecute($value_type = 'sample') {
161
    $path = drupal_get_path('module', 'webform');
162
    module_load_include('inc', 'webform', 'includes/webform.submissions');
163

    
164
    // Create a new Webform test node.
165
    $node = $this->webformForm();
166
    $submission_values = $value_type == 'sample' ? $this->webformPost() : array();
167

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

    
173
    // Submit our test data.
174
    $this->drupalPost(NULL, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
175

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

    
179
    // Get the SID of the new submission.
180
    $matches = array();
181
    preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
182
    $sid = $matches[1];
183

    
184
    // Pull in the database submission and check the values.
185
    drupal_static_reset('webform_get_submission');
186
    $actual_submission = webform_get_submission($node->nid, $sid);
187

    
188
    $component_info = $this->webformComponents();
189
    foreach ($node->webform['components'] as $cid => $component) {
190
      $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
191
      $actual_value = $actual_submission->data[$cid];
192
      $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'));
193
      if (!$result || $result === 'fail') {
194
        $this->fail(t("Expected !expected\n\nRecieved !recieved", array('!expected' => print_r($stable_value, TRUE), '!recieved' => print_r($actual_value, TRUE))), t('Webform'));
195
      }
196
    }
197
  }
198

    
199
  /**
200
   * Execute a validation check for a single component.
201
   */
202
  public function webformSubmissionValidateExecute() {
203
    $path = drupal_get_path('module', 'webform');
204
    module_load_include('inc', 'webform', 'includes/webform.submissions');
205

    
206
    // Create a new Webform test node.
207
    $node = $this->webformForm();
208

    
209
    // Visit the node page.
210
    $this->drupalGet('node/' . $node->nid);
211

    
212
    foreach ($this->webformComponents() as $key => $component_info) {
213
      if (isset($component_info['error values'])) {
214
        foreach ($component_info['error values'] as $value => $error_message) {
215
          $submission_values = array();
216
          $submission_values["submitted[$key]"] = $value;
217

    
218
          // Submit our test data.
219
          $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
220

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

    
225
          $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
226
        }
227
      }
228
    }
229
  }
230

    
231
}