Commit 7e3c8582 authored by alexpott's avatar alexpott

Issue #2859308 by Jo Fitzgerald: Improve assertNoFieldById() compatibility for empty strings

parent d66623ef
......@@ -35,6 +35,12 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => 'Test name',
];
$form['description'] = [
'#type' => 'textfield',
'#title' => 'Description',
'#default_value' => '',
];
$form['options'] = [
'#type' => 'select',
'#title' => 'Options',
......
......@@ -412,7 +412,7 @@ protected function assertNoLinkByHref($href) {
* $this->assertSession()->fieldValueNotEquals() instead.
*/
protected function assertNoFieldById($id, $value = '') {
if ($this->getSession()->getPage()->findField($id)) {
if ($this->getSession()->getPage()->findField($id) && isset($value)) {
$this->assertSession()->fieldValueNotEquals($id, (string) $value);
}
else {
......
......@@ -2,6 +2,7 @@
namespace Drupal\FunctionalTests;
use Behat\Mink\Exception\ExpectationException;
use Drupal\Component\Utility\Html;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
......@@ -148,9 +149,9 @@ public function testLegacyTextAsserts() {
}
/**
* Tests legacy XPath asserts.
* Tests legacy field asserts.
*/
public function testLegacyXPathAsserts() {
public function testLegacyFieldAsserts() {
$this->drupalGet('test-field-xpath');
$this->assertFieldsByValue($this->xpath("//h1[@class = 'page-title']"), NULL);
$this->assertFieldsByValue($this->xpath('//table/tbody/tr[2]/td[1]'), 'one');
......@@ -163,6 +164,29 @@ public function testLegacyXPathAsserts() {
$this->assertNoFieldByXPath('//notexisting');
$this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
$this->assertNoFieldById('name');
$this->assertNoFieldById('name', 'not the value');
$this->assertNoFieldById('notexisting');
$this->assertNoFieldById('notexisting', NULL);
// Test that the assertion fails correctly if no value is passed in.
try {
$this->assertNoFieldById('description');
$this->fail('The "description" field, with no value was not found.');
}
catch (ExpectationException $e) {
$this->pass('The "description" field, with no value was found.');
}
// Test that the assertion fails correctly if a NULL value is passed in.
try {
$this->assertNoFieldById('name', NULL);
$this->fail('The "name" field was not found.');
}
catch (ExpectationException $e) {
$this->pass('The "name" field was found.');
}
}
/**
......
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