Commit 95023b8c authored by webchick's avatar webchick

#297972 by scor, yched, Steven Jones, and heyrocker: Allow drupal_execute() to...

#297972 by scor, yched, Steven Jones, and heyrocker: Allow drupal_execute() to be performed within batch API (with tests).
parent 1c2033f0
......@@ -856,7 +856,10 @@ function form_execute_handlers($type, &$form, &$form_state) {
foreach ($handlers as $function) {
if (drupal_function_exists($function)) {
if ($type == 'submit' && ($batch =& batch_get())) {
// Check to see if a previous _submit handler has set a batch, but
// make sure we do not react to a batch that is already being processed
// (for instance if a batch operation performs a drupal_execute()).
if ($type == 'submit' && ($batch =& batch_get()) && !isset($batch['current_set'])) {
// Some previous _submit handler has set a batch. We store the call
// in a special 'control' batch set, for execution at the correct
// time during the batch processing workflow.
......
......@@ -344,3 +344,42 @@ class FormsFormCleanIdFunctionalTest extends DrupalWebTestCase {
}
}
/**
* Test using drupal_execute in a batch.
*/
class FormAPITestCase extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Drupal Execute and Batch API'),
'description' => t('Tests the compatibility of drupal_execute and the Batch API'),
'group' => t('Form API'),
);
}
/**
* Check that we can run drupal_execute during a batch.
*/
function testDrupalExecuteInBatch() {
// Our test is going to modify the following variable.
variable_set('form_test_mock_submit', 'initial_state');
// This is a page that sets a batch, which calls drupal_execute, which
// modifies the variable we set up above.
$this->drupalGet('form_test/drupal_execute_batch_api');
// If the drupal_execute call executed correctly our test variable will be
// set to 'form_submitted'.
$this->assertEqual('form_submitted', variable_get('form_test_mock_submit', 'initial_state'), t('Check drupal_execute called submit handlers when running in a batch'));
// Clean our variable up.
variable_del('form_test_mock_submit');
}
function setUp() {
parent::setUp('form_test');
}
}
......@@ -51,6 +51,13 @@ function form_test_menu() {
'type' => MENU_CALLBACK,
);
$items['form_test/drupal_execute_batch_api'] = array(
'title' => 'BatchAPI Drupal_execute tests',
'page callback' => 'form_test_drupal_execute_batch_api',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -209,3 +216,66 @@ function _form_test_tableselect_js_select_form($form_state, $action) {
return _form_test_tableselect_form_builder($form_state, $options);
}
/**
* Page callback for the batch/drupal_execute interaction test.
*
* When called without any arguments we set up a batch that calls
* form_test_batch_callback. That function will submit a form using
* drupal_execute using the values specified in this function.
*
* The form's field test_value begins at 'initial_value', and is changed
* to 'form_submitted' when the form is submitted successfully. On
* completion this function is passed 'done' to complete the process.
*/
function form_test_drupal_execute_batch_api($arg = '') {
// If we're at the end of the batch process, return.
if ($arg == 'done') {
return t('Done');
}
// Otherwise set up the batch.
$batch['operations'] = array(
array('form_test_batch_callback', array('form_submitted')),
);
// Set the batch and process it.
batch_set($batch);
batch_process('form_test/drupal_execute_batch_api/done');
}
/**
* Submits form_test_mock_form using drupal_execute using the given $value.
*/
function form_test_batch_callback($value) {
$state['values']['test_value'] = $value;
drupal_execute('form_test_mock_form', $state);
}
/**
* A simple form with a textfield and submit button.
*/
function form_test_mock_form($form_state) {
$form = array();
$form['test_value'] = array(
'#type' => 'textfield',
'#default_value' => 'initial_state',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Form submission callback.
*
* Updates the variable 'form_test_mock_submit' to the submitted form value.
*/
function form_test_mock_form_submit($form, &$form_state) {
variable_set('form_test_mock_submit', $form_state['values']['test_value']);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment