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

MC-30171: Add to Cart Form wrong Form Key in FPC #30961

Merged
merged 3 commits into from
Dec 2, 2020

Conversation

engcom-Golf
Copy link
Contributor

Description (*)

Fixes behavior of late form key update by js script. The product could be added to the cart with a cached form_key value that causes an exception.

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Add to Cart Form wrong Form Key in FPC #13746

Manual testing scenarios (*)

  1. Magento installed
  2. Simple product created
  3. Full page cache flushed
  4. Open product view page, note down form key
  5. Open the same page in other browser or incognito window
  6. Open page source
    Expected result:
    form_key is the same as in step 4. It is a cached value
  7. Open normal page view
  8. Open DevTools in the browser
  9. Enable Slow 3G throttling in the network tab
  10. Refresh product page
  11. Find form key element until page completely loaded
    Expected result:
    Form key value differs from the value on steps 4 and 6
    Correct form key value is loaded instantly when the element is rendered

Questions or comments

Script logic in inline_form_key_provider.phtml can not be covered by any kind of automated tests. Just only manual testing.

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 are green)

@m2-assistant
Copy link

m2-assistant bot commented Nov 18, 2020

Hi @engcom-Golf. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-community-project m2-community-project bot added Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround. labels Nov 18, 2020
@engcom-Golf
Copy link
Contributor Author

@magento run all tests


use Magento\Framework\View\Helper\SecureHtmlRenderer;

$scriptString = <<<HTML
Copy link
Contributor

Choose a reason for hiding this comment

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

his is not HTML, should be <<<JS probably.

Copy link
Contributor

Choose a reason for hiding this comment

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

this should be <<<script and close with script;

Comment on lines 10 to 48
function setFormKeyCookie(value) {
var expires,
secure,
date = new Date(),
isSecure = !!window.cookiesConfig && window.cookiesConfig.secure;
date.setTime(date.getTime() + 86400000);
expires = '; expires=' + date.toUTCString();
secure = isSecure ? '; secure' : '';

document.cookie = 'form_key=' + (value || '') + expires + secure + '; path=/';
}
function getFormKeyCookie() {
var nameEQ = 'form_key=',
cookieArr = document.cookie.split(';');
for (var i = 0; i < cookieArr.length; i++) {
var cookie = cookieArr[i];
while(cookie.charAt(0) == ' ') {
cookie = cookie.substring(1, cookie.length);
};
if (cookie.indexOf(nameEQ) == 0) {
return cookie.substring(nameEQ.length, cookie.length);
};
}
return null;
}
function generateCookieString() {
var result = '',
length = 16;
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
while (length--) {
result += chars[Math.round(Math.random() * (chars.length - 1))];
}
return result;
}
var formKey = getFormKeyCookie();
if (!formKey) {
formKey = generateCookieString();
setFormKeyCookie(formKey);
}
Copy link
Contributor

@ihor-sviziev ihor-sviziev Nov 18, 2020

Choose a reason for hiding this comment

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

  1. Could you not expose all these functions to the global scope, but only the needed (window.formKey)?
  2. As this code will be added to all pages - maybe it's better to include it as external JS file, so it will be at least cached between the pages OR maybe we can minify it for production mode?

@m2-community-project m2-community-project bot moved this from Review in Progress to Changes Requested in High Priority Pull Requests Dashboard Nov 18, 2020
@ihor-sviziev ihor-sviziev added Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: test coverage labels Nov 18, 2020
@engcom-Golf
Copy link
Contributor Author

@magento run Functional Tests B2B, Functional Tests CE, Functional Tests EE, Integration Tests

@engcom-Golf
Copy link
Contributor Author

@magento run all tests

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

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

Could you fix one small issue and squash your changes to single commit?

<body>
<referenceBlock name="head.components">
<block class="Magento\Framework\View\Element\Js\Components" name="pagecache_page_head_components" template="Magento_PageCache::js/components.phtml"/>
</referenceBlock>
<referenceBlock name="head.additional">
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this block could be removed. Could you do that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure thing. accidentally missed it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved logic from head to head.additional block to load asynchronously, won't remove

@m2-community-project m2-community-project bot moved this from Review in Progress to Changes Requested in High Priority Pull Requests Dashboard Nov 18, 2020
@engcom-Golf
Copy link
Contributor Author

@magento run all tests

@magento-engcom-team
Copy link
Contributor

Hi @ihor-sviziev, thank you for the review.
ENGCOM-8489 has been created to process this Pull Request

@engcom-Kilo
Copy link
Contributor

@magento-engcom-team
Copy link
Contributor

@engcom-Kilo the branch with code successfully imported intomagento-tsg/magento2ce repository. Branch name: imported-magento-magento2-30961.

@engcom-Bravo
Copy link
Contributor

✔️ QA Passed

The result is the same like in the comment above #30961 (comment)

@engcom-Bravo engcom-Bravo moved this from Ready for Testing to Testing in Progress in High Priority Pull Requests Dashboard Nov 28, 2020
@engcom-Bravo engcom-Bravo moved this from Testing in Progress to Merge in Progress in High Priority Pull Requests Dashboard Nov 28, 2020
@m2-community-project m2-community-project bot moved this from Merge in Progress to Testing in Progress in High Priority Pull Requests Dashboard Nov 28, 2020
@m2-community-project m2-community-project bot moved this from Testing in Progress to Merge in Progress in High Priority Pull Requests Dashboard Nov 28, 2020
# Conflicts:
#	app/code/Magento/Checkout/Test/Mftf/Test/StoreFrontFreeShippingRecalculationAfterCouponCodeAddedTest.xml
@engcom-Golf
Copy link
Contributor Author

@magento run all tests

@zakdma
Copy link
Contributor

zakdma commented Dec 1, 2020

@magento-engcom-team
Copy link
Contributor

@zakdma the branch with code successfully imported intomagento-tsg/magento2ce repository. Branch name: imported-magento-magento2-30961.

@magento-engcom-team magento-engcom-team merged commit be5db4a into magento:2.4-develop Dec 2, 2020
@m2-assistant
Copy link

m2-assistant bot commented Dec 2, 2020

Hi @engcom-Golf, thank you for your contribution!
Please, complete Contribution Survey, it will take less than a minute.
Your feedback will help us to improve contribution process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Frontend Auto-Tests: Covered All changes in Pull Request is covered by auto-tests Award: bug fix Award: category of expertise Award: MFTF test coverage Award: test coverage Component: Catalog Component: Checkout Component: PageCache Priority: P1 Once P0 defects have been fixed, a defect having this priority is the next candidate for fixing. Progress: accept QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope Release Line: 2.4 Severity: S3 Affects non-critical data or functionality and does not force users to employ a workaround.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add to Cart Form wrong Form Key in FPC
8 participants