Skip to content

Commit

Permalink
LYNX-142: Provide only attributes that are marked as visible to the f…
Browse files Browse the repository at this point in the history
…rontend via GraphQL (#101)

* LYNX-142: Provide only attributes that are marked as visible to the frontend via GraphQL

* LYNX-142: Provide only attributes that are marked as visible to the frontend via GraphQL - new implementation

* LYNX-142: Refactoring; added tests for AttributeList and AttributeForm

* LYNX-142: CR changes

* LYNX-142: CR changes

* LYNX-142: CR changes
  • Loading branch information
bl4de authored Apr 28, 2023
1 parent 6a027c6 commit 0e8655d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
4 changes: 3 additions & 1 deletion app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public function execute(array $attributesInputs, int $storeId): array

foreach ($codes as $entityType => $attributeCodes) {
$builder = $this->searchCriteriaBuilderFactory->create();
$builder->addFilter('attribute_code', $attributeCodes, 'in');
$builder
->addFilter('attribute_code', $attributeCodes, 'in')
->addFilter('is_visible', true);
try {
$attributes = $this->attributeRepository->getList($entityType, $builder->create())->getItems();
} catch (LocalizedException $exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function resolve(
}
$searchCriteria->addFilter($key, $provider->resolve($field, $context, $info));
}
$searchCriteria = $searchCriteria->create();
$searchCriteria = $searchCriteria->addFilter("is_visible", true)->create();

$attributesList = $this->attributeRepository->getList(strtolower($entityType), $searchCriteria)->getItems();
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Eav\Api\Data\AttributeInterface;
use Magento\Framework\Exception\NotFoundException;

/**
* Resolve data for custom attribute metadata requests
Expand Down Expand Up @@ -52,7 +53,7 @@ public function resolve(
ResolveInfo $info,
array $value = null,
array $args = null
) {
): array {
$attributes['items'] = null;
$attributeInputs = $args['attributes'];
foreach ($attributeInputs as $attributeInput) {
Expand Down Expand Up @@ -123,7 +124,8 @@ private function getStorefrontProperties(AttributeInterface $attribute)
*
* @return string[]
*/
private function getLayeredNavigationPropertiesEnum() {
private function getLayeredNavigationPropertiesEnum()
{
return [
0 => 'NO',
1 => 'FILTERABLE_WITH_RESULTS',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ class AttributesFormTest extends GraphQlAbstract
'used_in_forms' => ['customer_address_edit']
],
'attribute_2'
),
DataFixture(
CustomerAttribute::class,
[
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
'used_in_forms' => ['customer_register_address']
],
'attribute_3'
)
]
public function testAttributesForm(): void
Expand All @@ -64,13 +72,18 @@ public function testAttributesForm(): void
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
/** @var AttributeInterface $attribute2 */
$attribute2 = DataFixtureStorageManager::getStorage()->get('attribute_2');
/** @var AttributeInterface $attribute3 */
$attribute3 = DataFixtureStorageManager::getStorage()->get('attribute_3');
$attribute3->setIsVisible(false)->save();

$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));

foreach ($result['attributesForm']['items'] as $item) {
if (array_contains($item, $attribute1->getAttributeCode())) {
return;
}
$this->assertNotContains($attribute2->getAttributeCode(), $item);
$this->assertNotContains($attribute3->getAttributeCode(), $item);
}
$this->fail(sprintf("Attribute '%s' not found in query response", $attribute1->getAttributeCode()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Magento\GraphQl\EavGraphQl;
Expand Down Expand Up @@ -47,6 +49,15 @@
],
'customerAttribute2'
),
DataFixture(
Attribute::class,
[
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
'frontend_input' => 'boolean',
'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
],
'customerAttribute3'
),
DataFixture(
Attribute::class,
[
Expand Down Expand Up @@ -81,6 +92,19 @@ class AttributesListTest extends GraphQlAbstract

public function testAttributesListForCustomerEntityType(): void
{
/** @var AttributeInterface $attribute */
$creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5');

/** @var AttributeInterface $attribute */
$customerAttribute0 = DataFixtureStorageManager::getStorage()->get('customerAttribute0');
/** @var AttributeInterface $attribute */
$customerAttribute1 = DataFixtureStorageManager::getStorage()->get('customerAttribute1');
/** @var AttributeInterface $attribute */
$customerAttribute2 = DataFixtureStorageManager::getStorage()->get('customerAttribute2');
/** @var AttributeInterface $attribute */
$customerAttribute3 = DataFixtureStorageManager::getStorage()->get('customerAttribute3');
$customerAttribute3->setIsVisible(false)->save();

$queryResult = $this->graphQlQuery(<<<QRY
{
attributesList(entityType: CUSTOMER) {
Expand All @@ -99,16 +123,6 @@ public function testAttributesListForCustomerEntityType(): void
$this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items');
$this->assertGreaterThanOrEqual(3, count($queryResult['attributesList']['items']));

/** @var AttributeInterface $attribute */
$creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5');

/** @var AttributeInterface $attribute */
$customerAttribute0 = DataFixtureStorageManager::getStorage()->get('customerAttribute0');
/** @var AttributeInterface $attribute */
$customerAttribute1 = DataFixtureStorageManager::getStorage()->get('customerAttribute1');
/** @var AttributeInterface $attribute */
$customerAttribute2 = DataFixtureStorageManager::getStorage()->get('customerAttribute2');

$this->assertEquals(
$customerAttribute0->getAttributeCode(),
$this->getAttributeByCode(
Expand All @@ -134,6 +148,13 @@ public function testAttributesListForCustomerEntityType(): void
)['code'],
self::ATTRIBUTE_NOT_FOUND_ERROR
);
$this->assertEquals(
[],
$this->getAttributeByCode(
$queryResult['attributesList']['items'],
$customerAttribute3->getAttributeCode()
)
);
$this->assertEquals(
[],
$this->getAttributeByCode(
Expand Down

0 comments on commit 0e8655d

Please sign in to comment.