Skip to content

Commit bde6a09

Browse files
committed
Merge remote-tracking branch 'adobe-commerce-tier-4/ACP2E-3079' into Tier4-Kings-PR-06-12-2024
2 parents 9218190 + f1b98a3 commit bde6a09

File tree

2 files changed

+113
-4
lines changed

2 files changed

+113
-4
lines changed

app/code/Magento/Customer/Model/ResourceModel/CustomerRepository.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,16 @@ private function validateDefaultAddress(
577577
CustomerInterface $customer,
578578
string $defaultAddressType
579579
): void {
580-
$addressId = $defaultAddressType === CustomerInterface::DEFAULT_BILLING ? $customer->getDefaultBilling()
581-
: $customer->getDefaultShipping();
580+
$defaultAddressId = $defaultAddressType === CustomerInterface::DEFAULT_BILLING ?
581+
(int) $customer->getDefaultBilling() : (int) $customer->getDefaultShipping();
582+
582583
if ($customer->getAddresses()) {
583584
foreach ($customer->getAddresses() as $address) {
584-
if ((int) $addressId === (int) $address->getId()) {
585+
$addressArray = $address->__toArray();
586+
$addressId = (int) $address->getId();
587+
if (!empty($addressArray[$defaultAddressType])
588+
|| empty($addressId)
589+
|| $defaultAddressId === $addressId) {
585590
return;
586591
}
587592
}

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

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
namespace Magento\Customer\Api;
88

9+
use Magento\Authorization\Test\Fixture\Role as RoleFixture;
910
use Magento\Customer\Api\Data\AddressInterface as Address;
11+
use Magento\Customer\Api\Data\CustomerInterface as Customer;
1012
use Magento\Customer\Api\Data\CustomerInterfaceFactory;
1113
use Magento\Customer\Model\CustomerRegistry;
1214
use Magento\Framework\Api\DataObjectHelper;
13-
use Magento\Customer\Api\Data\CustomerInterface as Customer;
1415
use Magento\Framework\Api\FilterBuilder;
1516
use Magento\Framework\Api\Search\FilterGroupBuilder;
1617
use Magento\Framework\Api\SearchCriteriaBuilder;
@@ -23,13 +24,20 @@
2324
use Magento\Framework\Exception\NoSuchEntityException;
2425
use Magento\Framework\Webapi\Exception as HTTPExceptionCodes;
2526
use Magento\Framework\Webapi\Rest\Request;
27+
use Magento\Integration\Api\AdminTokenServiceInterface;
2628
use Magento\Integration\Api\CustomerTokenServiceInterface;
2729
use Magento\Integration\Api\IntegrationServiceInterface;
2830
use Magento\Integration\Api\OauthServiceInterface;
2931
use Magento\Integration\Model\Integration;
32+
use Magento\TestFramework\Fixture\AppArea;
33+
use Magento\TestFramework\Fixture\AppIsolation;
34+
use Magento\TestFramework\Fixture\DataFixture;
35+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
36+
use Magento\TestFramework\Fixture\DbIsolation;
3037
use Magento\TestFramework\Helper\Bootstrap;
3138
use Magento\TestFramework\Helper\Customer as CustomerHelper;
3239
use Magento\TestFramework\TestCase\WebapiAbstract;
40+
use Magento\User\Test\Fixture\User as UserFixture;
3341

3442
/**
3543
* Test for \Magento\Customer\Api\CustomerRepositoryInterface.
@@ -1297,4 +1305,100 @@ public static function customerValidNameDataProvider(): array
12971305
],
12981306
];
12991307
}
1308+
1309+
/**
1310+
* Verify with an admin access token that customers' default addresses are successfully altered
1311+
* without requiring an address ID key.
1312+
*
1313+
* @throws InputException
1314+
* @throws LocalizedException
1315+
* @throws \Magento\Framework\Exception\AuthenticationException
1316+
*/
1317+
#[
1318+
DbIsolation(false),
1319+
AppIsolation(true),
1320+
AppArea('webapi_rest'),
1321+
DataFixture(RoleFixture::class, as: 'restrictedRole'),
1322+
DataFixture(UserFixture::class, ['role_id' => '$restrictedRole.id$'], 'restrictedUser'),
1323+
DataFixture(
1324+
\Magento\Customer\Test\Fixture\Customer::class,
1325+
[
1326+
'email' => 'john@doe.com',
1327+
'password' => 'test@123',
1328+
'addresses' => [
1329+
[
1330+
'country_id' => 'US',
1331+
'region_id' => 32,
1332+
'city' => 'Boston',
1333+
'street' => ['10 Milk Street'],
1334+
'postcode' => '02108',
1335+
'telephone' => '1234567890',
1336+
'default_billing' => true,
1337+
'default_shipping' => true,
1338+
],
1339+
],
1340+
],
1341+
'customer'
1342+
),
1343+
]
1344+
public function testCustomerAddressUpdateOperation(): void
1345+
{
1346+
$fixtures = DataFixtureStorageManager::getStorage();
1347+
$customer = $fixtures->get('customer');
1348+
$restrictedUser = $fixtures->get('restrictedUser');
1349+
1350+
// get admin user token
1351+
$adminTokens = Bootstrap::getObjectManager()->get(AdminTokenServiceInterface::class);
1352+
$accessToken = $adminTokens->createAdminAccessToken(
1353+
$restrictedUser->getData('username'),
1354+
\Magento\TestFramework\Bootstrap::ADMIN_PASSWORD
1355+
);
1356+
1357+
$serviceInfo = [
1358+
'rest' => [
1359+
'resourcePath' => self::RESOURCE_PATH . "/{$customer->getId()}",
1360+
'httpMethod' => Request::HTTP_METHOD_PUT,
1361+
'token' => $accessToken,
1362+
],
1363+
'soap' => [
1364+
'service' => self::SERVICE_NAME,
1365+
'serviceVersion' => self::SERVICE_VERSION,
1366+
'operation' => self::SERVICE_NAME . 'Save',
1367+
'token' => $accessToken
1368+
]
1369+
];
1370+
1371+
$address = current($customer->getAddresses())->getData();
1372+
$newCustomerDataObject = [
1373+
'email' => $customer->getEmail(),
1374+
'firstname' => $customer->getFirstname(),
1375+
'lastname' => $customer->getLastname(),
1376+
'addresses' => [
1377+
[
1378+
'region' => [
1379+
'region_code' => $address['region_code'],
1380+
'region' => $address['region'],
1381+
'region_id' => $address['region_id'],
1382+
],
1383+
'country_id' => $address['country_id'],
1384+
'street' => [$address['street']],
1385+
'telephone' => $address['telephone'],
1386+
'postcode' => $address['postcode'],
1387+
'city' => $address['city'],
1388+
'firstname' => $address['firstname'],
1389+
'lastname' => $address['lastname'],
1390+
'default_shipping' => true,
1391+
'default_billing' => true
1392+
]
1393+
]
1394+
];
1395+
1396+
$requestData['customer'] = TESTS_WEB_API_ADAPTER === self::ADAPTER_REST
1397+
? $newCustomerDataObject
1398+
: array_merge([Customer::ID => $customer->getId()], $newCustomerDataObject);
1399+
1400+
$response = $this->_webApiCall($serviceInfo, $requestData);
1401+
$this->assertIsArray($response);
1402+
$this->assertArrayHasKey('addresses', $response);
1403+
}
13001404
}

0 commit comments

Comments
 (0)