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', '[email protected]'), (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', '[email protected]'),(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', '[email protected]'),(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', '[email protected]'),(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']='[email protected]';
$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']='[email protected]';
$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: