Skip to content

Commit

Permalink
Merge pull request #7900 from magento-l3/PR_21_SEP_2022
Browse files Browse the repository at this point in the history
[Chaika] BugFix delivery
  • Loading branch information
ishakhsuvarov authored Oct 14, 2022
2 parents 735a01b + 5935803 commit 62aafe2
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ public function execute()
return $this->returnResult('catalog/*/', [], ['error' => false]);
} catch (\Exception $e) {
$this->messageManager->addErrorMessage($e->getMessage());
if ($attributeId === null) {
unset($data['frontend_input']);
}
$this->_session->setAttributeData($data);
return $this->returnResult(
'catalog/*/edit',
Expand Down
21 changes: 17 additions & 4 deletions app/code/Magento/CatalogImportExport/Model/Import/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Magento\CatalogImportExport\Model\Import\Product\StatusProcessor;
use Magento\CatalogImportExport\Model\Import\Product\StockProcessor;
use Magento\CatalogImportExport\Model\StockItemImporterInterface;
use Magento\CatalogImportExport\Model\StockItemProcessorInterface;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -229,7 +230,6 @@ class Product extends AbstractEntity
* @deprecated 101.1.0 use DI for LinkProcessor class if you want to add additional types
*
* @see Magento_CatalogImportExport::etc/di.xml
*
* @var array
*/
protected $_linkNameToId = [
Expand Down Expand Up @@ -615,8 +615,8 @@ class Product extends AbstractEntity
/**
* @var array
* @deprecated 100.0.3
* @since 100.0.3
*
* @since 100.0.3
* @see we don't recommend this approach anymore
*/
protected $productUrlKeys = [];
Expand Down Expand Up @@ -756,6 +756,11 @@ class Product extends AbstractEntity
*/
private $linkProcessor;

/**
* @var StockItemProcessorInterface
*/
private $stockItemProcessor;

/**
* @param \Magento\Framework\Json\Helper\Data $jsonHelper
* @param \Magento\ImportExport\Helper\Data $importExportData
Expand Down Expand Up @@ -805,6 +810,7 @@ class Product extends AbstractEntity
* @param StockProcessor|null $stockProcessor
* @param LinkProcessor|null $linkProcessor
* @param File|null $fileDriver
* @param StockItemProcessorInterface|null $stockItemProcessor
* @throws LocalizedException
* @throws \Magento\Framework\Exception\FileSystemException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand Down Expand Up @@ -859,7 +865,8 @@ public function __construct(
StatusProcessor $statusProcessor = null,
StockProcessor $stockProcessor = null,
LinkProcessor $linkProcessor = null,
?File $fileDriver = null
?File $fileDriver = null,
?StockItemProcessorInterface $stockItemProcessor = null
) {
$this->_eventManager = $eventManager;
$this->stockRegistry = $stockRegistry;
Expand Down Expand Up @@ -923,6 +930,8 @@ public function __construct(
$this->dateTimeFactory = $dateTimeFactory ?? ObjectManager::getInstance()->get(DateTimeFactory::class);
$this->productRepository = $productRepository ?? ObjectManager::getInstance()
->get(ProductRepositoryInterface::class);
$this->stockItemProcessor = $stockItemProcessor ?? ObjectManager::getInstance()
->get(StockItemProcessorInterface::class);
}

/**
Expand Down Expand Up @@ -1286,6 +1295,7 @@ protected function _prepareRowForDb(array $rowData)
*
* @deprecated 101.1.0 use linkProcessor Directly
* @see linkProcessor
*
* @return $this
*/
protected function _saveLinks()
Expand Down Expand Up @@ -2328,6 +2338,7 @@ protected function _saveStockItem()
{
while ($bunch = $this->_dataSourceModel->getNextBunch()) {
$stockData = [];
$importedData = [];
$productIdsToReindex = [];
$stockChangedProductIds = [];
// Format bunch to stock data rows
Expand All @@ -2353,12 +2364,13 @@ protected function _saveStockItem()

if (!isset($stockData[$sku])) {
$stockData[$sku] = $row;
$importedData[$sku] = $rowData;
}
}

// Insert rows
if (!empty($stockData)) {
$this->stockItemImporter->import($stockData);
$this->stockItemProcessor->process($stockData, $importedData);
}

$this->reindexStockStatus($stockChangedProductIds);
Expand Down Expand Up @@ -2512,6 +2524,7 @@ public function getRowScope(array $rowData)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @throws \Zend_Validate_Exception
*/
public function validateRow(array $rowData, $rowNum)
{
Expand Down
33 changes: 33 additions & 0 deletions app/code/Magento/CatalogImportExport/Model/StockItemProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model;

class StockItemProcessor implements StockItemProcessorInterface
{
/**
* @var StockItemImporterInterface
*/
private $stockItemImporter;

/**
* @param StockItemImporterInterface $stockItemImporter
*/
public function __construct(
StockItemImporterInterface $stockItemImporter
) {
$this->stockItemImporter = $stockItemImporter;
}

/**
* @inheritdoc
*/
public function process(array $stockData, array $importedData): void
{
$this->stockItemImporter->import($stockData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogImportExport\Model;

use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Validation\ValidationException;

interface StockItemProcessorInterface
{
/**
* Handle Import of Stock Item Data
*
* @param array $stockData
* @param array $importedData
* @return void
* @throws CouldNotSaveException
* @throws InputException
* @throws ValidationException
*/
public function process(array $stockData, array $importedData): void;
}
1 change: 1 addition & 0 deletions app/code/Magento/CatalogImportExport/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\CatalogImportExport\Model\Export\RowCustomizerInterface" type="Magento\CatalogImportExport\Model\Export\RowCustomizer\Composite" />
<preference for="Magento\CatalogImportExport\Model\StockItemImporterInterface" type="Magento\CatalogImportExport\Model\StockItemImporter" />
<preference for="Magento\CatalogImportExport\Model\StockItemProcessorInterface" type="Magento\CatalogImportExport\Model\StockItemProcessor" />
<preference for="Magento\CatalogImportExport\Model\Export\ProductFilterInterface" type="Magento\CatalogImportExport\Model\Export\ProductFilters" />
<type name="Magento\ImportExport\Model\Import">
<plugin name="catalogProductFlatIndexerImport" type="Magento\CatalogImportExport\Model\Indexer\Product\Flat\Plugin\Import" />
Expand Down
25 changes: 20 additions & 5 deletions app/code/Magento/Eav/Model/Entity/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Eav\Model\Entity;

use Magento\Eav\Model\ReservedAttributeCheckerInterface;
use Magento\Eav\Model\Validator\Attribute\Code as AttributeCodeValidator;
use Magento\Framework\Api\AttributeValueFactory;
use Magento\Framework\App\ObjectManager;
Expand All @@ -29,17 +30,17 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
* The value is defined as 60 because in the flat mode attribute code will be transformed into column name.
* MySQL allows only 64 symbols in column name.
*/
const ATTRIBUTE_CODE_MAX_LENGTH = 60;
public const ATTRIBUTE_CODE_MAX_LENGTH = 60;

/**
* Min accepted length of an attribute code.
*/
const ATTRIBUTE_CODE_MIN_LENGTH = 1;
public const ATTRIBUTE_CODE_MIN_LENGTH = 1;

/**
* Tag to use for attributes caching.
*/
const CACHE_TAG = 'EAV_ATTRIBUTE';
public const CACHE_TAG = 'EAV_ATTRIBUTE';

/**
* Prefix of model events names
Expand Down Expand Up @@ -69,6 +70,9 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im

/**
* @var \Magento\Catalog\Model\Product\ReservedAttributeList
*
* @deprecated Incorrect direct dependency on Product attribute.
* @see \Magento\Eav\Model\Entity\Attribute::$reservedAttributeChecker
*/
protected $reservedAttributeList;

Expand All @@ -87,6 +91,11 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
*/
private $attributeCodeValidator;

/**
* @var ReservedAttributeCheckerInterface|null
*/
private $reservedAttributeChecker;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand All @@ -108,6 +117,7 @@ class Attribute extends \Magento\Eav\Model\Entity\Attribute\AbstractAttribute im
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param AttributeCodeValidator|null $attributeCodeValidator
* @param ReservedAttributeCheckerInterface|null $reservedAttributeChecker
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @codeCoverageIgnore
*/
Expand All @@ -131,7 +141,8 @@ public function __construct(
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
AttributeCodeValidator $attributeCodeValidator = null
AttributeCodeValidator $attributeCodeValidator = null,
?ReservedAttributeCheckerInterface $reservedAttributeChecker = null
) {
parent::__construct(
$context,
Expand All @@ -157,6 +168,9 @@ public function __construct(
$this->attributeCodeValidator = $attributeCodeValidator ?: ObjectManager::getInstance()->get(
AttributeCodeValidator::class
);
$this->reservedAttributeChecker = $reservedAttributeChecker ?: ObjectManager::getInstance()->get(
ReservedAttributeCheckerInterface::class
);
}

/**
Expand Down Expand Up @@ -251,7 +265,7 @@ public function beforeSave()
}

// prevent overriding product data
if (isset($this->_data['attribute_code']) && $this->reservedAttributeList->isReservedAttribute($this)) {
if (isset($this->_data['attribute_code']) && $this->reservedAttributeChecker->isReservedAttribute($this)) {
throw new LocalizedException(
__(
'The attribute code \'%1\' is reserved by system. Please try another attribute code',
Expand Down Expand Up @@ -535,6 +549,7 @@ public function __wakeup()
$this->_localeDate = $objectManager->get(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
$this->_localeResolver = $objectManager->get(\Magento\Framework\Locale\ResolverInterface::class);
$this->reservedAttributeList = $objectManager->get(\Magento\Catalog\Model\Product\ReservedAttributeList::class);
$this->reservedAttributeChecker = $objectManager->get(ReservedAttributeCheckerInterface::class);
$this->dateTimeFormatter = $objectManager->get(DateTimeFormatterInterface::class);
}

Expand Down
22 changes: 20 additions & 2 deletions app/code/Magento/Eav/Model/ReservedAttributeChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
class ReservedAttributeChecker implements ReservedAttributeCheckerInterface
{
/**
* @var ReservedAttributeCheckerInterface[][]
* @var ReservedAttributeCheckerInterface[]
*/
private $validators;

Expand All @@ -37,7 +37,8 @@ public function __construct(
public function isReservedAttribute(AbstractAttribute $attribute): bool
{
$isReserved = false;
$validators = $this->validators[$attribute->getEntityType()->getEntityTypeCode()] ?? [];
$entityTypeCode = $this->getAttributeEntityTypeCode($attribute);
$validators = $this->validators[$entityTypeCode] ?? [];
foreach ($validators as $validator) {
$isReserved = $validator->isReservedAttribute($attribute);
if ($isReserved === true) {
Expand All @@ -47,4 +48,21 @@ public function isReservedAttribute(AbstractAttribute $attribute): bool

return $isReserved;
}

/**
* Returns attribute entity type code.
*
* @param AbstractAttribute $attribute
* @return string|null
*/
private function getAttributeEntityTypeCode(AbstractAttribute $attribute): ?string
{
try {
$result = $attribute->getEntityType()->getEntityTypeCode();
} catch (LocalizedException $e) {
$result = null;
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<group value="Theme"/>
</annotations>
<before>
<magentoCLI command="config:set {{EnableFlatRateConfigData.path}} {{EnableFlatRateConfigData.value}}" stepKey="enableFlatRate"/>
<createData entity="SimpleProduct2" stepKey="simpleProduct"/>
<createData entity="SalesRuleSpecificCouponAndByPercent" stepKey="createSalesRule"/>
<createData entity="SimpleSalesRuleCoupon" stepKey="createCouponForCartPriceRule">
Expand All @@ -28,6 +29,7 @@
<after>
<deleteData createDataKey="simpleProduct" stepKey="deleteProduct"/>
<deleteData createDataKey="createSalesRule" stepKey="deleteSalesRule"/>
<magentoCLI command="config:set {{DisableFlatRateConfigData.path}} {{DisableFlatRateConfigData.value}}" stepKey="disableFlatRate"/>
</after>

<actionGroup ref="AssertProductNameAndSkuInStorefrontProductPageByCustomAttributeUrlKeyActionGroup" stepKey="openProductPageAndVerifyProduct">
Expand Down

0 comments on commit 62aafe2

Please sign in to comment.