diff --git a/app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php b/app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php index 392a4aeedfeb3..76584ea2a65f3 100644 --- a/app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php +++ b/app/code/Magento/Catalog/Model/ResourceModel/Product/Relation.php @@ -149,12 +149,19 @@ public function getRelationsByChildren(array $childrenIds): array $select = $connection->select() ->from( ['cpe' => $this->getTable('catalog_product_entity')], - 'entity_id' + ['relation.child_id', 'cpe.entity_id'] )->join( ['relation' => $this->getTable('catalog_product_relation')], 'relation.parent_id = cpe.' . $linkField )->where('relation.child_id IN(?)', $childrenIds); - return $connection->fetchCol($select); + $result = $connection->fetchAll($select); + $parentIdsOfChildIds = []; + + foreach ($result as $row) { + $parentIdsOfChildIds[$row['child_id']][] = $row['entity_id']; + } + + return $parentIdsOfChildIds; } } diff --git a/dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product/RelationTest.php b/dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product/RelationTest.php index e9fa6d5bf96b7..20d366d05ac4a 100644 --- a/dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product/RelationTest.php +++ b/dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/ResourceModel/Product/RelationTest.php @@ -57,30 +57,69 @@ protected function setUp(): void */ public function testGetRelationsByChildren(): void { - // Find configurable products options - $productOptionSkus = ['simple_10', 'simple_20', 'simple_30', 'simple_40']; - $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $productOptionSkus, 'in') + $childSkusOfParentSkus = [ + 'configurable' => ['simple_10', 'simple_20'], + 'configurable_12345' => ['simple_30', 'simple_40'], + ]; + $configurableSkus = [ + 'configurable', + 'configurable_12345', + 'simple_10', + 'simple_20', + 'simple_30', + 'simple_40', + ]; + $configurableIdsOfSkus = []; + + $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', $configurableSkus, 'in') ->create(); - $productOptions = $this->productRepository->getList($searchCriteria) + $configurableProducts = $this->productRepository->getList($searchCriteria) ->getItems(); - $productOptionsIds = []; + $childIds = []; - foreach ($productOptions as $productOption) { - $productOptionsIds[] = $productOption->getId(); + foreach ($configurableProducts as $product) { + $configurableIdsOfSkus[$product->getSku()] = $product->getId(); + + if ($product->getTypeId() != 'configurable') { + $childIds[] = $product->getId(); + } } - // Find configurable products - $searchCriteria = $this->searchCriteriaBuilder->addFilter('sku', ['configurable', 'configurable_12345'], 'in') - ->create(); - $configurableProducts = $this->productRepository->getList($searchCriteria) - ->getItems(); + $parentIdsOfChildIds = []; + + foreach ($childSkusOfParentSkus as $parentSku => $childSkus) { + foreach ($childSkus as $childSku) { + $childId = $configurableIdsOfSkus[$childSku]; + $parentIdsOfChildIds[$childId][] = $configurableIdsOfSkus[$parentSku]; + } + } - // Assert there are configurable products ids in result of getRelationsByChildren method. - $result = $this->model->getRelationsByChildren($productOptionsIds); + /** + * Assert there are parent configurable products ids in result of getRelationsByChildren method + * and they are related to child ids. + */ + $result = $this->model->getRelationsByChildren($childIds); + $sortedResult = $this->sortParentIdsOfChildIds($result); + $sortedExpected = $this->sortParentIdsOfChildIds($parentIdsOfChildIds); - foreach ($configurableProducts as $configurableProduct) { - $this->assertContains($configurableProduct->getId(), $result); + $this->assertEquals($sortedExpected, $sortedResult); + } + + /** + * Sorts the "Parent Ids Of Child Ids" type of the array + * + * @param array $array + * @return array + */ + private function sortParentIdsOfChildIds(array $array): array + { + foreach ($array as $childId => &$parentIds) { + sort($parentIds, SORT_NUMERIC); } + + ksort($array, SORT_NUMERIC); + + return $array; } }