Skip to content

Commit 4e470c1

Browse files
authored
Merge pull request #4644 from magento-engcom/graphql-develop-prs
[Magento Community Engineering] Community Contributions - GraphQL
2 parents 0053757 + 46cd735 commit 4e470c1

File tree

12 files changed

+385
-45
lines changed

12 files changed

+385
-45
lines changed

app/code/Magento/Customer/Block/Form/Register.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
namespace Magento\Customer\Block\Form;
77

88
use Magento\Customer\Model\AccountManagement;
9-
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
9+
use Magento\Framework\App\ObjectManager;
10+
use Magento\Newsletter\Model\Config;
1011

1112
/**
1213
* Customer register form block
@@ -32,6 +33,11 @@ class Register extends \Magento\Directory\Block\Data
3233
*/
3334
protected $_customerUrl;
3435

36+
/**
37+
* @var Config
38+
*/
39+
private $newsLetterConfig;
40+
3541
/**
3642
* Constructor
3743
*
@@ -45,6 +51,7 @@ class Register extends \Magento\Directory\Block\Data
4551
* @param \Magento\Customer\Model\Session $customerSession
4652
* @param \Magento\Customer\Model\Url $customerUrl
4753
* @param array $data
54+
* @param Config $newsLetterConfig
4855
*
4956
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5057
*/
@@ -58,11 +65,13 @@ public function __construct(
5865
\Magento\Framework\Module\ModuleManagerInterface $moduleManager,
5966
\Magento\Customer\Model\Session $customerSession,
6067
\Magento\Customer\Model\Url $customerUrl,
61-
array $data = []
68+
array $data = [],
69+
Config $newsLetterConfig = null
6270
) {
6371
$this->_customerUrl = $customerUrl;
6472
$this->_moduleManager = $moduleManager;
6573
$this->_customerSession = $customerSession;
74+
$this->newsLetterConfig = $newsLetterConfig ?: ObjectManager::getInstance()->get(Config::class);
6675
parent::__construct(
6776
$context,
6877
$directoryHelper,
@@ -170,7 +179,7 @@ public function getRegion()
170179
public function isNewsletterEnabled()
171180
{
172181
return $this->_moduleManager->isOutputEnabled('Magento_Newsletter')
173-
&& $this->getConfig(PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE);
182+
&& $this->newsLetterConfig->isActive();
174183
}
175184

176185
/**

app/code/Magento/Customer/Test/Unit/Block/Form/RegisterTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
use Magento\Customer\Block\Form\Register;
99
use Magento\Customer\Model\AccountManagement;
10-
use Magento\Newsletter\Observer\PredispatchNewsletterObserver;
1110

1211
/**
1312
* Test class for \Magento\Customer\Block\Form\Register.
@@ -49,6 +48,9 @@ class RegisterTest extends \PHPUnit\Framework\TestCase
4948
/** @var Register */
5049
private $_block;
5150

51+
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Newsletter\Model\Config */
52+
private $newsletterConfig;
53+
5254
protected function setUp()
5355
{
5456
$this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
@@ -59,6 +61,7 @@ protected function setUp()
5961
\Magento\Customer\Model\Session::class,
6062
['getCustomerFormData']
6163
);
64+
$this->newsletterConfig = $this->createMock(\Magento\Newsletter\Model\Config::class);
6265
$context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
6366
$context->expects($this->any())->method('getScopeConfig')->will($this->returnValue($this->_scopeConfig));
6467

@@ -71,7 +74,9 @@ protected function setUp()
7174
$this->createMock(\Magento\Directory\Model\ResourceModel\Country\CollectionFactory::class),
7275
$this->_moduleManager,
7376
$this->_customerSession,
74-
$this->_customerUrl
77+
$this->_customerUrl,
78+
[],
79+
$this->newsletterConfig
7580
);
7681
}
7782

@@ -293,12 +298,10 @@ public function testIsNewsletterEnabled($isNewsletterEnabled, $isNewsletterActiv
293298
$this->returnValue($isNewsletterEnabled)
294299
);
295300

296-
$this->_scopeConfig->expects(
301+
$this->newsletterConfig->expects(
297302
$this->any()
298303
)->method(
299-
'getValue'
300-
)->with(
301-
PredispatchNewsletterObserver::XML_PATH_NEWSLETTER_ACTIVE
304+
'isActive'
302305
)->will(
303306
$this->returnValue($isNewsletterActive)
304307
);

app/code/Magento/CustomerGraphQl/Model/Customer/CreateCustomerAccount.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\Api\DataObjectHelper;
1414
use Magento\Framework\Exception\LocalizedException;
1515
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
16+
use Magento\Framework\Reflection\DataObjectProcessor;
1617
use Magento\Store\Api\Data\StoreInterface;
1718

1819
/**
@@ -41,21 +42,39 @@ class CreateCustomerAccount
4142
private $changeSubscriptionStatus;
4243

4344
/**
45+
* @var ValidateCustomerData
46+
*/
47+
private $validateCustomerData;
48+
49+
/**
50+
* @var DataObjectProcessor
51+
*/
52+
private $dataObjectProcessor;
53+
54+
/**
55+
* CreateCustomerAccount constructor.
56+
*
4457
* @param DataObjectHelper $dataObjectHelper
4558
* @param CustomerInterfaceFactory $customerFactory
4659
* @param AccountManagementInterface $accountManagement
4760
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
61+
* @param DataObjectProcessor $dataObjectProcessor
62+
* @param ValidateCustomerData $validateCustomerData
4863
*/
4964
public function __construct(
5065
DataObjectHelper $dataObjectHelper,
5166
CustomerInterfaceFactory $customerFactory,
5267
AccountManagementInterface $accountManagement,
53-
ChangeSubscriptionStatus $changeSubscriptionStatus
68+
ChangeSubscriptionStatus $changeSubscriptionStatus,
69+
DataObjectProcessor $dataObjectProcessor,
70+
ValidateCustomerData $validateCustomerData
5471
) {
5572
$this->dataObjectHelper = $dataObjectHelper;
5673
$this->customerFactory = $customerFactory;
5774
$this->accountManagement = $accountManagement;
5875
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
76+
$this->validateCustomerData = $validateCustomerData;
77+
$this->dataObjectProcessor = $dataObjectProcessor;
5978
}
6079

6180
/**
@@ -91,6 +110,15 @@ public function execute(array $data, StoreInterface $store): CustomerInterface
91110
private function createAccount(array $data, StoreInterface $store): CustomerInterface
92111
{
93112
$customerDataObject = $this->customerFactory->create();
113+
/**
114+
* Add required attributes for customer entity
115+
*/
116+
$requiredDataAttributes = $this->dataObjectProcessor->buildOutputDataArray(
117+
$customerDataObject,
118+
CustomerInterface::class
119+
);
120+
$data = array_merge($requiredDataAttributes, $data);
121+
$this->validateCustomerData->execute($data);
94122
$this->dataObjectHelper->populateWithArray(
95123
$customerDataObject,
96124
$data,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CustomerGraphQl\Model\Customer;
9+
10+
use Magento\Customer\Api\CustomerMetadataManagementInterface;
11+
use Magento\Customer\Api\Data\CustomerInterface;
12+
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
13+
use Magento\Eav\Model\AttributeRepository;
14+
use Magento\Eav\Model\Entity\Attribute\AbstractAttribute;
15+
use Magento\Framework\Api\SearchCriteriaBuilder;
16+
use Magento\Framework\Exception\InputException;
17+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
18+
use Magento\Framework\Reflection\DataObjectProcessor;
19+
20+
/**
21+
* Get allowed address attributes
22+
*/
23+
class GetAllowedCustomerAttributes
24+
{
25+
/**
26+
* @var AttributeRepository
27+
*/
28+
private $attributeRepository;
29+
30+
/**
31+
* @var CustomerInterfaceFactory\
32+
*/
33+
private $customerDataFactory;
34+
35+
/**
36+
* @var DataObjectProcessor
37+
*/
38+
private $dataObjectProcessor;
39+
40+
/**
41+
* @var SearchCriteriaBuilder
42+
*/
43+
private $searchCriteriaBuilder;
44+
45+
/**
46+
* GetAllowedCustomerAttributes constructor.
47+
*
48+
* @param AttributeRepository $attributeRepository
49+
* @param CustomerInterfaceFactory $customerDataFactory
50+
* @param DataObjectProcessor $dataObjectProcessor
51+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
52+
*/
53+
public function __construct(
54+
AttributeRepository $attributeRepository,
55+
CustomerInterfaceFactory $customerDataFactory,
56+
DataObjectProcessor $dataObjectProcessor,
57+
SearchCriteriaBuilder $searchCriteriaBuilder
58+
) {
59+
$this->attributeRepository = $attributeRepository;
60+
$this->customerDataFactory = $customerDataFactory;
61+
$this->dataObjectProcessor = $dataObjectProcessor;
62+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
63+
}
64+
65+
/**
66+
* Get allowed customer attributes
67+
*
68+
* @param array $attributeKeys
69+
*
70+
* @throws GraphQlInputException
71+
*
72+
* @return AbstractAttribute[]
73+
*/
74+
public function execute($attributeKeys): array
75+
{
76+
$this->searchCriteriaBuilder->addFilter('attribute_code', $attributeKeys, 'in');
77+
$searchCriteria = $this->searchCriteriaBuilder->create();
78+
try {
79+
$attributesSearchResult = $this->attributeRepository->getList(
80+
CustomerMetadataManagementInterface::ENTITY_TYPE_CUSTOMER,
81+
$searchCriteria
82+
);
83+
} catch (InputException $exception) {
84+
throw new GraphQlInputException(__($exception->getMessage()));
85+
}
86+
87+
/** @var AbstractAttribute[] $attributes */
88+
$attributes = $attributesSearchResult->getItems();
89+
90+
foreach ($attributes as $index => $attribute) {
91+
if (false === $attribute->getIsVisibleOnFront()) {
92+
unset($attributes[$index]);
93+
}
94+
}
95+
96+
return $attributes;
97+
}
98+
}

app/code/Magento/CustomerGraphQl/Model/Customer/UpdateCustomerAccount.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
namespace Magento\CustomerGraphQl\Model\Customer;
99

10+
use Magento\Customer\Api\Data\CustomerInterface;
11+
use Magento\Framework\Exception\NoSuchEntityException;
1012
use Magento\Framework\GraphQl\Exception\GraphQlAlreadyExistsException;
1113
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
1214
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
13-
use Magento\Customer\Api\Data\CustomerInterface;
15+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1416
use Magento\Framework\Api\DataObjectHelper;
1517
use Magento\Store\Api\Data\StoreInterface;
1618

@@ -41,6 +43,11 @@ class UpdateCustomerAccount
4143
*/
4244
private $changeSubscriptionStatus;
4345

46+
/**
47+
* @var ValidateCustomerData
48+
*/
49+
private $validateCustomerData;
50+
4451
/**
4552
* @var array
4653
*/
@@ -51,24 +58,27 @@ class UpdateCustomerAccount
5158
* @param CheckCustomerPassword $checkCustomerPassword
5259
* @param DataObjectHelper $dataObjectHelper
5360
* @param ChangeSubscriptionStatus $changeSubscriptionStatus
61+
* @param ValidateCustomerData $validateCustomerData
5462
* @param array $restrictedKeys
5563
*/
5664
public function __construct(
5765
SaveCustomer $saveCustomer,
5866
CheckCustomerPassword $checkCustomerPassword,
5967
DataObjectHelper $dataObjectHelper,
6068
ChangeSubscriptionStatus $changeSubscriptionStatus,
69+
ValidateCustomerData $validateCustomerData,
6170
array $restrictedKeys = []
6271
) {
6372
$this->saveCustomer = $saveCustomer;
6473
$this->checkCustomerPassword = $checkCustomerPassword;
6574
$this->dataObjectHelper = $dataObjectHelper;
6675
$this->restrictedKeys = $restrictedKeys;
6776
$this->changeSubscriptionStatus = $changeSubscriptionStatus;
77+
$this->validateCustomerData = $validateCustomerData;
6878
}
6979

7080
/**
71-
* Update customer account data
81+
* Update customer account
7282
*
7383
* @param CustomerInterface $customer
7484
* @param array $data
@@ -77,7 +87,7 @@ public function __construct(
7787
* @throws GraphQlAlreadyExistsException
7888
* @throws GraphQlAuthenticationException
7989
* @throws GraphQlInputException
80-
* @throws \Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException
90+
* @throws GraphQlNoSuchEntityException
8191
*/
8292
public function execute(CustomerInterface $customer, array $data, StoreInterface $store): void
8393
{
@@ -89,11 +99,15 @@ public function execute(CustomerInterface $customer, array $data, StoreInterface
8999
$this->checkCustomerPassword->execute($data['password'], (int)$customer->getId());
90100
$customer->setEmail($data['email']);
91101
}
92-
102+
$this->validateCustomerData->execute($data);
93103
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));
94104
$this->dataObjectHelper->populateWithArray($customer, $filteredData, CustomerInterface::class);
95105

96-
$customer->setStoreId($store->getId());
106+
try {
107+
$customer->setStoreId($store->getId());
108+
} catch (NoSuchEntityException $exception) {
109+
throw new GraphQlNoSuchEntityException(__($exception->getMessage()), $exception);
110+
}
97111

98112
$this->saveCustomer->execute($customer);
99113

0 commit comments

Comments
 (0)