Skip to content

Commit 0ab629d

Browse files
ENGCOM-9103: 32911 fixed product import with url key with spaces #33088
- Merge Pull Request #33088 from SilinMykola/magento2:32911-product-imoprt-url-key-format - Merged commits: 1. a7b9176
2 parents f121ac8 + a7b9176 commit 0ab629d

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3019,7 +3019,7 @@ protected function getUrlKey($rowData)
30193019
{
30203020
if (!empty($rowData[self::URL_KEY])) {
30213021
$urlKey = (string) $rowData[self::URL_KEY];
3022-
return trim(strtolower($urlKey));
3022+
return $this->productUrl->formatUrlKey($urlKey);
30233023
}
30243024

30253025
if (!empty($rowData[self::COL_NAME])
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
/** @var $product \Magento\Catalog\Model\Product */
8+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
9+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
10+
->setAttributeSetId(4)
11+
->setWebsiteIds([1])
12+
->setName('Simple Product')
13+
->setSku('simple1')
14+
->setPrice(10)
15+
->setDescription('Description with <b>html tag</b>')
16+
->setTaxClassId(2)
17+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
18+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
19+
->setCategoryIds([2])
20+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
21+
->setUrlKey('url with spaces 1')
22+
->save();
23+
24+
/** @var $product \Magento\Catalog\Model\Product */
25+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
26+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
27+
->setAttributeSetId(4)
28+
->setWebsiteIds([1])
29+
->setName('Simple Product 2')
30+
->setSku('simple2')
31+
->setPrice(10)
32+
->setDescription('Description with <b>html tag</b>')
33+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
34+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
35+
->setCategoryIds([2])
36+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
37+
->setUrlKey('url with spaces 2')
38+
->save();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\Exception\NoSuchEntityException;
9+
10+
\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();
11+
12+
/** @var \Magento\Framework\Registry $registry */
13+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
14+
15+
$registry->unregister('isSecureArea');
16+
$registry->register('isSecureArea', true);
17+
18+
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
19+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
20+
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
21+
try {
22+
$product = $productRepository->get('simple1', false, null, true);
23+
$productRepository->delete($product);
24+
$product = $productRepository->get('simple2', false, null, true);
25+
$productRepository->delete($product);
26+
} catch (NoSuchEntityException $e) {
27+
}
28+
$registry->unregister('isSecureArea');
29+
$registry->register('isSecureArea', false);

dev/tests/integration/testsuite/Magento/CatalogImportExport/Model/Import/ProductTest/ProductUrlKeyTest.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,9 @@ public function testExistingProductWithUrlKeys()
202202
public function testAddUpdateProductWithInvalidUrlKeys() : void
203203
{
204204
$products = [
205-
'simple1' => 'cuvée merlot-cabernet igp pays d\'oc frankrijk',
205+
'simple1' => 'cuvee-merlot-cabernet-igp-pays-d-oc-frankrijk',
206206
'simple2' => 'normal-url',
207-
'simple3' => 'some!wrong\'url'
207+
'simple3' => 'some-wrong-url'
208208
];
209209
// added by _files/products_to_import_with_invalid_url_keys.csv
210210
$this->importedProducts[] = 'simple3';
@@ -311,10 +311,10 @@ public function testImportWithNonLatinUrlKeys()
311311
{
312312
$productsCreatedByFixture = [
313313
'ukrainian-with-url-key' => 'nove-im-ja-pislja-importu-scho-stane-url-key',
314-
'ukrainian-without-url-key' => 'новий url key після імпорту',
314+
'ukrainian-without-url-key' => 'novij-url-key-pislja-importu',
315315
];
316316
$productsImportedByCsv = [
317-
'imported-ukrainian-with-url-key' => 'імпортований продукт',
317+
'imported-ukrainian-with-url-key' => 'importovanij-produkt',
318318
'imported-ukrainian-without-url-key' => 'importovanij-produkt-bez-url-key',
319319
];
320320
$productSkuMap = array_merge($productsCreatedByFixture, $productsImportedByCsv);
@@ -345,4 +345,42 @@ public function testImportWithNonLatinUrlKeys()
345345
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
346346
}
347347
}
348+
349+
/**
350+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_spaces_in_url_key.php
351+
* @magentoDbIsolation disabled
352+
* @magentoAppIsolation enabled
353+
* @return void
354+
* @throws \Magento\Framework\Exception\LocalizedException
355+
*/
356+
public function testImportWithSpacesInUrlKeys()
357+
{
358+
$products = [
359+
'simple1' => 'url-with-spaces-1',
360+
'simple2' => 'url-with-spaces-2'
361+
];
362+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
363+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
364+
$source = $this->objectManager->create(
365+
\Magento\ImportExport\Model\Import\Source\Csv::class,
366+
[
367+
'file' => __DIR__ . '/../_files/products_to_import_with_spaces_in_url_keys.csv',
368+
'directory' => $directory,
369+
]
370+
);
371+
$errors = $this->_model->setParameters(
372+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE, 'entity' => 'catalog_product']
373+
)
374+
->setSource($source)
375+
->validateData();
376+
377+
$this->assertEquals($errors->getErrorsCount(), 0);
378+
$this->_model->importData();
379+
380+
/** @var ProductRepositoryInterface $productRepository */
381+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
382+
foreach ($products as $productSku => $productUrlKey) {
383+
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
384+
}
385+
}
348386
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
sku,product_type,store_view_code,name,price,attribute_set_code,url_key
2+
simple1,simple,,"simple 1",25,Default,"url with spaces 1"
3+
simple2,simple,,"simple 2",34,Default,"url with spaces 2"

0 commit comments

Comments
 (0)