Commit 00e2631b authored by alexpott's avatar alexpott

Revert "Issue #2868019 by michielnugter, dawehner, Lendude: AssertLegacyTrait...

Revert "Issue #2868019 by michielnugter, dawehner, Lendude: AssertLegacyTrait field assertions not compatible with Simpletest assertions"

This reverts commit e918af66.
parent 9d6de39a
......@@ -35,18 +35,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#default_value' => 'Test name',
];
$form['checkbox_enabled'] = [
'#type' => 'checkbox',
'#title' => 'Checkbox enabled',
'#default_value' => TRUE,
];
$form['checkbox_disabled'] = [
'#type' => 'checkbox',
'#title' => 'Checkbox disabled',
'#default_value' => FALSE,
];
$form['description'] = [
'#type' => 'textfield',
'#title' => 'Description',
......@@ -69,18 +57,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#value' => $this->t('Save'),
];
$form['duplicate_button'] = [
'#type' => 'submit',
'#name' => 'duplicate_button',
'#value' => 'Duplicate button 1',
];
$form['duplicate_button_2'] = [
'#type' => 'submit',
'#name' => 'duplicate_button',
'#value' => 'Duplicate button 2',
];
return $form;
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\FunctionalTests;
use Behat\Mink\Exception\ElementNotFoundException;
use Behat\Mink\Exception\ExpectationException;
use Behat\Mink\Selector\Xpath\Escaper;
use Drupal\Component\Render\FormattableMarkup;
......@@ -219,11 +220,13 @@ protected function assertResponse($code) {
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldExists() or
* $this->assertSession()->buttonExists() or
* $this->assertSession()->fieldValueEquals() instead.
*/
protected function assertFieldByName($name, $value = NULL) {
$this->assertFieldByXPath($this->constructFieldXpath('name', $name), $value);
$this->assertSession()->fieldExists($name);
if ($value !== NULL) {
$this->assertSession()->fieldValueEquals($name, (string) $value);
}
}
/**
......@@ -239,11 +242,15 @@ protected function assertFieldByName($name, $value = NULL) {
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldNotExists() or
* $this->assertSession()->buttonNotExists() or
* $this->assertSession()->fieldValueNotEquals() instead.
*/
protected function assertNoFieldByName($name, $value = '') {
$this->assertNoFieldByXPath($this->constructFieldXpath('name', $name), $value);
if ($this->getSession()->getPage()->findField($name) && isset($value)) {
$this->assertSession()->fieldValueNotEquals($name, (string) $value);
}
else {
$this->assertSession()->fieldNotExists($name);
}
}
/**
......@@ -261,11 +268,19 @@ protected function assertNoFieldByName($name, $value = '') {
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldExists() or
* $this->assertSession()->buttonExists() or
* $this->assertSession()->fieldValueEquals() instead.
*/
protected function assertFieldById($id, $value = '') {
$this->assertFieldByXPath($this->constructFieldXpath('id', $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());
}
}
/**
......@@ -275,11 +290,10 @@ protected function assertFieldById($id, $value = '') {
* Name or ID of field to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldExists() or
* $this->assertSession()->buttonExists() instead.
* Use $this->assertSession()->fieldExists() instead.
*/
protected function assertField($field) {
$this->assertFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field));
$this->assertSession()->fieldExists($field);
}
/**
......@@ -289,11 +303,10 @@ protected function assertField($field) {
* Name or ID of field to assert.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldNotExists() or
* $this->assertSession()->buttonNotExists() instead.
* Use $this->assertSession()->fieldNotExists() instead.
*/
protected function assertNoField($field) {
$this->assertNoFieldByXPath($this->constructFieldXpath('name', $field) . '|' . $this->constructFieldXpath('id', $field), NULL);
$this->assertSession()->fieldNotExists($field);
}
/**
......@@ -414,11 +427,23 @@ protected function assertNoLinkByHref($href) {
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->assertSession()->fieldNotExists() or
* $this->assertSession()->buttonNotExists() or
* $this->assertSession()->fieldValueNotEquals() instead.
*/
protected function assertNoFieldById($id, $value = '') {
$this->assertNoFieldByXPath($this->constructFieldXpath('id', $id), $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;
}
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());
}
}
/**
......@@ -560,32 +585,25 @@ protected function assertFieldByXPath($xpath, $value = NULL, $message = '') {
* (optional) A message to display with the assertion. Do not translate
* messages with t().
*
* @throws \Behat\Mink\Exception\ExpectationException
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->xpath() instead and assert that the result is empty.
*/
protected function assertNoFieldByXPath($xpath, $value = NULL, $message = '') {
$fields = $this->xpath($xpath);
if (!empty($fields)) {
if (isset($value)) {
$found = FALSE;
try {
$this->assertFieldsByValue($fields, $value);
$found = TRUE;
}
catch (\Exception $e) {
}
if ($found) {
throw new ExpectationException(sprintf('The field resulting from %s was found with the provided value %s.', $xpath, $value), $this->getSession()->getDriver());
// 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;
}
}
}
else {
throw new ExpectationException(sprintf('The field resulting from %s was found.', $xpath), $this->getSession()->getDriver());
}
}
return $this->assertFalse($fields && $found, $message);
}
/**
......@@ -611,22 +629,18 @@ protected function assertFieldsByValue($fields, $value = NULL, $message = '') {
$found = FALSE;
if ($fields) {
foreach ($fields as $field) {
if ($field->getAttribute('type') == 'checkbox') {
// Checkbox with the correct checked state.
$found = $field->isChecked() === $value;
if ($field->getAttribute('value') == $value) {
// Input element with correct value.
$found = TRUE;
}
elseif ($field->getTagName() == 'select' && $field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) {
elseif ($field->find('xpath', '//option[@value = ' . (new Escaper())->escapeLiteral($value) . ' and @selected = "selected"]')) {
// Select element with an option.
$found = TRUE;
}
elseif ($field->getTagName() !== 'input' && $field->getText() == $value) {
elseif ($field->getText() == $value) {
// Text area with correct text.
$found = TRUE;
}
elseif ($field->getAttribute('value') == $value) {
// Input element with correct value.
$found = TRUE;
}
}
}
}
......@@ -758,25 +772,6 @@ protected function buildXPathQuery($xpath, array $args = []) {
return $this->assertSession()->buildXPathQuery($xpath, $args);
}
/**
* Helper: Constructs an XPath for the given set of attributes and value.
*
* @param string $attribute
* Field attributes.
* @param string $value
* Value of field.
*
* @return string
* XPath for specified values.
*
* @deprecated Scheduled for removal in Drupal 9.0.0.
* Use $this->getSession()->getPage()->findField() instead.
*/
protected function constructFieldXpath($attribute, $value) {
$xpath = '//textarea[@' . $attribute . '=:value]|//input[@' . $attribute . '=:value]|//select[@' . $attribute . '=:value]';
return $this->buildXPathQuery($xpath, [':value' => $value]);
}
/**
* Gets the current raw content.
*
......
......@@ -165,64 +165,6 @@ public function testLegacyFieldAsserts() {
$this->assertFieldsByValue($this->xpath("//select[@id = 'edit-options']"), '2');
$this->assertFieldByXPath("//select[@id = 'edit-options']", '2');
$this->assertNoField('invalid_name_and_id');
$this->assertField('name');
$this->assertField('edit-name');
// Test that the assertion fails correctly when searching by name.
try {
$this->assertNoField('name');
$this->fail('The "name" field was not found based on name.');
}
catch (ExpectationException $e) {
$this->pass('The "name" field was found by name.');
}
// Test that the assertion fails correctly when searching by id.
try {
$this->assertNoField('edit-name');
$this->fail('The "name" field was not found based on id.');
}
catch (ExpectationException $e) {
$this->pass('The "name" field was found by id.');
}
// Assert that assertion correctly fails when searching for the name field
// without a value.
try {
$this->assertFieldsByValue($this->xpath("//input[@id = 'edit-name']"), '');
$this->fail('The "name" field, with no value was found.');
}
catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('The "name" field, with no value was found.');
}
$this->assertFieldByName('checkbox_enabled', TRUE);
$this->assertFieldByName('checkbox_disabled', FALSE);
$this->assertNoFieldByName('checkbox_enabled', FALSE);
$this->assertNoFieldByName('checkbox_disabled', TRUE);
try {
$this->assertFieldByName('checkbox_enabled', FALSE);
$this->fail('The checked "checkbox_enableRd" field was not found.');
}
catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('The checked "checkbox_enabled" field was found.');
}
try {
$this->assertNoFieldByName('checkbox_enabled', TRUE);
$this->fail('The checked "checkbox_enabled" field was not found.');
}
catch (ExpectationException $e) {
$this->pass('The checked "checkbox_enabled" field was found.');
}
catch (\PHPUnit_Framework_ExpectationFailedException $e) {
$this->pass('The checked "checkbox_enabled" field was found.');
}
$this->assertNoFieldByXPath('//notexisting');
$this->assertNoFieldByXPath("//input[@id = 'edit-name']", 'wrong value');
......@@ -263,12 +205,12 @@ public function testLegacyFieldAsserts() {
$this->pass('The "edit-name" field with no value was not found.');
}
// Test that the assertion fails correctly if another value is passed in.
// Test that the assertion fails correctly if NULL is passed in.
try {
$this->assertFieldById('edit-name', 'not the value');
$this->assertFieldById('name', NULL);
$this->fail('The "name" field was found.');
}
catch (\PHPUnit_Framework_ExpectationFailedException $e) {
catch (ExpectationException $e) {
$this->pass('The "name" field was not found.');
}
......@@ -295,19 +237,6 @@ public function testLegacyFieldAsserts() {
$this->pass('The "name" field was found.');
}
// Test that multiple fields with the same name are validated correctly.
$this->assertFieldByName('duplicate_button', 'Duplicate button 1');
$this->assertFieldByName('duplicate_button', 'Duplicate button 2');
$this->assertNoFieldByName('duplicate_button', 'Rabbit');
try {
$this->assertNoFieldByName('duplicate_button', 'Duplicate button 2');
$this->fail('The "duplicate_button" field with the value Duplicate button 2 was not found.');
}
catch (ExpectationException $e) {
$this->pass('The "duplicate_button" field with the value Duplicate button 2 was found.');
}
$this->assertOptionByText('options', 'one');
try {
$this->assertOptionByText('options', 'four');
......@@ -324,7 +253,7 @@ public function testLegacyFieldAsserts() {
$this->assertFieldById('Save', NULL);
$this->fail('The field with id of "Save" was found.');
}
catch (\PHPUnit_Framework_ExpectationFailedException $e) {
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