Skip to content
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

Remove zend json from customer data #10259

Merged
Merged
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
46 changes: 42 additions & 4 deletions app/code/Magento/Customer/Block/CustomerScopeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,28 @@ class CustomerScopeData extends \Magento\Framework\View\Element\Template
private $storeManager;

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

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Framework\Json\EncoderInterface $jsonEncoder,
array $data = []
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
parent::__construct($context, $data);
$this->storeManager = $context->getStoreManager();
$this->jsonEncoder = $jsonEncoder;
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand All @@ -50,4 +55,37 @@ public function getWebsiteId()
{
return (int)$this->_storeManager->getStore()->getWebsiteId();
}

/**
* @return array
*/
public function getInvalidationRules()
{
return [
'*' => [
'Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => $this->getWebsiteId(),
]
]
]
]
]
],
];
}

/**
* Get the invalidation rules json encoded
*
* @return bool|string
* @throws \InvalidArgumentException
*/
public function getSerializedInvalidationRules()
{
return $this->serializer->serialize($this->getInvalidationRules());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still... What are the advantages compared to echo json_encode($block->getInvalidationRules(); in template? As to me encoding is purely responsibility of template.

Another thing which just came to my mind: do we have a recommendation to leave JS component declaration in templates? Thus they may be overridden more easily, without touching PHP classes. At least I was not able to find similar occurrence of /js/ or '*' in *.php.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the leaving of the js declaration in the templates. Though to me this feels like config and to have to change a full template to change the config feels odd.

With regards to the json_encode in the template I dont agree here. I feel that we should either be using json_encode everywhere or wrap it's usage across the system, which would include templates. This way if a mass change is needed it is easier to manage and roll out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have no idea which kind of mass change could it be :D

Ok, I see your point, you want to encapsulate serializer logic in block class and don't make template aware of it (while it does escapeHtml and other similar things). There is nothing to discuss here, let's wait for the feedback from Oleksii and then maybe put a common recommendation for such situation somewhere to keep the code consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah something like escapeHtml would be a nice option here also thought about that. It's above this PR but I think some "common" way to do this through templates would be amazing.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Block\CustomerScopeData;
use Magento\Framework\Json\EncoderInterface;

class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -29,6 +28,9 @@ class CustomerScopeDataTest extends \PHPUnit_Framework_TestCase
/** @var \Magento\Framework\Json\EncoderInterface|\PHPUnit_Framework_MockObject_MockObject */
private $encoderMock;

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

protected function setUp()
{
$this->contextMock = $this->getMockBuilder(Context::class)
Expand All @@ -41,7 +43,10 @@ protected function setUp()
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->getMock();

$this->encoderMock = $this->getMockBuilder(EncoderInterface::class)
$this->encoderMock = $this->getMockBuilder(\Magento\Framework\Json\EncoderInterface::class)
->getMock();

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

$this->contextMock->expects($this->exactly(2))
Expand All @@ -55,7 +60,8 @@ protected function setUp()
$this->model = new CustomerScopeData(
$this->contextMock,
$this->encoderMock,
[]
[],
$this->serializerMock
);
}

Expand All @@ -78,4 +84,84 @@ public function testGetWebsiteId()

$this->assertEquals($storeId, $this->model->getWebsiteId());
}

public function testGetInvalidationRules()
{
$storeId = 1;

$storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['getWebsiteId'])
->getMockForAbstractClass();

$storeMock->expects($this->any())
->method('getWebsiteId')
->willReturn($storeId);

$this->storeManagerMock->expects($this->any())
->method('getStore')
->with(null)
->willReturn($storeMock);

$this->assertEquals(
[
'*' => [
'Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => 1,
]
]
]
]
]
],
],
$this->model->getInvalidationRules()
);
}

public function testGetSerializedInvalidationRules()
{
$storeId = 1;
$rules = [
'*' => [
'Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => 1,
]
]
]
]
]
],
];

$storeMock = $this->getMockBuilder(StoreInterface::class)
->setMethods(['getWebsiteId'])
->getMockForAbstractClass();

$storeMock->expects($this->any())
->method('getWebsiteId')
->willReturn($storeId);

$this->storeManagerMock->expects($this->any())
->method('getStore')
->with(null)
->willReturn($storeMock);

$this->serializerMock->expects($this->any())
->method('serialize')
->with($rules)
->willReturn(json_encode($rules));

$this->assertEquals(
json_encode($rules),
$this->model->getSerializedInvalidationRules()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
<script type="text/x-magento-init">
<?php
/* @noEscape */
echo \Zend_Json::encode([
'*' => ['Magento_Customer/js/invalidation-processor' => [
'invalidationRules' => [
'website-rule' => [
'Magento_Customer/js/invalidation-rules/website-rule' => [
'scopeConfig' => [
'websiteId' => $block->getWebsiteId(),
]
]
]
]
]],
]);
?>
echo $block->getSerializedInvalidationRules();
?>
</script>