Skip to content

Commit 981ba5b

Browse files
committed
AC-10896: Customer account REST API fix
1 parent 478ea9b commit 981ba5b

File tree

3 files changed

+44
-31
lines changed

3 files changed

+44
-31
lines changed

app/code/Magento/Customer/Model/Plugin/UpdateCustomer.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,19 @@ public function beforeSave(
5252
CustomerInterface $customer,
5353
?string $passwordHash = null
5454
): array {
55-
$customerSessionId = $this->userContext->getUserType() === $this->userContext::USER_TYPE_CUSTOMER ?
56-
(int)$this->userContext->getUserId() : 0;
55+
$userType = $this->userContext->getUserType();
56+
$customerSessionId = (int)$this->userContext->getUserId();
5757
$customerId = (int)$this->request->getParam('customerId');
5858
$bodyParams = $this->request->getBodyParams();
59-
if (!isset($bodyParams['customer']['Id']) && $customerId) {
60-
if ($customerId === $customerSessionId || $customerSessionId === 0) {
61-
$customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer);
62-
}
59+
60+
if ($userType === UserContextInterface::USER_TYPE_CUSTOMER &&
61+
!isset($bodyParams['customer']['Id']) &&
62+
$customerId &&
63+
$customerId === $customerSessionId
64+
) {
65+
$customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer);
66+
} elseif ($userType === UserContextInterface::USER_TYPE_ADMIN) {
67+
$customer = $this->getUpdatedCustomer($customerRepository->getById($customerId), $customer);
6368
}
6469

6570
return [$customer, $passwordHash];

dev/tests/api-functional/testsuite/Magento/Customer/Api/CustomerRepositoryTest.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
67

78
namespace Magento\Customer\Api;
89

@@ -142,7 +143,7 @@ protected function tearDown(): void
142143
}
143144

144145
/**
145-
* Validate update by invalid customer.
146+
* Validate update operation by invalid customer
146147
*
147148
*/
148149
public function testInvalidCustomerUpdate()
@@ -152,9 +153,8 @@ public function testInvalidCustomerUpdate()
152153
//Create first customer and retrieve customer token.
153154
$firstCustomerData = $this->_createCustomer();
154155

155-
// get customer ID token
156+
//Get customer ID token
156157
/** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */
157-
//$customerTokenService = $this->objectManager->create(CustomerTokenServiceInterface::class);
158158
$customerTokenService = Bootstrap::getObjectManager()->create(
159159
\Magento\Integration\Api\CustomerTokenServiceInterface::class
160160
);
@@ -238,11 +238,9 @@ public function testDeleteCustomer()
238238
'operation' => self::SERVICE_NAME . 'DeleteById',
239239
],
240240
];
241-
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
242-
$response = $this->_webApiCall($serviceInfo, ['customerId' => $customerData['id']]);
243-
} else {
244-
$response = $this->_webApiCall($serviceInfo);
245-
}
241+
$response = (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP)
242+
? $this->_webApiCall($serviceInfo, ['customerId' => $customerData['id']])
243+
: $this->_webApiCall($serviceInfo);
246244

247245
$this->assertTrue($response);
248246

@@ -253,7 +251,7 @@ public function testDeleteCustomer()
253251
}
254252

255253
/**
256-
* Check that non authorized consumer can`t delete customer.
254+
* Check that non-authorized consumer can`t delete customer.
257255
*
258256
* @return void
259257
*/
@@ -347,10 +345,10 @@ public function testDeleteCustomerInvalidCustomerId(): void
347345
public function testUpdateCustomer(): void
348346
{
349347
$customerId = 1;
350-
$updatedLastname = 'Updated lastname';
348+
$updatedLastName = 'Updated lastname';
351349
$customer = $this->getCustomerData($customerId);
352350
$customerData = $this->dataObjectProcessor->buildOutputDataArray($customer, Customer::class);
353-
$customerData[Customer::LASTNAME] = $updatedLastname;
351+
$customerData[Customer::LASTNAME] = $updatedLastName;
354352

355353
$serviceInfo = [
356354
'rest' => [
@@ -364,16 +362,20 @@ public function testUpdateCustomer(): void
364362
],
365363
];
366364

367-
$requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
365+
$requestData['customer'] = (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP)
368366
? $customerData
369-
: [Customer::LASTNAME => $updatedLastname];
370-
367+
: [
368+
Customer::FIRSTNAME => $customer->getFirstname(),
369+
Customer::LASTNAME => $updatedLastName,
370+
Customer::EMAIL => $customer->getEmail(),
371+
Customer::ID => $customerId,
372+
];
371373
$response = $this->_webApiCall($serviceInfo, $requestData);
372374
$this->assertNotNull($response);
373375

374376
//Verify if the customer is updated
375377
$existingCustomerDataObject = $this->getCustomerData($customerId);
376-
$this->assertEquals($updatedLastname, $existingCustomerDataObject->getLastname());
378+
$this->assertEquals($updatedLastName, $existingCustomerDataObject->getLastname());
377379
$this->assertEquals($customerData[Customer::FIRSTNAME], $existingCustomerDataObject->getFirstname());
378380
}
379381

dev/tests/api-functional/testsuite/Magento/LoginAsCustomerAssistance/Plugin/CustomerAfterPluginTest.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,19 @@ public function testUpdateCustomer(int $state, bool $expected): void
7777
{
7878
$customerId = (int)$this->customerRepository->get('[email protected]')->getId();
7979

80-
$updatedLastname = 'Updated lastname';
80+
$updatedLastName = 'Updated lastname';
8181
$customer = $this->getCustomerData($customerId);
8282
$customerData = $this->dataObjectProcessor->buildOutputDataArray($customer, Customer::class);
83-
$customerData[Customer::LASTNAME] = $updatedLastname;
83+
$customerData[Customer::LASTNAME] = $updatedLastName;
8484
$customerData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['assistance_allowed'] = $state;
8585

86-
$requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
86+
$requestData['customer'] = (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP)
8787
? $customerData
8888
: [
89-
Customer::LASTNAME => $updatedLastname,
89+
Customer::FIRSTNAME => $customer->getFirstname(),
90+
Customer::LASTNAME => $updatedLastName,
91+
Customer::EMAIL => $customer->getEmail(),
92+
Customer::ID => $customerId,
9093
Customer::EXTENSION_ATTRIBUTES_KEY => ['assistance_allowed' => $state]
9194
];
9295

@@ -95,7 +98,7 @@ public function testUpdateCustomer(int $state, bool $expected): void
9598
$this->assertNotNull($response);
9699

97100
$existingCustomerDataObject = $this->getCustomerData($customerId);
98-
$this->assertEquals($updatedLastname, $existingCustomerDataObject->getLastname());
101+
$this->assertEquals($updatedLastName, $existingCustomerDataObject->getLastname());
99102
$this->assertEquals($expected, $this->isAssistanceEnabled->execute($customerId));
100103
}
101104

@@ -117,16 +120,19 @@ public function testUpdateCustomerWithLimitedResources(int $state): void
117120
];
118121
$customerId = (int)$this->customerRepository->get('[email protected]')->getId();
119122

120-
$updatedLastname = 'Updated lastname';
123+
$updatedLastName = 'Updated lastname';
121124
$customer = $this->getCustomerData($customerId);
122125
$customerData = $this->dataObjectProcessor->buildOutputDataArray($customer, Customer::class);
123-
$customerData[Customer::LASTNAME] = $updatedLastname;
126+
$customerData[Customer::LASTNAME] = $updatedLastName;
124127
$customerData[ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY]['assistance_allowed'] = $state;
125128

126-
$requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP
129+
$requestData['customer'] = (TESTS_WEB_API_ADAPTER === self::ADAPTER_SOAP)
127130
? $customerData
128131
: [
129-
Customer::LASTNAME => $updatedLastname,
132+
Customer::FIRSTNAME => $customer->getFirstname(),
133+
Customer::LASTNAME => $updatedLastName,
134+
Customer::EMAIL => $customer->getEmail(),
135+
Customer::ID => $customerId,
130136
Customer::EXTENSION_ATTRIBUTES_KEY => ['assistance_allowed' => $state]
131137
];
132138

@@ -136,7 +142,7 @@ public function testUpdateCustomerWithLimitedResources(int $state): void
136142
$this->assertNotNull($response);
137143

138144
$existingCustomerDataObject = $this->getCustomerData($customerId);
139-
$this->assertEquals($updatedLastname, $existingCustomerDataObject->getLastname());
145+
$this->assertEquals($updatedLastName, $existingCustomerDataObject->getLastname());
140146
$this->assertEquals(false, $this->isAssistanceEnabled->execute($customerId));
141147
}
142148

0 commit comments

Comments
 (0)