ConfirmTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Account;
  7. use Magento\Customer\Controller\Account\Confirm;
  8. use Magento\Customer\Helper\Address;
  9. use Magento\Customer\Model\Url;
  10. use Magento\Store\Model\ScopeInterface;
  11. /**
  12. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. */
  15. class ConfirmTest extends \PHPUnit\Framework\TestCase
  16. {
  17. /**
  18. * @var Confirm
  19. */
  20. protected $model;
  21. /**
  22. * @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
  23. */
  24. protected $requestMock;
  25. /**
  26. * @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
  27. */
  28. protected $responseMock;
  29. /**
  30. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  31. */
  32. protected $customerSessionMock;
  33. /**
  34. * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
  35. */
  36. protected $redirectMock;
  37. /**
  38. * @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
  39. */
  40. protected $urlMock;
  41. /**
  42. * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  43. */
  44. protected $customerAccountManagementMock;
  45. /**
  46. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  47. */
  48. protected $customerRepositoryMock;
  49. /**
  50. * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  51. */
  52. protected $customerDataMock;
  53. /**
  54. * @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  55. */
  56. protected $messageManagerMock;
  57. /**
  58. * @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject
  59. */
  60. protected $addressHelperMock;
  61. /**
  62. * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject
  63. */
  64. protected $storeManagerMock;
  65. /**
  66. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  67. */
  68. protected $storeMock;
  69. /**
  70. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  71. */
  72. protected $scopeConfigMock;
  73. /**
  74. * @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
  75. */
  76. protected $contextMock;
  77. /**
  78. * @var \Magento\Framework\Controller\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
  79. */
  80. protected $redirectResultMock;
  81. protected function setUp()
  82. {
  83. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  84. $this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
  85. $this->responseMock = $this->createPartialMock(
  86. \Magento\Framework\App\Response\Http::class,
  87. ['setRedirect', '__wakeup']
  88. );
  89. $viewMock = $this->createMock(\Magento\Framework\App\ViewInterface::class);
  90. $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  91. $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
  92. $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
  93. $urlFactoryMock->expects($this->any())
  94. ->method('create')
  95. ->will($this->returnValue($this->urlMock));
  96. $this->customerAccountManagementMock =
  97. $this->getMockForAbstractClass(\Magento\Customer\Api\AccountManagementInterface::class);
  98. $this->customerDataMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  99. $this->customerRepositoryMock =
  100. $this->getMockForAbstractClass(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  101. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
  102. $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
  103. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
  104. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  105. $this->redirectResultMock = $this->createMock(\Magento\Framework\Controller\Result\Redirect::class);
  106. $resultFactoryMock = $this->createPartialMock(\Magento\Framework\Controller\ResultFactory::class, ['create']);
  107. $resultFactoryMock->expects($this->once())
  108. ->method('create')
  109. ->with(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT)
  110. ->willReturn($this->redirectResultMock);
  111. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  112. $this->contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  113. $this->contextMock->expects($this->any())
  114. ->method('getRequest')
  115. ->willReturn($this->requestMock);
  116. $this->contextMock->expects($this->any())
  117. ->method('getResponse')
  118. ->willReturn($this->responseMock);
  119. $this->contextMock->expects($this->any())
  120. ->method('getRedirect')
  121. ->willReturn($this->redirectMock);
  122. $this->contextMock->expects($this->any())
  123. ->method('getView')
  124. ->willReturn($viewMock);
  125. $this->contextMock->expects($this->any())
  126. ->method('getMessageManager')
  127. ->willReturn($this->messageManagerMock);
  128. $this->contextMock->expects($this->any())
  129. ->method('getResultFactory')
  130. ->willReturn($resultFactoryMock);
  131. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  132. $this->model = $objectManagerHelper->getObject(
  133. \Magento\Customer\Controller\Account\Confirm::class,
  134. [
  135. 'context' => $this->contextMock,
  136. 'customerSession' => $this->customerSessionMock,
  137. 'scopeConfig' => $this->scopeConfigMock,
  138. 'storeManager' => $this->storeManagerMock,
  139. 'customerAccountManagement' => $this->customerAccountManagementMock,
  140. 'customerRepository' => $this->customerRepositoryMock,
  141. 'addressHelper' => $this->addressHelperMock,
  142. 'urlFactory' => $urlFactoryMock,
  143. ]
  144. );
  145. }
  146. public function testIsLoggedIn()
  147. {
  148. $this->customerSessionMock->expects($this->once())
  149. ->method('isLoggedIn')
  150. ->will($this->returnValue(true));
  151. $this->redirectResultMock->expects($this->once())
  152. ->method('setPath')
  153. ->with('*/*/')
  154. ->willReturnSelf();
  155. $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
  156. }
  157. /**
  158. * @dataProvider getParametersDataProvider
  159. */
  160. public function testNoCustomerIdInRequest($customerId, $key)
  161. {
  162. $this->customerSessionMock->expects($this->once())
  163. ->method('isLoggedIn')
  164. ->will($this->returnValue(false));
  165. $this->requestMock->expects($this->at(0))
  166. ->method('getParam')
  167. ->with($this->equalTo('id'), false)
  168. ->will($this->returnValue($customerId));
  169. $this->requestMock->expects($this->at(1))
  170. ->method('getParam')
  171. ->with($this->equalTo('key'), false)
  172. ->will($this->returnValue($key));
  173. $exception = new \Exception('Bad request.');
  174. $this->messageManagerMock->expects($this->once())
  175. ->method('addException')
  176. ->with($this->equalTo($exception), $this->equalTo('There was an error confirming the account'));
  177. $testUrl = 'http://example.com';
  178. $this->urlMock->expects($this->once())
  179. ->method('getUrl')
  180. ->with($this->equalTo('*/*/index'), ['_secure' => true])
  181. ->will($this->returnValue($testUrl));
  182. $this->redirectMock->expects($this->once())
  183. ->method('error')
  184. ->with($this->equalTo($testUrl))
  185. ->will($this->returnValue($testUrl));
  186. $this->redirectResultMock->expects($this->once())
  187. ->method('setUrl')
  188. ->with($this->equalTo($testUrl))
  189. ->willReturnSelf();
  190. $this->assertInstanceOf(\Magento\Framework\Controller\Result\Redirect::class, $this->model->execute());
  191. }
  192. /**
  193. * @return array
  194. */
  195. public function getParametersDataProvider()
  196. {
  197. return [
  198. [true, false],
  199. [false, true],
  200. ];
  201. }
  202. /**
  203. * @param $customerId
  204. * @param $key
  205. * @param $vatValidationEnabled
  206. * @param $addressType
  207. * @param $successMessage
  208. *
  209. * @dataProvider getSuccessMessageDataProvider
  210. */
  211. public function testSuccessMessage($customerId, $key, $vatValidationEnabled, $addressType, $successMessage)
  212. {
  213. $this->customerSessionMock->expects($this->once())
  214. ->method('isLoggedIn')
  215. ->will($this->returnValue(false));
  216. $this->requestMock->expects($this->any())
  217. ->method('getParam')
  218. ->willReturnMap([
  219. ['id', false, $customerId],
  220. ['key', false, $key],
  221. ]);
  222. $this->customerRepositoryMock->expects($this->any())
  223. ->method('getById')
  224. ->with($customerId)
  225. ->will($this->returnValue($this->customerDataMock));
  226. $email = 'test@example.com';
  227. $this->customerDataMock->expects($this->once())
  228. ->method('getEmail')
  229. ->will($this->returnValue($email));
  230. $this->customerAccountManagementMock->expects($this->once())
  231. ->method('activate')
  232. ->with($this->equalTo($email), $this->equalTo($key))
  233. ->will($this->returnValue($this->customerDataMock));
  234. $this->customerSessionMock->expects($this->any())
  235. ->method('setCustomerDataAsLoggedIn')
  236. ->with($this->equalTo($this->customerDataMock))
  237. ->willReturnSelf();
  238. $this->messageManagerMock->expects($this->any())
  239. ->method('addSuccess')
  240. ->with($this->stringContains($successMessage))
  241. ->willReturnSelf();
  242. $this->addressHelperMock->expects($this->once())
  243. ->method('isVatValidationEnabled')
  244. ->will($this->returnValue($vatValidationEnabled));
  245. $this->addressHelperMock->expects($this->any())
  246. ->method('getTaxCalculationAddressType')
  247. ->will($this->returnValue($addressType));
  248. $this->storeMock->expects($this->any())
  249. ->method('getFrontendName')
  250. ->will($this->returnValue('frontend'));
  251. $this->storeManagerMock->expects($this->any())
  252. ->method('getStore')
  253. ->will($this->returnValue($this->storeMock));
  254. $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
  255. ->disableOriginalConstructor()
  256. ->getMock();
  257. $cookieMetadataManager->expects($this->once())
  258. ->method('getCookie')
  259. ->with('mage-cache-sessid')
  260. ->willReturn(true);
  261. $cookieMetadataFactory = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadataFactory::class)
  262. ->disableOriginalConstructor()
  263. ->getMock();
  264. $cookieMetadata = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\CookieMetadata::class)
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. $cookieMetadataFactory->expects($this->once())
  268. ->method('createCookieMetadata')
  269. ->willReturn($cookieMetadata);
  270. $cookieMetadata->expects($this->once())
  271. ->method('setPath')
  272. ->with('/');
  273. $cookieMetadataManager->expects($this->once())
  274. ->method('deleteCookie')
  275. ->with('mage-cache-sessid', $cookieMetadata);
  276. $refClass = new \ReflectionClass(Confirm::class);
  277. $cookieMetadataManagerProperty = $refClass->getProperty('cookieMetadataManager');
  278. $cookieMetadataManagerProperty->setAccessible(true);
  279. $cookieMetadataManagerProperty->setValue($this->model, $cookieMetadataManager);
  280. $cookieMetadataFactoryProperty = $refClass->getProperty('cookieMetadataFactory');
  281. $cookieMetadataFactoryProperty->setAccessible(true);
  282. $cookieMetadataFactoryProperty->setValue($this->model, $cookieMetadataFactory);
  283. $this->model->execute();
  284. }
  285. /**
  286. * @return array
  287. */
  288. public function getSuccessMessageDataProvider()
  289. {
  290. return [
  291. [1, 1, false, null, __('Thank you for registering with')],
  292. [1, 1, true, Address::TYPE_BILLING, __('enter your billing address for proper VAT calculation')],
  293. [1, 1, true, Address::TYPE_SHIPPING, __('enter your shipping address for proper VAT calculation')],
  294. ];
  295. }
  296. /**
  297. * @param $customerId
  298. * @param $key
  299. * @param $backUrl
  300. * @param $successUrl
  301. * @param $resultUrl
  302. * @param $isSetFlag
  303. * @param $successMessage
  304. *
  305. * @dataProvider getSuccessRedirectDataProvider
  306. */
  307. public function testSuccessRedirect(
  308. $customerId,
  309. $key,
  310. $backUrl,
  311. $successUrl,
  312. $resultUrl,
  313. $isSetFlag,
  314. $successMessage
  315. ) {
  316. $this->customerSessionMock->expects($this->once())
  317. ->method('isLoggedIn')
  318. ->will($this->returnValue(false));
  319. $this->requestMock->expects($this->any())
  320. ->method('getParam')
  321. ->willReturnMap([
  322. ['id', false, $customerId],
  323. ['key', false, $key],
  324. ['back_url', false, $backUrl],
  325. ]);
  326. $this->customerRepositoryMock->expects($this->any())
  327. ->method('getById')
  328. ->with($customerId)
  329. ->will($this->returnValue($this->customerDataMock));
  330. $email = 'test@example.com';
  331. $this->customerDataMock->expects($this->once())
  332. ->method('getEmail')
  333. ->will($this->returnValue($email));
  334. $this->customerAccountManagementMock->expects($this->once())
  335. ->method('activate')
  336. ->with($this->equalTo($email), $this->equalTo($key))
  337. ->will($this->returnValue($this->customerDataMock));
  338. $this->customerSessionMock->expects($this->any())
  339. ->method('setCustomerDataAsLoggedIn')
  340. ->with($this->equalTo($this->customerDataMock))
  341. ->willReturnSelf();
  342. $this->messageManagerMock->expects($this->any())
  343. ->method('addSuccess')
  344. ->with($this->stringContains($successMessage))
  345. ->willReturnSelf();
  346. $this->storeMock->expects($this->any())
  347. ->method('getFrontendName')
  348. ->will($this->returnValue('frontend'));
  349. $this->storeManagerMock->expects($this->any())
  350. ->method('getStore')
  351. ->will($this->returnValue($this->storeMock));
  352. $this->urlMock->expects($this->any())
  353. ->method('getUrl')
  354. ->with($this->equalTo('*/*/index'), ['_secure' => true])
  355. ->will($this->returnValue($successUrl));
  356. $this->redirectMock->expects($this->once())
  357. ->method('success')
  358. ->with($this->equalTo($resultUrl))
  359. ->willReturn($resultUrl);
  360. $this->scopeConfigMock->expects($this->any())
  361. ->method('isSetFlag')
  362. ->with(
  363. Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD,
  364. ScopeInterface::SCOPE_STORE
  365. )
  366. ->willReturn($isSetFlag);
  367. $cookieMetadataManager = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
  368. ->disableOriginalConstructor()
  369. ->getMock();
  370. $cookieMetadataManager->expects($this->once())
  371. ->method('getCookie')
  372. ->with('mage-cache-sessid')
  373. ->willReturn(false);
  374. $refClass = new \ReflectionClass(Confirm::class);
  375. $refProperty = $refClass->getProperty('cookieMetadataManager');
  376. $refProperty->setAccessible(true);
  377. $refProperty->setValue($this->model, $cookieMetadataManager);
  378. $this->model->execute();
  379. }
  380. /**
  381. * @return array
  382. */
  383. public function getSuccessRedirectDataProvider()
  384. {
  385. return [
  386. [
  387. 1,
  388. 1,
  389. 'http://example.com/back',
  390. null,
  391. 'http://example.com/back',
  392. true,
  393. __('Thank you for registering with'),
  394. ],
  395. [
  396. 1,
  397. 1,
  398. null,
  399. 'http://example.com/success',
  400. 'http://example.com/success',
  401. true,
  402. __('Thank you for registering with'),
  403. ],
  404. [
  405. 1,
  406. 1,
  407. null,
  408. 'http://example.com/success',
  409. 'http://example.com/success',
  410. false,
  411. __('Thank you for registering with'),
  412. ],
  413. ];
  414. }
  415. }