Skip to content

Commit 0e8655d

Browse files
authored
LYNX-142: Provide only attributes that are marked as visible to the frontend 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
1 parent 6a027c6 commit 0e8655d

File tree

5 files changed

+52
-14
lines changed

5 files changed

+52
-14
lines changed

app/code/Magento/EavGraphQl/Model/GetAttributesMetadata.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ public function execute(array $attributesInputs, int $storeId): array
7474

7575
foreach ($codes as $entityType => $attributeCodes) {
7676
$builder = $this->searchCriteriaBuilderFactory->create();
77-
$builder->addFilter('attribute_code', $attributeCodes, 'in');
77+
$builder
78+
->addFilter('attribute_code', $attributeCodes, 'in')
79+
->addFilter('is_visible', true);
7880
try {
7981
$attributes = $this->attributeRepository->getList($entityType, $builder->create())->getItems();
8082
} catch (LocalizedException $exception) {

app/code/Magento/EavGraphQl/Model/Resolver/AttributesList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public function resolve(
9999
}
100100
$searchCriteria->addFilter($key, $provider->resolve($field, $context, $info));
101101
}
102-
$searchCriteria = $searchCriteria->create();
102+
$searchCriteria = $searchCriteria->addFilter("is_visible", true)->create();
103103

104104
$attributesList = $this->attributeRepository->getList(strtolower($entityType), $searchCriteria)->getItems();
105105
return [

app/code/Magento/EavGraphQl/Model/Resolver/CustomAttributeMetadata.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1818
use Magento\Framework\GraphQl\Query\ResolverInterface;
1919
use Magento\Eav\Api\Data\AttributeInterface;
20+
use Magento\Framework\Exception\NotFoundException;
2021

2122
/**
2223
* Resolve data for custom attribute metadata requests
@@ -52,7 +53,7 @@ public function resolve(
5253
ResolveInfo $info,
5354
array $value = null,
5455
array $args = null
55-
) {
56+
): array {
5657
$attributes['items'] = null;
5758
$attributeInputs = $args['attributes'];
5859
foreach ($attributeInputs as $attributeInput) {
@@ -123,7 +124,8 @@ private function getStorefrontProperties(AttributeInterface $attribute)
123124
*
124125
* @return string[]
125126
*/
126-
private function getLayeredNavigationPropertiesEnum() {
127+
private function getLayeredNavigationPropertiesEnum()
128+
{
127129
return [
128130
0 => 'NO',
129131
1 => 'FILTERABLE_WITH_RESULTS',

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ class AttributesFormTest extends GraphQlAbstract
5656
'used_in_forms' => ['customer_address_edit']
5757
],
5858
'attribute_2'
59+
),
60+
DataFixture(
61+
CustomerAttribute::class,
62+
[
63+
'entity_type_id' => AddressMetadataInterface::ATTRIBUTE_SET_ID_ADDRESS,
64+
'used_in_forms' => ['customer_register_address']
65+
],
66+
'attribute_3'
5967
)
6068
]
6169
public function testAttributesForm(): void
@@ -64,13 +72,18 @@ public function testAttributesForm(): void
6472
$attribute1 = DataFixtureStorageManager::getStorage()->get('attribute_1');
6573
/** @var AttributeInterface $attribute2 */
6674
$attribute2 = DataFixtureStorageManager::getStorage()->get('attribute_2');
75+
/** @var AttributeInterface $attribute3 */
76+
$attribute3 = DataFixtureStorageManager::getStorage()->get('attribute_3');
77+
$attribute3->setIsVisible(false)->save();
78+
6779
$result = $this->graphQlQuery(sprintf(self::QUERY, 'customer_register_address'));
6880

6981
foreach ($result['attributesForm']['items'] as $item) {
7082
if (array_contains($item, $attribute1->getAttributeCode())) {
7183
return;
7284
}
7385
$this->assertNotContains($attribute2->getAttributeCode(), $item);
86+
$this->assertNotContains($attribute3->getAttributeCode(), $item);
7487
}
7588
$this->fail(sprintf("Attribute '%s' not found in query response", $attribute1->getAttributeCode()));
7689
}

dev/tests/api-functional/testsuite/Magento/GraphQl/EavGraphQl/AttributesListTest.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<?php
2+
23
/**
34
* Copyright © Magento, Inc. All rights reserved.
45
* See COPYING.txt for license details.
56
*/
7+
68
declare(strict_types=1);
79

810
namespace Magento\GraphQl\EavGraphQl;
@@ -47,6 +49,15 @@
4749
],
4850
'customerAttribute2'
4951
),
52+
DataFixture(
53+
Attribute::class,
54+
[
55+
'entity_type_id' => CustomerMetadataInterface::ATTRIBUTE_SET_ID_CUSTOMER,
56+
'frontend_input' => 'boolean',
57+
'source_model' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean'
58+
],
59+
'customerAttribute3'
60+
),
5061
DataFixture(
5162
Attribute::class,
5263
[
@@ -81,6 +92,19 @@ class AttributesListTest extends GraphQlAbstract
8192

8293
public function testAttributesListForCustomerEntityType(): void
8394
{
95+
/** @var AttributeInterface $attribute */
96+
$creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5');
97+
98+
/** @var AttributeInterface $attribute */
99+
$customerAttribute0 = DataFixtureStorageManager::getStorage()->get('customerAttribute0');
100+
/** @var AttributeInterface $attribute */
101+
$customerAttribute1 = DataFixtureStorageManager::getStorage()->get('customerAttribute1');
102+
/** @var AttributeInterface $attribute */
103+
$customerAttribute2 = DataFixtureStorageManager::getStorage()->get('customerAttribute2');
104+
/** @var AttributeInterface $attribute */
105+
$customerAttribute3 = DataFixtureStorageManager::getStorage()->get('customerAttribute3');
106+
$customerAttribute3->setIsVisible(false)->save();
107+
84108
$queryResult = $this->graphQlQuery(<<<QRY
85109
{
86110
attributesList(entityType: CUSTOMER) {
@@ -99,16 +123,6 @@ public function testAttributesListForCustomerEntityType(): void
99123
$this->assertArrayHasKey('items', $queryResult['attributesList'], 'Query result does not contain items');
100124
$this->assertGreaterThanOrEqual(3, count($queryResult['attributesList']['items']));
101125

102-
/** @var AttributeInterface $attribute */
103-
$creditmemoAttribute5 = DataFixtureStorageManager::getStorage()->get('creditmemoAttribute5');
104-
105-
/** @var AttributeInterface $attribute */
106-
$customerAttribute0 = DataFixtureStorageManager::getStorage()->get('customerAttribute0');
107-
/** @var AttributeInterface $attribute */
108-
$customerAttribute1 = DataFixtureStorageManager::getStorage()->get('customerAttribute1');
109-
/** @var AttributeInterface $attribute */
110-
$customerAttribute2 = DataFixtureStorageManager::getStorage()->get('customerAttribute2');
111-
112126
$this->assertEquals(
113127
$customerAttribute0->getAttributeCode(),
114128
$this->getAttributeByCode(
@@ -134,6 +148,13 @@ public function testAttributesListForCustomerEntityType(): void
134148
)['code'],
135149
self::ATTRIBUTE_NOT_FOUND_ERROR
136150
);
151+
$this->assertEquals(
152+
[],
153+
$this->getAttributeByCode(
154+
$queryResult['attributesList']['items'],
155+
$customerAttribute3->getAttributeCode()
156+
)
157+
);
137158
$this->assertEquals(
138159
[],
139160
$this->getAttributeByCode(

0 commit comments

Comments
 (0)