Skip to content

Commit

Permalink
Merge pull request #4802 from magento-epam/EPAM-PR-74
Browse files Browse the repository at this point in the history
- fixed  Category with invalid data loses new products assignment after validation
- fixed Catalog product collection filters produce errors and cause inconsistent behavior
- fixed Exported customer without modification can not be imported
  • Loading branch information
irenelagno authored Sep 20, 2019
2 parents 5860041 + 8d897fa commit 50a085d
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 21 deletions.
53 changes: 48 additions & 5 deletions app/code/Magento/Catalog/Model/ResourceModel/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
*
* @author Magento Core Team <core@magentocommerce.com>
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel;

use Magento\Catalog\Model\Indexer\Category\Product\Processor;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\EntityManager\EntityManager;
use Magento\Catalog\Model\Category as CategoryEntity;
use Magento\Catalog\Setup\CategorySetup;

/**
* Resource model for category entity
Expand Down Expand Up @@ -92,6 +94,7 @@ class Category extends AbstractResource
* @var Processor
*/
private $indexerProcessor;

/**
* Category constructor.
* @param \Magento\Eav\Model\Entity\Context $context
Expand Down Expand Up @@ -275,7 +278,7 @@ protected function _beforeSave(\Magento\Framework\DataObject $object)
if ($object->getPosition() === null) {
$object->setPosition($this->_getMaxPosition($object->getPath()) + 1);
}
$path = explode('/', $object->getPath());
$path = explode('/', (string)$object->getPath());
$level = count($path) - ($object->getId() ? 1 : 0);
$toUpdateChild = array_diff($path, [$object->getId()]);

Expand Down Expand Up @@ -314,7 +317,7 @@ protected function _afterSave(\Magento\Framework\DataObject $object)
/**
* Add identifier for new category
*/
if (substr($object->getPath(), -1) == '/') {
if (substr((string)$object->getPath(), -1) == '/') {
$object->setPath($object->getPath() . $object->getId());
$this->_savePath($object);
}
Expand Down Expand Up @@ -352,7 +355,7 @@ protected function _getMaxPosition($path)
{
$connection = $this->getConnection();
$positionField = $connection->quoteIdentifier('position');
$level = count(explode('/', $path));
$level = count(explode('/', (string)$path));
$bind = ['c_level' => $level, 'c_path' => $path . '/%'];
$select = $connection->select()->from(
$this->getTable('catalog_category_entity'),
Expand Down Expand Up @@ -717,7 +720,7 @@ public function getCategories($parent, $recursionLevel = 0, $sorted = false, $as
*/
public function getParentCategories($category)
{
$pathIds = array_reverse(explode(',', $category->getPathInStore()));
$pathIds = array_reverse(explode(',', (string)$category->getPathInStore()));
/** @var \Magento\Catalog\Model\ResourceModel\Category\Collection $categories */
$categories = $this->_categoryCollectionFactory->create();
return $categories->setStore(
Expand Down Expand Up @@ -1134,4 +1137,44 @@ private function getAggregateCount()
}
return $this->aggregateCount;
}

/**
* Get category with children.
*
* @param int $categoryId
* @return array
*/
public function getCategoryWithChildren(int $categoryId): array
{
$connection = $this->getConnection();

$selectAttributeCode = $connection->select()
->from(
['eav_attribute' => $this->getTable('eav_attribute')],
['attribute_id']
)->where('entity_type_id = ?', CategorySetup::CATEGORY_ENTITY_TYPE_ID)
->where('attribute_code = ?', 'is_anchor')
->limit(1);
$isAnchorAttributeCode = $connection->fetchOne($selectAttributeCode);
if (empty($isAnchorAttributeCode) || (int)$isAnchorAttributeCode <= 0) {
return [];
}

$select = $connection->select()
->from(
['cce' => $this->getTable('catalog_category_entity')],
['entity_id', 'parent_id', 'path']
)->join(
['cce_int' => $this->getTable('catalog_category_entity_int')],
'cce.entity_id = cce_int.entity_id',
['is_anchor' => 'cce_int.value']
)->where(
'cce_int.attribute_id = ?',
$isAnchorAttributeCode
)->where(
"cce.path LIKE '%/{$categoryId}' OR cce.path LIKE '%/{$categoryId}/%'"
)->order('path');

return $connection->fetchAll($select);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Catalog\Model\ResourceModel\Product;

Expand All @@ -22,6 +23,7 @@
use Magento\Framework\Indexer\DimensionFactory;
use Magento\Store\Model\Indexer\WebsiteDimensionProvider;
use Magento\Store\Model\Store;
use Magento\Catalog\Model\ResourceModel\Category;

/**
* Product collection
Expand Down Expand Up @@ -302,6 +304,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
*/
private $urlFinder;

/**
* @var Category
*/
private $categoryResourceModel;

/**
* Collection constructor
*
Expand Down Expand Up @@ -330,6 +337,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
* @param TableMaintainer|null $tableMaintainer
* @param PriceTableResolver|null $priceTableResolver
* @param DimensionFactory|null $dimensionFactory
* @param Category|null $categoryResourceModel
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
Expand Down Expand Up @@ -358,7 +366,8 @@ public function __construct(
MetadataPool $metadataPool = null,
TableMaintainer $tableMaintainer = null,
PriceTableResolver $priceTableResolver = null,
DimensionFactory $dimensionFactory = null
DimensionFactory $dimensionFactory = null,
Category $categoryResourceModel = null
) {
$this->moduleManager = $moduleManager;
$this->_catalogProductFlatState = $catalogProductFlatState;
Expand Down Expand Up @@ -392,6 +401,8 @@ public function __construct(
$this->priceTableResolver = $priceTableResolver ?: ObjectManager::getInstance()->get(PriceTableResolver::class);
$this->dimensionFactory = $dimensionFactory
?: ObjectManager::getInstance()->get(DimensionFactory::class);
$this->categoryResourceModel = $categoryResourceModel ?: ObjectManager::getInstance()
->get(Category::class);
}

/**
Expand Down Expand Up @@ -1673,7 +1684,11 @@ public function addFilterByRequiredOptions()
public function setVisibility($visibility)
{
$this->_productLimitationFilters['visibility'] = $visibility;
$this->_applyProductLimitations();
if ($this->getStoreId() == Store::DEFAULT_STORE_ID) {
$this->addAttributeToFilter('visibility', $visibility);
} else {
$this->_applyProductLimitations();
}

return $this;
}
Expand Down Expand Up @@ -2053,12 +2068,13 @@ protected function _applyProductLimitations()
protected function _applyZeroStoreProductLimitations()
{
$filters = $this->_productLimitationFilters;
$categories = $this->getChildrenCategories((int)$filters['category_id']);

$conditions = [
'cat_pro.product_id=e.entity_id',
$this->getConnection()->quoteInto(
'cat_pro.category_id=?',
$filters['category_id']
'cat_pro.category_id IN (?)',
$categories
),
];
$joinCond = join(' AND ', $conditions);
Expand All @@ -2079,6 +2095,39 @@ protected function _applyZeroStoreProductLimitations()
return $this;
}

/**
* Get children categories.
*
* @param int $categoryId
* @return array
*/
private function getChildrenCategories(int $categoryId): array
{
$categoryIds[] = $categoryId;
$anchorCategory = [];

$categories = $this->categoryResourceModel->getCategoryWithChildren($categoryId);
if (empty($categories)) {
return $categoryIds;
}

$firstCategory = array_shift($categories);
if ($firstCategory['is_anchor'] == 1) {
$anchorCategory[] = (int)$firstCategory['entity_id'];
foreach ($categories as $category) {
if (in_array($category['parent_id'], $categoryIds)
&& in_array($category['parent_id'], $anchorCategory)) {
$categoryIds[] = (int)$category['entity_id'];
if ($category['is_anchor'] == 1) {
$anchorCategory[] = (int)$category['entity_id'];
}
}
}
}

return $categoryIds;
}

/**
* Add category ids to loaded items
*
Expand Down
14 changes: 11 additions & 3 deletions app/code/Magento/CustomerImportExport/Model/Import/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CustomerImportExport\Model\Import;

use Magento\Customer\Api\Data\CustomerInterface;
Expand All @@ -21,7 +23,7 @@
class Customer extends AbstractCustomer
{
/**
* Attribute collection name
* Collection name attribute
*/
const ATTRIBUTE_COLLECTION_NAME = \Magento\Customer\Model\ResourceModel\Attribute\Collection::class;

Expand Down Expand Up @@ -519,8 +521,10 @@ protected function _importData()
);
} elseif ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) {
$processedData = $this->_prepareDataForUpdate($rowData);
// phpcs:disable Magento2.Performance.ForeachArrayMerge
$entitiesToCreate = array_merge($entitiesToCreate, $processedData[self::ENTITIES_TO_CREATE_KEY]);
$entitiesToUpdate = array_merge($entitiesToUpdate, $processedData[self::ENTITIES_TO_UPDATE_KEY]);
// phpcs:enable
foreach ($processedData[self::ATTRIBUTES_TO_SAVE_KEY] as $tableName => $customerAttributes) {
if (!isset($attributesToSave[$tableName])) {
$attributesToSave[$tableName] = [];
Expand Down Expand Up @@ -598,14 +602,18 @@ protected function _validateRowForUpdate(array $rowData, $rowNumber)
$isFieldNotSetAndCustomerDoesNotExist =
!isset($rowData[$attributeCode]) && !$this->_getCustomerId($email, $website);
$isFieldSetAndTrimmedValueIsEmpty
= isset($rowData[$attributeCode]) && '' === trim($rowData[$attributeCode]);
= isset($rowData[$attributeCode]) && '' === trim((string)$rowData[$attributeCode]);

if ($isFieldRequired && ($isFieldNotSetAndCustomerDoesNotExist || $isFieldSetAndTrimmedValueIsEmpty)) {
$this->addRowError(self::ERROR_VALUE_IS_REQUIRED, $rowNumber, $attributeCode);
continue;
}

if (isset($rowData[$attributeCode]) && strlen($rowData[$attributeCode])) {
if (isset($rowData[$attributeCode]) && strlen((string)$rowData[$attributeCode])) {
if ($attributeParams['type'] == 'select') {
continue;
}

$this->isAttributeValid(
$attributeCode,
$attributeParams,
Expand Down
Loading

0 comments on commit 50a085d

Please sign in to comment.