Skip to content

Commit ae44f6d

Browse files
[Magento Community Engineering] Community Contributions - 2.4-develop-expedited-prs
- merged with '2.4-develop-temporary-one-prs' branch
2 parents 8479d2f + 5f57f06 commit ae44f6d

File tree

4 files changed

+213
-12
lines changed

4 files changed

+213
-12
lines changed

app/code/Magento/Customer/view/frontend/email/change_email.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
<!--@vars {
99
"var store.frontend_name":"Store Name",
1010
"var store_email":"Store Email",
11-
"var store_phone":"Store Phone"
11+
"var store_phone":"Store Phone",
12+
"var customer.name":"Customer Name"
1213
} @-->
1314
{{template config_path="design/email/header_template"}}
1415

15-
<p class="greeting">{{trans "Hello,"}}</p>
16-
<br>
17-
16+
<p class="greeting">{{trans "%name," name=$customer.name}}</p>
1817
<p>
1918
{{trans "We have received a request to change the following information associated with your account at %store_name: email." store_name=$store.frontend_name}}
2019
{{trans 'If you have not authorized this action, please contact us immediately at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans 'or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.

app/code/Magento/Customer/view/frontend/email/change_email_and_password.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@
88
<!--@vars {
99
"var store.frontend_name":"Store Name",
1010
"var store_email":"Store Email",
11-
"var store_phone":"Store Phone"
11+
"var store_phone":"Store Phone",
12+
"var customer.name":"Customer Name"
1213
} @-->
1314
{{template config_path="design/email/header_template"}}
1415

15-
<p class="greeting">{{trans "Hello,"}}</p>
16-
<br>
17-
16+
<p class="greeting">{{trans "%name," name=$customer.name}}</p>
1817
<p>
1918
{{trans "We have received a request to change the following information associated with your account at %store_name: email, password." store_name=$store.frontend_name}}
2019
{{trans 'If you have not authorized this action, please contact us immediately at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans 'or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.

app/code/Magento/Customer/view/frontend/email/password_reset.html

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
"var customer.name":"Customer Name",
1010
"var store.frontend_name":"Store Name",
1111
"var store_email":"Store Email",
12-
"var store_phone":"Store Phone"
12+
"var store_phone":"Store Phone",
13+
"var customer.name":"Customer Name"
1314
} @-->
1415
{{template config_path="design/email/header_template"}}
1516

16-
<p class="greeting">{{trans "Hello,"}}</p>
17-
<br>
18-
17+
<p class="greeting">{{trans "%name," name=$customer.name}}</p>
1918
<p>
2019
{{trans "We have received a request to change the following information associated with your account at %store_name: password." store_name=$store.frontend_name}}
2120
{{trans 'If you have not authorized this action, please contact us immediately at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans 'or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Controller\Account;
9+
10+
use Magento\Customer\Model\Session;
11+
use Magento\Framework\App\Request\Http as HttpRequest;
12+
use Magento\Framework\Data\Form\FormKey;
13+
use Magento\Framework\Mail\EmailMessage;
14+
use Magento\Framework\Message\MessageInterface;
15+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
16+
use Magento\TestFramework\TestCase\AbstractController;
17+
18+
/**
19+
* Set of tests to verify e-mail templates delivered to Customers
20+
*
21+
* @magentoAppArea frontend
22+
*/
23+
class EmailTemplateTest extends AbstractController
24+
{
25+
private const FIXTURE_CUSTOMER_EMAIL = 'customer@example.com';
26+
private const FIXTURE_CUSTOMER_FIRSTNAME = 'John';
27+
private const FIXTURE_CUSTOMER_LASTNAME = 'Smith';
28+
private const FIXTURE_CUSTOMER_ID = 1;
29+
private const FIXTURE_CUSTOMER_PASSWORD = 'password';
30+
private const EXPECTED_GREETING = self::FIXTURE_CUSTOMER_FIRSTNAME . ' ' . self::FIXTURE_CUSTOMER_LASTNAME . ',';
31+
32+
/**
33+
* @var TransportBuilderMock
34+
*/
35+
private $transportBuilderMock;
36+
37+
/**
38+
* @var Session
39+
*/
40+
private $session;
41+
42+
/**
43+
* @var FormKey
44+
*/
45+
private $formKey;
46+
47+
protected function setUp()
48+
{
49+
parent::setUp();
50+
$this->transportBuilderMock = $this->_objectManager->get(TransportBuilderMock::class);
51+
$this->session = $this->_objectManager->get(Session::class);
52+
$this->formKey = $this->_objectManager->get(FormKey::class);
53+
}
54+
55+
/**
56+
* @magentoDataFixture Magento/Customer/_files/customer.php
57+
* @magentoConfigFixture current_store customer/captcha/enable 0
58+
*/
59+
public function testForgotPasswordEmailTemplateGreeting()
60+
{
61+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST)
62+
->setPostValue(['email' => self::FIXTURE_CUSTOMER_EMAIL]);
63+
$this->dispatch('customer/account/forgotPasswordPost');
64+
65+
$this->assertSameGreeting(self::EXPECTED_GREETING, $this->transportBuilderMock->getSentMessage());
66+
}
67+
68+
/**
69+
* Covers Magento_Customer::view/frontend/email/change_email.html
70+
*
71+
* @magentoDataFixture Magento/Customer/_files/customer.php
72+
* @magentoConfigFixture current_store customer/captcha/enable 0
73+
*/
74+
public function testCustomerEmailChangeNotificationTemplateGreeting()
75+
{
76+
$this->loginByCustomerId(self::FIXTURE_CUSTOMER_ID);
77+
78+
$this->sendAccountEditRequest([
79+
'email' => 'new.email@example.com',
80+
'change_email' => 1,
81+
]);
82+
83+
$this->assertRedirect($this->stringContains('customer/account/'));
84+
$this->assertSessionMessages(
85+
$this->equalTo(['You saved the account information.']),
86+
MessageInterface::TYPE_SUCCESS
87+
);
88+
89+
$this->assertSameGreeting(self::EXPECTED_GREETING, $this->transportBuilderMock->getSentMessage());
90+
}
91+
92+
/**
93+
* Covers Magento_Customer::view/frontend/email/change_email_and_password.html
94+
*
95+
* @magentoDataFixture Magento/Customer/_files/customer.php
96+
* @magentoConfigFixture current_store customer/captcha/enable 0
97+
*/
98+
public function testCustomerEmailAndPasswordChangeNotificationTemplateGreeting()
99+
{
100+
$this->loginByCustomerId(self::FIXTURE_CUSTOMER_ID);
101+
102+
$this->sendAccountEditRequest([
103+
'email' => 'new.email@example.com',
104+
'change_email' => 1,
105+
'change_password' => 1,
106+
'password' => 'new-Password1',
107+
'password_confirmation' => 'new-Password1',
108+
]);
109+
110+
$this->assertRedirect($this->stringContains('customer/account/'));
111+
$this->assertSessionMessages(
112+
$this->equalTo(['You saved the account information.']),
113+
MessageInterface::TYPE_SUCCESS
114+
);
115+
116+
$this->assertSameGreeting(self::EXPECTED_GREETING, $this->transportBuilderMock->getSentMessage());
117+
}
118+
119+
/**
120+
* Covers Magento_Customer::view/frontend/email/change_password.html
121+
*
122+
* @magentoDataFixture Magento/Customer/_files/customer.php
123+
* @magentoConfigFixture current_store customer/captcha/enable 0
124+
*/
125+
public function testCustomerPasswordChangeNotificationTemplateGreeting()
126+
{
127+
$this->loginByCustomerId(self::FIXTURE_CUSTOMER_ID);
128+
129+
$this->sendAccountEditRequest([
130+
'change_password' => 1,
131+
'password' => 'new-Password1',
132+
'password_confirmation' => 'new-Password1',
133+
]);
134+
135+
$this->assertRedirect($this->stringContains('customer/account/'));
136+
$this->assertSessionMessages(
137+
$this->equalTo(['You saved the account information.']),
138+
MessageInterface::TYPE_SUCCESS
139+
);
140+
141+
$this->assertSameGreeting(self::EXPECTED_GREETING, $this->transportBuilderMock->getSentMessage());
142+
}
143+
144+
/**
145+
* Wraps Customer Edit POST request
146+
*
147+
* @param array $customData
148+
*/
149+
private function sendAccountEditRequest(array $customData): void
150+
{
151+
$basicData = [
152+
'form_key' => $this->formKey->getFormKey(),
153+
'firstname' => self::FIXTURE_CUSTOMER_FIRSTNAME,
154+
'lastname' => self::FIXTURE_CUSTOMER_LASTNAME,
155+
'current_password' => self::FIXTURE_CUSTOMER_PASSWORD
156+
];
157+
158+
$this->getRequest()->setMethod(HttpRequest::METHOD_POST)
159+
->setPostValue(array_merge($basicData, $customData));
160+
161+
$this->dispatch('customer/account/editPost');
162+
}
163+
164+
/**
165+
* Verifies if `<p class="greeting"/>` text contents equals the expected one.
166+
*
167+
* @param string $expectedGreeting
168+
* @param EmailMessage $message
169+
*/
170+
private function assertSameGreeting(string $expectedGreeting, EmailMessage $message)
171+
{
172+
$messageContent = $this->getMessageRawContent($message);
173+
$emailDom = new \DOMDocument();
174+
$emailDom->loadHTML($messageContent);
175+
176+
$emailXpath = new \DOMXPath($emailDom);
177+
$greeting = $emailXpath->query('//p[@class="greeting"]');
178+
179+
$this->assertSame(1, $greeting->length);
180+
$this->assertSame($expectedGreeting, $greeting->item(0)->textContent);
181+
}
182+
183+
/**
184+
* Returns raw content of provided message
185+
*
186+
* @param EmailMessage $message
187+
* @return string
188+
*/
189+
private function getMessageRawContent(EmailMessage $message): string
190+
{
191+
$emailParts = $message->getBody()->getParts();
192+
return current($emailParts)->getRawContent();
193+
}
194+
195+
/**
196+
* Performs Customer log in
197+
*
198+
* @param int $customerId
199+
*/
200+
private function loginByCustomerId(int $customerId): void
201+
{
202+
$this->session->loginById($customerId);
203+
}
204+
}

0 commit comments

Comments
 (0)