diff --git a/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php index affe910afee1eb14ba91b48b97e4a7ff0a3c5b91..5f25dd75d651bebbaf3e9182a8252d840ffdfbd6 100644 --- a/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php +++ b/core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php @@ -52,6 +52,11 @@ public function buildForm(array $form, FormStateInterface $form_state) { '#default_value' => 2, ]; + $form['save'] = [ + '#type' => 'submit', + '#value' => $this->t('Save'), + ]; + return $form; } diff --git a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php index f1293212ddcc76887186639360832d2951b1b379..c88949df74403232dec1fba4a9cb6825e702c9ef 100644 --- a/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php +++ b/core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php @@ -2,6 +2,8 @@ namespace Drupal\FunctionalTests; +use Behat\Mink\Exception\ElementNotFoundException; +use Behat\Mink\Exception\ExpectationException; use Behat\Mink\Selector\Xpath\Escaper; use Drupal\Component\Render\FormattableMarkup; use Drupal\Component\Utility\Xss; @@ -262,12 +264,23 @@ protected function assertNoFieldByName($name, $value = '') { * However, the default value ('') asserts that the field value is an empty * string. * + * @throws \Behat\Mink\Exception\ElementNotFoundException + * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldExists() or * $this->assertSession()->fieldValueEquals() instead. */ - protected function assertFieldById($id, $value = NULL) { - $this->assertFieldByName($id, $value); + protected function assertFieldById($id, $value = '') { + $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]); + $field = $this->getSession()->getPage()->find('xpath', $xpath); + + if (empty($field)) { + throw new ElementNotFoundException($this->getSession()->getDriver(), 'form field', 'id', $field); + } + + if ($value !== NULL) { + $this->assertEquals($value, $field->getValue()); + } } /** @@ -410,16 +423,26 @@ protected function assertNoLinkByHref($href) { * while still checking that the field doesn't exist. However, the default * value ('') asserts that the field value is not an empty string. * + * @throws \Behat\Mink\Exception\ExpectationException + * * @deprecated Scheduled for removal in Drupal 9.0.0. * Use $this->assertSession()->fieldNotExists() or * $this->assertSession()->fieldValueNotEquals() instead. */ protected function assertNoFieldById($id, $value = '') { - if ($this->getSession()->getPage()->findField($id) && isset($value)) { - $this->assertSession()->fieldValueNotEquals($id, (string) $value); + $xpath = $this->assertSession()->buildXPathQuery('//textarea[@id=:value]|//input[@id=:value]|//select[@id=:value]', [':value' => $id]); + $field = $this->getSession()->getPage()->find('xpath', $xpath); + + // Return early if the field could not be found as expected. + if ($field === NULL) { + return; } - else { - $this->assertSession()->fieldNotExists($id); + + if (!isset($value)) { + throw new ExpectationException(sprintf('Id "%s" appears on this page, but it should not.', $id), $this->getSession()->getDriver()); + } + elseif ($value === $field->getValue()) { + throw new ExpectationException(sprintf('Failed asserting that %s is not equal to %s', $field->getValue(), $value), $this->getSession()->getDriver()); } } diff --git a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php index 8f5091411355b962ce0ce5bc3ba62b416654d3ef..9931804232d8f155852e632d9917624add12e461 100644 --- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php @@ -175,7 +175,7 @@ public function testLegacyFieldAsserts() { // Test that the assertion fails correctly if no value is passed in. try { - $this->assertNoFieldById('description'); + $this->assertNoFieldById('edit-description'); $this->fail('The "description" field, with no value was not found.'); } catch (ExpectationException $e) { @@ -184,13 +184,36 @@ public function testLegacyFieldAsserts() { // Test that the assertion fails correctly if a NULL value is passed in. try { - $this->assertNoFieldById('name', NULL); + $this->assertNoFieldById('edit-name', NULL); $this->fail('The "name" field was not found.'); } catch (ExpectationException $e) { $this->pass('The "name" field was found.'); } + $this->assertFieldById('edit-name', NULL); + $this->assertFieldById('edit-name', 'Test name'); + $this->assertFieldById('edit-description', NULL); + $this->assertFieldById('edit-description'); + + // Test that the assertion fails correctly if no value is passed in. + try { + $this->assertFieldById('edit-name'); + $this->fail('The "edit-name" field with no value was found.'); + } + catch (\PHPUnit_Framework_ExpectationFailedException $e) { + $this->pass('The "edit-name" field with no value was not found.'); + } + + // Test that the assertion fails correctly if NULL is passed in. + try { + $this->assertFieldById('name', NULL); + $this->fail('The "name" field was found.'); + } + catch (ExpectationException $e) { + $this->pass('The "name" field was not found.'); + } + $this->assertNoFieldByName('name'); $this->assertNoFieldByName('name', 'not the value'); $this->assertNoFieldByName('notexisting'); @@ -222,6 +245,28 @@ public function testLegacyFieldAsserts() { catch (ExpectationException $e) { $this->pass($e->getMessage()); } + + $this->assertFieldById('edit-save', NULL); + // Test that the assertion fails correctly if the field value is passed in + // rather than the id. + try { + $this->assertFieldById('Save', NULL); + $this->fail('The field with id of "Save" was found.'); + } + catch (ExpectationException $e) { + $this->pass($e->getMessage()); + } + + $this->assertNoFieldById('Save', NULL); + // Test that the assertion fails correctly if the id of an actual field is + // passed in. + try { + $this->assertNoFieldById('edit-save', NULL); + $this->fail('The field with id of "edit-save" was not found.'); + } + catch (ExpectationException $e) { + $this->pass($e->getMessage()); + } } /**