Skip to content

Commit 2afabfa

Browse files
committed
MC-38122: Admin: New user notification email: Custom template not picked up
1 parent f55f411 commit 2afabfa

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

app/code/Magento/User/etc/adminhtml/system.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<comment>Email template chosen based on theme fallback when "Default" option is selected.</comment>
1515
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
1616
</field>
17+
<field id="new_user_notification_template" translate="label comment" type="select" sortOrder="50" showInDefault="1" canRestore="1">
18+
<label>New User Notification Template</label>
19+
<comment>Email template chosen based on theme fallback when "Default" option is selected.</comment>
20+
<source_model>Magento\Config\Model\Config\Source\Email\Template</source_model>
21+
</field>
1722
</group>
1823
<group id="security">
1924
<field id="lockout_failures" translate="label comment" sortOrder="100" showInDefault="1" canRestore="1">
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
8+
/** @var \Magento\Email\Model\Template $template */
9+
$template = $objectManager->create(\Magento\Email\Model\Template::class);
10+
$template->setOptions(['area' => 'test area', 'store' => 1]);
11+
$template->setData(
12+
[
13+
'template_text' => 'New User Notification Custom Text',
14+
'template_code' => 'New User Notification Custom Code',
15+
'template_type' => \Magento\Email\Model\Template::TYPE_TEXT,
16+
'orig_template_code' => 'admin_emails_new_user_notification_template'
17+
]
18+
);
19+
$template->save();

dev/tests/integration/testsuite/Magento/User/Model/UserTest.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
use Magento\Framework\Stdlib\DateTime;
1414
use Magento\TestFramework\Helper\Bootstrap;
1515
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;
1621

1722
/**
1823
* @magentoAppArea adminhtml
@@ -565,4 +570,68 @@ public function testPerformIdentityCheckLockExpires()
565570
. 'Please wait and try again later.'
566571
);
567572
}
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+
}
568637
}

0 commit comments

Comments
 (0)