Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
\Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait::assertBreadcrumbParts() is vulnerable to false positive because if
$parts = $this->getBreadcrumbParts();
returns an empty array it will always pass when it should fail. There's never a case for calling \Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait::assertBreadcrumb() when there is no breadcrumb.
Proposed resolution
Change
$parts = $this->getBreadcrumbParts();
$pass = TRUE;
to
$parts = $this->getBreadcrumbParts();
// Fail the test if there is no breadcrumb to assert against.
$pass = !empty($parts);
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#15 | 3025580-8.patch | 3.2 KB | alexpott |
#14 | interdiff-8-14.txt | 1.94 KB | Krzysztof Domański |
#14 | 3025580-14.patch | 4.17 KB | Krzysztof Domański |
#11 | Screen Shot 2019-01-13 at 5.37.01 PM.png | 218.3 KB | Kristen Pol |
#8 | 3025580-8.patch | 3.2 KB | alexpott |
Comments
Comment #2
alexpottLet's see if anything breaks.
Comment #3
alexpottHere's a test that shows the problem and that the fix fixes it.
Comment #5
Kristen PolThanks for the patch. These are nitpicks of the comments that could be applied.
Nitpick: This wording seems unclear. "No trail should fail" sounds to me like there is no trail that should fail. Perhaps a slight change like:
If there is no trail, this assert should fail.
Or
If there is no trail, this should fail.
Nitpick: Since this is used twice and is used for comparison, consider pulling out string into a variable.
Nitpick: Since this text is used twice and is used for comparison, consider pulling out string into a variable.
Nitpick: If the other phrasing is changed above, consider updating this one as well:
If the trail is incorrect, this assert should fail.
Or
If the trail is incorrect, this should fail.
Nitpick: Similar to previous suggestion, perhaps:
If there is no trail, this should pass as there is no breadcrumb.
Or, use a wording similar to the next comment:
There is no breadcrumb, so testing without a trail should pass.
Nitpick: Since this text is used twice and is used for comparison, consider pulling out string into a variable.
Comment #6
Kristen PolI'm not sure how to test this (since it's only for testing code) but one of my notes from previous comment wasn't saved in dreditor so adding here:
Nitpick: For those who don't know about the test routes, consider changes comment to something like:
Ensure the test trait works as expected using menu_test routes.
Comment #7
Kristen PolComment #8
alexpottThanks for the review @Kristen Pol - I hope I've addressed all your feedback. I've used your suggestions. With respect to testing the patch - the test only patch is a good test - as you can see that the assertion doesn't fail when you'd expect it to. I.e. the following case.
Comment #9
Kristen PolWow. You are fast. :) Ok, I'm going to review the changes in a couple hours (going for a walk). Thanks!
Comment #11
Kristen PolThanks for the updates. I reviewed the interdiff and the changes look good to me. I see the failure in the tests (screenshot attached). Should I be doing anything else to test this?
Comment #12
Kristen PolI'll mark RTBC but please see the last comment.
Comment #13
larowlanShould we be doing the same in the deprecated \Drupal\system\Tests\Menu\AssertBreadcrumbTrait ?
why not just $this->fail() here instead of continuing?
Comment #14
Krzysztof Domański1. Refactoring assertBreadcrumbParts().
Check in a loop if both (trait and breadcrumb) are not empty.
Fail if there is no breadcrumb and we have a trail or we have breadcrumb but trait is empty.
2. IMO this comments is not necessary.
Comment #15
alexpott@Krzysztof Domański mixing a refactor with a bug fix is not how to do things. It makes it harder to understand the changes. If you want to do that open a task.
@larowlan we could but then we'd have to duplicate the later message and message consistency is nice. So for me this is better. Re-uploading #8.
Comment #16
alexpottAlso I think we should be leaving deprecated code alone. Otherwise we have to add a test for this and there is no test for the old trait atm.
Comment #17
Krzysztof Domański#15
I think the while loop is more difficult to analyze. If
empty($trail) && !empty($parts)
loop will not execute so $pass will be also FALSE. It can be written more simply:#14
Considering that #15 solving a problem so RTBC. I will add a new tusk to refactor while loop after fix this issue.
Comment #19
larowlanCommitted aba8c85 and pushed to 8.7.x. Thanks!
sounds good
not sure that is a reason on its own - we have local variables, but don't care enough to push the issue - let's just get this in
Comment #20
Krzysztof DomańskiI added the issue in order to replace the while loop to conditional statement (#14.1).
#3030403: Improve the readability of the code in assertBreadcrumbParts