Skip to content

Commit c04a4b8

Browse files
authored
LYNX-469: Cannot add products due to SKU in mutation being case sensitive
1 parent f3294e9 commit c04a4b8

File tree

2 files changed

+143
-3
lines changed

2 files changed

+143
-3
lines changed

app/code/Magento/Quote/Model/Cart/ProductReader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ProductReader implements ProductReaderInterface
2525
/**
2626
* @var ProductInterface[]
2727
*/
28-
private $productsBySku;
28+
private array $productsBySku;
2929

3030
/**
3131
* @var Config
@@ -65,7 +65,7 @@ public function loadProducts(array $skus, int $storeId): void
6565
$this->productCollection->addOptionsToResult();
6666
$this->productCollection->load();
6767
foreach ($this->productCollection->getItems() as $productItem) {
68-
$this->productsBySku[$productItem->getData(ProductInterface::SKU)] = $productItem;
68+
$this->productsBySku[strtolower($productItem->getData(ProductInterface::SKU))] = $productItem;
6969
}
7070
}
7171

@@ -74,6 +74,6 @@ public function loadProducts(array $skus, int $storeId): void
7474
*/
7575
public function getProductBySku(string $sku) : ?ProductInterface
7676
{
77-
return $this->productsBySku[$sku] ?? null;
77+
return $this->productsBySku[strtolower($sku)] ?? null;
7878
}
7979
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* ADOBE CONFIDENTIAL
5+
* ___________________
6+
*
7+
* Copyright 2024 Adobe
8+
* All Rights Reserved.
9+
*
10+
* NOTICE: All information contained herein is, and remains
11+
* the property of Adobe and its suppliers, if any. The intellectual
12+
* and technical concepts contained herein are proprietary to Adobe
13+
* and its suppliers and are protected by all applicable intellectual
14+
* property laws, including trade secret and copyright laws.
15+
* Dissemination of this information or reproduction of this material
16+
* is strictly forbidden unless prior written permission is obtained
17+
* from Adobe.
18+
* ************************************************************************
19+
*/
20+
declare(strict_types=1);
21+
22+
namespace Magento\GraphQl\Quote;
23+
24+
use Exception;
25+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
26+
use Magento\Framework\Exception\NoSuchEntityException;
27+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
28+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
29+
use Magento\TestFramework\Fixture\DataFixture;
30+
use Magento\TestFramework\Fixture\DataFixtureStorage;
31+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
32+
use Magento\TestFramework\Helper\Bootstrap;
33+
use Magento\TestFramework\TestCase\GraphQlAbstract;
34+
35+
/**
36+
* Get add to cart through GraphQl query and variables
37+
*/
38+
class AddProductsToCartTest extends GraphQlAbstract
39+
{
40+
/**
41+
* @var QuoteIdToMaskedQuoteIdInterface
42+
*/
43+
private $quoteIdToMaskedQuoteId;
44+
45+
/**
46+
* @var DataFixtureStorage
47+
*/
48+
private $fixtures;
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
protected function setUp(): void
54+
{
55+
$this->quoteIdToMaskedQuoteId = Bootstrap::getObjectManager()->get(QuoteIdToMaskedQuoteIdInterface::class);
56+
$this->fixtures = Bootstrap::getObjectManager()->get(DataFixtureStorageManager::class)->getStorage();
57+
}
58+
59+
/**
60+
* Test addProductsToCart mutation by passing SKU Upper & Lower case
61+
*
62+
* @param string $sku
63+
* @dataProvider skuDataProvider
64+
* @throws NoSuchEntityException
65+
* @throws Exception
66+
*/
67+
#[
68+
DataFixture(ProductFixture::class, ['sku' => 'Upper_And_Lower_Test_Prod']),
69+
DataFixture(GuestCartFixture::class, as: 'cart'),
70+
]
71+
public function testAddProductsToCartWithSKUCaseInsensitive(string $sku): void
72+
{
73+
$cart = $this->fixtures->get('cart');
74+
$maskedQuoteId = $this->quoteIdToMaskedQuoteId->execute((int) $cart->getId());
75+
76+
$query = $this->getAddToCartMutation($maskedQuoteId, $sku);
77+
$response = $this->graphQlMutation($query);
78+
$result = $response['addProductsToCart'];
79+
80+
self::assertEmpty($result['user_errors']);
81+
self::assertCount(1, $result['cart']['items']);
82+
83+
$cartItem = $result['cart']['items'][0];
84+
self::assertEquals('Upper_And_Lower_Test_Prod', $cartItem['product']['sku']);
85+
self::assertEquals(1, $cartItem['quantity']);
86+
}
87+
88+
/**
89+
* Data provider with sku in uppercase and lowercase
90+
*
91+
* @return array
92+
*/
93+
public static function skuDataProvider(): array
94+
{
95+
return [
96+
'upper' => ['UPPER_AND_LOWER_TEST_PROD'],
97+
'lower' => ['upper_and_lower_test_prod'],
98+
];
99+
}
100+
101+
/**
102+
* Returns GraphQl mutation for (addProductsToCart) adding item to cart
103+
*
104+
* @param string $maskedQuoteId
105+
* @param string $sku
106+
* @return string
107+
*/
108+
private function getAddToCartMutation(string $maskedQuoteId, string $sku): string
109+
{
110+
return <<<MUTATION
111+
mutation {
112+
addProductsToCart(
113+
cartId: "{$maskedQuoteId}",
114+
cartItems: [
115+
{
116+
sku: "{$sku}"
117+
quantity: 1
118+
}
119+
]
120+
) {
121+
cart {
122+
id
123+
items {
124+
uid
125+
quantity
126+
product {
127+
sku
128+
name
129+
}
130+
}
131+
}
132+
user_errors {
133+
code
134+
message
135+
}
136+
}
137+
}
138+
MUTATION;
139+
}
140+
}

0 commit comments

Comments
 (0)