diff options
author | Dries Buytaert <dries@buytaert.net> | 2010-04-07 17:30:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2010-04-07 17:30:43 +0000 |
commit | d5ce7f5281373044b8e1df6d95287270c0b33145 (patch) | |
tree | 0105e507304fc399d892bcca8f6805b3ae3d4739 /modules/simpletest/tests/ajax.test | |
parent | 27aa5b1b174cb24ac10d3524773d9f6412d2297d (diff) | |
download | brdo-d5ce7f5281373044b8e1df6d95287270c0b33145.tar.gz brdo-d5ce7f5281373044b8e1df6d95287270c0b33145.tar.bz2 |
- Patch #384992 by effulgentsia, sun: drupal_html_id() does not work correctly in AJAX context with multiple forms on page.
Diffstat (limited to 'modules/simpletest/tests/ajax.test')
-rw-r--r-- | modules/simpletest/tests/ajax.test | 89 |
1 files changed, 87 insertions, 2 deletions
diff --git a/modules/simpletest/tests/ajax.test b/modules/simpletest/tests/ajax.test index ca6f718dd..f6e2e28de 100644 --- a/modules/simpletest/tests/ajax.test +++ b/modules/simpletest/tests/ajax.test @@ -2,8 +2,8 @@ // $Id$ class AJAXTestCase extends DrupalWebTestCase { - function setUp() { - parent::setUp('ajax_test', 'ajax_forms_test'); + function setUp($modules = array()) { + parent::setUp(array_unique(array_merge(array('ajax_test', 'ajax_forms_test'), $modules))); } /** @@ -204,6 +204,91 @@ class AJAXFormValuesTestCase extends AJAXTestCase { } } +/** + * Tests that AJAX-enabled forms work when multiple instances of the same form are on a page. + */ +class AJAXMultiFormTestCase extends AJAXTestCase { + public static function getInfo() { + return array( + 'name' => 'AJAX multi form', + 'description' => 'Tests that AJAX-enabled forms work when multiple instances of the same form are on a page.', + 'group' => 'AJAX', + ); + } + + function setUp() { + parent::setUp(array('form_test')); + + // Create a multi-valued field for 'page' nodes to use for AJAX testing. + $field_name = 'field_ajax_test'; + $field = array( + 'field_name' => $field_name, + 'type' => 'text', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + ); + field_create_field($field); + $instance = array( + 'field_name' => $field_name, + 'entity_type' => 'node', + 'bundle' => 'page', + ); + field_create_instance($instance); + + // Login a user who can create 'page' nodes. + $this->web_user = $this->drupalCreateUser(array('create page content')); + $this->drupalLogin($this->web_user); + } + + /** + * Test that a page with the 'page_node_form' included twice works correctly. + */ + function testMultiForm() { + // HTML IDs for elements within the field are potentially modified with + // each AJAX submission, but these variables are stable and help target the + // desired elements. + $field_name = 'field_ajax_test'; + $field_xpaths = array( + 'page-node-form' => '//form[@id="page-node-form"]//div[contains(@class, "field-name-field-ajax-test")]', + 'page-node-form--2' => '//form[@id="page-node-form--2"]//div[contains(@class, "field-name-field-ajax-test")]', + ); + $button_name = $field_name . '_add_more'; + $button_value = t('Add another item'); + $button_xpath_suffix = '//input[@name="' . $button_name . '"]'; + $field_items_xpath_suffix = '//input[@type="text"]'; + + // Ensure the initial page contains both node forms and the correct number + // of field items and "add more" button for the multi-valued field within + // each form. + $this->drupalGet('form-test/two-instances-of-same-form'); + foreach ($field_xpaths as $form_id => $field_xpath) { + $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == 1, t('Found the correct number of field items on the initial page.')); + $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, t('Found the "add more" button on the initial page.')); + } + // @todo Legacy bug of duplicate ids for filter-guidelines-FORMAT. See + // http://drupal.org/node/755566. + $this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other', array('filter-guidelines-1', 'filter-guidelines-2', 'filter-guidelines-3')); + + // Submit the "add more" button of each form twice. After each corresponding + // page update, ensure the same as above. To successfully implement + // consecutive AJAX submissions, we need to manage $settings as ajax.js + // does for Drupal.settings. + preg_match('/jQuery\.extend\(Drupal\.settings, (.*?)\);/', $this->content, $matches); + $settings = drupal_json_decode($matches[1]); + foreach ($field_xpaths as $form_id => $field_xpath) { + for ($i=0; $i<2; $i++) { + $button = $this->xpath($field_xpath . $button_xpath_suffix); + $button_id = (string) $button[0]['id']; + $commands = $this->drupalPostAJAX(NULL, array(), array($button_name => $button_value), 'system/ajax', array(), array(), $form_id, $settings['ajax'][$button_id]); + $settings = array_merge_recursive($settings, $commands[0]['settings']); + $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == $i+2, t('Found the correct number of field items after an AJAX submission.')); + $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, t('Found the "add more" button after an AJAX submission.')); + // @todo Legacy bug of duplicate ids for filter-guidelines-FORMAT. See + // http://drupal.org/node/755566. + $this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other', array('filter-guidelines-1', 'filter-guidelines-2', 'filter-guidelines-3')); + } + } + } +} /** * Miscellaneous AJAX tests using ajax_test module. |