<?php
// $Id$

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

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

class WebformSubmissionTestCase extends WebformTestCase {
  /**
   * Implementation of 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'),
    );
  }

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

  /**
   * Implementation of 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();
  }

  function testWebformSubmissionDefault() {
    $this->drupalLogin($this->webform_users['admin']);
    $this->webformReset();
    $this->webformSubmissionExecute('default');
    $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' => 'foo=bar'));
    $this->assertText($node->body, 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');

    // 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'));
      }
    }
  }

}
