Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.400
diff -u -p -r1.400 form.inc
--- includes/form.inc	16 Nov 2009 05:11:01 -0000	1.400
+++ includes/form.inc	17 Nov 2009 19:54:34 -0000
@@ -306,6 +306,16 @@ function drupal_rebuild_form($form_id, &
     form_set_cache($form_build_id, $form, $form_state);
   }
 
+  // A submit handler or other function in the form processing pipeline can
+  // set $form_state['rebuild_values'] to explicitly specify data values to use
+  // for populating the rebuilt form. If not explicitly set, and
+  // $form_state['rebuild'] has been set or the form has errors, we want to
+  // build a fresh state, but otherwise, we want to carry over values from
+  // before the rebuild.
+  if (!isset($form_state['rebuild_values']) && !$form_state['rebuild'] && !form_get_errors()) {
+    $form_state['rebuild_values'] = $form_state['values'];
+  }
+
   // Clear out all post data, as we don't want the previous step's
   // data to pollute this one and trigger validate/submit handling,
   // then process the form for rendering.
@@ -315,9 +325,15 @@ function drupal_rebuild_form($form_id, &
   // when rerendering the form.
   $form_state['groups'] = array();
 
+  // Also clear out values.
+  $form_state['values'] = array();
+
   // Do not call drupal_process_form(), since it would prevent the rebuilt form
   // to submit.
   $form = form_builder($form_id, $form, $form_state);
+
+  // Done rebuilding. Don't carry over the rebuild values to future rebuilds.
+  unset($form_state['rebuild_values']);
   return $form;
 }
 
@@ -1173,6 +1189,7 @@ function _form_builder_handle_input_elem
   if (!isset($element['#value']) && !array_key_exists('#value', $element)) {
     $value_callback = !empty($element['#value_callback']) ? $element['#value_callback'] : 'form_type_' . $element['#type'] . '_value';
 
+    // Set #value based on submitted input.
     if ($form_state['programmed'] || ($form_state['process_input'] && (!isset($element['#access']) || $element['#access']))) {
       $input = $form_state['input'];
       foreach ($element['#parents'] as $parent) {
@@ -1180,10 +1197,13 @@ function _form_builder_handle_input_elem
       }
       // If we have input for the current element, assign it to the #value property.
       if (!$form_state['programmed'] || isset($input)) {
-        // Call #type_value to set the form value;
+        // Call the value callback to set the form element value.
         if (function_exists($value_callback)) {
           $element['#value'] = $value_callback($element, $input, $form_state);
         }
+        // If there's no value callback or it didn't return a value, then set
+        // the #value to $input. Validation functions will still have a chance
+        // to run.
         if (!isset($element['#value']) && isset($input)) {
           $element['#value'] = $input;
         }
@@ -1193,6 +1213,21 @@ function _form_builder_handle_input_elem
         $element['#needs_validation'] = TRUE;
       }
     }
+    // If rebuilding from previously validated values, set #value based on them.
+    if (!empty($form_state['rebuild_values'])) {
+      $value = $form_state['rebuild_values'];
+      foreach ($element['#parents'] as $parent) {
+        $value = isset($value[$parent]) ? $value[$parent] : NULL;
+      }
+      if (isset($value)) {
+        $element['#value'] = $value;
+      }
+      // New $form, so new validation needed, even if the values were validated
+      // prior to rebuild.
+      if (isset($element['#value']) || (!empty($element['#required']))) {
+        $element['#needs_validation'] = TRUE;
+      }
+    }
     // Load defaults.
     if (!isset($element['#value'])) {
       // Call #type_value without a second argument to request default_value handling.
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.170
diff -u -p -r1.170 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	14 Nov 2009 07:58:49 -0000	1.170
+++ modules/simpletest/drupal_web_test_case.php	17 Nov 2009 19:54:36 -0000
@@ -1500,6 +1500,84 @@ class DrupalWebTestCase extends DrupalTe
     }
   }
 
+
+  /**
+   * Execute a POST request on an AJAX path (normally system/ajax).
+   *
+   * It will be done as usual POST request with SimpleBrowser.
+   *
+   * @param $path
+   *   Location of the post form. A Drupal path.
+   * @param  $edit
+   *   Field data in an associative array. Changes the current input fields
+   *   (where possible) to the values indicated. A checkbox can be set to
+   *   TRUE to be checked and FALSE to be unchecked. Note that when a form
+   *   contains file upload fields, other fields cannot start with the '@'
+   *   character.
+   *   Multiple select fields can be set using name[] and setting each of the
+   *   possible values. Example:
+   *   @code
+   *     $edit = array();
+   *     $edit['name[]'] = array('value1', 'value2');
+   *   @endcode
+   * @param $triggering_element
+   *   Name of the element that triggered the AJAX operation.
+   * @param $ajax_path
+   *   Path to which to post the AJAX request.
+   * @param $options
+   *   Options to be forwarded to url().
+   * @param $headers
+   *   An array containing additional HTTP request headers, each formatted as
+   *   "name: value".
+   */
+  protected function drupalAJAXPost($path, $edit, $triggering_element, $ajax_path = 'system/ajax', array $options = array(), array $headers = array()) {
+    $submit_matches = FALSE;
+    if (isset($path)) {
+      $html = $this->drupalGet($path, $options);
+    }
+
+    if ($this->parse()) {
+      $edit_save = $edit;
+      // Let's iterate over all the forms.
+      $forms = $this->xpath('//form');
+      foreach ($forms as $form) {
+        // We try to set the fields of this form as specified in $edit.
+        $edit = $edit_save;
+        $post = array();
+        $upload = array();
+        $this->handleForm($post, $edit, $upload, NULL, $form);
+        $action = $this->getAbsoluteUrl($ajax_path);
+        $post_array = $post;
+        foreach ($post as $key => $value) {
+          // Encode according to application/x-www-form-urlencoded
+          // Both names and values needs to be urlencoded, according to
+          // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1
+          $post[$key] = urlencode($key) . '=' . urlencode($value);
+        }
+        $post['ajax_triggering_element'] = 'ajax_triggering_element=' . urlencode($triggering_element);
+        $post = implode('&', $post);
+        $out = $this->curlExec(array(CURLOPT_URL => $action, CURLOPT_POST => TRUE, CURLOPT_POSTFIELDS => $post, CURLOPT_HTTPHEADER => $headers));
+        // Ensure that any changes to variables in the other thread are picked up.
+        $this->refreshVariables();
+
+        // Replace original page output with new output from redirected page(s).
+        if (($new = $this->checkForMetaRefresh())) {
+          $out = $new;
+        }
+        $this->verbose('AJAX POST request to: ' . $ajax_path .
+                       '<hr />Ending URL: ' . $this->getUrl() .
+                       '<hr />Fields: ' . highlight_string('<?php ' . var_export($post_array, TRUE), TRUE) .
+                       '<hr />' . $out);
+        return json_decode($out, TRUE);
+
+      }
+      // We have not found a form which contained all fields of $edit.
+      foreach ($edit as $name => $value) {
+        $this->fail(t('Failed to set field @name to @value', array('@name' => $name, '@value' => $value)));
+      }
+    }
+  }
+
   /**
    * Runs cron in the Drupal installed by Simpletest.
    */
Index: modules/simpletest/tests/ajax.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/ajax.test,v
retrieving revision 1.1
diff -u -p -r1.1 ajax.test
--- modules/simpletest/tests/ajax.test	18 Oct 2009 05:14:39 -0000	1.1
+++ modules/simpletest/tests/ajax.test	17 Nov 2009 19:54:36 -0000
@@ -78,3 +78,46 @@ class AJAXCommandsTestCase extends AJAXT
   }
 }
 
+/**
+ * Test that $form_state['values'] is properly delivered to $ajax['callback'].
+ */
+class AJAXFormValuesTestCase extends AJAXTestCase {
+  public static function getInfo() {
+    return array(
+      'name' => 'AJAX Form Values in command',
+      'description' => 'Creates a form, then POSTS to system/ajax to change the form via pseudo-ajax.',
+      'group' => 'AJAX',
+    );
+  }
+
+  /**
+   * Create a simple form with a select, then POST a change to it.
+   */
+  function testSimpleAJAXFormValue() {
+    $web_user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($web_user);
+
+    $form_path = 'ajax_forms_test_get_form';
+
+    $selects_to_test = array('red', 'green', 'blue');
+    foreach($selects_to_test as $item) {
+      $edit = array();
+      $edit['select'] = $item;
+      $commands = $this->drupalAJAXPost($form_path, $edit, 'select');
+      $data_command = $commands[2];
+      $this->assertEqual($data_command['value'], $edit['select']);
+    }
+
+    $values_to_test = array(FALSE, TRUE);
+    foreach($values_to_test as $item) {
+      $edit = array();
+      $edit['checkbox'] = $item;
+      $commands = $this->drupalAJAXPost($form_path, $edit, 'checkbox');
+      $data_command = $commands[2];
+      $value_expected = (int) $item;
+      $value_received = (int) $data_command['value'];
+      $this->assertEqual($value_received, $value_expected);
+    }
+  }
+}
+
Index: modules/simpletest/tests/ajax_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/ajax_test.module,v
retrieving revision 1.1
diff -u -p -r1.1 ajax_test.module
--- modules/simpletest/tests/ajax_test.module	18 Oct 2009 05:14:39 -0000	1.1
+++ modules/simpletest/tests/ajax_test.module	17 Nov 2009 19:54:36 -0000
@@ -22,6 +22,14 @@ function ajax_test_menu() {
     'access callback' => TRUE,
     'type' => MENU_CALLBACK,
   );
+
+  $items['ajax_forms_test_get_form'] = array(
+    'title' => 'Simple AJAX form test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ajax_forms_test_simple_form'),
+    'access callback' => TRUE,
+  );
+
   return $items;
 }
 
@@ -57,3 +65,51 @@ function ajax_test_render_error() {
   ajax_render_error($message);
 }
 
+/**
+ * A basic form used to test form_state['values'] during callback.
+ */
+function ajax_forms_test_simple_form($form, &$form_state) {
+  $form['select'] = array(
+    '#type' => 'select',
+    '#options' => array('red' => 'red', 'green' => 'green', 'blue' => 'blue'),
+    '#ajax' => array(
+      'callback' => 'ajax_forms_test_simple_form_select_callback',
+    ),
+    '#suffix' => "<div id='ajax_selected_color'>No color yet selected</div>",
+  );
+
+  $form['checkbox'] = array(
+    '#type' => 'checkbox',
+    '#title' => 'Test checkbox',
+    '#ajax' => array(
+       'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
+    ),
+    '#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
+  );
+  $form['submit'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save',
+  );
+  return $form;
+}
+
+/**
+ * Form element value callback for 'select' element in ajax_forms_test_simple_form().
+ */
+function ajax_forms_test_simple_form_select_callback($form, $form_state) {
+  $commands = array();
+  $commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
+  $commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
+  return array('#type' => 'ajax_commands', '#ajax_commands' => $commands);
+}
+
+/**
+ * Form element value callback for 'checkbox' element in ajax_forms_test_simple_form().
+ */
+function ajax_forms_test_simple_form_checkbox_callback($form, $form_state) {
+  $commands = array();
+  $commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
+  $commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
+  return array('#type' => 'ajax_commands', '#ajax_commands' => $commands);
+}
+
Index: modules/simpletest/tests/form.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form.test,v
retrieving revision 1.24
diff -u -p -r1.24 form.test
--- modules/simpletest/tests/form.test	15 Nov 2009 21:36:06 -0000	1.24
+++ modules/simpletest/tests/form.test	17 Nov 2009 19:54:36 -0000
@@ -533,3 +533,55 @@ class FormStateValuesCleanTestCase exten
   }
 }
 
+/**
+ * Test that form values are kept or not kept after a rebuild, as appropriate.
+ */
+class FormsFormRebuildTestCase extends DrupalWebTestCase {
+
+  public static function getInfo() {
+    return array(
+      'name'  => 'Form rebuilding',
+      'description'  => 'Tests forms using different rebuild options to make sure values from one step are or are not carried over into the next step, as appropriate.',
+      'group' => 'Form API',
+    );
+  }
+
+  function setUp() {
+    parent::setUp('form_test');
+  }
+
+  /**
+   * Tests form rebuilding for a form that uses $form_state['storage'].
+   */
+  function testUsingStorage() {
+    $user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($user);
+
+    $this->drupalPost('form_test/form-multistep', array('name' => 'foo'), 'Continue');
+    $this->assertText('Friend\'s name', t('The correct step is shown.'));
+    $this->assertFieldByName('name', 'DEFAULT', t('The name field for the next step was correctly set to its default value.'));
+  }
+
+  /**
+   * Tests form rebuilding for a form that uses $form_state['rebuild'].
+   */
+  function testUsingRebuild() {
+    $user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($user);
+
+    $this->drupalPost('form_test/form-rebuild', array('name' => 'foo'), 'Delete');
+    $this->assertFieldByName('name', 'DEFAULT FOR DELETE', t('The name field for the rebuilt form was correctly set to its default value.'));
+  }
+
+  /**
+   * Tests form rebuilding using FORM_REBUILD_FROM_VALUES.
+   */
+  function testUsingProgressiveRebuild() {
+    $user = $this->drupalCreateUser(array('access content'));
+    $this->drupalLogin($user);
+
+    $this->drupalPost('form_test/form-progressive-rebuild', array('groceries[0]' => 'apples'), 'Add grocery item');
+    $this->assertFieldByName('groceries[0]', 'apples', t('A submitted field for the progressively rebuilt form was correctly set to its previously submitted value.'));
+    $this->assertFieldByName('groceries[1]', 'DEFAULT', t('A new field for the progressively rebuilt form was correctly set to its default value.'));
+  }
+}
Index: modules/simpletest/tests/form_test.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/form_test.module,v
retrieving revision 1.14
diff -u -p -r1.14 form_test.module
--- modules/simpletest/tests/form_test.module	15 Nov 2009 21:36:06 -0000	1.14
+++ modules/simpletest/tests/form_test.module	17 Nov 2009 19:54:36 -0000
@@ -78,6 +78,30 @@ function form_test_menu() {
     'type' => MENU_CALLBACK,
   );
 
+  $items['form_test/form-multistep'] = array(
+    'title' => 'Form multistep test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_multistep_test_form'),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+
+  $items['form_test/form-rebuild'] = array(
+    'title' => 'Form rebuild test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_rebuild_test_form'),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+
+  $items['form_test/form-progressive-rebuild'] = array(
+    'title' => 'Form progressive rebuild test',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('form_progressive_rebuild_test_form'),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
+
   return $items;
 }
 
@@ -476,3 +500,194 @@ function _form_test_checkbox_submit($for
   drupal_json_output($form_state['values']);
   exit();
 }
+
+/**
+ * A multistep form.
+ *
+ * Used by form.test to ensure that the 'name' field on step 2 isn't polluted
+ * with data from the different 'name' field from step 1.
+ *
+ * @see form_multistep_test_form_submit().
+ */
+function form_multistep_test_form($form, &$form_state) {
+  if (!isset($form_state['storage']['step'])) {
+    $form_state['storage']['step'] = 1;
+  }
+  if ($form_state['storage']['step'] == 1) {
+    $form['name'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Your name',
+      '#default_value' => 'DEFAULT',
+      '#required' => TRUE,
+    );
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => 'Continue',
+    );
+  }
+  else {
+    $form['name'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Friend\'s name',
+      '#default_value' => 'DEFAULT',
+      '#required' => TRUE,
+    );
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => 'Done',
+    );
+  }
+  return $form;
+}
+
+/**
+ * Multistep form submit callback.
+ */
+function form_multistep_test_form_submit($form, &$form_state) {
+  if ($form_state['storage']['step'] == 1) {
+    $form_state['storage']['name1'] = $form_state['values']['name'];
+    $form_state['storage']['step']++;
+    // By default, just because a form is a multistep form, doesn't mean
+    // the next step is rebuilt without carrying over the input values from
+    // the previous step. If we want the next step to reuse element names
+    // from the previous step without carrying over their values, we need to
+    // say so explicitly.
+    $form_state['rebuild_values'] = array();
+  }
+  else {
+    $form_state['storage']['name2'] = $form_state['values']['name'];
+    drupal_set_message(t("Your name: %name1, Friend's name: %name2", array('%name1' => $form_state['storage']['name1'], '%name2' => $form_state['storage']['name2'])));
+    // After the last step of a multistep form, unsetting $form_state['storage']
+    // is one way to trigger what would normally happen after a form submission
+    // (i.e., a redirect to a specified page or the original page).
+    unset($form_state['storage']);
+  }
+}
+
+/**
+ * A form requiring a rebuild.
+ *
+ * This example is pretty contrived, but the goal is to test something similar
+ * to what is tested by form_multistep_test_form(), but for a form that uses
+ * 'rebuild' instead of 'storage' to trigger a rebuild.
+ *
+ * @see form_rebuild_test_form_submit().
+ */
+function form_rebuild_test_form($form, &$form_state) {
+  if (!isset($form_state['confirm_delete'])) {
+    $form['name'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Your name',
+      '#default_value' => 'DEFAULT',
+    );
+    $form['color'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Your favorite color',
+      '#default_value' => 'DEFAULT',
+    );
+    $form['delete'] = array(
+      '#type' => 'submit',
+      '#value' => 'Delete',
+      '#submit' => array('form_rebuild_test_form_submit_delete'),
+    );
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => 'Save',
+    );
+  }
+  else {
+    $form['name'] = array(
+      '#type' => 'textfield',
+      '#title' => 'Your name',
+      '#default_value' => 'DEFAULT FOR DELETE',
+      '#description' => 'To delete yourself from the system, enter your name.',
+      '#required' => TRUE,
+    );
+    $form['submit'] = array(
+      '#type' => 'submit',
+      '#value' => 'Yes, Really Delete',
+      '#submit' => array('form_rebuild_test_form_submit_delete_confirmed'),
+    );
+  }
+  return $form;
+}
+
+/**
+ * Submit callbacks for form_rebuild_test_form().
+ */
+function form_rebuild_test_form_submit($form, &$form_state) {
+  drupal_set_message(t("Your name: %name, Your favorite color: %color", array('%name' => $form_state['values']['name'], '%color' => $form_state['values']['color'])));
+}
+function form_rebuild_test_form_submit_delete($form, &$form_state) {
+  $form_state['rebuild'] = TRUE;
+  $form_state['confirm_delete'] = TRUE;
+}
+function form_rebuild_test_form_submit_delete_confirmed($form, &$form_state) {
+  drupal_set_message(t("Ok, %name has been deleted.", array('%name' => $form_state['values']['name'])));
+}
+
+/**
+ * A form requiring a rebuild with values preserved.
+ *
+ * Tests the FORM_REBUILD_FROM_VALUES form rebuilding mode to make sure that
+ * incremental additions to the form don't wipe pre-rebuild values.
+ *
+ * @see form_progressive_rebuild_test_form_submit().
+ */
+function form_progressive_rebuild_test_form($form, &$form_state) {
+  $num_grocery_items = isset($form_state['num_grocery_items']) ? $form_state['num_grocery_items'] : 1;
+  $num_movies = isset($form_state['num_movies']) ? $form_state['num_movies'] : 1;
+  $form['#tree'] = TRUE;
+
+  $form['groceries'] = array();
+  for ($i=0; $i < $num_grocery_items; $i++) {
+    $form['groceries'][$i] = array(
+      '#type' => 'textfield',
+      '#title' => 'Grocery item ' . ($i + 1),
+      '#default_value' => 'DEFAULT',
+    );
+  }
+  $form['add_grocery_item'] = array(
+    '#type' => 'submit',
+    '#value' => 'Add grocery item',
+    '#submit' => array('form_progressive_rebuild_test_form_submit_add_item'),
+    '#_which_counter' => 'num_grocery_items',
+  );
+  $form['movies'] = array();
+  for ($i=0; $i < $num_movies; $i++) {
+    $form['movies'][$i] = array(
+      '#type' => 'textfield',
+      '#title' => 'Movie to rent ' . ($i + 1),
+      '#default_value' => 'DEFAULT',
+    );
+  }
+  $form['add_movie'] = array(
+    '#type' => 'submit',
+    '#value' => 'Add movie',
+    '#submit' => array('form_progressive_rebuild_test_form_submit_add_item'),
+    '#_which_counter' => 'num_movies',
+  );
+  $form['submit'] = array(
+    '#prefix' => '<br/><br/>',
+    '#type' => 'submit',
+    '#value' => 'Submit',
+  );
+  return $form;
+}
+
+/**
+ * Submit callbacks for form_progressive_rebuild_test_form().
+ */
+function form_progressive_rebuild_test_form_submit($form, &$form_state) {
+  drupal_set_message(t("Your grocery items: %groceries, Your movies: %movies", array('%groceries' => implode(', ', $form_state['values']['groceries']), '%movies' => implode(', ', $form_state['values']['movies']))));
+}
+function form_progressive_rebuild_test_form_submit_add_item($form, &$form_state) {
+  $form_state['rebuild'] = TRUE;
+  // By default, a rebuild triggered by setting $form_state['rebuild'] results
+  // in a completely fresh rebuild. If we want to carry over the values, we
+  // need to do so explicitly.
+  $form_state['rebuild_values'] = $form_state['values'];
+  $form_state['num_grocery_items'] = count($form_state['values']['groceries']);
+  $form_state['num_movies'] = count($form_state['values']['movies']);
+  $form_state[$form_state['clicked_button']['#_which_counter']]++;
+}
