Skip to content

Commit ec10e8f

Browse files
author
Anna Bukatar
committed
Merge branch 'ACP2E-378' of https://github.com/magento-l3/magento2ce into PR-2022-03-11
2 parents da1e681 + c97ec9e commit ec10e8f

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

app/code/Magento/Catalog/Test/Fixture/Product.php

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Catalog\Test\Fixture;
99

10+
use Magento\Catalog\Api\Data\ProductInterface;
1011
use Magento\Catalog\Api\ProductRepositoryInterface;
1112
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1213
use Magento\Catalog\Model\Product\Type;
@@ -50,6 +51,12 @@ class Product implements RevertibleDataFixtureInterface
5051
'updated_at' => null,
5152
];
5253

54+
private const DEFAULT_PRODUCT_LINK_DATA = [
55+
'sku' => null,
56+
'type' => 'related',
57+
'position' => 1,
58+
];
59+
5360
/**
5461
* @var ServiceFactory
5562
*/
@@ -65,18 +72,25 @@ class Product implements RevertibleDataFixtureInterface
6572
*/
6673
private $dataMerger;
6774

75+
/**
76+
* @var ProductRepositoryInterface
77+
*/
78+
private $productRepository;
79+
6880
/**
6981
* @param ServiceFactory $serviceFactory
7082
* @param ProcessorInterface $dataProcessor
7183
*/
7284
public function __construct(
7385
ServiceFactory $serviceFactory,
7486
ProcessorInterface $dataProcessor,
75-
DataMerger $dataMerger
87+
DataMerger $dataMerger,
88+
ProductRepositoryInterface $productRepository
7689
) {
7790
$this->serviceFactory = $serviceFactory;
7891
$this->dataProcessor = $dataProcessor;
7992
$this->dataMerger = $dataMerger;
93+
$this->productRepository = $productRepository;
8094
}
8195

8296
/**
@@ -122,6 +136,49 @@ private function prepareData(array $data): array
122136
unset($data['extension_attributes']['category_links']);
123137
}
124138

139+
$data['product_links'] = $this->prepareLinksData($data);
140+
125141
return $this->dataProcessor->process($this, $data);
126142
}
143+
144+
/**
145+
* Prepare links data
146+
*
147+
* @param array $data
148+
* @return array
149+
*/
150+
private function prepareLinksData(array $data): array
151+
{
152+
$links = [];
153+
154+
$position = 1;
155+
foreach ($data['product_links'] as $link) {
156+
$defaultLinkData = self::DEFAULT_PRODUCT_LINK_DATA;
157+
$defaultLinkData['position'] = $position;
158+
$linkData = [];
159+
if (is_numeric($link)) {
160+
$product = $this->productRepository->getById($link);
161+
} elseif (is_string($link)) {
162+
$product = $this->productRepository->get($link);
163+
} elseif ($link instanceof ProductInterface) {
164+
$product = $this->productRepository->get($link->getSku());
165+
} else {
166+
$linkData = $link instanceof DataObject ? $link->toArray() : $link;
167+
$product = $this->productRepository->get($linkData['sku']);
168+
}
169+
170+
$linkData += $defaultLinkData;
171+
$links[] = [
172+
'sku' => $data['sku'],
173+
'link_type' => $linkData['type'],
174+
'linked_product_sku' => $product->getSku(),
175+
'linked_product_type' => $product->getTypeId(),
176+
'position' => $linkData['position'],
177+
'extension_attributes' => array_diff_key($linkData, $defaultLinkData),
178+
];
179+
$position++;
180+
}
181+
182+
return $links;
183+
}
127184
}

app/code/Magento/ConfigurableProduct/Test/Fixture/Product.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ class Product extends \Magento\Catalog\Test\Fixture\Product
5353
* @param ServiceFactory $serviceFactory
5454
* @param ProcessorInterface $dataProcessor
5555
* @param DataMerger $dataMerger
56-
* @param Config $eavConfig
5756
* @param ProductRepositoryInterface $productRepository
57+
* @param Config $eavConfig
5858
* @param VariationMatrix $variationMatrix
5959
*/
6060
public function __construct(
6161
ServiceFactory $serviceFactory,
6262
ProcessorInterface $dataProcessor,
6363
DataMerger $dataMerger,
64-
Config $eavConfig,
6564
ProductRepositoryInterface $productRepository,
65+
Config $eavConfig,
6666
VariationMatrix $variationMatrix
6767
) {
68-
parent::__construct($serviceFactory, $dataProcessor, $dataMerger);
68+
parent::__construct($serviceFactory, $dataProcessor, $dataMerger, $productRepository);
6969
$this->eavConfig = $eavConfig;
7070
$this->variationMatrix = $variationMatrix;
7171
$this->productRepository = $productRepository;

app/code/Magento/GroupedProduct/Test/Fixture/Product.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Product extends \Magento\Catalog\Test\Fixture\Product
2828

2929
private const DEFAULT_PRODUCT_LINK_DATA = [
3030
'sku' => null,
31+
'type' => 'associated',
3132
'position' => 1,
3233
'qty' => 1,
3334
];
@@ -49,7 +50,7 @@ public function __construct(
4950
DataMerger $dataMerger,
5051
ProductRepositoryInterface $productRepository
5152
) {
52-
parent::__construct($serviceFactory, $dataProcessor, $dataMerger);
53+
parent::__construct($serviceFactory, $dataProcessor, $dataMerger, $productRepository);
5354
$this->productRepository = $productRepository;
5455
}
5556

@@ -102,18 +103,12 @@ private function prepareLinksData(array $data): array
102103
$linkData = $link instanceof DataObject ? $link->toArray() : $link;
103104
$product = $this->productRepository->get($linkData['sku']);
104105
}
105-
106+
if (isset($link['type']) && $link['type'] !== $defaultLinkData['type']) {
107+
unset($defaultLinkData['qty']);
108+
}
109+
$linkData['sku'] = $product->getSku();
106110
$linkData += $defaultLinkData;
107-
$links[] = [
108-
'sku' => $data['sku'],
109-
'link_type' => 'associated',
110-
'linked_product_sku' => $product->getSku(),
111-
'linked_product_type' => $product->getTypeId(),
112-
'position' => $linkData['position'],
113-
'extension_attributes' => [
114-
'qty' => $linkData['qty']
115-
],
116-
];
111+
$links[] = $linkData;
117112
$position++;
118113
}
119114

0 commit comments

Comments
 (0)