Skip to content

Commit

Permalink
MAGETWO-83131: #6948: CatalogImportExport categoryProcessor does not …
Browse files Browse the repository at this point in the history
…support escaped delimiter #11801
  • Loading branch information
Oleksii Korshenko authored Nov 3, 2017
2 parents c638d82 + 660cbb0 commit dbfe642
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 10 deletions.
21 changes: 19 additions & 2 deletions app/code/Magento/CatalogImportExport/Model/Export/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\CatalogImportExport\Model\Export;

use Magento\CatalogImportExport\Model\Import\Product\CategoryProcessor;
use Magento\ImportExport\Model\Import;
use \Magento\Store\Model\Store;
use \Magento\CatalogImportExport\Model\Import\Product as ImportProduct;
Expand Down Expand Up @@ -438,11 +439,12 @@ protected function initCategories()
if ($pathSize > 1) {
$path = [];
for ($i = 1; $i < $pathSize; $i++) {
$path[] = $collection->getItemById($structure[$i])->getName();
$name = $collection->getItemById($structure[$i])->getName();
$path[] = $this->quoteCategoryDelimiter($name);
}
$this->_rootCategories[$category->getId()] = array_shift($path);
if ($pathSize > 2) {
$this->_categories[$category->getId()] = implode('/', $path);
$this->_categories[$category->getId()] = implode(CategoryProcessor::DELIMITER_CATEGORY, $path);
}
}
}
Expand Down Expand Up @@ -1470,4 +1472,19 @@ protected function getProductEntityLinkField()
}
return $this->productEntityLinkField;
}

/**
* Quoting category delimiter character in string.
*
* @param string $string
* @return string
*/
private function quoteCategoryDelimiter($string)
{
return str_replace(
CategoryProcessor::DELIMITER_CATEGORY,
'\\' . CategoryProcessor::DELIMITER_CATEGORY,
$string
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ protected function initCategories()
if ($pathSize > 1) {
$path = [];
for ($i = 1; $i < $pathSize; $i++) {
$path[] = $collection->getItemById((int)$structure[$i])->getName();
$name = $collection->getItemById((int)$structure[$i])->getName();
$path[] = $this->quoteDelimiter($name);
}
/** @var string $index */
$index = $this->standardizeString(
Expand Down Expand Up @@ -114,7 +115,7 @@ protected function createCategory($name, $parentId)
}
$category->setPath($parentCategory->getPath());
$category->setParentId($parentId);
$category->setName($name);
$category->setName($this->unquoteDelimiter($name));
$category->setIsActive(true);
$category->setIncludeInMenu(true);
$category->setAttributeSetId($category->getDefaultAttributeSetId());
Expand All @@ -137,7 +138,7 @@ protected function upsertCategory($categoryPath)
$index = $this->standardizeString($categoryPath);

if (!isset($this->categories[$index])) {
$pathParts = explode(self::DELIMITER_CATEGORY, $categoryPath);
$pathParts = preg_split('~(?<!\\\)' . preg_quote(self::DELIMITER_CATEGORY, '~') . '~', $categoryPath);
$parentId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
$path = '';

Expand Down Expand Up @@ -243,4 +244,26 @@ private function standardizeString($string)
{
return mb_strtolower($string);
}

/**
* Quoting delimiter character in string.
*
* @param string $string
* @return string
*/
private function quoteDelimiter($string)
{
return str_replace(self::DELIMITER_CATEGORY, '\\' . self::DELIMITER_CATEGORY, $string);
}

/**
* Remove quoting delimiter in string.
*
* @param string $string
* @return string
*/
private function unquoteDelimiter($string)
{
return str_replace('\\' . self::DELIMITER_CATEGORY, self::DELIMITER_CATEGORY, $string);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
$category->isObjectNew(true);
$category->setId(
3331
)->setCreatedAt(
'2017-06-23 09:50:07'
)->setName(
'Category with slash/ symbol'
)->setParentId(
2
)->setPath(
'1/2/3331'
)->setLevel(
2
)->setAvailableSortBy(
['position', 'name']
)->setDefaultSortBy(
'name'
)->setIsActive(
true
)->setPosition(
1
)->save();
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public function testExportSpecialChars()
);
$exportData = $this->model->export();
$this->assertContains('simple ""1""', $exportData);
$this->assertContains('Category with slash\/ symbol', $exportData);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sku,product_type,store_view_code,name,price,attribute_set_code,categories,product_websites
simple1,simple,,"simple 2",25,Default,"Default Category/Category 1|Default Category/Category 2",base
simple1,simple,,"simple 2",25,Default,"Default Category/Category 1|Default Category/Category\/ 2",base
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sku,product_type,store_view_code,name,price,attribute_set_code,categories,product_websites,url_key
simple1,simple,,"simple 1",25,Default,"Default Category/Category 1,Default Category/Category 2",base,simple1-ds
simple1,simple,,"simple 1",25,Default,"Default Category/Category 1,Default Category/Category\/ 2",base,simple1-ds
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
/** Create category */
require dirname(dirname(__DIR__)) . '/Catalog/_files/category.php';
/** Create category with special chars */
require dirname(dirname(__DIR__)) . '/Catalog/_files/catalog_category_with_slash.php';
/** Create fixture store */
require dirname(dirname(__DIR__)) . '/Store/_files/second_store.php';
/** Create product with multiselect attribute and values */
Expand All @@ -28,10 +30,8 @@
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setWebsiteIds([1])
->setCateroryIds([])
->setStockData(['qty' => 100, 'is_in_stock' => 1])
->setCanSaveCustomOptions(true)
->setCategoryIds([333])
->setUpSellLinkData([$product->getId() => ['position' => 1]]);
->setCategoryIds([333, 3331]);

$productModel->setOptions([])->save();

0 comments on commit dbfe642

Please sign in to comment.