Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes regression bug introduced in Magento 2.1.6 where the layered navigation options are sometimes being cached using the wrong store id. #9704

Merged
merged 1 commit into from
Jun 7, 2017

Conversation

hostep
Copy link
Contributor

@hostep hostep commented May 19, 2017

Description

Fixes problem with layered navigation options being cached using the wrong store id.

This problem only exists on Magento 2.1.6, not on the develop branch.
The problem was introduced in 67b5ff7

Fixed Issues (if relevant)

  1. Translation for layered navigation attribute option not working #9679: Translation for layered navigation attribute option not working

Manual testing scenarios

  1. Set up a new Magento 2.1.6
  2. Make sure you have 2 storeviews, I used the codes: 'en' and 'nl' (English and Dutch)
  3. Go to Stores => Configuration => Web => Url Options and set 'Add Store Code to Urls' to 'Yes'
  4. Create a new category, I used 'Test'
  5. Open the 'Color' attribute and add 2 options:
Admin English Dutch
Red Red Rood
Black Black Zwart
  1. Add the 'Color' attribute to the 'Default' attributeset
  2. Create 2 products, assign them to the 'Test' category, make sure the first one has color 'Red' and the second one has color 'Black'
  3. Reindex
  4. Flush cache
  5. VERY IMPORTANT: Do NOT use the storeswitcher on the frontend in the next steps
  6. In the frontend, go the the test category in English: http(s)://mydomain/en/test.html
  7. Check the color options, it will say: 'Red' and 'Black', good!
  8. Now go to the test category in Dutch, directly using the url, not the storeswitcher: http(s)://mydomain/nl/test.html
  9. Check the color options, it will say: 'Red' and 'Black', not good!
  10. Flush the cache
  11. Refresh the Dutch page
  12. Check the color options, it will say: 'Rood' and 'Zwart', good!
  13. Again, go to the English page using the url
  14. Check the color options, it will say: 'Rood' and 'Zwart', not good!

Discussion

This problem exists because the $cacheKey is using the default store id every time if you don't use the storeswitcher. So the wrong info is cached and being used from the cache in the above cases.

The bug happens, because the original code calls $this->storeResolver->getCurrentStoreId(), this function checks if either the url has a '___store=xxx' param or if a cookie named 'store' exists. If neither of these things exist, it will return the default store view id.
When you use the storeswitcher, it will set a cookie and the functionality will work. But what about visitors going to the url directly just after the cache was flushed? They will put the wrong info into the cache.

This PR targets the 2.1-develop branch, as the develop branch hasn't gotten this MAGETWO-65484 feature yet, which is strange...

Inspiration of this PR came from this other PR, which fixes some similar issues: #9429

Would be really great if this could get included in the next minor release, as this is a pretty important bug which should get fixed ASAP.

It might be a good idea if this functionality is covered by some kind of test.
I have almost 0 experience with writing tests, so I won't do this for now, if somebody else is willing to do this, be my guest :)

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

* @param array $cacheTags
* @codeCoverageIgnore
*/
public function __construct(
\Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory $attrBooleanFactory,
CacheInterface $cache = null,
StoreResolverInterface $storeResolver = null,
StoreManagerInterface $storeManager = null,
Copy link
Contributor

Choose a reason for hiding this comment

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

that's a backward incompatible change,
and we can't just remove existing dependency and add another one instead.

So, existing dependency should be @deprecated
while new dependency added at the end of the list

Copy link
Contributor Author

@hostep hostep May 20, 2017

Choose a reason for hiding this comment

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

Really? It only existed in Magento 2.1.6, in no other version. It would very much surprise me if any 3rd party already extends this class.

But if you really want me to add it as a new param, then I can do that.
Please let me know.

@hostep
Copy link
Contributor Author

hostep commented May 21, 2017

@maghamed: I applied your requested changes, please let me know if everything is ok now. Thanks! :)

UPDATE: I forgot to mark the $storeResolver param in the constructor as @deprecated, but I don't know exactly how to do that? I can't seem to find any examples for marking a param as @deprecated at first sight in Magento's current code?

@hostep
Copy link
Contributor Author

hostep commented May 22, 2017

I just noticed the 3 failures in the static tests.

I don't understand the first two, because there is a doc comment about the $storeManager variable?
I understand the third one, but how can I solve it? It is being generated because we want to remain backwards compatible by not removing the $storeResolver from the constructor params. But that param isn't being used anymore, so it makes sense that a warning is being thrown.

Please let me know how to resolve these static test failures. Thanks! :)

hostep referenced this pull request May 22, 2017
[Tango] MAGETWO-66825: Caching of attribute options for Layered Navigation
@hostep
Copy link
Contributor Author

hostep commented May 22, 2017

I just see that this feature (with the same bug) has landed on the develop branch: 938313e

@hostep
Copy link
Contributor Author

hostep commented May 22, 2017

After figuring out how to run these tests locally, I was able to suppress the third warning.

The first two warnings feel like a bug in PHPCS itself, if I change the default assignment of the $cacheTags param to an empty array in the constructor, the static tests go through fine, so PHPCS isn't properly detecting the different params, I think.
Just a super wild guess here, but it might be fixed by upgrading PHPCS to at least version 2.0.0RC1, where they switched to using a comment tokenizer instead of a parser: squizlabs/PHP_CodeSniffer@9113cd6 (but this is only guessing, I haven't actually tested this)

Anyway, I just pushed an update to suppress the third warning.

@hostep
Copy link
Contributor Author

hostep commented May 25, 2017

I just pushed another update, which should fix all static tests.

I took the approach like was done in 938313e with the $defaultCacheTags member.
Not entirely sure if this doesn't break your backwards compatibility rules, but I don't know another way of fixing the static tests (which I still believe occur because of a bug in PHP CodeSniffer).

I'm also still waiting on feedback about how to mark the $storeResolver param as @deprecated.

Also: since this bug now also exists in the develop branch, who will take care of this? Should I create a new PR against the develop branch, or are you guys looking into this?

Thanks!

@hostep
Copy link
Contributor Author

hostep commented May 29, 2017

@maghamed or @okorshenko: it would be great if I could get some feedback on this PR, because it feels like this PR doesn't get any attention anymore from Magento. And since it fixes a regression bug it should get included in the next minor release preferably.

Thanks!

@ishakhsuvarov ishakhsuvarov self-assigned this May 29, 2017
@ishakhsuvarov ishakhsuvarov added this to the May 2017 milestone May 29, 2017
@okorshenko
Copy link
Contributor

okorshenko commented May 29, 2017

Hi @hostep , @ishakhsuvarov is working on this PR. Initial testing successfully passed. We are working right now on more detailed testing of this pull request.

Thank you

@okorshenko okorshenko modified the milestones: May 2017, June 2017 Jun 1, 2017
@magento-team magento-team merged commit 488be10 into magento:2.1-develop Jun 7, 2017
magento-team pushed a commit that referenced this pull request Jun 7, 2017
… the layered navigation options are sometimes being cached using the wrong store id. #9704
magento-team pushed a commit that referenced this pull request Jun 7, 2017
… the layered navigation options are sometimes being cached using the wrong store id. #9704
magento-team pushed a commit that referenced this pull request Jun 7, 2017
[EngCom] Public Pull Requests - 2.1
 - MAGETWO-69665: Backport of MAGETWO-59089 for Magento 2.1: Magento 2.1.1 Problem with change currency #9841
 - MAGETWO-69543: Fixes regression bug introduced in Magento 2.1.6 where the layered navigation options are sometimes being cached using the wrong store id. #9704
 - MAGETWO-69151: Do not hardcode product link types [2.1 backport] #9601
 - MAGETWO-69691: Travis build failures because the job exceeded the maximum time limit for jobs - 2.1-develop
@magento-team
Copy link
Contributor

@hostep thank you for your contribution

@hostep
Copy link
Contributor Author

hostep commented Jun 7, 2017

@magento-team or @okorshenko: can you let me know who will take care of fixing this on the develop branch? Are you guys looking into this, or should I create a new PR for this?

Thanks!

@ishakhsuvarov
Copy link
Contributor

@hostep I don't think it is in progress internally at the moment, so feel free to submit a PR for that.
Thank you

@hostep
Copy link
Contributor Author

hostep commented Jun 7, 2017

@ishakhsuvarov: new PR created for the develop branch: #9873

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

Successfully merging this pull request may close these issues.

5 participants