Skip to content

Commit b5208cd

Browse files
#28570: createCustomer does not match validation requirements
1 parent 887f191 commit b5208cd

File tree

4 files changed

+462
-14
lines changed

4 files changed

+462
-14
lines changed

app/code/Magento/CustomerGraphQl/Model/Resolver/UpdateCustomerEmail.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<?php
2-
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
37

48
namespace Magento\CustomerGraphQl\Model\Resolver;
59

6-
710
use Magento\CustomerGraphQl\Model\Customer\ExtractCustomerData;
811
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
912
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
1013
use Magento\Framework\GraphQl\Config\Element\Field;
1114
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
12-
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13-
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
14-
use Magento\Framework\GraphQl\Query\Resolver\Value;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\GraphQl\Model\Query\ContextInterface;
1718

19+
/**
20+
* Customer email update, used for GraphQL request processing
21+
*/
1822
class UpdateCustomerEmail implements ResolverInterface
1923
{
2024
/**
@@ -55,15 +59,11 @@ public function resolve(
5559
array $value = null,
5660
array $args = null
5761
) {
58-
/** @var \Magento\GraphQl\Model\Query\ContextInterface $context */
62+
/** @var ContextInterface $context */
5963
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
6064
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
6165
}
6266

63-
if (empty($args['email']) || empty($args['password'])) {
64-
throw new GraphQlInputException(__('"email" and "password" values should be specified'));
65-
}
66-
6767
$customer = $this->getCustomer->execute($context);
6868
$this->updateCustomerAccount->execute(
6969
$customer,

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,18 @@ public function testCreateCustomerAccountWithoutPassword()
116116
*/
117117
public function testCreateCustomerIfInputDataIsEmpty()
118118
{
119+
$exceptionMessage = 'Field CustomerCreateInput.email of required type String! was not provided.
120+
Field CustomerCreateInput.firstname of required type String! was not provided.
121+
Field CustomerCreateInput.lastname of required type String! was not provided.';
122+
119123
$this->expectException(\Exception::class);
120-
$this->expectExceptionMessage('"input" value should be specified');
124+
$this->expectExceptionMessage($exceptionMessage);
121125

122126
$query = <<<QUERY
123127
mutation {
124128
createCustomer(
125129
input: {
126-
130+
127131
}
128132
) {
129133
customer {
@@ -144,7 +148,7 @@ public function testCreateCustomerIfInputDataIsEmpty()
144148
public function testCreateCustomerIfEmailMissed()
145149
{
146150
$this->expectException(\Exception::class);
147-
$this->expectExceptionMessage('Required parameters are missing: Email');
151+
$this->expectExceptionMessage('Field CustomerCreateInput.email of required type String! was not provided');
148152

149153
$newFirstname = 'Richard';
150154
$newLastname = 'Rowe';
@@ -234,7 +238,7 @@ public function invalidEmailAddressDataProvider(): array
234238
public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
235239
{
236240
$this->expectException(\Exception::class);
237-
$this->expectExceptionMessage('Field "test123" is not defined by type CustomerInput.');
241+
$this->expectExceptionMessage('Field "test123" is not defined by type CustomerCreateInput.');
238242

239243
$newFirstname = 'Richard';
240244
$newLastname = 'Rowe';
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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\GraphQl\Customer;
9+
10+
use Exception;
11+
use Magento\Customer\Api\CustomerRepositoryInterface;
12+
use Magento\CustomerGraphQl\Model\Customer\UpdateCustomerAccount;
13+
use Magento\Framework\Exception\AuthenticationException;
14+
use Magento\Integration\Api\CustomerTokenServiceInterface;
15+
use Magento\Store\Api\StoreRepositoryInterface;
16+
use Magento\TestFramework\Helper\Bootstrap;
17+
use Magento\TestFramework\TestCase\GraphQlAbstract;
18+
19+
/**
20+
* Test for update customer's email
21+
*/
22+
class UpdateCustomerEmailTest extends GraphQlAbstract
23+
{
24+
/**
25+
* @var CustomerTokenServiceInterface
26+
*/
27+
private $customerTokenService;
28+
29+
/**
30+
* @var CustomerRepositoryInterface
31+
*/
32+
private $customerRepository;
33+
/**
34+
* @var UpdateCustomerAccount
35+
*/
36+
private $updateCustomerAccount;
37+
/**
38+
* @var StoreRepositoryInterface
39+
*/
40+
private $storeRepository;
41+
42+
/**
43+
* Setting up tests
44+
*/
45+
protected function setUp(): void
46+
{
47+
parent::setUp();
48+
49+
$this->customerTokenService = Bootstrap::getObjectManager()->get(CustomerTokenServiceInterface::class);
50+
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
51+
$this->updateCustomerAccount = Bootstrap::getObjectManager()->get(UpdateCustomerAccount::class);
52+
$this->storeRepository = Bootstrap::getObjectManager()->get(StoreRepositoryInterface::class);
53+
}
54+
55+
/**
56+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
57+
*/
58+
public function testUpdateCustomerEmail(): void
59+
{
60+
$currentEmail = '[email protected]';
61+
$currentPassword = 'password';
62+
63+
$newEmail = '[email protected]';
64+
65+
$query = <<<QUERY
66+
mutation {
67+
updateCustomerEmail(
68+
email: "{$newEmail}"
69+
password: "{$currentPassword}"
70+
) {
71+
customer {
72+
email
73+
}
74+
}
75+
}
76+
QUERY;
77+
78+
$response = $this->graphQlMutation(
79+
$query,
80+
[],
81+
'',
82+
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
83+
);
84+
85+
$this->assertEquals($newEmail, $response['updateCustomerEmail']['customer']['email']);
86+
87+
/* $this->updateCustomerAccount->execute(
88+
$this->customerRepository->get($newEmail),
89+
['email' => $currentEmail, 'password' => $currentPassword],
90+
$this->storeRepository->getById(1)
91+
);*/
92+
}
93+
94+
/**
95+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
96+
*/
97+
public function testUpdateCustomerEmailIfPasswordIsWrong(): void
98+
{
99+
$this->expectException(Exception::class);
100+
$this->expectExceptionMessage('Invalid login or password.');
101+
102+
$currentEmail = '[email protected]';
103+
$currentPassword = 'password';
104+
105+
$newEmail = '[email protected]';
106+
$wrongPassword = 'wrongpassword';
107+
108+
$query = <<<QUERY
109+
mutation {
110+
updateCustomerEmail(
111+
email: "{$newEmail}"
112+
password: "{$wrongPassword}"
113+
) {
114+
customer {
115+
email
116+
}
117+
}
118+
}
119+
QUERY;
120+
121+
$this->graphQlMutation(
122+
$query,
123+
[],
124+
'',
125+
$this->getCustomerAuthHeaders($currentEmail, $currentPassword)
126+
);
127+
}
128+
129+
/**
130+
* @magentoApiDataFixture Magento/Customer/_files/two_customers.php
131+
*/
132+
public function testUpdateEmailIfEmailAlreadyExists()
133+
{
134+
$this->expectException(Exception::class);
135+
$this->expectExceptionMessage(
136+
'A customer with the same email address already exists in an associated website.'
137+
);
138+
139+
$currentEmail = '[email protected]';
140+
$currentPassword = 'password';
141+
$existedEmail = '[email protected]';
142+
143+
$query = <<<QUERY
144+
mutation {
145+
updateCustomerEmail(
146+
email: "{$existedEmail}"
147+
password: "{$currentPassword}"
148+
) {
149+
customer {
150+
firstname
151+
}
152+
}
153+
}
154+
QUERY;
155+
$this->graphQlMutation($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
156+
}
157+
158+
/**
159+
* Get customer authorization headers
160+
*
161+
* @param string $email
162+
* @param string $password
163+
* @return array
164+
* @throws AuthenticationException
165+
*/
166+
private function getCustomerAuthHeaders(string $email, string $password): array
167+
{
168+
$customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password);
169+
return ['Authorization' => 'Bearer ' . $customerToken];
170+
}
171+
}

0 commit comments

Comments
 (0)