Skip to content

Commit 1d4f70d

Browse files
authored
Merge pull request #10 from magento-plankton/MAGETWO-63816
MAGETWO-63816
2 parents cd65c2d + 7791e99 commit 1d4f70d

File tree

9 files changed

+249
-4
lines changed

9 files changed

+249
-4
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Block;
7+
8+
/**
9+
* Class CustomerScopeData provide scope (website, store or store_group) information on front
10+
* Can be used, for example, on store front, in order to determine
11+
* that private cache invalid for current scope, by comparing
12+
* with appropriate value in store front private cache.
13+
*/
14+
class CustomerScopeData extends \Magento\Framework\View\Element\Template
15+
{
16+
/**
17+
* @var \Magento\Framework\View\Element\Template\Context
18+
*/
19+
private $storeManager;
20+
21+
/**
22+
* @var \Magento\Framework\Json\EncoderInterface
23+
*/
24+
private $jsonEncoder;
25+
26+
/**
27+
* @param \Magento\Framework\View\Element\Template\Context $context
28+
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
29+
* @param array $data
30+
*/
31+
public function __construct(
32+
\Magento\Framework\View\Element\Template\Context $context,
33+
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
34+
array $data = []
35+
) {
36+
parent::__construct($context, $data);
37+
$this->storeManager = $context->getStoreManager();
38+
$this->jsonEncoder = $jsonEncoder;
39+
}
40+
41+
/**
42+
* Return id of current website
43+
*
44+
* Can be used when necessary to obtain website id of the current customer.
45+
*
46+
* @return integer
47+
*/
48+
public function getWebsiteId()
49+
{
50+
return (int)$this->_storeManager->getStore()->getWebsiteId();
51+
}
52+
}

app/code/Magento/Customer/CustomerData/Customer.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Customer implements SectionSourceInterface
1919
*/
2020
protected $currentCustomer;
2121

22+
/**
23+
* @var View
24+
*/
25+
private $customerViewHelper;
26+
2227
/**
2328
* @param CurrentCustomer $currentCustomer
2429
* @param View $customerViewHelper
@@ -39,10 +44,12 @@ public function getSectionData()
3944
if (!$this->currentCustomer->getCustomerId()) {
4045
return [];
4146
}
47+
4248
$customer = $this->currentCustomer->getCustomer();
4349
return [
4450
'fullname' => $this->customerViewHelper->getCustomerName($customer),
4551
'firstname' => $customer->getFirstname(),
52+
'websiteId' => $customer->getWebsiteId(),
4653
];
4754
}
4855
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Customer\Test\Unit\Block;
7+
8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
use Magento\Framework\View\Element\Template\Context;
10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
use Magento\Customer\Block\CustomerScopeData;
13+
use Magento\Framework\Json\EncoderInterface;
14+
15+
class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/** @var \Magento\Customer\Block\CustomerScopeData */
18+
private $model;
19+
20+
/** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
21+
private $contextMock;
22+
23+
/** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
24+
private $storeManagerMock;
25+
26+
/** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
27+
private $scopeConfigMock;
28+
29+
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
30+
private $encoderMock;
31+
32+
protected function setUp()
33+
{
34+
$this->contextMock = $this->getMockBuilder(Context::class)
35+
->disableOriginalConstructor()
36+
->getMock();
37+
38+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
39+
->getMock();
40+
41+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
42+
->getMock();
43+
44+
$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
45+
->getMock();
46+
47+
$this->contextMock->expects($this->exactly(2))
48+
->method('getStoreManager')
49+
->willReturn($this->storeManagerMock);
50+
51+
$this->contextMock->expects($this->once())
52+
->method('getScopeConfig')
53+
->willReturn($this->scopeConfigMock);
54+
55+
$this->model = new CustomerScopeData(
56+
$this->contextMock,
57+
$this->encoderMock,
58+
[]
59+
);
60+
}
61+
62+
public function testGetWebsiteId()
63+
{
64+
$storeId = 1;
65+
66+
$storeMock = $this->getMockBuilder(StoreInterface::class)
67+
->setMethods(['getWebsiteId'])
68+
->getMockForAbstractClass();
69+
70+
$storeMock->expects($this->any())
71+
->method('getWebsiteId')
72+
->willReturn($storeId);
73+
74+
$this->storeManagerMock->expects($this->any())
75+
->method('getStore')
76+
->with(null)
77+
->willReturn($storeMock);
78+
79+
$this->assertEquals($storeId, $this->model->getWebsiteId());
80+
}
81+
}

app/code/Magento/Customer/view/frontend/layout/default.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
</arguments>
2020
</block>
2121
<block class="Magento\Customer\Block\Account\AuthorizationLink" name="authorization-link"
22-
template="account/link/authorization.phtml"/>
22+
template="Magento_Customer::account/link/authorization.phtml"/>
2323
</referenceBlock>
2424
<referenceContainer name="content">
25-
<block class="Magento\Customer\Block\Account\AuthenticationPopup" name="authentication-popup" as="authentication-popup" template="account/authentication-popup.phtml">
25+
<block class="Magento\Customer\Block\Account\AuthenticationPopup" name="authentication-popup" as="authentication-popup" template="Magento_Customer::account/authentication-popup.phtml">
2626
<arguments>
2727
<argument name="jsLayout" xsi:type="array">
2828
<item name="components" xsi:type="array">
@@ -44,6 +44,8 @@
4444
<block name="customer.customer.data"
4545
class="Magento\Customer\Block\CustomerData"
4646
template="Magento_Customer::js/customer-data.phtml"/>
47+
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
48+
template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
4749
</referenceContainer>
4850
</body>
4951
</page>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
// @codingStandardsIgnoreFile
8+
?>
9+
<?php
10+
/* @var $block \Magento\Customer\Block\CustomerScopeData */
11+
?>
12+
<script type="text/x-magento-init">
13+
<?php
14+
/* @noEscape */
15+
echo \Zend_Json::encode([
16+
'*' => ['Magento_Customer/js/invalidation-processor' => [
17+
'invalidationRules' => [
18+
'website-rule' => [
19+
'Magento_Customer/js/invalidation-rules/website-rule' => [
20+
'scopeConfig' => [
21+
'websiteId' => $block->getWebsiteId(),
22+
]
23+
]
24+
]
25+
]
26+
]],
27+
]);
28+
?>
29+
</script>

app/code/Magento/Customer/view/frontend/web/js/customer-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ define([
340340
var sections,
341341
redirects;
342342

343-
if (settings.type.match(/post|put/i)) {
343+
if (settings.type.match(/post|put|delete/i)) {
344344
sections = sectionConfig.getAffectedSections(settings.url);
345345

346346
if (sections) {
@@ -361,7 +361,7 @@ define([
361361
$(document).on('submit', function (event) {
362362
var sections;
363363

364-
if (event.target.method.match(/post|put/i)) {
364+
if (event.target.method.match(/post|put|delete/i)) {
365365
sections = sectionConfig.getAffectedSections(event.target.action);
366366

367367
if (sections) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'underscore',
7+
'uiElement',
8+
'Magento_Customer/js/customer-data'
9+
], function (_, Element, customerData) {
10+
'use strict';
11+
12+
return Element.extend({
13+
/**
14+
* Initialize object
15+
*/
16+
initialize: function () {
17+
this._super();
18+
this.process(customerData);
19+
},
20+
21+
/**
22+
* Process all rules in loop, each rule can invalidate some sections in customer data
23+
*
24+
* @param {Object} customerDataObject
25+
*/
26+
process: function (customerDataObject) {
27+
_.each(this.invalidationRules, function (rule, ruleName) {
28+
_.each(rule, function (ruleArgs, rulePath) {
29+
require([rulePath], function (Rule) {
30+
var currentRule = new Rule(ruleArgs);
31+
32+
if (!_.isFunction(currentRule.process)) {
33+
throw new Error('Rule ' + ruleName + ' should implement invalidationProcessor interface');
34+
}
35+
currentRule.process(customerDataObject);
36+
});
37+
});
38+
});
39+
}
40+
});
41+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'uiClass'
7+
], function (Element) {
8+
'use strict';
9+
10+
return Element.extend({
11+
12+
defaults: {
13+
scopeConfig: {}
14+
},
15+
16+
/**
17+
* Takes website id from current customer data and compare it with current website id
18+
* If customer belongs to another scope, we need to invalidate current section
19+
*
20+
* @param {Object} customerData
21+
*/
22+
process: function (customerData) {
23+
var customer = customerData.get('customer');
24+
25+
if (this.scopeConfig && customer() &&
26+
~~customer().websiteId !== ~~this.scopeConfig.websiteId && ~~customer().websiteId !== 0) {
27+
customerData.reload(['customer']);
28+
}
29+
}
30+
});
31+
});

app/code/Magento/Customer/view/frontend/web/js/view/customer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
33
* See COPYING.txt for license details.
44
*/
5+
56
define([
67
'uiComponent',
78
'Magento_Customer/js/customer-data'
89
], function (Component, customerData) {
910
'use strict';
1011

1112
return Component.extend({
13+
/** @inheritdoc */
1214
initialize: function () {
1315
this._super();
1416

0 commit comments

Comments
 (0)