diff options
Diffstat (limited to 'modules/simpletest/tests')
-rw-r--r-- | modules/simpletest/tests/ajax.test | 89 | ||||
-rw-r--r-- | modules/simpletest/tests/common.test | 6 | ||||
-rw-r--r-- | modules/simpletest/tests/form.test | 41 | ||||
-rw-r--r-- | modules/simpletest/tests/form_test.module | 29 |
4 files changed, 136 insertions, 29 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. diff --git a/modules/simpletest/tests/common.test b/modules/simpletest/tests/common.test index c1d294e7d..c6d0d6369 100644 --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -748,15 +748,15 @@ class DrupalHTMLIdentifierTestCase extends DrupalUnitTestCase { $this->assertIdentical(drupal_html_id('invalid,./:@\\^`{Üidentifier'), 'invalididentifier', t('Strip invalid characters.')); // Verify Drupal coding standards are enforced. - $this->assertIdentical(drupal_html_id('ID NAME_[1]'), 'id-name--1', t('Enforce Drupal coding standards.')); + $this->assertIdentical(drupal_html_id('ID NAME_[1]'), 'id-name-1', t('Enforce Drupal coding standards.')); // Reset the static cache so we can ensure the unique id count is at zero. drupal_static_reset('drupal_html_id'); // Clean up IDs with invalid starting characters. $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id', t('Test the uniqueness of IDs #1.')); - $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id-2', t('Test the uniqueness of IDs #2.')); - $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id-3', t('Test the uniqueness of IDs #3.')); + $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id--2', t('Test the uniqueness of IDs #2.')); + $this->assertIdentical(drupal_html_id('test-unique-id'), 'test-unique-id--3', t('Test the uniqueness of IDs #3.')); } } diff --git a/modules/simpletest/tests/form.test b/modules/simpletest/tests/form.test index 20fbbb0f5..c5639be87 100644 --- a/modules/simpletest/tests/form.test +++ b/modules/simpletest/tests/form.test @@ -813,35 +813,28 @@ class FormsRebuildTestCase extends DrupalWebTestCase { $this->web_user = $this->drupalCreateUser(array('create page content')); $this->drupalLogin($this->web_user); - // Get the form for adding a 'page' node. Save the content in a local - // variable, because drupalPostAJAX() will replace $this->content. + // Get the form for adding a 'page' node. Submit an "add another item" AJAX + // submission and verify it worked by ensuring the updated page has two text + // field items in the field for which we just added an item. $this->drupalGet('node/add/page'); - $content = $this->content; - - // Submit an "add another item" AJAX submission and verify it worked by - // ensuring it returned two text fields. - $commands = $this->drupalPostAJAX(NULL, array(), array('field_ajax_test_add_more' => t('Add another item'))); - $fragment = simplexml_load_string('<div>' . $commands[1]['data'] . '</div>'); - $this->assert(count($fragment->xpath('//input[@type="text"]')) == 2, t('AJAX submission succeeded.')); - - // Submit the form with the non-AJAX "Save" button. Leave the title field - // blank to trigger a validation error. Restore $this->content first, as - // drupalPost() needs that to contain the form, not the JSON string left by - // drupalPostAJAX(). - // @todo While not necessary for this test, we would be emulating the - // browser better by calling drupalPost() with the AJAX-modified content - // rather than with the original content from the drupalGet(), but that's - // not possible with the current implementation of drupalPostAJAX(). See - // http://drupal.org/node/384992 - $this->drupalSetContent($content); + preg_match('/jQuery\.extend\(Drupal\.settings, (.*?)\);/', $this->content, $matches); + $settings = drupal_json_decode($matches[1]); + $button = $this->xpath('//input[@name="field_ajax_test_add_more"]'); + $button_id = (string) $button[0]['id']; + $this->drupalPostAJAX(NULL, array(), array('field_ajax_test_add_more' => t('Add another item')), 'system/ajax', array(), array(), 'page-node-form', $settings['ajax'][$button_id]); + $this->assert(count($this->xpath('//div[contains(@class, "field-name-field-ajax-test")]//input[@type="text"]')) == 2, t('AJAX submission succeeded.')); + + // Submit the form with the non-AJAX "Save" button, leaving the title field + // blank to trigger a validation error, and ensure that a validation error + // occurred, because this test is for testing what happens when a form is + // re-rendered without being re-built, which is what happens when there's + // a validation error. $this->drupalPost(NULL, array(), t('Save')); - - // Ensure that a validation error occurred, since this test is for testing - // what happens to the form action after a validation error. $this->assertText('Title field is required.', t('Non-AJAX submission correctly triggered a validation error.')); // Ensure that the form's action is correct. - $this->assertFieldByXPath('//form[@id="page-node-form" and @action="' . url('node/add/page') . '"]', NULL, t('Re-rendered form contains the correct action value.')); + $forms = $this->xpath('//form[contains(@class, "node-page-form")]'); + $this->assert(count($forms) == 1 && $forms[0]['action'] == url('node/add/page'), t('Re-rendered form contains the correct action value.')); } } diff --git a/modules/simpletest/tests/form_test.module b/modules/simpletest/tests/form_test.module index 09ca8f81c..d189fe56b 100644 --- a/modules/simpletest/tests/form_test.module +++ b/modules/simpletest/tests/form_test.module @@ -126,6 +126,18 @@ function form_test_menu() { 'type' => MENU_CALLBACK, ); + if (module_exists('node')) { + $items['form-test/two-instances-of-same-form'] = array( + 'title' => 'AJAX test with two form instances', + 'page callback' => 'form_test_two_instances', + 'access callback' => 'node_access', + 'access arguments' => array('create', 'page'), + 'file path' => drupal_get_path('module', 'node'), + 'file' => 'node.pages.inc', + 'type' => MENU_CALLBACK, + ); + } + return $items; } @@ -1040,3 +1052,20 @@ function form_test_user_register_form_rebuild($form, &$form_state) { drupal_set_message('Form rebuilt.'); $form_state['rebuild'] = TRUE; } + +/** + * Menu callback that returns two instances of the node form. + */ +function form_test_two_instances() { + global $user; + $node1 = (object) array( + 'uid' => $user->uid, + 'name' => (isset($user->name) ? $user->name : ''), + 'type' => 'page', + 'language' => LANGUAGE_NONE, + ); + $node2 = clone($node1); + $return['node_form_1'] = drupal_get_form('page_node_form', $node1); + $return['node_form_2'] = drupal_get_form('page_node_form', $node2); + return $return; +} |