Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit b745d6f

Browse files
committed
Merge branch 'MAGETWO-80177' into bugs
2 parents 5b1d6b1 + a6053ad commit b745d6f

File tree

21 files changed

+341
-81
lines changed

21 files changed

+341
-81
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' => [

app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxLoad.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class AjaxLoad extends \Magento\Tax\Controller\Adminhtml\Rate
1414
/**
1515
* Json needed for the Ajax Edit Form
1616
*
17-
* @return void
17+
* @return \Magento\Framework\Controller\Result\Json
18+
* @throws \InvalidArgumentException
1819
*/
1920
public function execute()
2021
{
@@ -23,13 +24,13 @@ public function execute()
2324
/* @var \Magento\Tax\Api\Data\TaxRateInterface */
2425
$taxRateDataObject = $this->_taxRateRepository->get($rateId);
2526
/* @var array */
26-
$resultArray= $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject, true);
27+
$resultArray = $this->_taxRateConverter->createArrayFromServiceObject($taxRateDataObject, true);
2728

2829
$responseContent = [
2930
'success' => true,
3031
'error_message' => '',
31-
'result'=>$resultArray,
32-
];
32+
'result' => $resultArray,
33+
];
3334
} catch (NoSuchEntityException $e) {
3435
$responseContent = [
3536
'success' => false,

app/code/Magento/Tax/Controller/Adminhtml/Rate/AjaxSave.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class AjaxSave extends \Magento\Tax\Controller\Adminhtml\Rate
1414
* Save Tax Rate via AJAX
1515
*
1616
* @return \Magento\Framework\Controller\Result\Json
17+
* @throws \InvalidArgumentException
1718
*/
1819
public function execute()
1920
{

dev/tests/integration/framework/Magento/TestFramework/TestCase/AbstractController.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ abstract class AbstractController extends \PHPUnit\Framework\TestCase
2626
protected $_runOptions = [];
2727

2828
/**
29-
* @var \Magento\TestFramework\Request
29+
* @var \Magento\Framework\App\RequestInterface
3030
*/
3131
protected $_request;
3232

3333
/**
34-
* @var \Magento\TestFramework\Response
34+
* @var \Magento\Framework\App\ResponseInterface
3535
*/
3636
protected $_response;
3737

@@ -103,7 +103,7 @@ public function dispatch($uri)
103103
/**
104104
* Request getter
105105
*
106-
* @return \Magento\TestFramework\Request
106+
* @return \Magento\Framework\App\RequestInterface
107107
*/
108108
public function getRequest()
109109
{
@@ -116,7 +116,7 @@ public function getRequest()
116116
/**
117117
* Response getter
118118
*
119-
* @return \Magento\TestFramework\Response
119+
* @return \Magento\Framework\App\ResponseInterface
120120
*/
121121
public function getResponse()
122122
{
@@ -269,14 +269,21 @@ protected function getCookieMessages($messageType = null)
269269
{
270270
/** @var $cookieManager CookieManagerInterface */
271271
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);
272+
273+
/** @var $jsonSerializer \Magento\Framework\Serialize\Serializer\Json */
274+
$jsonSerializer = $this->_objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class);
272275
try {
273-
$messages = \Zend_Json::decode(
274-
$cookieManager->getCookie(MessagePlugin::MESSAGES_COOKIES_NAME, \Zend_Json::encode([]))
276+
$messages = $jsonSerializer->unserialize(
277+
$cookieManager->getCookie(
278+
MessagePlugin::MESSAGES_COOKIES_NAME,
279+
$jsonSerializer->serialize([])
280+
)
275281
);
282+
276283
if (!is_array($messages)) {
277284
$messages = [];
278285
}
279-
} catch (\Zend_Json_Exception $e) {
286+
} catch (\InvalidArgumentException $e) {
280287
$messages = [];
281288
}
282289

dev/tests/integration/framework/tests/unit/testsuite/Magento/Test/TestCase/ControllerAbstractTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,25 @@ class ControllerAbstractTest extends \Magento\TestFramework\TestCase\AbstractCon
2525
/** @var \PHPUnit_Framework_MockObject_MockObject | CookieManagerInterface */
2626
private $cookieManagerMock;
2727

28+
/**
29+
* @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Serialize\Serializer\Json
30+
*/
31+
private $serializerMock;
32+
2833
protected function setUp()
2934
{
3035
$testObjectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
3136

3237
$this->messageManager = $this->createMock(\Magento\Framework\Message\Manager::class);
3338
$this->cookieManagerMock = $this->createMock(CookieManagerInterface::class);
39+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$this->serializerMock->expects($this->any())->method('unserialize')->willReturnCallback(
43+
function ($serializedData) {
44+
return json_decode($serializedData, true);
45+
}
46+
);
3447
$this->interpretationStrategyMock = $this->createMock(InterpretationStrategyInterface::class);
3548
$this->interpretationStrategyMock->expects($this->any())
3649
->method('interpret')
@@ -53,6 +66,7 @@ function (MessageInterface $message) {
5366
[\Magento\Framework\App\ResponseInterface::class, $response],
5467
[\Magento\Framework\Message\Manager::class, $this->messageManager],
5568
[CookieManagerInterface::class, $this->cookieManagerMock],
69+
[\Magento\Framework\Serialize\Serializer\Json::class, $this->serializerMock],
5670
[InterpretationStrategyInterface::class, $this->interpretationStrategyMock],
5771
]
5872
)
@@ -234,6 +248,6 @@ private function addSessionMessages()
234248

235249
$this->cookieManagerMock->expects($this->any())
236250
->method('getCookie')
237-
->willReturn(\Zend_Json::encode($cookieMessages));
251+
->willReturn(json_encode($cookieMessages));
238252
}
239253
}

dev/tests/integration/testsuite/Magento/Email/Model/TemplateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function testLoadDefault()
107107
$this->assertNotEmpty($this->model->getTemplateText());
108108
$this->assertNotEmpty($this->model->getTemplateSubject());
109109
$this->assertNotEmpty($this->model->getOrigTemplateVariables());
110-
$this->assertInternalType('array', \Zend_Json::decode($this->model->getOrigTemplateVariables()));
110+
$this->assertInternalType('array', json_decode($this->model->getOrigTemplateVariables(), true));
111111
}
112112

113113
/**

dev/tests/integration/testsuite/Magento/ImportExport/Block/Adminhtml/Import/Edit/BeforeTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ protected function setUp()
9090
public function testGetEntityBehaviors()
9191
{
9292
$actualEntities = $this->_model->getEntityBehaviors();
93-
$expectedEntities = \Zend_Json::encode($this->_expectedEntities);
93+
$expectedEntities = json_encode($this->_expectedEntities);
9494
$this->assertEquals($expectedEntities, $actualEntities);
9595
}
9696

@@ -102,7 +102,7 @@ public function testGetEntityBehaviors()
102102
public function testGetUniqueBehaviors()
103103
{
104104
$actualBehaviors = $this->_model->getUniqueBehaviors();
105-
$expectedBehaviors = \Zend_Json::encode($this->_expectedBehaviors);
105+
$expectedBehaviors = json_encode($this->_expectedBehaviors);
106106
$this->assertEquals($expectedBehaviors, $actualBehaviors);
107107
}
108108
}

lib/internal/Magento/Framework/DataObject.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,21 @@ public function convertToXml(
325325
* Convert object data to JSON
326326
*
327327
* @param array $keys array of required keys
328-
* @return string
328+
* @return bool|string
329+
* @throws \InvalidArgumentException
329330
*/
330331
public function toJson(array $keys = [])
331332
{
332333
$data = $this->toArray($keys);
333-
return \Zend_Json::encode($data);
334+
return \Magento\Framework\Serialize\JsonConverter::convert($data);
334335
}
335336

336337
/**
337338
* The "__" style wrapper for toJson
338339
*
339-
* @param array $keys
340-
* @return string
340+
* @param array $keys
341+
* @return bool|string
342+
* @throws \InvalidArgumentException
341343
*/
342344
public function convertToJson(array $keys = [])
343345
{

0 commit comments

Comments
 (0)