AuthenticationPopupTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Block\Account;
  7. use Magento\Customer\Block\Account\AuthenticationPopup;
  8. use Magento\Customer\Model\Form;
  9. use Magento\Framework\App\Config\ScopeConfigInterface;
  10. use Magento\Framework\UrlInterface;
  11. use Magento\Framework\View\Element\Template\Context;
  12. use Magento\Store\Api\Data\StoreInterface;
  13. use Magento\Store\Model\ScopeInterface;
  14. use Magento\Store\Model\StoreManagerInterface;
  15. class AuthenticationPopupTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /** @var \Magento\Customer\Block\Account\AuthenticationPopup */
  18. private $model;
  19. /** @var \Magento\Framework\View\Element\Template\Context|\PHPUnit_Framework_MockObject_MockObject */
  20. private $contextMock;
  21. /** @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. private $storeManagerMock;
  23. /** @var ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. private $scopeConfigMock;
  25. /** @var UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
  26. private $urlBuilderMock;
  27. /** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
  28. private $serializerMock;
  29. protected function setUp()
  30. {
  31. $this->contextMock = $this->getMockBuilder(Context::class)
  32. ->disableOriginalConstructor()
  33. ->getMock();
  34. $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
  35. ->getMock();
  36. $this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
  37. ->getMock();
  38. $this->urlBuilderMock = $this->getMockBuilder(UrlInterface::class)
  39. ->getMock();
  40. $this->contextMock->expects($this->once())
  41. ->method('getStoreManager')
  42. ->willReturn($this->storeManagerMock);
  43. $this->contextMock->expects($this->once())
  44. ->method('getScopeConfig')
  45. ->willReturn($this->scopeConfigMock);
  46. $this->contextMock->expects($this->once())
  47. ->method('getUrlBuilder')
  48. ->willReturn($this->urlBuilderMock);
  49. $escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. $escaperMock->method('escapeHtml')
  53. ->willReturnCallback(
  54. function ($string) {
  55. return 'escapeHtml' . $string;
  56. }
  57. );
  58. $escaperMock->method('escapeUrl')
  59. ->willReturnCallback(
  60. function ($string) {
  61. return 'escapeUrl' . $string;
  62. }
  63. );
  64. $this->contextMock->expects($this->once())
  65. ->method('getEscaper')
  66. ->willReturn($escaperMock);
  67. $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
  68. ->getMock();
  69. $this->model = new AuthenticationPopup(
  70. $this->contextMock,
  71. [],
  72. $this->serializerMock
  73. );
  74. }
  75. /**
  76. * @param mixed $isAutocomplete
  77. * @param string $baseUrl
  78. * @param string $registerUrl
  79. * @param string $forgotUrl
  80. * @param array $result
  81. * @throws \PHPUnit\Framework\Exception
  82. *
  83. * @dataProvider dataProviderGetConfig
  84. */
  85. public function testGetConfig($isAutocomplete, $baseUrl, $registerUrl, $forgotUrl, array $result)
  86. {
  87. $this->scopeConfigMock->expects($this->any())
  88. ->method('getValue')
  89. ->with(Form::XML_PATH_ENABLE_AUTOCOMPLETE, ScopeInterface::SCOPE_STORE, null)
  90. ->willReturn($isAutocomplete);
  91. /** @var StoreInterface||\PHPUnit_Framework_MockObject_MockObject $storeMock */
  92. $storeMock = $this->getMockBuilder(StoreInterface::class)
  93. ->setMethods(['getBaseUrl'])
  94. ->getMockForAbstractClass();
  95. $this->storeManagerMock->expects($this->any())
  96. ->method('getStore')
  97. ->with(null)
  98. ->willReturn($storeMock);
  99. $storeMock->expects($this->any())
  100. ->method('getBaseUrl')
  101. ->willReturn($baseUrl);
  102. $this->urlBuilderMock->expects($this->any())
  103. ->method('getUrl')
  104. ->willReturnMap(
  105. [
  106. ['customer/account/create', [], $registerUrl],
  107. ['customer/account/forgotpassword', [], $forgotUrl],
  108. ]
  109. );
  110. $this->assertEquals($result, $this->model->getConfig());
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function dataProviderGetConfig()
  116. {
  117. return [
  118. [
  119. 0,
  120. 'base',
  121. 'reg',
  122. 'forgot',
  123. [
  124. 'autocomplete' => 'escapeHtmloff',
  125. 'customerRegisterUrl' => 'escapeUrlreg',
  126. 'customerForgotPasswordUrl' => 'escapeUrlforgot',
  127. 'baseUrl' => 'escapeUrlbase',
  128. ],
  129. ],
  130. [
  131. 1,
  132. '',
  133. 'reg',
  134. 'forgot',
  135. [
  136. 'autocomplete' => 'escapeHtmlon',
  137. 'customerRegisterUrl' => 'escapeUrlreg',
  138. 'customerForgotPasswordUrl' => 'escapeUrlforgot',
  139. 'baseUrl' => 'escapeUrl',
  140. ],
  141. ],
  142. [
  143. '',
  144. 'base',
  145. '',
  146. 'forgot',
  147. [
  148. 'autocomplete' => 'escapeHtmloff',
  149. 'customerRegisterUrl' => 'escapeUrl',
  150. 'customerForgotPasswordUrl' => 'escapeUrlforgot',
  151. 'baseUrl' => 'escapeUrlbase',
  152. ],
  153. ],
  154. [
  155. true,
  156. 'base',
  157. 'reg',
  158. '',
  159. [
  160. 'autocomplete' => 'escapeHtmlon',
  161. 'customerRegisterUrl' => 'escapeUrlreg',
  162. 'customerForgotPasswordUrl' => 'escapeUrl',
  163. 'baseUrl' => 'escapeUrlbase',
  164. ],
  165. ],
  166. ];
  167. }
  168. /**
  169. * @param mixed $isAutocomplete
  170. * @param string $baseUrl
  171. * @param string $registerUrl
  172. * @param string $forgotUrl
  173. * @param array $result
  174. * @throws \PHPUnit\Framework\Exception
  175. *
  176. * @dataProvider dataProviderGetConfig
  177. */
  178. public function testGetSerializedConfig($isAutocomplete, $baseUrl, $registerUrl, $forgotUrl, array $result)
  179. {
  180. $this->scopeConfigMock->expects($this->any())
  181. ->method('getValue')
  182. ->with(Form::XML_PATH_ENABLE_AUTOCOMPLETE, ScopeInterface::SCOPE_STORE, null)
  183. ->willReturn($isAutocomplete);
  184. /** @var StoreInterface||\PHPUnit_Framework_MockObject_MockObject $storeMock */
  185. $storeMock = $this->getMockBuilder(StoreInterface::class)
  186. ->setMethods(['getBaseUrl'])
  187. ->getMockForAbstractClass();
  188. $this->storeManagerMock->expects($this->any())
  189. ->method('getStore')
  190. ->with(null)
  191. ->willReturn($storeMock);
  192. $storeMock->expects($this->any())
  193. ->method('getBaseUrl')
  194. ->willReturn($baseUrl);
  195. $this->urlBuilderMock->expects($this->any())
  196. ->method('getUrl')
  197. ->willReturnMap(
  198. [
  199. ['customer/account/create', [], $registerUrl],
  200. ['customer/account/forgotpassword', [], $forgotUrl],
  201. ]
  202. );
  203. $this->serializerMock->expects($this->any())->method('serialize')
  204. ->willReturn(
  205. json_encode($this->model->getConfig())
  206. );
  207. $this->assertEquals(json_encode($result), $this->model->getSerializedConfig());
  208. }
  209. }