Skip to content

Commit bd341e9

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4227' into PR_2025_09_25_muntianu
2 parents 84b66ed + c2ee91b commit bd341e9

File tree

2 files changed

+158
-4
lines changed

2 files changed

+158
-4
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CustomerCart.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2019 Adobe
4+
* All Rights Reserved.
55
*/
66
declare(strict_types=1);
77

@@ -12,6 +12,7 @@
1212
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1313
use Magento\GraphQl\Model\Query\ContextInterface;
1414
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
15+
use Magento\QuoteGraphQl\Model\Cart\UpdateCartCurrency;
1516
use Magento\Quote\Model\Cart\CustomerCartResolver;
1617

1718
/**
@@ -24,15 +25,23 @@ class CustomerCart implements ResolverInterface
2425
*/
2526
private $customerCartResolver;
2627

28+
/**
29+
* @var UpdateCartCurrency
30+
*/
31+
private UpdateCartCurrency $updateCartCurrency;
32+
2733
/**
2834
* CustomerCart constructor.
2935
*
3036
* @param CustomerCartResolver $customerCartResolver
37+
* @param UpdateCartCurrency $updateCartCurrency
3138
*/
3239
public function __construct(
33-
CustomerCartResolver $customerCartResolver
40+
CustomerCartResolver $customerCartResolver,
41+
UpdateCartCurrency $updateCartCurrency
3442
) {
3543
$this->customerCartResolver = $customerCartResolver;
44+
$this->updateCartCurrency = $updateCartCurrency;
3645
}
3746

3847
/**
@@ -50,7 +59,11 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
5059
}
5160

5261
try {
53-
$cart = $this->customerCartResolver->resolve($currentUserId);
62+
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
63+
$cart = $this->updateCartCurrency->execute(
64+
$this->customerCartResolver->resolve($currentUserId),
65+
$storeId
66+
);
5467
} catch (\Exception $e) {
5568
$cart = null;
5669
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\GraphQl\Quote\Customer;
9+
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Customer\Test\Fixture\Customer;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
14+
use Magento\Quote\Test\Fixture\CustomerCart;
15+
use Magento\Store\Test\Fixture\Store;
16+
use Magento\TestFramework\Fixture\DataFixture;
17+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
18+
use Magento\TestFramework\TestCase\GraphQlAbstract;
19+
20+
/**
21+
* Ensures customerCart respects Store header for store-scoped product attributes
22+
*/
23+
class CustomerCartStoreScopeTest extends GraphQlAbstract
24+
{
25+
/** @var CustomerTokenServiceInterface */
26+
private $customerTokenService;
27+
28+
/**
29+
* @var string Customer token for requests
30+
*/
31+
private $customerToken;
32+
33+
protected function setUp(): void
34+
{
35+
$this->customerTokenService = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
36+
->get(CustomerTokenServiceInterface::class);
37+
}
38+
39+
/**
40+
* Customer cart returns product name overridden per Store header
41+
*/
42+
#[
43+
DataFixture(Store::class, as: 'store2'),
44+
DataFixture(Customer::class, as: 'customer'),
45+
DataFixture(ProductFixture::class, ['name' => 'Def Name'], as: 'product'),
46+
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'cart'),
47+
DataFixture(
48+
AddProductToCartFixture::class,
49+
[
50+
'cart_id' => '$cart.id$',
51+
'product_id' => '$product.id$',
52+
'qty' => 1
53+
]
54+
)
55+
]
56+
public function testCustomerCartProductNameRespectsStoreHeader(): void
57+
{
58+
//Set product name per store
59+
$secondStore = DataFixtureStorageManager::getStorage()->get('store2');
60+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
61+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
62+
$product = DataFixtureStorageManager::getStorage()->get('product');
63+
$product->setStoreId($secondStore->getId())->setName('Second Name');
64+
$productRepository->save($product);
65+
//get customer cart for different stores
66+
$customer = DataFixtureStorageManager::getStorage()->get('customer');
67+
$query = <<<QUERY
68+
{
69+
customerCart {
70+
itemsV2(pageSize: 10) {
71+
items { product { sku name } }
72+
}
73+
}
74+
}
75+
QUERY;
76+
77+
// Default store
78+
$headersDefault = $this->getHeaderMap($customer->getEmail());
79+
$headersDefault['Store'] = 'default';
80+
$responseDefault = $this->graphQlQuery($query, [], '', $headersDefault);
81+
self::assertEquals('Def Name', $responseDefault['customerCart']['itemsV2']['items'][0]['product']['name']);
82+
83+
// Second store
84+
$headersSecond = $this->getHeaderMap($customer->getEmail());
85+
$headersSecond['Store'] = $secondStore->getCode();
86+
$responseSecond = $this->graphQlQuery($query, [], '', $headersSecond);
87+
self::assertEquals('Second Name', $responseSecond['customerCart']['itemsV2']['items'][0]['product']['name']);
88+
}
89+
90+
/**
91+
* Invalid Store header returns a standardized error
92+
*/
93+
#[
94+
DataFixture(Customer::class, as: 'customer')
95+
]
96+
public function testCustomerCartInvalidStoreHeader(): void
97+
{
98+
$this->expectException(\Exception::class);
99+
$this->expectExceptionMessage('Requested store is not found');
100+
101+
$query = <<<QUERY
102+
{
103+
customerCart { id }
104+
}
105+
QUERY;
106+
$customer = DataFixtureStorageManager::getStorage()->get('customer');
107+
$headers = $this->getHeaderMap($customer->getEmail());
108+
$headers['Store'] = 'not_existing_store';
109+
$this->graphQlQuery($query, [], '', $headers);
110+
}
111+
112+
/**
113+
* Get headers with authorization token
114+
*
115+
* @param string $username
116+
* @param string $password
117+
* @return array
118+
*/
119+
private function getHeaderMap(string $username = '[email protected]', string $password = 'password'): array
120+
{
121+
$token = $this->getCustomerToken($username, $password);
122+
return ['Authorization' => 'Bearer ' . $token];
123+
}
124+
125+
/**
126+
* Get and cache customer token
127+
*
128+
* @param string $username
129+
* @param string $password
130+
* @return string
131+
*/
132+
private function getCustomerToken(
133+
string $username,
134+
string $password
135+
) : string {
136+
if (!$this->customerToken) {
137+
$this->customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
138+
}
139+
return $this->customerToken;
140+
}
141+
}

0 commit comments

Comments
 (0)