Skip to content

Commit c105d4b

Browse files
ENGCOM-9103: 32911 fixed product import with url key with spaces #33088
2 parents 90ed3b2 + 0ab629d commit c105d4b

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
@@ -203,9 +203,9 @@ public function testExistingProductWithUrlKeys()
203203
public function testAddUpdateProductWithInvalidUrlKeys() : void
204204
{
205205
$products = [
206-
'simple1' => 'cuvée merlot-cabernet igp pays d\'oc frankrijk',
206+
'simple1' => 'cuvee-merlot-cabernet-igp-pays-d-oc-frankrijk',
207207
'simple2' => 'normal-url',
208-
'simple3' => 'some!wrong\'url'
208+
'simple3' => 'some-wrong-url'
209209
];
210210
// added by _files/products_to_import_with_invalid_url_keys.csv
211211
$this->importedProducts[] = 'simple3';
@@ -312,10 +312,10 @@ public function testImportWithNonLatinUrlKeys()
312312
{
313313
$productsCreatedByFixture = [
314314
'ukrainian-with-url-key' => 'nove-im-ja-pislja-importu-scho-stane-url-key',
315-
'ukrainian-without-url-key' => 'новий url key після імпорту',
315+
'ukrainian-without-url-key' => 'novij-url-key-pislja-importu',
316316
];
317317
$productsImportedByCsv = [
318-
'imported-ukrainian-with-url-key' => 'імпортований продукт',
318+
'imported-ukrainian-with-url-key' => 'importovanij-produkt',
319319
'imported-ukrainian-without-url-key' => 'importovanij-produkt-bez-url-key',
320320
];
321321
$productSkuMap = array_merge($productsCreatedByFixture, $productsImportedByCsv);
@@ -346,4 +346,42 @@ public function testImportWithNonLatinUrlKeys()
346346
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
347347
}
348348
}
349+
350+
/**
351+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_spaces_in_url_key.php
352+
* @magentoDbIsolation disabled
353+
* @magentoAppIsolation enabled
354+
* @return void
355+
* @throws \Magento\Framework\Exception\LocalizedException
356+
*/
357+
public function testImportWithSpacesInUrlKeys()
358+
{
359+
$products = [
360+
'simple1' => 'url-with-spaces-1',
361+
'simple2' => 'url-with-spaces-2'
362+
];
363+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
364+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
365+
$source = $this->objectManager->create(
366+
\Magento\ImportExport\Model\Import\Source\Csv::class,
367+
[
368+
'file' => __DIR__ . '/../_files/products_to_import_with_spaces_in_url_keys.csv',
369+
'directory' => $directory,
370+
]
371+
);
372+
$errors = $this->_model->setParameters(
373+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE, 'entity' => 'catalog_product']
374+
)
375+
->setSource($source)
376+
->validateData();
377+
378+
$this->assertEquals($errors->getErrorsCount(), 0);
379+
$this->_model->importData();
380+
381+
/** @var ProductRepositoryInterface $productRepository */
382+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
383+
foreach ($products as $productSku => $productUrlKey) {
384+
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
385+
}
386+
}
349387
}
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)