Skip to content

Commit 2393a36

Browse files
authored
ENGCOM-4713: graphQl-470: Creating/getting Cart queries should support Store context #501
2 parents 7e6e098 + 3dcd584 commit 2393a36

File tree

9 files changed

+367
-17
lines changed

9 files changed

+367
-17
lines changed

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Quote\Api\CartRepositoryInterface;
1414
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
1515
use Magento\Quote\Model\Quote;
16+
use Magento\Store\Model\StoreManagerInterface;
1617

1718
/**
1819
* Get cart
@@ -29,16 +30,24 @@ class GetCartForUser
2930
*/
3031
private $cartRepository;
3132

33+
/**
34+
* @var StoreManagerInterface
35+
*/
36+
private $storeManager;
37+
3238
/**
3339
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
3440
* @param CartRepositoryInterface $cartRepository
41+
* @param StoreManagerInterface $storeManager
3542
*/
3643
public function __construct(
3744
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
38-
CartRepositoryInterface $cartRepository
45+
CartRepositoryInterface $cartRepository,
46+
StoreManagerInterface $storeManager
3947
) {
4048
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
4149
$this->cartRepository = $cartRepository;
50+
$this->storeManager = $storeManager;
4251
}
4352

4453
/**
@@ -75,6 +84,15 @@ public function execute(string $cartHash, ?int $customerId): Quote
7584
);
7685
}
7786

87+
if ((int)$cart->getStoreId() !== (int)$this->storeManager->getStore()->getId()) {
88+
throw new GraphQlNoSuchEntityException(
89+
__(
90+
'Wrong store code specified for cart "%masked_cart_id"',
91+
['masked_cart_id' => $cartHash]
92+
)
93+
);
94+
}
95+
7896
$cartCustomerId = (int)$cart->getCustomerId();
7997

8098
/* Guest cart, allow operations */

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

Lines changed: 102 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
namespace Magento\GraphQl\Quote\Customer;
99

1010
use Magento\Integration\Api\CustomerTokenServiceInterface;
11+
use Magento\Quote\Model\QuoteFactory;
12+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
13+
use Magento\Quote\Model\QuoteIdMaskFactory;
14+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1115
use Magento\TestFramework\Helper\Bootstrap;
1216
use Magento\TestFramework\TestCase\GraphQlAbstract;
1317
use Magento\Quote\Api\GuestCartRepositoryInterface;
@@ -27,35 +31,124 @@ class CreateEmptyCartTest extends GraphQlAbstract
2731
*/
2832
private $customerTokenService;
2933

34+
/**
35+
* @var QuoteResource
36+
*/
37+
private $quoteResource;
38+
39+
/**
40+
* @var QuoteFactory
41+
*/
42+
private $quoteFactory;
43+
44+
/**
45+
* @var MaskedQuoteIdToQuoteIdInterface
46+
*/
47+
private $maskedQuoteIdToQuoteId;
48+
49+
/**
50+
* @var QuoteIdMaskFactory
51+
*/
52+
private $quoteIdMaskFactory;
53+
54+
/**
55+
* @var string
56+
*/
57+
private $maskedQuoteId;
58+
3059
protected function setUp()
3160
{
3261
$objectManager = Bootstrap::getObjectManager();
3362
$this->guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class);
3463
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
64+
$this->quoteResource = $objectManager->get(QuoteResource::class);
65+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
66+
$this->maskedQuoteIdToQuoteId = $objectManager->get(MaskedQuoteIdToQuoteIdInterface::class);
67+
$this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class);
3568
}
3669

3770
/**
3871
* @magentoApiDataFixture Magento/Customer/_files/customer.php
3972
*/
4073
public function testCreateEmptyCart()
4174
{
42-
$query = <<<QUERY
43-
mutation {
44-
createEmptyCart
45-
}
46-
QUERY;
75+
$query = $this->getQuery();
76+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMapWithCustomerToken());
4777

48-
$customerToken = $this->customerTokenService->createCustomerAccessToken('customer@example.com', 'password');
49-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
78+
self::assertArrayHasKey('createEmptyCart', $response);
79+
self::assertNotEmpty($response['createEmptyCart']);
80+
81+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
82+
$this->maskedQuoteId = $response['createEmptyCart'];
83+
84+
self::assertNotNull($guestCart->getId());
85+
self::assertEquals(1, $guestCart->getCustomer()->getId());
86+
self::assertEquals('default', $guestCart->getStore()->getCode());
87+
}
5088

89+
/**
90+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
91+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
92+
*/
93+
public function testCreateEmptyCartWithNotDefaultStore()
94+
{
95+
$query = $this->getQuery();
96+
97+
$headerMap = $this->getHeaderMapWithCustomerToken();
98+
$headerMap['Store'] = 'fixture_second_store';
5199
$response = $this->graphQlQuery($query, [], '', $headerMap);
52100

53101
self::assertArrayHasKey('createEmptyCart', $response);
102+
self::assertNotEmpty($response['createEmptyCart']);
54103

55-
$maskedCartId = $response['createEmptyCart'];
56-
$guestCart = $this->guestCartRepository->get($maskedCartId);
104+
/* guestCartRepository is used for registered customer to get the cart hash */
105+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
106+
$this->maskedQuoteId = $response['createEmptyCart'];
57107

58108
self::assertNotNull($guestCart->getId());
59109
self::assertEquals(1, $guestCart->getCustomer()->getId());
110+
self::assertEquals('fixture_second_store', $guestCart->getStore()->getCode());
111+
}
112+
113+
/**
114+
* @return string
115+
*/
116+
private function getQuery(): string
117+
{
118+
return <<<QUERY
119+
mutation {
120+
createEmptyCart
121+
}
122+
QUERY;
123+
}
124+
125+
/**
126+
* @param string $username
127+
* @param string $password
128+
* @return array
129+
*/
130+
private function getHeaderMapWithCustomerToken(
131+
string $username = 'customer@example.com',
132+
string $password = 'password'
133+
): array {
134+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
135+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
136+
return $headerMap;
137+
}
138+
139+
public function tearDown()
140+
{
141+
if (null !== $this->maskedQuoteId) {
142+
$quoteId = $this->maskedQuoteIdToQuoteId->execute($this->maskedQuoteId);
143+
144+
$quote = $this->quoteFactory->create();
145+
$this->quoteResource->load($quote, $quoteId);
146+
$this->quoteResource->delete($quote);
147+
148+
$quoteIdMask = $this->quoteIdMaskFactory->create();
149+
$quoteIdMask->setQuoteId($quoteId)
150+
->delete();
151+
}
152+
parent::tearDown();
60153
}
61154
}

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,58 @@ public function testGetInactiveCart()
124124
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
125125
}
126126

127+
/**
128+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_customer_not_default_store.php
129+
*/
130+
public function testGetCartWithNotDefaultStore()
131+
{
132+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1_not_default_store');
133+
$query = $this->getQuery($maskedQuoteId);
134+
135+
$headerMap = $this->getHeaderMap();
136+
$headerMap['Store'] = 'fixture_second_store';
137+
138+
$response = $this->graphQlQuery($query, [], '', $headerMap);
139+
140+
self::assertArrayHasKey('cart', $response);
141+
self::assertArrayHasKey('items', $response['cart']);
142+
}
143+
144+
/**
145+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
146+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
147+
*
148+
* @expectedException \Exception
149+
* @expectedExceptionMessage Wrong store code specified for cart
150+
*/
151+
public function testGetCartWithWrongStore()
152+
{
153+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1');
154+
$query = $this->getQuery($maskedQuoteId);
155+
156+
$headerMap = $this->getHeaderMap();
157+
$headerMap['Store'] = 'fixture_second_store';
158+
159+
$this->graphQlQuery($query, [], '', $headerMap);
160+
}
161+
162+
/**
163+
* @magentoApiDataFixture Magento/Checkout/_files/active_quote_customer_not_default_store.php
164+
*
165+
* @expectedException \Exception
166+
* @expectedExceptionMessage Store code not_existing_store does not exist
167+
*/
168+
public function testGetCartWithNotExistingStore()
169+
{
170+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_order_1_not_default_store');
171+
$query = $this->getQuery($maskedQuoteId);
172+
173+
$headerMap = $this->getHeaderMap();
174+
$headerMap['Store'] = 'not_existing_store';
175+
176+
$this->graphQlQuery($query, [], '', $headerMap);
177+
}
178+
127179
/**
128180
* @param string $maskedQuoteId
129181
* @return string

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Guest/CreateEmptyCartTest.php

Lines changed: 86 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
namespace Magento\GraphQl\Quote\Guest;
99

10+
use Magento\Quote\Model\QuoteFactory;
11+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
12+
use Magento\Quote\Model\QuoteIdMaskFactory;
13+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
1014
use Magento\TestFramework\Helper\Bootstrap;
1115
use Magento\TestFramework\TestCase\GraphQlAbstract;
1216
use Magento\Quote\Api\GuestCartRepositoryInterface;
@@ -21,27 +25,102 @@ class CreateEmptyCartTest extends GraphQlAbstract
2125
*/
2226
private $guestCartRepository;
2327

28+
/**
29+
* @var QuoteResource
30+
*/
31+
private $quoteResource;
32+
33+
/**
34+
* @var QuoteFactory
35+
*/
36+
private $quoteFactory;
37+
38+
/**
39+
* @var MaskedQuoteIdToQuoteIdInterface
40+
*/
41+
private $maskedQuoteIdToQuoteId;
42+
43+
/**
44+
* @var QuoteIdMaskFactory
45+
*/
46+
private $quoteIdMaskFactory;
47+
48+
/**
49+
* @var string
50+
*/
51+
private $maskedQuoteId;
52+
2453
protected function setUp()
2554
{
2655
$objectManager = Bootstrap::getObjectManager();
2756
$this->guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class);
57+
$this->quoteResource = $objectManager->get(QuoteResource::class);
58+
$this->quoteFactory = $objectManager->get(QuoteFactory::class);
59+
$this->maskedQuoteIdToQuoteId = $objectManager->get(MaskedQuoteIdToQuoteIdInterface::class);
60+
$this->quoteIdMaskFactory = $objectManager->get(QuoteIdMaskFactory::class);
2861
}
2962

3063
public function testCreateEmptyCart()
3164
{
32-
$query = <<<QUERY
33-
mutation {
34-
createEmptyCart
35-
}
36-
QUERY;
65+
$query = $this->getQuery();
3766
$response = $this->graphQlQuery($query);
3867

3968
self::assertArrayHasKey('createEmptyCart', $response);
69+
self::assertNotEmpty($response['createEmptyCart']);
70+
71+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
72+
$this->maskedQuoteId = $response['createEmptyCart'];
73+
74+
self::assertNotNull($guestCart->getId());
75+
self::assertNull($guestCart->getCustomer()->getId());
76+
self::assertEquals('default', $guestCart->getStore()->getCode());
77+
}
78+
79+
/**
80+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
81+
*/
82+
public function testCreateEmptyCartWithNotDefaultStore()
83+
{
84+
$query = $this->getQuery();
85+
$headerMap = ['Store' => 'fixture_second_store'];
86+
$response = $this->graphQlQuery($query, [], '', $headerMap);
87+
88+
self::assertArrayHasKey('createEmptyCart', $response);
89+
self::assertNotEmpty($response['createEmptyCart']);
4090

41-
$maskedCartId = $response['createEmptyCart'];
42-
$guestCart = $this->guestCartRepository->get($maskedCartId);
91+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
92+
$this->maskedQuoteId = $response['createEmptyCart'];
4393

4494
self::assertNotNull($guestCart->getId());
4595
self::assertNull($guestCart->getCustomer()->getId());
96+
self::assertSame('fixture_second_store', $guestCart->getStore()->getCode());
97+
}
98+
99+
/**
100+
* @return string
101+
*/
102+
private function getQuery(): string
103+
{
104+
return <<<QUERY
105+
mutation {
106+
createEmptyCart
107+
}
108+
QUERY;
109+
}
110+
111+
public function tearDown()
112+
{
113+
if (null !== $this->maskedQuoteId) {
114+
$quoteId = $this->maskedQuoteIdToQuoteId->execute($this->maskedQuoteId);
115+
116+
$quote = $this->quoteFactory->create();
117+
$this->quoteResource->load($quote, $quoteId);
118+
$this->quoteResource->delete($quote);
119+
120+
$quoteIdMask = $this->quoteIdMaskFactory->create();
121+
$quoteIdMask->setQuoteId($quoteId)
122+
->delete();
123+
}
124+
parent::tearDown();
46125
}
47126
}

0 commit comments

Comments
 (0)