diff --git a/app/code/Magento/Customer/Block/CustomerScopeData.php b/app/code/Magento/Customer/Block/CustomerScopeData.php index 244437e870b98..6ba08c6f59b75 100644 --- a/app/code/Magento/Customer/Block/CustomerScopeData.php +++ b/app/code/Magento/Customer/Block/CustomerScopeData.php @@ -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); } /** @@ -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()); + } } diff --git a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php index dcbe4882231ca..1f6976a7f3338 100644 --- a/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php +++ b/app/code/Magento/Customer/Test/Unit/Block/CustomerScopeDataTest.php @@ -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 { @@ -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) @@ -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)) @@ -55,7 +60,8 @@ protected function setUp() $this->model = new CustomerScopeData( $this->contextMock, $this->encoderMock, - [] + [], + $this->serializerMock ); } @@ -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() + ); + } } diff --git a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml index 7905b1d9925e3..ad8aab76e483f 100644 --- a/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/js/customer-data/invalidation-rules.phtml @@ -12,18 +12,6 @@