Skip to content

Commit

Permalink
MC-31586: Customer address is duplicated after setBillingAddressOnCar…
Browse files Browse the repository at this point in the history
…t GraphQL mutation.

- Added fix and api functional test
  • Loading branch information
prabhuram93 committed Mar 17, 2020
1 parent 756aedf commit 8a92d80
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ private function createBillingAddress(

$customerId = $context->getUserId();
// need to save address only for registered user and if save_in_address_book = true
if ((0 !== $customerId
if (0 !== $customerId
&& isset($addressInput['save_in_address_book'])
&& (bool)$addressInput['save_in_address_book'] && $sameAsShipping !== true) === true
&& (bool)$addressInput['save_in_address_book'] && !$sameAsShipping
) {
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,89 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
}
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
*/
public function testSetBillingAddressAndPlaceOrder()
{
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
$query = <<<QUERY
mutation {
setBillingAddressOnCart(
input: {
cart_id: "$maskedQuoteId"
billing_address: {
same_as_shipping: true
address: {
firstname: "test firstname"
lastname: "test lastname"
company: "test company"
street: ["test street 1", "test street 2"]
city: "test city"
region: "AZ"
postcode: "887766"
country_code: "US"
telephone: "88776655"
save_in_address_book: true
}
}
}
) {
cart {
billing_address {
firstname
lastname
company
street
city
postcode
telephone
country {
code
label
}
__typename
}
}
}
}
QUERY;
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
$this->graphQlMutation(
$this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'),
[],
'',
$this->getHeaderMap()
);
$this->graphQlMutation(
$this->getSetPaymentMethodQuery(
$maskedQuoteId,
'checkmo'
),
[],
'',
$this->getHeaderMap()
);
$this->graphQlMutation(
$this->getPlaceOrderQuery($maskedQuoteId),
[],
'',
$this->getHeaderMap()
);
$customer = $this->customerRepository->get('[email protected]');
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();

self::assertCount(1, $addresses);
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
foreach ($addresses as $address) {
$this->customerAddressRepository->delete($address);
}
}

/**
* @magentoApiDataFixture Magento/Customer/_files/customer.php
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
Expand Down Expand Up @@ -1111,4 +1194,88 @@ private function assignQuoteToCustomer(
$this->quoteResource->save($quote);
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
}

/**
* @param string $maskedQuoteId
* @param string $shippingMethodCode
* @param string $shippingCarrierCode
* @return string
*/
private function getSetShippingMethodsQuery(
string $maskedQuoteId,
string $shippingMethodCode,
string $shippingCarrierCode
): string {
return <<<QUERY
mutation {
setShippingMethodsOnCart(input:
{
cart_id: "$maskedQuoteId",
shipping_methods: [{
carrier_code: "$shippingCarrierCode"
method_code: "$shippingMethodCode"
}]
}) {
cart {
shipping_addresses {
selected_shipping_method {
carrier_code
method_code
carrier_title
method_title
amount {
value
currency
}
}
}
}
}
}
QUERY;
}

/**
* @param string $maskedQuoteId
* @param string $methodCode
* @return string
*/
private function getSetPaymentMethodQuery(
string $maskedQuoteId,
string $methodCode
) : string {
return <<<QUERY
mutation {
setPaymentMethodOnCart(input: {
cart_id: "$maskedQuoteId"
payment_method: {
code: "$methodCode"
}
}) {
cart {
selected_payment_method {
code
}
}
}
}
QUERY;
}

/**
* @param string $maskedQuoteId
* @return string
*/
private function getPlaceOrderQuery(string $maskedQuoteId): string
{
return <<<QUERY
mutation {
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
order {
order_number
}
}
}
QUERY;
}
}

0 comments on commit 8a92d80

Please sign in to comment.