Skip to content

Commit 16cb18d

Browse files
committed
magento#27589 Cover with Integration Tests the template changes.
1 parent 1ac923d commit 16cb18d

File tree

1 file changed

+199
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)