Skip to content

Commit 17fa6d9

Browse files
committed
graphQl-914: [Customer] Improve consistency of country field in customer address
1 parent 296e280 commit 17fa6d9

File tree

5 files changed

+229
-2
lines changed

5 files changed

+229
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public function __construct(
6767
*/
6868
public function execute(int $customerId, array $data): AddressInterface
6969
{
70+
if (isset($data['country_code'])) {
71+
$data['country_id'] = $data['country_code'];
72+
}
7073
$this->validateData($data);
7174

7275
/** @var AddressInterface $address */

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public function __construct(
6666
*/
6767
public function execute(AddressInterface $address, array $data): void
6868
{
69+
if (isset($data['country_code'])) {
70+
$data['country_id'] = $data['country_code'];
71+
}
6972
$this->validateData($data);
7073

7174
$filteredData = array_diff_key($data, array_flip($this->restrictedKeys));

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ 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")
31+
country_id: CountryCodeEnum @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
32+
country_code: CountryCodeEnum @doc(description: "The customer's country")
3233
default_shipping: Boolean @doc(description: "Indicates whether the address is the default shipping address")
3334
default_billing: Boolean @doc(description: "Indicates whether the address is the default billing address")
3435
fax: String @doc(description: "The fax number")
@@ -100,7 +101,8 @@ type CustomerAddress @doc(description: "CustomerAddress contains detailed inform
100101
customer_id: Int @doc(description: "The customer ID")
101102
region: CustomerAddressRegion @doc(description: "An object containing the region name, region code, and region ID")
102103
region_id: Int @doc(description: "A number that uniquely identifies the state, province, or other area")
103-
country_id: String @doc(description: "The customer's country")
104+
country_id: String @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.")
105+
country_code: CountryCodeEnum @doc(description: "The customer's country")
104106
street: [String] @doc(description: "An array of strings that define the street number and name")
105107
company: String @doc(description: "The customer's company")
106108
telephone: String @doc(description: "The telephone number")

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,105 @@ public function testCreateCustomerAddress()
133133
$this->assertCustomerAddressesFields($address, $newAddress);
134134
}
135135

136+
/**
137+
* @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php
138+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
139+
*/
140+
public function testCreateCustomerAddressWithCountryCode()
141+
{
142+
$customerId = 1;
143+
$newAddress = [
144+
'region' => [
145+
'region' => 'Arizona',
146+
'region_id' => 4,
147+
'region_code' => 'AZ'
148+
],
149+
'country_code' => 'US',
150+
'street' => ['Line 1 Street', 'Line 2'],
151+
'company' => 'Company name',
152+
'telephone' => '123456789',
153+
'fax' => '123123123',
154+
'postcode' => '7777',
155+
'city' => 'City Name',
156+
'firstname' => 'Adam',
157+
'lastname' => 'Phillis',
158+
'middlename' => 'A',
159+
'prefix' => 'Mr.',
160+
'suffix' => 'Jr.',
161+
'vat_id' => '1',
162+
'default_shipping' => true,
163+
'default_billing' => false
164+
];
165+
166+
$mutation
167+
= <<<MUTATION
168+
mutation {
169+
createCustomerAddress(input: {
170+
region: {
171+
region: "{$newAddress['region']['region']}"
172+
region_id: {$newAddress['region']['region_id']}
173+
region_code: "{$newAddress['region']['region_code']}"
174+
}
175+
country_code: {$newAddress['country_code']}
176+
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
177+
company: "{$newAddress['company']}"
178+
telephone: "{$newAddress['telephone']}"
179+
fax: "{$newAddress['fax']}"
180+
postcode: "{$newAddress['postcode']}"
181+
city: "{$newAddress['city']}"
182+
firstname: "{$newAddress['firstname']}"
183+
lastname: "{$newAddress['lastname']}"
184+
middlename: "{$newAddress['middlename']}"
185+
prefix: "{$newAddress['prefix']}"
186+
suffix: "{$newAddress['suffix']}"
187+
vat_id: "{$newAddress['vat_id']}"
188+
default_shipping: true
189+
default_billing: false
190+
}) {
191+
id
192+
customer_id
193+
region {
194+
region
195+
region_id
196+
region_code
197+
}
198+
country_id
199+
street
200+
company
201+
telephone
202+
fax
203+
postcode
204+
city
205+
firstname
206+
lastname
207+
middlename
208+
prefix
209+
suffix
210+
vat_id
211+
default_shipping
212+
default_billing
213+
}
214+
}
215+
MUTATION;
216+
217+
$userName = 'customer@example.com';
218+
$password = 'password';
219+
220+
$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
221+
$this->assertArrayHasKey('createCustomerAddress', $response);
222+
$this->assertArrayHasKey('customer_id', $response['createCustomerAddress']);
223+
$this->assertEquals($customerId, $response['createCustomerAddress']['customer_id']);
224+
$this->assertArrayHasKey('id', $response['createCustomerAddress']);
225+
226+
$address = $this->addressRepository->getById($response['createCustomerAddress']['id']);
227+
$this->assertEquals($address->getId(), $response['createCustomerAddress']['id']);
228+
229+
$newAddress['country_id'] = $newAddress['country_code'];
230+
unset($newAddress['country_code']);
231+
$this->assertCustomerAddressesFields($address, $response['createCustomerAddress']);
232+
$this->assertCustomerAddressesFields($address, $newAddress);
233+
}
234+
136235
/**
137236
* @expectedException Exception
138237
* @expectedExceptionMessage The current customer isn't authorized.

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

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,34 @@ public function testUpdateCustomerAddress()
7777
$this->assertCustomerAddressesFields($address, $updateAddress);
7878
}
7979

80+
/**
81+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
82+
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
83+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
84+
*/
85+
public function testUpdateCustomerAddressWithCountryCode()
86+
{
87+
$userName = 'customer@example.com';
88+
$password = 'password';
89+
$customerId = 1;
90+
$addressId = 1;
91+
92+
$mutation = $this->getMutationWithCountryCode($addressId);
93+
94+
$response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password));
95+
$this->assertArrayHasKey('updateCustomerAddress', $response);
96+
$this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']);
97+
$this->assertEquals($customerId, $response['updateCustomerAddress']['customer_id']);
98+
$this->assertArrayHasKey('id', $response['updateCustomerAddress']);
99+
100+
$address = $this->addressRepository->getById($addressId);
101+
$this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']);
102+
$this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']);
103+
104+
$updateAddress = $this->getAddressDataCanadaCountry();
105+
$this->assertCustomerAddressesFields($address, $updateAddress);
106+
}
107+
80108
/**
81109
* @expectedException Exception
82110
* @expectedExceptionMessage The current customer isn't authorized.
@@ -405,6 +433,35 @@ private function getAddressData(): array
405433
];
406434
}
407435

436+
/**
437+
* @return array
438+
*/
439+
private function getAddressDataCanadaCountry(): array
440+
{
441+
return [
442+
'region' => [
443+
'region' => 'Alberta',
444+
'region_id' => 66,
445+
'region_code' => 'AB'
446+
],
447+
'country_id' => 'CA',
448+
'street' => ['Line 1 Street', 'Line 2'],
449+
'company' => 'Company Name',
450+
'telephone' => '123456789',
451+
'fax' => '123123123',
452+
'postcode' => '7777',
453+
'city' => 'City Name',
454+
'firstname' => 'Adam',
455+
'lastname' => 'Phillis',
456+
'middlename' => 'A',
457+
'prefix' => 'Mr.',
458+
'suffix' => 'Jr.',
459+
'vat_id' => '1',
460+
'default_shipping' => true,
461+
'default_billing' => true
462+
];
463+
}
464+
408465
/**
409466
* @param int $addressId
410467
* @return string
@@ -464,6 +521,69 @@ private function getMutation(int $addressId): string
464521
default_billing
465522
}
466523
}
524+
MUTATION;
525+
return $mutation;
526+
}
527+
528+
/**
529+
* @param int $addressId
530+
* @return string
531+
*/
532+
private function getMutationWithCountryCode(int $addressId): string
533+
{
534+
$updateAddress = $this->getAddressDataCanadaCountry();
535+
$defaultShippingText = $updateAddress['default_shipping'] ? "true" : "false";
536+
$defaultBillingText = $updateAddress['default_billing'] ? "true" : "false";
537+
538+
$mutation
539+
= <<<MUTATION
540+
mutation {
541+
updateCustomerAddress(id: {$addressId}, input: {
542+
region: {
543+
region: "{$updateAddress['region']['region']}"
544+
region_id: {$updateAddress['region']['region_id']}
545+
region_code: "{$updateAddress['region']['region_code']}"
546+
}
547+
country_code: {$updateAddress['country_id']}
548+
street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
549+
company: "{$updateAddress['company']}"
550+
telephone: "{$updateAddress['telephone']}"
551+
fax: "{$updateAddress['fax']}"
552+
postcode: "{$updateAddress['postcode']}"
553+
city: "{$updateAddress['city']}"
554+
firstname: "{$updateAddress['firstname']}"
555+
lastname: "{$updateAddress['lastname']}"
556+
middlename: "{$updateAddress['middlename']}"
557+
prefix: "{$updateAddress['prefix']}"
558+
suffix: "{$updateAddress['suffix']}"
559+
vat_id: "{$updateAddress['vat_id']}"
560+
default_shipping: {$defaultShippingText}
561+
default_billing: {$defaultBillingText}
562+
}) {
563+
id
564+
customer_id
565+
region {
566+
region
567+
region_id
568+
region_code
569+
}
570+
country_id
571+
street
572+
company
573+
telephone
574+
fax
575+
postcode
576+
city
577+
firstname
578+
lastname
579+
middlename
580+
prefix
581+
suffix
582+
vat_id
583+
default_shipping
584+
default_billing
585+
}
586+
}
467587
MUTATION;
468588
return $mutation;
469589
}

0 commit comments

Comments
 (0)