From eb5ca286580d2de31da286434fbd41edb91e8872 Mon Sep 17 00:00:00 2001 From: eliseacornejo Date: Fri, 21 Apr 2023 10:29:51 +0200 Subject: [PATCH] LYNX-158: Replace uid input with combinations of entity_type, attribute_code in attributesMetadata query (#103) * LYNX-158: Replace uid input with combinations of entity_type, attribute_code in attributesMetadata query --- .../Model/Customer/GetAttributesForm.php | 12 +---- .../Model/GetAttributesMetadata.php | 27 +++-------- .../Model/Resolver/AttributesMetadata.php | 23 ++++++++-- .../Magento/EavGraphQl/etc/schema.graphqls | 7 +-- .../Customer/Attribute/BooleanTest.php | 4 +- .../GraphQl/Customer/Attribute/DateTest.php | 4 +- .../GraphQl/Customer/Attribute/FileTest.php | 4 +- .../GraphQl/Customer/Attribute/ImageTest.php | 4 +- .../Customer/Attribute/MultilineTest.php | 4 +- .../Customer/Attribute/MultiselectTest.php | 4 +- .../GraphQl/Customer/Attribute/SelectTest.php | 4 +- .../Attribute/StoreViewOptionsTest.php | 11 +++-- .../GraphQl/Customer/Attribute/TextTest.php | 45 +++++++++++-------- .../Customer/Attribute/TextareaTest.php | 4 +- 14 files changed, 77 insertions(+), 80 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php index 727379f40c289..8c476abba90bc 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/GetAttributesForm.php @@ -9,7 +9,6 @@ use Magento\Customer\Api\MetadataInterface; use Magento\EavGraphQl\Model\GetAttributesFormInterface; -use Magento\EavGraphQl\Model\Uid; /** * Attributes form provider for customer @@ -21,11 +20,6 @@ class GetAttributesForm implements GetAttributesFormInterface */ private MetadataInterface $entity; - /** - * @var Uid - */ - private Uid $uid; - /** * @var string */ @@ -33,13 +27,11 @@ class GetAttributesForm implements GetAttributesFormInterface /** * @param MetadataInterface $metadata - * @param Uid $uid * @param string $type */ - public function __construct(MetadataInterface $metadata, Uid $uid, string $type) + public function __construct(MetadataInterface $metadata, string $type) { $this->entity = $metadata; - $this->uid = $uid; $this->type = $type; } @@ -50,7 +42,7 @@ public function execute(string $formCode): ?array { $attributes = []; foreach ($this->entity->getAttributes($formCode) as $attribute) { - $attributes[] = $this->uid->encode($this->type, $attribute->getAttributeCode()); + $attributes[] = ['entity_type' => $this->type, 'attribute_code' => $attribute->getAttributeCode()]; } return $attributes; } diff --git a/app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php b/app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php index 90f826923cb0a..236e5e0be005e 100644 --- a/app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php +++ b/app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php @@ -13,18 +13,12 @@ use Magento\Framework\Api\SearchCriteriaBuilderFactory; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\RuntimeException; -use Magento\Framework\GraphQl\Exception\GraphQlInputException; /** * Retrieve EAV attributes details */ class GetAttributesMetadata { - /** - * @var Uid - */ - private Uid $uid; - /** * @var AttributeRepositoryInterface */ @@ -43,48 +37,37 @@ class GetAttributesMetadata /** * @param AttributeRepositoryInterface $attributeRepository * @param SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory - * @param Uid $uid * @param GetAttributeDataInterface $getAttributeData */ public function __construct( AttributeRepositoryInterface $attributeRepository, SearchCriteriaBuilderFactory $searchCriteriaBuilderFactory, - Uid $uid, GetAttributeDataInterface $getAttributeData ) { $this->attributeRepository = $attributeRepository; $this->searchCriteriaBuilderFactory = $searchCriteriaBuilderFactory; - $this->uid = $uid; $this->getAttributeData = $getAttributeData; } /** * Get attribute metadata details * - * @param string[] $uids + * @param array $attributesInputs * @param int $storeId * @return array * @throws RuntimeException */ - public function execute(array $uids, int $storeId): array + public function execute(array $attributesInputs, int $storeId): array { - if (empty($uids)) { + if (empty($attributesInputs)) { return []; } $codes = []; $errors = []; - foreach ($uids as $uid) { - try { - list($entityType, $attributeCode) = $this->uid->decode($uid); - $codes[$entityType][] = $attributeCode; - } catch (GraphQlInputException $exception) { - $errors[] = [ - 'type' => 'INCORRECT_UID', - 'message' => $exception->getMessage() - ]; - } + foreach ($attributesInputs as $attributeInput) { + $codes[$attributeInput['entity_type']][] = $attributeInput['attribute_code']; } $items = []; diff --git a/app/code/Magento/EavGraphQl/Model/Resolver/AttributesMetadata.php b/app/code/Magento/EavGraphQl/Model/Resolver/AttributesMetadata.php index 93fa07de159d6..f4f9853c1b6ab 100644 --- a/app/code/Magento/EavGraphQl/Model/Resolver/AttributesMetadata.php +++ b/app/code/Magento/EavGraphQl/Model/Resolver/AttributesMetadata.php @@ -14,7 +14,7 @@ use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; /** - * Load EAV attributes by UIDs + * Load EAV attributes by attribute_code and entity_type */ class AttributesMetadata implements ResolverInterface { @@ -42,12 +42,27 @@ public function resolve( array $value = null, array $args = null ) { - if (empty($args['input']['uids']) || !is_array($args['input']['uids'])) { - throw new GraphQlInputException(__('Required parameter "%1" of type array.', 'uids')); + $attributeInputs = $args['attributes']; + + if (empty($attributeInputs)) { + throw new GraphQlInputException( + __( + 'Required parameters "attribute_code" and "entity_type" of type String.' + ) + ); + } + + foreach ($attributeInputs as $attributeInput) { + if (!isset($attributeInput['attribute_code'])) { + throw new GraphQlInputException(__('The attribute_code is required to retrieve the metadata')); + } + if (!isset($attributeInput['entity_type'])) { + throw new GraphQlInputException(__('The entity_type is required to retrieve the metadata')); + } } return $this->getAttributesMetadata->execute( - $args['input']['uids'], + $attributeInputs, (int) $context->getExtensionAttributes()->getStore()->getId() ); } diff --git a/app/code/Magento/EavGraphQl/etc/schema.graphqls b/app/code/Magento/EavGraphQl/etc/schema.graphqls index 250d9baf59dad..4890c1544bf54 100644 --- a/app/code/Magento/EavGraphQl/etc/schema.graphqls +++ b/app/code/Magento/EavGraphQl/etc/schema.graphqls @@ -3,7 +3,7 @@ type Query { customAttributeMetadata(attributes: [AttributeInput!]! @doc(description: "An input object that specifies the attribute code and entity type to search.")): CustomAttributeMetadata @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\CustomAttributeMetadata") @doc(description: "Return the attribute type, given an attribute code and entity type.") @cache(cacheIdentity: "Magento\\EavGraphQl\\Model\\Resolver\\Cache\\CustomAttributeMetadataIdentity") - attributesMetadata(input: AttributesMetadataInput!): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.") + attributesMetadata(attributes: [AttributeInput!]): AttributesMetadataOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesMetadata") @doc(description: "Retrieve EAV attributes metadata.") attributesForm(type: String! @doc(description: "Form type")): AttributesFormOutput! @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesForm") @doc(description: "Retrieve EAV attributes associated to a frontend form.") attributesList(entityType: AttributeEntityTypeEnum! @doc(description: "Entity type.")): AttributesMetadataOutput @resolver(class: "Magento\\EavGraphQl\\Model\\Resolver\\AttributesList") @doc(description: "Returns list of atributes metadata for given entity type.") @cache(cacheable: false) } @@ -45,10 +45,6 @@ input AttributeInput @doc(description: "Defines the attribute characteristics to entity_type: String @doc(description: "The type of entity that defines the attribute.") } -input AttributesMetadataInput @doc(description: "attributesMetadata query input.") { - uids: [ID!]! @doc(description: "UIDs of attributes to query.") -} - type AttributesMetadataOutput @doc(description: "Metadata of EAV attributes.") { items: [AttributeMetadataInterface!]! @doc(description: "Requested attributes metadata.") errors: [AttributeMetadataError!]! @doc(description: "Errors of retrieving certain attributes metadata.") @@ -60,7 +56,6 @@ type AttributeMetadataError @doc(description: "Attribute metadata retrieval erro } enum AttributeMetadataErrorType @doc(description: "Attribute metadata retrieval error types.") { - INCORRECT_UID @doc(description: "The UID of the attribute is corrupted.") ENTITY_NOT_FOUND @doc(description: "The requested entity was not found.") ATTRIBUTE_NOT_FOUND @doc(description: "The requested attribute was not found.") UNDEFINED @doc(description: "Not categorized error, see the error message.") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/BooleanTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/BooleanTest.php index dea32f77b8109..de23fbf52a5a0 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/BooleanTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/BooleanTest.php @@ -23,7 +23,7 @@ class BooleanTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/DateTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/DateTest.php index c5419b4f21175..190e81bea0fbd 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/DateTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/DateTest.php @@ -23,7 +23,7 @@ class DateTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/FileTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/FileTest.php index d015c79af9d3e..d88c19df82309 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/FileTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/FileTest.php @@ -23,7 +23,7 @@ class FileTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/ImageTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/ImageTest.php index 43448b4db62cb..fe2dfb19d9472 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/ImageTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/ImageTest.php @@ -23,7 +23,7 @@ class ImageTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultilineTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultilineTest.php index 41fd2b37a38c1..19687ee64fa5a 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultilineTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultilineTest.php @@ -23,7 +23,7 @@ class MultilineTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php index c3e8412605680..e5bb65efdcd39 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/MultiselectTest.php @@ -26,7 +26,7 @@ class MultiselectTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/SelectTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/SelectTest.php index 02a537361c6d8..846efe4472d8d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/SelectTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/SelectTest.php @@ -27,7 +27,7 @@ class SelectTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [ diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/StoreViewOptionsTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/StoreViewOptionsTest.php index 929c36d6632c9..70ef969c0366d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/StoreViewOptionsTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/StoreViewOptionsTest.php @@ -99,7 +99,7 @@ class StoreViewOptionsTest extends GraphQlAbstract { private const QUERY = <<graphQlQuery( sprintf( self::QUERY, - $uid + $attribute->getAttributeCode(), + 'customer' ) ) ); @@ -219,7 +220,8 @@ public function testAttributeLabelsMultipleStoreViews(): void $this->graphQlQuery( sprintf( self::QUERY, - $uid + $attribute->getAttributeCode(), + 'customer' ), [], '', @@ -256,7 +258,8 @@ public function testAttributeLabelsMultipleStoreViews(): void $this->graphQlQuery( sprintf( self::QUERY, - $uid + $attribute->getAttributeCode(), + 'customer' ), [], '', diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextTest.php index 2f7c43466e5ae..f81533a02ee5c 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextTest.php @@ -23,7 +23,7 @@ class TextTest extends GraphQlAbstract { private const QUERY = << [] ] ], - $this->graphQlQuery(sprintf(self::QUERY, $uid)) + $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')) ); } - public function testErrors(): void + public function testErrorEntityNotFound(): void { - $nonExistingEntity = Bootstrap::getObjectManager()->get(Uid::class)->encode( - 'non_existing_entity_type', - 'name' - ); - $nonExistingAttributeCode = Bootstrap::getObjectManager()->get(Uid::class)->encode( - 'catalog_product', - 'non_existing_code' - ); $this->assertEquals( [ 'attributesMetadata' => [ 'items' => [], 'errors' => [ - [ - 'type' => 'INCORRECT_UID', - 'message' => 'Value of uid "incorrect" is incorrect.' - ], [ 'type' => 'ENTITY_NOT_FOUND', 'message' => 'Entity "non_existing_entity_type" could not be found.' - ], + ] + ] + ] + ], + $this->graphQlQuery( + sprintf( + self::QUERY, + 'lastname', + 'non_existing_entity_type' + ) + ) + ); + } + + public function testErrorAttributeNotFound(): void + { + $this->assertEquals( + [ + 'attributesMetadata' => [ + 'items' => [], + 'errors' => [ [ 'type' => 'ATTRIBUTE_NOT_FOUND', 'message' => 'Attribute code "non_existing_code" could not be found.' - ], + ] ] ] ], $this->graphQlQuery( sprintf( self::QUERY, - implode('","', ['incorrect', $nonExistingEntity, $nonExistingAttributeCode]) + 'non_existing_code', + 'customer' ) ) ); diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextareaTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextareaTest.php index 6ff48a8395b2b..6abf824336202 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextareaTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/Attribute/TextareaTest.php @@ -23,7 +23,7 @@ class TextareaTest extends GraphQlAbstract { private const QUERY = <<getAttributeCode() ); - $result = $this->graphQlQuery(sprintf(self::QUERY, $uid)); + $result = $this->graphQlQuery(sprintf(self::QUERY, $attribute->getAttributeCode(), 'customer')); $this->assertEquals( [