Skip to content

Commit

Permalink
MAGETWO-69452: Remove zend json from form elements #9754
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleksii Korshenko authored Jun 29, 2017
2 parents 5c516ad + d86bcde commit e5508b4
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,36 @@
*/
namespace Magento\Framework\Data\Form\Element;

use Magento\Framework\Escaper;

class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multiselect
{
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* Editablemultiselect constructor.
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
* Name of the default JavaScript class that is used to make multiselect editable
*
Expand All @@ -26,6 +54,7 @@ class Editablemultiselect extends \Magento\Framework\Data\Form\Element\Multisele
* Retrieve HTML markup of the element
*
* @return string
* @throws \InvalidArgumentException
*/
public function getElementHtml()
{
Expand All @@ -41,7 +70,7 @@ public function getElementHtml()
$elementJsClass = $this->getData('element_js_class');
}

$selectConfigJson = \Zend_Json::encode($selectConfig);
$selectConfigJson = $this->serializer->serialize($selectConfig);
$jsObjectName = $this->getJsObjectName();

// TODO: TaxRateEditableMultiselect should be moved to a static .js module.
Expand Down
36 changes: 30 additions & 6 deletions lib/internal/Magento/Framework/Data/Form/Element/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@
class Editor extends Textarea
{
/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* Editor constructor.
* @param Factory $factoryElement
* @param CollectionFactory $factoryCollection
* @param Escaper $escaper
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
Factory $factoryElement,
CollectionFactory $factoryCollection,
Escaper $escaper,
$data = []
$data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($factoryElement, $factoryCollection, $escaper, $data);

Expand All @@ -36,6 +45,8 @@ public function __construct(
$this->setType('textarea');
$this->setExtType('textarea');
}
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand All @@ -52,6 +63,21 @@ protected function getButtonTranslations()
return $buttonTranslations;
}

/**
* @return bool|string
* @throws \InvalidArgumentException
*/
private function getJsonConfig()
{
if (is_object($this->getConfig()) && method_exists($this->getConfig(), 'toJson')) {
return $this->getConfig()->toJson();
} else {
return $this->serializer->serialize(
$this->getConfig()
);
}
}

/**
* @return string
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
Expand Down Expand Up @@ -132,7 +158,7 @@ public function getElementHtml()
], function(jQuery){' .
"\n" .
' (function($) {$.mage.translate.add(' .
\Zend_Json::encode(
$this->serializer->serialize(
$this->getButtonTranslations()
) .
')})(jQuery);' .
Expand All @@ -141,9 +167,7 @@ public function getElementHtml()
' = new tinyMceWysiwygSetup("' .
$this->getHtmlId() .
'", ' .
\Zend_Json::encode(
$this->getConfig()
) .
$this->getJsonConfig() .
');' .
$forceLoad .
'
Expand Down Expand Up @@ -180,7 +204,7 @@ public function getElementHtml()
//<![CDATA[
require(["jquery", "mage/translate", "mage/adminhtml/wysiwyg/widget"], function(jQuery){
(function($) {
$.mage.translate.add(' . \Zend_Json::encode($this->getButtonTranslations()) . ')
$.mage.translate.add(' . $this->serializer->serialize($this->getButtonTranslations()) . ')
})(jQuery);
});
//]]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class EditorTest extends \PHPUnit_Framework_TestCase
*/
protected $objectManager;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

protected function setUp()
{
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
Expand All @@ -62,13 +67,16 @@ protected function setUp()
$this->escaperMock = $this->getMock(\Magento\Framework\Escaper::class, [], [], '', false);
$this->configMock = $this->getMock(\Magento\Framework\DataObject::class, ['getData'], [], '', false);

$this->serializer = $this->getMock(\Magento\Framework\Serialize\Serializer\Json::class, [], [], '', false);

$this->model = $this->objectManager->getObject(
\Magento\Framework\Data\Form\Element\Editor::class,
[
'factoryElement' => $this->factoryMock,
'factoryCollection' => $this->collectionFactoryMock,
'escaper' => $this->escaperMock,
'data' => ['config' => $this->configMock]
'data' => ['config' => $this->configMock],
'serializer' => $this->serializer
]
);

Expand Down Expand Up @@ -203,7 +211,14 @@ public function testGetConfig()
public function testGetTranslatedString()
{
$this->configMock->expects($this->any())->method('getData')->withConsecutive(['enabled'])->willReturn(true);
$this->serializer->expects($this->any())
->method('serialize')
->willReturnCallback(function ($params) {
return json_encode($params);
}
);
$html = $this->model->getElementHtml();

$this->assertRegExp('/.*"Insert Image...":"Insert Image...".*/i', $html);
}
}

0 comments on commit e5508b4

Please sign in to comment.