Commit 4c01abe2 authored by alexpott's avatar alexpott

Issue #2862947 by michielnugter, Jo Fitzgerald, boaloysius, klausi, alexpott,...

Issue #2862947 by michielnugter, Jo Fitzgerald, boaloysius, klausi, alexpott, GoZ: Incorrect field assertions in AssertLegacyTrait
parent e2554ced
......@@ -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;
}
......
......@@ -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());
}
}
......
......@@ -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());
}
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment