Skip to content

Commit 3051ff6

Browse files
committed
MC-19689: Simple product disappearing in the configurable grid after qty set to 0
1 parent c4484d6 commit 3051ff6

File tree

2 files changed

+20
-76
lines changed

2 files changed

+20
-76
lines changed

app/code/Magento/ConfigurableProduct/Model/Plugin/Frontend/UsedProductsCache.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
/**
2121
* Cache of used products for configurable product
22+
*
23+
* @SuppressWarnings(PHPMD.CookieAndSessionMisuse)
2224
*/
2325
class UsedProductsCache
2426
{
@@ -76,6 +78,7 @@ public function __construct(
7678
* @param Product $product
7779
* @param array|null $requiredAttributeIds
7880
* @return ProductInterface[]
81+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
7982
*/
8083
public function aroundGetUsedProducts(
8184
Configurable $subject,
@@ -163,19 +166,21 @@ private function readUsedProductsCacheData(string $cacheKey): ?array
163166
private function saveUsedProductsCacheData(Product $product, array $subProducts, string $cacheKey): bool
164167
{
165168
$metadata = $this->metadataPool->getMetadata(ProductInterface::class);
166-
$data = $this->serializer->serialize(array_map(
167-
function ($item) {
168-
return $item->getData();
169-
},
170-
$subProducts
171-
));
169+
$data = $this->serializer->serialize(
170+
array_map(
171+
function ($item) {
172+
return $item->getData();
173+
},
174+
$subProducts
175+
)
176+
);
172177
$tags = array_merge(
173178
$product->getIdentities(),
174179
[
175180
Category::CACHE_TAG,
176181
Product::CACHE_TAG,
177182
'price',
178-
Configurable::TYPE_CODE . '_' . $product->getData($metadata->getLinkField())
183+
Configurable::TYPE_CODE . '_' . $product->getData($metadata->getLinkField()),
179184
]
180185
);
181186
$result = $this->cache->save($data, $cacheKey, $tags);

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/Type/ConfigurableTest.php

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -344,25 +344,13 @@ public function testCanUseAttribute()
344344

345345
public function testGetUsedProducts()
346346
{
347-
$productCollectionItemData = ['array'];
347+
$productCollectionItem = $this->createMock(\Magento\Catalog\Model\Product::class);
348+
$attributeCollection = $this->createMock(Collection::class);
349+
$product = $this->createMock(\Magento\Catalog\Model\Product::class);
350+
$productCollection = $this->createMock(ProductCollection::class);
348351

349-
$productCollectionItem = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
350-
->disableOriginalConstructor()
351-
->getMock();
352-
$attributeCollection = $this->getMockBuilder(Collection::class)
353-
->disableOriginalConstructor()
354-
->getMock();
355-
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
356-
->disableOriginalConstructor()
357-
->getMock();
358-
$productCollection = $this->getMockBuilder(ProductCollection::class)
359-
->disableOriginalConstructor()
360-
->getMock();
361-
362-
$productCollectionItem->expects($this->once())->method('getData')->willReturn($productCollectionItemData);
363352
$attributeCollection->expects($this->any())->method('setProductFilter')->willReturnSelf();
364353
$product->expects($this->atLeastOnce())->method('getStoreId')->willReturn(5);
365-
$product->expects($this->once())->method('getIdentities')->willReturn(['123']);
366354

367355
$product->expects($this->exactly(2))
368356
->method('hasData')
@@ -388,59 +376,10 @@ public function testGetUsedProducts()
388376
$productCollection->expects($this->once())->method('setStoreId')->with(5)->willReturn([]);
389377
$productCollection->expects($this->once())->method('getItems')->willReturn([$productCollectionItem]);
390378

391-
$this->serializer->expects($this->once())
392-
->method('serialize')
393-
->with([$productCollectionItemData])
394-
->willReturn('result');
395-
396379
$this->productCollectionFactory->expects($this->any())->method('create')->willReturn($productCollection);
397380
$this->model->getUsedProducts($product);
398381
}
399382

400-
public function testGetUsedProductsWithDataInCache()
401-
{
402-
$product = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
403-
->disableOriginalConstructor()
404-
->getMock();
405-
$childProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
406-
->disableOriginalConstructor()
407-
->getMock();
408-
409-
$dataKey = '_cache_instance_products';
410-
$usedProductsData = [['first']];
411-
$usedProducts = [$childProduct];
412-
413-
$product->expects($this->once())
414-
->method('hasData')
415-
->with($dataKey)
416-
->willReturn(false);
417-
$product->expects($this->once())
418-
->method('setData')
419-
->with($dataKey, $usedProducts);
420-
$product->expects($this->any())
421-
->method('getData')
422-
->willReturnOnConsecutiveCalls(1, $usedProducts);
423-
424-
$childProduct->expects($this->once())
425-
->method('setData')
426-
->with($usedProductsData[0]);
427-
428-
$this->productFactory->expects($this->once())
429-
->method('create')
430-
->willReturn($childProduct);
431-
432-
$this->cache->expects($this->once())
433-
->method('load')
434-
->willReturn($usedProductsData);
435-
436-
$this->serializer->expects($this->once())
437-
->method('unserialize')
438-
->with($usedProductsData)
439-
->willReturn($usedProductsData);
440-
441-
$this->assertEquals($usedProducts, $this->model->getUsedProducts($product));
442-
}
443-
444383
/**
445384
* @param int $productStore
446385
*
@@ -878,12 +817,12 @@ public function testSetImageFromChildProduct()
878817
->method('getLinkField')
879818
->willReturn('link');
880819
$productMock->expects($this->any())->method('hasData')
881-
->withConsecutive(['store_id'], ['_cache_instance_products'])
882-
->willReturnOnConsecutiveCalls(true, true);
820+
->withConsecutive(['_cache_instance_products'])
821+
->willReturnOnConsecutiveCalls(true);
883822

884823
$productMock->expects($this->any())->method('getData')
885-
->withConsecutive(['image'], ['image'], ['link'], ['store_id'], ['_cache_instance_products'])
886-
->willReturnOnConsecutiveCalls('no_selection', 'no_selection', 1, 1, [$childProductMock]);
824+
->withConsecutive(['image'], ['image'], ['_cache_instance_products'])
825+
->willReturnOnConsecutiveCalls('no_selection', 'no_selection', [$childProductMock]);
887826

888827
$childProductMock->expects($this->any())->method('getData')->with('image')->willReturn('image_data');
889828
$productMock->expects($this->once())->method('setImage')->with('image_data')->willReturnSelf();

0 commit comments

Comments
 (0)