Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EmailNotificationTest.php
Go to the documentation of this file.
1 <?php
8 
14 
20 class EmailNotificationTest extends \PHPUnit\Framework\TestCase
21 {
25  private $customerRegistryMock;
26 
30  private $storeManagerMock;
31 
35  private $transportBuilderMock;
36 
40  private $customerViewHelperMock;
41 
45  private $dataProcessorMock;
46 
50  private $customerSecureMock;
51 
55  private $scopeConfigMock;
56 
60  private $storeMock;
61 
65  private $model;
66 
70  private $senderResolverMock;
71 
72  public function setUp()
73  {
74  $this->customerRegistryMock = $this->createMock(\Magento\Customer\Model\CustomerRegistry::class);
75 
76  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
77 
78  $this->transportBuilderMock = $this->createMock(\Magento\Framework\Mail\Template\TransportBuilder::class);
79 
80  $this->customerViewHelperMock = $this->createMock(\Magento\Customer\Helper\View::class);
81 
82  $this->dataProcessorMock = $this->createMock(\Magento\Framework\Reflection\DataObjectProcessor::class);
83 
84  $contextMock = $this->createPartialMock(\Magento\Framework\App\Helper\Context::class, ['getScopeConfig']);
85 
86  $this->scopeConfigMock = $this->createPartialMock(
87  \Magento\Framework\App\Config\ScopeConfigInterface::class,
88  ['getValue', 'isSetFlag']
89  );
90 
91  $contextMock->expects($this->any())
92  ->method('getScopeConfig')
93  ->willReturn($this->scopeConfigMock);
94 
95  $this->customerSecureMock = $this->createMock(\Magento\Customer\Model\Data\CustomerSecure::class);
96 
97  $this->storeMock = $this->createMock(\Magento\Store\Model\Store::class);
98 
99  $this->senderResolverMock = $this->getMockBuilder(SenderResolverInterface::class)
100  ->setMethods(['resolve'])
101  ->disableOriginalConstructor()
102  ->getMockForAbstractClass();
103 
104  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
105 
106  $this->model = $objectManager->getObject(
107  EmailNotification::class,
108  [
109  'customerRegistry' => $this->customerRegistryMock,
110  'storeManager' => $this->storeManagerMock,
111  'transportBuilder' => $this->transportBuilderMock,
112  'customerViewHelper' => $this->customerViewHelperMock,
113  'dataProcessor' => $this->dataProcessorMock,
114  'scopeConfig' => $this->scopeConfigMock,
115  'senderResolver' => $this->senderResolverMock,
116  ]
117  );
118  }
119 
129  public function testCredentialsChanged($testNumber, $oldEmail, $newEmail, $isPasswordChanged)
130  {
131  $customerId = 1;
132  $customerStoreId = 2;
133  $customerWebsiteId = 1;
134  $customerData = ['key' => 'value'];
135  $customerName = 'Customer Name';
136  $templateIdentifier = 'Template Identifier';
137  $sender = 'Sender';
138  $senderValues = ['name' => $sender, 'email' => $sender];
139 
140  $expects = $this->once();
142  switch ($testNumber) {
143  case 1:
145  $expects = $this->once();
146  break;
147  case 2:
149  $expects = $this->exactly(2);
150  break;
151  case 3:
153  $expects = $this->exactly(2);
154  break;
155  }
156 
157  $this->senderResolverMock
158  ->expects($expects)
159  ->method('resolve')
160  ->with($sender, $customerStoreId)
161  ->willReturn($senderValues);
162 
164  $origCustomer = $this->createMock(CustomerInterface::class);
165  $origCustomer->expects($this->any())
166  ->method('getStoreId')
167  ->willReturn(0);
168  $origCustomer->expects($this->any())
169  ->method('getId')
170  ->willReturn($customerId);
171  $origCustomer->expects($this->any())
172  ->method('getWebsiteId')
173  ->willReturn($customerWebsiteId);
174 
175  $storeMock = $this->createMock(\Magento\Store\Model\Store::class);
176  $storeMock->expects($this->any())
177  ->method('getId')
178  ->willReturn($customerStoreId);
179 
180  $this->storeManagerMock->expects(clone $expects)
181  ->method('getStore')
182  ->willReturn($storeMock);
183 
184  $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
185  $websiteMock->expects($this->any())
186  ->method('getStoreIds')
187  ->willReturn([$customerStoreId]);
188 
189  $this->storeManagerMock->expects(clone $expects)
190  ->method('getWebsite')
191  ->with($customerWebsiteId)
192  ->willReturn($websiteMock);
193 
194  $customerSecureMock = $this->createMock(\Magento\Customer\Model\Data\CustomerSecure::class);
195  $this->customerRegistryMock->expects(clone $expects)
196  ->method('retrieveSecureData')
197  ->with($customerId)
198  ->willReturn($customerSecureMock);
199 
200  $this->dataProcessorMock->expects(clone $expects)
201  ->method('buildOutputDataArray')
202  ->with($origCustomer, CustomerInterface::class)
203  ->willReturn($customerData);
204 
205  $this->customerViewHelperMock->expects($this->any())
206  ->method('getCustomerName')
207  ->with($origCustomer)
208  ->willReturn($customerName);
209 
210  $customerSecureMock->expects(clone $expects)
211  ->method('addData')
212  ->with($customerData)
213  ->willReturnSelf();
214  $customerSecureMock->expects(clone $expects)
215  ->method('setData')
216  ->with('name', $customerName)
217  ->willReturnSelf();
218 
220  $savedCustomer = clone $origCustomer;
221 
222  $origCustomer->expects($this->any())
223  ->method('getEmail')
224  ->willReturn($oldEmail);
225 
226  $savedCustomer->expects($this->any())
227  ->method('getEmail')
228  ->willReturn($newEmail);
229 
230  $this->scopeConfigMock->expects($this->any())
231  ->method('getValue')
232  ->withConsecutive(
233  [$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId],
234  [
236  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
237  $customerStoreId
238  ],
239  [$xmlPathTemplate, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $customerStoreId],
240  [
242  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
243  $customerStoreId
244  ]
245  )
246  ->willReturnOnConsecutiveCalls($templateIdentifier, $sender, $templateIdentifier, $sender);
247 
248  $this->transportBuilderMock->expects(clone $expects)
249  ->method('setTemplateIdentifier')
250  ->with($templateIdentifier)
251  ->willReturnSelf();
252  $this->transportBuilderMock->expects(clone $expects)
253  ->method('setTemplateOptions')
254  ->with(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $customerStoreId])
255  ->willReturnSelf();
256  $this->transportBuilderMock->expects(clone $expects)
257  ->method('setTemplateVars')
258  ->with(['customer' => $customerSecureMock, 'store' => $storeMock])
259  ->willReturnSelf();
260  $this->transportBuilderMock->expects(clone $expects)
261  ->method('setFrom')
262  ->with($senderValues)
263  ->willReturnSelf();
264 
265  $this->transportBuilderMock->expects(clone $expects)
266  ->method('addTo')
267  ->withConsecutive([$oldEmail, $customerName], [$newEmail, $customerName])
268  ->willReturnSelf();
269 
270  $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
271 
272  $this->transportBuilderMock->expects(clone $expects)
273  ->method('getTransport')
274  ->willReturn($transport);
275 
276  $transport->expects(clone $expects)
277  ->method('sendMessage');
278 
279  $this->model->credentialsChanged($savedCustomer, $oldEmail, $isPasswordChanged);
280  }
281 
286  {
287  return [
288  [
289  'test_number' => 1,
290  'old_email' => '[email protected]',
291  'new_email' => '[email protected]',
292  'is_password_changed' => true
293  ],
294  [
295  'test_number' => 2,
296  'old_email' => '[email protected]',
297  'new_email' => '[email protected]',
298  'is_password_changed' => false
299  ],
300  [
301  'test_number' => 3,
302  'old_email' => '[email protected]',
303  'new_email' => '[email protected]',
304  'is_password_changed' => true
305  ]
306  ];
307  }
308 
312  public function testPasswordReminder()
313  {
314  $customerId = 1;
315  $customerWebsiteId = 1;
316  $customerStoreId = 2;
318  $customerData = ['key' => 'value'];
319  $customerName = 'Customer Name';
320  $templateIdentifier = 'Template Identifier';
321  $sender = 'Sender';
322  $senderValues = ['name' => $sender, 'email' => $sender];
323  $storeIds = [1, 2];
324 
325  $this->senderResolverMock
326  ->expects($this->once())
327  ->method('resolve')
328  ->with($sender, $customerStoreId)
329  ->willReturn($senderValues);
330 
332  $customer = $this->createMock(CustomerInterface::class);
333  $customer->expects($this->any())
334  ->method('getWebsiteId')
335  ->willReturn($customerWebsiteId);
336  $customer->expects($this->any())
337  ->method('getStoreId')
338  ->willReturn($customerStoreId);
339  $customer->expects($this->any())
340  ->method('getId')
341  ->willReturn($customerId);
342  $customer->expects($this->any())
343  ->method('getEmail')
344  ->willReturn($customerEmail);
345 
346  $this->storeMock->expects($this->any())
347  ->method('getId')
348  ->willReturn($customerStoreId);
349 
350  $this->storeManagerMock->expects($this->at(0))
351  ->method('getStore')
352  ->willReturn($this->storeMock);
353 
354  $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
355  $websiteMock->expects($this->any())
356  ->method('getStoreIds')
357  ->willReturn($storeIds);
358 
359  $this->storeManagerMock->expects($this->any())
360  ->method('getWebsite')
361  ->with($customerWebsiteId)
362  ->willReturn($websiteMock);
363 
364  $this->customerRegistryMock->expects($this->once())
365  ->method('retrieveSecureData')
366  ->with($customerId)
367  ->willReturn($this->customerSecureMock);
368 
369  $this->dataProcessorMock->expects($this->once())
370  ->method('buildOutputDataArray')
371  ->with($customer, CustomerInterface::class)
372  ->willReturn($customerData);
373 
374  $this->customerViewHelperMock->expects($this->any())
375  ->method('getCustomerName')
376  ->with($customer)
377  ->willReturn($customerName);
378 
379  $this->customerSecureMock->expects($this->once())
380  ->method('addData')
381  ->with($customerData)
382  ->willReturnSelf();
383  $this->customerSecureMock->expects($this->once())
384  ->method('setData')
385  ->with('name', $customerName)
386  ->willReturnSelf();
387 
388  $this->scopeConfigMock->expects($this->at(0))
389  ->method('getValue')
390  ->with(EmailNotification::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
391  ->willReturn($templateIdentifier);
392  $this->scopeConfigMock->expects($this->at(1))
393  ->method('getValue')
394  ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
395  ->willReturn($sender);
396 
397  $this->mockDefaultTransportBuilder(
398  $templateIdentifier,
399  $customerStoreId,
400  $senderValues,
402  $customerName,
403  ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
404  );
405 
406  $this->model->passwordReminder($customer);
407  }
408 
412  public function testPasswordReminderCustomerWithoutStoreId()
413  {
414  $customerId = 1;
415  $customerWebsiteId = 1;
416  $customerStoreId = null;
418  $customerData = ['key' => 'value'];
419  $customerName = 'Customer Name';
420  $templateIdentifier = 'Template Identifier';
421  $sender = 'Sender';
422  $senderValues = ['name' => $sender, 'email' => $sender];
423  $storeIds = [1, 2];
424  $defaultStoreId = reset($storeIds);
425  $this->senderResolverMock
426  ->expects($this->once())
427  ->method('resolve')
428  ->with($sender, $defaultStoreId)
429  ->willReturn($senderValues);
431  $customer = $this->createMock(CustomerInterface::class);
432  $customer->expects($this->any())
433  ->method('getWebsiteId')
434  ->willReturn($customerWebsiteId);
435  $customer->expects($this->any())
436  ->method('getStoreId')
437  ->willReturn($customerStoreId);
438  $customer->expects($this->any())
439  ->method('getId')
440  ->willReturn($customerId);
441  $customer->expects($this->any())
442  ->method('getEmail')
443  ->willReturn($customerEmail);
444  $this->storeMock->expects($this->any())
445  ->method('getId')
446  ->willReturn($defaultStoreId);
447  $this->storeManagerMock->expects($this->at(0))
448  ->method('getStore')
449  ->willReturn($this->storeMock);
450  $this->storeManagerMock->expects($this->at(1))
451  ->method('getStore')
452  ->with($defaultStoreId)
453  ->willReturn($this->storeMock);
454  $websiteMock = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
455  $websiteMock->expects($this->any())
456  ->method('getStoreIds')
457  ->willReturn($storeIds);
458  $this->storeManagerMock->expects($this->any())
459  ->method('getWebsite')
460  ->with($customerWebsiteId)
461  ->willReturn($websiteMock);
462 
463  $this->customerRegistryMock->expects($this->once())
464  ->method('retrieveSecureData')
465  ->with($customerId)
466  ->willReturn($this->customerSecureMock);
467  $this->dataProcessorMock->expects($this->once())
468  ->method('buildOutputDataArray')
469  ->with($customer, CustomerInterface::class)
470  ->willReturn($customerData);
471  $this->customerViewHelperMock->expects($this->any())
472  ->method('getCustomerName')
473  ->with($customer)
474  ->willReturn($customerName);
475  $this->customerSecureMock->expects($this->once())
476  ->method('addData')
477  ->with($customerData)
478  ->willReturnSelf();
479  $this->customerSecureMock->expects($this->once())
480  ->method('setData')
481  ->with('name', $customerName)
482  ->willReturnSelf();
483  $this->scopeConfigMock->expects($this->at(0))
484  ->method('getValue')
485  ->with(EmailNotification::XML_PATH_REMIND_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $defaultStoreId)
486  ->willReturn($templateIdentifier);
487  $this->scopeConfigMock->expects($this->at(1))
488  ->method('getValue')
489  ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $defaultStoreId)
490  ->willReturn($sender);
491  $this->mockDefaultTransportBuilder(
492  $templateIdentifier,
493  $defaultStoreId,
494  $senderValues,
496  $customerName,
497  ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
498  );
499  $this->model->passwordReminder($customer);
500  }
501 
505  public function testPasswordResetConfirmation()
506  {
507  $customerId = 1;
508  $customerStoreId = 2;
510  $customerData = ['key' => 'value'];
511  $customerName = 'Customer Name';
512  $templateIdentifier = 'Template Identifier';
513  $sender = 'Sender';
514  $senderValues = ['name' => $sender, 'email' => $sender];
515 
516  $this->senderResolverMock
517  ->expects($this->once())
518  ->method('resolve')
519  ->with($sender, $customerStoreId)
520  ->willReturn($senderValues);
521 
523  $customer = $this->createMock(CustomerInterface::class);
524  $customer->expects($this->any())
525  ->method('getStoreId')
526  ->willReturn($customerStoreId);
527  $customer->expects($this->any())
528  ->method('getId')
529  ->willReturn($customerId);
530  $customer->expects($this->any())
531  ->method('getEmail')
532  ->willReturn($customerEmail);
533 
534  $this->storeMock->expects($this->any())
535  ->method('getId')
536  ->willReturn($customerStoreId);
537 
538  $this->storeManagerMock->expects($this->at(0))
539  ->method('getStore')
540  ->willReturn($this->storeMock);
541 
542  $this->storeManagerMock->expects($this->at(1))
543  ->method('getStore')
544  ->with($customerStoreId)
545  ->willReturn($this->storeMock);
546 
547  $this->customerRegistryMock->expects($this->once())
548  ->method('retrieveSecureData')
549  ->with($customerId)
550  ->willReturn($this->customerSecureMock);
551 
552  $this->dataProcessorMock->expects($this->once())
553  ->method('buildOutputDataArray')
554  ->with($customer, CustomerInterface::class)
555  ->willReturn($customerData);
556 
557  $this->customerViewHelperMock->expects($this->any())
558  ->method('getCustomerName')
559  ->with($customer)
560  ->willReturn($customerName);
561 
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 
571  $this->scopeConfigMock->expects($this->at(0))
572  ->method('getValue')
573  ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
574  ->willReturn($templateIdentifier);
575  $this->scopeConfigMock->expects($this->at(1))
576  ->method('getValue')
577  ->with(EmailNotification::XML_PATH_FORGOT_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
578  ->willReturn($sender);
579 
580  $this->mockDefaultTransportBuilder(
581  $templateIdentifier,
582  $customerStoreId,
583  $senderValues,
585  $customerName,
586  ['customer' => $this->customerSecureMock, 'store' => $this->storeMock]
587  );
588 
589  $this->model->passwordResetConfirmation($customer);
590  }
591 
595  public function testNewAccount()
596  {
597  $customerId = 1;
598  $customerStoreId = 2;
600  $customerData = ['key' => 'value'];
601  $customerName = 'Customer Name';
602  $templateIdentifier = 'Template Identifier';
603  $sender = 'Sender';
604  $senderValues = ['name' => $sender, 'email' => $sender];
605 
606  $this->senderResolverMock
607  ->expects($this->once())
608  ->method('resolve')
609  ->with($sender, $customerStoreId)
610  ->willReturn($senderValues);
611 
613  $customer = $this->createMock(CustomerInterface::class);
614  $customer->expects($this->any())
615  ->method('getStoreId')
616  ->willReturn($customerStoreId);
617  $customer->expects($this->any())
618  ->method('getId')
619  ->willReturn($customerId);
620  $customer->expects($this->any())
621  ->method('getEmail')
622  ->willReturn($customerEmail);
623 
624  $this->storeMock->expects($this->any())
625  ->method('getId')
626  ->willReturn($customerStoreId);
627 
628  $this->storeManagerMock->expects($this->once())
629  ->method('getStore')
630  ->with($customerStoreId)
631  ->willReturn($this->storeMock);
632 
633  $this->customerRegistryMock->expects($this->once())
634  ->method('retrieveSecureData')
635  ->with($customerId)
636  ->willReturn($this->customerSecureMock);
637 
638  $this->dataProcessorMock->expects($this->once())
639  ->method('buildOutputDataArray')
640  ->with($customer, CustomerInterface::class)
641  ->willReturn($customerData);
642 
643  $this->customerViewHelperMock->expects($this->any())
644  ->method('getCustomerName')
645  ->with($customer)
646  ->willReturn($customerName);
647 
648  $this->customerSecureMock->expects($this->once())
649  ->method('addData')
650  ->with($customerData)
651  ->willReturnSelf();
652  $this->customerSecureMock->expects($this->once())
653  ->method('setData')
654  ->with('name', $customerName)
655  ->willReturnSelf();
656 
657  $this->scopeConfigMock->expects($this->at(0))
658  ->method('getValue')
659  ->with(EmailNotification::XML_PATH_REGISTER_EMAIL_TEMPLATE, ScopeInterface::SCOPE_STORE, $customerStoreId)
660  ->willReturn($templateIdentifier);
661  $this->scopeConfigMock->expects($this->at(1))
662  ->method('getValue')
663  ->with(EmailNotification::XML_PATH_REGISTER_EMAIL_IDENTITY, ScopeInterface::SCOPE_STORE, $customerStoreId)
664  ->willReturn($sender);
665 
666  $this->mockDefaultTransportBuilder(
667  $templateIdentifier,
668  $customerStoreId,
669  $senderValues,
671  $customerName,
672  ['customer' => $this->customerSecureMock, 'back_url' => '', 'store' => $this->storeMock]
673  );
674 
675  $this->model->newAccount($customer, EmailNotification::NEW_ACCOUNT_EMAIL_REGISTERED, '', $customerStoreId);
676  }
677 
690  private function mockDefaultTransportBuilder(
691  string $templateIdentifier,
692  int $customerStoreId,
693  array $senderValues,
694  string $customerEmail,
695  string $customerName,
696  array $templateVars = []
697  ): void {
698  $transport = $this->createMock(\Magento\Framework\Mail\TransportInterface::class);
699 
700  $this->transportBuilderMock->expects($this->once())
701  ->method('setTemplateIdentifier')
702  ->with($templateIdentifier)
703  ->willReturnSelf();
704  $this->transportBuilderMock->expects($this->once())
705  ->method('setTemplateOptions')
706  ->with(['area' => Area::AREA_FRONTEND, 'store' => $customerStoreId])
707  ->willReturnSelf();
708  $this->transportBuilderMock->expects($this->once())
709  ->method('setTemplateVars')
710  ->with($templateVars)
711  ->willReturnSelf();
712  $this->transportBuilderMock->expects($this->once())
713  ->method('setFrom')
714  ->with($senderValues)
715  ->willReturnSelf();
716  $this->transportBuilderMock->expects($this->once())
717  ->method('addTo')
718  ->with($customerEmail, $customerName)
719  ->willReturnSelf();
720  $this->transportBuilderMock->expects($this->once())
721  ->method('getTransport')
722  ->willReturn($transport);
723 
724  $transport->expects($this->once())
725  ->method('sendMessage');
726  }
727 }
$customerData
$objectManager
Definition: bootstrap.php:17
$customer
Definition: customers.php:11
return false
Definition: gallery.phtml:36