Skip to content

Commit 89e587b

Browse files
committed
MC-30332: Storefront: Configurable Product with Out Of Stock Child(s)
1 parent f9a8884 commit 89e587b

File tree

6 files changed

+687
-0
lines changed

6 files changed

+687
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\TestFramework\ConfigurableProduct\Model;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
12+
use Magento\Framework\Exception\NoSuchEntityException;
13+
use Magento\Framework\Registry;
14+
15+
/**
16+
* Delete configurable product with linked products
17+
*/
18+
class DeleteConfigurableProduct
19+
{
20+
/** @var ProductRepositoryInterface */
21+
private $productRepository;
22+
23+
/** @var ProductResource */
24+
private $productResource;
25+
26+
/** @var Registry */
27+
private $registry;
28+
29+
/**
30+
* @param ProductRepositoryInterface $productRepository
31+
* @param ProductResource $productResource
32+
* @param Registry $registry
33+
*/
34+
public function __construct(
35+
ProductRepositoryInterface $productRepository,
36+
ProductResource $productResource,
37+
Registry $registry
38+
) {
39+
$this->productRepository = $productRepository;
40+
$this->productResource = $productResource;
41+
$this->registry = $registry;
42+
}
43+
44+
/**
45+
* Delete configurable product and linked products
46+
*
47+
* @param string $sku
48+
* @return void
49+
*/
50+
public function execute(string $sku): void
51+
{
52+
$configurableProduct = $this->productRepository->get($sku, false, null, true);
53+
$childrenIds = $configurableProduct->getExtensionAttributes()->getConfigurableProductLinks();
54+
$childrenSkus = array_column($this->productResource->getProductsSku($childrenIds), 'sku');
55+
$childrenSkus[] = $sku;
56+
$this->registry->unregister('isSecureArea');
57+
$this->registry->register('isSecureArea', true);
58+
59+
foreach ($childrenSkus as $childSku) {
60+
try {
61+
$this->productRepository->deleteById($childSku);
62+
} catch (NoSuchEntityException $e) {
63+
//product already removed
64+
}
65+
}
66+
67+
$this->registry->unregister('isSecureArea');
68+
$this->registry->register('isSecureArea', false);
69+
}
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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\ConfigurableProduct\Block\Product\View\Type;
9+
10+
use Magento\Catalog\Block\Product\ListProduct;
11+
use Magento\Eav\Model\Entity\Collection\AbstractCollection;
12+
use Magento\Framework\ObjectManagerInterface;
13+
use Magento\Framework\View\LayoutInterface;
14+
use Magento\TestFramework\Helper\Bootstrap;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* Class checks configurable product displaying on category view page
19+
*
20+
* @magentoDbIsolation disabled
21+
* @magentoAppIsolation enabled
22+
* @magentoAppArea frontend
23+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_product_with_out_of_stock_children.php
24+
*/
25+
class ConfigurableViewOnCategoryPageTest extends TestCase
26+
{
27+
/** @var ObjectManagerInterface */
28+
private $objectManager;
29+
30+
/** @var LayoutInterface */
31+
private $layout;
32+
33+
/** @var ListProduct $listingBlock */
34+
private $listingBlock;
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
protected function setUp()
40+
{
41+
parent::setUp();
42+
43+
$this->objectManager = Bootstrap::getObjectManager();
44+
$this->layout = $this->objectManager->get(LayoutInterface::class);
45+
$this->listingBlock = $this->layout->createBlock(ListProduct::class);
46+
$this->listingBlock->setCategoryId(333);
47+
}
48+
49+
/**
50+
* @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 1
51+
*
52+
* @return void
53+
*/
54+
public function testOutOfStockProductWithEnabledConfigView(): void
55+
{
56+
$collection = $this->listingBlock->getLoadedProductCollection();
57+
$this->assertCollectionSize(1, $collection);
58+
}
59+
60+
/**
61+
* @magentoConfigFixture current_store cataloginventory/options/show_out_of_stock 0
62+
*
63+
* @return void
64+
*/
65+
public function testOutOfStockProductWithDisabledConfigView(): void
66+
{
67+
$collection = $this->listingBlock->getLoadedProductCollection();
68+
$this->assertCollectionSize(0, $collection);
69+
}
70+
71+
/**
72+
* Check collection size
73+
*
74+
* @param int $expectedSize
75+
* @param AbstractCollection $collection
76+
* @return void
77+
*/
78+
private function assertCollectionSize(int $expectedSize, AbstractCollection $collection): void
79+
{
80+
$this->assertEquals($expectedSize, $collection->getSize());
81+
$this->assertCount($expectedSize, $collection->getItems());
82+
}
83+
}

0 commit comments

Comments
 (0)