Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit 6690a49

Browse files
committed
GraphQL-388: updateCustomer mutation doesn't update undefined fields
1 parent 234f3ad commit 6690a49

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/CheckCustomerPassword.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99

1010
use Magento\Customer\Model\AuthenticationInterface;
1111
use Magento\Framework\Exception\InvalidEmailOrPasswordException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Framework\Exception\State\UserLockedException;
1215
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
16+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1318

1419
/**
1520
* Check customer password
@@ -36,15 +41,21 @@ public function __construct(
3641
* @param string $password
3742
* @param int $customerId
3843
* @throws GraphQlAuthenticationException
44+
* @throws GraphQlInputException
45+
* @throws GraphQlNoSuchEntityException
3946
*/
4047
public function execute(string $password, int $customerId)
4148
{
4249
try {
4350
$this->authentication->authenticate($customerId, $password);
4451
} catch (InvalidEmailOrPasswordException $e) {
45-
throw new GraphQlAuthenticationException(
46-
__('The password doesn\'t match this account. Verify the password and try again.')
47-
);
52+
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
53+
} catch (UserLockedException $e) {
54+
throw new GraphQlAuthenticationException(__($e->getMessage()), $e);
55+
} catch (NoSuchEntityException $e) {
56+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
57+
} catch (LocalizedException $e) {
58+
throw new GraphQlInputException(__($e->getMessage()), $e);
4859
}
4960
}
5061
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerData.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
1111
use Magento\Framework\Exception\AlreadyExistsException;
12+
use Magento\Framework\Exception\LocalizedException;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
15+
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1316
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1417
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1518
use Magento\Store\Model\StoreManagerInterface;
1619
use Magento\Customer\Api\Data\CustomerInterface;
17-
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1820
use Magento\Framework\Api\DataObjectHelper;
19-
use Magento\Framework\Reflection\DataObjectProcessor;
2021

2122
/**
2223
* Update customer data
@@ -38,21 +39,11 @@ class UpdateCustomerData
3839
*/
3940
private $checkCustomerPassword;
4041

41-
/**
42-
* @var CustomerInterfaceFactory
43-
*/
44-
private $customerFactory;
45-
4642
/**
4743
* @var DataObjectHelper
4844
*/
4945
private $dataObjectHelper;
5046

51-
/**
52-
* @var DataObjectProcessor
53-
*/
54-
private $dataObjectProcessor;
55-
5647
/**
5748
* @var array
5849
*/
@@ -62,26 +53,20 @@ class UpdateCustomerData
6253
* @param CustomerRepositoryInterface $customerRepository
6354
* @param StoreManagerInterface $storeManager
6455
* @param CheckCustomerPassword $checkCustomerPassword
65-
* @param CustomerInterfaceFactory $customerFactory
6656
* @param DataObjectHelper $dataObjectHelper
67-
* @param DataObjectProcessor $dataObjectProcessor
6857
* @param array $restrictedKeys
6958
*/
7059
public function __construct(
7160
CustomerRepositoryInterface $customerRepository,
7261
StoreManagerInterface $storeManager,
7362
CheckCustomerPassword $checkCustomerPassword,
74-
CustomerInterfaceFactory $customerFactory,
7563
DataObjectHelper $dataObjectHelper,
76-
DataObjectProcessor $dataObjectProcessor,
7764
array $restrictedKeys = []
7865
) {
7966
$this->customerRepository = $customerRepository;
8067
$this->storeManager = $storeManager;
8168
$this->checkCustomerPassword = $checkCustomerPassword;
82-
$this->customerFactory = $customerFactory;
8369
$this->dataObjectHelper = $dataObjectHelper;
84-
$this->dataObjectProcessor = $dataObjectProcessor;
8570
$this->restrictedKeys = $restrictedKeys;
8671
}
8772

@@ -91,24 +76,23 @@ public function __construct(
9176
* @param int $customerId
9277
* @param array $data
9378
* @return void
94-
* @throws GraphQlNoSuchEntityException
95-
* @throws GraphQlInputException
9679
* @throws GraphQlAlreadyExistsException
80+
* @throws GraphQlInputException
81+
* @throws GraphQlNoSuchEntityException
82+
* @throws GraphQlAuthenticationException
9783
*/
9884
public function execute(int $customerId, array $data): void
9985
{
100-
$customer = $this->customerRepository->getById($customerId);
101-
$newData = array_diff_key($data, array_flip($this->restrictedKeys));
102-
103-
$oldData = $this->dataObjectProcessor->buildOutputDataArray($customer, CustomerInterface::class);
104-
$newData = array_merge($oldData, $newData);
86+
try {
87+
$customer = $this->customerRepository->getById($customerId);
88+
} catch (NoSuchEntityException $e) {
89+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
90+
} catch (LocalizedException $e) {
91+
throw new GraphQlInputException(__($e->getMessage()), $e);
92+
}
10593

106-
$customer = $this->customerFactory->create();
107-
$this->dataObjectHelper->populateWithArray(
108-
$customer,
109-
$newData,
110-
CustomerInterface::class
111-
);
94+
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
95+
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);
11296

11397
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
11498
if (!isset($data['password']) || empty($data['password'])) {
@@ -128,6 +112,8 @@ public function execute(int $customerId, array $data): void
128112
__('A customer with the same email address already exists in an associated website.'),
129113
$e
130114
);
115+
} catch (LocalizedException $e) {
116+
throw new GraphQlInputException(__($e->getMessage()), $e);
131117
}
132118
}
133119
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testUpdateCustomer()
7070
taxvat: "{$newTaxVat}"
7171
email: "{$newEmail}"
7272
password: "{$currentPassword}"
73+
gender: "{$newGender}"
7374
}
7475
) {
7576
customer {
@@ -96,6 +97,7 @@ public function testUpdateCustomer()
9697
$this->assertEquals($newDobDate->format('Y-m-d'), $response['updateCustomer']['customer']['dob']);
9798
$this->assertEquals($newTaxVat, $response['updateCustomer']['customer']['taxvat']);
9899
$this->assertEquals($newEmail, $response['updateCustomer']['customer']['email']);
100+
$this->assertEquals($newGender, $response['updateCustomer']['customer']['gender']);
99101
}
100102

101103
/**

0 commit comments

Comments
 (0)