Problem/Motivation
When using a drupal/recommended-project setup, composer.json exists outside of the web root where Drupal core lives. In PHPUnit 9 you could run PHPUnit tests from the project root, for example:
$ ./vendor/bin/phpunit -c web/core/phpunit.xml.dist web/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
PHPUnit 9.6.19 by Sebastian Bergmann and contributors.
Testing Drupal\Tests\Core\Access\AccessManagerTest
.................. 18 / 18 (100%)
With PHPUnit 10 this fails to find the test:
PHPUnit 10.5.20 by Sebastian Bergmann and contributors.
Test file "web/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php" not found
You can omit the web/ directory and it passes, but this means shell autocompletion is broken:
$ ./vendor/bin/phpunit -c web/core/phpunit.xml.dist core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
PHPUnit 10.5.20 by Sebastian Bergmann and contributors.
Runtime: PHP 8.3.6
Configuration: /home/michael/www/phpunit10/web/core/phpunit.xml.dist
.................. 18 / 18 (100%)
Or you can cd in to the web/ directory first.
$ cd web
$ ../vendor/bin/phpunit -c core/phpunit.xml.dist core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
PHPUnit 10.5.20 by Sebastian Bergmann and contributors.
Runtime: PHP 8.3.6
Configuration: /home/michael/www/phpunit10/web/core/phpunit.xml.dist
.................. 18 / 18 (100%)
Steps to reproduce
Testing this on drupal/core-recommended:11.0.x-dev because 11.x-dev hasn't updated yet.
$ composer create-project drupal/recommended-project:11.0.x-dev@dev .
$ ./vendor/bin/phpunit -c web/core/phpunit.xml.dist web/core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
$ ./vendor/bin/phpunit -c web/core/phpunit.xml.dist core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
$ cd web/
$ ../vendor/bin/phpunit -c core/phpunit.xml.dist core/tests/Drupal/Tests/Core/Access/AccessManagerTest.php
Proposed resolution
Move chdir call from core/tests/bootstrap.php to test base classes, right after they set $this->root.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #22 | issue3445847-reroll.patch | 5.91 KB | alexander allen |
Issue fork drupal-3445847
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 #2
mstrelan commentedFWIW if you run tests via PhpStorm you're not affected by this as it use absolute file paths.
Comment #4
mstrelan commentedComment #5
longwaveI don't understand what has changed here, the
chdir()was added a while back in #3391776: InfoParser returns an empty array if passed a non-existing file so why is PHPUnit 9 behaviour different?Comment #6
mstrelan commentedGuessing PHPUnit 10 handles bootstrap differently, maybe it loads the file earlier now.
FWIW the docs for bootstrap say:
If I get a chance I'll try track down the change in PHPUnit.
Comment #7
alexpottMissing build tests - but they might mess with the working directory even more.
Comment #8
mondrakeComment #9
mstrelan commentedRe: #5
In PHPUnit 9 I put a breakpoint in
\PHPUnit\TextUI\Command::handleArguments. Stepping throw I can see that$this->arguments['test'] = realpath($arguments->argument());converts the relative path to absolute much before$this->handleBootstrap()is called in the same method.In PHPUnit 10 I put a breakpoint in
core/tests/bootstrap.phpand in\PHPUnit\TextUI\Configuration\TestSuiteBuilder::build. The bootstrap file is loaded first, invoked from\PHPUnit\TextUI\Application::run, thenTestSuiteBuilder::buildcallsrealpath($cliArgument)which fails.Re: #7
Build tests are currently passing and seem to have their own logic for how to find Drupal. They also weren't touched in #3391776: InfoParser returns an empty array if passed a non-existing file and don't have an existing
$rootproperty the others have. I'd be inclined to leave them out.Comment #10
mark_fullmerI was able to reproduce the problem running core/contrib tests with Drupal 11.x and PHPUnit 10.
I've tested this MR with PHPUnit 10 and can confirm that it resolves the issue with the webroot. Tests execute as expected, and pass. Marking this as RTBC!
Comment #11
alexander allen commentedI was able to install and test the following contrib modules against the patch, using core 11.0.x-dev:
All install and test results are available on the MR comments. No more testing to be done from my end.
Comment #14
catchI'm having similar problems just running tests from the core directory with
../vendor/bin/phpunit tests/whateverhave to use core/tests/whatever instead which breaks terminal autocomplete. This is working well so I think we should go ahead here. If phpunit hadn't intentionally deprecated so many things we've been relying on in phpunit 10 would have suggested a bit more investigation and an upstream bug report, but not sure what that would get us here.Committed/pushed to 11.x and cherry-picked to 11.0.x, thanks!
Comment #17
catchNope this broke HEAD, might be new tests that need an update. https://git.drupalcode.org/project/drupal/-/pipelines/179231
Comment #18
alexander allen commentedI see three failures.
1. Core test job is failing on test
RecipeQuickStartTest@docroot/web/core/tests/Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php.Curious that locally I see it getting skipped instead of attempted and failed.
2. Build job is failing with
3. As for nightwatch it is reporting it is failing to install the database during one of the tests:
The command is
Under the [Tests/Toolbar Api Test] Test Suite
Comment #19
alexander allen commentedRunning the core unit test with
--debugshows PHPUnit decides to bounce on the test while doing the setupI'm none the wiser as to the exact root cause so I'll be debugging it. I am on the same version of PHPUnit reported on the build log:
Comment #20
longwaveThe nightwatch one is a random fail, SQLite doesn't work perfectly under the sorts of loads we put it under in the test suite, "database is locked" is a symptom of this and the test will likely pass on the next run.
Comment #21
alexander allen commented@longwave, thanks for the context.
For the SQLite unit test, I am observing that it is path related.
Changing
to
web/core/scripts/drupaldoes allow the install step to resume (as opposed to causing it to skip right then and there).Edit: I was missing simplexml, fixed that. Now I see failure in line
chmod($this->testDb->getTestSitePath(), 0755);, which makes sense because it is path related.Wish me luck figuring out a patch, if @mstrelan doesn't beat me to it (I'm sure he will haha)
Comment #22
alexander allen commentedI don't have a fork to which issue MRs from.Fixed, tests running.Comment #24
mstrelan commented@Alexander Allen thanks for investigating this and creating the MR.
In future, instead of creating a new MR, please rebase the existing MR and commit to that, so we can see what has changed. I've generated an interdiff below for committers to see.
I'm not sure we need the additional assertion but I can see how it helps. But you're creating a new local variable but then not using it in
$install_command. Also don't need{}around$script.Comment #25
alexander allen commentedI've fixed the unused variable on on MR 8153 and pushed the change.
Wasn't sure about pushing to existing MR branch, thanks for the guidance.
Comment #26
quietone commentedThere are two MRs here both on 11.x and with different diff stats. Which one should be reviewed? The issue summary should have details on the difference.
Comment #30
mstrelan commented@quietone I've hidden the original branch that was previously committed. The remaining MR is for review. As mentioned in an earlier comment we should have continued on with the one MR, but Alexander Allen was not aware of this. Please review MR 8153.
Comment #31
smustgrave commentedRemoving issue summary tag as there is only 1 MR up now.
Comment #32
xjmI reviewed the changes between the two MRs; basically, there was an additional test base class added for recipe quickstart things between the original MR and the commit. Everything else is the same.
Given the obvious necessity and that this was already committed once aside from the above, I'm also comfortable committing it despite RTBCing it myself.
Comment #33
xjmSaving credits.
Comment #36
xjmCommitted to 11.x and cherry-picked to 11.0.x. Thanks everyone!
Comment #37
kim.pepperNoticed that writing
test.bbbto the filesystem was causing issues here so created a follow up #3455183: FileSaveUploadTest should not write to the app root