Skip to content

Commit 8474a57

Browse files
author
Oleksandr Dubovyk
committed
Merge remote-tracking branch 'origin/develop' into PR
2 parents 570110a + 38dd0ed commit 8474a57

File tree

17 files changed

+301
-47
lines changed

17 files changed

+301
-47
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

Lines changed: 70 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,18 @@ public function execute()
9898
return $resultRedirect->setPath('catalog/*/', ['_current' => true, 'id' => null]);
9999
}
100100

101-
$refreshTree = false;
102101
$data['general'] = $this->getRequest()->getPostValue();
102+
$isNewCategory = !isset($data['general']['entity_id']);
103103
$data = $this->stringToBoolConverting($this->stringToBoolInputs, $data);
104104
$data = $this->imagePreprocessing($data);
105105
$storeId = isset($data['general']['store_id']) ? $data['general']['store_id'] : null;
106-
if ($data) {
106+
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
107+
if ($data['general']) {
107108
$category->addData($this->_filterCategoryPostData($data['general']));
108-
if (!$category->getId()) {
109-
$parentId = isset($data['general']['parent']) ? $data['general']['parent'] : null;
110-
if (!$parentId) {
111-
if ($storeId) {
112-
$parentId = $this->_objectManager->get(
113-
'Magento\Store\Model\StoreManagerInterface'
114-
)->getStore(
115-
$storeId
116-
)->getRootCategoryId();
117-
} else {
118-
$parentId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
119-
}
120-
}
121-
$parentCategory = $this->_objectManager->create('Magento\Catalog\Model\Category')->load($parentId);
109+
if ($isNewCategory) {
110+
$parentCategory = $this->getParentCategory($parentId, $storeId);
122111
$category->setPath($parentCategory->getPath());
123-
$category->setParentId($parentId);
112+
$category->setParentId($parentCategory->getId());
124113
}
125114

126115
/**
@@ -193,15 +182,21 @@ public function execute()
193182

194183
$category->save();
195184
$this->messageManager->addSuccess(__('You saved the category.'));
196-
$refreshTree = true;
185+
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
186+
$this->messageManager->addError($e->getMessage());
187+
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
188+
$this->_getSession()->setCategoryData($data);
197189
} catch (\Exception $e) {
198190
$this->messageManager->addError(__('Something went wrong while saving the category.'));
199191
$this->_objectManager->get(\Psr\Log\LoggerInterface::class)->critical($e);
200192
$this->_getSession()->setCategoryData($data);
201-
$refreshTree = false;
202193
}
203194
}
204195

196+
$hasError = (bool)$this->messageManager->getMessages()->getCountByType(
197+
\Magento\Framework\Message\MessageInterface::TYPE_ERROR
198+
);
199+
205200
if ($this->getRequest()->getPost('return_session_messages_only')) {
206201
$category->load($category->getId());
207202
// to obtain truncated category name
@@ -214,23 +209,17 @@ public function execute()
214209
return $resultJson->setData(
215210
[
216211
'messages' => $block->getGroupedHtml(),
217-
'error' => !$refreshTree,
212+
'error' => $hasError,
218213
'category' => $category->toArray(),
219214
]
220215
);
221216
}
222217

223-
$redirectParams = [
224-
'_current' => true,
225-
'id' => $category->getId()
226-
];
227-
if ($storeId) {
228-
$redirectParams['store'] = $storeId;
229-
}
218+
$redirectParams = $this->getRedirectParams($isNewCategory, $hasError, $category->getId(), $parentId, $storeId);
230219

231220
return $resultRedirect->setPath(
232-
'catalog/*/edit',
233-
$redirectParams
221+
$redirectParams['path'],
222+
$redirectParams['params']
234223
);
235224
}
236225

@@ -283,4 +272,56 @@ public function stringToBoolConverting($stringToBoolInputs, $data)
283272
}
284273
return $data;
285274
}
275+
276+
/**
277+
* Get parent category
278+
*
279+
* @param int $parentId
280+
* @param int $storeId
281+
*
282+
* @return \Magento\Catalog\Model\Category
283+
*/
284+
protected function getParentCategory($parentId, $storeId)
285+
{
286+
if (!$parentId) {
287+
if ($storeId) {
288+
$parentId = $this->_objectManager->get(
289+
\Magento\Store\Model\StoreManagerInterface::class
290+
)->getStore(
291+
$storeId
292+
)->getRootCategoryId();
293+
} else {
294+
$parentId = \Magento\Catalog\Model\Category::TREE_ROOT_ID;
295+
}
296+
}
297+
return $this->_objectManager->create(\Magento\Catalog\Model\Category::class)->load($parentId);
298+
}
299+
300+
/**
301+
* Get category redirect path
302+
*
303+
* @param bool $isNewCategory
304+
* @param bool $hasError
305+
* @param int $categoryId
306+
* @param int $parentId
307+
* @param int $storeId
308+
*
309+
* @return array
310+
*/
311+
protected function getRedirectParams($isNewCategory, $hasError, $categoryId, $parentId, $storeId)
312+
{
313+
$params = ['_current' => true];
314+
if ($storeId) {
315+
$params['store'] = $storeId;
316+
}
317+
if ($isNewCategory && $hasError) {
318+
$path = 'catalog/*/add';
319+
$params['parent'] = $parentId;
320+
} else {
321+
$path = 'catalog/*/edit';
322+
$params['id'] = $categoryId;
323+
324+
}
325+
return ['path' => $path, 'params' => $params];
326+
}
286327
}

app/code/Magento/Catalog/Model/Category.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ public function move($parentId, $afterCategoryId)
442442
if (!$productIndexer->isScheduled()) {
443443
$productIndexer->reindexList(array_merge($this->getPathIds(), $oldParentIds));
444444
}
445+
$this->_eventManager->dispatch('clean_cache_by_tags', ['object' => $this]);
445446
$this->_cacheManager->clean([self::CACHE_TAG]);
446447

447448
return $this;

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ public function testExecute($categoryId, $storeId, $parentId)
368368
false
369369
);
370370

371+
$messagesMock->expects($this->once())
372+
->method('getCountByType')
373+
->will($this->returnValue(0));
374+
371375
$this->resultRedirectFactoryMock->expects($this->once())
372376
->method('create')
373377
->will($this->returnValue($resultRedirectMock));
@@ -430,7 +434,7 @@ public function testExecute($categoryId, $storeId, $parentId)
430434
$categoryMock->expects($this->once())
431435
->method('addData')
432436
->with($postData);
433-
$categoryMock->expects($this->at(0))
437+
$categoryMock->expects($this->any())
434438
->method('getId')
435439
->will($this->returnValue($categoryId));
436440

@@ -452,6 +456,9 @@ public function testExecute($categoryId, $storeId, $parentId)
452456
$parentCategoryMock->expects($this->once())
453457
->method('getPath')
454458
->will($this->returnValue('parent_category_path'));
459+
$parentCategoryMock->expects($this->once())
460+
->method('getId')
461+
->will($this->returnValue($parentId));
455462
$categoryMock->expects($this->once())
456463
->method('setPath')
457464
->with('parent_category_path');
@@ -518,9 +525,8 @@ public function testExecute($categoryId, $storeId, $parentId)
518525
$layoutMock->expects($this->once())
519526
->method('getMessagesBlock')
520527
->will($this->returnValue($blockMock));
521-
$this->messageManagerMock->expects($this->once())
528+
$this->messageManagerMock->expects($this->any())
522529
->method('getMessages')
523-
->with(true)
524530
->will($this->returnValue($messagesMock));
525531
$blockMock->expects($this->once())
526532
->method('setMessages')

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingError;
1818
use Magento\ImportExport\Model\Import\ErrorProcessing\ProcessingErrorAggregatorInterface;
1919
use Magento\Catalog\Api\Data\ProductInterface;
20+
use Magento\ImportExport\Model\Import\Entity\AbstractEntity;
2021

2122
/**
2223
* Import entity product model
@@ -231,15 +232,15 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
231232
*/
232233
protected $_messageTemplates = [
233234
ValidatorInterface::ERROR_INVALID_SCOPE => 'Invalid value in Scope column',
234-
ValidatorInterface::ERROR_INVALID_WEBSITE => 'Invalid value in Website column (website does not exists?)',
235+
ValidatorInterface::ERROR_INVALID_WEBSITE => 'Invalid value in Website column (website does not exist?)',
235236
ValidatorInterface::ERROR_INVALID_STORE => 'Invalid value in Store column (store doesn\'t exist?)',
236237
ValidatorInterface::ERROR_INVALID_ATTR_SET => 'Invalid value for Attribute Set column (set doesn\'t exist?)',
237238
ValidatorInterface::ERROR_INVALID_TYPE => 'Product Type is invalid or not supported',
238-
ValidatorInterface::ERROR_INVALID_CATEGORY => 'Category does not exists',
239+
ValidatorInterface::ERROR_INVALID_CATEGORY => 'Category does not exist',
239240
ValidatorInterface::ERROR_VALUE_IS_REQUIRED => 'Please make sure attribute "%s" is not empty.',
240241
ValidatorInterface::ERROR_TYPE_CHANGED => 'Trying to change type of existing products',
241242
ValidatorInterface::ERROR_SKU_IS_EMPTY => 'SKU is empty',
242-
ValidatorInterface::ERROR_NO_DEFAULT_ROW => 'Default values row does not exists',
243+
ValidatorInterface::ERROR_NO_DEFAULT_ROW => 'Default values row does not exist',
243244
ValidatorInterface::ERROR_CHANGE_TYPE => 'Product type change is not allowed',
244245
ValidatorInterface::ERROR_DUPLICATE_SCOPE => 'Duplicate scope',
245246
ValidatorInterface::ERROR_DUPLICATE_SKU => 'Duplicate SKU',
@@ -263,7 +264,7 @@ class Product extends \Magento\ImportExport\Model\Import\Entity\AbstractEntity
263264
ValidatorInterface::ERROR_MEDIA_PATH_NOT_ACCESSIBLE => 'Imported resource (image) does not exist in the local media storage',
264265
ValidatorInterface::ERROR_MEDIA_URL_NOT_ACCESSIBLE => 'Imported resource (image) could not be downloaded from external resource due to timeout or access permissions',
265266
ValidatorInterface::ERROR_INVALID_WEIGHT => 'Product weight is invalid',
266-
ValidatorInterface::ERROR_DUPLICATE_URL_KEY => 'Specified url key is already exist',
267+
ValidatorInterface::ERROR_DUPLICATE_URL_KEY => 'Specified url key already exists',
267268
];
268269

269270
/**
@@ -1467,10 +1468,12 @@ protected function _saveProducts()
14671468
if (!array_key_exists($rowSku, $this->categoriesCache)) {
14681469
$this->categoriesCache[$rowSku] = [];
14691470
}
1471+
$rowData['rowNum'] = $rowNum;
14701472
$categoryIds = $this->processRowCategories($rowData);
14711473
foreach ($categoryIds as $id) {
14721474
$this->categoriesCache[$rowSku][$id] = true;
14731475
}
1476+
unset($rowData['rowNum']);
14741477

14751478
// 4.1. Tier prices phase
14761479
if (!empty($rowData['_tier_price_website'])) {
@@ -1625,6 +1628,12 @@ protected function _saveProducts()
16251628
}
16261629
}
16271630

1631+
foreach ($bunch as $rowNum => $rowData) {
1632+
if ($this->getErrorAggregator()->isRowInvalid($rowNum)) {
1633+
unset($bunch[$rowNum]);
1634+
}
1635+
}
1636+
16281637
$this->saveProductEntity(
16291638
$entityRowsIn,
16301639
$entityRowsUp
@@ -1661,6 +1670,16 @@ protected function processRowCategories($rowData)
16611670
$categoriesString,
16621671
$this->getMultipleValueSeparator()
16631672
);
1673+
foreach ($this->categoryProcessor->getFailedCategories() as $error) {
1674+
$this->errorAggregator->addError(
1675+
AbstractEntity::ERROR_CODE_CATEGORY_NOT_VALID,
1676+
ProcessingError::ERROR_LEVEL_NOT_CRITICAL,
1677+
$rowData['rowNum'],
1678+
self::COL_CATEGORY,
1679+
__('Category "%1" has not been created.', $error['category'])
1680+
. ' ' . $error['exception']->getMessage()
1681+
);
1682+
}
16641683
}
16651684
return $categoryIds;
16661685
}

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

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class CategoryProcessor
3838
*/
3939
protected $categoryFactory;
4040

41+
/**
42+
* Failed categories during creation
43+
*
44+
* @var array
45+
*/
46+
protected $failedCategories = [];
47+
4148
/**
4249
* @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryColFactory
4350
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
@@ -112,7 +119,7 @@ protected function createCategory($name, $parentId)
112119
* Returns ID of category by string path creating nonexistent ones.
113120
*
114121
* @param string $categoryPath
115-
*
122+
*
116123
* @return int
117124
*/
118125
protected function upsertCategory($categoryPath)
@@ -149,12 +156,44 @@ public function upsertCategories($categoriesString, $categoriesSeparator)
149156
$categories = explode($categoriesSeparator, $categoriesString);
150157

151158
foreach ($categories as $category) {
152-
$categoriesIds[] = $this->upsertCategory($category);
159+
try {
160+
$categoriesIds[] = $this->upsertCategory($category);
161+
} catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
162+
$this->addFailedCategory($category, $e);
163+
}
153164
}
154165

155166
return $categoriesIds;
156167
}
157168

169+
/**
170+
* Add failed category
171+
*
172+
* @param string $category
173+
* @param \Magento\Framework\Exception\AlreadyExistsException $exception
174+
*
175+
* @return array
176+
*/
177+
private function addFailedCategory($category, $exception)
178+
{
179+
$this->failedCategories[] =
180+
[
181+
'category' => $category,
182+
'exception' => $exception,
183+
];
184+
return $this;
185+
}
186+
187+
/**
188+
* Return failed categories
189+
*
190+
* @return array
191+
*/
192+
public function getFailedCategories()
193+
{
194+
return $this->failedCategories;
195+
}
196+
158197
/**
159198
* Get category by Id
160199
*

app/code/Magento/ImportExport/Model/Import/Entity/AbstractEntity.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ abstract class AbstractEntity
3939
const ERROR_CODE_INVALID_ATTRIBUTE = 'invalidAttributeName';
4040
const ERROR_CODE_WRONG_QUOTES = 'wrongQuotes';
4141
const ERROR_CODE_COLUMNS_NUMBER = 'wrongColumnsNumber';
42+
const ERROR_CODE_CATEGORY_NOT_VALID = 'categoryNotValid';
4243

4344
protected $errorMessageTemplates = [
4445
self::ERROR_CODE_SYSTEM_EXCEPTION => 'General system exception happened',

dev/tests/functional/lib/Magento/Mtf/Client/Element/SwitcherElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SwitcherElement extends SimpleElement
1717
*
1818
* @var string
1919
*/
20-
protected $parentContainer = 'parent::div[@class="switcher"]';
20+
protected $parentContainer = 'parent::div[@data-role="switcher"]';
2121

2222
/**
2323
* Set value to Yes or No.

dev/tests/functional/tests/app/Magento/Catalog/Test/Page/Category/CatalogCategory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class CatalogCategory extends Page
2525
*
2626
* @var string
2727
*/
28-
protected $formBlock = '.entry-edit';
28+
protected $formBlock = '//div[contains(@data-bind, "category_form")]';
2929

3030
/**
3131
* Categories tree block.
@@ -80,7 +80,7 @@ public function open(array $params = [])
8080
public function getFormBlock()
8181
{
8282
return Factory::getBlockFactory()->getMagentoCatalogAdminhtmlCategoryEditCategoryForm(
83-
$this->browser->find($this->formBlock, Locator::SELECTOR_CSS)
83+
$this->browser->find($this->formBlock, Locator::SELECTOR_XPATH)
8484
);
8585
}
8686

dev/tests/functional/tests/app/Magento/Catalog/Test/Page/Category/CatalogCategoryEdit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CatalogCategoryEdit extends Page
2626
*
2727
* @var string
2828
*/
29-
protected $formBlock = '.entry-edit';
29+
protected $formBlock = '//div[contains(@data-bind, "category_form")]';
3030

3131
/**
3232
* Categories tree block.

dev/tests/functional/tests/app/Magento/Catalog/Test/TestCase/Category/CreateCategoryEntityTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public function __inject(CatalogCategoryIndex $catalogCategoryIndex, CatalogCate
6969
*/
7070
public function test(Category $category, $addCategory)
7171
{
72-
$this->markTestIncomplete('MAGETWO-48731');
7372
$this->catalogCategoryIndex->open();
7473
$this->catalogCategoryIndex->getTreeCategories()->selectCategory($category, false);
7574
$this->catalogCategoryIndex->getTreeCategories()->$addCategory();

0 commit comments

Comments
 (0)