Creating webform submissions programatically in order to use with SimpleTest

I’m trying to build out a SimpleTest to test out functionality on submitted webforms. I need to simulate loading a drupal webform node, applying fake data, and submitting the form (triggering a database save).

Currently, I’m forcing data in (on a vagrant box) a drupal instance, but I have a feeling there is a better way to do this.

Question: In Simpletest, how do I properly load a webform, inject fake data into the form fields, and submit the form?

First attemtp: Original fixture code. It works, but it’s sloppy!

Class myTest extends DrupalWebTestCase { . . .   /*   Drops ALL DATA in the tables being tested   */   function _dropData() {      db_query('delete from {webform_submitted_data}');      db_query('delete from {webform_submissions}');   }    /*   Fixtures, test data. Clears both webform_submitted_data and webform_submissions   and then adds a new set of mock data generated initially by a browser plugin   that autofills forms. This allows for a standardized set of data used on the   simple test form (import script included in the package).   */   function _populateTablesWithTestData() {     $q1 = "SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; INSERT INTO `webform_submitted_data` (`nid`, `sid`, `cid`, `no`, `data`) VALUES (1, 59, 2, '0', 'Dignissimos enim dolorem quia et minim velit eiusmod eiusmod voluptas nemo nostrum cum aliquam unde mollit obcaecati'), (1, 59, 3, '0', 'pugy@example.com'), (1, 59, 4, '0', '2013-03-11'), (1, 60, 2, '0', 'Sapiente velit voluptatem et quod et proident voluptatem Sunt minima reprehenderit reprehenderit'),(1, 60, 3, '0', 'nejemuno@example.com'),(1, 60, 4, '0', '2014-05-16'),(1, 61, 2, '0', 'Id nisi pariatur Quidem in nihil culpa qui rerum minim nihil ea minim ipsa fugiat'),(1, 61, 3, '0', 'mohafowoba@example.com'),(1, 61, 4, '0', '2015-05-02'),(1, 62, 2, '0', 'Omnis quisquam nisi non et est est eiusmod qui qui odio ut dignissimos'),(1, 62, 3, '0', 'vujil@example.com'),(1, 62, 4, '0', '2015-03-02');";     $q2 = "SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; INSERT INTO `webform_submissions` (`sid`, `nid`, `serial`, `uid`, `is_draft`, `submitted`, `remote_addr`) VALUES (59, 1, 59, 0, 0, 1424275998, '192.168.33.1'), (60, 1, 60, 0, 0, 1424276006, '192.168.33.1'), (61, 1, 61, 0, 0, 1424276013, '192.168.33.1'), (62, 1, 62, 0, 0, 1424276031, '192.168.33.1');";      $this->_dropData();     db_query($q1);     db_query($q2);   } 

Second Attempt:

In a test script to be run from drush…

$form_state = array(); $form_state['values']['test2']='Something'; $form_state['values']['test_email']='Some@email.ext'; $form_state['values']['test_date']="";  drupal_form_submit('webform_client_form_1', $form_state); 

However, this gives me:

$ drush scr test.php  Missing argument 3 for webform_client_form() webform.module:2268                                                               [warning] Invalid argument supplied for foreach() webform.module:2843    

Third Attempt

$form_state = array(); $form_state['values']['test2']='Something'; $form_state['values']['test_email']='Some@email.ext'; $form_state['values']['test_date']=""; $form_state['op'] = t('Submit');   drupal_form_submit('webform_client_form_1', $form_state, node_load(1), array()); print_r(form_get_errors()); 

Which gets:

$drush scr test.php  end() expects parameter 1 to be array, null given webform.module:3120                                                          [warning] end() expects parameter 1 to be array, null given webform.module:3123   

What the form looks like

Export Code:

$webform = array (   'nid' => '1',   'next_serial' => '63',   'confirmation' => '',   'confirmation_format' => 'filtered_html',   'redirect_url' => '<confirmation>',   'status' => '1',   'block' => '0',   'allow_draft' => '0',   'auto_save' => '0',   'submit_notice' => '1',   'submit_text' => '',   'submit_limit' => '-1',   'submit_interval' => '-1',   'total_submit_limit' => '-1',   'total_submit_interval' => '-1',   'progressbar_bar' => '1',   'progressbar_page_number' => '0',   'progressbar_percent' => '0',   'progressbar_pagebreak_labels' => '1',   'progressbar_include_confirmation' => '1',   'progressbar_label_first' => 'Start',   'progressbar_label_confirmation' => 'Complete',   'preview' => '0',   'preview_next_button_label' => '',   'preview_prev_button_label' => '',   'preview_title' => '',   'preview_message' => '',   'preview_message_format' => 'filtered_html',   'preview_excluded_components' =>    array (   ),   'record_exists' => true,   'roles' =>    array (     0 => '1',     1 => '2',   ),   'emails' =>    array (   ),   'components' =>    array (     2 =>      array (       'nid' => 1,       'cid' => '2',       'pid' => '0',       'form_key' => 'test2',       'name' => 'TEST Text ',       'type' => 'textfield',       'value' => '',       'extra' =>        array (         'title_display' => 'before',         'private' => 0,         'wrapper_classes' => '',         'css_classes' => '',         'width' => '',         'maxlength' => '',         'field_prefix' => '',         'field_suffix' => '',         'disabled' => 0,         'unique' => 0,         'description' => '',         'placeholder' => '',         'attributes' =>          array (         ),         'analysis' => false,       ),       'required' => '0',       'weight' => '1',       'page_num' => 1,     ),     3 =>      array (       'nid' => 1,       'cid' => '3',       'pid' => '0',       'form_key' => 'test_email',       'name' => 'TEst Email',       'type' => 'email',       'value' => '',       'extra' =>        array (         'title_display' => 'before',         'private' => 0,         'wrapper_classes' => '',         'css_classes' => '',         'width' => '',         'unique' => 0,         'disabled' => 0,         'description' => '',         'placeholder' => '',         'attributes' =>          array (         ),         'analysis' => false,       ),       'required' => '0',       'weight' => '2',       'page_num' => 1,     ),     4 =>      array (       'nid' => 1,       'cid' => '4',       'pid' => '0',       'form_key' => 'date',       'name' => 'Date',       'type' => 'date',       'value' => '',       'extra' =>        array (         'title_display' => 'before',         'private' => 0,         'wrapper_classes' => '',         'timezone' => 'user',         'start_date' => '-2 years',         'end_date' => '+2 years',         'year_textfield' => 0,         'datepicker' => 1,         'description' => '',         'analysis' => false,       ),       'required' => '0',       'weight' => '4',       'page_num' => 1,     ),   ),   'conditionals' =>    array (   ), ); 

Screenshot: enter image description here

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Creating webform submissions programatically in order to use with SimpleTest

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.