Skip to content

Commit

Permalink
Merge branch 'magento-commerce:2.4-develop' into L3-PR-2024-02-16
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemVasyliev authored Mar 18, 2024
2 parents 5f735a2 + c971859 commit c3615e1
Show file tree
Hide file tree
Showing 18 changed files with 703 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/************************************************************************
*
* Copyright 2024 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
* ************************************************************************
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Plugin;

use GraphQL\Language\AST\Node;
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\Visitor;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GraphQl\Model\Query\ContextInterface;

class ProductAttributeSortInput
{
/**
* Plugin to preserve the original order of sort fields
*
* @param \Magento\Framework\GraphQl\Query\ResolverInterface $subject
* @param \Magento\Framework\GraphQl\Config\Element\Field $field
* @param ContextInterface $context
* @param ResolveInfo $info
* @param array|null $value
* @param array|null $args
* @return array
* @throws \Exception
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeResolve(
\Magento\Framework\GraphQl\Query\ResolverInterface $subject,
\Magento\Framework\GraphQl\Config\Element\Field $field,
ContextInterface $context,
ResolveInfo $info,
array $value = null,
array $args = null
): array {
if (isset($args['sort'])) {
$args['sort'] = $this->getSortFieldsOrder($info, $args['sort']);
}
return [$field, $context, $info, $value, $args];
}

/**
* Get sort fields in the original order
*
* @param ResolveInfo $info
* @param array $sortFields
* @return array
* @throws \Exception
*/
private function getSortFieldsOrder(ResolveInfo $info, array $sortFields)
{
$sortFieldsOriginal = [];
Visitor::visit(
$info->operation,
[
'enter' => [
NodeKind::ARGUMENT => function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
if ($node->name->value === 'sort') {
Visitor::visit(
$node->value,
[
'enter' => [
NodeKind::OBJECT_FIELD =>
function (Node $node) use (&$sortFieldsOriginal, $sortFields) {
if (isset($sortFields[$node->name->value])) {
$sortFieldsOriginal[$node->name->value] =
$sortFields[$node->name->value];
}
}
]
]
);
return Visitor::stop();
}
}
]
]
);
return $sortFieldsOriginal;
}
}
8 changes: 8 additions & 0 deletions app/code/Magento/CatalogGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,12 @@
</argument>
</arguments>
</type>

<type name="Magento\CatalogGraphQl\Model\Resolver\Products">
<plugin name="originalProductSortOrder" type="Magento\CatalogGraphQl\Plugin\ProductAttributeSortInput" />
</type>

<type name="Magento\CatalogGraphQl\Model\Resolver\Category\Products">
<plugin name="originalCategoryProductSortOrder" type="Magento\CatalogGraphQl\Plugin\ProductAttributeSortInput" />
</type>
</config>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\CustomerImportExport\Model\Import;

use Magento\Customer\Model\Config\Share;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Validator\EmailAddress;
use Magento\Framework\Validator\ValidateException;
use Magento\Framework\Validator\ValidatorChain;
Expand Down Expand Up @@ -87,6 +89,11 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit
*/
protected $masterAttributeCode = '_email';

/**
* @var Share
*/
private $configShare;

/**
* @param \Magento\Framework\Stdlib\StringUtils $string
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
Expand All @@ -99,6 +106,7 @@ abstract class AbstractCustomer extends \Magento\ImportExport\Model\Import\Entit
* @param \Magento\Eav\Model\Config $eavConfig
* @param \Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory
* @param array $data
* @param Share|null $configShare
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -112,7 +120,8 @@ public function __construct(
\Magento\ImportExport\Model\Export\Factory $collectionFactory,
\Magento\Eav\Model\Config $eavConfig,
\Magento\CustomerImportExport\Model\ResourceModel\Import\Customer\StorageFactory $storageFactory,
array $data = []
array $data = [],
?Share $configShare = null
) {
$this->_storageFactory = $storageFactory;
parent::__construct(
Expand All @@ -127,7 +136,7 @@ public function __construct(
$eavConfig,
$data
);

$this->configShare = $configShare ?? ObjectManager::getInstance()->get(Share::class);
$this->addMessageTemplate(self::ERROR_WEBSITE_IS_EMPTY, __('Please specify a website.'));
$this->addMessageTemplate(
self::ERROR_EMAIL_IS_EMPTY,
Expand Down Expand Up @@ -174,6 +183,11 @@ protected function _initCustomers(array $data)
protected function _getCustomerId($email, $websiteCode)
{
$email = strtolower(trim($email));

if ($this->configShare->isGlobalScope()) {
return $this->_customerStorage->getCustomerIdByEmail($email);
}

if (isset($this->_websiteCodeToId[$websiteCode])) {
$websiteId = $this->_websiteCodeToId[$websiteCode];
return $this->_customerStorage->getCustomerId($email, $websiteId);
Expand Down
10 changes: 7 additions & 3 deletions app/code/Magento/CustomerImportExport/Model/Import/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\CustomerImportExport\Model\Import;

use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\ResourceModel\Address\Attribute\Source\CountryWithWebsites as CountryWithWebsitesSource;
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
use Magento\Framework\App\ObjectManager;
Expand Down Expand Up @@ -272,7 +273,8 @@ class Address extends AbstractCustomer
* @param array $data
* @param CountryWithWebsitesSource|null $countryWithWebsites
* @param AddressStorage|null $addressStorage
* @param Processor $indexerProcessor
* @param Processor|null $indexerProcessor
* @param Share|null $configShare
*
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -297,7 +299,8 @@ public function __construct(
array $data = [],
?CountryWithWebsitesSource $countryWithWebsites = null,
?AddressStorage $addressStorage = null,
?Processor $indexerProcessor = null
?Processor $indexerProcessor = null,
?Share $configShare = null
) {
$this->_customerFactory = $customerFactory;
$this->_addressFactory = $addressFactory;
Expand Down Expand Up @@ -325,7 +328,8 @@ public function __construct(
$collectionFactory,
$eavConfig,
$storageFactory,
$data
$data,
$configShare
);

$this->_entityTable = isset(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\CustomerImportExport\Model\ResourceModel\Import\Customer;

use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Model\ResourceModel\Customer\Collection as CustomerCollection;
use Magento\Customer\Model\ResourceModel\Customer\CollectionFactory as CustomerCollectionFactory;
use Magento\Framework\DataObject;
Expand All @@ -29,6 +30,11 @@ class Storage
*/
protected $_customerIds = [];

/**
* @var array
*/
private $customerIdsByEmail = [];

/**
* Number of items to fetch from db in one query
*
Expand Down Expand Up @@ -60,19 +66,27 @@ class Storage
*/
private $customerStoreIds = [];

/**
* @var CustomerRepositoryInterface
*/
private $customerRepository;

/**
* @param CustomerCollectionFactory $collectionFactory
* @param CustomerRepositoryInterface $customerRepository
* @param array $data
*/
public function __construct(
CustomerCollectionFactory $collectionFactory,
CustomerRepositoryInterface $customerRepository,
array $data = []
) {
$this->_customerCollection = isset(
$data['customer_collection']
) ? $data['customer_collection'] : $collectionFactory->create();
$this->_pageSize = isset($data['page_size']) ? (int) $data['page_size'] : 0;
$this->customerCollectionFactory = $collectionFactory;
$this->customerRepository = $customerRepository;
}

/**
Expand Down Expand Up @@ -130,7 +144,8 @@ public function addCustomerByArray(array $customer): Storage
/**
* Add customer to array
*
* @deprecated 100.3.0 @see addCustomerByArray
* @deprecated 100.3.0
* @see addCustomerByArray
* @param DataObject $customer
* @return $this
*/
Expand Down Expand Up @@ -164,6 +179,25 @@ public function getCustomerId(string $email, int $websiteId)
return false;
}

/**
* Find customer ID by email.
*
* @param string $email
* @return bool|int
*/
public function getCustomerIdByEmail(string $email)
{
if (!isset($this->customerIdsByEmail[$email])) {
try {
$this->customerIdsByEmail[$email] = $this->customerRepository->get($email)->getId();
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->customerIdsByEmail[$email] = false;
}
}

return $this->customerIdsByEmail[$email];
}

/**
* Get previously loaded customer id.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Magento\Customer\Model\Address\Validator\Postcode;
use Magento\Customer\Model\AddressFactory;
use Magento\Customer\Model\Config\Share;
use Magento\Customer\Model\CustomerFactory;
use Magento\Customer\Model\Indexer\Processor;
use Magento\Customer\Model\ResourceModel\Address\Attribute as AddressAttribute;
Expand Down Expand Up @@ -149,6 +150,16 @@ class AddressTest extends TestCase
*/
private $countryWithWebsites;

/**
* @var Share|MockObject
*/
private $configShare;

/**
* @var Storage
*/
private $customerStorage;

/**
* Init entity adapter model
*/
Expand All @@ -171,6 +182,7 @@ protected function setUp(): void

->method('getAllOptions')
->willReturn([]);
$this->configShare = $this->createMock(Share::class);
$this->_model = $this->_getModelMock();
$this->errorAggregator = $this->createPartialMock(
ProcessingErrorAggregator::class,
Expand Down Expand Up @@ -198,7 +210,7 @@ protected function _getModelDependencies()
->getMock();
$connection = $this->createMock(\stdClass::class);
$attributeCollection = $this->_createAttrCollectionMock();
$customerStorage = $this->_createCustomerStorageMock();
$this->customerStorage = $this->_createCustomerStorageMock();
$customerEntity = $this->_createCustomerEntityMock();
$addressCollection = new Collection(
$this->createMock(EntityFactory::class)
Expand All @@ -222,7 +234,7 @@ protected function _getModelDependencies()
'bunch_size' => 1,
'attribute_collection' => $attributeCollection,
'entity_type_id' => 1,
'customer_storage' => $customerStorage,
'customer_storage' => $this->customerStorage,
'customer_entity' => $customerEntity,
'address_collection' => $addressCollection,
'entity_table' => 'not_used',
Expand Down Expand Up @@ -388,7 +400,8 @@ protected function _getModelMock()
$this->_getModelDependencies(),
$this->countryWithWebsites,
$this->createMock(\Magento\CustomerImportExport\Model\ResourceModel\Import\Address\Storage::class),
$this->createMock(Processor::class)
$this->createMock(Processor::class),
$this->configShare
);

$property = new \ReflectionProperty($modelMock, '_availableBehaviors');
Expand Down Expand Up @@ -447,6 +460,37 @@ public function testValidateRowForUpdate(array $rowData, array $errors, $isValid
{
$this->_model->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE]);

$this->configShare->expects($this->once())
->method('isGlobalScope')
->willReturn(false);

if ($isValid) {
$this->assertTrue($this->_model->validateRow($rowData, 0));
} else {
$this->assertFalse($this->_model->validateRow($rowData, 0));
}
}

/**
* @dataProvider validateRowForUpdateDataProvider
*
* @param array $rowData
* @param array $errors
* @param boolean $isValid
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function testValidateRowForUpdateGlobalCustomer(array $rowData, array $errors, $isValid = false)
{
$this->_model->setParameters(['behavior' => Import::BEHAVIOR_ADD_UPDATE]);

$this->configShare->expects($this->once())
->method('isGlobalScope')
->willReturn(true);

$this->customerStorage->expects($this->once())
->method('getCustomerIdByEmail')
->willReturn(1);

if ($isValid) {
$this->assertTrue($this->_model->validateRow($rowData, 0));
} else {
Expand Down
Loading

0 comments on commit c3615e1

Please sign in to comment.