Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved check when CategoryProcessor attempts to create a new category #8930

Merged
merged 5 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1529,6 +1529,9 @@ protected function _saveProducts()
$existingImages = $this->getExistingImages($bunch);

foreach ($bunch as $rowNum => $rowData) {
// reset category processor's failed categories array
$this->categoryProcessor->clearFailedCategories();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change clears failed categories' array for every bunch, avoiding to log the same failed categories every time a bunch is imported. See app/code/Magento/CatalogImportExport/Model/Import/Product.php::processRowCategories that is called for every bunch's import called in the same file at line 1603


if (!$this->validateRow($rowData, $rowNum)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ protected function initCategories()
for ($i = 1; $i < $pathSize; $i++) {
$path[] = $collection->getItemById((int)$structure[$i])->getName();
}
$index = implode(self::DELIMITER_CATEGORY, $path);
/** @var string $index */
$index = $this->standardizeString(
implode(self::DELIMITER_CATEGORY, $path)
);
$this->categories[$index] = $category->getId();
}
}
Expand Down Expand Up @@ -123,13 +126,16 @@ protected function createCategory($name, $parentId)
*/
protected function upsertCategory($categoryPath)
{
if (!isset($this->categories[$categoryPath])) {
/** @var string $index */
$index = $this->standardizeString($categoryPath);

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

foreach ($pathParts as $pathPart) {
$path .= $pathPart;
$path .= $this->standardizeString($pathPart);
if (!isset($this->categories[$path])) {
$this->categories[$path] = $this->createCategory($pathPart, $parentId);
}
Expand All @@ -138,7 +144,7 @@ protected function upsertCategory($categoryPath)
}
}

return $this->categories[$categoryPath];
return $this->categories[$index];
}

/**
Expand Down Expand Up @@ -171,7 +177,7 @@ public function upsertCategories($categoriesString, $categoriesSeparator)
* @param string $category
* @param \Magento\Framework\Exception\AlreadyExistsException $exception
*
* @return array
* @return $this
*/
private function addFailedCategory($category, $exception)
{
Expand All @@ -190,7 +196,18 @@ private function addFailedCategory($category, $exception)
*/
public function getFailedCategories()
{
return $this->failedCategories;
return $this->failedCategories;
}

/**
* Resets failed categories' array
*
* @return $this
*/
public function clearFailedCategories()
{
$this->failedCategories = [];
return $this;
}

/**
Expand All @@ -204,4 +221,17 @@ public function getCategoryById($categoryId)
{
return isset($this->categoriesCache[$categoryId]) ? $this->categoriesCache[$categoryId] : null;
}

/**
* Standardize a string.
* For now it performs only a lowercase action, this method is here to include more complex checks in the future
* if needed.
*
* @param string $string
* @return string
*/
private function standardizeString($string)
{
return mb_strtolower($string);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected function setUp()
->disableOriginalConstructor()
->getMock();
$childCategory->method('getId')->will($this->returnValue(self::CHILD_CATEGORY_ID));
$childCategory->method('getName')->will($this->returnValue('Child'));
$childCategory->method('getName')->will($this->returnValue(self::CHILD_CATEGORY_NAME));
$childCategory->method('getPath')->will($this->returnValue(
self::PARENT_CATEGORY_ID . CategoryProcessor::DELIMITER_CATEGORY
. self::CHILD_CATEGORY_ID
Expand Down Expand Up @@ -115,6 +115,22 @@ public function testUpsertCategories()
$this->assertArrayHasKey(self::CHILD_CATEGORY_ID, array_flip($categoryIds));
}

public function testClearFailedCategories()
{
$dummyFailedCategory = [
[
'category' => 'dummy category',
'exception' => 'dummy exception',
]
];

$this->setPropertyValue($this->categoryProcessor, 'failedCategories', $dummyFailedCategory);
$this->assertCount(count($dummyFailedCategory), $this->categoryProcessor->getFailedCategories());

$this->categoryProcessor->clearFailedCategories();
$this->assertEmpty($this->categoryProcessor->getFailedCategories());
}

/**
* Cover getCategoryById().
*
Expand Down