diff --git a/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php b/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php index c4528a5..f4e38d6 100644 --- a/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php +++ b/core/modules/simpletest/src/Tests/SimpleTestBrowserTest.php @@ -131,7 +131,7 @@ public function testTestingThroughUI() { // A PHPUnit unit test. 'Drupal\Tests\action\Unit\Menu\ActionLocalTasksTest', // A PHPUnit functional test. - 'Drupal\FunctionalTests\BrowserTestBaseTest', + 'Drupal\FunctionalTests\Breadcrumb\Breadcrumb404Test', ); foreach ($tests as $test) { diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index 2393f61..4426d62 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -2,6 +2,7 @@ namespace Drupal\FunctionalTests; +use Behat\Mink\Selector\Xpath\Escaper; use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Xss; use Drupal\KernelTests\AssertLegacyTrait as BaseAssertLegacyTrait; @@ -510,6 +511,103 @@ protected function assertNoFieldChecked($id) { } /** + * 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 with t(). + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->xpath() instead and check the values directly in the test. + */ + protected function assertFieldByXPath($xpath, $value = NULL, $message = '') { + $fields = $this->xpath($xpath); + + $this->assertFieldsByValue($fields, $value, $message); + } + + /** + * 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 with t(). + * + * @deprecated Scheduled for removal in Drupal 9.0.0. + * Use $this->assertSession()->fieldNotExists() or + * $this->assertSession()->fieldValueNotEquals() instead. + */ + protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') { + $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->getAttribute('value') == $value) { + $found = TRUE; + } + } + } + } + return $this->assertFalse($fields && $found, $message); + } + + /** + * 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 81fa024..490ab52 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -124,9 +124,9 @@ public function testError() { } /** - * Tests legacy asserts. + * Tests legacy text asserts. */ - public function testLegacyAsserts() { + public function testLegacyTextAsserts() { $this->drupalGet('test-encoded'); $dangerous = 'Bad html '; $sanitized = Html::escape($dangerous); @@ -134,4 +134,26 @@ public function testLegacyAsserts() { $this->assertText($sanitized); } + /** + * Tests legacy XPath asserts. + */ + public function testLegacyXPathAsserts() { + $account = $this->drupalCreateUser(['administer users'], 'test'); + $this->drupalLogin($account); + + $this->drupalGet('admin/people'); + $this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL); + $this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]/span'), $account->getAccountName()); + $this->assertFieldByXPath('//table/tbody/tr[2]/td[1]/span', $account->getAccountName()); + + $this->drupalGet('user/' . $account->id() . '/edit'); + $this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), $account->getAccountName()); + $this->assertFieldByXPath("//input[@id = 'edit-name']", $account->getAccountName()); + $this->assertFieldsByValue($this->xpath("//select[@id = 'edit-timezone--2']"), 'Australia/Sydney'); + $this->assertFieldByXPath("//select[@id = 'edit-timezone--2']", 'Australia/Sydney'); + + $this->assertNoFieldByXPath('//notexisting'); + $this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value'); + } + }