-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MC-30171: Add to Cart Form wrong Form Key in FPC
- Loading branch information
Viktor Kopin
committed
Nov 27, 2020
1 parent
f55f411
commit 6f68dc5
Showing
12 changed files
with
287 additions
and
12 deletions.
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