Skip to content

Commit 8a92d80

Browse files
author
Prabhu Ram
committed
MC-31586: Customer address is duplicated after setBillingAddressOnCart GraphQL mutation.
- Added fix and api functional test
1 parent 756aedf commit 8a92d80

File tree

2 files changed

+169
-2
lines changed

2 files changed

+169
-2
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/SetBillingAddressOnCart.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ private function createBillingAddress(
118118

119119
$customerId = $context->getUserId();
120120
// need to save address only for registered user and if save_in_address_book = true
121-
if ((0 !== $customerId
121+
if (0 !== $customerId
122122
&& isset($addressInput['save_in_address_book'])
123-
&& (bool)$addressInput['save_in_address_book'] && $sameAsShipping !== true) === true
123+
&& (bool)$addressInput['save_in_address_book'] && !$sameAsShipping
124124
) {
125125
$this->saveQuoteAddressToCustomerAddressBook->execute($billingAddress, $customerId);
126126
}

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

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,89 @@ public function testSetNewBillingAddressWithSaveInAddressBook()
871871
}
872872
}
873873

874+
/**
875+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
876+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
877+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
878+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
879+
*/
880+
public function testSetBillingAddressAndPlaceOrder()
881+
{
882+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
883+
$query = <<<QUERY
884+
mutation {
885+
setBillingAddressOnCart(
886+
input: {
887+
cart_id: "$maskedQuoteId"
888+
billing_address: {
889+
same_as_shipping: true
890+
address: {
891+
firstname: "test firstname"
892+
lastname: "test lastname"
893+
company: "test company"
894+
street: ["test street 1", "test street 2"]
895+
city: "test city"
896+
region: "AZ"
897+
postcode: "887766"
898+
country_code: "US"
899+
telephone: "88776655"
900+
save_in_address_book: true
901+
}
902+
}
903+
}
904+
) {
905+
cart {
906+
billing_address {
907+
firstname
908+
lastname
909+
company
910+
street
911+
city
912+
postcode
913+
telephone
914+
country {
915+
code
916+
label
917+
}
918+
__typename
919+
}
920+
}
921+
}
922+
}
923+
QUERY;
924+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
925+
$this->graphQlMutation(
926+
$this->getSetShippingMethodsQuery($maskedQuoteId, 'flatrate', 'flatrate'),
927+
[],
928+
'',
929+
$this->getHeaderMap()
930+
);
931+
$this->graphQlMutation(
932+
$this->getSetPaymentMethodQuery(
933+
$maskedQuoteId,
934+
'checkmo'
935+
),
936+
[],
937+
'',
938+
$this->getHeaderMap()
939+
);
940+
$this->graphQlMutation(
941+
$this->getPlaceOrderQuery($maskedQuoteId),
942+
[],
943+
'',
944+
$this->getHeaderMap()
945+
);
946+
$customer = $this->customerRepository->get('customer@example.com');
947+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('parent_id', $customer->getId())->create();
948+
$addresses = $this->customerAddressRepository->getList($searchCriteria)->getItems();
949+
950+
self::assertCount(1, $addresses);
951+
self::assertArrayHasKey('cart', $response['setBillingAddressOnCart']);
952+
foreach ($addresses as $address) {
953+
$this->customerAddressRepository->delete($address);
954+
}
955+
}
956+
874957
/**
875958
* @magentoApiDataFixture Magento/Customer/_files/customer.php
876959
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
@@ -1111,4 +1194,88 @@ private function assignQuoteToCustomer(
11111194
$this->quoteResource->save($quote);
11121195
return $this->quoteIdToMaskedId->execute((int)$quote->getId());
11131196
}
1197+
1198+
/**
1199+
* @param string $maskedQuoteId
1200+
* @param string $shippingMethodCode
1201+
* @param string $shippingCarrierCode
1202+
* @return string
1203+
*/
1204+
private function getSetShippingMethodsQuery(
1205+
string $maskedQuoteId,
1206+
string $shippingMethodCode,
1207+
string $shippingCarrierCode
1208+
): string {
1209+
return <<<QUERY
1210+
mutation {
1211+
setShippingMethodsOnCart(input:
1212+
{
1213+
cart_id: "$maskedQuoteId",
1214+
shipping_methods: [{
1215+
carrier_code: "$shippingCarrierCode"
1216+
method_code: "$shippingMethodCode"
1217+
}]
1218+
}) {
1219+
cart {
1220+
shipping_addresses {
1221+
selected_shipping_method {
1222+
carrier_code
1223+
method_code
1224+
carrier_title
1225+
method_title
1226+
amount {
1227+
value
1228+
currency
1229+
}
1230+
}
1231+
}
1232+
}
1233+
}
1234+
}
1235+
QUERY;
1236+
}
1237+
1238+
/**
1239+
* @param string $maskedQuoteId
1240+
* @param string $methodCode
1241+
* @return string
1242+
*/
1243+
private function getSetPaymentMethodQuery(
1244+
string $maskedQuoteId,
1245+
string $methodCode
1246+
) : string {
1247+
return <<<QUERY
1248+
mutation {
1249+
setPaymentMethodOnCart(input: {
1250+
cart_id: "$maskedQuoteId"
1251+
payment_method: {
1252+
code: "$methodCode"
1253+
}
1254+
}) {
1255+
cart {
1256+
selected_payment_method {
1257+
code
1258+
}
1259+
}
1260+
}
1261+
}
1262+
QUERY;
1263+
}
1264+
1265+
/**
1266+
* @param string $maskedQuoteId
1267+
* @return string
1268+
*/
1269+
private function getPlaceOrderQuery(string $maskedQuoteId): string
1270+
{
1271+
return <<<QUERY
1272+
mutation {
1273+
placeOrder(input: {cart_id: "{$maskedQuoteId}"}) {
1274+
order {
1275+
order_number
1276+
}
1277+
}
1278+
}
1279+
QUERY;
1280+
}
11141281
}

0 commit comments

Comments
 (0)