forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENGCOM-5606: magento#271 [My Account] Add support of Customer attribu…
…tes magento#489
- Loading branch information
Showing
6 changed files
with
277 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
app/code/Magento/CustomerGraphQl/Model/Customer/GetAllowedCustomerAttributes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
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 | ||
*/ | ||
class GetAllowedCustomerAttributes | ||
{ | ||
/** | ||
* @var AttributeRepository | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* Get allowed customer attributes | ||
* | ||
* @param array $attributeKeys | ||
* | ||
* @throws GraphQlInputException | ||
* | ||
* @return AbstractAttribute[] | ||
*/ | ||
public function execute($attributeKeys): array | ||
{ | ||
$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[$index]); | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
app/code/Magento/CustomerGraphQl/Model/Customer/ValidateCustomerData.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Customer; | ||
|
||
use Magento\Framework\GraphQl\Exception\GraphQlInputException; | ||
|
||
/** | ||
* Class ValidateCustomerData | ||
*/ | ||
class ValidateCustomerData | ||
{ | ||
/** | ||
* Get allowed/required customer attributes | ||
* | ||
* @var GetAllowedCustomerAttributes | ||
*/ | ||
private $getAllowedCustomerAttributes; | ||
|
||
/** | ||
* ValidateCustomerData constructor. | ||
* | ||
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes | ||
*/ | ||
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes) | ||
{ | ||
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes; | ||
} | ||
|
||
/** | ||
* Validate customer data | ||
* | ||
* @param array $customerData | ||
* | ||
* @return void | ||
* | ||
* @throws GraphQlInputException | ||
*/ | ||
public function execute(array $customerData): void | ||
{ | ||
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData)); | ||
$errorInput = []; | ||
|
||
foreach ($attributes as $attributeInfo) { | ||
if ($attributeInfo->getIsRequired() | ||
&& (!isset($customerData[$attributeInfo->getAttributeCode()]) | ||
|| $customerData[$attributeInfo->getAttributeCode()] == '') | ||
) { | ||
$errorInput[] = $attributeInfo->getDefaultFrontendLabel(); | ||
} | ||
} | ||
|
||
if ($errorInput) { | ||
throw new GraphQlInputException( | ||
__('Required parameters are missing: %1', [implode(', ', $errorInput)]) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
{ | ||
|
@@ -241,6 +241,41 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput() | |
$this->graphQlMutation($query); | ||
} | ||
|
||
/** | ||
* @expectedException \Exception | ||
* @expectedExceptionMessage Required parameters are missing: First Name | ||
*/ | ||
public function testCreateCustomerIfNameEmpty() | ||
{ | ||
$newEmail = 'customer_created' . rand(1, 2000000) . '@example.com'; | ||
$newFirstname = ''; | ||
$newLastname = 'Rowe'; | ||
$currentPassword = 'test123#'; | ||
|
||
$query = <<<QUERY | ||
mutation { | ||
createCustomer( | ||
input: { | ||
email: "{$newEmail}" | ||
firstname: "{$newFirstname}" | ||
lastname: "{$newLastname}" | ||
password: "{$currentPassword}" | ||
is_subscribed: true | ||
} | ||
) { | ||
customer { | ||
id | ||
firstname | ||
lastname | ||
is_subscribed | ||
} | ||
} | ||
} | ||
QUERY; | ||
$this->graphQlMutation($query); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
$newEmail = '[email protected]'; | ||
|
Oops, something went wrong.