Skip to content

Commit a7b9176

Browse files
author
silinmykola
committed
32911 fix product import url key format
1 parent f25fc03 commit a7b9176

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
@@ -213,9 +213,9 @@ public function testExistingProductWithUrlKeys()
213213
public function testAddUpdateProductWithInvalidUrlKeys() : void
214214
{
215215
$products = [
216-
'simple1' => 'cuvée merlot-cabernet igp pays d\'oc frankrijk',
216+
'simple1' => 'cuvee-merlot-cabernet-igp-pays-d-oc-frankrijk',
217217
'simple2' => 'normal-url',
218-
'simple3' => 'some!wrong\'url'
218+
'simple3' => 'some-wrong-url'
219219
];
220220
// added by _files/products_to_import_with_invalid_url_keys.csv
221221
$this->importedProducts[] = 'simple3';
@@ -328,10 +328,10 @@ public function testImportWithNonLatinUrlKeys()
328328
{
329329
$productsCreatedByFixture = [
330330
'ukrainian-with-url-key' => 'nove-im-ja-pislja-importu-scho-stane-url-key',
331-
'ukrainian-without-url-key' => 'новий url key після імпорту',
331+
'ukrainian-without-url-key' => 'novij-url-key-pislja-importu',
332332
];
333333
$productsImportedByCsv = [
334-
'imported-ukrainian-with-url-key' => 'імпортований продукт',
334+
'imported-ukrainian-with-url-key' => 'importovanij-produkt',
335335
'imported-ukrainian-without-url-key' => 'importovanij-produkt-bez-url-key',
336336
];
337337
$productSkuMap = array_merge($productsCreatedByFixture, $productsImportedByCsv);
@@ -362,4 +362,42 @@ public function testImportWithNonLatinUrlKeys()
362362
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
363363
}
364364
}
365+
366+
/**
367+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_spaces_in_url_key.php
368+
* @magentoDbIsolation disabled
369+
* @magentoAppIsolation enabled
370+
* @return void
371+
* @throws \Magento\Framework\Exception\LocalizedException
372+
*/
373+
public function testImportWithSpacesInUrlKeys()
374+
{
375+
$products = [
376+
'simple1' => 'url-with-spaces-1',
377+
'simple2' => 'url-with-spaces-2'
378+
];
379+
$filesystem = $this->objectManager->create(\Magento\Framework\Filesystem::class);
380+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
381+
$source = $this->objectManager->create(
382+
\Magento\ImportExport\Model\Import\Source\Csv::class,
383+
[
384+
'file' => __DIR__ . '/../_files/products_to_import_with_spaces_in_url_keys.csv',
385+
'directory' => $directory,
386+
]
387+
);
388+
$errors = $this->_model->setParameters(
389+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE, 'entity' => 'catalog_product']
390+
)
391+
->setSource($source)
392+
->validateData();
393+
394+
$this->assertEquals($errors->getErrorsCount(), 0);
395+
$this->_model->importData();
396+
397+
/** @var ProductRepositoryInterface $productRepository */
398+
$productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
399+
foreach ($products as $productSku => $productUrlKey) {
400+
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
401+
}
402+
}
365403
}
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)