diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php index 240aea9..d6ac7bd 100644 --- a/core/modules/simpletest/src/AssertContentTrait.php +++ b/core/modules/simpletest/src/AssertContentTrait.php @@ -822,14 +822,13 @@ protected function assertThemeOutput($callback, array $variables = array(), $exp } /** - * Asserts that a field exists in the current page by the given XPath. + * Asserts that a field exists in the current page with a given xpath result. * - * @param string $xpath - * XPath used to find the field. + * @param \SimpleXmlElement[] $fields + * Xml elements. * @param string $value - * (optional) Value of the field to assert. You may pass in NULL (default) - * to skip checking the actual value, while still checking that the field - * exists. + * Value of the field to assert. You may pass in NULL (default) to skip + * checking the actual value, while still checking that the field exists. * @param string $message * (optional) A message to display with the assertion. Do not translate * messages: use format_string() to embed variables in the message text, not @@ -843,9 +842,7 @@ protected function assertThemeOutput($callback, array $variables = array(), $exp * @return bool * TRUE on pass, FALSE on fail. */ - protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { - $fields = $this->xpath($xpath); - + protected function assertFieldsByValue($fields, $value = NULL, $message = '', $group = 'Other') { // If value specified then check array for match. $found = TRUE; if (isset($value)) { @@ -881,6 +878,34 @@ protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $gro } /** + * Asserts that a field exists in the current page by the given XPath. + * + * @param string $xpath + * XPath used to find the field. + * @param string $value + * (optional) Value of the field to assert. You may pass in NULL (default) + * to skip checking the actual value, while still checking that the field + * exists. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use format_string() to embed variables in the message text, not + * t(). If left blank, a default message will be displayed. + * @param string $group + * (optional) The group this message is in, which is displayed in a column + * in test output. Use 'Debug' to indicate this is debugging output. Do not + * translate this string. Defaults to 'Other'; most tests do not override + * this default. + * + * @return bool + * TRUE on pass, FALSE on fail. + */ + protected function assertFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { + $fields = $this->xpath($xpath); + + return $this->assertFieldsByValue($fields, $value, $message, $group); + } + + /** * Get the selected value from a select field. * * @param \SimpleXmlElement $element diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 79f2e39..6578078 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -19,6 +19,7 @@ use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\EventSubscriber\AjaxSubscriber; use Drupal\Core\EventSubscriber\MainContentViewSubscriber; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; @@ -1680,7 +1681,7 @@ protected function drupalPostAjaxForm($path, $edit, $triggering_element, $ajax_p $extra_post[$key] = $value; } } - $extra_post['ajax'] = 1; + $extra_post[AjaxSubscriber::AJAX_REQUEST_PARAMETER] = 1; $extra_post += $this->getAjaxPageStatePostData(); // Now serialize all the $extra_post values, and prepend it with an '&'. $extra_post = '&' . $this->serializePostValues($extra_post); diff --git a/core/modules/system/src/Tests/Ajax/MultiFormTest.php b/core/modules/system/src/Tests/Ajax/MultiFormTest.php index 449861c..87a00fe 100644 --- a/core/modules/system/src/Tests/Ajax/MultiFormTest.php +++ b/core/modules/system/src/Tests/Ajax/MultiFormTest.php @@ -58,10 +58,9 @@ function testMultiForm() { // each Ajax submission, but these variables are stable and help target the // desired elements. $field_name = 'field_ajax_test'; - $field_xpaths = array( - 'node-page-form' => '//form[@id="node-page-form"]//div[contains(@class, "field-name-field-ajax-test")]', - 'node-page-form--2' => '//form[@id="node-page-form--2"]//div[contains(@class, "field-name-field-ajax-test")]', - ); + + $form_xpath = '//form[starts-with(@id, "node-page-form")]'; + $field_xpath = '//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 . '"]'; @@ -71,19 +70,28 @@ function testMultiForm() { // 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 $field_xpath) { - $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == 1, 'Found the correct number of field items on the initial page.'); - $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, 'Found the "add more" button on the initial page.'); + + $fields = $this->xpath($form_xpath . $field_xpath); + $this->assertEqual(count($fields), 2); + foreach ($fields as $field) { + $this->assertEqual(count($field->xpath('.' . $field_items_xpath_suffix)), 1, 'Found the correct number of field items on the initial page.'); + $this->assertFieldsByValue($field->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button on the initial page.'); } + $this->assertNoDuplicateIds(t('Initial page contains unique IDs'), 'Other'); // Submit the "add more" button of each form twice. After each corresponding // page update, ensure the same as above. - foreach ($field_xpaths as $form_html_id => $field_xpath) { - for ($i = 0; $i < 2; $i++) { + for ($i = 0; $i < 2; $i++) { + $forms = $this->xpath($form_xpath); + foreach ($forms as $offset => $form) { + $form_html_id = (string) $form['id']; $this->drupalPostAjaxForm(NULL, array(), array($button_name => $button_value), 'system/ajax', array(), array(), $form_html_id); - $this->assert(count($this->xpath($field_xpath . $field_items_xpath_suffix)) == $i+2, 'Found the correct number of field items after an AJAX submission.'); - $this->assertFieldByXPath($field_xpath . $button_xpath_suffix, NULL, 'Found the "add more" button after an AJAX submission.'); + $form = $this->xpath($form_xpath)[$offset]; + $field = $form->xpath('.' . $field_xpath); + + $this->assertEqual(count($field[0]->xpath('.' . $field_items_xpath_suffix)), $i+2, 'Found the correct number of field items after an AJAX submission.'); + $this->assertFieldsByValue($field[0]->xpath('.' . $button_xpath_suffix), NULL, 'Found the "add more" button after an AJAX submission.'); $this->assertNoDuplicateIds(t('Updated page contains unique IDs'), 'Other'); } }