summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-04-03 14:48:52 (GMT)
committerAlex Pott2017-04-03 14:48:52 (GMT)
commit4c01abe205914f3d692438b250a466df4609940f (patch)
treea0aceca577f773a7c5cc5a22af00808fc70e9563
parente2554cedeb1e533fcd35a2ad00899e67375f332a (diff)
Issue #2862947 by michielnugter, Jo Fitzgerald, boaloysius, klausi, alexpott, GoZ: Incorrect field assertions in AssertLegacyTrait
-rw-r--r--core/modules/system/tests/modules/test_page_test/src/Form/TestForm.php5
-rw-r--r--core/tests/Drupal/FunctionalTests/AssertLegacyTrait.php35
-rw-r--r--core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php49
3 files changed, 81 insertions, 8 deletions
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 affe910..5f25dd7 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 @@ class TestForm extends FormBase {
'#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 f129321..c88949d 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 @@ trait AssertLegacyTrait {
* 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 @@ trait AssertLegacyTrait {
* 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 8f50914..9931804 100644
--- a/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
+++ b/core/tests/Drupal/FunctionalTests/BrowserTestBaseTest.php
@@ -175,7 +175,7 @@ class BrowserTestBaseTest extends BrowserTestBase {
// 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 @@ class BrowserTestBaseTest extends BrowserTestBase {
// 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 @@ class BrowserTestBaseTest extends BrowserTestBase {
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());
+ }
}
/**