Problem/Motivation
We've seen a test failure in BookTest a few times recently while testing patches on \Drupal\Tests\book\Functional\BookTest::testGetTableOfContents
One example: https://www.drupal.org/pift-ci-job/2476573
Testing Drupal\Tests\book\Functional\BookTest
.....E.......... 16 / 16 (100%)
Time: 04:09.807, Memory: 4.00 MB
There was 1 error:
1) Drupal\Tests\book\Functional\BookTest::testGetTableOfContents
Behat\Mink\Exception\ExpectationException: An option "12" exists in select "edit-book-pid", but it should not.
/var/www/html/core/tests/Drupal/Tests/WebAssert.php:540
/var/www/html/core/tests/Drupal/Tests/WebAssert.php:264
/var/www/html/core/modules/book/tests/src/Functional/BookTest.php:361
/var/www/html/vendor/phpunit/phpunit/src/Framework/TestResult.php:726
So here's what happens (AFAICT): We check the non-existence of an option in an HTML select element in \Drupal\Tests\WebAssert::optionNotExists.
(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
That in turn uses $select_field->find('named', ['option', $option]) to try and get the non-existent option, which we expect to be NULL.
(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
All great, _except_ that the 'named' selector in the above, first does an exact search and if that comes back empty (which is what we want), it tries an partial search:
if ('named' === $selector) {
$items = $this->findAll('named_exact', $locator);
if (empty($items)) {
$items = $this->findAll('named_partial', $locator);
}
return $items;
}
(https://github.com/minkphp/Mink/blob/master/src/Element/Element.php#L156...)
That 'named_partial' search does (sometimes) returns with another option in our select, which isn't the one we're after.
Since it returns something, our NULL-assert fails, making the test fail:
$this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));
(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
Doing a search with selector 'named_exact' to begin with, will skip all the "fuzziness" of the 'named_partial' and returns NULL when the exact option-value we're after isn't there.
Steps to reproduce
Proposed resolution
Update \Drupal\Tests\WebAssert such that optionExists and optionNotExists use `named_exact` instead of `named` to search for options. This will fix the failing test (as demonstrated in the patches from @Spokje) and it will bring these methods inline with the behavior described in their docblocks and their error messages.
Remaining tasks
User interface changes
N/A
API changes
The option(Not)Exists functions no longer allow partial matches of options.
Data model changes
N/A
Release notes snippet
TBD
| Comment | File | Size | Author |
|---|
Issue fork drupal-3309907
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:
- 3309907-random-test-failure
changes, plain diff MR !2767
Comments
Comment #2
spokjeComment #3
spokjeLet's first show
Drupal\Tests\book\Functional\BookTest::testGetTableOfContentsis an actual (not-so) Random Test Failure by testing a patch that _only_ tests that method 100 times.Comment #4
spokjeSo here's what happens (AFAICT): We check the non-existence of an option in an HTML select element in
\Drupal\Tests\WebAssert::optionNotExists.(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
That in turn uses
$select_field->find('named', ['option', $option])to try and get the non-existent option, which we expect to be NULL.(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
All great, _except_ that the
'named'selector in the above, first does an exact search and if that comes back empty (which is what we want), it tries an partial search:(https://github.com/minkphp/Mink/blob/master/src/Element/Element.php#L156...)
That
'named_partial'search does (sometimes) returns with another option in our select, which isn't the one we're after.Since it returns something, our NULL-assert fails, making the test fail:
(https://git.drupalcode.org/project/drupal/-/blob/10.0.x/core/tests/Drupa...)
Doing a search with selector
'named_exact'to begin with, will skip all the "fuzziness" of the'named_partial'and returns NULL when the exact option-value we're after isn't there.Comment #5
spokjeSo if #4 is true, doing a 1500x test on
Drupal\Tests\book\Functional\BookTest::testGetTableOfContentsonly (like the 100x test in #3) and use the'named_exact'selector, we should have no failures.Comment #6
spokjeChecking to see if this also happens on
9.5.xas well.Comment #8
spokjeSince it's always hard to proof that something _isn't_ there, let's test with
$option_field = $select_field->find('named_partial', ['option', $option]);.If that "fuzziness" is indeed the root cause of our test failure, this test should also fail.
Comment #9
spokjeWell, that's good enough for me, on to NR.
Comment #10
danflanagan8Good enough for me too, @Spokje.
The IS is very clear and the repeated test runs are very persuasive. I don't see any possible downside of using `named_exact` in this case.
edit: FWIW I saw this failure myself recently: https://www.drupal.org/project/drupal/issues/2919158#comment-14697838
Comment #11
jibranHow can we back this statement up?
Comment #12
spokjeBesides the above (multiple) test-runs, I think what's important here is that we're testing something is _not_ there.
Which means we want an _exact_ answer when were looking for what _is_ there, since we're going to test that returns nothing.
If the
->find('named', ['option', $option]);is trying to be helpful and returning things that are "kinda almost" what we're looking for, we could be (and IMHO are in the failing test runs) getting things back that we're _not_ looking for.Using find with the
`named_exact`option doesn't return the "kinda almost" options.Comment #13
danflanagan8@jibran makes a good point that my claim isn't really substantiated.
The assertion following the change to named_exact reads:
$this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));That message is certainly incorrect unless `named_exact` is being used. An accurate message of the current implementation would be something more like: An option containing "%s" exists ...
Perhaps the safest path forward would be to add a couple assertions for options that a patterned off of the assertion for links. For links there are separate assertions
linkExists()andlinkExistsExactthat use `named` and `named_exact` in their code, respectively.We could add
optionExistsExactandoptionNotExistsExact()that follow the same pattern. And then update the book test to use the new method(s).Comment #14
danflanagan8I've had a chance to come back to this after a bit of thinking. Maybe touching
optionNotExistswithin this issue is not the right move. A change like that may be worth an argument. I see it as an improvement, but somebody out there relying on the fuzziness may not. Who knows.In the interest of not getting bogged down here, I would propose updating
BookTest::testGetTableOfContents()to avoid usingoptions(Not)Existsat all. What if we made changed those lines from:to:
or something similar.
This might be the easiest way to get a fix for this not-so-random fail committed ASAP.
Comment #15
danflanagan8In the interest of getting this fixed quickly, here's an alternate approach to fixing the test fail that only touches the test itself. We stop using
option(Not)Existsin favor of longer, uglier, but exact assertions. I changed the positive assertion as well in order to demonstrate the the selector structure being used in the negative assertions is appropriate.Comment #16
danflanagan8Changing priority because I think test fails are typically set as Major since they get in the way of other stuff.
Comment #17
nod_Selector change looks good to me, thank so much for finding the reason why this seemed random.
Comment #18
danflanagan8Updating the IS to account for the solution in the RTBCed patch.
Comment #19
spokjePersonally I don't think we need this change:
(and also personally I think we're setting us up for another random fail at some point in time with another test that _does_ still use/is going to use
optionNotExists, but that's something for a follow-up maybe)Comment #20
danflanagan8It's definitely not a mandatory change. My reasoning was in #15:
Comment #21
alexpottGiven optionNotExists is responsible for the problem should we be changing it - it is our code? Do we need the partial name functionality?
If we do want to make the above change then I think we need to add a comment as why. Future us might come along an want to optimise this.
Comment #22
danflanagan8That's the big question. I personally think that we don't need the partial name functionality in
option(Not)Exists. I can't imagine wanting that feature, and none of the 50 or so usages in core of optionNotExists appear to leverage the fuzziness as a feature.The partial match is part of our
select(Not)Existsbut I *think* that is useful. After an ajax call a form element can be reloaded with an updated id that contains some kind of random string at the end. So the partial match allows that random string to be ignored.With options though this random string is not appended, so the partial match for options does not serve a role there. Options are different from other form elements in that way.
I figure we have two "options" here:
1. Update both
optionExistsandoptionNotExiststo use `named_exact` when finding the option. This is half-dane through the MR.2. Apply a quick fix (e.g. the patch) and discuss changing
option(Not)Existsin a followup. A @todo could be added to the patch to revisit after the followup.I'm going to tag this for a subsystem maintainer review and reach out in Slack.
Comment #23
longwaveThe method docs just say "the option value" instead of "the partial option value" or similar, so to me this is a behaviour/bug fix. I can see the use case in the select being fuzzy but not the option value itself - we surely know the exact option value we are looking for.
Comment #24
mondrake+1 to #23, that seems the real bahavior we should expect from the method. So I think we should fix the WebAssert code here.
Comment #25
danflanagan8Thank you to both @longwave and @mondrake for chiming in. At this point it seems like everyone here (myself included) would prefer to fix the root cause of the BookTest failure by updating the
option(Not)Existsmethods. @Spokje started that approach in the MR. I have just added a commit to the MR that updatesoptionExiststo use `named_exact` so that it stays in sync withoptionNotExists.I've updated the IS a bit and I'm hiding the patch.
Comment #26
longwaveLet's write a change record in case anyone was relying on the existing behaviour.
Comment #27
alexpottI like the new approach - it might fix random test fails elsewhere and in contrib & custom.
I ummed and ahhed on a CR - if we do have one I think we should still do this in 9.5.x.
Comment #28
longwaveFor maximum safety in 9.5.x and 10.0.x given that the betas are already out, we could make this change only in
optionNotExistsin those branches?Comment #29
alexpott@longwave given this is only affects tests I think an argument could be made that for maximum safety we make the consistent change everywhere.
Comment #30
catchYeah given this is test-only I think it's OK to backport as a bugfix to 9.5.x with a change record, if it somehow makes someone's test fail consistently somewhere, that's still less inconvenient than random test failures.
Comment #31
danflanagan8I banged out a little CR. Could someone review it?
Comment #32
longwaveBoth the MR and the change record look good to me.
Comment #33
longwaveOh, and let's make it clear this can go back to 9.5.x - the MR applies to all three branches.
Comment #34
longwaveComment #35
alexpottCommitted and pushed c9f1974820 to 10.1.x and 2b85363333 to 10.0.x and 600f32029e to 9.5.x. Thanks!
Comment #39
heddnFor select lists, this seems like a late breaking change to 9.5. Could we do anything so this doesn't cause unexpected 9.5 failing tests? I was not including carrots
<,>in any of my tests for select list items and this was an unexpected breakage without warning. See #3311965-6: Route names do not mesh with the relation names.