Skip to content

Commit c4acbff

Browse files
committed
GraphQL-470: Creating/getting Cart queries should support Store context
1 parent 04ca01a commit c4acbff

File tree

3 files changed

+150
-55
lines changed

3 files changed

+150
-55
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function execute(string $cartHash, ?int $customerId): Quote
8585
}
8686

8787
if ((int)$cart->getStoreId() !== (int)$this->storeManager->getStore()->getId()) {
88-
throw new GraphQlAuthorizationException(
88+
throw new GraphQlNoSuchEntityException(
8989
__(
9090
'Wrong store code specified for cart "%masked_cart_id"',
9191
['masked_cart_id' => $cartHash]

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

Lines changed: 79 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77

88
namespace Magento\GraphQl\Quote\Customer;
99

10-
use Magento\Framework\App\ResourceConnection;
1110
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;
1215
use Magento\TestFramework\Helper\Bootstrap;
1316
use Magento\TestFramework\TestCase\GraphQlAbstract;
1417
use Magento\Quote\Api\GuestCartRepositoryInterface;
@@ -29,44 +32,58 @@ class CreateEmptyCartTest extends GraphQlAbstract
2932
private $customerTokenService;
3033

3134
/**
32-
* @var ResourceConnection
35+
* @var QuoteResource
3336
*/
34-
private $resourceConnection;
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;
3558

3659
protected function setUp()
3760
{
3861
$objectManager = Bootstrap::getObjectManager();
3962
$this->guestCartRepository = $objectManager->get(GuestCartRepositoryInterface::class);
4063
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
41-
$this->resourceConnection = $objectManager->get(ResourceConnection::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);
4268
}
4369

4470
/**
4571
* @magentoApiDataFixture Magento/Customer/_files/customer.php
4672
*/
4773
public function testCreateEmptyCart()
4874
{
49-
$query = <<<QUERY
50-
mutation {
51-
createEmptyCart
52-
}
53-
QUERY;
54-
55-
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password');
56-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
57-
58-
$response = $this->graphQlQuery($query, [], '', $headerMap);
75+
$query = $this->getQuery();
76+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMapWithCustomerToken());
5977

6078
self::assertArrayHasKey('createEmptyCart', $response);
79+
self::assertNotEmpty($response['createEmptyCart']);
6180

62-
$maskedCartId = $response['createEmptyCart'];
63-
$guestCart = $this->guestCartRepository->get($maskedCartId);
81+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
82+
$this->maskedQuoteId = $response['createEmptyCart'];
6483

6584
self::assertNotNull($guestCart->getId());
6685
self::assertEquals(1, $guestCart->getCustomer()->getId());
67-
self::assertSame('default', $guestCart->getStore()->getCode());
68-
69-
$this->deleteCreatedQuote($guestCart->getId());
86+
self::assertEquals('default', $guestCart->getStore()->getCode());
7087
}
7188

7289
/**
@@ -75,40 +92,63 @@ public function testCreateEmptyCart()
7592
*/
7693
public function testCreateEmptyCartWithNotDefaultStore()
7794
{
78-
$query = <<<QUERY
79-
mutation {
80-
createEmptyCart
81-
}
82-
QUERY;
83-
84-
$customerToken = $this->customerTokenService->createCustomerAccessToken('[email protected]', 'password');
85-
$headerMap = ['Authorization' => 'Bearer ' . $customerToken, 'Store' => 'fixture_second_store'];
95+
$query = $this->getQuery();
8696

97+
$headerMap = $this->getHeaderMapWithCustomerToken();
98+
$headerMap['Store'] = 'fixture_second_store';
8799
$response = $this->graphQlQuery($query, [], '', $headerMap);
88100

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

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

95108
self::assertNotNull($guestCart->getId());
96109
self::assertEquals(1, $guestCart->getCustomer()->getId());
97-
self::assertSame('fixture_second_store', $guestCart->getStore()->getCode());
110+
self::assertEquals('fixture_second_store', $guestCart->getStore()->getCode());
111+
}
98112

99-
$this->deleteCreatedQuote($guestCart->getId());
113+
/**
114+
* @return string
115+
*/
116+
private function getQuery(): string
117+
{
118+
return <<<QUERY
119+
mutation {
120+
createEmptyCart
121+
}
122+
QUERY;
100123
}
101124

102125
/**
103-
* Delete active quote for customer by customer id.
104-
* This is needed to have ability to create new quote for another store and not return the active one.
105-
* @see QuoteManagement::createCustomerCart
106-
*
107-
* @param $quoteId
126+
* @param string $username
127+
* @param string $password
128+
* @return array
108129
*/
109-
private function deleteCreatedQuote($quoteId)
130+
private function getHeaderMapWithCustomerToken(
131+
string $username = '[email protected]',
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()
110140
{
111-
$connection = $this->resourceConnection->getConnection();
112-
$connection->query('DELETE FROM quote WHERE entity_id = ' . $quoteId);
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();
113153
}
114154
}

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

Lines changed: 70 additions & 15 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,51 +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']);
4070

41-
$maskedCartId = $response['createEmptyCart'];
42-
$guestCart = $this->guestCartRepository->get($maskedCartId);
71+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
72+
$this->maskedQuoteId = $response['createEmptyCart'];
4373

4474
self::assertNotNull($guestCart->getId());
4575
self::assertNull($guestCart->getCustomer()->getId());
76+
self::assertEquals('default', $guestCart->getStore()->getCode());
4677
}
4778

4879
/**
4980
* @magentoApiDataFixture Magento/Store/_files/second_store.php
5081
*/
5182
public function testCreateEmptyCartWithNotDefaultStore()
5283
{
53-
$query = <<<QUERY
54-
mutation {
55-
createEmptyCart
56-
}
57-
QUERY;
84+
$query = $this->getQuery();
5885
$headerMap = ['Store' => 'fixture_second_store'];
59-
6086
$response = $this->graphQlQuery($query, [], '', $headerMap);
6187

6288
self::assertArrayHasKey('createEmptyCart', $response);
89+
self::assertNotEmpty($response['createEmptyCart']);
6390

64-
$maskedCartId = $response['createEmptyCart'];
65-
$guestCart = $this->guestCartRepository->get($maskedCartId);
91+
$guestCart = $this->guestCartRepository->get($response['createEmptyCart']);
92+
$this->maskedQuoteId = $response['createEmptyCart'];
6693

6794
self::assertNotNull($guestCart->getId());
6895
self::assertNull($guestCart->getCustomer()->getId());
6996
self::assertSame('fixture_second_store', $guestCart->getStore()->getCode());
7097
}
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();
125+
}
71126
}

0 commit comments

Comments
 (0)