1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2021 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
6
declare (strict_types=1 );
7
7
8
8
namespace Magento \CatalogImportExport \Model \Import \ProductTest ;
9
9
10
+ use Magento \Catalog \Api \Data \ProductInterface ;
10
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Catalog \Model \Attribute \ScopeOverriddenValue ;
11
13
use Magento \Catalog \Test \Fixture \Product as ProductFixture ;
12
14
use Magento \CatalogImportExport \Model \Import \Product \RowValidatorInterface ;
15
+ use Magento \CatalogImportExport \Model \Import \Product \StoreResolver ;
13
16
use Magento \CatalogImportExport \Model \Import \ProductTestBase ;
14
17
use Magento \Framework \App \Filesystem \DirectoryList ;
15
18
use Magento \Framework \Exception \LocalizedException ;
16
19
use Magento \Framework \Filesystem ;
17
20
use Magento \ImportExport \Model \Import ;
21
+ use Magento \ImportExport \Model \Import \ErrorProcessing \ProcessingErrorAggregatorInterface ;
18
22
use Magento \ImportExport \Model \Import \Source \Csv ;
23
+ use Magento \ImportExport \Test \Fixture \CsvFile as CsvFileFixture ;
24
+ use Magento \Store \Test \Fixture \Store ;
19
25
use Magento \TestFramework \Fixture \Config ;
20
26
use Magento \TestFramework \Fixture \DataFixture ;
27
+ use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
28
+ use Magento \TestFramework \Helper \Bootstrap ;
21
29
22
30
/**
23
31
* Integration test for \Magento\CatalogImportExport\Model\Import\Product class.
@@ -41,26 +49,26 @@ public function testImportWithoutUrlKeysAndName()
41
49
'simple1 ' => 'url-key ' ,
42
50
'simple2 ' => 'url-key2 ' ,
43
51
];
44
- $ filesystem = $ this ->objectManager ->create (\ Magento \ Framework \ Filesystem::class);
52
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
45
53
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
46
54
$ source = $ this ->objectManager ->create (
47
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
55
+ Csv::class,
48
56
[
49
57
'file ' => __DIR__ . '/../_files/products_to_import_without_url_keys_and_name.csv ' ,
50
58
'directory ' => $ directory
51
59
]
52
60
);
53
61
54
62
$ errors = $ this ->_model ->setParameters (
55
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
63
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
56
64
)
57
65
->setSource ($ source )
58
66
->validateData ();
59
67
60
68
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
61
69
$ this ->_model ->importData ();
62
70
63
- $ productRepository = $ this ->objectManager ->create (\ Magento \ Catalog \ Api \ ProductRepositoryInterface::class);
71
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
64
72
foreach ($ products as $ productSku => $ productUrlKey ) {
65
73
$ this ->assertEquals ($ productUrlKey , $ productRepository ->get ($ productSku )->getUrlKey ());
66
74
}
@@ -75,21 +83,21 @@ public function testImportWithoutUrlKeysAndName()
75
83
*/
76
84
public function testValidateUrlKeys ($ importFile , $ expectedErrors )
77
85
{
78
- $ filesystem = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
79
- \ Magento \ Framework \ Filesystem::class
86
+ $ filesystem = Bootstrap::getObjectManager ()->create (
87
+ Filesystem::class
80
88
);
81
89
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
82
90
83
91
$ source = $ this ->objectManager ->create (
84
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
92
+ Csv::class,
85
93
[
86
94
'file ' => __DIR__ . '/../_files/ ' . $ importFile ,
87
95
'directory ' => $ directory
88
96
]
89
97
);
90
- /** @var \Magento\ImportExport\Model\Import\ErrorProcessing\ ProcessingErrorAggregatorInterface $errors */
98
+ /** @var ProcessingErrorAggregatorInterface $errors */
91
99
$ errors = $ this ->_model ->setParameters (
92
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
100
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
93
101
)->setSource (
94
102
$ source
95
103
)->validateData ();
@@ -139,20 +147,20 @@ public static function validateUrlKeysDataProvider()
139
147
*/
140
148
public function testValidateUrlKeysMultipleStores ()
141
149
{
142
- $ filesystem = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
143
- \ Magento \ Framework \ Filesystem::class
150
+ $ filesystem = Bootstrap::getObjectManager ()->create (
151
+ Filesystem::class
144
152
);
145
153
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
146
154
147
155
$ source = $ this ->objectManager ->create (
148
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
156
+ Csv::class,
149
157
[
150
158
'file ' => __DIR__ . '/../_files/products_to_check_valid_url_keys_multiple_stores.csv ' ,
151
159
'directory ' => $ directory
152
160
]
153
161
);
154
162
$ errors = $ this ->_model ->setParameters (
155
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
163
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
156
164
)->setSource (
157
165
$ source
158
166
)->validateData ();
@@ -173,28 +181,28 @@ public function testExistingProductWithUrlKeys()
173
181
// added by _files/products_to_import_with_valid_url_keys.csv
174
182
$ this ->importedProducts [] = 'simple3 ' ;
175
183
176
- $ filesystem = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
177
- ->create (\ Magento \ Framework \ Filesystem::class);
184
+ $ filesystem = Bootstrap::getObjectManager ()
185
+ ->create (Filesystem::class);
178
186
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
179
187
$ source = $ this ->objectManager ->create (
180
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
188
+ Csv::class,
181
189
[
182
190
'file ' => __DIR__ . '/../_files/products_to_import_with_valid_url_keys.csv ' ,
183
191
'directory ' => $ directory
184
192
]
185
193
);
186
194
187
195
$ errors = $ this ->_model ->setParameters (
188
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
196
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
189
197
)->setSource (
190
198
$ source
191
199
)->validateData ();
192
200
193
201
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
194
202
$ this ->_model ->importData ();
195
203
196
- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
197
- \ Magento \ Catalog \ Api \ ProductRepositoryInterface::class
204
+ $ productRepository = Bootstrap::getObjectManager ()->create (
205
+ ProductRepositoryInterface::class
198
206
);
199
207
foreach ($ products as $ productSku => $ productUrlKey ) {
200
208
$ this ->assertEquals ($ productUrlKey , $ productRepository ->get ($ productSku )->getUrlKey ());
@@ -214,28 +222,28 @@ public function testAddUpdateProductWithInvalidUrlKeys() : void
214
222
// added by _files/products_to_import_with_invalid_url_keys.csv
215
223
$ this ->importedProducts [] = 'simple3 ' ;
216
224
217
- $ filesystem = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()
218
- ->create (\ Magento \ Framework \ Filesystem::class);
225
+ $ filesystem = Bootstrap::getObjectManager ()
226
+ ->create (Filesystem::class);
219
227
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
220
228
$ source = $ this ->objectManager ->create (
221
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
229
+ Csv::class,
222
230
[
223
231
'file ' => __DIR__ . '/../_files/products_to_import_with_invalid_url_keys.csv ' ,
224
232
'directory ' => $ directory
225
233
]
226
234
);
227
235
228
236
$ errors = $ this ->_model ->setParameters (
229
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
237
+ ['behavior ' => Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
230
238
)->setSource (
231
239
$ source
232
240
)->validateData ();
233
241
234
242
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
235
243
$ this ->_model ->importData ();
236
244
237
- $ productRepository = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
238
- \ Magento \ Catalog \ Api \ ProductRepositoryInterface::class
245
+ $ productRepository = Bootstrap::getObjectManager ()->create (
246
+ ProductRepositoryInterface::class
239
247
);
240
248
foreach ($ products as $ productSku => $ productUrlKey ) {
241
249
$ this ->assertEquals ($ productUrlKey , $ productRepository ->get ($ productSku )->getUrlKey ());
@@ -282,26 +290,26 @@ public function testImportWithoutUrlKeys()
282
290
// added by _files/products_to_import_without_url_keys.csv
283
291
$ this ->importedProducts [] = 'simple3 ' ;
284
292
285
- $ filesystem = $ this ->objectManager ->create (\ Magento \ Framework \ Filesystem::class);
293
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
286
294
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
287
295
$ source = $ this ->objectManager ->create (
288
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
296
+ Csv::class,
289
297
[
290
298
'file ' => __DIR__ . '/../_files/products_to_import_without_url_keys.csv ' ,
291
299
'directory ' => $ directory
292
300
]
293
301
);
294
302
295
303
$ errors = $ this ->_model ->setParameters (
296
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
304
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
297
305
)
298
306
->setSource ($ source )
299
307
->validateData ();
300
308
301
309
$ this ->assertTrue ($ errors ->getErrorsCount () == 0 );
302
310
$ this ->_model ->importData ();
303
311
304
- $ productRepository = $ this ->objectManager ->create (\ Magento \ Catalog \ Api \ ProductRepositoryInterface::class);
312
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
305
313
foreach ($ products as $ productSku => $ productUrlKey ) {
306
314
$ this ->assertEquals ($ productUrlKey , $ productRepository ->get ($ productSku )->getUrlKey ());
307
315
}
@@ -325,18 +333,18 @@ public function testImportWithNonLatinUrlKeys()
325
333
$ productSkuMap = array_merge ($ productsCreatedByFixture , $ productsImportedByCsv );
326
334
$ this ->importedProducts = array_keys ($ productsImportedByCsv );
327
335
328
- $ filesystem = $ this ->objectManager ->create (\ Magento \ Framework \ Filesystem::class);
336
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
329
337
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
330
338
$ source = $ this ->objectManager ->create (
331
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
339
+ Csv::class,
332
340
[
333
341
'file ' => __DIR__ . '/../_files/products_to_import_with_non_latin_url_keys.csv ' ,
334
342
'directory ' => $ directory ,
335
343
]
336
344
);
337
345
338
346
$ errors = $ this ->_model ->setParameters (
339
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
347
+ ['behavior ' => Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
340
348
)
341
349
->setSource ($ source )
342
350
->validateData ();
@@ -364,17 +372,17 @@ public function testImportWithSpacesInUrlKeys()
364
372
'simple1 ' => 'url-with-spaces-1 ' ,
365
373
'simple2 ' => 'url-with-spaces-2 '
366
374
];
367
- $ filesystem = $ this ->objectManager ->create (\ Magento \ Framework \ Filesystem::class);
375
+ $ filesystem = $ this ->objectManager ->create (Filesystem::class);
368
376
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
369
377
$ source = $ this ->objectManager ->create (
370
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
378
+ Csv::class,
371
379
[
372
380
'file ' => __DIR__ . '/../_files/products_to_import_with_spaces_in_url_keys.csv ' ,
373
381
'directory ' => $ directory ,
374
382
]
375
383
);
376
384
$ errors = $ this ->_model ->setParameters (
377
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
385
+ ['behavior ' => Import::BEHAVIOR_ADD_UPDATE , 'entity ' => 'catalog_product ' ]
378
386
)
379
387
->setSource ($ source )
380
388
->validateData ();
@@ -408,25 +416,77 @@ public function testImportWithSpacesInUrlKeys()
408
416
]
409
417
public function testValidateMixedCharNumUrlKeysWithNullUrlSuffix ()
410
418
{
411
- $ filesystem = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ()->create (
412
- \ Magento \ Framework \ Filesystem::class
419
+ $ filesystem = Bootstrap::getObjectManager ()->create (
420
+ Filesystem::class
413
421
);
414
422
$ directory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
415
423
416
424
$ source = $ this ->objectManager ->create (
417
- \ Magento \ ImportExport \ Model \ Import \ Source \ Csv::class,
425
+ Csv::class,
418
426
[
419
427
'file ' => __DIR__ . '/../_files/products_to_check_valid_url_keys_mixed_chars_nums.csv ' ,
420
428
'directory ' => $ directory
421
429
]
422
430
);
423
431
424
432
$ errors = $ this ->_model ->setParameters (
425
- ['behavior ' => \ Magento \ ImportExport \ Model \ Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
433
+ ['behavior ' => Import::BEHAVIOR_APPEND , 'entity ' => 'catalog_product ' ]
426
434
)->setSource (
427
435
$ source
428
436
)->validateData ();
429
437
430
438
$ this ->assertEmpty ($ errors ->getAllErrors (), 'Assert that import validation returns no errors ' );
431
439
}
440
+
441
+ #[
442
+ DataFixture(Store::class, as: 'store2 ' ),
443
+ DataFixture(
444
+ CsvFileFixture::class,
445
+ [
446
+ 'rows ' => [
447
+ ['sku ' , 'store_view_code ' , 'attribute_set_code ' , 'product_type ' , 'price ' , 'name ' , 'url_key ' ],
448
+ // simple1 has url_key specified in the main row
449
+ ['simple1%uniqid% ' , '' , 'Default ' , 'simple ' , '10.00 ' , 'Simple Product 1%uniqid% ' , 's1%uniqid% ' ],
450
+ ['simple1%uniqid% ' , '$store2.code$ ' , 'Default ' , 'simple ' , '' , '' , '' ],
451
+ // simple2 has no url_key specified in the main row
452
+ ['simple2%uniqid% ' , '' , 'Default ' , 'simple ' , '10.00 ' , 'Simple Product 1%uniqid% ' , '' ],
453
+ ['simple2%uniqid% ' , '$store2.code$ ' , 'Default ' , 'simple ' , '' , '' , '' ]
454
+ ]
455
+ ],
456
+ 'file '
457
+ ),
458
+ ]
459
+ public function testStoreViewShouldInheritUrlKeyIfNotSpecified (): void
460
+ {
461
+ // reset store resolver cache to ensure that new store view code is resolved correctly
462
+ $ this ->objectManager ->removeSharedInstance (StoreResolver::class);
463
+ $ fixtures = DataFixtureStorageManager::getStorage ();
464
+ $ store = $ fixtures ->get ('store2 ' )->getCode ();
465
+ $ pathToFile = $ fixtures ->get ('file ' )->getAbsolutePath ();
466
+ $ model = $ this ->createImportModel ($ pathToFile );
467
+ $ this ->assertErrorsCount (0 , $ model ->validateData ());
468
+ $ model ->importData ();
469
+ [$ headers , $ row1 ,, $ row3 ,] = $ fixtures ->get ('file ' )->getRows ();
470
+ $ row1 = array_combine ($ headers , $ row1 );
471
+ $ row3 = array_combine ($ headers , $ row3 );
472
+ // generate url_key for simple2 based on name
473
+ $ row3 ['url_key ' ] = strtolower (preg_replace ('/\s+/ ' , '- ' , $ row3 ['name ' ]));
474
+ foreach ([$ row1 , $ row3 ] as $ row ) {
475
+ $ product = $ this ->getProductBySku ($ row ['sku ' ], $ store );
476
+ $ this ->assertFalse (
477
+ $ this ->objectManager ->create (ScopeOverriddenValue::class)->containsValue (
478
+ ProductInterface::class,
479
+ $ product ,
480
+ 'url_key ' ,
481
+ $ store
482
+ ),
483
+ "Assert that url_key is not overridden in store ' {$ store }' for SKU ' {$ row ['sku ' ]}' "
484
+ );
485
+ $ this ->assertEquals (
486
+ $ row ['url_key ' ],
487
+ $ product ->getUrlKey (),
488
+ "Assert that url_key is set to ' {$ row ['url_key ' ]}' for SKU ' {$ row ['sku ' ]}' in store ' {$ store }' "
489
+ );
490
+ }
491
+ }
432
492
}
0 commit comments