Skip to content

Commit 8cfa3c6

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-59135' into BUGS
2 parents 8858ecc + edd846b commit 8cfa3c6

File tree

9 files changed

+286
-1
lines changed

9 files changed

+286
-1
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
/**
3+
* Copyright © 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+
* @api
14+
*/
15+
class CustomerScopeData extends \Magento\Framework\View\Element\Template
16+
{
17+
/**
18+
* @var \Magento\Framework\View\Element\Template\Context
19+
*/
20+
private $storeManager;
21+
22+
/**
23+
* @var \Magento\Framework\Json\EncoderInterface
24+
*/
25+
private $jsonEncoder;
26+
27+
/**
28+
* @param \Magento\Framework\View\Element\Template\Context $context
29+
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
30+
* @param array $data
31+
*/
32+
public function __construct(
33+
\Magento\Framework\View\Element\Template\Context $context,
34+
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
35+
array $data = []
36+
) {
37+
parent::__construct($context, $data);
38+
$this->storeManager = $context->getStoreManager();
39+
$this->jsonEncoder = $jsonEncoder;
40+
}
41+
42+
/**
43+
* Return id of current website
44+
*
45+
* Can be used when necessary to obtain website id of the current customer.
46+
*
47+
* @return integer
48+
*/
49+
public function getWebsiteId()
50+
{
51+
return (int)$this->_storeManager->getStore()->getWebsiteId();
52+
}
53+
}

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 © 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
<block name="customer.customer.data"
4646
class="Magento\Customer\Block\CustomerData"
4747
template="Magento_Customer::js/customer-data.phtml"/>
48+
<block name="customer.data.invalidation.rules" class="Magento\Customer\Block\CustomerScopeData"
49+
template="Magento_Customer::js/customer-data/invalidation-rules.phtml"/>
4850
</referenceContainer>
4951
</body>
5052
</page>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © 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+
'website_id' => $block->getWebsiteId(),
22+
]
23+
]
24+
]
25+
]
26+
]],
27+
]);
28+
?>
29+
</script>

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ define([
214214
this.reload(storageInvalidation.keys(), false);
215215
}
216216
}
217-
218217
if (!_.isEmpty(privateContent)) {
219218
countryData = this.get('directory-data');
220219

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
var invalidationRules;
12+
13+
return Element.extend({
14+
initialize: function () {
15+
this._super();
16+
this.process(customerData);
17+
},
18+
19+
/**
20+
* Process all rules in loop, each rule can invalidate some sections in customer data
21+
*
22+
* @param {Object} customerData
23+
*/
24+
process: function (customerData) {
25+
_.each(this.invalidationRules, function (rule, ruleName) {
26+
_.each(rule, function (ruleArgs, rulePath) {
27+
require([rulePath], function (Rule) {
28+
var rule = new Rule(ruleArgs);
29+
if (!_.isFunction(rule.process)) {
30+
throw new Error("Rule " + ruleName + " should implement invalidationProcessor interface");
31+
}
32+
rule.process(customerData);
33+
});
34+
});
35+
});
36+
}
37+
});
38+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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() && customer().websiteId != this.scopeConfig.website_id) {
26+
customerData.reload(['customer']);
27+
}
28+
}
29+
});
30+
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
define([
2+
'squire'
3+
], function (Squire) {
4+
'use strict';
5+
6+
var injector = new Squire(),
7+
mocks = {
8+
'Magento_Customer/js/customer-data': {
9+
get: jasmine.createSpy().and.returnValue({})
10+
}
11+
},
12+
processor;
13+
14+
beforeEach(function (done) {
15+
injector.mock(mocks);
16+
injector.require(['Magento_Customer/js/invalidation-processor'], function (Constr) {
17+
processor = new Constr({
18+
name: 'processor'
19+
});
20+
done();
21+
});
22+
});
23+
24+
describe('Magento_Customer/js/invalidation-processor', function () {
25+
26+
describe('"process" method', function () {
27+
it('record status is 1', function () {
28+
var requireTmp = require;
29+
30+
processor.invalidationRules = {
31+
'website-rule': {
32+
'Magento_Customer/js/invalidation-rules/website-rule': {
33+
process: jasmine.createSpy()
34+
}
35+
}
36+
};
37+
38+
require = jasmine.createSpy();
39+
processor.process();
40+
41+
expect(require).toHaveBeenCalled();
42+
require = requireTmp;
43+
});
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)