7
7
8
8
namespace Magento \GraphQl \Quote ;
9
9
10
- use Magento \Framework \App \Config \ScopeConfigInterface ;
11
10
use Magento \Integration \Api \CustomerTokenServiceInterface ;
12
- use Magento \OfflinePayments \Model \Banktransfer ;
13
- use Magento \OfflinePayments \Model \Cashondelivery ;
14
11
use Magento \OfflinePayments \Model \Checkmo ;
15
- use Magento \OfflinePayments \Model \Purchaseorder ;
16
- use Magento \Quote \Model \Quote ;
12
+ use Magento \Quote \Model \QuoteFactory ;
17
13
use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
18
14
use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
19
15
use Magento \TestFramework \Helper \Bootstrap ;
20
- use Magento \TestFramework \ObjectManager ;
21
16
use Magento \TestFramework \TestCase \GraphQlAbstract ;
22
- use Magento \Framework \App \Cache \TypeListInterface ;
23
- use Magento \Config \Model \ResourceModel \Config ;
24
17
25
18
/**
26
19
* Test for setting payment methods on cart
27
20
*/
28
21
class SetPaymentMethodOnCartTest extends GraphQlAbstract
29
22
{
30
- private const OFFLINE_METHOD_CODES = [
31
- Checkmo::PAYMENT_METHOD_CHECKMO_CODE ,
32
- Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE ,
33
- Cashondelivery::PAYMENT_METHOD_CASHONDELIVERY_CODE ,
34
- Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE ,
35
- ];
36
-
37
23
/**
38
24
* @var CustomerTokenServiceInterface
39
25
*/
@@ -45,139 +31,56 @@ class SetPaymentMethodOnCartTest extends GraphQlAbstract
45
31
private $ quoteResource ;
46
32
47
33
/**
48
- * @var Quote
34
+ * @var QuoteFactory
49
35
*/
50
- private $ quote ;
36
+ private $ quoteFactory ;
51
37
52
38
/**
53
39
* @var QuoteIdToMaskedQuoteIdInterface
54
40
*/
55
41
private $ quoteIdToMaskedId ;
56
42
57
- /**
58
- * @var Config
59
- */
60
- private $ config ;
61
-
62
- /**
63
- * @var TypeListInterface
64
- */
65
- private $ cacheList ;
66
-
67
43
/**
68
44
* @inheritdoc
69
45
*/
70
46
protected function setUp ()
71
47
{
72
48
$ objectManager = Bootstrap::getObjectManager ();
73
- $ this ->quoteResource = $ objectManager ->create (QuoteResource::class);
74
- $ this ->quote = $ objectManager ->create (Quote ::class);
75
- $ this ->quoteIdToMaskedId = $ objectManager ->create (QuoteIdToMaskedQuoteIdInterface::class);
49
+ $ this ->quoteResource = $ objectManager ->get (QuoteResource::class);
50
+ $ this ->quoteFactory = $ objectManager ->get (QuoteFactory ::class);
51
+ $ this ->quoteIdToMaskedId = $ objectManager ->get (QuoteIdToMaskedQuoteIdInterface::class);
76
52
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
77
- $ this ->config = $ objectManager ->get (Config::class);
78
- $ this ->cacheList = $ objectManager ->get (TypeListInterface::class);
79
-
80
- foreach (static ::OFFLINE_METHOD_CODES as $ offlineMethodCode ) {
81
- $ this ->config ->saveConfig (
82
- 'payment/ ' . $ offlineMethodCode . '/active ' ,
83
- '1 ' ,
84
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
85
- 0
86
- );
87
- }
88
- $ this ->cacheList ->cleanType ('config ' );
89
53
}
90
54
91
55
/**
92
- * @inheritdoc
93
- */
94
- protected function tearDown ()
95
- {
96
- foreach (static ::OFFLINE_METHOD_CODES as $ offlineMethodCode ) {
97
- //Never no disable checkmo method
98
- if ($ offlineMethodCode === Checkmo::PAYMENT_METHOD_CHECKMO_CODE ) {
99
- continue ;
100
- }
101
- $ this ->config ->saveConfig (
102
- 'payment/ ' . $ offlineMethodCode . '/active ' ,
103
- '0 ' ,
104
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
105
- 0
106
- );
107
- }
108
- $ this ->cacheList ->cleanType ('config ' );
109
- }
110
-
111
- /**
112
- * @param string $methodCode
113
- * @dataProvider dataProviderOfflinePaymentMethods
114
56
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
115
57
*/
116
- public function testSetPaymentMethodOnCart (string $ methodCode )
58
+ public function testSetPaymentMethodOnCart ()
117
59
{
118
- /** @var \Magento\Config\Model\ResourceModel\Config $config */
119
- $ config = ObjectManager::getInstance ()->get (\Magento \Config \Model \ResourceModel \Config::class);
120
- $ config ->saveConfig (
121
- 'payment/ ' . $ methodCode . '/active ' ,
122
- 1 ,
123
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
124
- 0
125
- );
126
-
127
- $ this ->quoteResource ->load (
128
- $ this ->quote ,
129
- 'test_order_1 ' ,
130
- 'reserved_order_id '
131
- );
132
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
133
-
134
- $ query = $ this ->prepareMutationQuery (
135
- $ maskedQuoteId ,
136
- $ methodCode
137
- );
60
+ $ methodCode = Checkmo::PAYMENT_METHOD_CHECKMO_CODE ;
61
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_order_1 ' );
138
62
63
+ $ query = $ this ->prepareMutationQuery ($ maskedQuoteId , $ methodCode );
139
64
$ response = $ this ->sendRequestWithToken ($ query );
140
65
141
- $ this ->assertArrayHasKey ('setPaymentMethodOnCart ' , $ response );
142
- $ this ->assertArrayHasKey ('cart ' , $ response ['setPaymentMethodOnCart ' ]);
143
- $ this ->assertEquals ($ maskedQuoteId , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['cart_id ' ]);
144
- $ this ->assertArrayHasKey ('payment_method ' , $ response ['setPaymentMethodOnCart ' ]['cart ' ]);
145
- $ this ->assertEquals ($ methodCode , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['payment_method ' ]['code ' ]);
146
- }
147
-
148
- public function dataProviderOfflinePaymentMethods (): array
149
- {
150
- $ methods = [];
151
- foreach (static ::OFFLINE_METHOD_CODES as $ offlineMethodCode ) {
152
- //Purchase order requires additional input and is tested separately
153
- if ($ offlineMethodCode === Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE ) {
154
- continue ;
155
- }
156
- $ methods [] = [$ offlineMethodCode ];
157
- }
158
-
159
- return $ methods ;
66
+ self ::assertArrayHasKey ('setPaymentMethodOnCart ' , $ response );
67
+ self ::assertArrayHasKey ('cart ' , $ response ['setPaymentMethodOnCart ' ]);
68
+ self ::assertEquals ($ maskedQuoteId , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['cart_id ' ]);
69
+ self ::assertArrayHasKey ('selected_payment_method ' , $ response ['setPaymentMethodOnCart ' ]['cart ' ]);
70
+ self ::assertEquals ($ methodCode , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['selected_payment_method ' ]['code ' ]);
160
71
}
161
72
162
73
/**
163
74
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
75
+ * @expectedException \Exception
76
+ * @expectedExceptionMessage The requested Payment Method is not available.
164
77
*/
165
78
public function testSetNonExistingPaymentMethod ()
166
79
{
167
- $ paymentMethod = 'noway ' ;
168
- $ this ->quoteResource ->load (
169
- $ this ->quote ,
170
- 'test_order_1 ' ,
171
- 'reserved_order_id '
172
- );
173
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
174
-
175
- $ query = $ this ->prepareMutationQuery (
176
- $ maskedQuoteId ,
177
- $ paymentMethod
178
- );
80
+ $ methodCode = 'noway ' ;
81
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_order_1 ' );
179
82
180
- $ this ->expectExceptionMessage ( ' The requested Payment Method is not available. ' );
83
+ $ query = $ this ->prepareMutationQuery ( $ maskedQuoteId , $ methodCode );
181
84
$ this ->sendRequestWithToken ($ query );
182
85
}
183
86
@@ -186,18 +89,10 @@ public function testSetNonExistingPaymentMethod()
186
89
*/
187
90
public function testSetPaymentMethodByGuestToCustomerCart ()
188
91
{
189
- $ paymentMethod = 'checkmo ' ;
190
- $ this ->quoteResource ->load (
191
- $ this ->quote ,
192
- 'test_order_1 ' ,
193
- 'reserved_order_id '
194
- );
195
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
92
+ $ methodCode = 'checkmo ' ;
93
+ $ maskedQuoteId = $ this ->getMaskedQuoteIdByReversedQuoteId ('test_order_1 ' );
196
94
197
- $ query = $ this ->prepareMutationQuery (
198
- $ maskedQuoteId ,
199
- $ paymentMethod
200
- );
95
+ $ query = $ this ->prepareMutationQuery ($ maskedQuoteId , $ methodCode );
201
96
202
97
$ this ->expectExceptionMessage (
203
98
"The current user cannot perform operations on cart \"$ maskedQuoteId \""
@@ -206,84 +101,6 @@ public function testSetPaymentMethodByGuestToCustomerCart()
206
101
$ this ->graphQlQuery ($ query );
207
102
}
208
103
209
- /**
210
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
211
- */
212
- public function testSetPaymentMethodPurchaseOrderOnCart ()
213
- {
214
- $ methodCode = \Magento \OfflinePayments \Model \Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE ;
215
- $ poNumber = 'GQL-19002 ' ;
216
-
217
- /** @var \Magento\Config\Model\ResourceModel\Config $config */
218
- $ config = ObjectManager::getInstance ()->get (\Magento \Config \Model \ResourceModel \Config::class);
219
- $ config ->saveConfig (
220
- 'payment/ ' . $ methodCode . '/active ' ,
221
- 1 ,
222
- ScopeConfigInterface::SCOPE_TYPE_DEFAULT ,
223
- 0
224
- );
225
-
226
- $ this ->quoteResource ->load (
227
- $ this ->quote ,
228
- 'test_order_1 ' ,
229
- 'reserved_order_id '
230
- );
231
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
232
-
233
- $ query = <<<QUERY
234
- mutation {
235
- setPaymentMethodOnCart(input:
236
- {
237
- cart_id: " $ maskedQuoteId",
238
- payment_method: {
239
- code: " $ methodCode"
240
- po_number: " $ poNumber"
241
- }
242
- }) {
243
-
244
- cart {
245
- cart_id,
246
- payment_method {
247
- code
248
- po_number
249
- }
250
- }
251
- }
252
- }
253
-
254
- QUERY ;
255
-
256
- $ response = $ this ->sendRequestWithToken ($ query );
257
-
258
- $ this ->assertArrayHasKey ('setPaymentMethodOnCart ' , $ response );
259
- $ this ->assertArrayHasKey ('cart ' , $ response ['setPaymentMethodOnCart ' ]);
260
- $ this ->assertEquals ($ maskedQuoteId , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['cart_id ' ]);
261
- $ this ->assertArrayHasKey ('payment_method ' , $ response ['setPaymentMethodOnCart ' ]['cart ' ]);
262
- $ this ->assertEquals ($ methodCode , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['payment_method ' ]['code ' ]);
263
- $ this ->assertEquals ($ poNumber , $ response ['setPaymentMethodOnCart ' ]['cart ' ]['payment_method ' ]['po_number ' ]);
264
- }
265
-
266
- /**
267
- * @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
268
- */
269
- public function testPurchaseOrderPaymentMethodFailingValidation ()
270
- {
271
- $ this ->quoteResource ->load (
272
- $ this ->quote ,
273
- 'test_order_1 ' ,
274
- 'reserved_order_id '
275
- );
276
- $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
277
-
278
- $ query = $ this ->prepareMutationQuery (
279
- $ maskedQuoteId ,
280
- Purchaseorder::PAYMENT_METHOD_PURCHASEORDER_CODE
281
- );
282
-
283
- $ this ->expectExceptionMessage ('Purchase order number is a required field. ' );
284
- $ this ->sendRequestWithToken ($ query );
285
- }
286
-
287
104
/**
288
105
* Generates query for setting the specified shipping method on cart
289
106
*
@@ -307,13 +124,12 @@ private function prepareMutationQuery(
307
124
308
125
cart {
309
126
cart_id,
310
- payment_method {
127
+ selected_payment_method {
311
128
code
312
129
}
313
130
}
314
131
}
315
132
}
316
-
317
133
QUERY ;
318
134
}
319
135
@@ -326,10 +142,21 @@ private function prepareMutationQuery(
326
142
*/
327
143
private function sendRequestWithToken (string $ query ): array
328
144
{
329
-
330
145
$ customerToken =
$ this ->
customerTokenService ->
createCustomerAccessToken (
'[email protected] ' ,
'password ' );
331
146
$ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
332
147
333
148
return $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
334
149
}
150
+
151
+ /**
152
+ * @param string $reversedQuoteId
153
+ * @return string
154
+ */
155
+ private function getMaskedQuoteIdByReversedQuoteId (string $ reversedQuoteId ): string
156
+ {
157
+ $ quote = $ this ->quoteFactory ->create ();
158
+ $ this ->quoteResource ->load ($ quote , $ reversedQuoteId , 'reserved_order_id ' );
159
+
160
+ return $ this ->quoteIdToMaskedId ->execute ((int )$ quote ->getId ());
161
+ }
335
162
}
0 commit comments