Problem/Motivation

As mentioned at #3517299-24: Convert functional tests in help module to kernel tests it'd be helpful to add xpath() and getUrl() helpers that are available to Browser tests to \Drupal\Tests\HttpKernelUiHelperTrait.

This would help with some of the tests considered at #3583587: Convert some functional tests in system module to kernel tests for an immediate example.

However, Kernel tests already have their own version of xpath(), which has a different return type to the Browser test xpath(). Therefore, the xpath() being copied over from Browser tests is being renamed to avoid a clash and to be unambiguous. A follow-on issue #3585774: rename xpath() methods in tests will rename the existing xpath() methods for DX.

Steps to reproduce

Proposed resolution

Remaining tasks

Once #3583587: Convert some functional tests in system module to kernel tests is in we need to update DefaultMetatagsTest here to use the xpath function instead of this line:
$result = $this->getSession()->getPage()->findAll('xpath', '//meta');

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-3583594

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

dww created an issue. See original summary.

dww’s picture

Moved the code. But now that I did, I'm unclear why we need / want to override the copy of xpath() that lives in core/tests/Drupal/KernelTests/AssertContentTrait.php which is already included for us in KernelTestBase. 😅 Could use better comments from @joachim or whoever best understands this detail. Maybe they'll be equivalent once #3582985: Add content from HttpKernelUiHelperTrait::drupalGet() to $this->content for use by AssertContentTrait is done? Should we postpone this on that and then we don't need a separate xpath() here?

dww’s picture

Also starting to wonder if all these separate follow-ups are a good idea, or if it'd be easier to manage them all, and hopefully backport them, if we merged all these into a single "make Kernel drupalGet() more useful" issue? 😅

In particular, merging these 3:

  1. #3582228: add $options and $headers parameters to HttpKernelUiHelperTrait::drupalGet()
  2. #3582985: Add content from HttpKernelUiHelperTrait::drupalGet() to $this->content for use by AssertContentTrait
  3. #3583594: Add getUrl() and (renamed) xpath() helpers to HttpKernelUiHelperTrait (this issue)

(since #3582769: Add clickLink() to HttpKernelUiHelperTrait is already committed - but not yet backported)

dww’s picture

Title: Add xpath() and getUrl() helpers to HttpKernelUiHelperTrait » Add getUrl() helper to HttpKernelUiHelperTrait
dww’s picture

p.s. If we merge these followups, maybe we should also move the namespace for HttpKernelUiHelperTrait to Drupal\KernelTests (since it's specific to Kernel tests) and perhaps rename the trait to something like HttpKernelRequestTrait. We had a Slack thread in #core-development about this.

dww’s picture

Sorry, thought I already posted this to this issue. But yeah, the original commit with the alternate xpath() failed spectacularly with:

Fatal error: Trait method Drupal\Tests\HttpKernelUiHelperTrait::xpath has not been applied as Drupal\KernelTests\KernelTestBase::xpath, because of collision with Drupal\KernelTests\AssertContentTrait::xpath in /builds/core/tests/Drupal/KernelTests/KernelTestBase.php on line 98
mstrelan’s picture

It's maybe worth noting that BrowserTestBase::xpath returns \Behat\Mink\Element\NodeElement[] whereas AssertContentTrait::xpath returns \SimpleXMLElement[]|false. So even with #3582985: Add content from HttpKernelUiHelperTrait::drupalGet() to $this->content for use by AssertContentTrait we still have to refactor any calls to $this->xpath().

joachim’s picture

Yeah, I got stung by the different behaviour of xpath() when I was working on #3582580: add checkForMetaRefresh() method to HttpKernelUiHelperTrait.

joachim’s picture

I have mixed feelings about this.

On the one hand, we should make conversion to Kernel tests as simple as possible with as few changes as possible -- for the work itself, but also for ease of reviewing, so reviewers can be confident that the test coverage is not changing.

On the other, getUrl() is only needed by the $goto parameter of AssertBreadcrumbTrait::assertBreadcrumb(). I'm not keen at all on an assertion that sneakily makes a drupalGet() call, which changes the state of the test. Requiring an explicit call to drupalGet() first would be better DX - helps readability and prevents bugs.

But that's not something we should change the behaviour of here -- having two flavours of assertBreadcrumb() floating around would be bad.

However, the two flavours of xpath() already exist, and while at the moment they're segregated in Functional / Kernel tests, if we start having both flavours in Kernel tests people's heads are going to explode.

So if we're bringing the Functional one into Kernel tests, I think we have to rename it to something unambiguous like getNodeElementByXpath(), and file a follow-on to rename the two existing xpath() methods in a similar way.

smustgrave’s picture

With regards to #4 I’ll lean on you have been in the weeds of this issue. But will say the MR would still be small and manageable and if you need sign off I feel you would have better luck with a single ticket vs many. But think that’s a you guys call

joachim’s picture

Title: Add getUrl() helper to HttpKernelUiHelperTrait » Add getUrl() and (renamed) xpath() helpers to HttpKernelUiHelperTrait

Ok, adding the xpath() method back with a distinguishing name.

Filed #3585774: rename xpath() methods in tests as a follow-up to standardize and differentiate the names of the xpath() methods.

mstrelan’s picture

Issue summary: View changes
joachim’s picture

Issue summary: View changes

Added reasons for the rename to IS.

smustgrave’s picture

Status: Needs review » Needs work

per the remaining task section now that #3583587: Convert some functional tests in system module to kernel tests landed can DefaultMetatagsTest be updated. Then probably good to move this one forward I assume?

joachim’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

All tasks seem to be addressed. LGTM!

dww’s picture

  1. I really don't like the scoping of converting DefaultMetatagsTest as part of this issue. The goal is to backport this (and a handful of others), perhaps all the way back to 10.6.x so that contrib can start converting their Functional to Kernel tests. See #3390193-160: Add a drupalGet() method to KernelTestBase. So this issue should just do what the title says. Conversions of tests to use this helper should happen elsewhere. Therefore, I moved @mstrelan's work over to #3588181: [pp-1] Convert DefaultMetatagsTest as Kernel test to use getNodeElementsByXpath().
  2. Very minor doc improvement (which I foolishly committed at the same time as the removal of xpath()) got lost when that commit was reverted. Pushed another commit to restore it.

That said, very happy to see getNodeElementsByXpath() here with its new name. I think that's excellent, and totally self-documenting (even if it's a little verbose).

Leaving RTBC, since these are very trivial changes I'm making since the last review.

Thanks, everyone!
-Derek

smustgrave’s picture

up to you guys as you've all been doing all the work. Only reason I liked the one conversion was it shows this function being used. But perfectly fine doing in another ticket fwiw

mstrelan’s picture

It was asked to be done here when the system module issue was committed. To me it doesn't seem worth having its own issue, and it also wouldn't necessarily need to be backported, but not sure why it couldn't be.

joachim’s picture

@dww what would you prefer as a shorter name? We need to distinguish a return of a NodeElement from a return of an XML element.

dww’s picture

Re: scope - Yes, I realize @catch asked for it to happen like this. I disagree. 😅 I think the scoping is better if we only add the helpers here, not changing usages. I'd love to see drupalGet() and all the helpers backported all the way to 10.6.x. I don't think we're going to backport all the test conversions that far.

Re: name - I wrote:

very happy to see getNodeElementsByXpath() here with its new name. I think that's excellent, and totally self-documenting...

I think getNodeElementsByXpath() is great as-is. No need to change it. As I've written elsewhere, I'd rather methods with longer, self-documenting names than brief and ambiguous (or worse).

Thanks!
-Derek

  • catch committed 326c357e on main
    task: #3583594 Add getUrl() and (renamed) xpath() helpers to...
catch’s picture

Status: Reviewed & tested by the community » Fixed

We'll need a new, combined, backport MR with the various changes for an 11.x backport - won't be easy or necessarily even work to individually cherry-pick multiple commits from HEAD in the correct order directly. Easy to drop any test changes as part of that process.

Also we often include one or two conversions when adding a new API. The other option was to postpone the previous test conversion on this issue.

But also I don't really mind if it's done here or a follow-up so just going ahead. Committed/pushed to main, thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

joachim’s picture

I thought we were backporting each issue to 11.x, and the complicated backport was going to be 11.2/10.6?

  • catch committed 554d7c0f on 11.x
    task: #3583594 Add getUrl() and (renamed) xpath() helpers to...
catch’s picture

Yes good point... Cherry-picked to 11.x too.

Status: Fixed » Closed (fixed)

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