Thanks to visit codestin.com
Credit goes to github.com

Skip to content

FIX: Avoid passing empty needle to strpos()#13431

Merged
lchrusciel merged 3 commits into
Sylius:1.10from
rimas-kudelis:patch-1
Feb 7, 2022
Merged

FIX: Avoid passing empty needle to strpos()#13431
lchrusciel merged 3 commits into
Sylius:1.10from
rimas-kudelis:patch-1

Conversation

@rimas-kudelis
Copy link
Copy Markdown
Contributor

Q A
Branch? 1.10
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets none
License MIT

The province validator returns early if it finds preexisting validation errors on the address. However, when checking that, it uses strpos() and passes current $propertyPath as a needle. This might cause problems when an address is validated as a top-level entity, because $propertyPath is an empty string in such case.

@rimas-kudelis rimas-kudelis requested a review from a team as a code owner December 31, 2021 15:53
@rimas-kudelis rimas-kudelis changed the title Avoid passing empty needle to strpos() FIX: Avoid passing empty needle to strpos() Dec 31, 2021
@Zales0123 Zales0123 added the Bug Confirmed bugs or bugfixes. label Jan 3, 2022
Copy link
Copy Markdown
Contributor

@vvasiloi vvasiloi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thank you, @rimas-kudelis!

@rimas-kudelis
Copy link
Copy Markdown
Contributor Author

@lchrusciel ping

Copy link
Copy Markdown
Contributor

@Zales0123 Zales0123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Comment on lines +82 to +91
$country->getCode()->willReturn('PL');
$address->getCountryCode()->willReturn('PL');
$countryRepository->findOneBy(['code' => 'PL'])->willReturn($country);

$country->hasProvinces()->willReturn(true);
$address->getProvinceCode()->willReturn(null);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this and the similar mocks in the spec above are not needed? These functions are never called as the isProvinceValid function is not reached. I even removed them and specs were still green 💃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I suggested to add those, but I can't remember why.
Were the tests false positive, @rimas-kudelis?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zales0123 Yes, I think that was it... that's how we tested that we need the fix.
With the fix, the builds are green either way, but without the fix, it only fails with the proper setup (the mocks).
The mocks are required to prevent regression.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rimas-kudelis maybe leave a comment there, because in another month who knows if anyone can remember this. 😅

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually put that info in the commit message. But sure, I can put it in a comment as well.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those extra bits of setup that were added for both tests are for the whole foreach, not just for the condition that Rimas added, which can be reproduced with PHP 7.4.
If you revert everything in changed in this PR, or just start with a 1.10 branch and remove that foreach, then the tests will still pass. Somehow I forgot to mention this in my previous comments.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why are you not using https://3v4l.org/, @lchrusciel?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can see the result for all versions at once there: https://3v4l.org/5ma7S

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Zales0123 like I said, I agree that asserting that ->getCountryCode() must not be called would make the tests fail. But it feels to me like an assertion on How the validator works instead of What the expected result is. The way I wrote it allows the test subject to remain more of a blackbox, doesn't it?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I missed the Mateusz's latest message.
I second Rimas here and I also said something among those line earlier.
We're testing the validator here and the test is called it_does_not_add_violation, so I don't see why we would assert the behavior on the address object instead of the validator context.
There's a good synergy between: it_does_not_add_violation and $context->addViolation(Argument::any())->shouldNotBeCalled();

@probot-autolabeler probot-autolabeler Bot added Documentation Documentation related issues and PRs - requests, fixes, proposals. Maintenance CI configurations, READMEs, releases, etc. labels Jan 24, 2022
Rimas Kudelis added 3 commits January 24, 2022 20:50
It seems current tests would pass even without the logic in place. This
makes them more explicit by adding actual conditions which would cause
them to fail if the necessary logic wasn't in place.

See review comments on #13431.
lchrusciel
lchrusciel previously approved these changes Feb 2, 2022
@lchrusciel lchrusciel dismissed their stale review February 3, 2022 15:46

I would like to test it manually

@lchrusciel lchrusciel merged commit 61a1041 into Sylius:1.10 Feb 7, 2022
@lchrusciel
Copy link
Copy Markdown
Contributor

Thank you, Rimas! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Confirmed bugs or bugfixes. Documentation Documentation related issues and PRs - requests, fixes, proposals. Maintenance CI configurations, READMEs, releases, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants