Problem/Motivation

In #3167880: [meta] Convert assertions involving use of xpath to WebAssert, where possible we identified that use of $this->xpath() can be dangerous as we aren't always testing what we expect to test. In WebAssert we have more appropriate methods to find and assert HTML elements, so we should use them where possible.

This issue is scoped to find XPath selectors for <input> elements (except checkbox type ones) and convert those where possible.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3198400

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mondrake created an issue. See original summary.

mondrake’s picture

Assigned: mondrake » Unassigned
Status: Active » Needs work
daffie’s picture

When I do a code base search for $this->xpath('//input[, I still find instances that are not with checkboxes.

mondrake’s picture

Title: Convert assertions involving use of xpath on input tags to WebAssert » [WIP] Convert assertions involving use of xpath on input tags to WebAssert

Sure, it's not complete yet. Making it visible on the IT.

mondrake’s picture

Title: [WIP] Convert assertions involving use of xpath on input tags to WebAssert » Convert assertions involving use of xpath on input tags to WebAssert
Status: Needs work » Needs review

I think the remaining ones should stay.

daffie’s picture

Status: Needs review » Needs work

I think the remaining ones should stay.

Could you explain why?

Back to needs work for the unresolved threads.

mondrake’s picture

Status: Needs work » Needs review

Thanks @daffie

#7 because the use of xpath in those cases is needed to build an array of node elements that is then traversed by foreach constructs.

But happy to fix if I missed anything.

daffie’s picture

Could you take a look at the class Drupal\Tests\system\Functional\Form\ElementsTableSelectTest on the lines 41 and 58. Should we also do these in this issue? I agree with you on all the other ones that we should not change them. Found the answer myself. We are not doing the checkboxes in this issue.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

All code change look good to me.
All instances that should be changed are changed.
A number cannot be changed, because the resulting array is used in some way like in a foreach loop.
For me it is RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

It looks like we are losing the check that only a single form_build_id exists. This was added as part of #2242749: Port Form API security fix SA-CORE-2014-002 to Drupal 8 which is a security issue. This should be removed.

If the change to not count the form_build_id hidden fields is valid then we need to document why in the issue summary.

mondrake’s picture

Status: Needs work » Needs review

Thanks, fixed.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

All the points of @alexpott have been addressed.
Back to RTBC.

longwave’s picture

ElementsTableSelectTest:

    $rows = ['row1', 'row2', 'row3'];
    foreach ($rows as $row) {
      $this->assertNotEmpty($this->xpath('//input[@type="checkbox"]', [$row]), "Checkbox for the value $row.");
    }
    $rows = ['row1', 'row2', 'row3'];
    foreach ($rows as $row) {
      $this->assertNotEmpty($this->xpath('//input[@type="radio"]', [$row], "Radio button value: $row"));
    }

We could fix these here? These seem buggy anyway, I think the second argument is used incorrectly.

FormElementMaxlengthTest and FilterBooleanWebTest also look like they could be converted to be a bit cleaner.

mondrake’s picture

Status: Reviewed & tested by the community » Needs work

Thanks

#14 I saw that, but could not figure out what it is meant to be tested there... the entire setting of $rows and the foreach seem irrelevant. I think this is a bug that deserves an issue of its own.

FilterBooleanWebTest - not really sure... the xpath there is selecting multiple nodes and then the assertion is on a specific element in the array.

NW for FormElementMaxlengthTest

mondrake’s picture

Status: Needs work » Reviewed & tested by the community

Actually, no - FormElementMaxlengthTest is a kernel test, we cannot use WebAssert there

  • catch committed 39e4429 on 9.2.x
    Issue #3198400 by mondrake, daffie, alexpott, longwave: Convert...

  • catch committed 20aa51e on 9.1.x
    Issue #3198400 by mondrake, daffie, alexpott, longwave: Convert...
catch’s picture

Version: 9.2.x-dev » 9.1.x-dev
Status: Reviewed & tested by the community » Fixed

Good clean-up again.

Committed/pushed to 9.2.x and cherry-picked to 9.1.x, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.