Skip to content

Commit ae2ce06

Browse files
committed
MC-15079: Exception is thrown when cart product is removed from shared catalog
1 parent fefed3e commit ae2ce06

File tree

1 file changed

+96
-0
lines changed
  • dev/tests/integration/testsuite/Magento/Quote/Model/ResourceModel/Quote/Item

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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\ResourceModel\Quote\Item;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
12+
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
13+
use Magento\Framework\ObjectManagerInterface;
14+
use Magento\Quote\Model\ResourceModel\Quote\Item\Collection as QuoteItemCollection;
15+
use Magento\TestFramework\Helper\Bootstrap;
16+
17+
/**
18+
* Tests Magento\Quote\Model\ResourceModel\Quote\Item\Collection.
19+
*/
20+
class CollectionTest extends \PHPUnit\Framework\TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var ProductRepositoryInterface
29+
*/
30+
private $productRepository;
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
protected function setUp()
36+
{
37+
$this->objectManager = Bootstrap::getObjectManager();
38+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
39+
}
40+
41+
/**
42+
* Covers case when during quote item collection load product exists in db but not accessible.
43+
*
44+
* @magentoDataFixture Magento/Sales/_files/quote.php
45+
* @return void
46+
*/
47+
public function testLoadCollectionWithNotAccessibleProduct()
48+
{
49+
/** @var Quote $quote */
50+
$quote = $this->objectManager->create(Quote::class);
51+
$quote->load('test01', 'reserved_order_id');
52+
53+
$this->assertCount(1, $quote->getItemsCollection());
54+
55+
$product = $this->productRepository->get('simple');
56+
/** @var ProductCollection $productCollection */
57+
$productCollection = $this->objectManager->create(ProductCollection::class);
58+
$this->setPropertyValue($productCollection, '_isCollectionLoaded', true);
59+
/** @var ProductCollectionFactory|\PHPUnit_Framework_MockObject_MockObject $productCollectionFactoryMock */
60+
$productCollectionFactoryMock = $this->createMock(ProductCollectionFactory::class);
61+
$productCollectionFactoryMock->expects($this->any())->method('create')->willReturn($productCollection);
62+
63+
/** @var QuoteItemCollection $quoteItemCollection */
64+
$quoteItemCollection = $this->objectManager->create(
65+
QuoteItemCollection::class,
66+
[
67+
'productCollectionFactory' => $productCollectionFactoryMock,
68+
]
69+
);
70+
71+
$quoteItemCollection->setQuote($quote);
72+
$this->assertCount(1, $quoteItemCollection);
73+
$item = $quoteItemCollection->getItemByColumnValue('product_id', $product->getId());
74+
75+
$this->assertNotNull($item);
76+
$this->assertTrue($item->isDeleted());
77+
}
78+
79+
/**
80+
* Set object non-public property value.
81+
*
82+
* @param object $object
83+
* @param string $propertyName
84+
* @param mixed $value
85+
* @return void
86+
*/
87+
private function setPropertyValue($object, string $propertyName, $value)
88+
{
89+
$reflectionClass = new \ReflectionClass($object);
90+
if ($reflectionClass->hasProperty($propertyName)) {
91+
$reflectionProperty = $reflectionClass->getProperty($propertyName);
92+
$reflectionProperty->setAccessible(true);
93+
$reflectionProperty->setValue($object, $value);
94+
}
95+
}
96+
}

0 commit comments

Comments
 (0)