Skip to content

Commit 42b002c

Browse files
authored
ENGCOM-4380: Implement updates all properties in updateCustomer mutation #397
2 parents 800449d + 64225ce commit 42b002c

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed

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

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Store\Model\StoreManagerInterface;
16+
use Magento\Customer\Api\Data\CustomerInterface;
17+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
18+
use Magento\Framework\Api\DataObjectHelper;
19+
use Magento\Framework\Reflection\DataObjectProcessor;
1620

1721
/**
1822
* Update customer data
@@ -34,19 +38,51 @@ class UpdateCustomerData
3438
*/
3539
private $checkCustomerPassword;
3640

41+
/**
42+
* @var CustomerInterfaceFactory
43+
*/
44+
private $customerFactory;
45+
46+
/**
47+
* @var DataObjectHelper
48+
*/
49+
private $dataObjectHelper;
50+
51+
/**
52+
* @var DataObjectProcessor
53+
*/
54+
private $dataObjectProcessor;
55+
56+
/**
57+
* @var array
58+
*/
59+
private $restrictedKeys;
60+
3761
/**
3862
* @param CustomerRepositoryInterface $customerRepository
3963
* @param StoreManagerInterface $storeManager
4064
* @param CheckCustomerPassword $checkCustomerPassword
65+
* @param CustomerInterfaceFactory $customerFactory
66+
* @param DataObjectHelper $dataObjectHelper
67+
* @param DataObjectProcessor $dataObjectProcessor
68+
* @param array $restrictedKeys
4169
*/
4270
public function __construct(
4371
CustomerRepositoryInterface $customerRepository,
4472
StoreManagerInterface $storeManager,
45-
CheckCustomerPassword $checkCustomerPassword
73+
CheckCustomerPassword $checkCustomerPassword,
74+
CustomerInterfaceFactory $customerFactory,
75+
DataObjectHelper $dataObjectHelper,
76+
DataObjectProcessor $dataObjectProcessor,
77+
array $restrictedKeys = []
4678
) {
4779
$this->customerRepository = $customerRepository;
4880
$this->storeManager = $storeManager;
4981
$this->checkCustomerPassword = $checkCustomerPassword;
82+
$this->customerFactory = $customerFactory;
83+
$this->dataObjectHelper = $dataObjectHelper;
84+
$this->dataObjectProcessor = $dataObjectProcessor;
85+
$this->restrictedKeys = $restrictedKeys;
5086
}
5187

5288
/**
@@ -62,14 +98,17 @@ public function __construct(
6298
public function execute(int $customerId, array $data): void
6399
{
64100
$customer = $this->customerRepository->getById($customerId);
101+
$newData = array_diff_key($data, array_flip($this->restrictedKeys));
65102

66-
if (isset($data['firstname'])) {
67-
$customer->setFirstname($data['firstname']);
68-
}
103+
$oldData = $this->dataObjectProcessor->buildOutputDataArray($customer, CustomerInterface::class);
104+
$newData = array_merge($oldData, $newData);
69105

70-
if (isset($data['lastname'])) {
71-
$customer->setLastname($data['lastname']);
72-
}
106+
$customer = $this->customerFactory->create();
107+
$this->dataObjectHelper->populateWithArray(
108+
$customer,
109+
$newData,
110+
CustomerInterface::class
111+
);
73112

74113
if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
75114
if (!isset($data['password']) || empty($data['password'])) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<type name="Magento\CustomerGraphQl\Model\Customer\UpdateCustomerData">
10+
<arguments>
11+
<argument name="restrictedKeys" xsi:type="array">
12+
<item name="email" xsi:type="const">Magento\Customer\Api\Data\CustomerInterface::EMAIL</item>
13+
</argument>
14+
</arguments>
15+
</type>
16+
</config>

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,54 @@ public function testUpdateCustomer()
4747
$currentEmail = 'customer@example.com';
4848
$currentPassword = 'password';
4949

50+
$newPrefix = 'Dr';
5051
$newFirstname = 'Richard';
52+
$newMiddlename = 'Riley';
5153
$newLastname = 'Rowe';
54+
$newSuffix = 'III';
55+
$newDob = '3/11/1972';
56+
$newTaxVat = 'GQL1234567';
57+
$newGender = 2;
5258
$newEmail = 'customer_updated@example.com';
5359

5460
$query = <<<QUERY
5561
mutation {
5662
updateCustomer(
5763
input: {
64+
prefix: "{$newPrefix}"
5865
firstname: "{$newFirstname}"
66+
middlename: "{$newMiddlename}"
5967
lastname: "{$newLastname}"
68+
suffix: "{$newSuffix}"
69+
dob: "{$newDob}"
70+
taxvat: "{$newTaxVat}"
6071
email: "{$newEmail}"
6172
password: "{$currentPassword}"
6273
}
6374
) {
6475
customer {
76+
prefix
6577
firstname
78+
middlename
6679
lastname
80+
suffix
81+
dob
82+
taxvat
6783
email
6884
}
6985
}
7086
}
7187
QUERY;
7288
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));
7389

90+
$this->assertEquals($newPrefix, $response['updateCustomer']['customer']['prefix']);
7491
$this->assertEquals($newFirstname, $response['updateCustomer']['customer']['firstname']);
92+
$this->assertEquals($newMiddlename, $response['updateCustomer']['customer']['middlename']);
7593
$this->assertEquals($newLastname, $response['updateCustomer']['customer']['lastname']);
94+
$this->assertEquals($newSuffix, $response['updateCustomer']['customer']['suffix']);
95+
$newDobDate = new \DateTime($newDob);
96+
$this->assertEquals($newDobDate->format('Y-m-d'), $response['updateCustomer']['customer']['dob']);
97+
$this->assertEquals($newTaxVat, $response['updateCustomer']['customer']['taxvat']);
7698
$this->assertEquals($newEmail, $response['updateCustomer']['customer']['email']);
7799
}
78100

0 commit comments

Comments
 (0)