Skip to content

Commit 2d34a2f

Browse files
committed
Merge branch 'PWA-806' into PWA-805-group
# Conflicts: # app/code/Magento/StoreGraphQl/etc/graphql/di.xml
2 parents e86df34 + c65fb64 commit 2d34a2f

File tree

8 files changed

+439
-0
lines changed

8 files changed

+439
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\StoreGraphQl\Plugin;
8+
9+
use Magento\Framework\App\AreaInterface;
10+
use Magento\Framework\App\AreaList;
11+
use Magento\Framework\App\State;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Mail\Template\TransportBuilder;
15+
use Magento\Store\Model\App\Emulation;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
18+
/**
19+
* Emulate the correct store when GraphQL is sending an email
20+
*/
21+
class LocalizeEmail
22+
{
23+
/**
24+
* @var StoreManagerInterface
25+
*/
26+
private $storeManager;
27+
28+
/**
29+
* @var Emulation
30+
*/
31+
private $emulation;
32+
33+
/**
34+
* @var AreaList
35+
*/
36+
private $areaList;
37+
38+
/**
39+
* @var State
40+
*/
41+
private $appState;
42+
43+
/**
44+
* @param StoreManagerInterface $storeManager
45+
* @param Emulation $emulation
46+
* @param AreaList $areaList
47+
* @param State $appState
48+
*/
49+
public function __construct(
50+
StoreManagerInterface $storeManager,
51+
Emulation $emulation,
52+
AreaList $areaList,
53+
State $appState
54+
) {
55+
$this->storeManager = $storeManager;
56+
$this->emulation = $emulation;
57+
$this->areaList = $areaList;
58+
$this->appState = $appState;
59+
}
60+
61+
/**
62+
* Emulate the correct store during email preparation
63+
*
64+
* @param TransportBuilder $subject
65+
* @param \Closure $proceed
66+
* @return mixed
67+
* @throws NoSuchEntityException|LocalizedException
68+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
69+
*/
70+
public function aroundGetTransport(TransportBuilder $subject, \Closure $proceed)
71+
{
72+
// Load translations for the app
73+
$area = $this->areaList->getArea($this->appState->getAreaCode());
74+
$area->load(AreaInterface::PART_TRANSLATE);
75+
76+
$currentStore = $this->storeManager->getStore();
77+
$this->emulation->startEnvironmentEmulation($currentStore->getId());
78+
$output = $proceed();
79+
$this->emulation->stopEnvironmentEmulation();
80+
81+
return $output;
82+
}
83+
}

app/code/Magento/StoreGraphQl/etc/graphql/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,7 @@
3030
</argument>
3131
</arguments>
3232
</type>
33+
<type name="Magento\Framework\Mail\Template\TransportBuilder">
34+
<plugin name="graphQlEmulateEmail" type="Magento\StoreGraphQl\Plugin\LocalizeEmail" />
35+
</type>
3336
</config>
Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
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\CustomerGraphQl\Model\Resolver;
9+
10+
use Magento\Customer\Api\CustomerRepositoryInterface;
11+
use Magento\Framework\ObjectManagerInterface;
12+
use Magento\Framework\Serialize\SerializerInterface;
13+
use Magento\GraphQl\Service\GraphQlRequest;
14+
use Magento\Store\Api\StoreRepositoryInterface;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
use Magento\TestFramework\Mail\Template\TransportBuilderMock;
17+
use PHPUnit\Framework\TestCase;
18+
19+
/**
20+
* Test creating a customer through GraphQL
21+
*
22+
* @magentoAppArea graphql
23+
*/
24+
class CreateCustomerTest extends TestCase
25+
{
26+
/**
27+
* @var ObjectManagerInterface
28+
*/
29+
private $objectManager;
30+
31+
/**
32+
* @var GraphQlRequest
33+
*/
34+
private $graphQlRequest;
35+
36+
/**
37+
* @var SerializerInterface
38+
*/
39+
private $json;
40+
41+
/**
42+
* @var CustomerRepositoryInterface
43+
*/
44+
private $customerRepository;
45+
46+
/**
47+
* @var StoreRepositoryInterface
48+
*/
49+
private $storeRepository;
50+
51+
public function setUp(): void
52+
{
53+
$this->objectManager = Bootstrap::getObjectManager();
54+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
55+
$this->json = $this->objectManager->get(SerializerInterface::class);
56+
57+
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
58+
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
59+
}
60+
61+
/**
62+
* Test that creating a customer sends an email
63+
*/
64+
public function testCreateCustomerSendsEmail()
65+
{
66+
$query
67+
= <<<QUERY
68+
mutation createAccount {
69+
createCustomer(
70+
input: {
71+
72+
firstname: "Test"
73+
lastname: "Magento"
74+
password: "T3stP4assw0rd"
75+
is_subscribed: false
76+
}
77+
) {
78+
customer {
79+
id
80+
}
81+
}
82+
}
83+
QUERY;
84+
85+
$response = $this->graphQlRequest->send($query);
86+
$responseData = $this->json->unserialize($response->getContent());
87+
88+
// Assert the response of the GraphQL request
89+
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
90+
91+
// Verify the customer was created and has the correct data
92+
$customer = $this->customerRepository->get('[email protected]');
93+
$this->assertEquals('Test', $customer->getFirstname());
94+
$this->assertEquals('Magento', $customer->getLastname());
95+
96+
/** @var TransportBuilderMock $transportBuilderMock */
97+
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
98+
$sentMessage = $transportBuilderMock->getSentMessage();
99+
100+
// Verify an email was dispatched to the correct user
101+
$this->assertNotNull($sentMessage);
102+
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
103+
$this->assertEquals('[email protected]', $sentMessage->getTo()[0]->getEmail());
104+
105+
// Assert the email contains the expected content
106+
$this->assertEquals('Welcome to Main Website Store', $sentMessage->getSubject());
107+
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
108+
$this->assertStringContainsString('Welcome to Main Website Store.', $messageRaw);
109+
}
110+
111+
/**
112+
* Test that creating a customer on an alternative store sends an email
113+
*
114+
* @magentoDataFixture Magento/CustomerGraphQl/_files/website_store_with_store_view.php
115+
*/
116+
public function testCreateCustomerForStoreSendsEmail()
117+
{
118+
$query
119+
= <<<QUERY
120+
mutation createAccount {
121+
createCustomer(
122+
input: {
123+
124+
firstname: "Test"
125+
lastname: "Magento"
126+
password: "T3stP4assw0rd"
127+
is_subscribed: false
128+
}
129+
) {
130+
customer {
131+
id
132+
}
133+
}
134+
}
135+
QUERY;
136+
137+
$response = $this->graphQlRequest->send(
138+
$query,
139+
[],
140+
'',
141+
[
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('[email protected]');
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('[email protected]', $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+
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'
208+
]
209+
);
210+
$responseData = $this->json->unserialize($response->getContent());
211+
212+
// Assert the response of the GraphQL request
213+
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
214+
215+
// Verify the customer was created and has the correct data
216+
$customer = $this->customerRepository->get('[email protected]');
217+
$this->assertEquals('Test', $customer->getFirstname());
218+
$this->assertEquals('Magento', $customer->getLastname());
219+
$this->assertEquals('Test Store View', $customer->getCreatedIn());
220+
221+
$store = $this->storeRepository->getById($customer->getStoreId());
222+
$this->assertEquals('test_store_view', $store->getCode());
223+
224+
/** @var TransportBuilderMock $transportBuilderMock */
225+
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
226+
$sentMessage = $transportBuilderMock->getSentMessage();
227+
228+
// Verify an email was dispatched to the correct user
229+
$this->assertNotNull($sentMessage);
230+
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
231+
$this->assertEquals('[email protected]', $sentMessage->getTo()[0]->getEmail());
232+
233+
// Assert the email contains the expected content
234+
$this->assertEquals('Bienvenue sur Test Group', $sentMessage->getSubject());
235+
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
236+
$this->assertStringContainsString('Bienvenue sur Test Group.', $messageRaw);
237+
}
238+
}
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_fr_fr', __DIR__);

0 commit comments

Comments
 (0)