66namespace Magento \BundleImportExport \Model \Import \Product \Type ;
77
88use Magento \Catalog \Api \ProductRepositoryInterface ;
9+ use Magento \Catalog \Model \Product ;
10+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
911use Magento \Framework \Exception \NoSuchEntityException ;
12+ use Magento \Framework \Filesystem ;
13+ use Magento \ImportExport \Model \Import ;
14+ use Magento \ImportExport \Model \Import \Source \Csv ;
1015use Magento \TestFramework \Helper \Bootstrap ;
1116use 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 */
0 commit comments