Skip to content

Commit 32be7bb

Browse files
author
mastiuhin-olexandr
committed
MC-33288: [2.4][MSI][MFTF] StorefrontLoggedInCustomerCreateOrderAllOptionQuantityConfigurableProductCustomStockTest fails because of bad design
1 parent a1e5689 commit 32be7bb

File tree

2 files changed

+134
-1
lines changed
  • app/code/Magento/Catalog/Model/ResourceModel/Product
  • dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product

2 files changed

+134
-1
lines changed

app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@
55
*/
66
namespace Magento\Catalog\Model\ResourceModel\Product;
77

8+
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
9+
use Magento\Framework\Model\ResourceModel\Db\Context;
10+
use Magento\Framework\EntityManager\MetadataPool;
11+
use Magento\Catalog\Api\Data\ProductInterface;
12+
813
/**
914
* Catalog Product Relations Resource model
1015
*
1116
* @author Magento Core Team <core@magentocommerce.com>
1217
*/
13-
class Relation extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
18+
class Relation extends AbstractDb
1419
{
20+
/**
21+
* @var MetadataPool
22+
*/
23+
private $metadataPool;
24+
25+
/**
26+
* @param Context $context
27+
* @param null $connectionName
28+
* @param MetadataPool $metadataPool
29+
*/
30+
public function __construct(
31+
Context $context,
32+
$connectionName = null,
33+
MetadataPool $metadataPool
34+
) {
35+
parent::__construct($context, $connectionName);
36+
$this->metadataPool = $metadataPool;
37+
}
38+
1539
/**
1640
* Initialize resource model and define main table
1741
*
@@ -109,4 +133,27 @@ public function removeRelations($parentId, $childIds)
109133
}
110134
return $this;
111135
}
136+
137+
/**
138+
* Finds parent relations by given children ids.
139+
*
140+
* @param array $childrenIds Child products entity ids.
141+
* @return array Parent products entity ids.
142+
*/
143+
public function getRelationsByChildren(array $childrenIds): array
144+
{
145+
$connection = $this->getConnection();
146+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)
147+
->getLinkField();
148+
$select = $connection->select()
149+
->from(
150+
['cpe' => $this->getTable('catalog_product_entity')],
151+
'entity_id'
152+
)->join(
153+
['relation' => $this->getTable('catalog_product_relation')],
154+
'relation.parent_id = cpe.' . $linkField
155+
)->where('child_id IN(?)', $childrenIds);
156+
157+
return $connection->fetchCol($select);
158+
}
112159
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\ResourceModel\Product;
7+
8+
use Magento\Catalog\Model\ResourceModel\Product\Relation;
9+
use Magento\Framework\ObjectManagerInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\Api\SearchCriteriaBuilder;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* Tests Catalog Product Relation resource model.
17+
*
18+
* @see Relation
19+
*/
20+
class RelationTest extends TestCase
21+
{
22+
/**
23+
* @var ObjectManagerInterface
24+
*/
25+
private $objectManager;
26+
27+
/**
28+
* @var Relation
29+
*/
30+
private $model;
31+
32+
/**
33+
* @var ProductRepositoryInterface
34+
*/
35+
private $productRepository;
36+
37+
/**
38+
* @var SearchCriteriaBuilder
39+
*/
40+
private $searchCriteriaBuilder;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp(): void
46+
{
47+
$this->objectManager = Bootstrap::getObjectManager();
48+
$this->model = $this->objectManager->get(Relation::class);
49+
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
50+
$this->searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
51+
}
52+
53+
/**
54+
* Tests that getRelationsByChildren will return parent products entity ids of child products entity ids.
55+
*
56+
* @magentoDataFixture Magento/ConfigurableProduct/_files/configurable_products.php
57+
*/
58+
public function testGetRelationsByChildren(): void
59+
{
60+
// Find configurable products options
61+
$productOptionSkus = ['simple_10', 'simple_20', 'simple_30', 'simple_40'];
62+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $productOptionSkus, 'in')
63+
->create();
64+
$productOptions = $this->productRepository->getList($searchCriteria)
65+
->getItems();
66+
67+
$productOptionsIds = [];
68+
69+
foreach ($productOptions as $productOption) {
70+
$productOptionsIds[] = $productOption->getId();
71+
}
72+
73+
// Find configurable products
74+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', ['configurable', 'configurable_12345'], 'in')
75+
->create();
76+
$configurableProducts = $this->productRepository->getList($searchCriteria)
77+
->getItems();
78+
79+
// Assert there are configurable products ids in result of getRelationsByChildren method.
80+
$result = $this->model->getRelationsByChildren($productOptionsIds);
81+
82+
foreach ($configurableProducts as $configurableProduct) {
83+
$this->assertContains($configurableProduct->getId(), $result);
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)