1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests for the Payment Gateways REST API. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
* @since 3.5.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Payment_Gateways extends WC_REST_Unit_Test_Case { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Setup our test server, endpoints, and user info. |
13
|
|
|
*/ |
14
|
|
|
public function setUp() { |
15
|
|
|
parent::setUp(); |
16
|
|
|
$this->endpoint = new WC_REST_Payment_Gateways_Controller(); |
|
|
|
|
17
|
|
|
$this->user = $this->factory->user->create( |
|
|
|
|
18
|
|
|
array( |
19
|
|
|
'role' => 'administrator', |
20
|
|
|
) |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Test route registration. |
26
|
|
|
* |
27
|
|
|
* @since 3.5.0 |
28
|
|
|
*/ |
29
|
|
|
public function test_register_routes() { |
30
|
|
|
$routes = $this->server->get_routes(); |
31
|
|
|
$this->assertArrayHasKey( '/wc/v3/payment_gateways', $routes ); |
32
|
|
|
$this->assertArrayHasKey( '/wc/v3/payment_gateways/(?P<id>[\w-]+)', $routes ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test getting all payment gateways. |
37
|
|
|
* |
38
|
|
|
* @since 3.5.0 |
39
|
|
|
*/ |
40
|
|
|
public function test_get_payment_gateways() { |
41
|
|
|
wp_set_current_user( $this->user ); |
42
|
|
|
|
43
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) ); |
44
|
|
|
$gateways = $response->get_data(); |
45
|
|
|
|
46
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
47
|
|
|
$this->assertContains( |
48
|
|
|
array( |
49
|
|
|
'id' => 'cheque', |
50
|
|
|
'title' => 'Check payments', |
51
|
|
|
'description' => 'Please send a check to Store Name, Store Street, Store Town, Store State / County, Store Postcode.', |
52
|
|
|
'order' => '', |
53
|
|
|
'enabled' => false, |
54
|
|
|
'method_title' => 'Check payments', |
55
|
|
|
'method_description' => 'Take payments in person via checks. This offline gateway can also be useful to test purchases.', |
56
|
|
|
'method_supports' => array( |
57
|
|
|
'products', |
58
|
|
|
), |
59
|
|
|
'settings' => array_diff_key( |
60
|
|
|
$this->get_settings( 'WC_Gateway_Cheque' ), |
61
|
|
|
array( |
62
|
|
|
'enabled' => false, |
63
|
|
|
'description' => false, |
64
|
|
|
) |
65
|
|
|
), |
66
|
|
|
'_links' => array( |
67
|
|
|
'self' => array( |
68
|
|
|
array( |
69
|
|
|
'href' => rest_url( '/wc/v3/payment_gateways/cheque' ), |
70
|
|
|
), |
71
|
|
|
), |
72
|
|
|
'collection' => array( |
73
|
|
|
array( |
74
|
|
|
'href' => rest_url( '/wc/v3/payment_gateways' ), |
75
|
|
|
), |
76
|
|
|
), |
77
|
|
|
), |
78
|
|
|
), |
79
|
|
|
$gateways |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Tests to make sure payment gateways cannot viewed without valid permissions. |
85
|
|
|
* |
86
|
|
|
* @since 3.5.0 |
87
|
|
|
*/ |
88
|
|
|
public function test_get_payment_gateways_without_permission() { |
89
|
|
|
wp_set_current_user( 0 ); |
90
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways' ) ); |
91
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Test getting a single payment gateway. |
96
|
|
|
* |
97
|
|
|
* @since 3.5.0 |
98
|
|
|
*/ |
99
|
|
|
public function test_get_payment_gateway() { |
100
|
|
|
wp_set_current_user( $this->user ); |
101
|
|
|
|
102
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); |
103
|
|
|
$paypal = $response->get_data(); |
104
|
|
|
|
105
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
106
|
|
|
$this->assertEquals( |
107
|
|
|
array( |
108
|
|
|
'id' => 'paypal', |
109
|
|
|
'title' => 'PayPal', |
110
|
|
|
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.", |
111
|
|
|
'order' => '', |
112
|
|
|
'enabled' => false, |
113
|
|
|
'method_title' => 'PayPal', |
114
|
|
|
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.', |
115
|
|
|
'method_supports' => array( |
116
|
|
|
'products', |
117
|
|
|
'refunds', |
118
|
|
|
), |
119
|
|
|
'settings' => array_diff_key( |
120
|
|
|
$this->get_settings( 'WC_Gateway_Paypal' ), |
121
|
|
|
array( |
122
|
|
|
'enabled' => false, |
123
|
|
|
'description' => false, |
124
|
|
|
) |
125
|
|
|
), |
126
|
|
|
), |
127
|
|
|
$paypal |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Test getting a payment gateway without valid permissions. |
133
|
|
|
* |
134
|
|
|
* @since 3.5.0 |
135
|
|
|
*/ |
136
|
|
|
public function test_get_payment_gateway_without_permission() { |
137
|
|
|
wp_set_current_user( 0 ); |
138
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); |
139
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Test getting a payment gateway with an invalid id. |
144
|
|
|
* |
145
|
|
|
* @since 3.5.0 |
146
|
|
|
*/ |
147
|
|
|
public function test_get_payment_gateway_invalid_id() { |
148
|
|
|
wp_set_current_user( $this->user ); |
149
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/totally_fake_method' ) ); |
150
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Test updating a single payment gateway. |
155
|
|
|
* |
156
|
|
|
* @since 3.5.0 |
157
|
|
|
*/ |
158
|
|
|
public function test_update_payment_gateway() { |
159
|
|
|
wp_set_current_user( $this->user ); |
160
|
|
|
|
161
|
|
|
// Test defaults |
162
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/payment_gateways/paypal' ) ); |
163
|
|
|
$paypal = $response->get_data(); |
164
|
|
|
|
165
|
|
|
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); |
166
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
167
|
|
|
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); |
168
|
|
|
|
169
|
|
|
// test updating single setting |
170
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
171
|
|
|
$request->set_body_params( |
172
|
|
|
array( |
173
|
|
|
'settings' => array( |
174
|
|
|
'email' => '[email protected]', |
175
|
|
|
), |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
$response = $this->server->dispatch( $request ); |
179
|
|
|
$paypal = $response->get_data(); |
180
|
|
|
|
181
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
182
|
|
|
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); |
183
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
184
|
|
|
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); |
185
|
|
|
|
186
|
|
|
// test updating multiple settings |
187
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
188
|
|
|
$request->set_body_params( |
189
|
|
|
array( |
190
|
|
|
'settings' => array( |
191
|
|
|
'testmode' => 'yes', |
192
|
|
|
'title' => 'PayPal - New Title', |
193
|
|
|
), |
194
|
|
|
) |
195
|
|
|
); |
196
|
|
|
$response = $this->server->dispatch( $request ); |
197
|
|
|
$paypal = $response->get_data(); |
198
|
|
|
|
199
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
200
|
|
|
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); |
201
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
202
|
|
|
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); |
203
|
|
|
|
204
|
|
|
// Test other parameters, and recheck settings |
205
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
206
|
|
|
$request->set_body_params( |
207
|
|
|
array( |
208
|
|
|
'enabled' => false, |
209
|
|
|
'order' => 2, |
210
|
|
|
) |
211
|
|
|
); |
212
|
|
|
$response = $this->server->dispatch( $request ); |
213
|
|
|
$paypal = $response->get_data(); |
214
|
|
|
|
215
|
|
|
$this->assertFalse( $paypal['enabled'] ); |
216
|
|
|
$this->assertEquals( 2, $paypal['order'] ); |
217
|
|
|
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); |
218
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
219
|
|
|
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); |
220
|
|
|
|
221
|
|
|
// test bogus |
222
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
223
|
|
|
$request->set_body_params( |
224
|
|
|
array( |
225
|
|
|
'settings' => array( |
226
|
|
|
'paymentaction' => 'afasfasf', |
227
|
|
|
), |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
$response = $this->server->dispatch( $request ); |
231
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
232
|
|
|
|
233
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
234
|
|
|
$request->set_body_params( |
235
|
|
|
array( |
236
|
|
|
'settings' => array( |
237
|
|
|
'paymentaction' => 'authorization', |
238
|
|
|
), |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
$response = $this->server->dispatch( $request ); |
242
|
|
|
$paypal = $response->get_data(); |
243
|
|
|
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] ); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Test updating a payment gateway without valid permissions. |
248
|
|
|
* |
249
|
|
|
* @since 3.5.0 |
250
|
|
|
*/ |
251
|
|
|
public function test_update_payment_gateway_without_permission() { |
252
|
|
|
wp_set_current_user( 0 ); |
253
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/paypal' ); |
254
|
|
|
$request->set_body_params( |
255
|
|
|
array( |
256
|
|
|
'settings' => array( |
257
|
|
|
'testmode' => 'yes', |
258
|
|
|
'title' => 'PayPal - New Title', |
259
|
|
|
), |
260
|
|
|
) |
261
|
|
|
); |
262
|
|
|
$response = $this->server->dispatch( $request ); |
263
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Test updating a payment gateway with an invalid id. |
268
|
|
|
* |
269
|
|
|
* @since 3.5.0 |
270
|
|
|
*/ |
271
|
|
|
public function test_update_payment_gateway_invalid_id() { |
272
|
|
|
wp_set_current_user( $this->user ); |
273
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/payment_gateways/totally_fake_method' ); |
274
|
|
|
$request->set_body_params( |
275
|
|
|
array( |
276
|
|
|
'enabled' => true, |
277
|
|
|
) |
278
|
|
|
); |
279
|
|
|
$response = $this->server->dispatch( $request ); |
280
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Test the payment gateway schema. |
285
|
|
|
* |
286
|
|
|
* @since 3.5.0 |
287
|
|
|
*/ |
288
|
|
|
public function test_payment_gateway_schema() { |
289
|
|
|
wp_set_current_user( $this->user ); |
290
|
|
|
|
291
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/payment_gateways' ); |
292
|
|
|
$response = $this->server->dispatch( $request ); |
293
|
|
|
$data = $response->get_data(); |
294
|
|
|
$properties = $data['schema']['properties']; |
295
|
|
|
|
296
|
|
|
$this->assertEquals( 9, count( $properties ) ); |
297
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
298
|
|
|
$this->assertArrayHasKey( 'title', $properties ); |
299
|
|
|
$this->assertArrayHasKey( 'description', $properties ); |
300
|
|
|
$this->assertArrayHasKey( 'order', $properties ); |
301
|
|
|
$this->assertArrayHasKey( 'enabled', $properties ); |
302
|
|
|
$this->assertArrayHasKey( 'method_title', $properties ); |
303
|
|
|
$this->assertArrayHasKey( 'method_description', $properties ); |
304
|
|
|
$this->assertArrayHasKey( 'method_supports', $properties ); |
305
|
|
|
$this->assertArrayHasKey( 'settings', $properties ); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Loads a particular gateway's settings so we can correctly test API output. |
310
|
|
|
* |
311
|
|
|
* @since 3.5.0 |
312
|
|
|
* @param string $gateway_class Name of WC_Payment_Gateway class. |
313
|
|
|
*/ |
314
|
|
|
private function get_settings( $gateway_class ) { |
315
|
|
|
$gateway = new $gateway_class(); |
316
|
|
|
$settings = array(); |
317
|
|
|
$gateway->init_form_fields(); |
318
|
|
|
foreach ( $gateway->form_fields as $id => $field ) { |
319
|
|
|
// Make sure we at least have a title and type |
320
|
|
|
if ( empty( $field['title'] ) || empty( $field['type'] ) ) { |
321
|
|
|
continue; |
322
|
|
|
} |
323
|
|
|
// Ignore 'enabled' and 'description', to be in line with \WC_REST_Payment_Gateways_Controller::get_settings. |
324
|
|
|
if ( in_array( $id, array( 'enabled', 'description' ), true ) ) { |
325
|
|
|
continue; |
326
|
|
|
} |
327
|
|
|
$data = array( |
328
|
|
|
'id' => $id, |
329
|
|
|
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'], |
330
|
|
|
'description' => empty( $field['description'] ) ? '' : $field['description'], |
331
|
|
|
'type' => $field['type'], |
332
|
|
|
'value' => $gateway->settings[ $id ], |
333
|
|
|
'default' => empty( $field['default'] ) ? '' : $field['default'], |
334
|
|
|
'tip' => empty( $field['description'] ) ? '' : $field['description'], |
335
|
|
|
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'], |
336
|
|
|
); |
337
|
|
|
if ( ! empty( $field['options'] ) ) { |
338
|
|
|
$data['options'] = $field['options']; |
339
|
|
|
} |
340
|
|
|
$settings[ $id ] = $data; |
341
|
|
|
} |
342
|
|
|
return $settings; |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
} |
346
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths