home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (submission.test)

This example Drupal source code file (submission.test) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, component_info, function, integrity, matches, node, php, result, sample, sid, url, value_type, values, webform

The submission.test Drupal example source code

<?php

/**
 * @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' => 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');

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

}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal submission.test source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.