7
7
8
8
namespace Magento \GraphQl \Quote \Customer ;
9
9
10
- use Magento \Catalog \Api \ProductRepositoryInterface ;
10
+ use Magento \GraphQl \Quote \GetMaskedQuoteIdByReservedOrderId ;
11
+ use Magento \GraphQl \Quote \GetQuoteItemIdByReservedQuoteIdAndSku ;
11
12
use Magento \Integration \Api \CustomerTokenServiceInterface ;
12
- use Magento \Quote \Model \QuoteFactory ;
13
- use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
14
- use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
15
13
use Magento \TestFramework \Helper \Bootstrap ;
16
14
use Magento \TestFramework \TestCase \GraphQlAbstract ;
17
15
@@ -26,46 +24,37 @@ class RemoveItemFromCartTest extends GraphQlAbstract
26
24
private $ customerTokenService ;
27
25
28
26
/**
29
- * @var QuoteResource
27
+ * @var GetMaskedQuoteIdByReservedOrderId
30
28
*/
31
- private $ quoteResource ;
29
+ private $ getMaskedQuoteIdByReservedOrderId ;
32
30
33
31
/**
34
- * @var QuoteFactory
32
+ * @var GetQuoteItemIdByReservedQuoteIdAndSku
35
33
*/
36
- private $ quoteFactory ;
37
-
38
- /**
39
- * @var QuoteIdToMaskedQuoteIdInterface
40
- */
41
- private $ quoteIdToMaskedId ;
42
-
43
- /**
44
- * @var ProductRepositoryInterface
45
- */
46
- private $ productRepository ;
34
+ private $ getQuoteItemIdByReservedQuoteIdAndSku ;
47
35
48
36
protected function setUp ()
49
37
{
50
38
$ objectManager = Bootstrap::getObjectManager ();
51
- $ this ->quoteResource = $ objectManager ->get (QuoteResource::class);
52
- $ this ->quoteFactory = $ objectManager ->get (QuoteFactory::class);
53
- $ this ->quoteIdToMaskedId = $ objectManager ->get (QuoteIdToMaskedQuoteIdInterface::class);
54
39
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
55
- $ this ->productRepository = $ objectManager ->get (ProductRepositoryInterface::class);
40
+ $ this ->getMaskedQuoteIdByReservedOrderId = $ objectManager ->get (GetMaskedQuoteIdByReservedOrderId::class);
41
+ $ this ->getQuoteItemIdByReservedQuoteIdAndSku = $ objectManager ->get (
42
+ GetQuoteItemIdByReservedQuoteIdAndSku::class
43
+ );
56
44
}
57
45
58
46
/**
59
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
47
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
48
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
49
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
50
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
60
51
*/
61
52
public function testRemoveItemFromCart ()
62
53
{
63
- $ quote = $ this ->quoteFactory ->create ();
64
- $ this ->quoteResource ->load ($ quote , 'test_order_1 ' , 'reserved_order_id ' );
65
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ quote ->getId ());
66
- $ itemId = (int )$ quote ->getItemByProduct ($ this ->productRepository ->get ('simple ' ))->getId ();
54
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
55
+ $ itemId = $ this ->getQuoteItemIdByReservedQuoteIdAndSku ->execute ('test_quote ' , 'simple_product ' );
67
56
68
- $ query = $ this ->prepareMutationQuery ($ maskedQuoteId , $ itemId );
57
+ $ query = $ this ->getQuery ($ maskedQuoteId , $ itemId );
69
58
$ response = $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
70
59
71
60
$ this ->assertArrayHasKey ('removeItemFromCart ' , $ response );
@@ -80,105 +69,24 @@ public function testRemoveItemFromCart()
80
69
*/
81
70
public function testRemoveItemFromNonExistentCart ()
82
71
{
83
- $ query = $ this ->prepareMutationQuery ('non_existent_masked_id ' , 1 );
72
+ $ query = $ this ->getQuery ('non_existent_masked_id ' , 1 );
84
73
$ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
85
74
}
86
75
87
76
/**
88
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
77
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
78
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
79
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
80
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
89
81
*/
90
82
public function testRemoveNonExistentItem ()
91
83
{
92
- $ quote = $ this ->quoteFactory ->create ();
93
- $ this ->quoteResource ->load ($ quote , 'test_order_1 ' , 'reserved_order_id ' );
94
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ quote ->getId ());
84
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
95
85
$ notExistentItemId = 999 ;
96
86
97
87
$ this ->expectExceptionMessage ("Cart doesn't contain the {$ notExistentItemId } item. " );
98
88
99
- $ query = $ this ->prepareMutationQuery ($ maskedQuoteId , $ notExistentItemId );
100
- $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
101
- }
102
-
103
- /**
104
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
105
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
106
- */
107
- public function testRemoveItemIfItemIsNotBelongToCart ()
108
- {
109
- $ firstQuote = $ this ->quoteFactory ->create ();
110
- $ this ->quoteResource ->load ($ firstQuote , 'test_order_1 ' , 'reserved_order_id ' );
111
- $ firstQuoteMaskedId = $ this ->quoteIdToMaskedId ->execute ((int )$ firstQuote ->getId ());
112
-
113
- $ secondQuote = $ this ->quoteFactory ->create ();
114
- $ this ->quoteResource ->load (
115
- $ secondQuote ,
116
- 'test_order_with_virtual_product_without_address ' ,
117
- 'reserved_order_id '
118
- );
119
- $ secondQuote ->setCustomerId (1 );
120
- $ this ->quoteResource ->save ($ secondQuote );
121
- $ secondQuoteItemId = (int )$ secondQuote
122
- ->getItemByProduct ($ this ->productRepository ->get ('virtual-product ' ))
123
- ->getId ();
124
-
125
- $ this ->expectExceptionMessage ("Cart doesn't contain the {$ secondQuoteItemId } item. " );
126
-
127
- $ query = $ this ->prepareMutationQuery ($ firstQuoteMaskedId , $ secondQuoteItemId );
128
- $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
129
- }
130
-
131
- /**
132
- * @magentoApiDataFixture Magento/Customer/_files/customer.php
133
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
134
- */
135
- public function testRemoveItemFromGuestCart ()
136
- {
137
- $ guestQuote = $ this ->quoteFactory ->create ();
138
- $ this ->quoteResource ->load (
139
- $ guestQuote ,
140
- 'test_order_with_virtual_product_without_address ' ,
141
- 'reserved_order_id '
142
- );
143
- $ guestQuoteMaskedId = $ this ->quoteIdToMaskedId ->execute ((int )$ guestQuote ->getId ());
144
- $ guestQuoteItemId = (int )$ guestQuote
145
- ->getItemByProduct ($ this ->productRepository ->get ('virtual-product ' ))
146
- ->getId ();
147
-
148
- $ this ->expectExceptionMessage (
149
- "The current user cannot perform operations on cart \"$ guestQuoteMaskedId \""
150
- );
151
-
152
- $ query = $ this ->prepareMutationQuery ($ guestQuoteMaskedId , $ guestQuoteItemId );
153
- $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
154
- }
155
-
156
- /**
157
- * @magentoApiDataFixture Magento/Customer/_files/three_customers.php
158
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
159
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_saved.php
160
- */
161
- public function testRemoveItemFromAnotherCustomerCart ()
162
- {
163
- $ anotherCustomerQuote = $ this ->quoteFactory ->create ();
164
- $ this ->quoteResource ->load (
165
- $ anotherCustomerQuote ,
166
- 'test_order_with_virtual_product_without_address ' ,
167
- 'reserved_order_id '
168
- );
169
- $ anotherCustomerQuote ->setCustomerId (2 );
170
- $ this ->quoteResource ->save ($ anotherCustomerQuote );
171
-
172
- $ anotherCustomerQuoteMaskedId = $ this ->quoteIdToMaskedId ->execute ((int )$ anotherCustomerQuote ->getId ());
173
- $ anotherCustomerQuoteItemId = (int )$ anotherCustomerQuote
174
- ->getItemByProduct ($ this ->productRepository ->get ('virtual-product ' ))
175
- ->getId ();
176
-
177
- $ this ->expectExceptionMessage (
178
- "The current user cannot perform operations on cart \"$ anotherCustomerQuoteMaskedId \""
179
- );
180
-
181
- $ query = $ this ->prepareMutationQuery ($ anotherCustomerQuoteMaskedId , $ anotherCustomerQuoteItemId );
89
+ $ query = $ this ->getQuery ($ maskedQuoteId , $ notExistentItemId );
182
90
$ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
183
91
}
184
92
@@ -226,12 +134,77 @@ public function dataProviderUpdateWithMissedRequiredParameters(): array
226
134
];
227
135
}
228
136
137
+ /**
138
+ * _security
139
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
140
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
141
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
142
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
143
+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_virtual_product_and_address.php
144
+ */
145
+ public function testRemoveItemIfItemIsNotBelongToCart ()
146
+ {
147
+ $ firstQuoteMaskedId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
148
+ $ secondQuoteItemId = $ this ->getQuoteItemIdByReservedQuoteIdAndSku ->execute (
149
+ 'test_order_with_virtual_product ' ,
150
+ 'virtual-product '
151
+ );
152
+
153
+ $ this ->expectExceptionMessage ("Cart doesn't contain the {$ secondQuoteItemId } item. " );
154
+
155
+ $ query = $ this ->getQuery ($ firstQuoteMaskedId , $ secondQuoteItemId );
156
+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
157
+ }
158
+
159
+ /**
160
+ * _security
161
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
162
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
163
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
164
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
165
+ */
166
+ public function testRemoveItemFromGuestCart ()
167
+ {
168
+ $ guestQuoteMaskedId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
169
+ $ guestQuoteItemId = $ this ->getQuoteItemIdByReservedQuoteIdAndSku ->execute ('test_quote ' , 'simple_product ' );
170
+
171
+ $ this ->expectExceptionMessage (
172
+ "The current user cannot perform operations on cart \"$ guestQuoteMaskedId \""
173
+ );
174
+
175
+ $ query = $ this ->getQuery ($ guestQuoteMaskedId , $ guestQuoteItemId );
176
+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
177
+ }
178
+
179
+ /**
180
+ * _security
181
+ * @magentoApiDataFixture Magento/Customer/_files/three_customers.php
182
+ * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
183
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
184
+ * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
185
+ */
186
+ public function testRemoveItemFromAnotherCustomerCart ()
187
+ {
188
+ $ anotherCustomerQuoteMaskedId = $ this ->getMaskedQuoteIdByReservedOrderId ->execute ('test_quote ' );
189
+ $ anotherCustomerQuoteItemId = $ this ->getQuoteItemIdByReservedQuoteIdAndSku ->execute (
190
+ 'test_quote ' ,
191
+ 'simple_product '
192
+ );
193
+
194
+ $ this ->expectExceptionMessage (
195
+ "The current user cannot perform operations on cart \"$ anotherCustomerQuoteMaskedId \""
196
+ );
197
+
198
+ $ query = $ this ->getQuery ($ anotherCustomerQuoteMaskedId , $ anotherCustomerQuoteItemId );
199
+ $ this ->
graphQlQuery (
$ query, [],
'' ,
$ this ->
getHeaderMap (
'[email protected] ' ));
200
+ }
201
+
229
202
/**
230
203
* @param string $maskedQuoteId
231
204
* @param int $itemId
232
205
* @return string
233
206
*/
234
- private function prepareMutationQuery (string $ maskedQuoteId , int $ itemId ): string
207
+ private function getQuery (string $ maskedQuoteId , int $ itemId ): string
235
208
{
236
209
return <<<QUERY
237
210
mutation {
0 commit comments