Skip to content

Commit fd7522f

Browse files
committed
graphQl-914: [Customer] Improve consistency of country field in customer address
1 parent 42ecae6 commit fd7522f

File tree

5 files changed

+34
-24
lines changed

5 files changed

+34
-24
lines changed

app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function __construct(
6767
*/
6868
public function execute(int $customerId, array $data): AddressInterface
6969
{
70+
// It is needed because AddressInterface has country_id field.
7071
if (isset($data['country_code'])) {
7172
$data['country_id'] = $data['country_code'];
7273
}

app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public function execute(AddressInterface $address): array
127127

128128
$addressData['customer_id'] = null;
129129

130+
if (isset($addressData['country_id'])) {
131+
$addressData['country_code'] = $addressData['country_id'];
132+
}
133+
130134
return $addressData;
131135
}
132136
}

app/code/Magento/CustomerGraphQl/etc/schema.graphqls

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ input CustomerAddressInput {
2828
city: String @doc(description: "The city or town")
2929
region: CustomerAddressRegionInput @doc(description: "An object containing the region name, region code, and region ID")
3030
postcode: String @doc(description: "The customer's ZIP or postal code")
31-
country_id: CountryCodeEnum @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
31+
country_id: CountryCodeEnum @doc(description: "Deprecated, use country_code instead.")
3232
country_code: CountryCodeEnum @doc(description: "The customer's country")
3333
default_shipping: Boolean @doc(description: "Indicates whether the address is the default shipping address")
3434
default_billing: Boolean @doc(description: "Indicates whether the address is the default billing address")
@@ -103,7 +103,8 @@ type CustomerAddress @doc(description: "CustomerAddress contains detailed inform
103103
customer_id: Int @doc(description: "The customer ID") @deprecated(reason: "customer_id is not needed as part of CustomerAddress, address ID (id) is unique identifier for the addresses.")
104104
region: CustomerAddressRegion @doc(description: "An object containing the region name, region code, and region ID")
105105
region_id: Int @deprecated(reason: "Region ID is excessive on storefront and region code should suffice for all scenarios")
106-
country_id: String @doc(description: "The customer's country")
106+
country_id: String @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
107+
country_code: CountryCodeEnum @doc(description: "The customer's country")
107108
street: [String] @doc(description: "An array of strings that define the street number and name")
108109
company: String @doc(description: "The customer's company")
109110
telephone: String @doc(description: "The telephone number")

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public function testCreateCustomerAddress()
139139
*/
140140
public function testCreateCustomerAddressWithCountryCode()
141141
{
142-
$customerId = 1;
143142
$newAddress = [
144143
'region' => [
145144
'region' => 'Arizona',
@@ -195,7 +194,7 @@ public function testCreateCustomerAddressWithCountryCode()
195194
region_id
196195
region_code
197196
}
198-
country_id
197+
country_code
199198
street
200199
company
201200
telephone
@@ -220,16 +219,14 @@ public function testCreateCustomerAddressWithCountryCode()
220219
$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
221220
$this->assertArrayHasKey('createCustomerAddress', $response);
222221
$this->assertArrayHasKey('customer_id', $response['createCustomerAddress']);
223-
$this->assertEquals($customerId, $response['createCustomerAddress']['customer_id']);
222+
$this->assertEquals(null, $response['createCustomerAddress']['customer_id']);
224223
$this->assertArrayHasKey('id', $response['createCustomerAddress']);
225224

226225
$address = $this->addressRepository->getById($response['createCustomerAddress']['id']);
227226
$this->assertEquals($address->getId(), $response['createCustomerAddress']['id']);
228227

229-
$newAddress['country_id'] = $newAddress['country_code'];
230-
unset($newAddress['country_code']);
231-
$this->assertCustomerAddressesFields($address, $response['createCustomerAddress']);
232-
$this->assertCustomerAddressesFields($address, $newAddress);
228+
$this->assertCustomerAddressesFields($address, $response['createCustomerAddress'], 'country_code');
229+
$this->assertCustomerAddressesFields($address, $newAddress, 'country_code');
233230
}
234231

235232
/**
@@ -412,12 +409,16 @@ public function invalidInputDataProvider()
412409
*
413410
* @param AddressInterface $address
414411
* @param array $actualResponse
412+
* @param string $countryFieldName
415413
*/
416-
private function assertCustomerAddressesFields(AddressInterface $address, array $actualResponse): void
417-
{
414+
private function assertCustomerAddressesFields(
415+
AddressInterface $address,
416+
array $actualResponse,
417+
string $countryFieldName = 'country_id'
418+
): void {
418419
/** @var $addresses */
419420
$assertionMap = [
420-
['response_field' => 'country_id', 'expected_value' => $address->getCountryId()],
421+
['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()],
421422
['response_field' => 'street', 'expected_value' => $address->getStreet()],
422423
['response_field' => 'company', 'expected_value' => $address->getCompany()],
423424
['response_field' => 'telephone', 'expected_value' => $address->getTelephone()],

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,22 @@ public function testUpdateCustomerAddressWithCountryCode()
8585
{
8686
$userName = 'customer@example.com';
8787
$password = 'password';
88-
$customerId = 1;
8988
$addressId = 1;
9089

9190
$mutation = $this->getMutationWithCountryCode($addressId);
9291

9392
$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
9493
$this->assertArrayHasKey('updateCustomerAddress', $response);
9594
$this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']);
96-
$this->assertEquals($customerId, $response['updateCustomerAddress']['customer_id']);
95+
$this->assertEquals(null, $response['updateCustomerAddress']['customer_id']);
9796
$this->assertArrayHasKey('id', $response['updateCustomerAddress']);
9897

9998
$address = $this->addressRepository->getById($addressId);
10099
$this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']);
101-
$this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']);
100+
$this->assertCustomerAddressesFields($address, $response['updateCustomerAddress'], 'country_code');
102101

103102
$updateAddress = $this->getAddressDataCanadaCountry();
104-
$this->assertCustomerAddressesFields($address, $updateAddress);
103+
$this->assertCustomerAddressesFields($address, $updateAddress, 'country_code');
105104
}
106105

107106
/**
@@ -159,12 +158,16 @@ public function testUpdateCustomerAddressWithMissingAttribute()
159158
*
160159
* @param AddressInterface $address
161160
* @param array $actualResponse
161+
* @param string $countryFieldName
162162
*/
163-
private function assertCustomerAddressesFields(AddressInterface $address, $actualResponse): void
164-
{
163+
private function assertCustomerAddressesFields(
164+
AddressInterface $address,
165+
$actualResponse,
166+
string $countryFieldName = 'country_id'
167+
): void {
165168
/** @var $addresses */
166169
$assertionMap = [
167-
['response_field' => 'country_id', 'expected_value' => $address->getCountryId()],
170+
['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()],
168171
['response_field' => 'street', 'expected_value' => $address->getStreet()],
169172
['response_field' => 'company', 'expected_value' => $address->getCompany()],
170173
['response_field' => 'telephone', 'expected_value' => $address->getTelephone()],
@@ -443,7 +446,7 @@ private function getAddressDataCanadaCountry(): array
443446
'region_id' => 66,
444447
'region_code' => 'AB'
445448
],
446-
'country_id' => 'CA',
449+
'country_code' => 'CA',
447450
'street' => ['Line 1 Street', 'Line 2'],
448451
'company' => 'Company Name',
449452
'telephone' => '123456789',
@@ -531,8 +534,8 @@ private function getMutation(int $addressId): string
531534
private function getMutationWithCountryCode(int $addressId): string
532535
{
533536
$updateAddress = $this->getAddressDataCanadaCountry();
534-
$defaultShippingText = $updateAddress['default_shipping'] ? "true" : "false";
535-
$defaultBillingText = $updateAddress['default_billing'] ? "true" : "false";
537+
$defaultShippingText = $updateAddress['default_shipping'] ? 'true' : 'false';
538+
$defaultBillingText = $updateAddress['default_billing'] ? 'true' : 'false';
536539

537540
$mutation
538541
= <<<MUTATION
@@ -543,7 +546,7 @@ private function getMutationWithCountryCode(int $addressId): string
543546
region_id: {$updateAddress['region']['region_id']}
544547
region_code: "{$updateAddress['region']['region_code']}"
545548
}
546-
country_code: {$updateAddress['country_id']}
549+
country_code: {$updateAddress['country_code']}
547550
street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
548551
company: "{$updateAddress['company']}"
549552
telephone: "{$updateAddress['telephone']}"
@@ -566,7 +569,7 @@ private function getMutationWithCountryCode(int $addressId): string
566569
region_id
567570
region_code
568571
}
569-
country_id
572+
country_code
570573
street
571574
company
572575
telephone

0 commit comments

Comments
 (0)