-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6263 from magento-tsg/2.4-develop-pr98
[Arrows] Fixes for 2.4 (pr98) (2.4-develop)
- Loading branch information
Showing
9 changed files
with
383 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
app/code/Magento/Catalog/Model/CategoryRepository/PopulateWithValues.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Magento\Catalog\Model\CategoryRepository; | ||
|
||
use Magento\Catalog\Api\Data\CategoryInterface; | ||
use Magento\Catalog\Model\Attribute\ScopeOverriddenValue; | ||
use Magento\Catalog\Model\Category; | ||
use Magento\Catalog\Api\CategoryAttributeRepositoryInterface as AttributeRepository; | ||
use Magento\Eav\Api\Data\AttributeInterface; | ||
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface; | ||
use Magento\Framework\Api\FilterBuilder; | ||
use Magento\Framework\Api\SearchCriteriaBuilder; | ||
use Magento\Store\Model\Store; | ||
|
||
/** | ||
* Add data to category entity and populate with default values | ||
*/ | ||
class PopulateWithValues | ||
{ | ||
/** | ||
* @var ScopeOverriddenValue | ||
*/ | ||
private $scopeOverriddenValue; | ||
|
||
/** | ||
* @var AttributeRepository | ||
*/ | ||
private $attributeRepository; | ||
|
||
/** | ||
* @var SearchCriteriaBuilder | ||
*/ | ||
private $searchCriteriaBuilder; | ||
|
||
/** | ||
* @var FilterBuilder | ||
*/ | ||
private $filterBuilder; | ||
|
||
/** | ||
* @var AttributeInterface[] | ||
*/ | ||
private $attributes; | ||
|
||
/** | ||
* @param ScopeOverriddenValue $scopeOverriddenValue | ||
* @param AttributeRepository $attributeRepository | ||
* @param SearchCriteriaBuilder $searchCriteriaBuilder | ||
* @param FilterBuilder $filterBuilder | ||
*/ | ||
public function __construct( | ||
ScopeOverriddenValue $scopeOverriddenValue, | ||
AttributeRepository $attributeRepository, | ||
SearchCriteriaBuilder $searchCriteriaBuilder, | ||
FilterBuilder $filterBuilder | ||
) { | ||
$this->scopeOverriddenValue = $scopeOverriddenValue; | ||
$this->attributeRepository = $attributeRepository; | ||
$this->searchCriteriaBuilder = $searchCriteriaBuilder; | ||
$this->filterBuilder = $filterBuilder; | ||
} | ||
|
||
/** | ||
* Set null to entity default values | ||
* | ||
* @param CategoryInterface $category | ||
* @param array $existingData | ||
* @return void | ||
*/ | ||
public function execute(CategoryInterface $category, array $existingData): void | ||
{ | ||
$storeId = $existingData['store_id'] ?? Store::DEFAULT_STORE_ID; | ||
if ((int)$storeId !== Store::DEFAULT_STORE_ID) { | ||
$overriddenValues = array_filter( | ||
$category->getData(), | ||
function ($key) use ($category, $storeId) { | ||
/** @var Category $category */ | ||
return $this->scopeOverriddenValue->containsValue( | ||
CategoryInterface::class, | ||
$category, | ||
$key, | ||
$storeId | ||
); | ||
}, | ||
ARRAY_FILTER_USE_KEY | ||
); | ||
$defaultValues = array_diff_key($category->getData(), $overriddenValues); | ||
array_walk( | ||
$defaultValues, | ||
function (&$value, $key) { | ||
$attributes = $this->getAttributes(); | ||
if (isset($attributes[$key]) && !$attributes[$key]->isStatic()) { | ||
$value = null; | ||
} | ||
} | ||
); | ||
$category->addData($defaultValues); | ||
} | ||
|
||
$category->addData($existingData); | ||
$useDefaultAttributes = array_filter( | ||
$category->getData(), | ||
function ($attributeValue) { | ||
return null === $attributeValue; | ||
} | ||
); | ||
$category->setData( | ||
'use_default', | ||
array_map( | ||
function () { | ||
return true; | ||
}, | ||
$useDefaultAttributes | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns entity attributes. | ||
* | ||
* @return AttributeInterface[] | ||
*/ | ||
private function getAttributes(): array | ||
{ | ||
if ($this->attributes) { | ||
return $this->attributes; | ||
} | ||
|
||
$searchResult = $this->attributeRepository->getList( | ||
$this->searchCriteriaBuilder->addFilters( | ||
[ | ||
$this->filterBuilder | ||
->setField('is_global') | ||
->setConditionType('in') | ||
->setValue([ScopedAttributeInterface::SCOPE_STORE, ScopedAttributeInterface::SCOPE_WEBSITE]) | ||
->create() | ||
] | ||
)->create() | ||
); | ||
|
||
$this->attributes = []; | ||
foreach ($searchResult->getItems() as $attribute) { | ||
$this->attributes[$attribute->getAttributeCode()] = $attribute; | ||
} | ||
|
||
return $this->attributes; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/code/Magento/Catalog/Test/Mftf/Data/NonexistentProductData.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
|
||
<entities xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:mftf:DataGenerator/etc/dataProfileSchema.xsd"> | ||
<entity name="NonexistentProduct" type="product"> | ||
<data key="sku">NonexistentProductSku</data> | ||
<data key="qty">1</data> | ||
</entity> | ||
<entity name="SecondNonexistentProduct" type="product"> | ||
<data key="sku">SecondNonexistentProductSku</data> | ||
<data key="qty">1</data> | ||
</entity> | ||
</entities> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.