Skip to content

Commit bcbdb8b

Browse files
committed
ACP2E-2791: Not able to Save Customer attribute information in Admin Edit customer section;
1 parent 888ae6a commit bcbdb8b

File tree

4 files changed

+58
-19
lines changed

4 files changed

+58
-19
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,15 @@ private function getCurrentCustomerId()
561561
private function setCurrentCustomerStore(): void
562562
{
563563
$originalRequestData = $this->getRequest()->getPostValue(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
564-
565-
$storeId = $originalRequestData['store_id'] ?? null;
566-
if (!$storeId) {
567-
$websiteId = $originalRequestData['website_id'] ?? null;
568-
$website = $this->storeManager->getWebsite($websiteId);
569-
$storeId = current($website->getStoreIds());
564+
if ($originalRequestData) {
565+
$storeId = $originalRequestData['store_id'] ?? null;
566+
if (!$storeId) {
567+
$websiteId = $originalRequestData['website_id'] ?? null;
568+
$website = $this->storeManager->getWebsite($websiteId);
569+
$storeId = $website ? current($website->getStoreIds()) : null;
570+
}
571+
$this->storeManager->setCurrentStore($storeId);
570572
}
571-
$this->storeManager->setCurrentStore($storeId);
572573
}
573574

574575
/**

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,14 @@ public function execute()
212212
private function setCurrentCustomerStore(): void
213213
{
214214
$requestData = $this->getRequest()->getParam(CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
215-
216-
$storeId = $requestData['store_id'] ?? null;
217-
if (!$storeId) {
218-
$websiteId = $requestData['website_id'] ?? null;
219-
$website = $this->storeManager->getWebsite($websiteId);
220-
$storeId = current($website->getStoreIds());
215+
if ($requestData) {
216+
$storeId = $requestData['store_id'] ?? null;
217+
if (!$storeId) {
218+
$websiteId = $requestData['website_id'] ?? null;
219+
$website = $this->storeManager->getWebsite($websiteId);
220+
$storeId = $website ? current($website->getStoreIds()) : null;
221+
}
222+
$this->storeManager->setCurrentStore($storeId);
221223
}
222-
$this->storeManager->setCurrentStore($storeId);
223224
}
224225
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,13 @@ protected function setUp(): void
287287
->disableOriginalConstructor()
288288
->getMockForAbstractClass();
289289
$website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getStoreIds']);
290-
$website->method('getStoreIds')
290+
$website->expects($this->exactly(2))->method('getStoreIds')
291291
->willReturn([1]);
292292
$storeManager = $this->getMockBuilder(StoreManagerInterface::class)
293293
->getMockForAbstractClass();
294-
$storeManager->method('getWebsite')
294+
$storeManager->expects($this->exactly(2))->method('getWebsite')
295295
->willReturn($website);
296+
$storeManager->expects($this->exactly(2))->method('setCurrentStore')->with(1);
296297

297298
$objectManager = new ObjectManager($this);
298299

@@ -347,6 +348,7 @@ public function testExecuteWithExistentCustomer()
347348
'code' => 'value',
348349
'coolness' => false,
349350
'disable_auto_group_change' => 'false',
351+
'website_id' => 1
350352
],
351353
'subscription_status' => [$subscriptionWebsite => $subscriptionStatus],
352354
'subscription_store' => [$subscriptionWebsite => $subscriptionStore],
@@ -356,12 +358,14 @@ public function testExecuteWithExistentCustomer()
356358
'code' => 'value',
357359
'coolness' => false,
358360
'disable_auto_group_change' => 'false',
361+
'website_id' => 1
359362
];
360363
$compactedData = [
361364
'entity_id' => $customerId,
362365
'code' => 'value',
363366
'coolness' => false,
364367
'disable_auto_group_change' => 'false',
368+
'website_id' => 1,
365369
CustomerInterface::DEFAULT_BILLING => 2,
366370
CustomerInterface::DEFAULT_SHIPPING => 2
367371
];
@@ -381,6 +385,7 @@ public function testExecuteWithExistentCustomer()
381385
'confirmation' => false,
382386
'sendemail_store_id' => '1',
383387
'id' => $customerId,
388+
'website_id' => 1
384389
];
385390

386391
/** @var AttributeMetadataInterface|MockObject $customerFormMock */

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

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,15 @@
2020
use Magento\Framework\App\ResponseInterface;
2121
use Magento\Framework\Controller\Result\Json;
2222
use Magento\Framework\Controller\Result\JsonFactory;
23+
use Magento\Framework\DataObject;
2324
use Magento\Framework\Message\Error;
2425
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
2526
use Magento\Framework\Validator\Exception;
27+
use Magento\Store\Api\Data\StoreInterface;
28+
use Magento\Store\Api\Data\WebsiteInterface;
29+
use Magento\Store\Model\Store;
30+
use Magento\Store\Model\StoreManagerInterface;
31+
use Magento\Store\Model\Website;
2632
use PHPUnit\Framework\MockObject\MockObject;
2733
use PHPUnit\Framework\TestCase;
2834

@@ -85,6 +91,11 @@ class ValidateTest extends TestCase
8591
/** @var Validate */
8692
protected $controller;
8793

94+
/**
95+
* @var MockObject|StoreManagerInterface
96+
*/
97+
private $storeManagerMock;
98+
8899
protected function setUp(): void
89100
{
90101
if (!function_exists('libxml_set_external_entity_loader')) {
@@ -143,6 +154,10 @@ protected function setUp(): void
143154
);
144155
$this->resultJsonFactory->expects($this->once())->method('create')->willReturn($this->resultJson);
145156

157+
$this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
158+
->disableOriginalConstructor()
159+
->getMockForAbstractClass();
160+
146161
$objectHelper = new ObjectManager($this);
147162
$this->controller = $objectHelper->getObject(
148163
Validate::class,
@@ -155,20 +170,37 @@ protected function setUp(): void
155170
'customerAccountManagement' => $this->customerAccountManagement,
156171
'resultJsonFactory' => $this->resultJsonFactory,
157172
'dataObjectHelper' => $this->dataObjectHelper,
173+
'storeManager' => $this->storeManagerMock
158174
]
159175
);
176+
160177
}
161178

162179
public function testExecute()
163180
{
164181
$customerEntityId = 2;
165-
$this->request->expects($this->once())
182+
$this->request->expects($this->exactly(2))
166183
->method('getParam')
167184
->with('customer')
168185
->willReturn([
169-
'entity_id' => $customerEntityId
186+
'entity_id' => $customerEntityId,
187+
'website_id' => 1
170188
]);
171189

190+
$storeIds = [1];
191+
$websiteMock = $this->getMockBuilder(WebsiteInterface::class)
192+
->disableOriginalConstructor()
193+
->addMethods(['getStoreIds'])
194+
->getMockForAbstractClass();
195+
$websiteMock->expects($this->once())
196+
->method('getStoreIds')
197+
->willReturn($storeIds);
198+
$this->storeManagerMock->expects($this->once())
199+
->method('getWebsite')
200+
->with(1)
201+
->willReturn($websiteMock);
202+
$this->storeManagerMock->expects($this->once())->method('setCurrentStore')->with(1);
203+
172204
$this->customer->expects($this->once())
173205
->method('setId')
174206
->with($customerEntityId);
@@ -268,7 +300,7 @@ public function testExecuteWithException()
268300

269301
public function testExecuteWithNewCustomerAndNoEntityId()
270302
{
271-
$this->request->expects($this->once())
303+
$this->request->expects($this->exactly(2))
272304
->method('getParam')
273305
->with('customer')
274306
->willReturn([]);

0 commit comments

Comments
 (0)