|
13 | 13 | use Magento\Framework\Stdlib\DateTime;
|
14 | 14 | use Magento\TestFramework\Helper\Bootstrap;
|
15 | 15 | use Magento\User\Model\User as UserModel;
|
| 16 | +use Magento\Email\Model\ResourceModel\Template\Collection as TemplateCollection; |
| 17 | +use Magento\Framework\Exception\NotFoundException; |
| 18 | +use Magento\Framework\Phrase; |
| 19 | +use Magento\Framework\App\Config\MutableScopeConfigInterface; |
| 20 | +use Magento\TestFramework\Mail\Template\TransportBuilderMock; |
16 | 21 |
|
17 | 22 | /**
|
18 | 23 | * @magentoAppArea adminhtml
|
@@ -565,4 +570,68 @@ public function testPerformIdentityCheckLockExpires()
|
565 | 570 | . 'Please wait and try again later.'
|
566 | 571 | );
|
567 | 572 | }
|
| 573 | + |
| 574 | + /** |
| 575 | + * Verify custom notification is sent when new user created |
| 576 | + * |
| 577 | + * @magentoDbIsolation enabled |
| 578 | + * @magentoDataFixture Magento/Email/Model/_files/email_template_new_user_notification.php |
| 579 | + */ |
| 580 | + public function testSendNotificationEmailsIfRequired() |
| 581 | + { |
| 582 | + /** @var MutableScopeConfigInterface $config */ |
| 583 | + $config = Bootstrap::getObjectManager()->get(MutableScopeConfigInterface::class); |
| 584 | + $config->setValue( |
| 585 | + 'admin/emails/new_user_notification_template', |
| 586 | + $this->getCustomEmailTemplateIdForNewUserNotification() |
| 587 | + ); |
| 588 | + $userModel = Bootstrap::getObjectManager()->create( |
| 589 | + \Magento\User\Model\User::class |
| 590 | + ); |
| 591 | + $userModel->setFirstname( |
| 592 | + 'John' |
| 593 | + )->setLastname( |
| 594 | + 'Doe' |
| 595 | + )->setUsername( |
| 596 | + 'user2' |
| 597 | + )->setPassword( |
| 598 | + \Magento\TestFramework\Bootstrap::ADMIN_PASSWORD |
| 599 | + )->setEmail( |
| 600 | + 'user@magento.com' |
| 601 | + ); |
| 602 | + $userModel->save(); |
| 603 | + $userModel->sendNotificationEmailsIfRequired(); |
| 604 | + /** @var TransportBuilderMock $transportBuilderMock */ |
| 605 | + $transportBuilderMock = Bootstrap::getObjectManager()->get(TransportBuilderMock::class); |
| 606 | + $sentMessage = $transportBuilderMock->getSentMessage(); |
| 607 | + $this->assertSame( |
| 608 | + 'New User Notification Custom Text', |
| 609 | + $sentMessage->getBodyText() |
| 610 | + ); |
| 611 | + } |
| 612 | + |
| 613 | + /** |
| 614 | + * Return email template id for new user notification |
| 615 | + * |
| 616 | + * @return int|null |
| 617 | + * @throws NotFoundException |
| 618 | + */ |
| 619 | + private function getCustomEmailTemplateIdForNewUserNotification(): ?int |
| 620 | + { |
| 621 | + $templateId = null; |
| 622 | + $templateCollection = Bootstrap::getObjectManager()->get(TemplateCollection::class); |
| 623 | + $origTemplateCode = 'admin_emails_new_user_notification_template'; |
| 624 | + foreach ($templateCollection as $template) { |
| 625 | + if ($template->getOrigTemplateCode() == $origTemplateCode) { |
| 626 | + $templateId = (int) $template->getId(); |
| 627 | + } |
| 628 | + } |
| 629 | + if ($templateId === null) { |
| 630 | + throw new NotFoundException(new Phrase( |
| 631 | + 'Customized %templateCode% email template not found', |
| 632 | + ['templateCode' => $origTemplateCode] |
| 633 | + )); |
| 634 | + } |
| 635 | + return $templateId; |
| 636 | + } |
568 | 637 | }
|
0 commit comments