Problem/Motivation
While investigating a strange failure on #2857157: Implement registration after guest checkout I was getting some weird behaviour when trying to recreate and fix the test failures:
$this->assertUrl('user/3/edit'); was causing:
'Current page is "/subdirectory/user/3/edit", but "/user/3/edit" expected.'
When I first tried to run tests locally with drupal in a subdirectory (i.e. /var/www/htdocs/subdirectory with php ./core/scripts/run-tests.sh --url http://test.local/subdirectory ...) then a lot of url assertions failed and only after replicating the CI environment by moving drupal to /var/www/html and creating the symlink (ln -s /var/www/html /var/www/html/subdirectory) could I get all tests to pass except the one in question.
I was able to 'fix' the test by changing $this->drupalGet($this->product->toUrl()->toString()); to $this->drupalGet($this->product->toUrl());.
I think this weird fix works because the url generator used by $this->product->toUrl()->toString() is correctly adding '/subdirectory' but $this->drupalGet() is not. This kicks the test environment to / but still works because although run-tests.sh is called with i.e. --url "http://php-apache-jenkins-drupal8-contrib-patches-32847/subdirectory" and starts off at /subdirectory, it is just a symlink of the webroot so all paths without /subdirectory will always work still.
As unlikely as this first seems, I think an explanation for no other tests spotting it could be because nobody has committed a test that passes a Url->toString() to $this->drupalGet(), always a Url object (i.e. $this->drupalGet(Url::fromRoute('<front>'));) or a hard coded uri (i.e. $this->drupalGet('admin/content');) and anyone who even notices the /subdirectory in URLs assumes that drupalGet() is automatically adding '/subdirectory' and assertUrl() is handling it like a uri.
Proposed resolution
Strip $base_path from the path given to "drupalGet" when this path is a string and the "url_generator" service is available.
Note that when a string is given and the service is not available, the function "getAbsoluteUrl" already strips the base path.
Remaining tasks
- Confirm that tests fail with drupal installed in a subdirectory
- Review proposed resolution
- Do fixes
- Fix tests broken by fixes
- Open a discussion in the relevant project about removing the symlink from drupal CI and installing to /var/www/htdocs/subdirectory (there is probably a very good reason for the symlink but i assume the whole point of it was to test everything works when drupal is in a subdirectory?)
Issue fork drupal-2986962
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
Comment #3
krzysztof domańskiSimilar problem #2527628: Deleting a block does not redirect back to the correct theme.
Comment #7
quietone commentedTriaging issues in simpletest.module as part of the Bug Smash Initiative to determine if they should be in the Simpletest Project or core.
This looks like it an issue for the Phpunit component.
Comment #15
bhanu951 commentedComment #16
bhanu951 commentedComment #18
fjgarlin commentedWe are running into this issue when trying to convert DrupalCI to GitlabCI.
See the slack message: https://drupal.slack.com/archives/C223PR743/p1692017659269539
Quoting here for those not in slack:
I'm still not sure why it fails in GitlabCI and _not_ in DrupalCI, but the situation is the same as the one presented in this issue.
If I find a solution to it, I'll also report here, I just wanted to "link" these two issues as the fix might help one another.
Comment #19
fjgarlin commentedI made a change that I think fixes the issue, but I find it hard to write a test that will fail...
MRs:
* 9.4.x: https://git.drupalcode.org/project/drupal/-/merge_requests/1950/diffs
* 11.x: https://git.drupalcode.org/project/drupal/-/merge_requests/4602/diffs
So far, DrupalCI has not caught this, even though there are multiple issues related to this.
See core:
* #3060842: Improve documentation on the working of Url::fromUserInput
* #2582295: Confirmation cancel links are incorrect if installed in a subdirectory
* #2990664: Media library does not work when Drupal is installed into a sub-directory
And contrib:
* #3320228: IMCE UI generates links that are not the same as those generated by Drupal Core
* #2899166: WORKAROUND: Issue #2582295: Confirmation cancel links are incorrect if installed in a subdirectory
* #3046182: Work around Core bug: 404 on confirmation cancel in subdirectory installs
For this fix, while using GitlabCI for core, we can see the before and after (open the link and then click on the right arrow in the "Downstream" job to expand the child pipeline):
* Before: https://git.drupalcode.org/project/gitlab_ci_testbed_for_drupal_core/-/p...
* After: https://git.drupalcode.org/project/gitlab_ci_testbed_for_drupal_core/-/p...
“Functional Javascript” errors go from 8 to 3.
“Functional” ones from 5 to 0.
The related issue where those tests are being made is: #3375827: Investigate tests failures
Comment #20
fjgarlin commentedComment #22
smustgrave commentedCould the issue summary be updated to match the new solution please.
I've been bit by this bug numerous times too and had to do some weird workarounds. Usually the failure only happened on drupalCi but would pass locally because of this issue.
Not sure if any test coverage is needed.
Comment #23
fjgarlin commentedComment #24
fjgarlin commentedUpdated the issue description to match the solution proposed in the MR.
Comment #25
smustgrave commentedThanks! Think this is ready.
Comment #28
catchIt's probably theoretically possible to test the trait with different values for $base_path, however this is going to be implicitly tested by every gitlab CI run for the forseeable future, so I think that could be a follow-up.
Committed/pushed to 11.x and cherry-picked to 10.1.x, thanks!
Comment #29
catchComment #30
quietone commentedThis is tagged for a followup. Is that #5 of the remaining tasks? Does anyone know if that has been created or is no longer needed?
Comment #31
catchSorry the follow-up would be to add additional, explicit, test coverage to UiHelperTrait to ensure that it works with different values for $base_url. However because we have implicit test coverage of this on every test run, I'm wasn't sure whether it's really worth adding tests for the test helper in this case.
Comment #32
quietone commentedSeems better to make one than to forget. Here it is #3386051: Add tests of $base_url in UiHelperTrait
Removing tag