diff --git a/app/code/Magento/Customer/Block/Account/AuthenticationPopup.php b/app/code/Magento/Customer/Block/Account/AuthenticationPopup.php index a0f94e1b8f733..eefec4c35679c 100644 --- a/app/code/Magento/Customer/Block/Account/AuthenticationPopup.php +++ b/app/code/Magento/Customer/Block/Account/AuthenticationPopup.php @@ -15,16 +15,26 @@ class AuthenticationPopup extends \Magento\Framework\View\Element\Template */ protected $jsLayout; + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + private $serializer; + /** * @param \Magento\Framework\View\Element\Template\Context $context * @param array $data + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException */ public function __construct( \Magento\Framework\View\Element\Template\Context $context, - array $data = [] + array $data = [], + \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { parent::__construct($context, $data); $this->jsLayout = isset($data['jsLayout']) && is_array($data['jsLayout']) ? $data['jsLayout'] : []; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); } /** @@ -32,7 +42,7 @@ public function __construct( */ public function getJsLayout() { - return \Zend_Json::encode($this->jsLayout); + return $this->serializer->serialize($this->jsLayout); } /** @@ -50,6 +60,18 @@ public function getConfig() ]; } + /** + * Returns popup config in JSON format. + * + * Added in scope of https://github.com/magento/magento2/pull/8617 + * + * @return bool|string + */ + public function getSerializedConfig() + { + return $this->serializer->serialize($this->getConfig()); + } + /** * Is autocomplete enabled for storefront * diff --git a/app/code/Magento/Customer/Test/Unit/Block/Account/AuthenticationPopupTest.php b/app/code/Magento/Customer/Test/Unit/Block/Account/AuthenticationPopupTest.php index 67a3fb8f3a576..40f770ab9fbd4 100644 --- a/app/code/Magento/Customer/Test/Unit/Block/Account/AuthenticationPopupTest.php +++ b/app/code/Magento/Customer/Test/Unit/Block/Account/AuthenticationPopupTest.php @@ -31,6 +31,9 @@ class AuthenticationPopupTest extends \PHPUnit_Framework_TestCase /** @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */ private $urlBuilderMock; + /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */ + private $serializerMock; + protected function setUp() { $this->contextMock = $this->getMockBuilder(Context::class) @@ -72,8 +75,13 @@ function ($string) { ->method('getEscaper') ->willReturn($escaperMock); + $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) + ->getMock(); + $this->model = new AuthenticationPopup( - $this->contextMock + $this->contextMock, + [], + $this->serializerMock ); } @@ -83,6 +91,7 @@ function ($string) { * @param string $registerUrl * @param string $forgotUrl * @param array $result + * @throws \PHPUnit_Framework_Exception * * @dataProvider dataProviderGetConfig */ @@ -172,4 +181,51 @@ public function dataProviderGetConfig() ], ]; } + + /** + * @param mixed $isAutocomplete + * @param string $baseUrl + * @param string $registerUrl + * @param string $forgotUrl + * @param array $result + * @throws \PHPUnit_Framework_Exception + * + * @dataProvider dataProviderGetConfig + */ + public function testGetSerializedConfig($isAutocomplete, $baseUrl, $registerUrl, $forgotUrl, array $result) + { + $this->scopeConfigMock->expects($this->any()) + ->method('getValue') + ->with(Form::XML_PATH_ENABLE_AUTOCOMPLETE, ScopeInterface::SCOPE_STORE, null) + ->willReturn($isAutocomplete); + + /** @var StoreInterface||\PHPUnit_Framework_MockObject_MockObject $storeMock */ + $storeMock = $this->getMockBuilder(StoreInterface::class) + ->setMethods(['getBaseUrl']) + ->getMockForAbstractClass(); + + $this->storeManagerMock->expects($this->any()) + ->method('getStore') + ->with(null) + ->willReturn($storeMock); + + $storeMock->expects($this->any()) + ->method('getBaseUrl') + ->willReturn($baseUrl); + + $this->urlBuilderMock->expects($this->any()) + ->method('getUrl') + ->willReturnMap( + [ + ['customer/account/create', [], $registerUrl], + ['customer/account/forgotpassword', [], $forgotUrl], + ] + ); + $this->serializerMock->expects($this->any())->method('serialize') + ->willReturn( + json_encode($this->model->getConfig()) + ); + + $this->assertEquals(json_encode($result), $this->model->getSerializedConfig()); + } } diff --git a/app/code/Magento/Customer/view/frontend/templates/account/authentication-popup.phtml b/app/code/Magento/Customer/view/frontend/templates/account/authentication-popup.phtml index bcf0cc92bc912..20ea488931398 100644 --- a/app/code/Magento/Customer/view/frontend/templates/account/authentication-popup.phtml +++ b/app/code/Magento/Customer/view/frontend/templates/account/authentication-popup.phtml @@ -10,7 +10,7 @@ ?>