Skip to content

Commit 6252c36

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-39531' into 2.4-develop-pr47
2 parents 2d9842b + 8752d9c commit 6252c36

File tree

4 files changed

+142
-26
lines changed

4 files changed

+142
-26
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Quote\Model\Quote\Plugin;
9+
10+
use Magento\Quote\Model\Quote;
11+
use Magento\Store\Model\StoreManagerInterface;
12+
13+
/**
14+
* Updates quote store id.
15+
*/
16+
class UpdateQuoteStoreId
17+
{
18+
/**
19+
* @var StoreManagerInterface
20+
*/
21+
private $storeManager;
22+
23+
/**
24+
* @param StoreManagerInterface $storeManager
25+
*/
26+
public function __construct(
27+
StoreManagerInterface $storeManager
28+
) {
29+
$this->storeManager = $storeManager;
30+
}
31+
32+
/**
33+
* Update store id in requested quote by store id from request.
34+
*
35+
* @param Quote $subject
36+
* @param Quote $result
37+
* @return Quote
38+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
39+
*/
40+
public function afterLoadByIdWithoutStore(Quote $subject, Quote $result): Quote
41+
{
42+
$storeId = $this->storeManager->getStore()
43+
->getId() ?: $this->storeManager->getDefaultStoreView()
44+
->getId();
45+
$result->setStoreId($storeId);
46+
47+
return $result;
48+
}
49+
}

app/code/Magento/Quote/etc/webapi_rest/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@
1616
<type name="Magento\Quote\Api\GuestCartItemRepositoryInterface">
1717
<plugin name="updateCartIdFromRequest" type="Magento\Quote\Plugin\UpdateCartId" />
1818
</type>
19+
<type name="Magento\Quote\Model\Quote">
20+
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
21+
</type>
1922
</config>

app/code/Magento/Quote/etc/webapi_soap/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,7 @@
1313
<plugin name="accessControl" type="Magento\Quote\Model\QuoteRepository\Plugin\AccessChangeQuoteControl" />
1414
<plugin name="authorization" type="Magento\Quote\Model\QuoteRepository\Plugin\Authorization" />
1515
</type>
16+
<type name="Magento\Quote\Model\Quote">
17+
<plugin name="updateQuoteStoreId" type="Magento\Quote\Model\Quote\Plugin\UpdateQuoteStoreId" />
18+
</type>
1619
</config>

dev/tests/api-functional/testsuite/Magento/Quote/Api/GuestCartItemRepositoryTest.php

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
*/
66
namespace Magento\Quote\Api;
77

8+
use Magento\Catalog\Model\Product;
89
use Magento\CatalogInventory\Api\StockRegistryInterface;
910
use Magento\CatalogInventory\Model\Stock;
11+
use Magento\Quote\Model\Quote;
12+
use Magento\Quote\Model\QuoteIdMask;
13+
use Magento\Quote\Model\QuoteIdMaskFactory;
14+
use Magento\Store\Model\StoreManagerInterface;
1015
use Magento\TestFramework\Helper\Bootstrap;
1116
use Magento\TestFramework\ObjectManager;
1217
use Magento\TestFramework\TestCase\WebapiAbstract;
@@ -40,14 +45,14 @@ protected function setUp(): void
4045
*/
4146
public function testGetList()
4247
{
43-
/** @var \Magento\Quote\Model\Quote $quote */
44-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
48+
/** @var Quote $quote */
49+
$quote = $this->objectManager->create(Quote::class);
4550
$quote->load('test_order_item_with_items', 'reserved_order_id');
4651
$cartId = $quote->getId();
4752

48-
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
49-
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
50-
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
53+
/** @var QuoteIdMask $quoteIdMask */
54+
$quoteIdMask = Bootstrap::getObjectManager()
55+
->create(QuoteIdMaskFactory::class)
5156
->create();
5257
$quoteIdMask->load($cartId, 'quote_id');
5358
//Use masked cart Id
@@ -92,17 +97,17 @@ public function testGetList()
9297
*/
9398
public function testAddItem()
9499
{
95-
/** @var \Magento\Catalog\Model\Product $product */
96-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class)->load(2);
100+
/** @var Product $product */
101+
$product = $this->objectManager->create(Product::class)->load(2);
97102
$productSku = $product->getSku();
98-
/** @var \Magento\Quote\Model\Quote $quote */
99-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
103+
/** @var Quote $quote */
104+
$quote = $this->objectManager->create(Quote::class);
100105
$quote->load('test_order_1', 'reserved_order_id');
101106
$cartId = $quote->getId();
102107

103-
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
104-
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
105-
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
108+
/** @var QuoteIdMask $quoteIdMask */
109+
$quoteIdMask = Bootstrap::getObjectManager()
110+
->create(QuoteIdMaskFactory::class)
106111
->create();
107112
$quoteIdMask->load($cartId, 'quote_id');
108113
//Use masked cart Id
@@ -141,20 +146,20 @@ public function testAddItem()
141146
*/
142147
public function testRemoveItem()
143148
{
144-
/** @var \Magento\Quote\Model\Quote $quote */
145-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
149+
/** @var Quote $quote */
150+
$quote = $this->objectManager->create(Quote::class);
146151
$quote->load('test_order_item_with_items', 'reserved_order_id');
147152
$cartId = $quote->getId();
148153

149-
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
150-
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
151-
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
154+
/** @var QuoteIdMask $quoteIdMask */
155+
$quoteIdMask = Bootstrap::getObjectManager()
156+
->create(QuoteIdMaskFactory::class)
152157
->create();
153158
$quoteIdMask->load($cartId, 'quote_id');
154159
//Use masked cart Id
155160
$cartId = $quoteIdMask->getMaskedId();
156161

157-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
162+
$product = $this->objectManager->create(Product::class);
158163
$productId = $product->getIdBySku('simple_one');
159164
$product->load($productId);
160165
$itemId = $quote->getItemByProduct($product)->getId();
@@ -175,7 +180,7 @@ public function testRemoveItem()
175180
"itemId" => $itemId,
176181
];
177182
$this->assertTrue($this->_webApiCall($serviceInfo, $requestData));
178-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
183+
$quote = $this->objectManager->create(Quote::class);
179184
$quote->load('test_order_item_with_items', 'reserved_order_id');
180185
$this->assertFalse($quote->hasProductId($productId));
181186
}
@@ -189,20 +194,20 @@ public function testRemoveItem()
189194
public function testUpdateItem(array $stockData, string $errorMessage = null)
190195
{
191196
$this->updateStockData('simple_one', $stockData);
192-
/** @var \Magento\Quote\Model\Quote $quote */
193-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
197+
/** @var Quote $quote */
198+
$quote = $this->objectManager->create(Quote::class);
194199
$quote->load('test_order_item_with_items', 'reserved_order_id');
195200
$cartId = $quote->getId();
196201

197-
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
198-
$quoteIdMask = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
199-
->create(\Magento\Quote\Model\QuoteIdMaskFactory::class)
202+
/** @var QuoteIdMask $quoteIdMask */
203+
$quoteIdMask = Bootstrap::getObjectManager()
204+
->create(QuoteIdMaskFactory::class)
200205
->create();
201206
$quoteIdMask->load($cartId, 'quote_id');
202207
//Use masked cart Id
203208
$cartId = $quoteIdMask->getMaskedId();
204209

205-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
210+
$product = $this->objectManager->create(Product::class);
206211
$productId = $product->getIdBySku('simple_one');
207212
$product->load($productId);
208213
$itemId = $quote->getItemByProduct($product)->getId();
@@ -229,14 +234,70 @@ public function testUpdateItem(array $stockData, string $errorMessage = null)
229234
$this->expectExceptionMessage($errorMessage);
230235
}
231236
$this->_webApiCall($serviceInfo, $requestData);
232-
$quote = $this->objectManager->create(\Magento\Quote\Model\Quote::class);
237+
$quote = $this->objectManager->create(Quote::class);
233238
$quote->load('test_order_item_with_items', 'reserved_order_id');
234239
$this->assertTrue($quote->hasProductId(1));
235240
$item = $quote->getItemByProduct($product);
236241
$this->assertEquals(5, $item->getQty());
237242
$this->assertEquals($itemId, $item->getItemId());
238243
}
239244

245+
/**
246+
* Verifies that store id for quote and quote item is being changed accordingly to the requested store code
247+
*
248+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_items_saved.php
249+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
250+
*/
251+
public function testUpdateItemWithChangingStoreId()
252+
{
253+
/** @var Quote $quote */
254+
$quote = $this->objectManager->create(Quote::class);
255+
$quote->load('test_order_item_with_items', 'reserved_order_id');
256+
$cartId = $quote->getId();
257+
258+
/** @var QuoteIdMask $quoteIdMask */
259+
$quoteIdMask = Bootstrap::getObjectManager()
260+
->create(QuoteIdMaskFactory::class)
261+
->create();
262+
$quoteIdMask->load($cartId, 'quote_id');
263+
$cartId = $quoteIdMask->getMaskedId();
264+
265+
$product = $this->objectManager->create(Product::class);
266+
$productId = $product->getIdBySku('simple');
267+
$product->load($productId);
268+
$itemId = $quote->getItemByProduct($product)->getId();
269+
$serviceInfo = [
270+
'rest' => [
271+
'resourcePath' => self::RESOURCE_PATH . $cartId . '/items/' . $itemId,
272+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
273+
],
274+
'soap' => [
275+
'service' => self::SERVICE_NAME,
276+
'serviceVersion' => self::SERVICE_VERSION,
277+
'operation' => self::SERVICE_NAME . 'Save',
278+
],
279+
];
280+
281+
$requestData['cartItem']['qty'] = 5;
282+
if (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP) {
283+
$requestData['cartItem'] += [
284+
'quote_id' => $cartId,
285+
'itemId' => $itemId,
286+
];
287+
}
288+
$this->_webApiCall($serviceInfo, $requestData, null, 'fixture_second_store');
289+
$quote = $this->objectManager->create(Quote::class);
290+
$quote->load('test_order_item_with_items', 'reserved_order_id');
291+
$this->assertTrue($quote->hasProductId(1));
292+
$item = $quote->getItemByProduct($product);
293+
/** @var StoreManagerInterface $storeManager */
294+
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
295+
$storeId = $storeManager->getStore('fixture_second_store')
296+
->getId();
297+
$this->assertEquals($storeId, $quote->getStoreId());
298+
$this->assertEquals($storeId, $item->getStoreId());
299+
}
300+
240301
/**
241302
* @return array
242303
*/

0 commit comments

Comments
 (0)