Projet

Général

Profil

Paste
Télécharger (6,28 ko) Statistiques
| Branche: | Révision:

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

1
<?php
2

    
3
/**
4
 * @file
5
 * Webform module submission tests.
6
 */
7

    
8
include_once(dirname(__FILE__) . '/webform.test');
9

    
10
class WebformSubmissionTestCase extends WebformTestCase {
11
  /**
12
   * Implements getInfo().
13
   */
14
  public static function getInfo() {
15
    return array(
16
      'name' => t('Webform submission'),
17
      'description' => t('Submits a sample webform and checks the database integrity.'),
18
      'group' => t('Webform'),
19
    );
20
  }
21

    
22
  /**
23
   * Implements setUp().
24
   */
25
  function setUp() {
26
    parent::setUp();
27
  }
28

    
29
  /**
30
   * Implements tearDown().
31
   */
32
  function tearDown() {
33
    parent::tearDown();
34
  }
35

    
36
  /**
37
   * Test sending a submission and check database integrity.
38
   */
39
  function testWebformSubmission() {
40
    $this->drupalLogin($this->webform_users['admin']);
41
    $this->webformReset();
42
    $this->webformSubmissionExecute('sample');
43
    $this->drupalLogout();
44
  }
45

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

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

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

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

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

    
95
    $this->drupalLogout();
96
  }
97

    
98
  /**
99
   * Execute the submission test.
100
   *
101
   * @param $value_type
102
   *   The values to be submitted to the webform. Either "sample" or "default".
103
   */
104
  function webformSubmissionExecute($value_type = 'sample') {
105
    $path = drupal_get_path('module', 'webform');
106
    module_load_include('inc', 'webform', 'includes/webform.submissions');
107

    
108
    // Create a new Webform test node.
109
    $node = $this->testWebformForm();
110
    $submission_values = $value_type == 'sample' ? $this->testWebformPost() : array();
111

    
112
    // Visit the node page with the "foo=bar" query, to test %get[] default values.
113
    $this->drupalGet('node/' . $node->nid, array('query' => array('foo' => 'bar')));
114
    $this->assertText($node->body[LANGUAGE_NONE][0]['value'], t('Webform node created and accessible at !url', array('!url' => 'node/' . $node->nid)), t('Webform'));
115

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

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

    
122
    // Get the SID of the new submission.
123
    $matches = array();
124
    preg_match('/sid=([0-9]+)/', $this->getUrl(), $matches);
125
    $sid = $matches[1];
126

    
127
    // Pull in the database submission and check the values.
128
    $actual_submission = webform_get_submission($node->nid, $sid, TRUE);
129

    
130
    $component_info = $this->testWebformComponents();
131
    foreach ($node->webform['components'] as $cid => $component) {
132
      $stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
133
      $actual_value = $actual_submission->data[$cid]['value'];
134
      $result = $this->assertEqual($stable_value, $actual_value, t('Component @form_key data integrity check', array('@form_key' => $component['form_key'])), t('Webform'));
135
      if (!$result || $result === 'fail') {
136
        $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'));
137
      }
138
    }
139
  }
140

    
141
  /**
142
   * Execute a validation check for a single component.
143
   *
144
   * @param $value_type
145
   *   The values to be submitted to the webform. Either "sample" or "default".
146
   */
147
  function webformSubmissionValidateExecute() {
148
    $path = drupal_get_path('module', 'webform');
149
    module_load_include('inc', 'webform', 'includes/webform.submissions');
150

    
151
    // Create a new Webform test node.
152
    $node = $this->testWebformForm();
153

    
154
    // Visit the node page.
155
    $this->drupalGet('node/' . $node->nid);
156

    
157
    foreach ($this->testWebformComponents() as $key => $component_info) {
158
      if (isset($component_info['error values'])) {
159
        foreach ($component_info['error values'] as $value => $error_message) {
160
          $submission_values = array();
161
          $submission_values["submitted[$key]"] = $value;
162

    
163
          // Submit our test data.
164
          $this->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
165

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

    
169
          $this->assertFalse(preg_match('/sid=([0-9]+)/', $this->getUrl()), t('Submission not saved.'));
170
        }
171
      }
172
    }
173
  }
174
}