diff --git modules/simpletest/tests/ajax.test modules/simpletest/tests/ajax.test
index c70e7e4..d5a4fc8 100644
--- modules/simpletest/tests/ajax.test
+++ modules/simpletest/tests/ajax.test
@@ -3,13 +3,90 @@
class AJAXTestCase extends DrupalWebTestCase {
function setUp() {
- parent::setUp('ajax_test');
+ parent::setUp('ajax_test', 'ajax_forms_test');
}
function drupalGetAJAX($path, $query = array()) {
$this->drupalGet($path, array('query' => $query));
return json_decode($this->content, TRUE);
}
+
+
+ /**
+ * 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:
+ * $edit = array();
+ * $edit['name[]'] = array('value1', 'value2');
+ * @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 AJAXPost($path, $edit, $triggering_element, $ajax_path = 'system/ajax', array $options = array(), array $headers = array()) {
+ 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);
+ // debug($post, "POST data");
+ $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('POST request to: ' . $ajax_path .
+ '
Ending URL: ' . $this->getUrl() .
+ '
Fields: ' . highlight_string('' . $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)));
+ }
+ }
+ }
+
}
/**
@@ -78,3 +155,52 @@ class AJAXCommandsTestCase extends AJAXTestCase {
}
}
+/**
+ * Test that $form_state['values'] is properly delivered to $ajax['callback'].
+ * @author rfay
+ *
+ */
+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',
+ );
+ }
+
+ function setUp() {
+ parent::setUp('ajax_forms_test');
+ }
+
+ /**
+ * Create a simple form with a select, then POST a change to it.
+ */
+ function testSimpleAJAXFormValue() {
+ $form_path = 'ajax_forms_test_get_form';
+ $web_user = $this->drupalCreateUser(array('access content'));
+ $this->drupalLogin($web_user);
+ // $page_data = $this->drupalGet($form_path);
+
+ $edit = array();
+
+ $selects_to_test = array('red', 'green', 'blue');
+ foreach($selects_to_test as $item) {
+ $edit['select'] = $item;
+ $commands = $this->AJAXPost($form_path, $edit, 'select');
+ $data_command = $commands[2];
+ $this->assertTrue($data_command['value'] == $edit['select'], "Expected form_state['values'] == '{$edit['select']}') and received '{$data_command['value']}'");
+ }
+
+ $values_to_test = array(FALSE,TRUE);
+ foreach($values_to_test as $item) {
+ $edit['checkbox'] = $item;
+ $commands = $this->AJAXPost($form_path, $edit, 'checkbox');
+ $data_command = $commands[2];
+ $value_expected = (int)$item;
+ $value_received = (int)$data_command['value'];
+ $this->assertTrue($value_received == $value_expected, "Expected form_state['values'] == '$value_expected') and received '{$value_received}'");
+ }
+ }
+}
diff --git modules/simpletest/tests/ajax_forms_test.info modules/simpletest/tests/ajax_forms_test.info
new file mode 100644
index 0000000..367d088
--- /dev/null
+++ modules/simpletest/tests/ajax_forms_test.info
@@ -0,0 +1,8 @@
+; $Id: database_test.info,v 1.2 2008/10/09 22:51:40 webchick Exp $
+name = "AJAX form test mock module"
+description = "Test for AJAX form calls."
+core = 7.x
+package = Testing
+files[] = ajax_forms_test.module
+version = VERSION
+;hidden = TRUE
diff --git modules/simpletest/tests/ajax_forms_test.module modules/simpletest/tests/ajax_forms_test.module
new file mode 100644
index 0000000..7bcdc99
--- /dev/null
+++ modules/simpletest/tests/ajax_forms_test.module
@@ -0,0 +1,65 @@
+ 'AJAX Forms Simple Form Test',
+ 'page callback' => 'ajax_forms_test_get_form',
+ 'access callback' => TRUE,
+ );
+ return $items;
+}
+
+function ajax_forms_test_get_form() {
+ $form = drupal_get_form('ajax_forms_test_simple_form');
+
+ $output[] = array('#markup' => "#form_id={$form['#form_id']} and #build_id={$form['#build_id']}.");
+ $output[] = $form;
+ return $output;
+
+}
+function ajax_forms_test_simple_form($form, &$form_state) {
+ $form = array();
+ $form['select'] = array(
+ '#type' => 'select',
+ '#options' => array('red' => 'red', 'green' => 'green', 'blue' => 'blue'),
+ '#ajax' => array(
+ 'callback' => 'ajax_forms_test_simple_form_select_callback',
+ ),
+ '#suffix' => "No color yet selected
",
+ );
+
+ $form['checkbox'] = array(
+ '#type' => 'checkbox',
+ '#title' => 'Test checkbox',
+ '#ajax' => array(
+ 'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
+ ),
+ '#suffix' => 'No action yet
',
+ );
+ $form['submit'] = array(
+ '#type' => 'submit',
+ '#value' => 'submit',
+ );
+ return $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);
+}
+
+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);
+}
\ No newline at end of file