Skip to content

Commit 248f9ed

Browse files
committed
Merge branch 'ACP2E-3488' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-VK-2024-12-17-CE
2 parents b1651ae + e75b48d commit 248f9ed

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

app/code/Magento/Quote/Model/ResourceModel/Quote/Item/Collection.php

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

89
namespace Magento\Quote\Model\ResourceModel\Quote\Item;
@@ -203,7 +204,10 @@ protected function _afterLoad(): self
203204
{
204205
parent::_afterLoad();
205206

206-
$productIds = [];
207+
/** @var QuoteItem $item */
208+
$productIds = array_map(fn ($item) => (int) $item->getProductId(), $this->getItems());
209+
$this->_productIds = array_merge($this->_productIds, $productIds);
210+
$this->removeItemsWithAbsentProducts();
207211
foreach ($this as $item) {
208212
// Assign parent items
209213
if ($item->getParentItemId()) {
@@ -212,11 +216,7 @@ protected function _afterLoad(): self
212216
if ($this->_quote) {
213217
$item->setQuote($this->_quote);
214218
}
215-
// Collect quote products ids
216-
$productIds[] = (int)$item->getProductId();
217219
}
218-
$this->_productIds = array_merge($this->_productIds, $productIds);
219-
$this->removeItemsWithAbsentProducts();
220220
/**
221221
* Assign options and products
222222
*/

dev/tests/integration/testsuite/Magento/Quote/Model/QuoteTest.php

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

89
namespace Magento\Quote\Model;
910

11+
use Magento\Bundle\Test\Fixture\AddProductToCart as AddBundleProductToCartFixture;
12+
use Magento\Bundle\Test\Fixture\Option as BundleOptionFixture;
13+
use Magento\Bundle\Test\Fixture\Product as BundleProductFixture;
1014
use Magento\Catalog\Api\ProductRepositoryInterface;
1115
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
1216
use Magento\Checkout\Model\Session as CheckoutSession;
17+
use Magento\ConfigurableProduct\Test\Fixture\AddProductToCart as AddConfigurableProductToCartFixture;
18+
use Magento\ConfigurableProduct\Test\Fixture\Attribute as AttributeFixture;
19+
use Magento\ConfigurableProduct\Test\Fixture\Product as ConfigurableProductFixture;
1320
use Magento\Customer\Api\CustomerRepositoryInterface;
1421
use Magento\Customer\Api\Data\CustomerInterface;
1522
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
@@ -21,6 +28,7 @@
2128
use Magento\Framework\Api\ExtensibleDataObjectConverter;
2229
use Magento\Framework\Exception\LocalizedException;
2330
use Magento\Framework\ObjectManagerInterface;
31+
use Magento\Quote\Api\CartRepositoryInterface;
2432
use Magento\Quote\Api\Data\AddressInterface;
2533
use Magento\Quote\Api\Data\AddressInterfaceFactory;
2634
use Magento\Quote\Api\Data\CartInterface;
@@ -32,6 +40,7 @@
3240
use Magento\TestFramework\Fixture\DataFixtureStorage;
3341
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
3442
use Magento\TestFramework\Helper\Bootstrap;
43+
use Magento\TestFramework\ObjectManager;
3544
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
3645
use PHPUnit\Framework\TestCase;
3746

@@ -42,6 +51,7 @@
4251
*
4352
* @magentoDbIsolation enabled
4453
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
54+
* @SuppressWarnings(PHPMD.TooManyFields)
4555
*/
4656
class QuoteTest extends TestCase
4757
{
@@ -87,6 +97,11 @@ class QuoteTest extends TestCase
8797
/** @var ExtensibleDataObjectConverter */
8898
private $extensibleDataObjectConverter;
8999

100+
/**
101+
* @var CartRepositoryInterface
102+
*/
103+
private $cartRepository;
104+
90105
/**
91106
* @var DataFixtureStorage
92107
*/
@@ -113,6 +128,7 @@ protected function setUp(): void
113128
$this->customerResourceModel = $this->objectManager->get(CustomerResourceModel::class);
114129
$this->groupFactory = $this->objectManager->get(GroupFactory::class);
115130
$this->extensibleDataObjectConverter = $this->objectManager->get(ExtensibleDataObjectConverter::class);
131+
$this->cartRepository = $this->objectManager->get(CartRepositoryInterface::class);
116132
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
117133
}
118134

@@ -840,4 +856,78 @@ public function testQuoteItemWithPriceGreaterThan100Millions()
840856
round((float)$item->getPrice(), 2)
841857
);
842858
}
859+
860+
#[
861+
DataFixture(ProductFixture::class, ['price' => 10], as: 'p', count: 2),
862+
DataFixture(AttributeFixture::class, as: 'attr'),
863+
DataFixture(
864+
ConfigurableProductFixture::class,
865+
['_options' => ['$attr$'], '_links' => ['$p1$', '$p2$']],
866+
'cp1'
867+
),
868+
DataFixture(GuestCartFixture::class, as: 'cart'),
869+
DataFixture(
870+
AddConfigurableProductToCartFixture::class,
871+
['cart_id' => '$cart.id$', 'product_id' => '$cp1.id$', 'child_product_id' => '$p1.id$', 'qty' => 2],
872+
),
873+
DataFixture(
874+
AddConfigurableProductToCartFixture::class,
875+
['cart_id' => '$cart.id$', 'product_id' => '$cp1.id$', 'child_product_id' => '$p2.id$', 'qty' => 2],
876+
),
877+
DataFixture('deleteProduct')
878+
]
879+
public function testCollectTotalsWhenConfigurableChildIsDeleted(): void
880+
{
881+
$cart = $this->fixtures->get('cart');
882+
$cart = $this->cartRepository->get($cart->getId());
883+
$cart->setTotalsCollectedFlag(false)->collectTotals();
884+
$items = $cart->getAllItems();
885+
$this->assertCount(3, $items);
886+
$this->assertCount(0, $items[0]->getChildren());
887+
$this->assertTrue($items[0]->getHasError());
888+
}
889+
890+
#[
891+
DataFixture(ProductFixture::class, ['price' => 10], as: 'p', count: 2),
892+
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p1$']], 'opt1'),
893+
DataFixture(BundleOptionFixture::class, ['product_links' => ['$p2$']], 'opt2'),
894+
DataFixture(BundleProductFixture::class, ['_options' => ['$opt1$', '$opt2$']], 'bp1'),
895+
DataFixture(GuestCartFixture::class, as: 'cart'),
896+
DataFixture(
897+
AddBundleProductToCartFixture::class,
898+
[
899+
'cart_id' => '$cart.id$',
900+
'product_id' => '$bp1.id$',
901+
'selections' => [['$p1.id$'], ['$p2.id$']],
902+
'qty' => 1
903+
],
904+
),
905+
DataFixture('deleteProduct')
906+
]
907+
public function testCollectTotalsWhenBundleChildIsDeleted(): void
908+
{
909+
$cart = $this->fixtures->get('cart');
910+
$cart = $this->cartRepository->get($cart->getId());
911+
$cart->setTotalsCollectedFlag(false)->collectTotals();
912+
$items = $cart->getAllItems();
913+
$this->assertCount(2, $items);
914+
$this->assertCount(1, $items[0]->getChildren());
915+
$this->assertTrue($items[0]->getHasError());
916+
}
917+
918+
public static function deleteProduct(): void
919+
{
920+
$registry = Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
921+
$isSecureArea = $registry->registry('isSecureArea');
922+
$registry->unregister('isSecureArea');
923+
$registry->register('isSecureArea', true);
924+
try {
925+
ObjectManager::getInstance()->get(ProductRepositoryInterface::class)->deleteById(
926+
DataFixtureStorageManager::getStorage()->get('p1')->getSku()
927+
);
928+
} finally {
929+
$registry->unregister('isSecureArea');
930+
$registry->register('isSecureArea', $isSecureArea);
931+
}
932+
}
843933
}

0 commit comments

Comments
 (0)