Projet

Général

Profil

Paste
Télécharger (7,84 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / webform / tests / WebformSubmissionTestCase.test @ 01f36513

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
    $this->drupalLogout();
27
  }
28

    
29
  /**
30
   * Test a submission that uses default values, and check database integrity.
31
   */
32
  public function testWebformSubmissionDefault() {
33
    $this->drupalLogin($this->webform_users['admin']);
34
    $this->webformReset();
35
    $this->webformSubmissionExecute('default');
36
    $this->drupalLogout();
37
  }
38

    
39
  /**
40
   * Test validation errors on each component that has specialized validation.
41
   */
42
  public function testWebformSubmissionValidate() {
43
    $this->drupalLogin($this->webform_users['admin']);
44
    $this->webformReset();
45
    $this->webformSubmissionValidateExecute();
46
    $this->drupalLogout();
47
  }
48

    
49
  /**
50
   * Test that required fields with no default value can't be submitted as-is.
51
   */
52
  public function testWebformSubmissionRequiredComponents() {
53
    $this->drupalLogin($this->webform_users['admin']);
54
    $this->webformReset();
55

    
56
    // Create the Webform test node, and set all components to be required
57
    // with no default value.
58
    $node = $this->webformForm();
59
    $node = node_load($node->nid);
60
    foreach ($node->webform['components'] as &$component) {
61
      $component['value'] = '';
62
      $component['required'] = '1';
63
    }
64
    node_save($node);
65

    
66
    // Submit the webform with no data. We should get a message that all the
67
    // components are required. The exceptions are hidden fields, which can't be
68
    // made required, and date fields, which default to the current date when no
69
    // default value is provided; therefore, we don't expect a message for
70
    // those.
71
    $this->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
72
    foreach ($node->webform['components'] as $component) {
73
      if ($component['type'] != 'hidden' && $component['type'] != 'date') {
74
        $this->assertText(t('!name field is required.', array('!name' => $component['name'])));
75
      }
76
    }
77

    
78
    $this->drupalLogout();
79
  }
80

    
81
  /**
82
   * Test length validation.
83
   */
84
  public function testWebformSubmissionComponentLength() {
85
    $this->drupalLogin($this->webform_users['admin']);
86
    $this->webformReset();
87

    
88
    // Create the Webform test node.
89
    $node = $this->webformForm();
90
    $node = node_load($node->nid);
91

    
92
    // Get the cid of the textfield component.
93
    foreach ($node->webform['components'] as &$component) {
94
      if ($component['form_key'] === 'textfield') {
95
        $textfield_cid = $component['cid'];
96
        break;
97
      }
98
    }
99

    
100
    // Set length validation rules.
101
    $node->webform['components'][$textfield_cid]['extra']['maxlength'] = 5;
102
    $node->webform['components'][$textfield_cid]['extra']['minlength'] = 4;
103

    
104
    node_save($node);
105

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

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

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

    
119
  /**
120
   * Execute the submission test.
121
   *
122
   * @param string $value_type
123
   *   The values to be submitted to the webform. Either "sample" or "default".
124
   */
125
  public function webformSubmissionExecute($value_type = 'sample') {
126
    $path = drupal_get_path('module', 'webform');
127
    module_load_include('inc', 'webform', 'includes/webform.submissions');
128

    
129
    // Create a new Webform test node.
130
    $node = $this->webformForm();
131
    $submission_values = $value_type == 'sample' ? $this->webformPost() : array();
132

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

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

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

    
144
    // Get the SID of the new submission.
145
    $matches = array();
146
    preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
147
    $sid = $matches[1];
148

    
149
    // Pull in the database submission and check the values.
150
    drupal_static_reset('webform_get_submission');
151
    $actual_submission = webform_get_submission($node->nid, $sid);
152

    
153
    $component_info = $this->webformComponents();
154
    foreach ($node->webform['components'] as $cid => $component) {
155
      $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
156
      $actual_value = $actual_submission->data[$cid];
157
      $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'));
158
      if (!$result || $result === 'fail') {
159
        $this->fail(t('Expected !expected', array('!expected' => print_r($stable_value, TRUE))) . "\n\n" . t('Recieved !recieved', array('!recieved' => print_r($actual_value, TRUE))), t('Webform'));
160
      }
161
    }
162
  }
163

    
164
  /**
165
   * Execute a validation check for a single component.
166
   */
167
  public function webformSubmissionValidateExecute() {
168
    $path = drupal_get_path('module', 'webform');
169
    module_load_include('inc', 'webform', 'includes/webform.submissions');
170

    
171
    // Create a new Webform test node.
172
    $node = $this->webformForm();
173

    
174
    // Visit the node page.
175
    $this->drupalGet('node/' . $node->nid);
176

    
177
    foreach ($this->webformComponents() as $key => $component_info) {
178
      if (isset($component_info['error values'])) {
179
        foreach ($component_info['error values'] as $value => $error_message) {
180
          $submission_values = array();
181
          $submission_values["submitted[$key]"] = $value;
182

    
183
          // Submit our test data.
184
          $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
185

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

    
190
          $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
191
        }
192
      }
193
    }
194
  }
195

    
196
}