6
6
namespace Magento \BundleImportExport \Model \Import \Product \Type ;
7
7
8
8
use Magento \Catalog \Api \ProductRepositoryInterface ;
9
+ use Magento \Catalog \Model \Product ;
10
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
9
11
use Magento \Framework \Exception \NoSuchEntityException ;
12
+ use Magento \Framework \Filesystem ;
13
+ use Magento \ImportExport \Model \Import ;
14
+ use Magento \ImportExport \Model \Import \Source \Csv ;
10
15
use Magento \TestFramework \Helper \Bootstrap ;
11
16
use Magento \Framework \App \Filesystem \DirectoryList ;
12
17
@@ -77,12 +82,12 @@ public function testBundleImport()
77
82
// import data from CSV file
78
83
$ pathToFile = __DIR__ . '/../../_files/import_bundle.csv ' ;
79
84
$ filesystem = $ this ->objectManager ->create (
80
- \ Magento \ Framework \ Filesystem::class
85
+ Filesystem::class
81
86
);
82
87
83
88
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
84
89
$ source = $ this ->objectManager ->create (
85
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
90
+ Csv::class,
86
91
[
87
92
'file ' => $ pathToFile ,
88
93
'directory ' => $ directory
@@ -92,19 +97,19 @@ public function testBundleImport()
92
97
$ source
93
98
)->setParameters (
94
99
[
95
- 'behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND ,
100
+ 'behavior ' => Import::BEHAVIOR_APPEND ,
96
101
'entity ' => 'catalog_product '
97
102
]
98
103
)->validateData ();
99
104
100
105
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
101
106
$ this ->model ->importData ();
102
107
103
- $ resource = $ this ->objectManager ->get (\ Magento \ Catalog \ Model \ ResourceModel \Product ::class);
108
+ $ resource = $ this ->objectManager ->get (ProductResource ::class);
104
109
$ productId = $ resource ->getIdBySku (self ::TEST_PRODUCT_NAME );
105
110
$ 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);
108
113
$ product ->load ($ productId );
109
114
110
115
$ this ->assertFalse ($ product ->isObjectNew ());
@@ -141,6 +146,94 @@ public function testBundleImport()
141
146
$ this ->importedProductSkus = ['Simple 1 ' , 'Simple 2 ' , 'Simple 3 ' , 'Bundle 1 ' ];
142
147
}
143
148
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
+
144
237
/**
145
238
* @magentoDataFixture Magento/Store/_files/second_store.php
146
239
* @magentoDbIsolation disabled
@@ -151,10 +244,10 @@ public function testBundleImportWithMultipleStoreViews(): void
151
244
{
152
245
// import data from CSV file
153
246
$ 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);
155
248
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
156
249
$ source = $ this ->objectManager ->create (
157
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
250
+ Csv::class,
158
251
[
159
252
'file ' => $ pathToFile ,
160
253
'directory ' => $ directory ,
@@ -163,25 +256,25 @@ public function testBundleImportWithMultipleStoreViews(): void
163
256
$ errors = $ this ->model ->setSource ($ source )
164
257
->setParameters (
165
258
[
166
- 'behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND ,
259
+ 'behavior ' => Import::BEHAVIOR_APPEND ,
167
260
'entity ' => 'catalog_product ' ,
168
261
]
169
262
)->validateData ();
170
263
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
171
264
$ this ->model ->importData ();
172
- $ resource = $ this ->objectManager ->get (\ Magento \ Catalog \ Model \ ResourceModel \Product ::class);
265
+ $ resource = $ this ->objectManager ->get (ProductResource ::class);
173
266
$ productId = $ resource ->getIdBySku (self ::TEST_PRODUCT_NAME );
174
267
$ 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);
177
270
$ product ->load ($ productId );
178
271
$ this ->assertFalse ($ product ->isObjectNew ());
179
272
$ this ->assertEquals (self ::TEST_PRODUCT_NAME , $ product ->getName ());
180
273
$ this ->assertEquals (self ::TEST_PRODUCT_TYPE , $ product ->getTypeId ());
181
274
$ this ->assertEquals (1 , $ product ->getShipmentType ());
182
275
$ 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);
185
278
$ i = 0 ;
186
279
foreach ($ product ->getStoreIds () as $ storeId ) {
187
280
$ bundleOptionCollection = $ productRepository ->get (self ::TEST_PRODUCT_NAME , false , $ storeId )
@@ -203,6 +296,33 @@ public function testBundleImportWithMultipleStoreViews(): void
203
296
$ this ->importedProductSkus = ['Simple 1 ' , 'Simple 2 ' , 'Simple 3 ' , 'Bundle 1 ' ];
204
297
}
205
298
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
+
206
326
/**
207
327
* teardown
208
328
*/
0 commit comments