From fd7522ff7c584a123be556c3b8fbb89d05a95c15 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Thu, 3 Oct 2019 14:40:50 +0300 Subject: [PATCH] graphQl-914: [Customer] Improve consistency of country field in customer address --- .../Address/CreateCustomerAddress.php | 1 + .../Address/ExtractCustomerAddressData.php | 4 +++ .../CustomerGraphQl/etc/schema.graphqls | 5 ++-- .../Customer/CreateCustomerAddressTest.php | 21 ++++++++------- .../Customer/UpdateCustomerAddressTest.php | 27 ++++++++++--------- 5 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php index 474bd99a8f13..9637b3e555b8 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php @@ -67,6 +67,7 @@ public function __construct( */ public function execute(int $customerId, array $data): AddressInterface { + // It is needed because AddressInterface has country_id field. if (isset($data['country_code'])) { $data['country_id'] = $data['country_code']; } diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php index 8741bff7aa88..7992ca834292 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/ExtractCustomerAddressData.php @@ -127,6 +127,10 @@ public function execute(AddressInterface $address): array $addressData['customer_id'] = null; + if (isset($addressData['country_id'])) { + $addressData['country_code'] = $addressData['country_id']; + } + return $addressData; } } diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index fea55ec38567..9ce2d61aa458 100644 --- a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls @@ -28,7 +28,7 @@ input CustomerAddressInput { city: String @doc(description: "The city or town") region: CustomerAddressRegionInput @doc(description: "An object containing the region name, region code, and region ID") postcode: String @doc(description: "The customer's ZIP or postal code") - country_id: CountryCodeEnum @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.") + country_id: CountryCodeEnum @doc(description: "Deprecated, use country_code instead.") country_code: CountryCodeEnum @doc(description: "The customer's country") default_shipping: Boolean @doc(description: "Indicates whether the address is the default shipping address") 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 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.") region: CustomerAddressRegion @doc(description: "An object containing the region name, region code, and region ID") region_id: Int @deprecated(reason: "Region ID is excessive on storefront and region code should suffice for all scenarios") - country_id: String @doc(description: "The customer's country") + country_id: String @doc(description: "The customer's country") @deprecated(reason: "Use country_code instead.") + country_code: CountryCodeEnum @doc(description: "The customer's country") street: [String] @doc(description: "An array of strings that define the street number and name") company: String @doc(description: "The customer's company") telephone: String @doc(description: "The telephone number") diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php index 8860965d07f0..9ccd3b0d46c7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -139,7 +139,6 @@ public function testCreateCustomerAddress() */ public function testCreateCustomerAddressWithCountryCode() { - $customerId = 1; $newAddress = [ 'region' => [ 'region' => 'Arizona', @@ -195,7 +194,7 @@ public function testCreateCustomerAddressWithCountryCode() region_id region_code } - country_id + country_code street company telephone @@ -220,16 +219,14 @@ public function testCreateCustomerAddressWithCountryCode() $response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); $this->assertArrayHasKey('createCustomerAddress', $response); $this->assertArrayHasKey('customer_id', $response['createCustomerAddress']); - $this->assertEquals($customerId, $response['createCustomerAddress']['customer_id']); + $this->assertEquals(null, $response['createCustomerAddress']['customer_id']); $this->assertArrayHasKey('id', $response['createCustomerAddress']); $address = $this->addressRepository->getById($response['createCustomerAddress']['id']); $this->assertEquals($address->getId(), $response['createCustomerAddress']['id']); - $newAddress['country_id'] = $newAddress['country_code']; - unset($newAddress['country_code']); - $this->assertCustomerAddressesFields($address, $response['createCustomerAddress']); - $this->assertCustomerAddressesFields($address, $newAddress); + $this->assertCustomerAddressesFields($address, $response['createCustomerAddress'], 'country_code'); + $this->assertCustomerAddressesFields($address, $newAddress, 'country_code'); } /** @@ -412,12 +409,16 @@ public function invalidInputDataProvider() * * @param AddressInterface $address * @param array $actualResponse + * @param string $countryFieldName */ - private function assertCustomerAddressesFields(AddressInterface $address, array $actualResponse): void - { + private function assertCustomerAddressesFields( + AddressInterface $address, + array $actualResponse, + string $countryFieldName = 'country_id' + ): void { /** @var $addresses */ $assertionMap = [ - ['response_field' => 'country_id', 'expected_value' => $address->getCountryId()], + ['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()], ['response_field' => 'street', 'expected_value' => $address->getStreet()], ['response_field' => 'company', 'expected_value' => $address->getCompany()], ['response_field' => 'telephone', 'expected_value' => $address->getTelephone()], diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php index 84525a55f8a9..d92c003c080e 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php @@ -85,7 +85,6 @@ public function testUpdateCustomerAddressWithCountryCode() { $userName = 'customer@example.com'; $password = 'password'; - $customerId = 1; $addressId = 1; $mutation = $this->getMutationWithCountryCode($addressId); @@ -93,15 +92,15 @@ public function testUpdateCustomerAddressWithCountryCode() $response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); $this->assertArrayHasKey('updateCustomerAddress', $response); $this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']); - $this->assertEquals($customerId, $response['updateCustomerAddress']['customer_id']); + $this->assertEquals(null, $response['updateCustomerAddress']['customer_id']); $this->assertArrayHasKey('id', $response['updateCustomerAddress']); $address = $this->addressRepository->getById($addressId); $this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']); - $this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']); + $this->assertCustomerAddressesFields($address, $response['updateCustomerAddress'], 'country_code'); $updateAddress = $this->getAddressDataCanadaCountry(); - $this->assertCustomerAddressesFields($address, $updateAddress); + $this->assertCustomerAddressesFields($address, $updateAddress, 'country_code'); } /** @@ -159,12 +158,16 @@ public function testUpdateCustomerAddressWithMissingAttribute() * * @param AddressInterface $address * @param array $actualResponse + * @param string $countryFieldName */ - private function assertCustomerAddressesFields(AddressInterface $address, $actualResponse): void - { + private function assertCustomerAddressesFields( + AddressInterface $address, + $actualResponse, + string $countryFieldName = 'country_id' + ): void { /** @var $addresses */ $assertionMap = [ - ['response_field' => 'country_id', 'expected_value' => $address->getCountryId()], + ['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()], ['response_field' => 'street', 'expected_value' => $address->getStreet()], ['response_field' => 'company', 'expected_value' => $address->getCompany()], ['response_field' => 'telephone', 'expected_value' => $address->getTelephone()], @@ -443,7 +446,7 @@ private function getAddressDataCanadaCountry(): array 'region_id' => 66, 'region_code' => 'AB' ], - 'country_id' => 'CA', + 'country_code' => 'CA', 'street' => ['Line 1 Street', 'Line 2'], 'company' => 'Company Name', 'telephone' => '123456789', @@ -531,8 +534,8 @@ private function getMutation(int $addressId): string private function getMutationWithCountryCode(int $addressId): string { $updateAddress = $this->getAddressDataCanadaCountry(); - $defaultShippingText = $updateAddress['default_shipping'] ? "true" : "false"; - $defaultBillingText = $updateAddress['default_billing'] ? "true" : "false"; + $defaultShippingText = $updateAddress['default_shipping'] ? 'true' : 'false'; + $defaultBillingText = $updateAddress['default_billing'] ? 'true' : 'false'; $mutation = <<