Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

graphQl-914: [Customer] Improve consistency of country field in custo… #958

Merged
merged 5 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ public function __construct(
*/
public function execute(int $customerId, array $data): AddressInterface
{
if (isset($data['country_code'])) {
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
$data['country_id'] = $data['country_code'];
}
$this->validateData($data);

/** @var AddressInterface $address */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions app/code/Magento/CustomerGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")

Input types do not support @deprecated directive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

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")
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
= <<<MUTATION
mutation {
createCustomerAddress(input: {
region: {
region: "{$newAddress['region']['region']}"
region_id: {$newAddress['region']['region_id']}
region_code: "{$newAddress['region']['region_code']}"
}
country_code: {$newAddress['country_code']}
street: ["{$newAddress['street'][0]}","{$newAddress['street'][1]}"]
company: "{$newAddress['company']}"
telephone: "{$newAddress['telephone']}"
fax: "{$newAddress['fax']}"
postcode: "{$newAddress['postcode']}"
city: "{$newAddress['city']}"
firstname: "{$newAddress['firstname']}"
lastname: "{$newAddress['lastname']}"
middlename: "{$newAddress['middlename']}"
prefix: "{$newAddress['prefix']}"
suffix: "{$newAddress['suffix']}"
vat_id: "{$newAddress['vat_id']}"
default_shipping: true
default_billing: false
}) {
id
customer_id
region {
region
region_id
region_code
}
country_id
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
street
company
telephone
fax
postcode
city
firstname
lastname
middlename
prefix
suffix
vat_id
default_shipping
default_billing
}
}
MUTATION;

$userName = 'customer@example.com';
$password = 'password';

$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->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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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',
lenaorobei marked this conversation as resolved.
Show resolved Hide resolved
'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
Expand Down Expand Up @@ -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
= <<<MUTATION
mutation {
updateCustomerAddress(id: {$addressId}, input: {
region: {
region: "{$updateAddress['region']['region']}"
region_id: {$updateAddress['region']['region_id']}
region_code: "{$updateAddress['region']['region_code']}"
}
country_code: {$updateAddress['country_id']}
street: ["{$updateAddress['street'][0]}","{$updateAddress['street'][1]}"]
company: "{$updateAddress['company']}"
telephone: "{$updateAddress['telephone']}"
fax: "{$updateAddress['fax']}"
postcode: "{$updateAddress['postcode']}"
city: "{$updateAddress['city']}"
firstname: "{$updateAddress['firstname']}"
lastname: "{$updateAddress['lastname']}"
middlename: "{$updateAddress['middlename']}"
prefix: "{$updateAddress['prefix']}"
suffix: "{$updateAddress['suffix']}"
vat_id: "{$updateAddress['vat_id']}"
default_shipping: {$defaultShippingText}
default_billing: {$defaultBillingText}
}) {
id
customer_id
region {
region
region_id
region_code
}
country_id
street
company
telephone
fax
postcode
city
firstname
lastname
middlename
prefix
suffix
vat_id
default_shipping
default_billing
}
}
MUTATION;
return $mutation;
}
Expand Down