Skip to content

Commit 3b51296

Browse files
akaashakaash
authored andcommitted
ACQE-4503 | Integration Testing - Create custom new account Email Templates
1 parent cd41bc2 commit 3b51296

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\Email\Model\Template;
11+
12+
use Magento\Email\Model\ResourceModel\Template\Collection as TemplateCollection;
13+
use Magento\Framework\App\Config\MutableScopeConfigInterface;
14+
use Magento\Framework\App\Config\ScopeConfigInterface;
15+
use Magento\Framework\Exception\NotFoundException;
16+
use Magento\Framework\ObjectManagerInterface;
17+
use Magento\Store\Model\ScopeInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
20+
use Magento\TestFramework\Bootstrap as TestFrameworkBootstrap;
21+
22+
23+
/**
24+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
25+
*/
26+
class NewAccountEmailTemplateTest extends \PHPUnit\Framework\TestCase
27+
{
28+
29+
/**
30+
* @var ObjectManagerInterface
31+
*/
32+
private $objectManager;
33+
34+
/**
35+
* @var ScopeConfigInterface
36+
*/
37+
private $config;
38+
39+
protected $storeData = array();
40+
41+
42+
/**
43+
* Set up
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$this->objectManager = Bootstrap::getObjectManager();
50+
$this->config = $this->objectManager->get(ScopeConfigInterface::class);
51+
$this->storeData['name'] = $this->config->getValue('general/store_information/name', ScopeInterface::SCOPE_STORES);
52+
$this->storeData['phone'] = $this->config->getValue('general/store_information/phone', ScopeInterface::SCOPE_STORES);
53+
$this->storeData['city'] = $this->config->getValue('general/store_information/city', ScopeInterface::SCOPE_STORES);
54+
$this->storeData['country'] = $this->config->getValue('general/store_information/country_id', ScopeInterface::SCOPE_STORES);
55+
}
56+
57+
58+
/**
59+
* @magentoConfigFixture current_store general/store_information/name TestStore
60+
* @magentoConfigFixture default_store general/store_information/phone 5124666492
61+
* @magentoConfigFixture default_store general/store_information/hours 10 to 2
62+
* @magentoConfigFixture default_store general/store_information/street_line1 1 Test Dr
63+
* @magentoConfigFixture default_store general/store_information/street_line2 2nd Addr Line
64+
* @magentoConfigFixture default_store general/store_information/city Austin
65+
* @magentoConfigFixture default_store general/store_information/zip 78739
66+
* @magentoConfigFixture default_store general/store_information/country_id US
67+
* @magentoConfigFixture default_store general/store_information/region_id 57
68+
* @magentoDataFixture Magento/Email/Model/_files/email_template_new_user_welcome.php
69+
*/
70+
public function testNewAccountEmailTemplate(): void
71+
{
72+
73+
/** @var MutableScopeConfigInterface $config */
74+
$config = Bootstrap::getObjectManager()
75+
->get(MutableScopeConfigInterface::class);
76+
$config->setValue(
77+
'admin/emails/new_user_notification_template',
78+
$this->getCustomEmailTemplateId(
79+
'admin_emails_new_user_notification_template'
80+
)
81+
);
82+
83+
/** @var \Magento\User\Model\User $userModel */
84+
$userModel = Bootstrap::getObjectManager()->get(\Magento\User\Model\User::class);
85+
$userModel->setFirstname(
86+
'John'
87+
)->setLastname(
88+
'Doe'
89+
)->setUsername(
90+
'user1'
91+
)->setPassword(
92+
TestFrameworkBootstrap::ADMIN_PASSWORD
93+
)->setEmail(
94+
'user1@magento.com'
95+
);
96+
$userModel->save();
97+
98+
$userModel->sendNotificationEmailsIfRequired();
99+
100+
/** @var TransportBuilderMock $transportBuilderMock */
101+
$transportBuilderMock = Bootstrap::getObjectManager()
102+
->get(TransportBuilderMock::class);
103+
$sentMessage = $transportBuilderMock->getSentMessage();
104+
$sentMessage->getBodyText();
105+
106+
$storeText = implode(',', $this->storeData);
107+
108+
$this->assertStringContainsString("John,", $sentMessage->getBodyText());
109+
$this->assertStringContainsString("TestStore", $storeText);
110+
$this->assertStringContainsString("5124666492", $storeText);
111+
$this->assertStringContainsString("Austin", $storeText);
112+
$this->assertStringContainsString("US", $storeText);
113+
114+
}
115+
116+
117+
/**
118+
* Return email template id by origin template code
119+
*
120+
* @param string $origTemplateCode
121+
* @return int|null
122+
* @throws NotFoundException
123+
*/
124+
private function getCustomEmailTemplateId(string $origTemplateCode): ?int
125+
{
126+
$templateId = null;
127+
$templateCollection = Bootstrap::getObjectManager()
128+
->create(TemplateCollection::class);
129+
foreach ($templateCollection as $template) {
130+
if ($template->getOrigTemplateCode() == $origTemplateCode) {
131+
$templateId = (int) $template->getId();
132+
}
133+
}
134+
if ($templateId === null) {
135+
throw new NotFoundException(new Phrase(
136+
'Customized %templateCode% email template not found',
137+
['templateCode' => $origTemplateCode]
138+
));
139+
}
140+
141+
return $templateId;
142+
}
143+
144+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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->setVars(['store_name' => 'TestStore']);
12+
$templateText = file_get_contents(__DIR__ . '/test.html');
13+
14+
15+
$template->setData(
16+
[
17+
'template_text' => $templateText,
18+
'template_code' => 'New User Notification Custom Code',
19+
'template_type' => \Magento\Email\Model\Template::TYPE_TEXT,
20+
'orig_template_code' => 'admin_emails_new_user_notification_template'
21+
]
22+
);
23+
$template->save();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
3+
<p class="greeting">{{trans "%first_name," first_name=$user.firstname}}</p>
4+
<p>{{trans "Welcome to %store_name." store_name=$store.getFrontendName()}}</p>
5+
<p>
6+
{{trans
7+
'To sign in to our site, use these credentials during checkout or on the <a href="%customer_url">My Account</a> page:'
8+
9+
customer_url=$this.getUrl($store,'customer/account/',[_nosid:1])
10+
|raw}}
11+
</p>
12+
<ul>
13+
<li><strong>{{trans "Email:"}}</strong> {{var customer.email}}</li>
14+
<li><strong>{{trans "Password:"}}</strong> <em>{{trans "Password you set when creating account"}}</em></li>
15+
</ul>
16+
<p>
17+
{{trans
18+
'Forgot your account password? Click <a href="%reset_url">here</a> to reset it.'
19+
20+
reset_url="$this.getUrl($store,'customer/account/createPassword/',[_query:[id:$customer.id,token:$customer.rp_token],_nosid:1])"
21+
|raw}}
22+
</p>
23+
<p>{{trans "When you sign in to your account, you will be able to:"}}</p>
24+
<ul>
25+
<li>{{trans "Proceed through checkout faster"}}</li>
26+
<li>{{trans "Check the status of orders"}}</li>
27+
<li>{{trans "View past orders"}}</li>
28+
<li>{{trans "Store alternative addresses (for shipping to multiple family members and friends)"}}</li>
29+
</ul>
30+
31+
<ul>
32+
<li><strong>{{trans "Base Unsecure URL:"}}</strong> {{config path="web/unsecure/base_url"}}</li>
33+
<li><strong>{{trans "Base Secure URL:"}}</strong> {{config path="web/secure/base_url"}}</li>
34+
<li><strong>{{trans "General Contact Name:"}}</strong>{{config path="trans_email/ident_general/name"}} </li>
35+
<li><strong>{{trans "General Contact Email:"}}</strong>{{config path="trans_email/ident_general/email"}} </li>
36+
<li><strong>{{trans "Sales Representative Contact Name:"}}</strong>{{config path="trans_email/ident_sales/name"}} </li>
37+
<li><strong>{{trans "Sales Representative Contact Email:"}}</strong>{{config path="trans_email/ident_sales/email"}} </li>
38+
<li><strong>{{trans "Store Name:"}}</strong>{{config path="general/store_information/name"}} </li>
39+
<li><strong>{{trans "Store Phone Number:"}}</strong> {{config path="general/store_information/phone"}}</li>
40+
<li><strong>{{trans "Store Hours:"}}</strong> {{config path="general/store_information/hours"}}</li>
41+
<li><strong>{{trans "Country:"}}</strong> {{config path="general/store_information/country_id"}}</li>
42+
<li><strong>{{trans "Region/State:"}}</strong>{{config path="general/store_information/region_id"}} </li>
43+
<li><strong>{{trans "Zip/Postal Code:"}}</strong>{{config path="general/store_information/postcode"}} </li>
44+
<li><strong>{{trans "City:"}}</strong> {{config path="general/store_information/city"}}</li>
45+
<li><strong>{{trans "Street Address 1:"}}</strong> {{config path="general/store_information/street_line1"}}</li>
46+
<li><strong>{{trans "Street Address 2:"}}</strong>{{config path="general/store_information/street_line2"}}</li>
47+
</ul>
48+

0 commit comments

Comments
 (0)