Skip to content

Commit 54cea64

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-62995' into 2.1.8-develop-pr19
2 parents 1bb6d00 + ba2a63b commit 54cea64

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,10 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
568568
/** @var array */
569569
protected $productUrlSuffix = [];
570570

571-
/** @var array */
571+
/**
572+
* @var array
573+
* @deprecated
574+
*/
572575
protected $productUrlKeys = [];
573576

574577
/**
@@ -1039,8 +1042,8 @@ protected function _initTypeModels()
10391042
if (!$model instanceof \Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType) {
10401043
throw new \Magento\Framework\Exception\LocalizedException(
10411044
__(
1042-
'Entity type model must be an instance of '
1043-
. 'Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType'
1045+
"Entity type model must be an instance of '%1'",
1046+
\Magento\CatalogImportExport\Model\Import\Product\Type\AbstractType::class
10441047
)
10451048
);
10461049
}
@@ -1521,6 +1524,10 @@ protected function _saveProducts()
15211524
}
15221525
$rowScope = $this->getRowScope($rowData);
15231526

1527+
if (empty($rowData[self::URL_KEY])) {
1528+
$rowData[self::URL_KEY] = $this->getUrlKey($rowData);
1529+
}
1530+
15241531
$rowSku = $rowData[self::COL_SKU];
15251532

15261533
if (null === $rowSku) {
@@ -2636,18 +2643,22 @@ protected function getProductUrlSuffix($storeId = null)
26362643
}
26372644

26382645
/**
2646+
* Return url key if specified, or generate one from product name otherwise.
2647+
*
26392648
* @param array $rowData
26402649
* @return string
26412650
*/
26422651
protected function getUrlKey($rowData)
26432652
{
26442653
if (!empty($rowData[self::URL_KEY])) {
2645-
$this->productUrlKeys[$rowData[self::COL_SKU]] = $rowData[self::URL_KEY];
2654+
return $rowData[self::URL_KEY];
26462655
}
2647-
$urlKey = !empty($this->productUrlKeys[$rowData[self::COL_SKU]])
2648-
? $this->productUrlKeys[$rowData[self::COL_SKU]]
2649-
: $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
2650-
return $urlKey;
2656+
2657+
if (!empty($rowData[self::COL_NAME])) {
2658+
return $this->productUrl->formatUrlKey($rowData[self::COL_NAME]);
2659+
}
2660+
2661+
return '';
26512662
}
26522663

26532664
/**

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ public function testProductsWithMultipleStores()
856856
$this->_model->importData();
857857

858858
/** @var \Magento\Catalog\Model\Product $product */
859-
$product = $objectManager->create('Magento\Catalog\Model\Product');
859+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
860860
$id = $product->getIdBySku('Configurable 03');
861861
$product->load($id);
862862
$this->assertEquals('1', $product->getHasOptions());
@@ -1142,6 +1142,47 @@ public function testValidateUrlKeys($importFile, $errorsCount)
11421142
}
11431143
}
11441144

1145+
/**
1146+
* Test import products without url keys will auto generate ones.
1147+
*
1148+
* @magentoDataFixture Magento/Catalog/_files/product_simple_with_url_key.php
1149+
* @magentoAppIsolation enabled
1150+
*/
1151+
public function testImportWithoutUrlKeys()
1152+
{
1153+
$products = [
1154+
'simple1' => 'simple-1',
1155+
'simple2' => 'simple-2',
1156+
'simple3' => 'simple-3'
1157+
];
1158+
$filesystem = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
1159+
->create(\Magento\Framework\Filesystem::class);
1160+
$directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT);
1161+
$source = $this->objectManager->create(
1162+
\Magento\ImportExport\Model\Import\Source\Csv::class,
1163+
[
1164+
'file' => __DIR__ . '/_files/products_to_import_without_url_keys.csv',
1165+
'directory' => $directory
1166+
]
1167+
);
1168+
1169+
$errors = $this->_model->setParameters(
1170+
['behavior' => \Magento\ImportExport\Model\Import::BEHAVIOR_APPEND, 'entity' => 'catalog_product']
1171+
)
1172+
->setSource($source)
1173+
->validateData();
1174+
1175+
$this->assertTrue($errors->getErrorsCount() == 0);
1176+
$this->_model->importData();
1177+
1178+
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
1179+
\Magento\Catalog\Api\ProductRepositoryInterface::class
1180+
);
1181+
foreach ($products as $productSku => $productUrlKey) {
1182+
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
1183+
}
1184+
}
1185+
11451186
/**
11461187
* @return array
11471188
*/
@@ -1235,7 +1276,7 @@ public function testProductWithLinks()
12351276
$productId = $resource->getIdBySku('simple4');
12361277
/** @var \Magento\Catalog\Model\Product $product */
12371278
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
1238-
'Magento\Catalog\Model\Product'
1279+
\Magento\Catalog\Model\Product::class
12391280
);
12401281
$product->load($productId);
12411282
$productLinks = [
@@ -1285,7 +1326,7 @@ public function testExistingProductWithUrlKeys()
12851326
$this->_model->importData();
12861327

12871328
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
1288-
'Magento\Catalog\Api\ProductRepositoryInterface'
1329+
\Magento\Catalog\Api\ProductRepositoryInterface::class
12891330
);
12901331
foreach ($products as $productSku => $productUrlKey) {
12911332
$this->assertEquals($productUrlKey, $productRepository->get($productSku)->getUrlKey());
@@ -1325,7 +1366,7 @@ public function testProductWithUseConfigSettings()
13251366
foreach ($products as $sku => $manageStockUseConfig) {
13261367
/** @var \Magento\CatalogInventory\Model\StockRegistry $stockRegistry */
13271368
$stockRegistry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
1328-
'Magento\CatalogInventory\Model\StockRegistry'
1369+
\Magento\CatalogInventory\Model\StockRegistry::class
13291370
);
13301371
$stockItem = $stockRegistry->getStockItemBySku($sku);
13311372
$this->assertEquals($manageStockUseConfig, $stockItem->getUseConfigManageStock());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sku,product_type,store_view_code,name,price,attribute_set_code,url_key
2+
simple1,simple,,"simple 1",25,Default,""
3+
simple2,simple,,"simple 2",34,Default,""
4+
simple3,simple,,"simple 3",58,Default,""

0 commit comments

Comments
 (0)