View source
<?php
include_once dirname(__FILE__) . '/webform.test';
class WebformSubmissionTestCase extends WebformTestCase {
public static function getInfo() {
return array(
'name' => t('Webform submission'),
'description' => t('Submits a sample webform and checks the database integrity.'),
'group' => t('Webform'),
);
}
function setUp() {
parent::setUp();
}
function tearDown() {
parent::tearDown();
}
function testWebformSubmission() {
$this
->drupalLogin($this->webform_users['admin']);
$this
->webformReset();
$this
->webformSubmissionExecute('sample');
$this
->drupalLogout();
}
function testWebformSubmissionDefault() {
$this
->drupalLogin($this->webform_users['admin']);
$this
->webformReset();
$this
->webformSubmissionExecute('default');
$this
->drupalLogout();
}
function testWebformSubmissionValidate() {
$this
->drupalLogin($this->webform_users['admin']);
$this
->webformReset();
$this
->webformSubmissionValidateExecute();
$this
->drupalLogout();
}
function testWebformSubmissionRequiredComponents() {
$this
->drupalLogin($this->webform_users['admin']);
$this
->webformReset();
$node = $this
->testWebformForm();
$node = node_load($node->nid);
foreach ($node->webform['components'] as &$component) {
$component['value'] = '';
$component['mandatory'] = '1';
}
node_save($node);
$this
->drupalPost('node/' . $node->nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
foreach ($node->webform['components'] as $component) {
if ($component['type'] != 'hidden' && $component['type'] != 'date') {
$this
->assertText(t('!name field is required.', array(
'!name' => $component['name'],
)));
}
}
$this
->drupalLogout();
}
function webformSubmissionExecute($value_type = 'sample') {
$path = drupal_get_path('module', 'webform');
module_load_include('inc', 'webform', 'includes/webform.submissions');
$node = $this
->testWebformForm();
$submission_values = $value_type == 'sample' ? $this
->testWebformPost() : array();
$this
->drupalGet('node/' . $node->nid, array(
'query' => 'foo=bar',
));
$this
->assertText($node->body, t('Webform node created and accessible at !url', array(
'!url' => 'node/' . $node->nid,
)), t('Webform'));
$this
->drupalPost(NULL, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
$this
->assertText($node->webform['confirmation'], t('Confirmation message "@confirmation" received.', array(
'@confirmation' => $node->webform['confirmation'],
)), t('Webform'));
$matches = array();
preg_match('/sid=([0-9]+)/', $this
->getUrl(), $matches);
$sid = $matches[1];
$actual_submission = webform_get_submission($node->nid, $sid, TRUE);
$component_info = $this
->testWebformComponents();
foreach ($node->webform['components'] as $cid => $component) {
$stable_value = $value_type == 'sample' ? $component_info[$component['form_key']]['database values'] : $component_info[$component['form_key']]['database default values'];
$actual_value = $actual_submission->data[$cid]['value'];
$result = $this
->assertEqual($stable_value, $actual_value, t('Component @form_key data integrity check', array(
'@form_key' => $component['form_key'],
)), t('Webform'));
if (!$result || $result === 'fail') {
$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'));
}
}
}
function webformSubmissionValidateExecute() {
$path = drupal_get_path('module', 'webform');
module_load_include('inc', 'webform', 'includes/webform.submissions');
$node = $this
->testWebformForm();
$this
->drupalGet('node/' . $node->nid);
foreach ($this
->testWebformComponents() as $key => $component_info) {
if (isset($component_info['error values'])) {
foreach ($component_info['error values'] as $value => $error_message) {
$submission_values = array();
$submission_values["submitted[{$key}]"] = $value;
$this
->drupalPost('node/' . $node->nid, $submission_values, 'Submit', array(), array(), 'webform-client-form-' . $node->nid);
$this
->assertRaw($error_message, t('Validation message properly thrown: "%message".', array(
'%message' => $error_message,
)), t('Webform'));
$this
->assertFalse(preg_match('/sid=([0-9]+)/', $this
->getUrl()), t('Submission not saved.'));
}
}
}
}
}