Skip to content

Commit 6e3afd3

Browse files
committed
PWA-806: Localize emails sent through GraphQL application
- Add test for checking customer graph QL end point sends approprite translated emails
1 parent c578203 commit 6e3afd3

File tree

6 files changed

+186
-8
lines changed

6 files changed

+186
-8
lines changed

dev/tests/integration/testsuite/Magento/CustomerGraphQl/Model/Resolver/CreateCustomerTest.php

Lines changed: 71 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ class CreateCustomerTest extends TestCase
5151
public function setUp(): void
5252
{
5353
$this->objectManager = Bootstrap::getObjectManager();
54-
5554
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
5655
$this->json = $this->objectManager->get(SerializerInterface::class);
5756

@@ -109,11 +108,10 @@ public function testCreateCustomerSendsEmail()
109108
$this->assertStringContainsString('Welcome to Main Website Store.', $messageRaw);
110109
}
111110

112-
113111
/**
114112
* Test that creating a customer on an alternative store sends an email
115113
*
116-
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
114+
* @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php
117115
*/
118116
public function testCreateCustomerForStoreSendsEmail()
119117
{
@@ -141,7 +139,72 @@ public function testCreateCustomerForStoreSendsEmail()
141139
[],
142140
'',
143141
[
144-
'Store' => 'fixture_second_store'
142+
'Store' => 'test_store_view'
143+
]
144+
);
145+
$responseData = $this->json->unserialize($response->getContent());
146+
147+
// Assert the response of the GraphQL request
148+
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
149+
150+
// Verify the customer was created and has the correct data
151+
$customer = $this->customerRepository->get('test@magento.com');
152+
$this->assertEquals('Test', $customer->getFirstname());
153+
$this->assertEquals('Magento', $customer->getLastname());
154+
$this->assertEquals('Test Store View', $customer->getCreatedIn());
155+
156+
$store = $this->storeRepository->getById($customer->getStoreId());
157+
$this->assertEquals('test_store_view', $store->getCode());
158+
159+
/** @var TransportBuilderMock $transportBuilderMock */
160+
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
161+
$sentMessage = $transportBuilderMock->getSentMessage();
162+
163+
// Verify an email was dispatched to the correct user
164+
$this->assertNotNull($sentMessage);
165+
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
166+
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
167+
168+
// Assert the email contains the expected content
169+
$this->assertEquals('Welcome to Test Group', $sentMessage->getSubject());
170+
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
171+
$this->assertStringContainsString('Welcome to Test Group.', $messageRaw);
172+
}
173+
174+
/**
175+
* Test that creating a customer on an alternative store sends an email in the translated language
176+
*
177+
* @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php
178+
* @magentoConfigFixture test_store_view_store general/locale/code fr_FR
179+
* @magentoComponentsDir Magento/CustomerGraphQl/_files
180+
*/
181+
public function testCreateCustomerForStoreSendsTranslatedEmail()
182+
{
183+
$query
184+
= <<<QUERY
185+
mutation createAccount {
186+
createCustomer(
187+
input: {
188+
email: "test@magento.com"
189+
firstname: "Test"
190+
lastname: "Magento"
191+
password: "T3stP4assw0rd"
192+
is_subscribed: false
193+
}
194+
) {
195+
customer {
196+
id
197+
}
198+
}
199+
}
200+
QUERY;
201+
202+
$response = $this->graphQlRequest->send(
203+
$query,
204+
[],
205+
'',
206+
[
207+
'Store' => 'test_store_view'
145208
]
146209
);
147210
$responseData = $this->json->unserialize($response->getContent());
@@ -153,10 +216,10 @@ public function testCreateCustomerForStoreSendsEmail()
153216
$customer = $this->customerRepository->get('test@magento.com');
154217
$this->assertEquals('Test', $customer->getFirstname());
155218
$this->assertEquals('Magento', $customer->getLastname());
156-
$this->assertEquals('Fixture Second Store', $customer->getCreatedIn());
219+
$this->assertEquals('Test Store View', $customer->getCreatedIn());
157220

158221
$store = $this->storeRepository->getById($customer->getStoreId());
159-
$this->assertEquals('fixture_second_store', $store->getCode());
222+
$this->assertEquals('test_store_view', $store->getCode());
160223

161224
/** @var TransportBuilderMock $transportBuilderMock */
162225
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
@@ -168,8 +231,8 @@ public function testCreateCustomerForStoreSendsEmail()
168231
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
169232

170233
// Assert the email contains the expected content
171-
$this->assertEquals('Welcome to second store group', $sentMessage->getSubject());
234+
$this->assertEquals('Bienvenue sur Test Group.', $sentMessage->getSubject());
172235
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
173-
$this->assertStringContainsString('Welcome to second store group.', $messageRaw);
236+
$this->assertStringContainsString('Bienvenue sur Test Group.', $messageRaw);
174237
}
175238
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"Welcome to %store_name","Bienvenue sur %store_name"
2+
"Welcome to %store_name.","Bienvenue sur %store_name."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<language xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/Language/package.xsd">
9+
<code>fr_FR</code>
10+
<vendor>french</vendor>
11+
<package>fr_fr</package>
12+
<sort_order>0</sort_order>
13+
</language>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Framework\Component\ComponentRegistrar;
8+
9+
ComponentRegistrar::register(ComponentRegistrar::LANGUAGE, 'french', __DIR__);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
use Magento\Store\Api\Data\GroupInterface;
9+
use Magento\Store\Api\Data\GroupInterfaceFactory;
10+
use Magento\Store\Api\Data\StoreInterface;
11+
use Magento\Store\Api\Data\StoreInterfaceFactory;
12+
use Magento\Store\Api\Data\WebsiteInterface;
13+
use Magento\Store\Api\Data\WebsiteInterfaceFactory;
14+
use Magento\Store\Model\ResourceModel\Group as GroupResource;
15+
use Magento\Store\Model\ResourceModel\Store as StoreResource;
16+
use Magento\Store\Model\ResourceModel\Website as WebsiteResource;
17+
use Magento\Store\Model\StoreManagerInterface;
18+
use Magento\TestFramework\Helper\Bootstrap;
19+
20+
$objectManager = Bootstrap::getObjectManager();
21+
/** @var StoreManagerInterface $storeManager */
22+
$storeManager = $objectManager->get(StoreManagerInterface::class);
23+
/** @var WebsiteResource $websiteResource */
24+
$websiteResource = $objectManager->get(WebsiteResource::class);
25+
/** @var StoreResource $storeResource */
26+
$storeResource = $objectManager->get(StoreResource::class);
27+
/** @var GroupResource $groupResource */
28+
$groupResource = $objectManager->get(GroupResource::class);
29+
/** @var WebsiteInterface $website */
30+
$website = $objectManager->get(WebsiteInterfaceFactory::class)->create();
31+
$website->setCode('test_website')->setName('Test Website');
32+
$websiteResource->save($website);
33+
/** @var GroupInterface $storeGroup */
34+
$storeGroup = $objectManager->get(GroupInterfaceFactory::class)->create();
35+
$storeGroup->setCode('test_group')
36+
->setName('Test Group')
37+
->setWebsite($website);
38+
$groupResource->save($storeGroup);
39+
/* Refresh stores memory cache */
40+
$storeManager->reinitStores();
41+
42+
/** @var StoreInterface $store */
43+
$store = $objectManager->get(StoreInterfaceFactory::class)->create();
44+
$store->setCode('test_store_view')
45+
->setWebsiteId($website->getId())
46+
->setGroupId($storeGroup->getId())
47+
->setName('Test Store View')
48+
->setSortOrder(10)
49+
->setIsActive(1);
50+
$storeResource->save($store);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
use Magento\Framework\Registry;
9+
use Magento\Store\Api\Data\StoreInterface;
10+
use Magento\Store\Api\Data\StoreInterfaceFactory;
11+
use Magento\Store\Api\Data\WebsiteInterface;
12+
use Magento\Store\Api\Data\WebsiteInterfaceFactory;
13+
use Magento\Store\Model\ResourceModel\Store as StoreResource;
14+
use Magento\Store\Model\ResourceModel\Website as WebsiteResource;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var WebsiteResource $websiteResource */
19+
$websiteResource = $objectManager->get(WebsiteResource::class);
20+
/** @var StoreResource $storeResource */
21+
$storeResource = $objectManager->get(StoreResource::class);
22+
/** @var Registry $registry */
23+
$registry = $objectManager->get(Registry::class);
24+
25+
$registry->unregister('isSecureArea');
26+
$registry->register('isSecureArea', true);
27+
/** @var WebsiteInterface $website */
28+
$website = $objectManager->get(WebsiteInterfaceFactory::class)->create();
29+
$websiteResource->load($website, 'test_website', 'code');
30+
if ($website->getId()) {
31+
$websiteResource->delete($website);
32+
}
33+
/** @var StoreInterface $store */
34+
$store = $objectManager->get(StoreInterfaceFactory::class)->create();
35+
$storeResource->load($store, 'test_store_view', 'code');
36+
if ($store->getId()) {
37+
$storeResource->delete($store);
38+
}
39+
40+
$registry->unregister('isSecureArea');
41+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)