Problem/Motivation
TestCase::checkRequirements used to be protect, but was made private in PHPUnit 7.0
https://github.com/sebastianbergmann/phpunit/commit/932238a6a3018cdfac6c...
How is this still working? This controls the @requires module annotation.
After digging, I see Drupal core did this to test the method, but that just tests the trait. It doesn't work.
/**
* Public access for checkRequirements() to avoid reflection.
*/
public function publicCheckRequirements() {
return parent::checkRequirements();
}
Steps to reproduce
Add @requires module, run PHPUnit. It fails when module does not exist.
But Drupal core is passing?
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comments
Comment #2
mglamanComment #3
mglamanComment #5
mondrake??? I have no idea how I triggered #4... I was just watching on mobile.
Comment #6
longwaveGreat catch. Drupal core never actually uses the trait properly - which is why the core test suite passes - but it doesn't actually work.
The test cases for the trait call the trait directly, which as you have discovered, isn't a valid test. There is an attempt to use it in JSON:API's FileUploadTest, but this also doesn't work; the six test methods are never skipped as proved by the attached patch. There are no other uses of it in core.
Comment #7
mglamanI dug into it, and there are no extension points into PHPUnit for this. In my test, I called `checkRequirements` in my setUp before parent::setUp to determine if the test should be skipped.
Comment #10
mglamanMade a MR which has #6 and how I fixed our tests, but placed in BrowserTestBase.
Comment #11
mglamanPutting to NR. We have a failing test-only patch and a proposal. I don't know what's the best approach here.
checkRequirements is private, has been since PHPUnit 7.0: https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/T...
PHPUnit 9 has a Requirements class.
However, it's not extensible: https://github.com/sebastianbergmann/phpunit/blob/a5ad0256678687e86f3fa8...
So any hook which is before the tests run would be appropriate: https://github.com/sebastianbergmann/phpunit/blob/master/src/Framework/T...
So it seems like setUp would be appropriate. I don't think beforeClass is invoked with each method (I have no idea.)
Comment #12
longwaveWe also have
TestDiscovery::parseTestClassAnnotations()which seems to be unused in core but also parses these annotations, with these comments:Comment #13
longwaveAnother place to do this check might be
DrupalListener::startTest()?Comment #14
mglamanI tried reviewing \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::startTest, which has more code than ours.
It looks like you can only mark the test as risky
Comment #15
rodrigoaguileraI came across this issue trying to understand why a test with "@requires module ..." was not skipped and I also found this draft CR.
https://www.drupal.org/node/2857979
I associated it with this issue so it can be properly published when this is fixed
Comment #16
rodrigoaguileraComment #18
jonathan1055 commented[off-topic] @mondrake in #4 and #5 yes I have often seen auto-comments saying "(user) made their first commit to this issue’s fork" when there was nothing by them and they had no intention to actually commit. Should that be raised as another issue?
Comment #19
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #need-reveiw-queue. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge require as a guide.
#17 appears to be a drive by rebasing. Removing credit please see https://www.drupal.org/docs/develop/issues/fields-and-other-parts-of-an-... for how to receive credit.
Reviewing the MR. The changes make sense to me. Think this can remain in the 10.0.x branch
Comment #20
xjmComment #21
xjmThanks for finding this... how are more things not broken?
I think we also need to update the documentation of TestRequirementsTrait::checkRequirements(), since it explicitly says something about overriding the parent method.
Since this apparently changed in PHPUnit 7, that means Drupal 9 is also affected. As a bugfix to test code, this can be backported to 9.5.x. The diff of the MR applies to 10.1.x and 10.0.x but not 9.5.x, so we will eventually need a 9.5.x version as well.
Thanks everyone!
Comment #22
xjmFixing attribution.
Comment #23
harshitthakore@xjm created patch for 9.5.x. I don't know the exact process to backport the patch for specific Drupal version, So I've created patch for 9.5.x without commit.
Comment #24
xjmMy feedback in #21 about updating the docblock also still needs to be addressed. Thanks!
Comment #25
mondrakeThis is on the critical path for PHPUnit 10, and just looks like cruft ATM. In #3418379: Deprecate Drupal\Tests\TestRequirementsTrait::checkRequirements and ::checkModuleRequirements, there is a proposal to deprecate with no replacement.
Comment #26
mondrakeComment #27
longwaveThis has been broken since PHPUnit 7 was adopted and nobody has cared enough to fix it. PHPUnit 10 means we have to remove this code entirely, so we have deprecated it in #3418379: Deprecate Drupal\Tests\TestRequirementsTrait::checkRequirements and ::checkModuleRequirements and therefore this issue is won't fix.