-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
LYNX-103: Customer GraphQL CustomerAttributeMetadata implementation o…
…f AttributeMetadataInterface (#98)
- Loading branch information
Showing
13 changed files
with
443 additions
and
43 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
122 changes: 122 additions & 0 deletions
122
app/code/Magento/CustomerGraphQl/Model/Output/CustomerAttributeMetadata.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,122 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\CustomerGraphQl\Model\Output; | ||
|
||
use Magento\Customer\Api\MetadataInterface; | ||
use Magento\Customer\Model\Data\ValidationRule; | ||
use Magento\Eav\Api\Data\AttributeInterface; | ||
use Magento\EavGraphQl\Model\Output\GetAttributeDataInterface; | ||
use Magento\EavGraphQl\Model\Uid as AttributeUid; | ||
use Magento\Framework\Exception\LocalizedException; | ||
use Magento\Framework\Exception\NoSuchEntityException; | ||
use Magento\Framework\GraphQl\Query\EnumLookup; | ||
use Magento\Framework\GraphQl\Query\Uid; | ||
|
||
/** | ||
* Format attributes metadata for GraphQL output | ||
*/ | ||
class CustomerAttributeMetadata implements GetAttributeDataInterface | ||
{ | ||
/** | ||
* @var AttributeUid | ||
*/ | ||
private AttributeUid $attributeUid; | ||
|
||
/** | ||
* @var Uid | ||
*/ | ||
private Uid $uid; | ||
|
||
/** | ||
* @var EnumLookup | ||
*/ | ||
private EnumLookup $enumLookup; | ||
|
||
/** | ||
* @var MetadataInterface | ||
*/ | ||
private MetadataInterface $metadata; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private string $entityType; | ||
|
||
/** | ||
* @param AttributeUid $attributeUid | ||
* @param Uid $uid | ||
* @param EnumLookup $enumLookup | ||
* @param MetadataInterface $metadata | ||
* @param string $entityType | ||
*/ | ||
public function __construct( | ||
AttributeUid $attributeUid, | ||
Uid $uid, | ||
EnumLookup $enumLookup, | ||
MetadataInterface $metadata, | ||
string $entityType | ||
) { | ||
$this->attributeUid = $attributeUid; | ||
$this->uid = $uid; | ||
$this->enumLookup = $enumLookup; | ||
$this->metadata = $metadata; | ||
$this->entityType = $entityType; | ||
} | ||
|
||
/** | ||
* Retrieve formatted attribute data | ||
* | ||
* @param AttributeInterface $attribute | ||
* @param string $entityType | ||
* @param int $storeId | ||
* @return array | ||
* @throws LocalizedException | ||
* @throws NoSuchEntityException | ||
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | ||
*/ | ||
public function execute( | ||
AttributeInterface $attribute, | ||
string $entityType, | ||
int $storeId | ||
): array { | ||
if ($entityType !== $this->entityType) { | ||
return []; | ||
} | ||
|
||
$attributeMetadata = $this->metadata->getAttributeMetadata($attribute->getAttributeCode()); | ||
$data = []; | ||
|
||
$validationRules = array_map(function (ValidationRule $validationRule) { | ||
return [ | ||
'name' => $this->enumLookup->getEnumValueFromField( | ||
'ValidationRuleEnum', | ||
strtoupper($validationRule->getName()) | ||
), | ||
'value' => $validationRule->getValue() | ||
]; | ||
}, $attributeMetadata->getValidationRules()); | ||
|
||
if ($attributeMetadata->isVisible()) { | ||
$data = [ | ||
'input_filter' => | ||
empty($attributeMetadata->getInputFilter()) | ||
? 'NONE' | ||
: $this->enumLookup->getEnumValueFromField( | ||
'InputFilterEnum', | ||
strtoupper($attributeMetadata->getInputFilter()) | ||
), | ||
'multiline_count' => $attributeMetadata->getMultilineCount(), | ||
'sort_order' => $attributeMetadata->getSortOrder(), | ||
'validate_rules' => $validationRules, | ||
'attributeMetadata' => $attributeMetadata | ||
]; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
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
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
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
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
107 changes: 107 additions & 0 deletions
107
...functional/testsuite/Magento/GraphQl/Customer/Attribute/CustomerAddressAttributesTest.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,107 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\GraphQl\Customer\Attribute; | ||
|
||
use Magento\Customer\Api\AddressMetadataInterface; | ||
use Magento\Customer\Api\Data\AttributeMetadataInterface; | ||
use Magento\Customer\Test\Fixture\CustomerAttribute; | ||
use Magento\EavGraphQl\Model\Uid; | ||
use Magento\TestFramework\Fixture\DataFixture; | ||
use Magento\TestFramework\Fixture\DataFixtureStorageManager; | ||
use Magento\TestFramework\Helper\Bootstrap; | ||
use Magento\TestFramework\TestCase\GraphQlAbstract; | ||
|
||
/** | ||
* Test catalog EAV attributes metadata retrieval via GraphQL API | ||
*/ | ||
class CustomerAddressAttributesTest extends GraphQlAbstract | ||
{ | ||
private const QUERY = <<<QRY | ||
{ | ||
attributesMetadata(attributes: [{attribute_code: "%s", entity_type: "%s"}]) { | ||
items { | ||
uid | ||
code | ||
label | ||
entity_type | ||
frontend_input | ||
is_required | ||
default_value | ||
is_unique | ||
... on CustomerAttributeMetadata { | ||
input_filter | ||
validate_rules { | ||
name | ||
value | ||
} | ||
} | ||
} | ||
errors { | ||
type | ||
message | ||
} | ||
} | ||
} | ||
QRY; | ||
|
||
#[ | ||
DataFixture( | ||
CustomerAttribute::class, | ||
[ | ||
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS, | ||
'frontend_input' => 'date', | ||
'default_value' => '2023-03-22 00:00:00', | ||
'input_filter' => 'DATE', | ||
'validate_rules' => | ||
'{"DATE_RANGE_MIN":"1679443200","DATE_RANGE_MAX":"1679875200","INPUT_VALIDATION":"DATE"}' | ||
], | ||
'attribute' | ||
), | ||
] | ||
public function testMetadata(): void | ||
{ | ||
/** @var AttributeMetadataInterface $attribute */ | ||
$attribute = DataFixtureStorageManager::getStorage()->get('attribute'); | ||
|
||
$uid = Bootstrap::getObjectManager()->get(Uid::class)->encode( | ||
'customer_address', | ||
$attribute->getAttributeCode() | ||
); | ||
|
||
$formattedValidationRules = Bootstrap::getObjectManager()->get(FormatValidationRulesCommand::class)->execute( | ||
$attribute->getValidationRules() | ||
); | ||
|
||
$result = $this->graphQlQuery( | ||
sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer_address') | ||
); | ||
|
||
$this->assertEquals( | ||
[ | ||
'attributesMetadata' => [ | ||
'items' => [ | ||
[ | ||
'uid' => $uid, | ||
'code' => $attribute->getAttributeCode(), | ||
'label' => $attribute->getFrontendLabel(), | ||
'entity_type' => 'CUSTOMER_ADDRESS', | ||
'frontend_input' => 'DATE', | ||
'is_required' => false, | ||
'default_value' => $attribute->getDefaultValue(), | ||
'is_unique' => false, | ||
'input_filter' => $attribute->getInputFilter(), | ||
'validate_rules' => $formattedValidationRules | ||
] | ||
], | ||
'errors' => [] | ||
] | ||
], | ||
$result | ||
); | ||
} | ||
} |
Oops, something went wrong.