Skip to content

Commit d4e5894

Browse files
andrewbessAndrii Beziazychnyi
authored andcommitted
M2CE-30469: The tests and logic fixes
1 parent 274cfca commit d4e5894

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/MergeCarts.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function resolve(
8989
}
9090
$currentUserId = $context->getUserId();
9191

92-
if (empty($args['destination_cart_id'])) {
92+
if (!isset($args['destination_cart_id'])) {
9393
try {
9494
$cart = $this->customerCartResolver->resolve($currentUserId);
9595
} catch (CouldNotSaveException $exception) {
@@ -101,6 +101,12 @@ public function resolve(
101101
$customerMaskedCartId = $this->quoteIdToMaskedQuoteId->execute(
102102
(int) $cart->getId()
103103
);
104+
} else {
105+
if (empty($args['destination_cart_id'])) {
106+
throw new GraphQlInputException(__(
107+
'The parameter "destination_cart_id" cannot be empty'
108+
));
109+
}
104110
}
105111

106112
$guestMaskedCartId = $args['source_cart_id'];

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ public function testMergeCartsWithEmptySourceCartId()
192192
*/
193193
public function testMergeCartsWithEmptyDestinationCartId()
194194
{
195+
$this->expectException(\Exception::class);
196+
$this->expectExceptionMessage('The parameter "destination_cart_id" cannot be empty');
197+
195198
$guestQuote = $this->quoteFactory->create();
196199
$this->quoteResource->load(
197200
$guestQuote,
@@ -206,6 +209,54 @@ public function testMergeCartsWithEmptyDestinationCartId()
206209
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
207210
}
208211

212+
/**
213+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
214+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
215+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
216+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
217+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
218+
*/
219+
public function testMergeCartsWithoutDestinationCartId()
220+
{
221+
$guestQuote = $this->quoteFactory->create();
222+
$this->quoteResource->load(
223+
$guestQuote,
224+
'test_order_with_virtual_product_without_address',
225+
'reserved_order_id'
226+
);
227+
$guestQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$guestQuote->getId());
228+
$query = $this->getCartMergeMutationWithoutDestinationCartId(
229+
$guestQuoteMaskedId
230+
);
231+
$mergeResponse = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
232+
233+
self::assertArrayHasKey('mergeCarts', $mergeResponse);
234+
$cartResponse = $mergeResponse['mergeCarts'];
235+
self::assertArrayHasKey('items', $cartResponse);
236+
self::assertCount(2, $cartResponse['items']);
237+
238+
$customerQuote = $this->quoteFactory->create();
239+
$this->quoteResource->load($customerQuote, 'test_quote', 'reserved_order_id');
240+
$customerQuoteMaskedId = $this->quoteIdToMaskedId->execute((int)$customerQuote->getId());
241+
242+
$cartResponse = $this->graphQlMutation(
243+
$this->getCartQuery($customerQuoteMaskedId),
244+
[],
245+
'',
246+
$this->getHeaderMap()
247+
);
248+
249+
self::assertArrayHasKey('cart', $cartResponse);
250+
self::assertArrayHasKey('items', $cartResponse['cart']);
251+
self::assertCount(2, $cartResponse['cart']['items']);
252+
$item1 = $cartResponse['cart']['items'][0];
253+
self::assertArrayHasKey('quantity', $item1);
254+
self::assertEquals(2, $item1['quantity']);
255+
$item2 = $cartResponse['cart']['items'][1];
256+
self::assertArrayHasKey('quantity', $item2);
257+
self::assertEquals(1, $item2['quantity']);
258+
}
259+
209260
/**
210261
* Add simple product to cart
211262
*
@@ -253,6 +304,31 @@ private function getCartMergeMutation(string $guestQuoteMaskedId, string $custom
253304
QUERY;
254305
}
255306

307+
/**
308+
* Create the mergeCart mutation
309+
*
310+
* @param string $guestQuoteMaskedId
311+
* @return string
312+
*/
313+
private function getCartMergeMutationWithoutDestinationCartId(
314+
string $guestQuoteMaskedId
315+
): string {
316+
return <<<QUERY
317+
mutation {
318+
mergeCarts(
319+
source_cart_id: "{$guestQuoteMaskedId}"
320+
){
321+
items {
322+
quantity
323+
product {
324+
sku
325+
}
326+
}
327+
}
328+
}
329+
QUERY;
330+
}
331+
256332
/**
257333
* Get cart query
258334
*

0 commit comments

Comments
 (0)