Projet

Général

Profil

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

root / drupal7 / sites / all / modules / webform / tests / WebformSubmissionTestCase.test @ 389fb945

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

    
44
  /**
45
   * Test a submission that uses default values, and check database integrity.
46
   */
47
  public function testWebformSubmissionDefault() {
48
    $this->drupalLogin($this->webform_users['admin']);
49
    $this->webformReset();
50
    $this->webformSubmissionExecute('default');
51
    $this->drupalLogout();
52
  }
53

    
54
  /**
55
   * Test validation errors on each component that has specialized validation.
56
   */
57
  public function testWebformSubmissionValidate() {
58
    $this->drupalLogin($this->webform_users['admin']);
59
    $this->webformReset();
60
    $this->webformSubmissionValidateExecute();
61
    $this->drupalLogout();
62
  }
63

    
64
  /**
65
   * Test that required fields with no default value can't be submitted as-is.
66
   */
67
  public function testWebformSubmissionRequiredComponents() {
68
    $this->drupalLogin($this->webform_users['admin']);
69
    $this->webformReset();
70

    
71
    // Create the Webform test node, and set all components to be required
72
    // with no default value.
73
    $node = $this->webformForm();
74
    $node = node_load($node->nid);
75
    foreach ($node->webform['components'] as &$component) {
76
      $component['value'] = '';
77
      $component['required'] = '1';
78
    }
79
    node_save($node);
80

    
81
    // Submit the webform with no data. We should get a message that all the
82
    // components are required. The exceptions are hidden fields, which can't be
83
    // made required, and date fields, which default to the current date when no
84
    // default value is provided; therefore, we don't expect a message for
85
    // those.
86
    $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
87
    foreach ($node->webform['components'] as $component) {
88
      if ($component['type'] != 'hidden' && $component['type'] != 'date') {
89
        $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
90
      }
91
    }
92

    
93
    $this->drupalLogout();
94
  }
95

    
96
  /**
97
   * Test length validation.
98
   */
99
  public function testWebformSubmissionComponentLength() {
100
    $this->drupalLogin($this->webform_users['admin']);
101
    $this->webformReset();
102

    
103
    // Create the Webform test node.
104
    $node = $this->webformForm();
105
    $node = node_load($node->nid);
106

    
107
    // Get the cid of the textfield component.
108
    foreach ($node->webform['components'] as &$component) {
109
      if ($component['form_key'] === 'textfield') {
110
        $textfield_cid = $component['cid'];
111
        break;
112
      }
113
    }
114

    
115
    // Set length validation rules.
116
    $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
117
    $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
118

    
119
    node_save($node);
120

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

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

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

    
134
  /**
135
   * Execute the submission test.
136
   *
137
   * @param string $value_type
138
   *   The values to be submitted to the webform. Either "sample" or "default".
139
   */
140
  public function webformSubmissionExecute($value_type = 'sample') {
141
    $path = drupal_get_path('module', 'webform');
142
    module_load_include('inc', 'webform', 'includes/webform.submissions');
143

    
144
    // Create a new Webform test node.
145
    $node = $this->webformForm();
146
    $submission_values = $value_type == 'sample' ? $this->webformPost() : array();
147

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

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

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

    
159
    // Get the SID of the new submission.
160
    $matches = array();
161
    preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
162
    $sid = $matches[1];
163

    
164
    // Pull in the database submission and check the values.
165
    drupal_static_reset('webform_get_submission');
166
    $actual_submission = webform_get_submission($node->nid, $sid);
167

    
168
    $component_info = $this->webformComponents();
169
    foreach ($node->webform['components'] as $cid => $component) {
170
      $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
171
      $actual_value = $actual_submission->data[$cid];
172
      $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'));
173
      if (!$result || $result === 'fail') {
174
        $this->fail(t("Expected !expected\n\nRecieved !recieved", array('!expected' => print_r($stable_value, TRUE), '!recieved' => print_r($actual_value, TRUE))), t('Webform'));
175
      }
176
    }
177
  }
178

    
179
  /**
180
   * Execute a validation check for a single component.
181
   */
182
  public function webformSubmissionValidateExecute() {
183
    $path = drupal_get_path('module', 'webform');
184
    module_load_include('inc', 'webform', 'includes/webform.submissions');
185

    
186
    // Create a new Webform test node.
187
    $node = $this->webformForm();
188

    
189
    // Visit the node page.
190
    $this->drupalGet('node/' . $node->nid);
191

    
192
    foreach ($this->webformComponents() as $key => $component_info) {
193
      if (isset($component_info['error values'])) {
194
        foreach ($component_info['error values'] as $value => $error_message) {
195
          $submission_values = array();
196
          $submission_values["submitted[$key]"] = $value;
197

    
198
          // Submit our test data.
199
          $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
200

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

    
205
          $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
206
        }
207
      }
208
    }
209
  }
210

    
211
}