Drupal - How do I programatically submit a webform via drupal_form_submit?

I used PhpStorm and xdebug to step through the form.inc code and finally found the correct structure for my $form_state. The structure I posted was close, just needed to add the 'op' => t('Submit'). See my final working code below.

To answer my questions:

  1. Yes, drupal_form_submit is what I should be using.
  2. See code below for the magical $form_state structure.
<?php
$form_id = 'webform_client_form_55805';
$form_state = array(
  'values' => array(
    'submitted' => array(
      'first_name' => 'David',
    ),
    'op' => t('Submit'),
  ),
);
$node = node_load(55805);
$submission = (object) array();

drupal_form_submit($form_id, $form_state, $node, $submission);
$form_errors = form_get_errors();
?>

Tags:

Forms

Webforms