PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-customer/Test/Unit/Controller/Account/ResetPasswordPostTest.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 307 lines | 239 code | 49 blank | 19 comment | 0 complexity | 607572880a863e8f1afe92ef469f2b9d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Controller\Account;
  7. use Magento\Framework\Controller\Result\Redirect;
  8. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  9. class ResetPasswordPostTest extends \PHPUnit_Framework_TestCase
  10. {
  11. /** @var \Magento\Customer\Controller\Account\ResetPasswordPost */
  12. protected $model;
  13. /** @var ObjectManagerHelper */
  14. protected $objectManagerHelper;
  15. /** @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject */
  16. protected $sessionMock;
  17. /** @var \Magento\Framework\View\Result\PageFactory|\PHPUnit_Framework_MockObject_MockObject */
  18. protected $pageFactoryMock;
  19. /** @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject */
  20. protected $accountManagementMock;
  21. /** @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $customerRepositoryMock;
  23. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. protected $requestMock;
  25. /** @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject */
  26. protected $redirectFactoryMock;
  27. /** @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject */
  28. protected $messageManagerMock;
  29. protected function setUp()
  30. {
  31. $this->sessionMock = $this->getMockBuilder('Magento\Customer\Model\Session')
  32. ->disableOriginalConstructor()
  33. ->setMethods(['unsRpToken', 'unsRpCustomerId'])
  34. ->getMock();
  35. $this->pageFactoryMock = $this->getMockBuilder('Magento\Framework\View\Result\PageFactory')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $this->accountManagementMock = $this->getMockBuilder('Magento\Customer\Api\AccountManagementInterface')
  39. ->getMockForAbstractClass();
  40. $this->customerRepositoryMock = $this->getMockBuilder('Magento\Customer\Api\CustomerRepositoryInterface')
  41. ->getMockForAbstractClass();
  42. $this->requestMock = $this->getMockBuilder('Magento\Framework\App\RequestInterface')
  43. ->setMethods(['getQuery', 'getPost'])
  44. ->getMockForAbstractClass();
  45. $this->redirectFactoryMock = $this->getMockBuilder('Magento\Framework\Controller\Result\RedirectFactory')
  46. ->disableOriginalConstructor()
  47. ->getMock();
  48. $this->messageManagerMock = $this->getMockBuilder('Magento\Framework\Message\ManagerInterface')
  49. ->getMockForAbstractClass();
  50. $this->objectManagerHelper = new ObjectManagerHelper($this);
  51. $this->model = $this->objectManagerHelper->getObject(
  52. 'Magento\Customer\Controller\Account\ResetPasswordPost',
  53. [
  54. 'customerSession' => $this->sessionMock,
  55. 'resultPageFactory' => $this->pageFactoryMock,
  56. 'accountManagement' => $this->accountManagementMock,
  57. 'customerRepository' => $this->customerRepositoryMock,
  58. 'request' => $this->requestMock,
  59. 'resultRedirectFactory' => $this->redirectFactoryMock,
  60. 'messageManager' => $this->messageManagerMock,
  61. ]
  62. );
  63. }
  64. public function testExecute()
  65. {
  66. $token = 'token';
  67. $customerId = '11';
  68. $password = 'password';
  69. $passwordConfirmation = 'password';
  70. $email = 'email@email.com';
  71. $this->requestMock->expects($this->exactly(2))
  72. ->method('getQuery')
  73. ->willReturnMap(
  74. [
  75. ['token', $token],
  76. ['id', $customerId],
  77. ]
  78. );
  79. $this->requestMock->expects($this->exactly(2))
  80. ->method('getPost')
  81. ->willReturnMap(
  82. [
  83. ['password', $password],
  84. ['password_confirmation', $passwordConfirmation],
  85. ]
  86. );
  87. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  88. $customerMock = $this->getMockBuilder('\Magento\Customer\Api\Data\CustomerInterface')
  89. ->getMockForAbstractClass();
  90. $this->customerRepositoryMock->expects($this->once())
  91. ->method('getById')
  92. ->with($customerId)
  93. ->willReturn($customerMock);
  94. $customerMock->expects($this->once())
  95. ->method('getEmail')
  96. ->willReturn($email);
  97. $this->accountManagementMock->expects($this->once())
  98. ->method('resetPassword')
  99. ->with($email, $token, $password)
  100. ->willReturn(true);
  101. $this->sessionMock->expects($this->once())
  102. ->method('unsRpToken');
  103. $this->sessionMock->expects($this->once())
  104. ->method('unsRpCustomerId');
  105. $this->messageManagerMock->expects($this->once())
  106. ->method('addSuccess')
  107. ->with(__('You updated your password.'))
  108. ->willReturnSelf();
  109. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  110. $redirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $this->redirectFactoryMock->expects($this->once())
  114. ->method('create')
  115. ->with([])
  116. ->willReturn($redirectMock);
  117. $redirectMock->expects($this->once())
  118. ->method('setPath')
  119. ->with('*/*/login', [])
  120. ->willReturnSelf();
  121. $this->assertEquals($redirectMock, $this->model->execute());
  122. }
  123. public function testExecuteWithException()
  124. {
  125. $token = 'token';
  126. $customerId = '11';
  127. $password = 'password';
  128. $passwordConfirmation = 'password';
  129. $email = 'email@email.com';
  130. $this->requestMock->expects($this->exactly(2))
  131. ->method('getQuery')
  132. ->willReturnMap(
  133. [
  134. ['token', $token],
  135. ['id', $customerId],
  136. ]
  137. );
  138. $this->requestMock->expects($this->exactly(2))
  139. ->method('getPost')
  140. ->willReturnMap(
  141. [
  142. ['password', $password],
  143. ['password_confirmation', $passwordConfirmation],
  144. ]
  145. );
  146. /** @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customerMock */
  147. $customerMock = $this->getMockBuilder('\Magento\Customer\Api\Data\CustomerInterface')
  148. ->getMockForAbstractClass();
  149. $this->customerRepositoryMock->expects($this->once())
  150. ->method('getById')
  151. ->with($customerId)
  152. ->willReturn($customerMock);
  153. $customerMock->expects($this->once())
  154. ->method('getEmail')
  155. ->willReturn($email);
  156. $this->accountManagementMock->expects($this->once())
  157. ->method('resetPassword')
  158. ->with($email, $token, $password)
  159. ->willThrowException(new \Exception('Exception.'));
  160. $this->messageManagerMock->expects($this->once())
  161. ->method('addError')
  162. ->with(__('Something went wrong while saving the new password.'))
  163. ->willReturnSelf();
  164. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  165. $redirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
  166. ->disableOriginalConstructor()
  167. ->getMock();
  168. $this->redirectFactoryMock->expects($this->once())
  169. ->method('create')
  170. ->with([])
  171. ->willReturn($redirectMock);
  172. $redirectMock->expects($this->once())
  173. ->method('setPath')
  174. ->with('*/*/createPassword', ['id' => $customerId, 'token' => $token])
  175. ->willReturnSelf();
  176. $this->assertEquals($redirectMock, $this->model->execute());
  177. }
  178. public function testExecuteWithWrongConfirmation()
  179. {
  180. $token = 'token';
  181. $customerId = '11';
  182. $password = 'password';
  183. $passwordConfirmation = 'wrong_password';
  184. $this->requestMock->expects($this->exactly(2))
  185. ->method('getQuery')
  186. ->willReturnMap(
  187. [
  188. ['token', $token],
  189. ['id', $customerId],
  190. ]
  191. );
  192. $this->requestMock->expects($this->exactly(2))
  193. ->method('getPost')
  194. ->willReturnMap(
  195. [
  196. ['password', $password],
  197. ['password_confirmation', $passwordConfirmation],
  198. ]
  199. );
  200. $this->messageManagerMock->expects($this->once())
  201. ->method('addError')
  202. ->with(__('New Password and Confirm New Password values didn\'t match.'))
  203. ->willReturnSelf();
  204. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  205. $redirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
  206. ->disableOriginalConstructor()
  207. ->getMock();
  208. $this->redirectFactoryMock->expects($this->once())
  209. ->method('create')
  210. ->with([])
  211. ->willReturn($redirectMock);
  212. $redirectMock->expects($this->once())
  213. ->method('setPath')
  214. ->with('*/*/createPassword', ['id' => $customerId, 'token' => $token])
  215. ->willReturnSelf();
  216. $this->assertEquals($redirectMock, $this->model->execute());
  217. }
  218. public function testExecuteWithEmptyPassword()
  219. {
  220. $token = 'token';
  221. $customerId = '11';
  222. $password = '';
  223. $passwordConfirmation = '';
  224. $this->requestMock->expects($this->exactly(2))
  225. ->method('getQuery')
  226. ->willReturnMap(
  227. [
  228. ['token', $token],
  229. ['id', $customerId],
  230. ]
  231. );
  232. $this->requestMock->expects($this->exactly(2))
  233. ->method('getPost')
  234. ->willReturnMap(
  235. [
  236. ['password', $password],
  237. ['password_confirmation', $passwordConfirmation],
  238. ]
  239. );
  240. $this->messageManagerMock->expects($this->once())
  241. ->method('addError')
  242. ->with(__('Please enter a new password.'))
  243. ->willReturnSelf();
  244. /** @var Redirect|\PHPUnit_Framework_MockObject_MockObject $redirectMock */
  245. $redirectMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Redirect')
  246. ->disableOriginalConstructor()
  247. ->getMock();
  248. $this->redirectFactoryMock->expects($this->once())
  249. ->method('create')
  250. ->with([])
  251. ->willReturn($redirectMock);
  252. $redirectMock->expects($this->once())
  253. ->method('setPath')
  254. ->with('*/*/createPassword', ['id' => $customerId, 'token' => $token])
  255. ->willReturnSelf();
  256. $this->assertEquals($redirectMock, $this->model->execute());
  257. }
  258. }