EmailNotificationTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model;
  7. use Magento\Customer\Api\Data\CustomerInterface;
  8. use Magento\Customer\Model\EmailNotification;
  9. use Magento\Framework\App\Area;
  10. use Magento\Framework\Mail\Template\SenderResolverInterface;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * Class EmailNotificationTest
  14. *
  15. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  16. */
  17. class EmailNotificationTest extends \PHPUnit\Framework\TestCase
  18. {
  19. /**
  20. * @var \Magento\Customer\Model\CustomerRegistry|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $customerRegistryMock;
  23. /**
  24. * @var \Magento\Store\Model\StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $storeManagerMock;
  27. /**
  28. * @var \Magento\Framework\Mail\Template\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
  29. */
  30. private $transportBuilderMock;
  31. /**
  32. * @var \Magento\Customer\Helper\View|\PHPUnit_Framework_MockObject_MockObject
  33. */
  34. private $customerViewHelperMock;
  35. /**
  36. * @var \Magento\Framework\Reflection\DataObjectProcessor|\PHPUnit_Framework_MockObject_MockObject
  37. */
  38. private $dataProcessorMock;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Customer\Model\Data\CustomerSecure
  41. */
  42. private $customerSecureMock;
  43. /**
  44. * @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $scopeConfigMock;
  47. /**
  48. * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Store\Model\Store
  49. */
  50. private $storeMock;
  51. /**
  52. * @var \Magento\Customer\Model\EmailNotification
  53. */
  54. private $model;
  55. /**
  56. * @var SenderResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  57. */
  58. private $senderResolverMock;
  59. public function setUp()
  60. {
  61. $this->customerRegistryMock = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
  62. $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  63. $this->transportBuilderMock = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
  64. $this->customerViewHelperMock = $this->createMock(\Magento\Customer\Helper\View::class);
  65. $this->dataProcessorMock = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
  66. $contextMock = $this->createPartialMock(\Magento\Framework\App\Helper\Context::class, ['getScopeConfig']);
  67. $this->scopeConfigMock = $this->createPartialMock(
  68. \Magento\Framework\App\Config\ScopeConfigInterface::class,
  69. ['getValue', 'isSetFlag']
  70. );
  71. $contextMock->expects($this->any())
  72. ->method('getScopeConfig')
  73. ->willReturn($this->scopeConfigMock);
  74. $this->customerSecureMock = $this->createMock(\Magento\Customer\Model\Data\CustomerSecure::class);
  75. $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  76. $this->senderResolverMock = $this->getMockBuilder(SenderResolverInterface::class)
  77. ->setMethods(['resolve'])
  78. ->disableOriginalConstructor()
  79. ->getMockForAbstractClass();
  80. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  81. $this->model = $objectManager->getObject(
  82. EmailNotification::class,
  83. [
  84. 'customerRegistry' => $this->customerRegistryMock,
  85. 'storeManager' => $this->storeManagerMock,
  86. 'transportBuilder' => $this->transportBuilderMock,
  87. 'customerViewHelper' => $this->customerViewHelperMock,
  88. 'dataProcessor' => $this->dataProcessorMock,
  89. 'scopeConfig' => $this->scopeConfigMock,
  90. 'senderResolver' => $this->senderResolverMock,
  91. ]
  92. );
  93. }
  94. /**
  95. * @param int $testNumber
  96. * @param string $oldEmail
  97. * @param string $newEmail
  98. * @param bool $isPasswordChanged
  99. *
  100. * @dataProvider sendNotificationEmailsDataProvider
  101. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  102. */
  103. public function testCredentialsChanged($testNumber, $oldEmail, $newEmail, $isPasswordChanged)
  104. {
  105. $customerId = 1;
  106. $customerStoreId = 2;
  107. $customerWebsiteId = 1;
  108. $customerData = ['key' => 'value'];
  109. $customerName = 'Customer Name';
  110. $templateIdentifier = 'Template Identifier';
  111. $sender = 'Sender';
  112. $senderValues = ['name' => $sender, 'email' => $sender];
  113. $expects = $this->once();
  114. $xmlPathTemplate = EmailNotification::XML_PATH_RESET_PASSWORD_TEMPLATE;
  115. switch ($testNumber) {
  116. case 1:
  117. $xmlPathTemplate = EmailNotification::XML_PATH_RESET_PASSWORD_TEMPLATE;
  118. $expects = $this->once();
  119. break;
  120. case 2:
  121. $xmlPathTemplate = \Magento\Customer\Model\EmailNotification::XML_PATH_CHANGE_EMAIL_TEMPLATE;
  122. $expects = $this->exactly(2);
  123. break;
  124. case 3:
  125. $xmlPathTemplate = EmailNotification::XML_PATH_CHANGE_EMAIL_AND_PASSWORD_TEMPLATE;
  126. $expects = $this->exactly(2);
  127. break;
  128. }
  129. $this->senderResolverMock
  130. ->expects($expects)
  131. ->method('resolve')
  132. ->with($sender, $customerStoreId)
  133. ->willReturn($senderValues);
  134. /** @var \PHPUnit_Framework_MockObject_MockObject $origCustomer */
  135. $origCustomer = $this->createMock(CustomerInterface::class);
  136. $origCustomer->expects($this->any())
  137. ->method('getStoreId')
  138. ->willReturn(0);
  139. $origCustomer->expects($this->any())
  140. ->method('getId')
  141. ->willReturn($customerId);
  142. $origCustomer->expects($this->any())
  143. ->method('getWebsiteId')
  144. ->willReturn($customerWebsiteId);
  145. $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
  146. $storeMock->expects($this->any())
  147. ->method('getId')
  148. ->willReturn($customerStoreId);
  149. $this->storeManagerMock->expects(clone $expects)
  150. ->method('getStore')
  151. ->willReturn($storeMock);
  152. $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
  153. $websiteMock->expects($this->any())
  154. ->method('getStoreIds')
  155. ->willReturn([$customerStoreId]);
  156. $this->storeManagerMock->expects(clone $expects)
  157. ->method('getWebsite')
  158. ->with($customerWebsiteId)
  159. ->willReturn($websiteMock);
  160. $customerSecureMock = $this->createMock(\Magento\Customer\Model\Data\CustomerSecure::class);
  161. $this->customerRegistryMock->expects(clone $expects)
  162. ->method('retrieveSecureData')
  163. ->with($customerId)
  164. ->willReturn($customerSecureMock);
  165. $this->dataProcessorMock->expects(clone $expects)
  166. ->method('buildOutputDataArray')
  167. ->with($origCustomer, CustomerInterface::class)
  168. ->willReturn($customerData);
  169. $this->customerViewHelperMock->expects($this->any())
  170. ->method('getCustomerName')
  171. ->with($origCustomer)
  172. ->willReturn($customerName);
  173. $customerSecureMock->expects(clone $expects)
  174. ->method('addData')
  175. ->with($customerData)
  176. ->willReturnSelf();
  177. $customerSecureMock->expects(clone $expects)
  178. ->method('setData')
  179. ->with('name', $customerName)
  180. ->willReturnSelf();
  181. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $savedCustomer */
  182. $savedCustomer = clone $origCustomer;
  183. $origCustomer->expects($this->any())
  184. ->method('getEmail')
  185. ->willReturn($oldEmail);
  186. $savedCustomer->expects($this->any())
  187. ->method('getEmail')
  188. ->willReturn($newEmail);
  189. $this->scopeConfigMock->expects($this->any())
  190. ->method('getValue')
  191. ->withConsecutive(
  192. [$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId],
  193. [
  194. \Magento\Customer\Model\EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY,
  195. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  196. $customerStoreId
  197. ],
  198. [$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId],
  199. [
  200. \Magento\Customer\Model\EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY,
  201. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  202. $customerStoreId
  203. ]
  204. )
  205. ->willReturnOnConsecutiveCalls($templateIdentifier, $sender, $templateIdentifier, $sender);
  206. $this->transportBuilderMock->expects(clone $expects)
  207. ->method('setTemplateIdentifier')
  208. ->with($templateIdentifier)
  209. ->willReturnSelf();
  210. $this->transportBuilderMock->expects(clone $expects)
  211. ->method('setTemplateOptions')
  212. ->with(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $customerStoreId])
  213. ->willReturnSelf();
  214. $this->transportBuilderMock->expects(clone $expects)
  215. ->method('setTemplateVars')
  216. ->with(['customer' => $customerSecureMock, 'store' => $storeMock])
  217. ->willReturnSelf();
  218. $this->transportBuilderMock->expects(clone $expects)
  219. ->method('setFrom')
  220. ->with($senderValues)
  221. ->willReturnSelf();
  222. $this->transportBuilderMock->expects(clone $expects)
  223. ->method('addTo')
  224. ->withConsecutive([$oldEmail, $customerName], [$newEmail, $customerName])
  225. ->willReturnSelf();
  226. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  227. $this->transportBuilderMock->expects(clone $expects)
  228. ->method('getTransport')
  229. ->willReturn($transport);
  230. $transport->expects(clone $expects)
  231. ->method('sendMessage');
  232. $this->model->credentialsChanged($savedCustomer, $oldEmail, $isPasswordChanged);
  233. }
  234. /**
  235. * @return array
  236. */
  237. public function sendNotificationEmailsDataProvider()
  238. {
  239. return [
  240. [
  241. 'test_number' => 1,
  242. 'old_email' => 'test@example.com',
  243. 'new_email' => 'test@example.com',
  244. 'is_password_changed' => true
  245. ],
  246. [
  247. 'test_number' => 2,
  248. 'old_email' => 'test1@example.com',
  249. 'new_email' => 'test2@example.com',
  250. 'is_password_changed' => false
  251. ],
  252. [
  253. 'test_number' => 3,
  254. 'old_email' => 'test1@example.com',
  255. 'new_email' => 'test2@example.com',
  256. 'is_password_changed' => true
  257. ]
  258. ];
  259. }
  260. /**
  261. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  262. */
  263. public function testPasswordReminder()
  264. {
  265. $customerId = 1;
  266. $customerWebsiteId = 1;
  267. $customerStoreId = 2;
  268. $customerEmail = 'email@email.com';
  269. $customerData = ['key' => 'value'];
  270. $customerName = 'Customer Name';
  271. $templateIdentifier = 'Template Identifier';
  272. $sender = 'Sender';
  273. $senderValues = ['name' => $sender, 'email' => $sender];
  274. $storeIds = [1, 2];
  275. $this->senderResolverMock
  276. ->expects($this->once())
  277. ->method('resolve')
  278. ->with($sender, $customerStoreId)
  279. ->willReturn($senderValues);
  280. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  281. $customer = $this->createMock(CustomerInterface::class);
  282. $customer->expects($this->any())
  283. ->method('getWebsiteId')
  284. ->willReturn($customerWebsiteId);
  285. $customer->expects($this->any())
  286. ->method('getStoreId')
  287. ->willReturn($customerStoreId);
  288. $customer->expects($this->any())
  289. ->method('getId')
  290. ->willReturn($customerId);
  291. $customer->expects($this->any())
  292. ->method('getEmail')
  293. ->willReturn($customerEmail);
  294. $this->storeMock->expects($this->any())
  295. ->method('getId')
  296. ->willReturn($customerStoreId);
  297. $this->storeManagerMock->expects($this->at(0))
  298. ->method('getStore')
  299. ->willReturn($this->storeMock);
  300. $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
  301. $websiteMock->expects($this->any())
  302. ->method('getStoreIds')
  303. ->willReturn($storeIds);
  304. $this->storeManagerMock->expects($this->any())
  305. ->method('getWebsite')
  306. ->with($customerWebsiteId)
  307. ->willReturn($websiteMock);
  308. $this->customerRegistryMock->expects($this->once())
  309. ->method('retrieveSecureData')
  310. ->with($customerId)
  311. ->willReturn($this->customerSecureMock);
  312. $this->dataProcessorMock->expects($this->once())
  313. ->method('buildOutputDataArray')
  314. ->with($customer, CustomerInterface::class)
  315. ->willReturn($customerData);
  316. $this->customerViewHelperMock->expects($this->any())
  317. ->method('getCustomerName')
  318. ->with($customer)
  319. ->willReturn($customerName);
  320. $this->customerSecureMock->expects($this->once())
  321. ->method('addData')
  322. ->with($customerData)
  323. ->willReturnSelf();
  324. $this->customerSecureMock->expects($this->once())
  325. ->method('setData')
  326. ->with('name', $customerName)
  327. ->willReturnSelf();
  328. $this->scopeConfigMock->expects($this->at(0))
  329. ->method('getValue')
  330. ->with(EmailNotification::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
  331. ->willReturn($templateIdentifier);
  332. $this->scopeConfigMock->expects($this->at(1))
  333. ->method('getValue')
  334. ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
  335. ->willReturn($sender);
  336. $this->mockDefaultTransportBuilder(
  337. $templateIdentifier,
  338. $customerStoreId,
  339. $senderValues,
  340. $customerEmail,
  341. $customerName,
  342. ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
  343. );
  344. $this->model->passwordReminder($customer);
  345. }
  346. /**
  347. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  348. */
  349. public function testPasswordReminderCustomerWithoutStoreId()
  350. {
  351. $customerId = 1;
  352. $customerWebsiteId = 1;
  353. $customerStoreId = null;
  354. $customerEmail = 'email@email.com';
  355. $customerData = ['key' => 'value'];
  356. $customerName = 'Customer Name';
  357. $templateIdentifier = 'Template Identifier';
  358. $sender = 'Sender';
  359. $senderValues = ['name' => $sender, 'email' => $sender];
  360. $storeIds = [1, 2];
  361. $defaultStoreId = reset($storeIds);
  362. $this->senderResolverMock
  363. ->expects($this->once())
  364. ->method('resolve')
  365. ->with($sender, $defaultStoreId)
  366. ->willReturn($senderValues);
  367. /** @var CustomerInterface | \PHPUnit_Framework_MockObject_MockObject $customer */
  368. $customer = $this->createMock(CustomerInterface::class);
  369. $customer->expects($this->any())
  370. ->method('getWebsiteId')
  371. ->willReturn($customerWebsiteId);
  372. $customer->expects($this->any())
  373. ->method('getStoreId')
  374. ->willReturn($customerStoreId);
  375. $customer->expects($this->any())
  376. ->method('getId')
  377. ->willReturn($customerId);
  378. $customer->expects($this->any())
  379. ->method('getEmail')
  380. ->willReturn($customerEmail);
  381. $this->storeMock->expects($this->any())
  382. ->method('getId')
  383. ->willReturn($defaultStoreId);
  384. $this->storeManagerMock->expects($this->at(0))
  385. ->method('getStore')
  386. ->willReturn($this->storeMock);
  387. $this->storeManagerMock->expects($this->at(1))
  388. ->method('getStore')
  389. ->with($defaultStoreId)
  390. ->willReturn($this->storeMock);
  391. $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
  392. $websiteMock->expects($this->any())
  393. ->method('getStoreIds')
  394. ->willReturn($storeIds);
  395. $this->storeManagerMock->expects($this->any())
  396. ->method('getWebsite')
  397. ->with($customerWebsiteId)
  398. ->willReturn($websiteMock);
  399. $this->customerRegistryMock->expects($this->once())
  400. ->method('retrieveSecureData')
  401. ->with($customerId)
  402. ->willReturn($this->customerSecureMock);
  403. $this->dataProcessorMock->expects($this->once())
  404. ->method('buildOutputDataArray')
  405. ->with($customer, CustomerInterface::class)
  406. ->willReturn($customerData);
  407. $this->customerViewHelperMock->expects($this->any())
  408. ->method('getCustomerName')
  409. ->with($customer)
  410. ->willReturn($customerName);
  411. $this->customerSecureMock->expects($this->once())
  412. ->method('addData')
  413. ->with($customerData)
  414. ->willReturnSelf();
  415. $this->customerSecureMock->expects($this->once())
  416. ->method('setData')
  417. ->with('name', $customerName)
  418. ->willReturnSelf();
  419. $this->scopeConfigMock->expects($this->at(0))
  420. ->method('getValue')
  421. ->with(EmailNotification::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId)
  422. ->willReturn($templateIdentifier);
  423. $this->scopeConfigMock->expects($this->at(1))
  424. ->method('getValue')
  425. ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $defaultStoreId)
  426. ->willReturn($sender);
  427. $this->mockDefaultTransportBuilder(
  428. $templateIdentifier,
  429. $defaultStoreId,
  430. $senderValues,
  431. $customerEmail,
  432. $customerName,
  433. ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
  434. );
  435. $this->model->passwordReminder($customer);
  436. }
  437. /**
  438. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  439. */
  440. public function testPasswordResetConfirmation()
  441. {
  442. $customerId = 1;
  443. $customerStoreId = 2;
  444. $customerEmail = 'email@email.com';
  445. $customerData = ['key' => 'value'];
  446. $customerName = 'Customer Name';
  447. $templateIdentifier = 'Template Identifier';
  448. $sender = 'Sender';
  449. $senderValues = ['name' => $sender, 'email' => $sender];
  450. $this->senderResolverMock
  451. ->expects($this->once())
  452. ->method('resolve')
  453. ->with($sender, $customerStoreId)
  454. ->willReturn($senderValues);
  455. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  456. $customer = $this->createMock(CustomerInterface::class);
  457. $customer->expects($this->any())
  458. ->method('getStoreId')
  459. ->willReturn($customerStoreId);
  460. $customer->expects($this->any())
  461. ->method('getId')
  462. ->willReturn($customerId);
  463. $customer->expects($this->any())
  464. ->method('getEmail')
  465. ->willReturn($customerEmail);
  466. $this->storeMock->expects($this->any())
  467. ->method('getId')
  468. ->willReturn($customerStoreId);
  469. $this->storeManagerMock->expects($this->at(0))
  470. ->method('getStore')
  471. ->willReturn($this->storeMock);
  472. $this->storeManagerMock->expects($this->at(1))
  473. ->method('getStore')
  474. ->with($customerStoreId)
  475. ->willReturn($this->storeMock);
  476. $this->customerRegistryMock->expects($this->once())
  477. ->method('retrieveSecureData')
  478. ->with($customerId)
  479. ->willReturn($this->customerSecureMock);
  480. $this->dataProcessorMock->expects($this->once())
  481. ->method('buildOutputDataArray')
  482. ->with($customer, CustomerInterface::class)
  483. ->willReturn($customerData);
  484. $this->customerViewHelperMock->expects($this->any())
  485. ->method('getCustomerName')
  486. ->with($customer)
  487. ->willReturn($customerName);
  488. $this->customerSecureMock->expects($this->once())
  489. ->method('addData')
  490. ->with($customerData)
  491. ->willReturnSelf();
  492. $this->customerSecureMock->expects($this->once())
  493. ->method('setData')
  494. ->with('name', $customerName)
  495. ->willReturnSelf();
  496. $this->scopeConfigMock->expects($this->at(0))
  497. ->method('getValue')
  498. ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
  499. ->willReturn($templateIdentifier);
  500. $this->scopeConfigMock->expects($this->at(1))
  501. ->method('getValue')
  502. ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
  503. ->willReturn($sender);
  504. $this->mockDefaultTransportBuilder(
  505. $templateIdentifier,
  506. $customerStoreId,
  507. $senderValues,
  508. $customerEmail,
  509. $customerName,
  510. ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
  511. );
  512. $this->model->passwordResetConfirmation($customer);
  513. }
  514. /**
  515. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  516. */
  517. public function testNewAccount()
  518. {
  519. $customerId = 1;
  520. $customerStoreId = 2;
  521. $customerEmail = 'email@email.com';
  522. $customerData = ['key' => 'value'];
  523. $customerName = 'Customer Name';
  524. $templateIdentifier = 'Template Identifier';
  525. $sender = 'Sender';
  526. $senderValues = ['name' => $sender, 'email' => $sender];
  527. $this->senderResolverMock
  528. ->expects($this->once())
  529. ->method('resolve')
  530. ->with($sender, $customerStoreId)
  531. ->willReturn($senderValues);
  532. /** @var CustomerInterface|\PHPUnit_Framework_MockObject_MockObject $customer */
  533. $customer = $this->createMock(CustomerInterface::class);
  534. $customer->expects($this->any())
  535. ->method('getStoreId')
  536. ->willReturn($customerStoreId);
  537. $customer->expects($this->any())
  538. ->method('getId')
  539. ->willReturn($customerId);
  540. $customer->expects($this->any())
  541. ->method('getEmail')
  542. ->willReturn($customerEmail);
  543. $this->storeMock->expects($this->any())
  544. ->method('getId')
  545. ->willReturn($customerStoreId);
  546. $this->storeManagerMock->expects($this->once())
  547. ->method('getStore')
  548. ->with($customerStoreId)
  549. ->willReturn($this->storeMock);
  550. $this->customerRegistryMock->expects($this->once())
  551. ->method('retrieveSecureData')
  552. ->with($customerId)
  553. ->willReturn($this->customerSecureMock);
  554. $this->dataProcessorMock->expects($this->once())
  555. ->method('buildOutputDataArray')
  556. ->with($customer, CustomerInterface::class)
  557. ->willReturn($customerData);
  558. $this->customerViewHelperMock->expects($this->any())
  559. ->method('getCustomerName')
  560. ->with($customer)
  561. ->willReturn($customerName);
  562. $this->customerSecureMock->expects($this->once())
  563. ->method('addData')
  564. ->with($customerData)
  565. ->willReturnSelf();
  566. $this->customerSecureMock->expects($this->once())
  567. ->method('setData')
  568. ->with('name', $customerName)
  569. ->willReturnSelf();
  570. $this->scopeConfigMock->expects($this->at(0))
  571. ->method('getValue')
  572. ->with(EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
  573. ->willReturn($templateIdentifier);
  574. $this->scopeConfigMock->expects($this->at(1))
  575. ->method('getValue')
  576. ->with(EmailNotification::XML_PATH_REGISTER_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
  577. ->willReturn($sender);
  578. $this->mockDefaultTransportBuilder(
  579. $templateIdentifier,
  580. $customerStoreId,
  581. $senderValues,
  582. $customerEmail,
  583. $customerName,
  584. ['customer' => $this->customerSecureMock, 'back_url' => '', 'store' => $this->storeMock]
  585. );
  586. $this->model->newAccount($customer, EmailNotification::NEW_ACCOUNT_EMAIL_REGISTERED, '', $customerStoreId);
  587. }
  588. /**
  589. * Create default mock for $this->transportBuilderMock.
  590. *
  591. * @param string $templateIdentifier
  592. * @param int $customerStoreId
  593. * @param array $senderValues
  594. * @param string $customerEmail
  595. * @param string $customerName
  596. * @param array $templateVars
  597. *
  598. * @return void
  599. */
  600. private function mockDefaultTransportBuilder(
  601. string $templateIdentifier,
  602. int $customerStoreId,
  603. array $senderValues,
  604. string $customerEmail,
  605. string $customerName,
  606. array $templateVars = []
  607. ): void {
  608. $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
  609. $this->transportBuilderMock->expects($this->once())
  610. ->method('setTemplateIdentifier')
  611. ->with($templateIdentifier)
  612. ->willReturnSelf();
  613. $this->transportBuilderMock->expects($this->once())
  614. ->method('setTemplateOptions')
  615. ->with(['area' => Area::AREA_FRONTEND, 'store' => $customerStoreId])
  616. ->willReturnSelf();
  617. $this->transportBuilderMock->expects($this->once())
  618. ->method('setTemplateVars')
  619. ->with($templateVars)
  620. ->willReturnSelf();
  621. $this->transportBuilderMock->expects($this->once())
  622. ->method('setFrom')
  623. ->with($senderValues)
  624. ->willReturnSelf();
  625. $this->transportBuilderMock->expects($this->once())
  626. ->method('addTo')
  627. ->with($customerEmail, $customerName)
  628. ->willReturnSelf();
  629. $this->transportBuilderMock->expects($this->once())
  630. ->method('getTransport')
  631. ->willReturn($transport);
  632. $transport->expects($this->once())
  633. ->method('sendMessage');
  634. }
  635. }