-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
...che/Test/Mftf/ActionGroup/AssertStorefrontAddToCartFormKeyValueIsNotCachedActionGroup.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<actionGroups xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/actionGroupSchema.xsd"> | ||
<actionGroup name="AssertStorefrontAddToCartFormKeyValueIsNotCachedActionGroup"> | ||
<annotations> | ||
<description>Assert that product page add to cart form key is different from cached value.</description> | ||
</annotations> | ||
<arguments> | ||
<argument name="cachedValue" type="string"/> | ||
</arguments> | ||
|
||
<grabValueFrom selector="{{StorefrontProductActionSection.inputFormKey}}" stepKey="grabUpdatedValue"/> | ||
<assertRegExp stepKey="validateCachedFormKey"> | ||
<expectedResult type="string">/\w{16}/</expectedResult> | ||
<actualResult type="string">{{cachedValue}}</actualResult> | ||
</assertRegExp> | ||
<assertRegExp stepKey="validateUpdatedFormKey"> | ||
<expectedResult type="string">/\w{16}/</expectedResult> | ||
<actualResult type="variable">grabUpdatedValue</actualResult> | ||
</assertRegExp> | ||
<assertNotEquals stepKey="assertFormKeyUpdated"> | ||
<expectedResult type="string">{{cachedValue}}</expectedResult> | ||
<actualResult type="variable">grabUpdatedValue</actualResult> | ||
</assertNotEquals> | ||
</actionGroup> | ||
</actionGroups> |
46 changes: 46 additions & 0 deletions
46
app/code/Magento/PageCache/Test/Mftf/Test/StorefrontCachedInputFormKeyValueUpdatedTest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd"> | ||
<test name="StorefrontCachedInputFormKeyValueUpdatedTest"> | ||
<annotations> | ||
<features value="PageCache"/> | ||
<stories value="FormKey"/> | ||
<title value="Form Key value should be updated by js script"/> | ||
<description value="Form Key value should be updated by js script"/> | ||
<testCaseId value="MC-39300"/> | ||
<useCaseId value="MC-30171"/> | ||
<severity value="AVERAGE"/> | ||
<group value="pageCache"/> | ||
</annotations> | ||
<before> | ||
<!-- Create Data --> | ||
<createData entity="SimpleProduct2" stepKey="createProduct"/> | ||
<actionGroup ref="CliCacheCleanActionGroup" stepKey="cleanCache"> | ||
<argument name="tags" value="full_page"/> | ||
</actionGroup> | ||
</before> | ||
<after> | ||
<!-- Delete data --> | ||
<deleteData createDataKey="createProduct" stepKey="deleteProduct"/> | ||
</after> | ||
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="openProductPage"> | ||
<argument name="productUrl" value="$createProduct.custom_attributes[url_key]$"/> | ||
</actionGroup> | ||
<grabValueFrom selector="{{StorefrontProductActionSection.inputFormKey}}" stepKey="grabCachedValue"/> | ||
<resetCookie userInput="PHPSESSID" stepKey="resetSessionCookie"/> | ||
<resetCookie userInput="form_key" stepKey="resetFormKeyCookie"/> | ||
<actionGroup ref="StorefrontOpenProductPageActionGroup" stepKey="reopenProductPage"> | ||
<argument name="productUrl" value="$createProduct.custom_attributes[url_key]$"/> | ||
</actionGroup> | ||
<actionGroup ref="AssertStorefrontAddToCartFormKeyValueIsNotCachedActionGroup" stepKey="assertValueIsUpdatedByScript"> | ||
<argument name="cachedValue" value="{$grabCachedValue}"/> | ||
</actionGroup> | ||
</test> | ||
</tests> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\PageCache\ViewModel; | ||
|
||
use Magento\Framework\View\Element\Block\ArgumentInterface; | ||
use Magento\PageCache\Model\Config; | ||
|
||
/** | ||
* Adds script to update form key from cookie after script rendering | ||
*/ | ||
class FormKeyProvider implements ArgumentInterface | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param Config $config | ||
*/ | ||
public function __construct( | ||
Config $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Is full page cache enabled | ||
* | ||
* @return bool | ||
*/ | ||
public function isFullPageCacheEnabled(): bool | ||
{ | ||
return $this->config->isEnabled(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
app/code/Magento/PageCache/view/frontend/templates/form_key_provider.phtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
if ($block->getFormKeyProvider()->isFullPageCacheEnabled()): ?> | ||
<script type="text/x-magento-init"> | ||
{ | ||
"*": { | ||
"Magento_PageCache/js/form-key-provider": {} | ||
} | ||
} | ||
</script> | ||
<?php endif; ?> |
93 changes: 93 additions & 0 deletions
93
app/code/Magento/PageCache/view/frontend/web/js/form-key-provider.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
define(function () { | ||
'use strict'; | ||
|
||
return function () { | ||
var formKey, | ||
inputElements, | ||
inputSelector = 'input[name="form_key"]'; | ||
|
||
/** | ||
* Set form_key cookie | ||
* @private | ||
*/ | ||
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=/'; | ||
} | ||
|
||
/** | ||
* Retrieves form key from cookie | ||
* @private | ||
*/ | ||
function getFormKeyCookie() { | ||
var cookie, | ||
i, | ||
nameEQ = 'form_key=', | ||
cookieArr = document.cookie.split(';'); | ||
|
||
for (i = 0; i < cookieArr.length; i++) { | ||
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; | ||
} | ||
|
||
/** | ||
* Generate form key string | ||
* @private | ||
*/ | ||
function generateFormKeyString() { | ||
var result = '', | ||
length = 16, | ||
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
|
||
while (length--) { | ||
result += chars[Math.round(Math.random() * (chars.length - 1))]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
/** | ||
* Init form_key inputs with value | ||
* @private | ||
*/ | ||
function initFormKey() { | ||
formKey = getFormKeyCookie(); | ||
|
||
if (!formKey) { | ||
formKey = generateFormKeyString(); | ||
setFormKeyCookie(formKey); | ||
} | ||
inputElements = document.querySelectorAll(inputSelector); | ||
|
||
if (inputElements.length) { | ||
Array.prototype.forEach.call(inputElements, function (element) { | ||
element.setAttribute('value', formKey); | ||
}); | ||
} | ||
} | ||
|
||
initFormKey(); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
dev/tests/js/jasmine/tests/app/code/Magento/PageCache/frontend/js/form-key-provider.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
/* eslint-disable max-nested-callbacks */ | ||
define([ | ||
'jquery', | ||
'Magento_PageCache/js/form-key-provider' | ||
], function ($, formKeyInit) { | ||
'use strict'; | ||
|
||
describe('Testing FormKey Provider', function () { | ||
var inputContainer; | ||
|
||
beforeEach(function () { | ||
inputContainer = document.createElement('input'); | ||
inputContainer.setAttribute('value', ''); | ||
inputContainer.setAttribute('name', 'form_key'); | ||
document.querySelector('body').appendChild(inputContainer); | ||
}); | ||
|
||
afterEach(function () { | ||
$(inputContainer).remove(); | ||
document.cookie = 'form_key= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'; | ||
}); | ||
|
||
it('sets value of input[form_key]', function () { | ||
var expires, | ||
date = new Date(); | ||
|
||
date.setTime(date.getTime() + 86400000); | ||
expires = '; expires=' + date.toUTCString(); | ||
document.cookie = 'form_key=FAKE_COOKIE' + expires + '; path=/'; | ||
formKeyInit(); | ||
expect($(inputContainer).val()).toEqual('FAKE_COOKIE'); | ||
}); | ||
|
||
it('widget sets value to input[form_key] in case it empty', function () { | ||
document.cookie = 'form_key= ; expires = Thu, 01 Jan 1970 00:00:00 GMT'; | ||
formKeyInit(); | ||
expect($(inputContainer).val()).toEqual(jasmine.any(String)); | ||
expect($(inputContainer).val().length).toEqual(16); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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