Skip to content

Commit 87e825b

Browse files
committed
Merge remote-tracking branch 'origin/MC-21814-cart' into pr-2.3.4
2 parents 12eae75 + 1c3a76b commit 87e825b

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use Magento\GraphQl\Model\Query\ContextInterface;
1616
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1717
use Magento\Quote\Api\CartManagementInterface;
18+
use Magento\Quote\Model\QuoteIdMaskFactory;
19+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
20+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
1821

1922
/**
2023
* Get cart for the customer
@@ -31,16 +34,39 @@ class CustomerCart implements ResolverInterface
3134
*/
3235
private $cartManagement;
3336

37+
/**
38+
* @var QuoteIdMaskFactory
39+
*/
40+
private $quoteIdMaskFactory;
41+
42+
/**
43+
* @var QuoteIdMaskResourceModel
44+
*/
45+
private $quoteIdMaskResourceModel;
46+
/**
47+
* @var QuoteIdToMaskedQuoteIdInterface
48+
*/
49+
private $quoteIdToMaskedQuoteId;
50+
3451
/**
3552
* @param CreateEmptyCartForCustomer $createEmptyCartForCustomer
3653
* @param CartManagementInterface $cartManagement
54+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
55+
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
56+
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
3757
*/
3858
public function __construct(
3959
CreateEmptyCartForCustomer $createEmptyCartForCustomer,
40-
CartManagementInterface $cartManagement
60+
CartManagementInterface $cartManagement,
61+
QuoteIdMaskFactory $quoteIdMaskFactory,
62+
QuoteIdMaskResourceModel $quoteIdMaskResourceModel,
63+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
4164
) {
4265
$this->createEmptyCartForCustomer = $createEmptyCartForCustomer;
4366
$this->cartManagement = $cartManagement;
67+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
68+
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
69+
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
4470
}
4571

4672
/**
@@ -61,6 +87,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6187
$cart = $this->cartManagement->getCartForCustomer($currentUserId);
6288
}
6389

90+
$maskedId = $this->quoteIdToMaskedQuoteId->execute((int) $cart->getId());
91+
if (empty($maskedId)) {
92+
$quoteIdMask = $this->quoteIdMaskFactory->create();
93+
$quoteIdMask->setQuoteId((int) $cart->getId());
94+
$this->quoteIdMaskResourceModel->save($quoteIdMask);
95+
}
96+
6497
return [
6598
'model' => $cart
6699
];

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testGetActiveCustomerCart()
5454
$this->assertNotEmpty($response['customerCart']['items']);
5555
$this->assertEquals(2, $response['customerCart']['total_quantity']);
5656
$this->assertArrayHasKey('id', $response['customerCart']);
57+
$this->assertNotEmpty($response['customerCart']['id']);
5758
$this->assertEquals($maskedQuoteId, $response['customerCart']['id']);
5859
$this->assertEquals(
5960
$quantity,
@@ -62,6 +63,26 @@ public function testGetActiveCustomerCart()
6263
);
6364
}
6465

66+
/**
67+
* Query for an existing customer cart with no masked quote id
68+
*
69+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
70+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
71+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart_without_maskedQuoteId.php
72+
*/
73+
public function testGetLoggedInCustomerCartWithoutMaskedQuoteId()
74+
{
75+
$customerCartQuery = $this->getCustomerCartQuery();
76+
$response = $this->graphQlQuery($customerCartQuery, [], '', $this->getHeaderMap());
77+
$this->assertArrayHasKey('customerCart', $response);
78+
$this->assertArrayHasKey('items', $response['customerCart']);
79+
$this->assertEmpty($response['customerCart']['items']);
80+
$this->assertEquals(0, $response['customerCart']['total_quantity']);
81+
$this->assertArrayHasKey('id', $response['customerCart']);
82+
$this->assertNotEmpty($response['customerCart']['id']);
83+
$this->assertNotNull($response['customerCart']['id']);
84+
}
85+
6586
/**
6687
* Query for customer cart for a user with no existing active cart
6788
*
@@ -76,6 +97,7 @@ public function testGetNewCustomerCart()
7697
$this->assertArrayHasKey('customerCart', $response);
7798
$this->assertArrayHasKey('id', $response['customerCart']);
7899
$this->assertNotNull($response['customerCart']['id']);
100+
$this->assertNotEmpty($response['customerCart']['id']);
79101
$this->assertEmpty($response['customerCart']['items']);
80102
$this->assertEquals(0, $response['customerCart']['total_quantity']);
81103
}
@@ -106,6 +128,7 @@ public function testGetCustomerCartAfterTokenRevoked()
106128
$this->assertArrayHasKey('customerCart', $response);
107129
$this->assertArrayHasKey('id', $response['customerCart']);
108130
$this->assertNotNull($response['customerCart']['id']);
131+
$this->assertNotEmpty($response['customerCart']['id']);
109132
$this->revokeCustomerToken();
110133
$customerCartQuery = $this->getCustomerCartQuery();
111134
$this->expectExceptionMessage(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Quote\Api\CartManagementInterface;
9+
use Magento\Quote\Api\CartRepositoryInterface;
10+
use Magento\Quote\Model\QuoteIdMaskFactory;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
13+
/** @var CartManagementInterface $cartManagement */
14+
$cartManagement = Bootstrap::getObjectManager()->get(CartManagementInterface::class);
15+
/** @var CartRepositoryInterface $cartRepository */
16+
$cartRepository = Bootstrap::getObjectManager()->get(CartRepositoryInterface::class);
17+
/** @var QuoteIdMaskFactory $quoteIdMaskFactory */
18+
$quoteIdMaskFactory = Bootstrap::getObjectManager()->get(QuoteIdMaskFactory::class);
19+
20+
$cartId = $cartManagement->createEmptyCartForCustomer(1);
21+
$cart = $cartRepository->get($cartId);
22+
$cartRepository->save($cart);

0 commit comments

Comments
 (0)