From 17fa6d9dad005ad38d69b1a1107f9bc220a77835 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Mon, 23 Sep 2019 13:02:50 +0300 Subject: [PATCH 1/4] graphQl-914: [Customer] Improve consistency of country field in customer address --- .../Address/CreateCustomerAddress.php | 3 + .../Address/UpdateCustomerAddress.php | 3 + .../CustomerGraphQl/etc/schema.graphqls | 6 +- .../Customer/CreateCustomerAddressTest.php | 99 +++++++++++++++ .../Customer/UpdateCustomerAddressTest.php | 120 ++++++++++++++++++ 5 files changed, 229 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php index 388b6dc2ea94..474bd99a8f13 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/CreateCustomerAddress.php @@ -67,6 +67,9 @@ public function __construct( */ public function execute(int $customerId, array $data): AddressInterface { + if (isset($data['country_code'])) { + $data['country_id'] = $data['country_code']; + } $this->validateData($data); /** @var AddressInterface $address */ diff --git a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/UpdateCustomerAddress.php b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/UpdateCustomerAddress.php index 65745a20bc8e..26e53c7c3a0a 100644 --- a/app/code/Magento/CustomerGraphQl/Model/Customer/Address/UpdateCustomerAddress.php +++ b/app/code/Magento/CustomerGraphQl/Model/Customer/Address/UpdateCustomerAddress.php @@ -66,6 +66,9 @@ public function __construct( */ public function execute(AddressInterface $address, array $data): void { + if (isset($data['country_code'])) { + $data['country_id'] = $data['country_code']; + } $this->validateData($data); $filteredData = array_diff_key($data, array_flip($this->restrictedKeys)); diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index d27debdc39c6..fa50ebeed09c 100644 --- a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls +++ b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls @@ -28,7 +28,8 @@ 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") + country_id: CountryCodeEnum @doc(description: "The customer's country") @deprecated(reason: "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") fax: String @doc(description: "The fax number") @@ -100,7 +101,8 @@ type CustomerAddress @doc(description: "CustomerAddress contains detailed inform customer_id: Int @doc(description: "The customer ID") region: CustomerAddressRegion @doc(description: "An object containing the region name, region code, and region ID") region_id: Int @doc(description: "A number that uniquely identifies the state, province, or other area") - 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 203e9b5cb42e..a065ab3f26e7 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -133,6 +133,105 @@ public function testCreateCustomerAddress() $this->assertCustomerAddressesFields($address, $newAddress); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testCreateCustomerAddressWithCountryCode() + { + $customerId = 1; + $newAddress = [ + 'region' => [ + 'region' => 'Arizona', + 'region_id' => 4, + 'region_code' => 'AZ' + ], + 'country_code' => 'US', + 'street' => ['Line 1 Street', 'Line 2'], + 'company' => 'Company name', + 'telephone' => '123456789', + 'fax' => '123123123', + 'postcode' => '7777', + 'city' => 'City Name', + 'firstname' => 'Adam', + 'lastname' => 'Phillis', + 'middlename' => 'A', + 'prefix' => 'Mr.', + 'suffix' => 'Jr.', + 'vat_id' => '1', + 'default_shipping' => true, + 'default_billing' => false + ]; + + $mutation + = <<graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); + $this->assertArrayHasKey('createCustomerAddress', $response); + $this->assertArrayHasKey('customer_id', $response['createCustomerAddress']); + $this->assertEquals($customerId, $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); + } + /** * @expectedException Exception * @expectedExceptionMessage The current customer isn't authorized. 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 9840236dc989..b83649061c33 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php @@ -77,6 +77,34 @@ public function testUpdateCustomerAddress() $this->assertCustomerAddressesFields($address, $updateAddress); } + /** + * @magentoApiDataFixture Magento/Customer/_files/customer.php + * @magentoApiDataFixture Magento/Customer/_files/customer_address.php + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testUpdateCustomerAddressWithCountryCode() + { + $userName = 'customer@example.com'; + $password = 'password'; + $customerId = 1; + $addressId = 1; + + $mutation = $this->getMutationWithCountryCode($addressId); + + $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->assertArrayHasKey('id', $response['updateCustomerAddress']); + + $address = $this->addressRepository->getById($addressId); + $this->assertEquals($address->getId(), $response['updateCustomerAddress']['id']); + $this->assertCustomerAddressesFields($address, $response['updateCustomerAddress']); + + $updateAddress = $this->getAddressDataCanadaCountry(); + $this->assertCustomerAddressesFields($address, $updateAddress); + } + /** * @expectedException Exception * @expectedExceptionMessage The current customer isn't authorized. @@ -405,6 +433,35 @@ private function getAddressData(): array ]; } + /** + * @return array + */ + private function getAddressDataCanadaCountry(): array + { + return [ + 'region' => [ + 'region' => 'Alberta', + 'region_id' => 66, + 'region_code' => 'AB' + ], + 'country_id' => 'CA', + 'street' => ['Line 1 Street', 'Line 2'], + 'company' => 'Company Name', + 'telephone' => '123456789', + 'fax' => '123123123', + 'postcode' => '7777', + 'city' => 'City Name', + 'firstname' => 'Adam', + 'lastname' => 'Phillis', + 'middlename' => 'A', + 'prefix' => 'Mr.', + 'suffix' => 'Jr.', + 'vat_id' => '1', + 'default_shipping' => true, + 'default_billing' => true + ]; + } + /** * @param int $addressId * @return string @@ -464,6 +521,69 @@ private function getMutation(int $addressId): string default_billing } } +MUTATION; + return $mutation; + } + + /** + * @param int $addressId + * @return string + */ + private function getMutationWithCountryCode(int $addressId): string + { + $updateAddress = $this->getAddressDataCanadaCountry(); + $defaultShippingText = $updateAddress['default_shipping'] ? "true" : "false"; + $defaultBillingText = $updateAddress['default_billing'] ? "true" : "false"; + + $mutation + = << Date: Thu, 3 Oct 2019 14:40:50 +0300 Subject: [PATCH 2/4] 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 = << Date: Wed, 16 Oct 2019 10:24:13 -0500 Subject: [PATCH 3/4] magento/graphql-ce#914: [Customer] Improve consistency of country field in customer address --- .../CustomerGraphQl/etc/schema.graphqls | 4 +- .../Customer/CreateCustomerAddressTest.php | 60 ++++--------- .../Customer/UpdateCustomerAddressTest.php | 89 +++++++++---------- 3 files changed, 59 insertions(+), 94 deletions(-) diff --git a/app/code/Magento/CustomerGraphQl/etc/schema.graphqls b/app/code/Magento/CustomerGraphQl/etc/schema.graphqls index 9ce2d61aa458..86ab39bbee25 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: "Deprecated, 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,7 @@ 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") @deprecated(reason: "Use country_code instead.") + 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") 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 9ccd3b0d46c7..bbe111c41db9 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerAddressTest.php @@ -49,7 +49,7 @@ public function testCreateCustomerAddress() 'region_id' => 4, 'region_code' => 'AZ' ], - 'country_id' => 'US', + 'country_code' => 'US', 'street' => ['Line 1 Street', 'Line 2'], 'company' => 'Company name', 'telephone' => '123456789', @@ -75,7 +75,7 @@ public function testCreateCustomerAddress() region_id: {$newAddress['region']['region_id']} region_code: "{$newAddress['region']['region_code']}" } - country_id: {$newAddress['country_id']} + country_code: {$newAddress['country_code']} street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"] company: "{$newAddress['company']}" telephone: "{$newAddress['telephone']}" @@ -98,7 +98,7 @@ public function testCreateCustomerAddress() region_id region_code } - country_id + country_code street company telephone @@ -134,10 +134,12 @@ public function testCreateCustomerAddress() } /** + * Test case for deprecated `country_id` field. + * * @magentoApiDataFixture Magento/Customer/_files/customer_without_addresses.php * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function testCreateCustomerAddressWithCountryCode() + public function testCreateCustomerAddressWithCountryId() { $newAddress = [ 'region' => [ @@ -145,7 +147,7 @@ public function testCreateCustomerAddressWithCountryCode() 'region_id' => 4, 'region_code' => 'AZ' ], - 'country_code' => 'US', + 'country_id' => 'US', 'street' => ['Line 1 Street', 'Line 2'], 'company' => 'Company name', 'telephone' => '123456789', @@ -171,7 +173,7 @@ public function testCreateCustomerAddressWithCountryCode() region_id: {$newAddress['region']['region_id']} region_code: "{$newAddress['region']['region_code']}" } - country_code: {$newAddress['country_code']} + country_id: {$newAddress['country_id']} street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"] company: "{$newAddress['company']}" telephone: "{$newAddress['telephone']}" @@ -187,28 +189,7 @@ public function testCreateCustomerAddressWithCountryCode() default_shipping: true default_billing: false }) { - id - customer_id - region { - region - region_id - region_code - } - country_code - street - company - telephone - fax - postcode - city - firstname - lastname - middlename - prefix - suffix - vat_id - default_shipping - default_billing + country_id } } MUTATION; @@ -218,15 +199,7 @@ public function testCreateCustomerAddressWithCountryCode() $response = $this->graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); $this->assertArrayHasKey('createCustomerAddress', $response); - $this->assertArrayHasKey('customer_id', $response['createCustomerAddress']); - $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']); - - $this->assertCustomerAddressesFields($address, $response['createCustomerAddress'], 'country_code'); - $this->assertCustomerAddressesFields($address, $newAddress, 'country_code'); + $this->assertEquals($newAddress['country_id'], $response['createCustomerAddress']['country_id']); } /** @@ -249,7 +222,7 @@ public function testCreateCustomerAddressIfUserIsNotAuthorized() region: { region_id: 1 } - country_id: US + country_code: US postcode: "9999" default_shipping: true default_billing: false @@ -278,7 +251,7 @@ public function testCreateCustomerAddressWithMissingAttribute() region: { region_id: 1 } - country_id: US + country_code: US street: ["Line 1 Street","Line 2"] company: "Company name" telephone: "123456789" @@ -310,7 +283,7 @@ public function testCreateCustomerAddressWithRedundantStreetLine() 'region_id' => 4, 'region_code' => 'AZ' ], - 'country_id' => 'US', + 'country_code' => 'US', 'street' => ['Line 1 Street', 'Line 2', 'Line 3'], 'company' => 'Company name', 'telephone' => '123456789', @@ -336,7 +309,7 @@ public function testCreateCustomerAddressWithRedundantStreetLine() region_id: {$newAddress['region']['region_id']} region_code: "{$newAddress['region']['region_code']}" } - country_id: {$newAddress['country_id']} + country_code: {$newAddress['country_code']} street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}","{$newAddress['street'][2]}"] company: "{$newAddress['company']}" telephone: "{$newAddress['telephone']}" @@ -413,12 +386,11 @@ public function invalidInputDataProvider() */ private function assertCustomerAddressesFields( AddressInterface $address, - array $actualResponse, - string $countryFieldName = 'country_id' + array $actualResponse ): void { /** @var $addresses */ $assertionMap = [ - ['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()], + ['response_field' => 'country_code', '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 d92c003c080e..025a994ec4ae 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php @@ -77,30 +77,53 @@ public function testUpdateCustomerAddress() } /** + * Test case for deprecated `country_id` field. + * * @magentoApiDataFixture Magento/Customer/_files/customer.php * @magentoApiDataFixture Magento/Customer/_files/customer_address.php * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function testUpdateCustomerAddressWithCountryCode() + public function testUpdateCustomerAddressWithCountryId() { $userName = 'customer@example.com'; $password = 'password'; $addressId = 1; - $mutation = $this->getMutationWithCountryCode($addressId); + $updateAddress = $this->getAddressData(); + + $mutation = $mutation + = <<graphQlMutation($mutation, [], '', $this->getCustomerAuthHeaders($userName, $password)); $this->assertArrayHasKey('updateCustomerAddress', $response); - $this->assertArrayHasKey('customer_id', $response['updateCustomerAddress']); - $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'], 'country_code'); - - $updateAddress = $this->getAddressDataCanadaCountry(); - $this->assertCustomerAddressesFields($address, $updateAddress, 'country_code'); + $this->assertEquals($updateAddress['country_code'], $response['updateCustomerAddress']['country_id']); } /** @@ -162,12 +185,11 @@ public function testUpdateCustomerAddressWithMissingAttribute() */ private function assertCustomerAddressesFields( AddressInterface $address, - $actualResponse, - string $countryFieldName = 'country_id' + $actualResponse ): void { /** @var $addresses */ $assertionMap = [ - ['response_field' => $countryFieldName, 'expected_value' => $address->getCountryId()], + ['response_field' => 'country_code', '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()], @@ -218,7 +240,7 @@ public function testUpdateCustomerAddressWithMissingId() region_id: {$updateAddress['region']['region_id']} region_code: "{$updateAddress['region']['region_code']}" } - country_id: {$updateAddress['country_id']} + country_code: {$updateAddress['country_code']} street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"] company: "{$updateAddress['company']}" telephone: "{$updateAddress['telephone']}" @@ -274,7 +296,7 @@ public function testUpdateCustomerAddressWithInvalidIdType() region_id: {$updateAddress['region']['region_id']} region_code: "{$updateAddress['region']['region_code']}" } - country_id: {$updateAddress['country_id']} + country_code: {$updateAddress['country_code']} street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"] company: "{$updateAddress['company']}" telephone: "{$updateAddress['telephone']}" @@ -410,35 +432,6 @@ private function getCustomerAuthHeaders(string $email, string $password): array * @return array */ private function getAddressData(): array - { - return [ - 'region' => [ - 'region' => 'Alaska', - 'region_id' => 2, - 'region_code' => 'AK' - ], - 'country_id' => 'US', - 'street' => ['Line 1 Street', 'Line 2'], - 'company' => 'Company Name', - 'telephone' => '123456789', - 'fax' => '123123123', - 'postcode' => '7777', - 'city' => 'City Name', - 'firstname' => 'Adam', - 'lastname' => 'Phillis', - 'middlename' => 'A', - 'prefix' => 'Mr.', - 'suffix' => 'Jr.', - 'vat_id' => '1', - 'default_shipping' => true, - 'default_billing' => true - ]; - } - - /** - * @return array - */ - private function getAddressDataCanadaCountry(): array { return [ 'region' => [ @@ -483,7 +476,7 @@ private function getMutation(int $addressId): string region_id: {$updateAddress['region']['region_id']} region_code: "{$updateAddress['region']['region_code']}" } - country_id: {$updateAddress['country_id']} + country_code: {$updateAddress['country_code']} street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"] company: "{$updateAddress['company']}" telephone: "{$updateAddress['telephone']}" @@ -506,7 +499,7 @@ private function getMutation(int $addressId): string region_id region_code } - country_id + country_code street company telephone From 21749ccc5c3de8d82e6902893f1180f9cda804a5 Mon Sep 17 00:00:00 2001 From: Lena Orobei Date: Wed, 16 Oct 2019 10:44:54 -0500 Subject: [PATCH 4/4] magento/graphql-ce#914: [Customer] Improve consistency of country field in customer address --- .../Customer/UpdateCustomerAddressTest.php | 63 ------------------- 1 file changed, 63 deletions(-) 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 025a994ec4ae..e214d770920d 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/UpdateCustomerAddressTest.php @@ -516,69 +516,6 @@ private function getMutation(int $addressId): string default_billing } } -MUTATION; - return $mutation; - } - - /** - * @param int $addressId - * @return string - */ - private function getMutationWithCountryCode(int $addressId): string - { - $updateAddress = $this->getAddressDataCanadaCountry(); - $defaultShippingText = $updateAddress['default_shipping'] ? 'true' : 'false'; - $defaultBillingText = $updateAddress['default_billing'] ? 'true' : 'false'; - - $mutation - = <<