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

Issue fork drupal-3309907

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

Spokje created an issue. See original summary.

spokje’s picture

spokje’s picture

StatusFileSize
new25.32 KB

Let's first show Drupal\Tests\book\Functional\BookTest::testGetTableOfContents is an actual (not-so) Random Test Failure by testing a patch that _only_ tests that method 100 times.

spokje’s picture

Issue summary: View changes

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.

spokje’s picture

So if #4 is true, doing a 1500x test on Drupal\Tests\book\Functional\BookTest::testGetTableOfContents only (like the 100x test in #3) and use the 'named_exact' selector, we should have no failures.

spokje’s picture

Checking to see if this also happens on 9.5.x as well.

spokje’s picture

Since 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.

spokje’s picture

Assigned: spokje » Unassigned
Status: Active » Needs review

Well, that's good enough for me, on to NR.

danflanagan8’s picture

Status: Needs review » Reviewed & tested by the community

Good 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

jibran’s picture

I don't see any possible downside of using `named_exact` in this case.

How can we back this statement up?

spokje’s picture

How can we back this statement up?

Besides 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.

    $option_field = $select_field->find('named', ['option', $option]);
    $this->assert($option_field === NULL, sprintf('An option "%s" exists in select "%s", but it should not.', $option, $select));

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.

danflanagan8’s picture

Status: Reviewed & tested by the community » Needs review

@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() and linkExistsExact that use `named` and `named_exact` in their code, respectively.

We could add optionExistsExact and optionNotExistsExact() that follow the same pattern. And then update the book test to use the new method(s).

danflanagan8’s picture

I've had a chance to come back to this after a bit of thinking. Maybe touching optionNotExists within 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 using options(Not)Exists at all. What if we made changed those lines from:

$this->assertSession()->optionNotExists('edit-book-pid', $nodes[10]->id());
$this->assertSession()->optionNotExists('edit-book-pid', $nodes[11]->id());
// Node 9 should be available as an option.
$this->assertSession()->optionExists('edit-book-pid', $nodes[9]->id());

to:

$this->assertSession()->elementNotExists('css', "select[data-drupal-selector='edit-book-pid'] option[value='{$nodes[10]->id()}']");
$this->assertSession()->elementNotExists('css', "select[data-drupal-selector='edit-book-pid'] option[value='{$nodes[11]->id()}']");
// Node 9 should be available as an option.
$this->assertSession()->elementExists('css', "select[data-drupal-selector='edit-book-pid'] option[value='{$nodes[9]->id()}']");

or something similar.

This might be the easiest way to get a fix for this not-so-random fail committed ASAP.

danflanagan8’s picture

StatusFileSize
new1.26 KB

In 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)Exists in 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.

danflanagan8’s picture

Priority: Normal » Major

Changing priority because I think test fails are typically set as Major since they get in the way of other stuff.

nod_’s picture

Status: Needs review » Reviewed & tested by the community

Selector change looks good to me, thank so much for finding the reason why this seemed random.

danflanagan8’s picture

Issue summary: View changes

Updating the IS to account for the solution in the RTBCed patch.

spokje’s picture

Personally I don't think we need this change:

     // Node 9 should be available as an option.
-    $this->assertSession()->optionExists('edit-book-pid', $nodes[9]->id());
+    $this->assertSession()->elementExists('css', "select[data-drupal-selector='edit-book-pid'] option[value='{$nodes[9]->id()}']");

(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)

danflanagan8’s picture

Personally I don't think we need this change:

It's definitely not a mandatory change. My reasoning was in #15:

I changed the positive assertion as well in order to demonstrate the the selector structure being used in the negative assertions is appropriate.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/book/tests/src/Functional/BookTest.php
@@ -358,10 +358,10 @@ public function testGetTableOfContents() {
-    $this->assertSession()->optionNotExists('edit-book-pid', $nodes[10]->id());
-    $this->assertSession()->optionNotExists('edit-book-pid', $nodes[11]->id());

Given 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.

danflanagan8’s picture

Given optionNotExists is responsible for the problem should we be changing it - it is our code? Do we need the partial name functionality?

That'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)Exists but 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 optionExists and optionNotExists to 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)Exists in 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.

longwave’s picture

   * @param string $option
   *   The option value.

The 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.

mondrake’s picture

+1 to #23, that seems the real bahavior we should expect from the method. So I think we should fix the WebAssert code here.

danflanagan8’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs subsystem maintainer review

Thank 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)Exists methods. @Spokje started that approach in the MR. I have just added a commit to the MR that updates optionExists to use `named_exact` so that it stays in sync with optionNotExists.

I've updated the IS a bit and I'm hiding the patch.

longwave’s picture

Issue tags: +Needs change record

Let's write a change record in case anyone was relying on the existing behaviour.

alexpott’s picture

I 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.

longwave’s picture

For maximum safety in 9.5.x and 10.0.x given that the betas are already out, we could make this change only in optionNotExists in those branches?

alexpott’s picture

@longwave given this is only affects tests I think an argument could be made that for maximum safety we make the consistent change everywhere.

catch’s picture

Yeah 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.

danflanagan8’s picture

Issue tags: -Needs change record

I banged out a little CR. Could someone review it?

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Both the MR and the change record look good to me.

longwave’s picture

Oh, and let's make it clear this can go back to 9.5.x - the MR applies to all three branches.

longwave’s picture

Version: 10.0.x-dev » 9.5.x-dev
alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed c9f1974820 to 10.1.x and 2b85363333 to 10.0.x and 600f32029e to 9.5.x. Thanks!

  • alexpott committed c9f1974 on 10.1.x
    Issue #3309907 by Spokje, danflanagan8, longwave: [random test failure]...

  • alexpott committed 2b85363 on 10.0.x
    Issue #3309907 by Spokje, danflanagan8, longwave: [random test failure]...

  • alexpott committed 600f320 on 9.5.x
    Issue #3309907 by Spokje, danflanagan8, longwave: [random test failure]...
heddn’s picture

For 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.

Status: Fixed » Closed (fixed)

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