Skip to content

Commit c578203

Browse files
committed
PWA-806: Localize emails sent through GraphQL application
- Add tests verifying creation of customer accounts through GraphQL
1 parent 0f6e435 commit c578203

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
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+
55+
$this->graphQlRequest = $this->objectManager->create(GraphQlRequest::class);
56+
$this->json = $this->objectManager->get(SerializerInterface::class);
57+
58+
$this->customerRepository = $this->objectManager->create(CustomerRepositoryInterface::class);
59+
$this->storeRepository = $this->objectManager->create(StoreRepositoryInterface::class);
60+
}
61+
62+
/**
63+
* Test that creating a customer sends an email
64+
*/
65+
public function testCreateCustomerSendsEmail()
66+
{
67+
$query
68+
= <<<QUERY
69+
mutation createAccount {
70+
createCustomer(
71+
input: {
72+
email: "test@magento.com"
73+
firstname: "Test"
74+
lastname: "Magento"
75+
password: "T3stP4assw0rd"
76+
is_subscribed: false
77+
}
78+
) {
79+
customer {
80+
id
81+
}
82+
}
83+
}
84+
QUERY;
85+
86+
$response = $this->graphQlRequest->send($query);
87+
$responseData = $this->json->unserialize($response->getContent());
88+
89+
// Assert the response of the GraphQL request
90+
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
91+
92+
// Verify the customer was created and has the correct data
93+
$customer = $this->customerRepository->get('test@magento.com');
94+
$this->assertEquals('Test', $customer->getFirstname());
95+
$this->assertEquals('Magento', $customer->getLastname());
96+
97+
/** @var TransportBuilderMock $transportBuilderMock */
98+
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
99+
$sentMessage = $transportBuilderMock->getSentMessage();
100+
101+
// Verify an email was dispatched to the correct user
102+
$this->assertNotNull($sentMessage);
103+
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
104+
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
105+
106+
// Assert the email contains the expected content
107+
$this->assertEquals('Welcome to Main Website Store', $sentMessage->getSubject());
108+
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
109+
$this->assertStringContainsString('Welcome to Main Website Store.', $messageRaw);
110+
}
111+
112+
113+
/**
114+
* Test that creating a customer on an alternative store sends an email
115+
*
116+
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
117+
*/
118+
public function testCreateCustomerForStoreSendsEmail()
119+
{
120+
$query
121+
= <<<QUERY
122+
mutation createAccount {
123+
createCustomer(
124+
input: {
125+
email: "test@magento.com"
126+
firstname: "Test"
127+
lastname: "Magento"
128+
password: "T3stP4assw0rd"
129+
is_subscribed: false
130+
}
131+
) {
132+
customer {
133+
id
134+
}
135+
}
136+
}
137+
QUERY;
138+
139+
$response = $this->graphQlRequest->send(
140+
$query,
141+
[],
142+
'',
143+
[
144+
'Store' => 'fixture_second_store'
145+
]
146+
);
147+
$responseData = $this->json->unserialize($response->getContent());
148+
149+
// Assert the response of the GraphQL request
150+
$this->assertNull($responseData['data']['createCustomer']['customer']['id']);
151+
152+
// Verify the customer was created and has the correct data
153+
$customer = $this->customerRepository->get('test@magento.com');
154+
$this->assertEquals('Test', $customer->getFirstname());
155+
$this->assertEquals('Magento', $customer->getLastname());
156+
$this->assertEquals('Fixture Second Store', $customer->getCreatedIn());
157+
158+
$store = $this->storeRepository->getById($customer->getStoreId());
159+
$this->assertEquals('fixture_second_store', $store->getCode());
160+
161+
/** @var TransportBuilderMock $transportBuilderMock */
162+
$transportBuilderMock = $this->objectManager->get(TransportBuilderMock::class);
163+
$sentMessage = $transportBuilderMock->getSentMessage();
164+
165+
// Verify an email was dispatched to the correct user
166+
$this->assertNotNull($sentMessage);
167+
$this->assertEquals('Test Magento', $sentMessage->getTo()[0]->getName());
168+
$this->assertEquals('test@magento.com', $sentMessage->getTo()[0]->getEmail());
169+
170+
// Assert the email contains the expected content
171+
$this->assertEquals('Welcome to second store group', $sentMessage->getSubject());
172+
$messageRaw = $sentMessage->getBody()->getParts()[0]->getRawContent();
173+
$this->assertStringContainsString('Welcome to second store group.', $messageRaw);
174+
}
175+
}

0 commit comments

Comments
 (0)