From dba98002eb7dba2e095c857d62312f3187398e94 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 16 Mar 2019 12:49:26 +0200 Subject: [PATCH 01/16] magento/graphql-ce#271: [My Account] Add support of Customer attributes - Customer attributes validation added --- .../Customer/GetAllowedCustomerAttributes.php | 49 +++++++++++++++++++ .../Model/Customer/UpdateCustomerAccount.php | 35 ++++++++++++- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php new file mode 100644 index 000000000000..1f716c70c098 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -0,0 +1,49 @@ +eavConfig = $eavConfig; + } + + /** + * Get allowed address attributes + * + * @return AbstractAttribute[] + */ + public function execute(): array + { + $attributes = $this->eavConfig->getEntityAttributes( + CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER + ); + foreach ($attributes as $attributeCode => $attribute) { + if (false === $attribute->getIsVisibleOnFront()) { + unset($attributes[$attributeCode]); + } + } + return $attributes; + } +} diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php index 8601d586b3c9..dff14a4506c9 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php @@ -44,6 +44,11 @@ class UpdateCustomerAccount */ private $changeSubscriptionStatus; + /** + * @var GetAllowedCustomerAttributes + */ + private $getAllowedCustomerAttributes; + /** * @var array */ @@ -55,6 +60,7 @@ class UpdateCustomerAccount * @param CheckCustomerPassword $checkCustomerPassword * @param DataObjectHelper $dataObjectHelper * @param ChangeSubscriptionStatus $changeSubscriptionStatus + * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes * @param array $restrictedKeys */ public function __construct( @@ -63,6 +69,7 @@ public function __construct( CheckCustomerPassword $checkCustomerPassword, DataObjectHelper $dataObjectHelper, ChangeSubscriptionStatus $changeSubscriptionStatus, + GetAllowedCustomerAttributes $getAllowedCustomerAttributes, array $restrictedKeys = [] ) { $this->saveCustomer = $saveCustomer; @@ -71,6 +78,7 @@ public function __construct( $this->dataObjectHelper = $dataObjectHelper; $this->restrictedKeys = $restrictedKeys; $this->changeSubscriptionStatus = $changeSubscriptionStatus; + $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; } /** @@ -93,7 +101,7 @@ public function execute(CustomerInterface $customer, array $data): void $this->checkCustomerPassword->execute($data['password'], (int)$customer->getId()); $customer->setEmail($data['email']); } - + $this->validateData($data); $filteredData = array_diff_key($data, array_flip($this->restrictedKeys)); $this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class); @@ -105,4 +113,29 @@ public function execute(CustomerInterface $customer, array $data): void $this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']); } } + + /** + * @param array $customerData + * @return void + * @throws GraphQlInputException + */ + public function validateData(array $customerData): void + { + $attributes = $this->getAllowedCustomerAttributes->execute(); + $errorInput = []; + + foreach ($attributes as $attributeName => $attributeInfo) { + if ($attributeInfo->getIsRequired() + && (isset($customerData[$attributeName]) && empty($customerData[$attributeName])) + ) { + $errorInput[] = $attributeName; + } + } + + if ($errorInput) { + throw new GraphQlInputException( + __('Required parameters are missing: %1', [implode(', ', $errorInput)]) + ); + } + } } From 7f6718d2faf69a0e701e3afabcba133fb02d645e Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 16 Mar 2019 13:13:26 +0200 Subject: [PATCH 02/16] magento/graphql-ce#271: [My Account] Add support of Customer attributes - Updating API functional test: adding --- .../GraphQl/Customer/UpdateCustomerTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php index df45e1de771d..07037f6d82a5 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php @@ -263,6 +263,34 @@ public function testUpdateEmailIfEmailAlreadyExists() $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @expectedException \Exception + * @expectedExceptionMessage Required parameters are missing: firstname + */ + public function testEmptyCustomerName() + { + $currentEmail = 'customer@example.com'; + $currentPassword = 'password'; + + $query = <<graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); + } + /** * @param string $email * @param string $password From 3735e912b66fe91acf545c7db597b2a4cda44edf Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 16 Mar 2019 13:21:52 +0200 Subject: [PATCH 03/16] magento/graphql-ce#271: [My Account] Add support of Customer attributes - Customer attributes validation added for create customer action --- .../Model/Customer/CreateCustomerAccount.php | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index b7b66df04246..685332e638d8 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -45,25 +45,33 @@ class CreateCustomerAccount */ private $changeSubscriptionStatus; + /** + * @var GetAllowedCustomerAttributes + */ + private $getAllowedCustomerAttributes; + /** * @param DataObjectHelper $dataObjectHelper * @param CustomerInterfaceFactory $customerFactory * @param StoreManagerInterface $storeManager * @param AccountManagementInterface $accountManagement * @param ChangeSubscriptionStatus $changeSubscriptionStatus + * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes */ public function __construct( DataObjectHelper $dataObjectHelper, CustomerInterfaceFactory $customerFactory, StoreManagerInterface $storeManager, AccountManagementInterface $accountManagement, - ChangeSubscriptionStatus $changeSubscriptionStatus + ChangeSubscriptionStatus $changeSubscriptionStatus, + GetAllowedCustomerAttributes $getAllowedCustomerAttributes ) { $this->dataObjectHelper = $dataObjectHelper; $this->customerFactory = $customerFactory; $this->accountManagement = $accountManagement; $this->storeManager = $storeManager; $this->changeSubscriptionStatus = $changeSubscriptionStatus; + $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; } /** @@ -96,6 +104,7 @@ public function execute(array $data): CustomerInterface */ private function createAccount(array $data): CustomerInterface { + $this->validateData($data); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray( $customerDataObject, @@ -109,4 +118,29 @@ private function createAccount(array $data): CustomerInterface $password = array_key_exists('password', $data) ? $data['password'] : null; return $this->accountManagement->createAccount($customerDataObject, $password); } + + /** + * @param array $customerData + * @return void + * @throws GraphQlInputException + */ + public function validateData(array $customerData): void + { + $attributes = $this->getAllowedCustomerAttributes->execute(); + $errorInput = []; + + foreach ($attributes as $attributeName => $attributeInfo) { + if ($attributeInfo->getIsRequired() + && (isset($customerData[$attributeName]) && empty($customerData[$attributeName])) + ) { + $errorInput[] = $attributeName; + } + } + + if ($errorInput) { + throw new GraphQlInputException( + __('Required parameters are missing: %1', [implode(', ', $errorInput)]) + ); + } + } } From 342675ad46b664d4be9e9669657da099b5a2655e Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 16 Mar 2019 13:31:05 +0200 Subject: [PATCH 04/16] magento/graphql-ce#271: [My Account] Add support of Customer attributes - Updating API functional test: adding empty first name scenario for customer account creation API --- .../GraphQl/Customer/CreateCustomerTest.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php index 7342800379d1..1018c4082018 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php @@ -234,6 +234,41 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput() } } } +QUERY; + $this->graphQlQuery($query); + } + + /** + * @expectedException \Exception + * @expectedExceptionMessage Required parameters are missing: firstname + */ + public function testCreateCustomerIfNameEmpty() + { + $newEmail = 'customer_created' . rand(1, 2000000) . '@example.com'; + $newFirstname = ''; + $newLastname = 'Rowe'; + $currentPassword = 'test123#'; + + $query = <<graphQlQuery($query); } From 9f1ba1a0ec488b894b261159324038bba095ddc1 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Mon, 6 May 2019 21:00:17 +0300 Subject: [PATCH 05/16] #271: [My Account] Refactoring customer attributes validation --- .../Model/Customer/CreateCustomerAccount.php | 35 ++---------- .../Customer/GetAllowedCustomerAttributes.php | 53 ++++++++++++++----- .../Model/Customer/UpdateCustomerAccount.php | 42 +++------------ .../Model/Customer/ValidateCustomerData.php | 51 ++++++++++++++++++ 4 files changed, 103 insertions(+), 78 deletions(-) create mode 100644 app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index 685332e638d8..764a4702752d 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -46,9 +46,9 @@ class CreateCustomerAccount private $changeSubscriptionStatus; /** - * @var GetAllowedCustomerAttributes + * @var ValidateCustomerData */ - private $getAllowedCustomerAttributes; + private $validateCustomerData; /** * @param DataObjectHelper $dataObjectHelper @@ -64,14 +64,14 @@ public function __construct( StoreManagerInterface $storeManager, AccountManagementInterface $accountManagement, ChangeSubscriptionStatus $changeSubscriptionStatus, - GetAllowedCustomerAttributes $getAllowedCustomerAttributes + ValidateCustomerData $validateCustomerData ) { $this->dataObjectHelper = $dataObjectHelper; $this->customerFactory = $customerFactory; $this->accountManagement = $accountManagement; $this->storeManager = $storeManager; $this->changeSubscriptionStatus = $changeSubscriptionStatus; - $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; + $this->validateCustomerData = $validateCustomerData; } /** @@ -104,7 +104,7 @@ public function execute(array $data): CustomerInterface */ private function createAccount(array $data): CustomerInterface { - $this->validateData($data); + $this->validateCustomerData->execute($data); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray( $customerDataObject, @@ -118,29 +118,4 @@ private function createAccount(array $data): CustomerInterface $password = array_key_exists('password', $data) ? $data['password'] : null; return $this->accountManagement->createAccount($customerDataObject, $password); } - - /** - * @param array $customerData - * @return void - * @throws GraphQlInputException - */ - public function validateData(array $customerData): void - { - $attributes = $this->getAllowedCustomerAttributes->execute(); - $errorInput = []; - - foreach ($attributes as $attributeName => $attributeInfo) { - if ($attributeInfo->getIsRequired() - && (isset($customerData[$attributeName]) && empty($customerData[$attributeName])) - ) { - $errorInput[] = $attributeName; - } - } - - if ($errorInput) { - throw new GraphQlInputException( - __('Required parameters are missing: %1', [implode(', ', $errorInput)]) - ); - } - } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 1f716c70c098..9ebc114432f6 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -8,8 +8,11 @@ namespace Magento\CustomerGraphQl\Model\Customer; use Magento\Customer\Api\CustomerMetadataManagementInterface; -use Magento\Eav\Model\Config; +use Magento\Eav\Model\AttributeRepository; use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; +use Magento\Framework\Api\SearchCriteriaBuilder; +use Magento\Framework\Exception\InputException; +use Magento\Framework\GraphQl\Exception\GraphQlInputException; /** * Get allowed address attributes @@ -17,33 +20,55 @@ class GetAllowedCustomerAttributes { /** - * @var Config + * @var AttributeRepository */ - private $eavConfig; + private $attributeRepository; /** - * @param Config $eavConfig + * @var SearchCriteriaBuilder */ - public function __construct(Config $eavConfig) - { - $this->eavConfig = $eavConfig; + private $searchCriteriaBuilder; + + /** + * @param AttributeRepository $attributeRepository + */ + public function __construct( + AttributeRepository $attributeRepository, + SearchCriteriaBuilder $searchCriteriaBuilder + ) { + $this->attributeRepository = $attributeRepository; + $this->searchCriteriaBuilder = $searchCriteriaBuilder; } /** - * Get allowed address attributes + * Get allowed customer attributes * + * @param array $attributeKeys + * @throws GraphQlInputException * @return AbstractAttribute[] */ - public function execute(): array + public function execute($attributeKeys): array { - $attributes = $this->eavConfig->getEntityAttributes( - CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER - ); - foreach ($attributes as $attributeCode => $attribute) { + $this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); + $searchCriteria = $this->searchCriteriaBuilder->create(); + try { + $attributesSearchResult = $this->attributeRepository->getList( + CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER, + $searchCriteria + ); + } catch (InputException $exception) { + throw new GraphQlInputException(__($exception->getMessage())); + } + + /** @var AbstractAttribute[] $attributes */ + $attributes = $attributesSearchResult->getItems(); + + foreach ($attributes as $index => $attribute) { if (false === $attribute->getIsVisibleOnFront()) { - unset($attributes[$attributeCode]); + unset($attributes[$index]); } } + return $attributes; } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php index dff14a4506c9..3aab5c29d3d0 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php @@ -45,9 +45,9 @@ class UpdateCustomerAccount private $changeSubscriptionStatus; /** - * @var GetAllowedCustomerAttributes + * @var ValidateCustomerData */ - private $getAllowedCustomerAttributes; + private $validateCustomerData; /** * @var array @@ -60,7 +60,7 @@ class UpdateCustomerAccount * @param CheckCustomerPassword $checkCustomerPassword * @param DataObjectHelper $dataObjectHelper * @param ChangeSubscriptionStatus $changeSubscriptionStatus - * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes + * @param ValidateCustomerData $validateCustomerData * @param array $restrictedKeys */ public function __construct( @@ -69,7 +69,7 @@ public function __construct( CheckCustomerPassword $checkCustomerPassword, DataObjectHelper $dataObjectHelper, ChangeSubscriptionStatus $changeSubscriptionStatus, - GetAllowedCustomerAttributes $getAllowedCustomerAttributes, + ValidateCustomerData $validateCustomerData, array $restrictedKeys = [] ) { $this->saveCustomer = $saveCustomer; @@ -78,18 +78,17 @@ public function __construct( $this->dataObjectHelper = $dataObjectHelper; $this->restrictedKeys = $restrictedKeys; $this->changeSubscriptionStatus = $changeSubscriptionStatus; - $this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; + $this->validateCustomerData = $validateCustomerData; } /** - * Update customer account data - * * @param CustomerInterface $customer * @param array $data - * @return void * @throws GraphQlAlreadyExistsException * @throws GraphQlAuthenticationException * @throws GraphQlInputException + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException */ public function execute(CustomerInterface $customer, array $data): void { @@ -101,7 +100,7 @@ public function execute(CustomerInterface $customer, array $data): void $this->checkCustomerPassword->execute($data['password'], (int)$customer->getId()); $customer->setEmail($data['email']); } - $this->validateData($data); + $this->validateCustomerData->execute($data); $filteredData = array_diff_key($data, array_flip($this->restrictedKeys)); $this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class); @@ -113,29 +112,4 @@ public function execute(CustomerInterface $customer, array $data): void $this->changeSubscriptionStatus->execute((int)$customer->getId(), (bool)$data['is_subscribed']); } } - - /** - * @param array $customerData - * @return void - * @throws GraphQlInputException - */ - public function validateData(array $customerData): void - { - $attributes = $this->getAllowedCustomerAttributes->execute(); - $errorInput = []; - - foreach ($attributes as $attributeName => $attributeInfo) { - if ($attributeInfo->getIsRequired() - && (isset($customerData[$attributeName]) && empty($customerData[$attributeName])) - ) { - $errorInput[] = $attributeName; - } - } - - if ($errorInput) { - throw new GraphQlInputException( - __('Required parameters are missing: %1', [implode(', ', $errorInput)]) - ); - } - } } diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php new file mode 100644 index 000000000000..daefe1b16a21 --- /dev/null +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -0,0 +1,51 @@ +getAllowedCustomerAttributes = $getAllowedCustomerAttributes; + } + + /** + * @param array $customerData + * @return void + * @throws GraphQlInputException + */ + public function execute(array $customerData): void + { + $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); + $errorInput = []; + + foreach ($attributes as $attributeName => $attributeInfo) { + if ($attributeInfo->getIsRequired() && empty($customerData[$attributeName])) { + $errorInput[] = $attributeName; + } + } + + if ($errorInput) { + throw new GraphQlInputException( + __('Required parameters are missing: %1', [implode(', ', $errorInput)]) + ); + } + } +} \ No newline at end of file From 40e23635353677c91bcfef1bb270af9a8f0eb016 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sun, 19 May 2019 12:11:28 +0300 Subject: [PATCH 06/16] #271: [My Account] Attribute validator fix --- .../CustomerGraphQl/Model/Customer/ValidateCustomerData.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index daefe1b16a21..23ca51efd595 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -36,9 +36,9 @@ public function execute(array $customerData): void $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); $errorInput = []; - foreach ($attributes as $attributeName => $attributeInfo) { - if ($attributeInfo->getIsRequired() && empty($customerData[$attributeName])) { - $errorInput[] = $attributeName; + foreach ($attributes as $attributeInfo) { + if ($attributeInfo->getIsRequired() && empty($customerData[$attributeInfo->getAttributeCode()])) { + $errorInput[] = $attributeInfo->getDefaultFrontendLabel(); } } From b4db736b7fece3719403fa10b7e10f25a590532c Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 25 May 2019 15:47:10 +0300 Subject: [PATCH 07/16] #271: [My Account] Adding required attributes from customer data model to validation --- .../Customer/GetAllowedCustomerAttributes.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 9ebc114432f6..03a2b96715c3 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -8,11 +8,14 @@ namespace Magento\CustomerGraphQl\Model\Customer; use Magento\Customer\Api\CustomerMetadataManagementInterface; +use Magento\Customer\Api\Data\CustomerInterface; +use Magento\Customer\Api\Data\CustomerInterfaceFactory; use Magento\Eav\Model\AttributeRepository; use Magento\Eav\Model\Entity\Attribute\AbstractAttribute; use Magento\Framework\Api\SearchCriteriaBuilder; use Magento\Framework\Exception\InputException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\Reflection\DataObjectProcessor; /** * Get allowed address attributes @@ -24,19 +27,38 @@ class GetAllowedCustomerAttributes */ private $attributeRepository; + /** + * @var CustomerInterfaceFactory\ + */ + private $customerDataFactory; + + /** + * @var DataObjectProcessor + */ + private $dataObjectProcessor; + /** * @var SearchCriteriaBuilder */ private $searchCriteriaBuilder; /** + * GetAllowedCustomerAttributes constructor. + * * @param AttributeRepository $attributeRepository + * @param CustomerInterfaceFactory $customerDataFactory + * @param DataObjectProcessor $dataObjectProcessor + * @param SearchCriteriaBuilder $searchCriteriaBuilder */ public function __construct( AttributeRepository $attributeRepository, + CustomerInterfaceFactory $customerDataFactory, + DataObjectProcessor $dataObjectProcessor, SearchCriteriaBuilder $searchCriteriaBuilder ) { $this->attributeRepository = $attributeRepository; + $this->customerDataFactory = $customerDataFactory; + $this->dataObjectProcessor = $dataObjectProcessor; $this->searchCriteriaBuilder = $searchCriteriaBuilder; } @@ -49,6 +71,13 @@ public function __construct( */ public function execute($attributeKeys): array { + /** @var CustomerInterface $customerDataDummy */ + $customerDataDummy = $this->customerDataFactory->create(); + $requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray( + $customerDataDummy, + CustomerInterface::class + ); + $attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes)); $this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); $searchCriteria = $this->searchCriteriaBuilder->create(); try { From 4b7acdc4996c9e543e52f5f6eb6b6fae92d8476b Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sun, 26 May 2019 10:02:24 +0300 Subject: [PATCH 08/16] #271: [My Account] Code style fixes --- .../Model/Customer/CreateCustomerAccount.php | 4 +++- .../Model/Customer/UpdateCustomerAccount.php | 2 ++ .../Model/Customer/ValidateCustomerData.php | 9 ++++++++- .../Magento/GraphQl/Customer/CreateCustomerTest.php | 2 -- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index 764a4702752d..2456f95254f2 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -51,12 +51,14 @@ class CreateCustomerAccount private $validateCustomerData; /** + * CreateCustomerAccount constructor. + * * @param DataObjectHelper $dataObjectHelper * @param CustomerInterfaceFactory $customerFactory * @param StoreManagerInterface $storeManager * @param AccountManagementInterface $accountManagement * @param ChangeSubscriptionStatus $changeSubscriptionStatus - * @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes + * @param ValidateCustomerData $validateCustomerData */ public function __construct( DataObjectHelper $dataObjectHelper, diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php index 3aab5c29d3d0..59353fb713af 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php @@ -82,6 +82,8 @@ public function __construct( } /** + * Update customer account + * * @param CustomerInterface $customer * @param array $data * @throws GraphQlAlreadyExistsException diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index 23ca51efd595..4d1ebba45269 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -9,9 +9,14 @@ use Magento\Framework\GraphQl\Exception\GraphQlInputException; +/** + * Class ValidateCustomerData + */ class ValidateCustomerData { /** + * Get allowed/required customer attributes + * * @var GetAllowedCustomerAttributes */ private $getAllowedCustomerAttributes; @@ -27,6 +32,8 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr } /** + * Validate customer data + * * @param array $customerData * @return void * @throws GraphQlInputException @@ -48,4 +55,4 @@ public function execute(array $customerData): void ); } } -} \ No newline at end of file +} diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php index d87a25ad3283..86360a71b1f6 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php @@ -292,6 +292,4 @@ public function tearDown() $this->registry->register('isSecureArea', false); parent::tearDown(); } - - } From 364162778b8ce82b0f9ab827ca1326eff37b3f9e Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 15 Jun 2019 10:40:43 +0300 Subject: [PATCH 09/16] #271: [My Account] API functional tests fix --- .../Magento/GraphQl/Customer/CreateCustomerTest.php | 4 ++-- .../Magento/GraphQl/Customer/UpdateCustomerTest.php | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php index 86360a71b1f6..1e6e0463fd31 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php @@ -139,7 +139,7 @@ public function testCreateCustomerIfInputDataIsEmpty() /** * @expectedException \Exception - * @expectedExceptionMessage The customer email is missing. Enter and try again. + * @expectedExceptionMessage Required parameters are missing: Email */ public function testCreateCustomerIfEmailMissed() { @@ -243,7 +243,7 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput() /** * @expectedException \Exception - * @expectedExceptionMessage Required parameters are missing: firstname + * @expectedExceptionMessage Required parameters are missing: First Name */ public function testCreateCustomerIfNameEmpty() { diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php index cd78018dc1ca..84dc6d4705cc 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php @@ -253,6 +253,8 @@ public function testUpdateEmailIfEmailAlreadyExists() $currentEmail = 'customer@example.com'; $currentPassword = 'password'; $existedEmail = 'customer_two@example.com'; + $firstname = 'Richard'; + $lastname = 'Rowe'; $query = << Date: Sat, 15 Jun 2019 16:56:47 +0300 Subject: [PATCH 10/16] #271: [My Account] Minor API test fix --- .../testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php | 2 +- .../testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php index 1e6e0463fd31..67c21a3798a5 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php @@ -273,7 +273,7 @@ public function testCreateCustomerIfNameEmpty() } } QUERY; - $this->graphQlQuery($query); + $this->graphQlMutation($query); } public function tearDown() diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php index 84dc6d4705cc..e9af6877151e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerTest.php @@ -300,7 +300,7 @@ public function testEmptyCustomerName() } } QUERY; - $this->graphQlQuery($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); + $this->graphQlMutation($query, [], '', $this->getCustomerAuthHeaders($currentEmail, $currentPassword)); } /** From cc69055aa67bda5c062836fe7137dfda1dca24eb Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 22 Jun 2019 09:45:05 +0300 Subject: [PATCH 11/16] #271: [My Account] Updating customer mutations validation: allowing partial customer data update --- .../Model/Customer/CreateCustomerAccount.php | 2 +- .../Customer/GetAllowedCustomerAttributes.php | 23 +++++++++++++------ .../Model/Customer/ValidateCustomerData.php | 7 ++++-- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index 2456f95254f2..6ec26853cf15 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -106,7 +106,7 @@ public function execute(array $data): CustomerInterface */ private function createAccount(array $data): CustomerInterface { - $this->validateCustomerData->execute($data); + $this->validateCustomerData->execute($data, true); $customerDataObject = $this->customerFactory->create(); $this->dataObjectHelper->populateWithArray( $customerDataObject, diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 03a2b96715c3..76186cc7423c 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -66,17 +66,26 @@ public function __construct( * Get allowed customer attributes * * @param array $attributeKeys + * @param $addRequiredAttributes + * * @throws GraphQlInputException + * * @return AbstractAttribute[] */ - public function execute($attributeKeys): array + public function execute($attributeKeys, $addRequiredAttributes = false): array { - /** @var CustomerInterface $customerDataDummy */ - $customerDataDummy = $this->customerDataFactory->create(); - $requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray( - $customerDataDummy, - CustomerInterface::class - ); + /** + * Add required attributes for customer entity to passed attribute keys + */ + if ($addRequiredAttributes) { + /** @var CustomerInterface $customerDataDummy */ + $customerDataDummy = $this->customerDataFactory->create(); + $requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray( + $customerDataDummy, + CustomerInterface::class + ); + } + $attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes)); $this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); $searchCriteria = $this->searchCriteriaBuilder->create(); diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index 4d1ebba45269..b9cf24337405 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -35,12 +35,15 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr * Validate customer data * * @param array $customerData + * @param $addRequiredAttributes + * * @return void + * * @throws GraphQlInputException */ - public function execute(array $customerData): void + public function execute(array $customerData, $addRequiredAttributes = false): void { - $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); + $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData), $addRequiredAttributes); $errorInput = []; foreach ($attributes as $attributeInfo) { From f358267f3928e961a3078c7755e588eaf182e576 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Sat, 22 Jun 2019 10:45:39 +0300 Subject: [PATCH 12/16] #271: [My Account] Minor fix --- .../Model/Customer/GetAllowedCustomerAttributes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 76186cc7423c..0c715a021096 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -84,9 +84,9 @@ public function execute($attributeKeys, $addRequiredAttributes = false): array $customerDataDummy, CustomerInterface::class ); + $attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes)); } - - $attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes)); + $this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); $searchCriteria = $this->searchCriteriaBuilder->create(); try { From eb48f449527a319bc2dfbe0237b832afb7be29e6 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Mon, 24 Jun 2019 20:51:31 +0300 Subject: [PATCH 13/16] #271: [My Account] Minor fixes for validation and exceptions handling --- .../Model/Customer/UpdateCustomerAccount.php | 12 +++++++++--- .../Model/Customer/ValidateCustomerData.php | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php index 59353fb713af..5b3eaaf92bb4 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php @@ -7,9 +7,11 @@ namespace Magento\CustomerGraphQl\Model\Customer; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException; use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException; 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\Framework\Api\DataObjectHelper; @@ -89,8 +91,7 @@ public function __construct( * @throws GraphQlAlreadyExistsException * @throws GraphQlAuthenticationException * @throws GraphQlInputException - * @throws \Magento\Framework\Exception\NoSuchEntityException - * @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException + * @throws GraphQlNoSuchEntityException */ public function execute(CustomerInterface $customer, array $data): void { @@ -106,7 +107,12 @@ public function execute(CustomerInterface $customer, array $data): void $filteredData = array_diff_key($data, array_flip($this->restrictedKeys)); $this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class); - $customer->setStoreId($this->storeManager->getStore()->getId()); + try { + $customer->setStoreId($this->storeManager->getStore()->getId()); + } catch (NoSuchEntityException $exception) { + throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception); + } + $this->saveCustomer->execute($customer); diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index b9cf24337405..1e4b653c13de 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -47,7 +47,7 @@ public function execute(array $customerData, $addRequiredAttributes = false): vo $errorInput = []; foreach ($attributes as $attributeInfo) { - if ($attributeInfo->getIsRequired() && empty($customerData[$attributeInfo->getAttributeCode()])) { + if ($attributeInfo->getIsRequired() && $customerData[$attributeInfo->getAttributeCode()] == '') { $errorInput[] = $attributeInfo->getDefaultFrontendLabel(); } } From 2eb7c155b1ec4496a874ba88363bec0775dc38c2 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Tue, 25 Jun 2019 10:16:01 +0300 Subject: [PATCH 14/16] #271: [My Account] Attributes validation: parameter missing in request fix --- .../CustomerGraphQl/Model/Customer/ValidateCustomerData.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index 1e4b653c13de..47386e515234 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -47,7 +47,10 @@ public function execute(array $customerData, $addRequiredAttributes = false): vo $errorInput = []; foreach ($attributes as $attributeInfo) { - if ($attributeInfo->getIsRequired() && $customerData[$attributeInfo->getAttributeCode()] == '') { + if ($attributeInfo->getIsRequired() + && (!isset($customerData[$attributeInfo->getAttributeCode()]) + || $customerData[$attributeInfo->getAttributeCode()] == '') + ) { $errorInput[] = $attributeInfo->getDefaultFrontendLabel(); } } From 1c30336296dbba00f3768ed74dfe84f95f536dd1 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Tue, 9 Jul 2019 22:49:01 +0300 Subject: [PATCH 15/16] #271: [My Account] Code style fixes --- .../Model/Customer/GetAllowedCustomerAttributes.php | 2 +- .../CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php | 1 - .../CustomerGraphQl/Model/Customer/ValidateCustomerData.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 0c715a021096..271f6003167f 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -66,7 +66,7 @@ public function __construct( * Get allowed customer attributes * * @param array $attributeKeys - * @param $addRequiredAttributes + * @param bool $addRequiredAttributes * * @throws GraphQlInputException * diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php index 5b3eaaf92bb4..da0ab7b2d884 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php @@ -113,7 +113,6 @@ public function execute(CustomerInterface $customer, array $data): void throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception); } - $this->saveCustomer->execute($customer); if (isset($data['is_subscribed'])) { diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index 47386e515234..82759688a6b6 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -35,7 +35,7 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr * Validate customer data * * @param array $customerData - * @param $addRequiredAttributes + * @param bool $addRequiredAttributes * * @return void * From da8d6ac580f3b3e47f913b7f0618e330a8693fc8 Mon Sep 17 00:00:00 2001 From: Ihor Furseyev Date: Thu, 18 Jul 2019 18:40:18 +0300 Subject: [PATCH 16/16] #271: [My Account] Refactoring the addition of required attributes to validation of customer creation --- .../Model/Customer/CreateCustomerAccount.php | 19 ++++++++++++++++++- .../Customer/GetAllowedCustomerAttributes.php | 16 +--------------- .../Model/Customer/ValidateCustomerData.php | 5 ++--- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php index 6ec26853cf15..f2beb1d6e98c 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php @@ -13,6 +13,7 @@ use Magento\Framework\Api\DataObjectHelper; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\GraphQl\Exception\GraphQlInputException; +use Magento\Framework\Reflection\DataObjectProcessor; use Magento\Store\Model\StoreManagerInterface; /** @@ -50,6 +51,11 @@ class CreateCustomerAccount */ private $validateCustomerData; + /** + * @var DataObjectProcessor + */ + private $dataObjectProcessor; + /** * CreateCustomerAccount constructor. * @@ -58,6 +64,7 @@ class CreateCustomerAccount * @param StoreManagerInterface $storeManager * @param AccountManagementInterface $accountManagement * @param ChangeSubscriptionStatus $changeSubscriptionStatus + * @param DataObjectProcessor $dataObjectProcessor * @param ValidateCustomerData $validateCustomerData */ public function __construct( @@ -66,6 +73,7 @@ public function __construct( StoreManagerInterface $storeManager, AccountManagementInterface $accountManagement, ChangeSubscriptionStatus $changeSubscriptionStatus, + DataObjectProcessor $dataObjectProcessor, ValidateCustomerData $validateCustomerData ) { $this->dataObjectHelper = $dataObjectHelper; @@ -74,6 +82,7 @@ public function __construct( $this->storeManager = $storeManager; $this->changeSubscriptionStatus = $changeSubscriptionStatus; $this->validateCustomerData = $validateCustomerData; + $this->dataObjectProcessor = $dataObjectProcessor; } /** @@ -106,8 +115,16 @@ public function execute(array $data): CustomerInterface */ private function createAccount(array $data): CustomerInterface { - $this->validateCustomerData->execute($data, true); $customerDataObject = $this->customerFactory->create(); + /** + * Add required attributes for customer entity + */ + $requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray( + $customerDataObject, + CustomerInterface::class + ); + $data = array_merge($requiredDataAttributes, $data); + $this->validateCustomerData->execute($data); $this->dataObjectHelper->populateWithArray( $customerDataObject, $data, diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php index 271f6003167f..aada79aa016b 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php @@ -66,27 +66,13 @@ public function __construct( * Get allowed customer attributes * * @param array $attributeKeys - * @param bool $addRequiredAttributes * * @throws GraphQlInputException * * @return AbstractAttribute[] */ - public function execute($attributeKeys, $addRequiredAttributes = false): array + public function execute($attributeKeys): array { - /** - * Add required attributes for customer entity to passed attribute keys - */ - if ($addRequiredAttributes) { - /** @var CustomerInterface $customerDataDummy */ - $customerDataDummy = $this->customerDataFactory->create(); - $requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray( - $customerDataDummy, - CustomerInterface::class - ); - $attributeKeys = array_merge($attributeKeys, array_keys($requiredDataAttributes)); - } - $this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in'); $searchCriteria = $this->searchCriteriaBuilder->create(); try { diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php index 82759688a6b6..794cb0048592 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php @@ -35,15 +35,14 @@ public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttr * Validate customer data * * @param array $customerData - * @param bool $addRequiredAttributes * * @return void * * @throws GraphQlInputException */ - public function execute(array $customerData, $addRequiredAttributes = false): void + public function execute(array $customerData): void { - $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData), $addRequiredAttributes); + $attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); $errorInput = []; foreach ($attributes as $attributeInfo) {