CreatePostTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Customer\Test\Unit\Controller\Account;
  8. use Magento\Customer\Api\AccountManagementInterface;
  9. use Magento\Customer\Helper\Address;
  10. use Magento\Customer\Model\Url;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class CreatePostTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var \Magento\Customer\Controller\Account\CreatePost
  20. */
  21. protected $model;
  22. /**
  23. * @var \Magento\Customer\Model\Session|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $customerSessionMock;
  26. /**
  27. * @var \Magento\Customer\Model\Url|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $customerUrl;
  30. /**
  31. * @var \Magento\Customer\Model\Registration|\PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $registration;
  34. /**
  35. * @var \Magento\Framework\App\Response\RedirectInterface|\PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $redirectMock;
  38. /**
  39. * @var \Magento\Customer\Api\CustomerRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  40. */
  41. protected $customerRepository;
  42. /**
  43. * @var \Magento\Customer\Api\AccountManagementInterface|\PHPUnit_Framework_MockObject_MockObject
  44. */
  45. protected $accountManagement;
  46. /**
  47. * @var \PHPUnit_Framework_MockObject_MockObject
  48. */
  49. protected $responseMock;
  50. /**
  51. * @var \Magento\Framework\App\Request\Http|\PHPUnit_Framework_MockObject_MockObject
  52. */
  53. protected $requestMock;
  54. /**
  55. * @var \Magento\Framework\Url|\PHPUnit_Framework_MockObject_MockObject
  56. */
  57. protected $urlMock;
  58. /**
  59. * @var \Magento\Customer\Model\CustomerExtractor|\PHPUnit_Framework_MockObject_MockObject
  60. */
  61. protected $customerExtractorMock;
  62. /**
  63. * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  64. */
  65. protected $customerMock;
  66. /**
  67. * @var \Magento\Customer\Api\Data\CustomerInterface|\PHPUnit_Framework_MockObject_MockObject
  68. */
  69. protected $customerDetailsMock;
  70. /**
  71. * @var \Magento\Customer\Api\Data\CustomerInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
  72. */
  73. protected $customerDetailsFactoryMock;
  74. /**
  75. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  76. */
  77. protected $scopeConfigMock;
  78. /**
  79. * @var \Magento\Store\Model\StoreManager|\PHPUnit_Framework_MockObject_MockObject
  80. */
  81. protected $storeManagerMock;
  82. /**
  83. * @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
  84. */
  85. protected $storeMock;
  86. /**
  87. * @var \Magento\Customer\Helper\Address|\PHPUnit_Framework_MockObject_MockObject
  88. */
  89. protected $addressHelperMock;
  90. /**
  91. * @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
  92. */
  93. protected $subscriberMock;
  94. /**
  95. * @var \Magento\Framework\Message\Manager|\PHPUnit_Framework_MockObject_MockObject
  96. */
  97. protected $messageManagerMock;
  98. /**
  99. * @var \Magento\Framework\Api\DataObjectHelper|\PHPUnit_Framework_MockObject_MockObject
  100. */
  101. protected $dataObjectHelperMock;
  102. /**
  103. * @var \Magento\Framework\Controller\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
  104. */
  105. protected $resultRedirectFactoryMock;
  106. /**
  107. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  108. */
  109. protected function setUp()
  110. {
  111. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  112. /**
  113. * This test can be unskipped when the Unit test object manager helper is enabled to return correct DataBuilders
  114. * For now the \Magento\Customer\Test\Unit\Controller\AccountTest sufficiently covers the SUT
  115. */
  116. $this->markTestSkipped('Cannot be unit tested with the auto generated builder dependencies');
  117. $this->customerSessionMock = $this->createMock(\Magento\Customer\Model\Session::class);
  118. $this->redirectMock = $this->createMock(\Magento\Framework\App\Response\RedirectInterface::class);
  119. $this->responseMock = $this->createMock(\Magento\Framework\Webapi\Response::class);
  120. $this->requestMock = $this->createMock(\Magento\Framework\App\Request\Http::class);
  121. $this->urlMock = $this->createMock(\Magento\Framework\Url::class);
  122. $urlFactoryMock = $this->createMock(\Magento\Framework\UrlFactory::class);
  123. $urlFactoryMock->expects($this->once())
  124. ->method('create')
  125. ->will($this->returnValue($this->urlMock));
  126. $this->customerMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  127. $this->customerDetailsMock = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
  128. $this->customerDetailsFactoryMock = $this->createMock(
  129. \Magento\Customer\Api\Data\CustomerInterfaceFactory::class
  130. );
  131. $this->messageManagerMock = $this->createMock(\Magento\Framework\Message\Manager::class);
  132. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  133. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManager::class);
  134. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  135. $this->customerRepository = $this->createMock(\Magento\Customer\Api\CustomerRepositoryInterface::class);
  136. $this->accountManagement = $this->createMock(\Magento\Customer\Api\AccountManagementInterface::class);
  137. $this->addressHelperMock = $this->createMock(\Magento\Customer\Helper\Address::class);
  138. $formFactoryMock = $this->createMock(\Magento\Customer\Model\Metadata\FormFactory::class);
  139. $this->subscriberMock = $this->createMock(\Magento\Newsletter\Model\Subscriber::class);
  140. $subscriberFactoryMock = $this->createPartialMock(
  141. \Magento\Newsletter\Model\SubscriberFactory::class,
  142. ['create']
  143. );
  144. $subscriberFactoryMock->expects($this->any())
  145. ->method('create')
  146. ->will($this->returnValue($this->subscriberMock));
  147. $regionFactoryMock = $this->createMock(\Magento\Customer\Api\Data\RegionInterfaceFactory::class);
  148. $addressFactoryMock = $this->createMock(\Magento\Customer\Api\Data\AddressInterfaceFactory::class);
  149. $this->customerUrl = $this->createMock(\Magento\Customer\Model\Url::class);
  150. $this->registration = $this->createMock(\Magento\Customer\Model\Registration::class);
  151. $escaperMock = $this->createMock(\Magento\Framework\Escaper::class);
  152. $this->customerExtractorMock = $this->createMock(\Magento\Customer\Model\CustomerExtractor::class);
  153. $this->dataObjectHelperMock = $this->createMock(\Magento\Framework\Api\DataObjectHelper::class);
  154. $eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
  155. $this->resultRedirectFactoryMock = $this->getMockBuilder(
  156. \Magento\Framework\Controller\Result\RedirectFactory::class
  157. )
  158. ->setMethods(['create'])
  159. ->disableOriginalConstructor()
  160. ->getMock();
  161. $this->resultRedirectFactoryMock->expects($this->any())
  162. ->method('create')
  163. ->willReturn($this->redirectMock);
  164. $contextMock = $this->createMock(\Magento\Framework\App\Action\Context::class);
  165. $contextMock->expects($this->any())
  166. ->method('getRequest')
  167. ->willReturn($this->requestMock);
  168. $contextMock->expects($this->any())
  169. ->method('getResponse')
  170. ->willReturn($this->responseMock);
  171. $contextMock->expects($this->any())
  172. ->method('getRedirect')
  173. ->willReturn($this->redirectMock);
  174. $contextMock->expects($this->any())
  175. ->method('getMessageManager')
  176. ->willReturn($this->messageManagerMock);
  177. $contextMock->expects($this->any())
  178. ->method('getEventManager')
  179. ->willReturn($eventManagerMock);
  180. $contextMock->expects($this->any())
  181. ->method('getResultRedirectFactory')
  182. ->willReturn($this->resultRedirectFactoryMock);
  183. $this->model = $objectManager->getObject(
  184. \Magento\Customer\Controller\Account\CreatePost::class,
  185. [
  186. 'context' => $contextMock,
  187. 'customerSession' => $this->customerSessionMock,
  188. 'scopeConfig' => $this->scopeConfigMock,
  189. 'storeManager' => $this->storeManagerMock,
  190. 'accountManagement' => $this->accountManagement,
  191. 'addressHelper' => $this->addressHelperMock,
  192. 'urlFactory' => $urlFactoryMock,
  193. 'formFactory' => $formFactoryMock,
  194. 'subscriberFactory' => $subscriberFactoryMock,
  195. 'regionDataFactory' => $regionFactoryMock,
  196. 'addressDataFactory' => $addressFactoryMock,
  197. 'customerDetailsFactory' => $this->customerDetailsFactoryMock,
  198. 'customerUrl' => $this->customerUrl,
  199. 'registration' => $this->registration,
  200. 'escape' => $escaperMock,
  201. 'customerExtractor' => $this->customerExtractorMock,
  202. 'dataObjectHelper' => $this->dataObjectHelperMock,
  203. ]
  204. );
  205. }
  206. /**
  207. * @return void
  208. */
  209. public function testCreatePostActionRegistrationDisabled()
  210. {
  211. $this->customerSessionMock->expects($this->once())
  212. ->method('isLoggedIn')
  213. ->will($this->returnValue(false));
  214. $this->registration->expects($this->once())
  215. ->method('isAllowed')
  216. ->will($this->returnValue(false));
  217. $this->redirectMock->expects($this->once())
  218. ->method('redirect')
  219. ->with($this->responseMock, '*/*/', [])
  220. ->will($this->returnValue(false));
  221. $this->customerRepository->expects($this->never())
  222. ->method('save');
  223. $this->model->execute();
  224. }
  225. public function testRegenerateIdOnExecution()
  226. {
  227. $this->customerSessionMock->expects($this->once())
  228. ->method('regenerateId');
  229. $this->customerSessionMock->expects($this->once())
  230. ->method('isLoggedIn')
  231. ->will($this->returnValue(false));
  232. $this->registration->expects($this->once())
  233. ->method('isAllowed')
  234. ->will($this->returnValue(true));
  235. $this->requestMock->expects($this->once())
  236. ->method('isPost')
  237. ->will($this->returnValue(true));
  238. $this->customerExtractorMock->expects($this->once())
  239. ->method('extract')
  240. ->willReturn($this->customerMock);
  241. $this->accountManagement->expects($this->once())
  242. ->method('createAccount')
  243. ->willReturn($this->customerMock);
  244. $this->storeManagerMock->expects($this->once())
  245. ->method('getStore')
  246. ->willReturn($this->storeMock);
  247. $this->model->execute();
  248. }
  249. /**
  250. * @param $customerId
  251. * @param $customerEmail
  252. * @param $password
  253. * @param $confirmationStatus
  254. * @param $vatValidationEnabled
  255. * @param $addressType
  256. * @param $successMessage
  257. *
  258. * @dataProvider getSuccessMessageDataProvider
  259. */
  260. public function testSuccessMessage(
  261. $customerId,
  262. $customerEmail,
  263. $password,
  264. $confirmationStatus,
  265. $vatValidationEnabled,
  266. $addressType,
  267. $successMessage
  268. ) {
  269. $this->customerSessionMock->expects($this->once())
  270. ->method('isLoggedIn')
  271. ->will($this->returnValue(false));
  272. $this->registration->expects($this->once())
  273. ->method('isAllowed')
  274. ->will($this->returnValue(true));
  275. $this->customerUrl->expects($this->once())
  276. ->method('getEmailConfirmationUrl')
  277. ->will($this->returnValue($customerEmail));
  278. $this->customerSessionMock->expects($this->once())
  279. ->method('regenerateId');
  280. $this->customerMock->expects($this->any())
  281. ->method('getId')
  282. ->will($this->returnValue($customerId));
  283. $this->customerMock->expects($this->any())
  284. ->method('getEmail')
  285. ->will($this->returnValue($customerEmail));
  286. $this->customerExtractorMock->expects($this->any())
  287. ->method('extract')
  288. ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
  289. ->will($this->returnValue($this->customerMock));
  290. $this->requestMock->expects($this->once())
  291. ->method('isPost')
  292. ->will($this->returnValue(true));
  293. $this->requestMock->expects($this->any())
  294. ->method('getPost')
  295. ->will($this->returnValue(false));
  296. $this->requestMock->expects($this->any())
  297. ->method('getParam')
  298. ->willReturnMap([
  299. ['password', null, $password],
  300. ['password_confirmation', null, $password],
  301. ['is_subscribed', false, true],
  302. ]);
  303. $this->customerMock->expects($this->once())
  304. ->method('setAddresses')
  305. ->with($this->equalTo([]))
  306. ->will($this->returnSelf());
  307. $this->accountManagement->expects($this->once())
  308. ->method('createAccount')
  309. ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
  310. ->will($this->returnValue($this->customerMock));
  311. $this->accountManagement->expects($this->once())
  312. ->method('getConfirmationStatus')
  313. ->with($this->equalTo($customerId))
  314. ->will($this->returnValue($confirmationStatus));
  315. $this->subscriberMock->expects($this->once())
  316. ->method('subscribeCustomerById')
  317. ->with($this->equalTo($customerId));
  318. $this->messageManagerMock->expects($this->any())
  319. ->method('addSuccess')
  320. ->with($this->stringContains($successMessage))
  321. ->will($this->returnSelf());
  322. $this->addressHelperMock->expects($this->any())
  323. ->method('isVatValidationEnabled')
  324. ->will($this->returnValue($vatValidationEnabled));
  325. $this->addressHelperMock->expects($this->any())
  326. ->method('getTaxCalculationAddressType')
  327. ->will($this->returnValue($addressType));
  328. $this->model->execute();
  329. }
  330. /**
  331. * @return array
  332. */
  333. public function getSuccessMessageDataProvider()
  334. {
  335. return [
  336. [
  337. 1,
  338. 'customer@example.com',
  339. '123123q',
  340. AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
  341. false,
  342. Address::TYPE_SHIPPING,
  343. 'An account confirmation is required',
  344. ],
  345. [
  346. 1,
  347. 'customer@example.com',
  348. '123123q',
  349. AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
  350. false,
  351. Address::TYPE_SHIPPING,
  352. 'Thank you for registering with',
  353. ],
  354. [
  355. 1,
  356. 'customer@example.com',
  357. '123123q',
  358. AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
  359. true,
  360. Address::TYPE_SHIPPING,
  361. 'enter you shipping address for proper VAT calculation',
  362. ],
  363. [
  364. 1,
  365. 'customer@example.com',
  366. '123123q',
  367. AccountManagementInterface::ACCOUNT_CONFIRMATION_REQUIRED,
  368. true,
  369. Address::TYPE_BILLING,
  370. 'enter you billing address for proper VAT calculation',
  371. ],
  372. ];
  373. }
  374. /**
  375. * @param $customerId
  376. * @param $password
  377. * @param $confirmationStatus
  378. * @param $successUrl
  379. * @param $isSetFlag
  380. * @param $successMessage
  381. *
  382. * @dataProvider getSuccessRedirectDataProvider
  383. */
  384. public function testSuccessRedirect(
  385. $customerId,
  386. $password,
  387. $confirmationStatus,
  388. $successUrl,
  389. $isSetFlag,
  390. $successMessage
  391. ) {
  392. $this->customerSessionMock->expects($this->once())
  393. ->method('isLoggedIn')
  394. ->will($this->returnValue(false));
  395. $this->registration->expects($this->once())
  396. ->method('isAllowed')
  397. ->will($this->returnValue(true));
  398. $this->customerSessionMock->expects($this->once())
  399. ->method('regenerateId');
  400. $this->customerMock->expects($this->any())
  401. ->method('getId')
  402. ->will($this->returnValue($customerId));
  403. $this->customerExtractorMock->expects($this->any())
  404. ->method('extract')
  405. ->with($this->equalTo('customer_account_create'), $this->equalTo($this->requestMock))
  406. ->will($this->returnValue($this->customerMock));
  407. $this->requestMock->expects($this->once())
  408. ->method('isPost')
  409. ->will($this->returnValue(true));
  410. $this->requestMock->expects($this->any())
  411. ->method('getPost')
  412. ->will($this->returnValue(false));
  413. $this->requestMock->expects($this->any())
  414. ->method('getParam')
  415. ->willReturnMap([
  416. ['password', null, $password],
  417. ['password_confirmation', null, $password],
  418. ['is_subscribed', false, true],
  419. ]);
  420. $this->customerMock->expects($this->once())
  421. ->method('setAddresses')
  422. ->with($this->equalTo([]))
  423. ->will($this->returnSelf());
  424. $this->accountManagement->expects($this->once())
  425. ->method('createAccount')
  426. ->with($this->equalTo($this->customerDetailsMock), $this->equalTo($password), '')
  427. ->will($this->returnValue($this->customerMock));
  428. $this->accountManagement->expects($this->once())
  429. ->method('getConfirmationStatus')
  430. ->with($this->equalTo($customerId))
  431. ->will($this->returnValue($confirmationStatus));
  432. $this->subscriberMock->expects($this->once())
  433. ->method('subscribeCustomerById')
  434. ->with($this->equalTo($customerId));
  435. $this->messageManagerMock->expects($this->any())
  436. ->method('addSuccess')
  437. ->with($this->stringContains($successMessage))
  438. ->will($this->returnSelf());
  439. $this->urlMock->expects($this->any())
  440. ->method('getUrl')
  441. ->willReturnMap([
  442. ['*/*/index', ['_secure' => true], $successUrl],
  443. ['*/*/create', ['_secure' => true], $successUrl],
  444. ]);
  445. $this->redirectMock->expects($this->once())
  446. ->method('success')
  447. ->with($this->equalTo($successUrl))
  448. ->will($this->returnValue($successUrl));
  449. $this->scopeConfigMock->expects($this->once())
  450. ->method('isSetFlag')
  451. ->with(
  452. $this->equalTo(Url::XML_PATH_CUSTOMER_STARTUP_REDIRECT_TO_DASHBOARD),
  453. $this->equalTo(ScopeInterface::SCOPE_STORE)
  454. )
  455. ->will($this->returnValue($isSetFlag));
  456. $this->storeMock->expects($this->any())
  457. ->method('getFrontendName')
  458. ->will($this->returnValue('frontend'));
  459. $this->storeManagerMock->expects($this->any())
  460. ->method('getStore')
  461. ->will($this->returnValue($this->storeMock));
  462. $this->model->execute();
  463. }
  464. /**
  465. * @return array
  466. */
  467. public function getSuccessRedirectDataProvider()
  468. {
  469. return [
  470. [
  471. 1,
  472. '123123q',
  473. AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED,
  474. 'http://example.com/success',
  475. true,
  476. 'Thank you for registering with',
  477. ],
  478. [
  479. 1,
  480. '123123q',
  481. AccountManagementInterface::ACCOUNT_CONFIRMATION_NOT_REQUIRED,
  482. 'http://example.com/success',
  483. false,
  484. 'Thank you for registering with',
  485. ],
  486. ];
  487. }
  488. }