Skip to content

Commit

Permalink
Merge pull request #6384 from magento-tsg/MC-33288
Browse files Browse the repository at this point in the history
[Arrows] MC-33288: [2.4][MSI][MFTF] StorefrontLoggedInCustomerCreateOrderAllOptionQuantityConfigurableProductCustomStockTest fails because of bad design
  • Loading branch information
zakdma authored Dec 6, 2020
2 parents b3b45cf + 52f221b commit 500a9e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 500a9e5

Please sign in to comment.