diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php index e19f319..dac896e 100644 --- a/core/modules/simpletest/src/AssertContentTrait.php +++ b/core/modules/simpletest/src/AssertContentTrait.php @@ -14,6 +14,8 @@ */ trait AssertContentTrait { + use AssertXpathTrait; + /** * The current raw content. * @@ -999,46 +1001,6 @@ protected function getSelectedItem(\SimpleXMLElement $element) { } /** - * Asserts that a field does not exist or its value does not match, by XPath. - * - * @param string $xpath - * XPath used to find the field. - * @param string $value - * (optional) Value of the field, to assert that the field's value on the - * page does not match it. - * @param string $message - * (optional) A message to display with the assertion. Do not translate - * messages: use \Drupal\Component\Utility\SafeMarkup::format() 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 assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { - $fields = $this->xpath($xpath); - - // If value specified then check array for match. - $found = TRUE; - if (isset($value)) { - $found = FALSE; - if ($fields) { - foreach ($fields as $field) { - if ($field['value'] == $value) { - $found = TRUE; - } - } - } - } - return $this->assertFalse($fields && $found, $message, $group); - } - - /** * Asserts that a field exists with the given name and value. * * @param string $name diff --git a/core/modules/simpletest/src/AssertXpathTrait.php b/core/modules/simpletest/src/AssertXpathTrait.php new file mode 100644 index 0000000..1535bee --- /dev/null +++ b/core/modules/simpletest/src/AssertXpathTrait.php @@ -0,0 +1,76 @@ +xpath($xpath); + + return $this->assertFieldsByValue($fields, $value, $message, $group); + } + + /** + * Asserts that a field does not exist or its value does not match, by XPath. + * + * @param string $xpath + * XPath used to find the field. + * @param string $value + * (optional) Value of the field, to assert that the field's value on the + * page does not match it. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages: use \Drupal\Component\Utility\SafeMarkup::format() 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 assertNoFieldByXPath($xpath, $value = NULL, $message = '', $group = 'Other') { + $fields = $this->xpath($xpath); + + // If value specified then check array for match. + $found = TRUE; + if (isset($value)) { + $found = FALSE; + if ($fields) { + foreach ($fields as $field) { + if ($field['value'] == $value) { + $found = TRUE; + } + } + } + } + return $this->assertFalse($fields && $found, $message, $group); + } + +} diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 5785a7d..2d23be1 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -2,7 +2,9 @@ namespace Drupal\FunctionalTests; +use Behat\Mink\Selector\Xpath\Escaper; use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait; +use Drupal\simpletest\AssertXpathTrait; /** * Provides convenience methods for assertions in browser tests. @@ -16,6 +18,7 @@ trait AssertLegacyTrait { use BaseAssertLegacyTrait; + use AssertXpathTrait; /** * Asserts that the element with the given CSS selector is present. @@ -464,6 +467,47 @@ protected function assertNoFieldChecked($id) { } /** + * Asserts that a field exists in the current page with a given Xpath result. + * + * @param \Behat\Mink\Element\NodeElement[] $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. + * @param string $message + * (optional) A message to display with the assertion. Do not translate + * messages with t(). + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Iterate over the fields yourself instead and directly check the values in + * the test. + */ + protected function assertFieldsByValue($fields, $value = NULL, $message = '') { + // If value specified then check array for match. + $found = TRUE; + if (isset($value)) { + $found = FALSE; + if ($fields) { + foreach ($fields as $field) { + if ($field->getAttribute('value') == $value) { + // Input element with correct value. + $found = TRUE; + } + elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) { + // Select element with an option. + $found = TRUE; + } + elseif ($field->getText() == $value) { + // Text area with correct text. + $found = TRUE; + } + } + } + } + $this->assertTrue($fields && $found, $message); + } + + /** * Passes if the raw text IS found escaped on the loaded page, fail otherwise. * * Raw text refers to the raw HTML that the page generated. diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index e4979d3..0024048 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -86,4 +86,22 @@ public function testError() { $this->drupalGet('test-error'); } + /** + * Tests that legacy assertions work. + */ + public function testAssertions() { + $account = $this->drupalCreateUser(['administer users'], 'test'); + $this->drupalLogin($account); + + $this->drupalGet('admin/people'); + $this->assertFieldByXpath('//table/tbody/tr[2]/td[1]/span', $account->getAccountName()); + + $this->drupalGet('user/' . $account->id() . '/edit'); + $this->assertFieldByXpath("//input[@id = 'edit-name']", $account->getAccountName()); + $this->assertFieldByXpath("//select[@id = 'edit-timezone--2']", 'Australia/Sydney'); + + $this->assertNoFieldByXPath('//notexisting'); + $this->assertNoFieldByXpath("//input[@id = 'edit-name']", 'wrong value'); + } + }