Skip to content

Commit 488c103

Browse files
Merge pull request #8851 from adobe-commerce-tier-4/Tier4-Kings-PR-03-21-2024
[Support Tier-4-Kings glo28218] 03.21.2024 Regular delivery of bugfixes and improvements
2 parents f2691bf + 975f233 commit 488c103

File tree

9 files changed

+259
-33
lines changed

9 files changed

+259
-33
lines changed

app/code/Magento/Customer/Controller/Adminhtml/Index/Save.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Magento\Customer\Model\EmailNotificationInterface;
2828
use Magento\Customer\Model\Metadata\Form;
2929
use Magento\Customer\Model\Metadata\FormFactory;
30+
use Magento\Customer\Model\SetCustomerStore;
3031
use Magento\Framework\Api\DataObjectHelper;
3132
use Magento\Framework\Api\ExtensibleDataObjectConverter;
3233
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -75,6 +76,11 @@ class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpP
7576
*/
7677
private $storeManager;
7778

79+
/**
80+
* @var SetCustomerStore|null
81+
*/
82+
private $customerStore;
83+
7884
/**
7985
* Constructor
8086
*
@@ -106,7 +112,9 @@ class Save extends \Magento\Customer\Controller\Adminhtml\Index implements HttpP
106112
* @param SubscriptionManagerInterface $subscriptionManager
107113
* @param AddressRegistry|null $addressRegistry
108114
* @param StoreManagerInterface|null $storeManager
115+
* @param SetCustomerStore|null $customerStore
109116
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
117+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
110118
*/
111119
public function __construct(
112120
Context $context,
@@ -136,7 +144,8 @@ public function __construct(
136144
JsonFactory $resultJsonFactory,
137145
SubscriptionManagerInterface $subscriptionManager,
138146
AddressRegistry $addressRegistry = null,
139-
?StoreManagerInterface $storeManager = null
147+
?StoreManagerInterface $storeManager = null,
148+
?SetCustomerStore $customerStore = null
140149
) {
141150
parent::__construct(
142151
$context,
@@ -168,6 +177,7 @@ public function __construct(
168177
$this->subscriptionManager = $subscriptionManager;
169178
$this->addressRegistry = $addressRegistry ?: ObjectManager::getInstance()->get(AddressRegistry::class);
170179
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
180+
$this->customerStore = $customerStore ?? ObjectManager::getInstance()->get(SetCustomerStore::class);
171181
}
172182

173183
/**
@@ -335,6 +345,10 @@ public function execute()
335345

336346
if ($this->getRequest()->getPostValue()) {
337347
try {
348+
$this->customerStore->setStore(
349+
$this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
350+
);
351+
338352
// optional fields might be set in request for future processing by observers in other modules
339353
$customerData = $this->_extractCustomerData();
340354

@@ -370,13 +384,6 @@ public function execute()
370384
}
371385
}
372386

373-
$storeId = $customer->getStoreId();
374-
if (empty($storeId)) {
375-
$website = $this->storeManager->getWebsite($customer->getWebsiteId());
376-
$storeId = current($website->getStoreIds());
377-
}
378-
$this->storeManager->setCurrentStore($storeId);
379-
380387
// Save customer
381388
if ($customerId) {
382389
$this->_customerRepository->save($customer);

app/code/Magento/Customer/Controller/Adminhtml/Index/Validate.php

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

88
use Magento\Customer\Api\AccountManagementInterface;
99
use Magento\Customer\Api\AddressRepositoryInterface;
10+
use Magento\Customer\Api\CustomerMetadataInterface;
1011
use Magento\Customer\Api\CustomerRepositoryInterface;
1112
use Magento\Customer\Api\Data\AddressInterfaceFactory;
1213
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1314
use Magento\Customer\Model\Address\Mapper;
15+
use Magento\Customer\Model\SetCustomerStore;
1416
use Magento\Framework\Api\DataObjectHelper;
1517
use Magento\Framework\App\Action\HttpGetActionInterface;
1618
use Magento\Framework\App\Action\HttpPostActionInterface as HttpPostActionInterface;
@@ -33,6 +35,11 @@ class Validate extends CustomerAction implements HttpPostActionInterface, HttpGe
3335
*/
3436
private $storeManager;
3537

38+
/**
39+
* @var SetCustomerStore|null
40+
*/
41+
private $customerStore;
42+
3643
/**
3744
* @param \Magento\Backend\App\Action\Context $context
3845
* @param \Magento\Framework\Registry $coreRegistry
@@ -60,7 +67,9 @@ class Validate extends CustomerAction implements HttpPostActionInterface, HttpGe
6067
* @param \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory
6168
* @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory
6269
* @param StoreManagerInterface|null $storeManager
70+
* @param SetCustomerStore|null $customerStore
6371
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
72+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
6473
*/
6574
public function __construct(
6675
\Magento\Backend\App\Action\Context $context,
@@ -88,7 +97,8 @@ public function __construct(
8897
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
8998
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
9099
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
91-
?StoreManagerInterface $storeManager = null
100+
?StoreManagerInterface $storeManager = null,
101+
?SetCustomerStore $customerStore = null
92102
) {
93103
parent::__construct(
94104
$context,
@@ -117,8 +127,8 @@ public function __construct(
117127
$resultForwardFactory,
118128
$resultJsonFactory
119129
);
120-
121130
$this->storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
131+
$this->customerStore = $customerStore ?? ObjectManager::getInstance()->get(SetCustomerStore::class);
122132
}
123133

124134
/**
@@ -145,6 +155,9 @@ protected function _validateCustomer($response)
145155
);
146156
$customerForm->setInvisibleIgnored(true);
147157

158+
$this->customerStore->setStore(
159+
$this->getRequest()->getParam(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER)
160+
);
148161
$data = $customerForm->extractData($this->getRequest(), 'customer');
149162

150163
if ($customer->getWebsiteId()) {
@@ -161,11 +174,6 @@ protected function _validateCustomer($response)
161174
$entity_id = $submittedData['entity_id'];
162175
$customer->setId($entity_id);
163176
}
164-
if (isset($data['website_id']) && is_numeric($data['website_id'])) {
165-
$website = $this->storeManager->getWebsite($data['website_id']);
166-
$storeId = current($website->getStoreIds());
167-
$this->storeManager->setCurrentStore($storeId);
168-
}
169177
$errors = $this->customerAccountManagement->validate($customer)->getMessages();
170178
} catch (\Magento\Framework\Validator\Exception $exception) {
171179
/* @var $error Error */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Customer\Model;
20+
21+
use Magento\Customer\Api\Data\CustomerInterface;
22+
use Magento\Framework\Exception\LocalizedException;
23+
use Magento\Store\Model\StoreManagerInterface;
24+
25+
class SetCustomerStore
26+
{
27+
/**
28+
* @param StoreManagerInterface $storeManager
29+
*/
30+
public function __construct(private StoreManagerInterface $storeManager)
31+
{
32+
}
33+
34+
/**
35+
* Set store ID for the current customer.
36+
*
37+
* @param array|null $requestData
38+
* @return void
39+
*/
40+
public function setStore(array|null $requestData = null): void
41+
{
42+
$websiteId = $requestData[CustomerInterface::WEBSITE_ID] ?? null;
43+
try {
44+
$website = $this->storeManager->getWebsite($websiteId);
45+
$storeId = $website ? current($website->getStoreIds()) : null;
46+
} catch (LocalizedException $e) {
47+
$storeId = null;
48+
}
49+
if (!$storeId) {
50+
$storeId = $requestData[CustomerInterface::STORE_ID] ?? null;
51+
}
52+
53+
$this->storeManager->setCurrentStore($storeId);
54+
}
55+
}

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/SaveTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Customer\Model\EmailNotificationInterface;
2727
use Magento\Customer\Model\Metadata\Form;
2828
use Magento\Customer\Model\Metadata\FormFactory;
29+
use Magento\Customer\Model\SetCustomerStore;
2930
use Magento\Framework\Api\DataObjectHelper;
3031
use Magento\Framework\App\Request\Http;
3132
use Magento\Framework\App\RequestInterface;
@@ -44,8 +45,6 @@
4445
use Magento\Framework\View\Result\PageFactory;
4546
use Magento\Newsletter\Model\SubscriberFactory;
4647
use Magento\Newsletter\Model\SubscriptionManagerInterface;
47-
use Magento\Store\Api\Data\WebsiteInterface;
48-
use Magento\Store\Model\StoreManagerInterface;
4948
use PHPUnit\Framework\MockObject\MockObject;
5049
use PHPUnit\Framework\TestCase;
5150

@@ -287,13 +286,9 @@ protected function setUp(): void
287286
$this->emailNotificationMock = $this->getMockBuilder(EmailNotificationInterface::class)
288287
->disableOriginalConstructor()
289288
->getMockForAbstractClass();
290-
$website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
291-
$website->method('getStoreIds')
292-
->willReturn([1]);
293-
$storeManager = $this->getMockBuilder(StoreManagerInterface::class)
294-
->getMockForAbstractClass();
295-
$storeManager->method('getWebsite')
296-
->willReturn($website);
289+
290+
$customerStoreMock = $this->createMock(SetCustomerStore::class);
291+
$customerStoreMock->expects($this->once())->method('setStore');
297292

298293
$objectManager = new ObjectManager($this);
299294

@@ -320,7 +315,7 @@ protected function setUp(): void
320315
'addressRepository' => $this->customerAddressRepositoryMock,
321316
'addressMapper' => $this->customerAddressMapperMock,
322317
'subscriptionManager' => $this->subscriptionManager,
323-
'storeManager' => $storeManager,
318+
'customerStore' => $customerStoreMock
324319
]
325320
);
326321

app/code/Magento/Customer/Test/Unit/Controller/Adminhtml/Index/ValidateTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Customer\Controller\Adminhtml\Index\Validate;
1515
use Magento\Customer\Model\Metadata\Form;
1616
use Magento\Customer\Model\Metadata\FormFactory;
17+
use Magento\Customer\Model\SetCustomerStore;
1718
use Magento\Framework\Api\DataObjectHelper;
1819
use Magento\Framework\Api\ExtensibleDataObjectConverter;
1920
use Magento\Framework\App\RequestInterface;
@@ -143,6 +144,9 @@ protected function setUp(): void
143144
);
144145
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
145146

147+
$customerStoreMock = $this->createMock(SetCustomerStore::class);
148+
$customerStoreMock->expects($this->once())->method('setStore');
149+
146150
$objectHelper = new ObjectManager($this);
147151
$this->controller = $objectHelper->getObject(
148152
Validate::class,
@@ -155,18 +159,20 @@ protected function setUp(): void
155159
'customerAccountManagement' => $this->customerAccountManagement,
156160
'resultJsonFactory' => $this->resultJsonFactory,
157161
'dataObjectHelper' => $this->dataObjectHelper,
162+
'customerStore' => $customerStoreMock
158163
]
159164
);
160165
}
161166

162167
public function testExecute()
163168
{
164169
$customerEntityId = 2;
165-
$this->request->expects($this->once())
170+
$this->request->expects($this->exactly(2))
166171
->method('getParam')
167172
->with('customer')
168173
->willReturn([
169-
'entity_id' => $customerEntityId
174+
'entity_id' => $customerEntityId,
175+
'website_id' => 1
170176
]);
171177

172178
$this->customer->expects($this->once())
@@ -268,7 +274,7 @@ public function testExecuteWithException()
268274

269275
public function testExecuteWithNewCustomerAndNoEntityId()
270276
{
271-
$this->request->expects($this->once())
277+
$this->request->expects($this->exactly(2))
272278
->method('getParam')
273279
->with('customer')
274280
->willReturn([]);
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2024 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\Customer\Test\Unit\Model;
20+
21+
use Magento\Customer\Api\Data\CustomerInterface;
22+
use Magento\Customer\Model\SetCustomerStore;
23+
use Magento\Store\Api\Data\WebsiteInterface;
24+
use Magento\Store\Model\StoreManagerInterface;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
27+
28+
class SetCustomerStoreTest extends TestCase
29+
{
30+
/**
31+
* @var SetCustomerStore
32+
*/
33+
private $model;
34+
35+
/**
36+
* @var MockObject|StoreManagerInterface
37+
*/
38+
private $storeManagerMock;
39+
40+
/**
41+
* @inheritDoc
42+
*/
43+
protected function setUp(): void
44+
{
45+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
46+
->disableOriginalConstructor()
47+
->getMockForAbstractClass();
48+
49+
$this->model = new SetCustomerStore(
50+
$this->storeManagerMock
51+
);
52+
}
53+
54+
/**
55+
* Test for setting up the customer's current store.
56+
*
57+
* @param $requestData
58+
* @dataProvider requestParamsDataProvider
59+
*/
60+
public function testSetStore($requestData)
61+
{
62+
$storeId = $requestData[CustomerInterface::STORE_ID] ?? null;
63+
$websiteId = $requestData[CustomerInterface::WEBSITE_ID] ?? null;
64+
if (!$storeId && $websiteId) {
65+
$storeId = 200;
66+
$websiteMock = $this->getMockBuilder(WebsiteInterface::class)
67+
->disableOriginalConstructor()
68+
->addMethods(['getStoreIds'])
69+
->getMockForAbstractClass();
70+
$websiteMock->expects($this->once())
71+
->method('getStoreIds')
72+
->willReturn([$storeId]);
73+
$this->storeManagerMock->expects($this->once())
74+
->method('getWebsite')
75+
->with($websiteId)
76+
->willReturn($websiteMock);
77+
}
78+
$this->storeManagerMock->expects($this->once())->method('setCurrentStore')->with($storeId);
79+
80+
$this->model->setStore($requestData);
81+
}
82+
83+
/**
84+
* Provides a customer data array to set the customer's current store.
85+
*
86+
* @return array
87+
*/
88+
public function requestParamsDataProvider(): array
89+
{
90+
return [
91+
[[]],
92+
[null],
93+
[['key1' => 4, 'key2' => 'anonymous']],
94+
[[CustomerInterface::STORE_ID => 5]],
95+
[[CustomerInterface::STORE_ID => 5, CustomerInterface::WEBSITE_ID => 2]],
96+
[[CustomerInterface::WEBSITE_ID => 7]]
97+
];
98+
}
99+
}

0 commit comments

Comments
 (0)