Skip to content

Commit 5d615b2

Browse files
authored
ENGCOM-7750: #28481: GraphQl. updateCustomer allows to set any INT value in gender argument #28487
2 parents 2efa700 + 5843c0b commit 5d615b2

File tree

7 files changed

+240
-27
lines changed

7 files changed

+240
-27
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Api;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
12+
/**
13+
* Interface for customer data validator
14+
*/
15+
interface ValidateCustomerDataInterface
16+
{
17+
/**
18+
* Validate customer data
19+
*
20+
* @param array $customerData
21+
* @throws GraphQlInputException
22+
*/
23+
public function execute(array $customerData): void;
24+
}

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

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
11+
use Magento\Framework\Exception\LocalizedException;
12+
use Magento\Framework\Exception\NoSuchEntityException;
1013
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1114
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
1215

@@ -27,53 +30,41 @@ class ValidateCustomerData
2730
*/
2831
private $emailAddressValidator;
2932

33+
/**
34+
* @var ValidateCustomerDataInterface[]
35+
*/
36+
private $validators = [];
37+
3038
/**
3139
* ValidateCustomerData constructor.
3240
*
3341
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
3442
* @param EmailAddressValidator $emailAddressValidator
43+
* @param array $validators
3544
*/
3645
public function __construct(
3746
GetAllowedCustomerAttributes $getAllowedCustomerAttributes,
38-
EmailAddressValidator $emailAddressValidator
47+
EmailAddressValidator $emailAddressValidator,
48+
$validators = []
3949
) {
4050
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
4151
$this->emailAddressValidator = $emailAddressValidator;
52+
$this->validators = $validators;
4253
}
4354

4455
/**
4556
* Validate customer data
4657
*
4758
* @param array $customerData
48-
*
49-
* @return void
50-
*
5159
* @throws GraphQlInputException
60+
* @throws LocalizedException
61+
* @throws NoSuchEntityException
5262
*/
53-
public function execute(array $customerData): void
63+
public function execute(array $customerData)
5464
{
55-
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
56-
$errorInput = [];
57-
58-
foreach ($attributes as $attributeInfo) {
59-
if ($attributeInfo->getIsRequired()
60-
&& (!isset($customerData[$attributeInfo->getAttributeCode()])
61-
|| $customerData[$attributeInfo->getAttributeCode()] == '')
62-
) {
63-
$errorInput[] = $attributeInfo->getDefaultFrontendLabel();
64-
}
65-
}
66-
67-
if ($errorInput) {
68-
throw new GraphQlInputException(
69-
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
70-
);
71-
}
72-
73-
if (isset($customerData['email']) && !$this->emailAddressValidator->isValid($customerData['email'])) {
74-
throw new GraphQlInputException(
75-
__('"%1" is not a valid email address.', $customerData['email'])
76-
);
65+
/** @var ValidateCustomerDataInterface $validator */
66+
foreach ($this->validators as $validator) {
67+
$validator->execute($customerData);
7768
}
7869
}
7970
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData;
9+
10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\Framework\Validator\EmailAddress as EmailAddressValidator;
13+
14+
/**
15+
* Validates an email
16+
*/
17+
class ValidateEmail implements ValidateCustomerDataInterface
18+
{
19+
/**
20+
* @var EmailAddressValidator
21+
*/
22+
private $emailAddressValidator;
23+
24+
/**
25+
* ValidateEmail constructor.
26+
*
27+
* @param EmailAddressValidator $emailAddressValidator
28+
*/
29+
public function __construct(EmailAddressValidator $emailAddressValidator)
30+
{
31+
$this->emailAddressValidator = $emailAddressValidator;
32+
}
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
public function execute(array $customerData): void
38+
{
39+
if (isset($customerData['email']) && !$this->emailAddressValidator->isValid($customerData['email'])) {
40+
throw new GraphQlInputException(
41+
__('"%1" is not a valid email address.', $customerData['email'])
42+
);
43+
}
44+
}
45+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData;
9+
10+
use Magento\Customer\Api\CustomerMetadataInterface;
11+
use Magento\Customer\Model\Data\AttributeMetadata;
12+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
13+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
14+
15+
/**
16+
* Validates gender value
17+
*/
18+
class ValidateGender implements ValidateCustomerDataInterface
19+
{
20+
/**
21+
* @var CustomerMetadataInterface
22+
*/
23+
private $customerMetadata;
24+
25+
/**
26+
* ValidateGender constructor.
27+
*
28+
* @param CustomerMetadataInterface $customerMetadata
29+
*/
30+
public function __construct(CustomerMetadataInterface $customerMetadata)
31+
{
32+
$this->customerMetadata = $customerMetadata;
33+
}
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function execute(array $customerData): void
39+
{
40+
if (isset($customerData['gender']) && $customerData['gender']) {
41+
/** @var AttributeMetadata $genderData */
42+
$options = $this->customerMetadata->getAttributeMetadata('gender')->getOptions();
43+
44+
$isValid = false;
45+
foreach ($options as $optionData) {
46+
if ($optionData->getValue() && $optionData->getValue() == $customerData['gender']) {
47+
$isValid = true;
48+
}
49+
}
50+
51+
if (!$isValid) {
52+
throw new GraphQlInputException(
53+
__('"%1" is not a valid gender value.', $customerData['gender'])
54+
);
55+
}
56+
}
57+
}
58+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData;
9+
10+
use Magento\CustomerGraphQl\Api\ValidateCustomerDataInterface;
11+
use Magento\CustomerGraphQl\Model\Customer\GetAllowedCustomerAttributes;
12+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13+
14+
/**
15+
* Validates required attributes
16+
*/
17+
class ValidateRequiredArguments implements ValidateCustomerDataInterface
18+
{
19+
/**
20+
* Get allowed/required customer attributes
21+
*
22+
* @var GetAllowedCustomerAttributes
23+
*/
24+
private $getAllowedCustomerAttributes;
25+
26+
/**
27+
* ValidateRequiredArguments constructor.
28+
*
29+
* @param GetAllowedCustomerAttributes $getAllowedCustomerAttributes
30+
*/
31+
public function __construct(GetAllowedCustomerAttributes $getAllowedCustomerAttributes)
32+
{
33+
$this->getAllowedCustomerAttributes = $getAllowedCustomerAttributes;
34+
}
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function execute(array $customerData): void
40+
{
41+
$attributes = $this->getAllowedCustomerAttributes->execute(array_keys($customerData));
42+
$errorInput = [];
43+
44+
foreach ($attributes as $attributeInfo) {
45+
if ($attributeInfo->getIsRequired()
46+
&& (!isset($customerData[$attributeInfo->getAttributeCode()])
47+
|| $customerData[$attributeInfo->getAttributeCode()] == '')
48+
) {
49+
$errorInput[] = $attributeInfo->getDefaultFrontendLabel();
50+
}
51+
}
52+
53+
if ($errorInput) {
54+
throw new GraphQlInputException(
55+
__('Required parameters are missing: %1', [implode(', ', $errorInput)])
56+
);
57+
}
58+
}
59+
}

app/code/Magento/CustomerGraphQl/etc/graphql/di.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@
2929
</argument>
3030
</arguments>
3131
</type>
32+
<!-- Validate input customer data -->
33+
<type name="Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData">
34+
<arguments>
35+
<argument name="validators" xsi:type="array">
36+
<item name="validateRequiredArguments" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateRequiredArguments</item>
37+
<item name="validateEmail" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateEmail</item>
38+
<item name="validateGender" xsi:type="object">Magento\CustomerGraphQl\Model\Customer\ValidateCustomerData\ValidateGender</item>
39+
</argument>
40+
</arguments>
41+
</type>
3242
</config>

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,32 @@ public function testEmptyCustomerLastName()
368368
$this->graphQlMutation($query, [], '', $this->getCustomerAuthHeaders('customer@example.com', 'password'));
369369
}
370370

371+
/**
372+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
373+
*/
374+
public function testUpdateCustomerWithIncorrectGender()
375+
{
376+
$gender = 5;
377+
378+
$this->expectException(Exception::class);
379+
$this->expectExceptionMessage('"' . $gender . '" is not a valid gender value.');
380+
381+
$query = <<<QUERY
382+
mutation {
383+
updateCustomer(
384+
input: {
385+
gender: {$gender}
386+
}
387+
) {
388+
customer {
389+
gender
390+
}
391+
}
392+
}
393+
QUERY;
394+
$this->graphQlMutation($query, [], '', $this->getCustomerAuthHeaders('customer@example.com', 'password'));
395+
}
396+
371397
/**
372398
* @magentoApiDataFixture Magento/Customer/_files/customer.php
373399
*/

0 commit comments

Comments
 (0)