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

Implement updates all properties in updateCustomer mutation #397

Merged
merged 2 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
use Magento\Framework\Api\DataObjectHelper;
use Magento\Framework\Reflection\DataObjectProcessor;

/**
* Update customer data
Expand All @@ -34,19 +38,51 @@ class UpdateCustomerData
*/
private $checkCustomerPassword;

/**
* @var CustomerInterfaceFactory
*/
private $customerFactory;

/**
* @var DataObjectHelper
*/
private $dataObjectHelper;

/**
* @var DataObjectProcessor
*/
private $dataObjectProcessor;

/**
* @var array
*/
private $restrictedKeys;

/**
* @param CustomerRepositoryInterface $customerRepository
* @param StoreManagerInterface $storeManager
* @param CheckCustomerPassword $checkCustomerPassword
* @param CustomerInterfaceFactory $customerFactory
* @param DataObjectHelper $dataObjectHelper
* @param DataObjectProcessor $dataObjectProcessor
* @param array $restrictedKeys
*/
public function __construct(
CustomerRepositoryInterface $customerRepository,
StoreManagerInterface $storeManager,
CheckCustomerPassword $checkCustomerPassword
CheckCustomerPassword $checkCustomerPassword,
CustomerInterfaceFactory $customerFactory,
DataObjectHelper $dataObjectHelper,
DataObjectProcessor $dataObjectProcessor,
array $restrictedKeys = []
) {
$this->customerRepository = $customerRepository;
$this->storeManager = $storeManager;
$this->checkCustomerPassword = $checkCustomerPassword;
$this->customerFactory = $customerFactory;
$this->dataObjectHelper = $dataObjectHelper;
$this->dataObjectProcessor = $dataObjectProcessor;
$this->restrictedKeys = $restrictedKeys;
}

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

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

if (isset($data['lastname'])) {
$customer->setLastname($data['lastname']);
}
$customer = $this->customerFactory->create();
$this->dataObjectHelper->populateWithArray(
$customer,
$newData,
CustomerInterface::class
);

if (isset($data['email']) && $customer->getEmail() !== $data['email']) {
if (!isset($data['password']) || empty($data['password'])) {
Expand Down
16 changes: 16 additions & 0 deletions app/code/Magento/CustomerGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\CustomerGraphQl\Model\Customer\UpdateCustomerData">
<arguments>
<argument name="restrictedKeys" xsi:type="array">
<item name="email" xsi:type="const">Magento\Customer\Api\Data\CustomerInterface::EMAIL</item>
</argument>
</arguments>
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,54 @@ public function testUpdateCustomer()
$currentEmail = 'customer@example.com';
$currentPassword = 'password';

$newPrefix = 'Dr';
$newFirstname = 'Richard';
$newMiddlename = 'Riley';
$newLastname = 'Rowe';
$newSuffix = 'III';
$newDob = '3/11/1972';
$newTaxVat = 'GQL1234567';
$newGender = 2;
$newEmail = 'customer_updated@example.com';

$query = <<<QUERY
mutation {
updateCustomer(
input: {
prefix: "{$newPrefix}"
firstname: "{$newFirstname}"
middlename: "{$newMiddlename}"
lastname: "{$newLastname}"
suffix: "{$newSuffix}"
dob: "{$newDob}"
taxvat: "{$newTaxVat}"
email: "{$newEmail}"
password: "{$currentPassword}"
}
) {
customer {
prefix
firstname
middlename
lastname
suffix
dob
taxvat
email
}
}
}
QUERY;
$response = $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword));

$this->assertEquals($newPrefix, $response['updateCustomer']['customer']['prefix']);
$this->assertEquals($newFirstname, $response['updateCustomer']['customer']['firstname']);
$this->assertEquals($newMiddlename, $response['updateCustomer']['customer']['middlename']);
$this->assertEquals($newLastname, $response['updateCustomer']['customer']['lastname']);
$this->assertEquals($newSuffix, $response['updateCustomer']['customer']['suffix']);
$newDobDate = new \DateTime($newDob);
$this->assertEquals($newDobDate->format('Y-m-d'), $response['updateCustomer']['customer']['dob']);
$this->assertEquals($newTaxVat, $response['updateCustomer']['customer']['taxvat']);
$this->assertEquals($newEmail, $response['updateCustomer']['customer']['email']);
}

Expand Down