Skip to content

Commit ab4f6bc

Browse files
committed
ACP2E-2098: fix static error; enhance integration test to return custom product attribute from add to cart mutation.
1 parent db1e6b4 commit ab4f6bc

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

app/code/Magento/QuoteGraphQl/Plugin/ProductAttributesExtender.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@
99

1010
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory as AttributeCollectionFactory;
1111
use Magento\Framework\GraphQl\Query\Fields;
12-
use Magento\Framework\Validator\StringLength;
13-
use Magento\Framework\Validator\ValidateException;
14-
use Magento\Framework\Validator\ValidatorChain;
1512
use Magento\Quote\Model\Quote\Config as QuoteConfig;
1613

1714
/**
@@ -65,7 +62,7 @@ public function __construct(
6562
*
6663
* @return array
6764
*/
68-
private function getValidatedAttributeCodes(): array
65+
private function getValidAttributeCodes(): array
6966
{
7067
return array_filter($this->fields->getFieldsUsedInQuery(), [$this,'validateAttributeCode']);
7168
}
@@ -75,7 +72,7 @@ private function getValidatedAttributeCodes(): array
7572
*
7673
* @param string|int $attributeCode
7774
* @return bool
78-
* @throws ValidateException
75+
* @SuppressWarnings(PHPMD.UnusedPrivateMethod)
7976
*/
8077
private function validateAttributeCode(string|int $attributeCode): bool
8178
{
@@ -88,11 +85,15 @@ private function validateAttributeCode(string|int $attributeCode): bool
8885

8986
$minLength = self::ATTRIBUTE_CODE_MIN_LENGTH;
9087
$maxLength = self::ATTRIBUTE_CODE_MAX_LENGTH;
91-
$isAllowedLength = ValidatorChain::is(
92-
$attributeCode,
93-
StringLength::class,
94-
['min' => $minLength, 'max' => $maxLength]
88+
89+
$isAllowedLength = filter_var(
90+
strlen($attributeCode),
91+
FILTER_VALIDATE_INT,
92+
['options' => [
93+
'min_range' => $minLength, 'max_range' => $maxLength]
94+
]
9595
);
96+
9697
if (!$isAllowedLength) {
9798
return false;
9899
}
@@ -101,16 +102,16 @@ private function validateAttributeCode(string|int $attributeCode): bool
101102
}
102103

103104
/**
104-
* Get attributes collection based on validated codes
105+
* Get attributes based on validated codes
105106
*
106107
* @return array
107108
*/
108-
private function getAttributeCollection()
109+
private function getAttributes(): array
109110
{
110111
$attributeCollection = $this->attributeCollectionFactory->create()
111112
->removeAllFieldsFromSelect()
112113
->addFieldToSelect('attribute_code')
113-
->setCodeFilter($this->getValidatedAttributeCodes())
114+
->setCodeFilter($this->getValidAttributeCodes())
114115
->load();
115116
return $attributeCollection->getColumnValues('attribute_code');
116117
}
@@ -128,7 +129,7 @@ public function afterGetProductAttributes(QuoteConfig $subject, array $result):
128129
$hash = hash('sha256', json_encode($this->fields->getFieldsUsedInQuery()));
129130
if (!$this->fieldsHash || $this->fieldsHash !== $hash) {
130131
$this->fieldsHash = hash('sha256', json_encode($this->fields->getFieldsUsedInQuery()));
131-
$this->attributes = $this->getAttributeCollection();
132+
$this->attributes = $this->getAttributes();
132133
}
133134
$attributes = $this->attributes;
134135

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/AddProductsToCartWithVariablesTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
namespace Magento\GraphQl\Quote;
99

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Catalog\Model\Product;
1211
use Magento\Catalog\Test\Fixture\Attribute as AttributeFixture;
1312
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1413
use Magento\Framework\Exception\NoSuchEntityException;
@@ -57,37 +56,43 @@ protected function setUp(): void
5756
* @throws \Exception
5857
*/
5958
#[
60-
DataFixture(AttributeFixture::class, ['attribute_code' => 'prod_attr'], as: 'attr'),
61-
DataFixture(ProductFixture::class, ['attribute_set_id' => 13], as: 'product'),
59+
DataFixture(AttributeFixture::class, ['is_visible_on_front' => true], as: 'attr'),
60+
DataFixture(ProductFixture::class, ['attribute_set_id' => 4], as: 'product'),
6261
DataFixture(GuestCartFixture::class, as: 'cart'),
6362
]
6463
public function testAddProductsToEmptyCartWithVariables(): void
6564
{
66-
/** @var Product $product */
65+
$attribute = $this->fixtures->get('attr');
6766
$product = $this->fixtures->get('product');
68-
$product->setData('prod_attr', 'default_value');
67+
$product->setCustomAttribute($attribute->getAttributeCode(), 'default_value');
6968
$this->productRepository->save($product);
69+
70+
$this->cleanCache();
71+
7072
$cart = $this->fixtures->get('cart');
7173
$maskedQuoteId = $this->quoteIdToMaskedQuoteIdInterface->execute((int) $cart->getId());
72-
$query = $this->getAddToCartMutation();
74+
$query = $this->getAddToCartMutation($attribute->getAttributeCode());
7375
$variables = $this->getAddToCartVariables($maskedQuoteId, 1, $product->getSku());
7476
$response = $this->graphQlMutation($query, $variables);
7577
$result = $response['addProductsToCart'];
78+
7679
self::assertEmpty($result['user_errors']);
7780
self::assertCount(1, $result['cart']['items']);
7881

7982
$cartItem = $result['cart']['items'][0];
8083
self::assertEquals($product->getSku(), $cartItem['product']['sku']);
84+
self::assertEquals('default_value', $cartItem['product'][$attribute->getAttributeCode()]);
8185
self::assertEquals(1, $cartItem['quantity']);
8286
self::assertEquals($product->getFinalPrice(), $cartItem['prices']['price']['value']);
8387
}
8488

8589
/**
8690
* Returns GraphQl mutation for adding item to cart
8791
*
92+
* @param string $customAttributeCode
8893
* @return string
8994
*/
90-
private function getAddToCartMutation(): string
95+
private function getAddToCartMutation(string $customAttributeCode): string
9196
{
9297
return <<<MUTATION
9398
mutation (\$cartId: String!, \$products: [CartItemInput!]!) {
@@ -100,6 +105,7 @@ private function getAddToCartMutation(): string
100105
product {
101106
sku
102107
name
108+
{$customAttributeCode}
103109
thumbnail {
104110
url
105111
__typename

0 commit comments

Comments
 (0)