Skip to content

Commit db08be4

Browse files
author
Serhii Balko
committed
Merge remote-tracking branch 'origin/MC-40898' into 2.4-develop-pr55
2 parents e7c6ebf + a2f0fea commit db08be4

File tree

4 files changed

+146
-17
lines changed

4 files changed

+146
-17
lines changed

app/code/Magento/BundleImportExport/Model/Import/Product/Type/Bundle.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,12 @@ protected function insertOptions()
573573

574574
$optionIds = $this->connection->fetchAssoc(
575575
$this->connection->select()->from(
576-
$this->_resource->getTableName('catalog_product_bundle_option'),
576+
['bo' => $this->_resource->getTableName('catalog_product_bundle_option')],
577577
['option_id', 'position', 'parent_id']
578+
)->joinLeft(
579+
['bov' => $this->_resource->getTableName('catalog_product_bundle_option_value')],
580+
'bo.option_id = bov.option_id',
581+
['title']
578582
)->where(
579583
'parent_id IN (?)',
580584
$productIds
@@ -600,8 +604,10 @@ protected function populateInsertOptionValues(array $optionIds): array
600604
foreach ($this->_cachedOptions as $entityId => $options) {
601605
foreach ($options as $key => $option) {
602606
foreach ($optionIds as $optionId => $assoc) {
603-
if ($assoc['position'] == $this->_cachedOptions[$entityId][$key]['index']
604-
&& $assoc['parent_id'] == $entityId) {
607+
if ($assoc['position'] == $this->_cachedOptions[$entityId][$key]['index'] &&
608+
$assoc['parent_id'] == $entityId &&
609+
(empty($assoc['title']) || $assoc['title'] == $this->_cachedOptions[$entityId][$key]['name'])
610+
) {
605611
$option['parent_id'] = $entityId;
606612
$optionValues[] = $this->populateOptionValueTemplate($option, $optionId);
607613
$this->_cachedOptions[$entityId][$key]['option_id'] = $optionId;

app/code/Magento/BundleImportExport/Test/Unit/Model/Import/Product/Type/BundleTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ public function testSaveData($skus, $bunch, $allowImport)
289289
'type' => 'bundle',
290290
'value_id' => '6',
291291
'title' => 'Bundle6',
292+
'name' => 'Bundle6',
292293
'selections' => [
293294
[
294295
'name' => 'Bundlen6',

dev/tests/integration/testsuite/Magento/BundleImportExport/Model/Import/Product/Type/BundleTest.php

Lines changed: 134 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@
66
namespace Magento\BundleImportExport\Model\Import\Product\Type;
77

88
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Catalog\Model\Product;
10+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
911
use Magento\Framework\Exception\NoSuchEntityException;
12+
use Magento\Framework\Filesystem;
13+
use Magento\ImportExport\Model\Import;
14+
use Magento\ImportExport\Model\Import\Source\Csv;
1015
use Magento\TestFramework\Helper\Bootstrap;
1116
use Magento\Framework\App\Filesystem\DirectoryList;
1217

@@ -77,12 +82,12 @@ public function testBundleImport()
7782
// import data from CSV file
7883
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
7984
$filesystem = $this->objectManager->create(
80-
\Magento\Framework\Filesystem::class
85+
Filesystem::class
8186
);
8287

8388
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
8489
$source = $this->objectManager->create(
85-
\Magento\ImportExport\Model\Import\Source\Csv::class,
90+
Csv::class,
8691
[
8792
'file' => $pathToFile,
8893
'directory' => $directory
@@ -92,19 +97,19 @@ public function testBundleImport()
9297
$source
9398
)->setParameters(
9499
[
95-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
100+
'behavior' => Import::BEHAVIOR_APPEND,
96101
'entity' => 'catalog_product'
97102
]
98103
)->validateData();
99104

100105
$this->assertTrue($errors->getErrorsCount() == 0);
101106
$this->model->importData();
102107

103-
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
108+
$resource = $this->objectManager->get(ProductResource::class);
104109
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
105110
$this->assertIsNumeric($productId);
106-
/** @var \Magento\Catalog\Model\Product $product */
107-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
111+
/** @var Product $product */
112+
$product = $this->objectManager->create(Product::class);
108113
$product->load($productId);
109114

110115
$this->assertFalse($product->isObjectNew());
@@ -141,6 +146,94 @@ public function testBundleImport()
141146
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
142147
}
143148

149+
/**
150+
* Test that Bundle options are updated correctly by import
151+
*
152+
* @dataProvider valuesDataProvider
153+
*
154+
* @magentoAppArea adminhtml
155+
* @magentoDbIsolation enabled
156+
* @magentoAppIsolation enabled
157+
* @param array $expectedValues
158+
* @return void
159+
*/
160+
public function testBundleImportUpdateValues(array $expectedValues): void
161+
{
162+
// import data from CSV file
163+
$pathToFile = __DIR__ . '/../../_files/import_bundle.csv';
164+
$filesystem = $this->objectManager->create(
165+
Filesystem::class
166+
);
167+
168+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
169+
$source = $this->objectManager->create(
170+
Csv::class,
171+
[
172+
'file' => $pathToFile,
173+
'directory' => $directory
174+
]
175+
);
176+
$errors = $this->model->setSource(
177+
$source
178+
)->setParameters(
179+
[
180+
'behavior' => Import::BEHAVIOR_APPEND,
181+
'entity' => 'catalog_product'
182+
]
183+
)->validateData();
184+
185+
$this->assertTrue($errors->getErrorsCount() == 0);
186+
$this->model->importData();
187+
188+
// import data from CSV file to update values
189+
$pathToFile2 = __DIR__ . '/../../_files/import_bundle_update_values.csv';
190+
$source2 = $this->objectManager->create(
191+
Csv::class,
192+
[
193+
'file' => $pathToFile2,
194+
'directory' => $directory
195+
]
196+
);
197+
$errors2 = $this->model->setSource(
198+
$source2
199+
)->setParameters(
200+
[
201+
'behavior' => Import::BEHAVIOR_APPEND,
202+
'entity' => 'catalog_product'
203+
]
204+
)->validateData();
205+
206+
$this->assertTrue($errors2->getErrorsCount() == 0);
207+
$this->model->importData();
208+
209+
$resource = $this->objectManager->get(ProductResource::class);
210+
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
211+
$this->assertIsNumeric($productId);
212+
/** @var Product $product */
213+
$product = $this->objectManager->create(Product::class);
214+
$product->load($productId);
215+
216+
$this->assertFalse($product->isObjectNew());
217+
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
218+
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
219+
$this->assertEquals(1, $product->getShipmentType());
220+
221+
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
222+
$bundleOptionCollection = $product->getExtensionAttributes()->getBundleProductOptions();
223+
$this->assertCount(3, $bundleOptionCollection);
224+
foreach ($bundleOptionCollection as $optionKey => $option) {
225+
$this->assertEquals('checkbox', $option->getData('type'));
226+
$this->assertEquals($expectedValues[$optionKey]['title'], $option->getData('title'));
227+
$this->assertEquals(self::TEST_PRODUCT_NAME, $option->getData('sku'));
228+
foreach ($option->getData('product_links') as $linkKey => $productLink) {
229+
$optionSku = $expectedValues[$optionKey]['product_links'][$linkKey];
230+
$this->assertEquals($optionIdList[$optionSku], $productLink->getData('entity_id'));
231+
$this->assertEquals($optionSku, $productLink->getData('sku'));
232+
}
233+
}
234+
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
235+
}
236+
144237
/**
145238
* @magentoDataFixture Magento/Store/_files/second_store.php
146239
* @magentoDbIsolation disabled
@@ -151,10 +244,10 @@ public function testBundleImportWithMultipleStoreViews(): void
151244
{
152245
// import data from CSV file
153246
$pathToFile = __DIR__ . '/../../_files/import_bundle_multiple_store_views.csv';
154-
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
247+
$filesystem = $this->objectManager->create(Filesystem::class);
155248
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
156249
$source = $this->objectManager->create(
157-
\Magento\ImportExport\Model\Import\Source\Csv::class,
250+
Csv::class,
158251
[
159252
'file' => $pathToFile,
160253
'directory' => $directory,
@@ -163,25 +256,25 @@ public function testBundleImportWithMultipleStoreViews(): void
163256
$errors = $this->model->setSource($source)
164257
->setParameters(
165258
[
166-
'behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND,
259+
'behavior' => Import::BEHAVIOR_APPEND,
167260
'entity' => 'catalog_product',
168261
]
169262
)->validateData();
170263
$this->assertTrue($errors->getErrorsCount() == 0);
171264
$this->model->importData();
172-
$resource = $this->objectManager->get(\Magento\Catalog\Model\ResourceModel\Product::class);
265+
$resource = $this->objectManager->get(ProductResource::class);
173266
$productId = $resource->getIdBySku(self::TEST_PRODUCT_NAME);
174267
$this->assertIsNumeric($productId);
175-
/** @var \Magento\Catalog\Model\Product $product */
176-
$product = $this->objectManager->create(\Magento\Catalog\Model\Product::class);
268+
/** @var Product $product */
269+
$product = $this->objectManager->create(Product::class);
177270
$product->load($productId);
178271
$this->assertFalse($product->isObjectNew());
179272
$this->assertEquals(self::TEST_PRODUCT_NAME, $product->getName());
180273
$this->assertEquals(self::TEST_PRODUCT_TYPE, $product->getTypeId());
181274
$this->assertEquals(1, $product->getShipmentType());
182275
$optionIdList = $resource->getProductsIdsBySkus($this->optionSkuList);
183-
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
184-
$productRepository = $this->objectManager->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
276+
/** @var ProductRepositoryInterface $productRepository */
277+
$productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
185278
$i = 0;
186279
foreach ($product->getStoreIds() as $storeId) {
187280
$bundleOptionCollection = $productRepository->get(self::TEST_PRODUCT_NAME, false, $storeId)
@@ -203,6 +296,33 @@ public function testBundleImportWithMultipleStoreViews(): void
203296
$this->importedProductSkus = ['Simple 1', 'Simple 2', 'Simple 3', 'Bundle 1'];
204297
}
205298

299+
/**
300+
* Provider for testBundleImportUpdateValues
301+
*
302+
* @return array
303+
*/
304+
public function valuesDataProvider(): array
305+
{
306+
return [
307+
[
308+
[
309+
0 => [
310+
'title' => 'Option 1',
311+
'product_links' => ['Simple 1'],
312+
],
313+
1 => [
314+
'title' => 'Option 1 new',
315+
'product_links' => ['Simple 1'],
316+
],
317+
2 => [
318+
'title' => 'Option 2',
319+
'product_links' => ['Simple 2', 'Simple 3'],
320+
],
321+
],
322+
],
323+
];
324+
}
325+
206326
/**
207327
* teardown
208328
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
sku,store_view_code,attribute_set_code,product_type,product_websites,name,product_online,price,additional_attributes,qty,out_of_stock_qty,website_id,bundle_price_type,bundle_sku_type,bundle_price_view,bundle_weight_type,bundle_values
2+
Bundle 1,,Default,bundle,base,Bundle 1,1,,shipment_type=separately,0,0,1,dynamic,dynamic,Price range,dynamic,"name=Option 1 new,type=checkbox,required=1,sku=Simple 1,price=0.0000,default=0,default_qty=1.0000,price_type=fixed,can_change_qty=1"

0 commit comments

Comments
 (0)