Skip to content

Commit ad118e6

Browse files
committed
MAGETWO-80177: [2.2.x] - Remove zend json from customer data #10259
1 parent 9c14af7 commit ad118e6

File tree

3 files changed

+63
-12
lines changed

3 files changed

+63
-12
lines changed

app/code/Magento/Customer/Block/CustomerScopeData.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,15 @@
55
*/
66
namespace Magento\Customer\Block;
77

8+
use Magento\Framework\App\ObjectManager;
9+
use Magento\Framework\Serialize\Serializer\Json;
10+
811
/**
912
* Class CustomerScopeData provide scope (website, store or store_group) information on front
1013
* Can be used, for example, on store front, in order to determine
1114
* that private cache invalid for current scope, by comparing
1215
* with appropriate value in store front private cache.
16+
*
1317
* @api
1418
* @since 100.2.0
1519
*/
@@ -21,23 +25,27 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
2125
private $storeManager;
2226

2327
/**
24-
* @var \Magento\Framework\Json\EncoderInterface
28+
* @var Json
2529
*/
26-
private $jsonEncoder;
30+
private $serializer;
2731

2832
/**
2933
* @param \Magento\Framework\View\Element\Template\Context $context
3034
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
3135
* @param array $data
36+
* @param Json|null $serializer
37+
* @throws \RuntimeException
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
3239
*/
3340
public function __construct(
3441
\Magento\Framework\View\Element\Template\Context $context,
3542
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
36-
array $data = []
43+
array $data = [],
44+
Json $serializer = null
3745
) {
3846
parent::__construct($context, $data);
3947
$this->storeManager = $context->getStoreManager();
40-
$this->jsonEncoder = $jsonEncoder;
48+
$this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
4149
}
4250

4351
/**
@@ -52,4 +60,16 @@ public function getWebsiteId()
5260
{
5361
return (int)$this->_storeManager->getStore()->getWebsiteId();
5462
}
63+
64+
/**
65+
* Encode invalidation rules.
66+
*
67+
* @param array $configuration
68+
* @return bool|string
69+
* @throws \InvalidArgumentException
70+
*/
71+
public function encodeConfiguration(array $configuration)
72+
{
73+
return $this->serializer->serialize($configuration);
74+
}
5575
}

app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Store\Api\Data\StoreInterface;
1111
use Magento\Store\Model\StoreManagerInterface;
1212
use Magento\Customer\Block\CustomerScopeData;
13-
use Magento\Framework\Json\EncoderInterface;
1413

1514
class CustomerScopeDataTest extends \PHPUnit\Framework\TestCase
1615
{
@@ -29,6 +28,9 @@ class CustomerScopeDataTest extends \PHPUnit\Framework\TestCase
2928
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
3029
private $encoderMock;
3130

31+
/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
32+
private $serializerMock;
33+
3234
protected function setUp()
3335
{
3436
$this->contextMock = $this->getMockBuilder(Context::class)
@@ -41,7 +43,10 @@ protected function setUp()
4143
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
4244
->getMock();
4345

44-
$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
46+
$this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
47+
->getMock();
48+
49+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
4550
->getMock();
4651

4752
$this->contextMock->expects($this->exactly(2))
@@ -55,7 +60,8 @@ protected function setUp()
5560
$this->model = new CustomerScopeData(
5661
$this->contextMock,
5762
$this->encoderMock,
58-
[]
63+
[],
64+
$this->serializerMock
5965
);
6066
}
6167

@@ -78,4 +84,33 @@ public function testGetWebsiteId()
7884

7985
$this->assertEquals($storeId, $this->model->getWebsiteId());
8086
}
87+
88+
public function testEncodeConfiguration()
89+
{
90+
$rules = [
91+
'*' => [
92+
'Magento_Customer/js/invalidation-processor' => [
93+
'invalidationRules' => [
94+
'website-rule' => [
95+
'Magento_Customer/js/invalidation-rules/website-rule' => [
96+
'scopeConfig' => [
97+
'websiteId' => 1,
98+
]
99+
]
100+
]
101+
]
102+
]
103+
],
104+
];
105+
106+
$this->serializerMock->expects($this->any())
107+
->method('serialize')
108+
->with($rules)
109+
->willReturn(json_encode($rules));
110+
111+
$this->assertEquals(
112+
json_encode($rules),
113+
$this->model->encodeConfiguration($rules)
114+
);
115+
}
81116
}

app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@
55
*/
66

77
// @codingStandardsIgnoreFile
8-
?>
9-
<?php
108
/* @var $block \Magento\Customer\Block\CustomerScopeData */
119
?>
1210
<script type="text/x-magento-init">
13-
<?php
14-
/* @noEscape */
15-
echo \Zend_Json::encode([
11+
<?= /* @noEscape */ $block->encodeConfiguration([
1612
'*' => ['Magento_Customer/js/invalidation-processor' => [
1713
'invalidationRules' => [
1814
'website-rule' => [

0 commit comments

Comments
 (0)