submission.test 6.28 KB
<?php

/**
 * @file
 * Webform module submission tests.
 */

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

class WebformSubmissionTestCase extends WebformTestCase {
  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Webform submission'),
      'description' => t('Submits a sample webform and checks the database integrity.'),
      'group' => t('Webform'),
    );
  }

  /**
   * Implements setUp().
   */
  function setUp() {
    parent::setUp();
  }

  /**
   * Implements tearDown().
   */
  function tearDown() {
    parent::tearDown();
  }

  /**
   * Test sending a submission and check database integrity.
   */
  function testWebformSubmission() {
    $this->drupalLogin($this->webform_users['admin']);
    $this->webformReset();
    $this->webformSubmissionExecute('sample');
    $this->drupalLogout();
  }

  /**
   * Test a submission that uses default values, and check database integrity.
   */
  function testWebformSubmissionDefault() {
    $this->drupalLogin($this->webform_users['admin']);
    $this->webformReset();
    $this->webformSubmissionExecute('default');
    $this->drupalLogout();
  }

  /**
   * Test validation errors on each component that has specialized validation.
   */
  function testWebformSubmissionValidate() {
    $this->drupalLogin($this->webform_users['admin']);
    $this->webformReset();
    $this->webformSubmissionValidateExecute();
    $this->drupalLogout();
  }

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

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

    // Submit the webform with no data. We should get a message that all the
    // components are required. (The exceptions are hidden fields, which can't
    // be made mandatory, and date fields, which default to the current date
    // when no default value is provided; therefore, we don't expect a message
    // for those.)
    $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();
  }

  /**
   * Execute the submission test.
   *
   * @param $value_type
   *   The values to be submitted to the webform. Either "sample" or "default".
   */
  function webformSubmissionExecute($value_type = 'sample') {
    $path = drupal_get_path('module', 'webform');
    module_load_include('inc', 'webform', 'includes/webform.submissions');

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

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

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

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

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

    // Pull in the database submission and check the values.
    $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'));
      }
    }
  }

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

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

    // Visit the node page.
    $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;

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

          // Confirm that the validation error occurred and the submission did not save.
          $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.'));
        }
      }
    }
  }
}