Skip to content

Commit 8138c0a

Browse files
authored
MAGETWO-65354: [GitHub][PR] Remove zend json from email #8707
2 parents ec7a276 + 739310a commit 8138c0a

File tree

7 files changed

+78
-12
lines changed

7 files changed

+78
-12
lines changed

app/code/Magento/Email/Block/Adminhtml/Template/Edit/Form.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,34 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
2121
*/
2222
protected $_variableFactory;
2323

24+
/**
25+
* @var \Magento\Framework\Serialize\Serializer\Json
26+
*/
27+
private $serializer;
28+
2429
/**
2530
* @param \Magento\Backend\Block\Template\Context $context
2631
* @param \Magento\Framework\Registry $registry
2732
* @param \Magento\Framework\Data\FormFactory $formFactory
2833
* @param \Magento\Variable\Model\VariableFactory $variableFactory
2934
* @param \Magento\Email\Model\Source\Variables $variables
3035
* @param array $data
36+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
37+
* @throws \RuntimeException
3138
*/
3239
public function __construct(
3340
\Magento\Backend\Block\Template\Context $context,
3441
\Magento\Framework\Registry $registry,
3542
\Magento\Framework\Data\FormFactory $formFactory,
3643
\Magento\Variable\Model\VariableFactory $variableFactory,
3744
\Magento\Email\Model\Source\Variables $variables,
38-
array $data = []
45+
array $data = [],
46+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
3947
) {
4048
$this->_variableFactory = $variableFactory;
4149
$this->_variables = $variables;
50+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
51+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
4252
parent::__construct($context, $registry, $formFactory, $data);
4353
}
4454

@@ -60,6 +70,7 @@ protected function _prepareLayout()
6070
* @return \Magento\Backend\Block\Widget\Form
6171
* @SuppressWarnings(PHPMD.NPathComplexity)
6272
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
73+
* @throws \Magento\Framework\Exception\LocalizedException
6374
*/
6475
protected function _prepareForm()
6576
{
@@ -100,7 +111,7 @@ protected function _prepareForm()
100111
$fieldset->addField(
101112
'variables',
102113
'hidden',
103-
['name' => 'variables', 'value' => \Zend_Json::encode($this->getVariables())]
114+
['name' => 'variables', 'value' => $this->serializer->serialize($this->getVariables())]
104115
);
105116
$fieldset->addField('template_variables', 'hidden', ['name' => 'template_variables']);
106117

app/code/Magento/Email/Controller/Adminhtml/Email/Template/DefaultTemplate.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,35 @@ class DefaultTemplate extends \Magento\Email\Controller\Adminhtml\Email\Template
1313
*/
1414
private $emailConfig;
1515

16+
/**
17+
* @var \Magento\Framework\Serialize\Serializer\Json
18+
*/
19+
private $serializer;
20+
1621
/**
1722
* @param \Magento\Backend\App\Action\Context $context
1823
* @param \Magento\Framework\Registry $coreRegistry
1924
* @param \Magento\Email\Model\Template\Config $emailConfig
25+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
26+
* @throws \RuntimeException
2027
*/
2128
public function __construct(
2229
\Magento\Backend\App\Action\Context $context,
2330
\Magento\Framework\Registry $coreRegistry,
24-
\Magento\Email\Model\Template\Config $emailConfig
31+
\Magento\Email\Model\Template\Config $emailConfig,
32+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
2533
) {
2634
$this->emailConfig = $emailConfig;
35+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
36+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
2737
parent::__construct($context, $coreRegistry);
2838
}
2939

3040
/**
3141
* Set template data to retrieve it in template info form
3242
*
3343
* @return void
44+
* @throws \RuntimeException
3445
*/
3546
public function execute()
3647
{
@@ -49,15 +60,18 @@ public function execute()
4960

5061
$template->loadDefault($templateId);
5162
$template->setData('orig_template_code', $templateId);
52-
$template->setData('template_variables', \Zend_Json::encode($template->getVariablesOptionArray(true)));
63+
$template->setData(
64+
'template_variables',
65+
$this->serializer->serialize($template->getVariablesOptionArray(true))
66+
);
5367

5468
$templateBlock = $this->_view->getLayout()->createBlock(
5569
\Magento\Email\Block\Adminhtml\Template\Edit::class
5670
);
5771
$template->setData('orig_template_currently_used_for', $templateBlock->getCurrentlyUsedForPaths(false));
5872

5973
$this->getResponse()->representJson(
60-
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($template->getData())
74+
$this->serializer->serialize($template->getData())
6175
);
6276
} catch (\Exception $e) {
6377
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);

app/code/Magento/Email/Model/BackendTemplate.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class BackendTemplate extends Template
3535
* @param \Magento\Email\Model\Template\FilterFactory $filterFactory
3636
* @param \Magento\Config\Model\Config\Structure $structure
3737
* @param array $data
38+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
3839
*
3940
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
4041
*/
@@ -53,7 +54,8 @@ public function __construct(
5354
\Magento\Framework\UrlInterface $urlModel,
5455
\Magento\Email\Model\Template\FilterFactory $filterFactory,
5556
\Magento\Config\Model\Config\Structure $structure,
56-
array $data = []
57+
array $data = [],
58+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
5759
) {
5860
$this->structure = $structure;
5961
parent::__construct(
@@ -70,7 +72,8 @@ public function __construct(
7072
$filterManager,
7173
$urlModel,
7274
$filterFactory,
73-
$data
75+
$data,
76+
$serializer
7477
);
7578
}
7679

app/code/Magento/Email/Model/Template.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
9292
private $filterFactory;
9393

9494
/**
95-
* Initialize dependencies.
95+
* @var \Magento\Framework\Serialize\Serializer\Json
96+
*/
97+
private $serializer;
98+
99+
/**
100+
* Template constructor.
96101
*
97102
* @param \Magento\Framework\Model\Context $context
98103
* @param \Magento\Framework\View\DesignInterface $design
@@ -108,6 +113,8 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
108113
* @param \Magento\Framework\UrlInterface $urlModel
109114
* @param Template\FilterFactory $filterFactory
110115
* @param array $data
116+
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
117+
* @throws \RuntimeException
111118
*
112119
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
113120
*/
@@ -125,9 +132,12 @@ public function __construct(
125132
\Magento\Framework\Filter\FilterManager $filterManager,
126133
\Magento\Framework\UrlInterface $urlModel,
127134
\Magento\Email\Model\Template\FilterFactory $filterFactory,
128-
array $data = []
135+
array $data = [],
136+
\Magento\Framework\Serialize\Serializer\Json $serializer = null
129137
) {
130138
$this->filterFactory = $filterFactory;
139+
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
140+
->get(\Magento\Framework\Serialize\Serializer\Json::class);
131141
parent::__construct(
132142
$context,
133143
$design,
@@ -289,7 +299,7 @@ protected function _parseVariablesString($variablesString)
289299
$variables = [];
290300
if ($variablesString && is_string($variablesString)) {
291301
$variablesString = str_replace("\n", '', $variablesString);
292-
$variables = \Zend_Json::decode($variablesString);
302+
$variables = $this->serializer->unserialize($variablesString);
293303
}
294304
return $variables;
295305
}

app/code/Magento/Email/Test/Unit/Block/Adminhtml/Template/EditTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ protected function setUp()
5656
$menuMock = $this->getMock(
5757
\Magento\Backend\Model\Menu::class,
5858
[],
59-
[$this->getMock(\Psr\Log\LoggerInterface::class)]
59+
[$this->getMock(\Psr\Log\LoggerInterface::class)],
60+
'',
61+
false,
62+
false
6063
);
6164
$menuItemMock = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false, false);
6265
$urlBuilder = $this->getMock(\Magento\Backend\Model\Url::class, [], [], '', false, false);

app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class BackendTemplateTest extends \PHPUnit_Framework_TestCase
4343
*/
4444
protected $objectManagerBackup;
4545

46+
/**
47+
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
48+
*/
49+
private $serializerMock;
50+
4651
protected function setUp()
4752
{
4853
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -70,9 +75,15 @@ protected function setUp()
7075

7176
\Magento\Framework\App\ObjectManager::setInstance($objectManagerMock);
7277

78+
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
79+
7380
$this->model = $helper->getObject(
7481
\Magento\Email\Model\BackendTemplate::class,
75-
['scopeConfig' => $this->scopeConfigMock, 'structure' => $this->structureMock]
82+
[
83+
'scopeConfig' => $this->scopeConfigMock,
84+
'structure' => $this->structureMock,
85+
'serializer' => $this->serializerMock
86+
]
7687
);
7788
}
7889

app/code/Magento/Email/Test/Unit/Model/TemplateTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
8484
*/
8585
private $templateFactory;
8686

87+
/**
88+
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
89+
*/
90+
private $serilizerMock;
91+
8792
protected function setUp()
8893
{
8994
$this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
@@ -138,6 +143,8 @@ protected function setUp()
138143
->setMethods(['create'])
139144
->disableOriginalConstructor()
140145
->getMock();
146+
147+
$this->serilizerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
141148
}
142149

143150
/**
@@ -164,6 +171,8 @@ protected function getModelMock(array $mockedMethods = [])
164171
$this->filterManager,
165172
$this->urlModel,
166173
$this->filterFactory,
174+
[],
175+
$this->serilizerMock
167176
])
168177
->getMock();
169178
}
@@ -532,6 +541,11 @@ public function testGetVariablesOptionArray($withGroup, $templateVariables, $exp
532541
{
533542
$model = $this->getModelMock();
534543
$model->setData('orig_template_variables', $templateVariables);
544+
545+
$this->serilizerMock->expects($this->any())->method('unserialize')
546+
->willReturn(
547+
json_decode($templateVariables, true)
548+
);
535549
$this->assertEquals($expectedResult, $model->getVariablesOptionArray($withGroup));
536550
}
537551

0 commit comments

Comments
 (0)