7
7
8
8
namespace Magento \GraphQl \Quote \Customer ;
9
9
10
+ use Magento \GraphQl \Quote \GetMaskedQuoteIdByReservedOrderId ;
10
11
use Magento \Integration \Api \CustomerTokenServiceInterface ;
11
- use Magento \Quote \Model \QuoteFactory ;
12
- use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
13
- use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
14
12
use Magento \TestFramework \Helper \Bootstrap ;
15
13
use Magento \TestFramework \TestCase \GraphQlAbstract ;
16
14
@@ -25,26 +23,14 @@ class CartTotalsTest extends GraphQlAbstract
25
23
private $ customerTokenService ;
26
24
27
25
/**
28
- * @var QuoteResource
26
+ * @var GetMaskedQuoteIdByReservedOrderId
29
27
*/
30
- private $ quoteResource ;
31
-
32
- /**
33
- * @var QuoteFactory
34
- */
35
- private $ quoteFactory ;
36
-
37
- /**
38
- * @var QuoteIdToMaskedQuoteIdInterface
39
- */
40
- private $ quoteIdToMaskedId ;
28
+ private $ getMaskedQuoteIdByReservedOrderId ;
41
29
42
30
protected function setUp ()
43
31
{
44
32
$ objectManager = Bootstrap::getObjectManager ();
45
- $ this ->quoteResource = $ objectManager ->get (QuoteResource::class);
46
- $ this ->quoteFactory = $ objectManager ->get (QuoteFactory::class);
47
- $ this ->quoteIdToMaskedId = $ objectManager ->get (QuoteIdToMaskedQuoteIdInterface::class);
33
+ $ this ->getMaskedQuoteIdByReservedOrderId = $ objectManager ->get (GetMaskedQuoteIdByReservedOrderId::class);
48
34
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
49
35
}
50
36
@@ -60,9 +46,9 @@ protected function setUp()
60
46
*/
61
47
public function testGetCartTotalsWithTaxApplied ()
62
48
{
63
- $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_quote ' );
64
- $ query = $ this ->getCartTotalsGraphqlQuery ($ maskedQuoteId );
65
- $ response = $ this ->sendRequestWithToken ($ query );
49
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId -> execute ('test_quote ' );
50
+ $ query = $ this ->getQuery ($ maskedQuoteId );
51
+ $ response = $ this ->graphQlQuery ($ query, [], '' , $ this -> getHeaderMap () );
66
52
67
53
self ::assertArrayHasKey ('prices ' , $ response ['cart ' ]);
68
54
$ pricesResponse = $ response ['cart ' ]['prices ' ];
@@ -88,9 +74,9 @@ public function testGetCartTotalsWithTaxApplied()
88
74
*/
89
75
public function testGetTotalsWithNoTaxApplied ()
90
76
{
91
- $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_quote ' );
92
- $ query = $ this ->getCartTotalsGraphqlQuery ($ maskedQuoteId );
93
- $ response = $ this ->sendRequestWithToken ($ query );
77
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId -> execute ('test_quote ' );
78
+ $ query = $ this ->getQuery ($ maskedQuoteId );
79
+ $ response = $ this ->graphQlQuery ($ query, [], '' , $ this -> getHeaderMap () );
94
80
95
81
$ pricesResponse = $ response ['cart ' ]['prices ' ];
96
82
self ::assertEquals (20 , $ pricesResponse ['grand_total ' ]['value ' ]);
@@ -111,9 +97,9 @@ public function testGetTotalsWithNoTaxApplied()
111
97
*/
112
98
public function testGetCartTotalsWithNoAddressSet ()
113
99
{
114
- $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_quote ' );
115
- $ query = $ this ->getCartTotalsGraphqlQuery ($ maskedQuoteId );
116
- $ response = $ this ->sendRequestWithToken ($ query );
100
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId -> execute ('test_quote ' );
101
+ $ query = $ this ->getQuery ($ maskedQuoteId );
102
+ $ response = $ this ->graphQlQuery ($ query, [], '' , $ this -> getHeaderMap () );
117
103
118
104
$ pricesResponse = $ response ['cart ' ]['prices ' ];
119
105
self ::assertEquals (20 , $ pricesResponse ['grand_total ' ]['value ' ]);
@@ -123,13 +109,57 @@ public function testGetCartTotalsWithNoAddressSet()
123
109
self ::assertEmpty ($ pricesResponse ['applied_taxes ' ]);
124
110
}
125
111
112
+ /**
113
+ * _security
114
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
115
+ * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
116
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
117
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
118
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
119
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
120
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
121
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
122
+ */
123
+ public function testGetTotalsFromGuestCart ()
124
+ {
125
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
126
+ $ query = $ this ->getQuery ($ maskedQuoteId );
127
+
128
+ $ this ->expectExceptionMessage (
129
+ "The current user cannot perform operations on cart \"$ maskedQuoteId \""
130
+ );
131
+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
132
+ }
133
+
134
+ /**
135
+ * _security
136
+ * @magentoApiDataFixture Magento/Customer/_files/three_customers.php
137
+ * @magentoApiDataFixture Magento/GraphQl/Tax/_files/tax_rule_for_region_1.php
138
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
139
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/apply_tax_for_simple_product.php
140
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
141
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
142
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_shipping_address.php
143
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
144
+ */
145
+ public function testGetTotalsFromAnotherCustomerCart ()
146
+ {
147
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
148
+ $ query = $ this ->getQuery ($ maskedQuoteId );
149
+
150
+ $ this ->expectExceptionMessage (
151
+ "The current user cannot perform operations on cart \"$ maskedQuoteId \""
152
+ );
153
+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ('customer3@search.example.com ' ));
154
+ }
155
+
126
156
/**
127
157
* Generates GraphQl query for retrieving cart totals
128
158
*
129
159
* @param string $maskedQuoteId
130
160
* @return string
131
161
*/
132
- private function getCartTotalsGraphqlQuery (string $ maskedQuoteId ): string
162
+ private function getQuery (string $ maskedQuoteId ): string
133
163
{
134
164
return <<<QUERY
135
165
{
@@ -165,30 +195,14 @@ private function getCartTotalsGraphqlQuery(string $maskedQuoteId): string
165
195
}
166
196
167
197
/**
168
- * @param string $reversedQuoteId
169
- * @return string
170
- */
171
- private function getMaskedQuoteIdByReversedQuoteId (string $ reversedQuoteId ): string
172
- {
173
- $ quote = $ this ->quoteFactory ->create ();
174
- $ this ->quoteResource ->load ($ quote , $ reversedQuoteId , 'reserved_order_id ' );
175
-
176
- return $ this ->quoteIdToMaskedId ->execute ((int )$ quote ->getId ());
177
- }
178
-
179
- /**
180
- * Sends a GraphQL request with using a bearer token
181
- *
182
- * @param string $query
198
+ * @param string $username
199
+ * @param string $password
183
200
* @return array
184
- * @throws \Magento\Framework\Exception\AuthenticationException
185
201
*/
186
- private function sendRequestWithToken (string $ query ): array
202
+ private function getHeaderMap (string $ username = ' customer@example.com ' , string $ password = ' password ' ): array
187
203
{
188
-
189
- $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ('customer@example.com ' , 'password ' );
204
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
190
205
$ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
191
-
192
- return $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
206
+ return $ headerMap ;
193
207
}
194
208
}
0 commit comments