From 95023b8c1314374d8dda9d7b8dcead5a946b12b0 Mon Sep 17 00:00:00 2001 From: Angie Byron <webchick@24967.no-reply.drupal.org> Date: Sat, 28 Mar 2009 18:09:11 +0000 Subject: [PATCH] #297972 by scor, yched, Steven Jones, and heyrocker: Allow drupal_execute() to be performed within batch API (with tests). --- includes/form.inc | 5 +- modules/simpletest/tests/form.test | 39 +++++++++++++ modules/simpletest/tests/form_test.module | 70 +++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/includes/form.inc b/includes/form.inc index 2713e9e8729a..8e57737fd7da 100644 --- a/includes/form.inc +++ b/includes/form.inc @@ -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. diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 9dfa7cf8c831..be5c3117d324 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -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'); + } + +} diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 057046caf13b..dd91f6ebfda0 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -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']); +} -- GitLab