Skip to content

Remove zend json from email #8707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/code/Magento/Email/Block/Adminhtml/Template/Edit/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,34 @@ class Form extends \Magento\Backend\Block\Widget\Form\Generic
*/
protected $_variableFactory;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @param \Magento\Backend\Block\Template\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Data\FormFactory $formFactory
* @param \Magento\Variable\Model\VariableFactory $variableFactory
* @param \Magento\Email\Model\Source\Variables $variables
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Backend\Block\Template\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Data\FormFactory $formFactory,
\Magento\Variable\Model\VariableFactory $variableFactory,
\Magento\Email\Model\Source\Variables $variables,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->_variableFactory = $variableFactory;
$this->_variables = $variables;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct($context, $registry, $formFactory, $data);
}

Expand All @@ -60,6 +70,7 @@ protected function _prepareLayout()
* @return \Magento\Backend\Block\Widget\Form
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _prepareForm()
{
Expand Down Expand Up @@ -100,7 +111,7 @@ protected function _prepareForm()
$fieldset->addField(
'variables',
'hidden',
['name' => 'variables', 'value' => \Zend_Json::encode($this->getVariables())]
['name' => 'variables', 'value' => $this->serializer->serialize($this->getVariables())]
);
$fieldset->addField('template_variables', 'hidden', ['name' => 'template_variables']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,35 @@ class DefaultTemplate extends \Magento\Email\Controller\Adminhtml\Email\Template
*/
private $emailConfig;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\Registry $coreRegistry
* @param \Magento\Email\Model\Template\Config $emailConfig
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\Registry $coreRegistry,
\Magento\Email\Model\Template\Config $emailConfig
\Magento\Email\Model\Template\Config $emailConfig,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->emailConfig = $emailConfig;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct($context, $coreRegistry);
}

/**
* Set template data to retrieve it in template info form
*
* @return void
* @throws \RuntimeException
*/
public function execute()
{
Expand All @@ -49,15 +60,18 @@ public function execute()

$template->loadDefault($templateId);
$template->setData('orig_template_code', $templateId);
$template->setData('template_variables', \Zend_Json::encode($template->getVariablesOptionArray(true)));
$template->setData(
'template_variables',
$this->serializer->serialize($template->getVariablesOptionArray(true))
);

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

$this->getResponse()->representJson(
$this->_objectManager->get(\Magento\Framework\Json\Helper\Data::class)->jsonEncode($template->getData())
$this->serializer->serialize($template->getData())
);
} catch (\Exception $e) {
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
Expand Down
6 changes: 4 additions & 2 deletions app/code/Magento/Email/Model/BackendTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public function __construct(
\Magento\Framework\UrlInterface $urlModel,
\Magento\Email\Model\Template\FilterFactory $filterFactory,
\Magento\Config\Model\Config\Structure $structure,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->structure = $structure;
parent::__construct(
Expand All @@ -70,7 +71,8 @@ public function __construct(
$filterManager,
$urlModel,
$filterFactory,
$data
$data,
$serializer
);
}

Expand Down
16 changes: 13 additions & 3 deletions app/code/Magento/Email/Model/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
private $filterFactory;

/**
* Initialize dependencies.
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* Template constructor.
*
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\View\DesignInterface $design
Expand All @@ -108,6 +113,8 @@ class Template extends AbstractTemplate implements \Magento\Framework\Mail\Templ
* @param \Magento\Framework\UrlInterface $urlModel
* @param Template\FilterFactory $filterFactory
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand All @@ -125,9 +132,12 @@ public function __construct(
\Magento\Framework\Filter\FilterManager $filterManager,
\Magento\Framework\UrlInterface $urlModel,
\Magento\Email\Model\Template\FilterFactory $filterFactory,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->filterFactory = $filterFactory;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
parent::__construct(
$context,
$design,
Expand Down Expand Up @@ -289,7 +299,7 @@ protected function _parseVariablesString($variablesString)
$variables = [];
if ($variablesString && is_string($variablesString)) {
$variablesString = str_replace("\n", '', $variablesString);
$variables = \Zend_Json::decode($variablesString);
$variables = $this->serializer->unserialize($variablesString);
}
return $variables;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ protected function setUp()
$menuMock = $this->getMock(
\Magento\Backend\Model\Menu::class,
[],
[$this->getMock(\Psr\Log\LoggerInterface::class)]
[$this->getMock(\Psr\Log\LoggerInterface::class)],
'',
false,
false
);
$menuItemMock = $this->getMock(\Magento\Backend\Model\Menu\Item::class, [], [], '', false, false);
$urlBuilder = $this->getMock(\Magento\Backend\Model\Url::class, [], [], '', false, false);
Expand Down
13 changes: 12 additions & 1 deletion app/code/Magento/Email/Test/Unit/Model/BackendTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class BackendTemplateTest extends \PHPUnit_Framework_TestCase
*/
protected $objectManagerBackup;

/**
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializerMock;

protected function setUp()
{
$helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand Down Expand Up @@ -70,9 +75,15 @@ protected function setUp()

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

$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();

$this->model = $helper->getObject(
\Magento\Email\Model\BackendTemplate::class,
['scopeConfig' => $this->scopeConfigMock, 'structure' => $this->structureMock]
[
'scopeConfig' => $this->scopeConfigMock,
'structure' => $this->structureMock,
'serializer' => $this->serializerMock
]
);
}

Expand Down
14 changes: 14 additions & 0 deletions app/code/Magento/Email/Test/Unit/Model/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ class TemplateTest extends \PHPUnit_Framework_TestCase
*/
private $templateFactory;

/**
* @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject
*/
private $serializerMock;

protected function setUp()
{
$this->context = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
Expand Down Expand Up @@ -138,6 +143,8 @@ protected function setUp()
->setMethods(['create'])
->disableOriginalConstructor()
->getMock();

$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)->getMock();
}

/**
Expand All @@ -164,6 +171,8 @@ protected function getModelMock(array $mockedMethods = [])
$this->filterManager,
$this->urlModel,
$this->filterFactory,
[],
$this->serializerMock
])
->getMock();
}
Expand Down Expand Up @@ -532,6 +541,11 @@ public function testGetVariablesOptionArray($withGroup, $templateVariables, $exp
{
$model = $this->getModelMock();
$model->setData('orig_template_variables', $templateVariables);

$this->serializerMock->expects($this->any())->method('unserialize')
->willReturn(
json_decode($templateVariables, true)
);
$this->assertEquals($expectedResult, $model->getVariablesOptionArray($withGroup));
}

Expand Down