1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests for the Payment Gateways REST API. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
* @since 3.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Payment_Gateways_V2 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.0.0 |
28
|
|
|
*/ |
29
|
|
|
public function test_register_routes() { |
30
|
|
|
$routes = $this->server->get_routes(); |
31
|
|
|
$this->assertArrayHasKey( '/wc/v2/payment_gateways', $routes ); |
32
|
|
|
$this->assertArrayHasKey( '/wc/v2/payment_gateways/(?P<id>[\w-]+)', $routes ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Test getting all payment gateways. |
37
|
|
|
* |
38
|
|
|
* @since 3.0.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/v2/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
|
|
|
'settings' => array_diff_key( |
57
|
|
|
$this->get_settings( 'WC_Gateway_Cheque' ), |
58
|
|
|
array( |
59
|
|
|
'enabled' => false, |
60
|
|
|
'description' => false, |
61
|
|
|
) |
62
|
|
|
), |
63
|
|
|
'_links' => array( |
64
|
|
|
'self' => array( |
65
|
|
|
array( |
66
|
|
|
'href' => rest_url( '/wc/v2/payment_gateways/cheque' ), |
67
|
|
|
), |
68
|
|
|
), |
69
|
|
|
'collection' => array( |
70
|
|
|
array( |
71
|
|
|
'href' => rest_url( '/wc/v2/payment_gateways' ), |
72
|
|
|
), |
73
|
|
|
), |
74
|
|
|
), |
75
|
|
|
), |
76
|
|
|
$gateways |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Tests to make sure payment gateways cannot viewed without valid permissions. |
82
|
|
|
* |
83
|
|
|
* @since 3.0.0 |
84
|
|
|
*/ |
85
|
|
|
public function test_get_payment_gateways_without_permission() { |
86
|
|
|
wp_set_current_user( 0 ); |
87
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways' ) ); |
88
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Test getting a single payment gateway. |
93
|
|
|
* |
94
|
|
|
* @since 3.0.0 |
95
|
|
|
*/ |
96
|
|
|
public function test_get_payment_gateway() { |
97
|
|
|
wp_set_current_user( $this->user ); |
98
|
|
|
|
99
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); |
100
|
|
|
$paypal = $response->get_data(); |
101
|
|
|
|
102
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
103
|
|
|
$this->assertEquals( |
104
|
|
|
array( |
105
|
|
|
'id' => 'paypal', |
106
|
|
|
'title' => 'PayPal', |
107
|
|
|
'description' => "Pay via PayPal; you can pay with your credit card if you don't have a PayPal account.", |
108
|
|
|
'order' => '', |
109
|
|
|
'enabled' => false, |
110
|
|
|
'method_title' => 'PayPal', |
111
|
|
|
'method_description' => 'PayPal Standard redirects customers to PayPal to enter their payment information.', |
112
|
|
|
'settings' => array_diff_key( |
113
|
|
|
$this->get_settings( 'WC_Gateway_Paypal' ), |
114
|
|
|
array( |
115
|
|
|
'enabled' => false, |
116
|
|
|
'description' => false, |
117
|
|
|
) |
118
|
|
|
), |
119
|
|
|
), |
120
|
|
|
$paypal |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Test getting a payment gateway without valid permissions. |
126
|
|
|
* |
127
|
|
|
* @since 3.0.0 |
128
|
|
|
*/ |
129
|
|
|
public function test_get_payment_gateway_without_permission() { |
130
|
|
|
wp_set_current_user( 0 ); |
131
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); |
132
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Test getting a payment gateway with an invalid id. |
137
|
|
|
* |
138
|
|
|
* @since 3.0.0 |
139
|
|
|
*/ |
140
|
|
|
public function test_get_payment_gateway_invalid_id() { |
141
|
|
|
wp_set_current_user( $this->user ); |
142
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/totally_fake_method' ) ); |
143
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Test updating a single payment gateway. |
148
|
|
|
* |
149
|
|
|
* @since 3.0.0 |
150
|
|
|
*/ |
151
|
|
|
public function test_update_payment_gateway() { |
152
|
|
|
wp_set_current_user( $this->user ); |
153
|
|
|
|
154
|
|
|
// Test defaults |
155
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/payment_gateways/paypal' ) ); |
156
|
|
|
$paypal = $response->get_data(); |
157
|
|
|
|
158
|
|
|
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); |
159
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
160
|
|
|
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); |
161
|
|
|
|
162
|
|
|
// test updating single setting |
163
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
164
|
|
|
$request->set_body_params( |
165
|
|
|
array( |
166
|
|
|
'settings' => array( |
167
|
|
|
'email' => '[email protected]', |
168
|
|
|
), |
169
|
|
|
) |
170
|
|
|
); |
171
|
|
|
$response = $this->server->dispatch( $request ); |
172
|
|
|
$paypal = $response->get_data(); |
173
|
|
|
|
174
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
175
|
|
|
$this->assertEquals( 'PayPal', $paypal['settings']['title']['value'] ); |
176
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
177
|
|
|
$this->assertEquals( 'no', $paypal['settings']['testmode']['value'] ); |
178
|
|
|
|
179
|
|
|
// test updating multiple settings |
180
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
181
|
|
|
$request->set_body_params( |
182
|
|
|
array( |
183
|
|
|
'settings' => array( |
184
|
|
|
'testmode' => 'yes', |
185
|
|
|
'title' => 'PayPal - New Title', |
186
|
|
|
), |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
$response = $this->server->dispatch( $request ); |
190
|
|
|
$paypal = $response->get_data(); |
191
|
|
|
|
192
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
193
|
|
|
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); |
194
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
195
|
|
|
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); |
196
|
|
|
|
197
|
|
|
// Test other parameters, and recheck settings |
198
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
199
|
|
|
$request->set_body_params( |
200
|
|
|
array( |
201
|
|
|
'enabled' => false, |
202
|
|
|
'order' => 2, |
203
|
|
|
) |
204
|
|
|
); |
205
|
|
|
$response = $this->server->dispatch( $request ); |
206
|
|
|
$paypal = $response->get_data(); |
207
|
|
|
|
208
|
|
|
$this->assertFalse( $paypal['enabled'] ); |
209
|
|
|
$this->assertEquals( 2, $paypal['order'] ); |
210
|
|
|
$this->assertEquals( 'PayPal - New Title', $paypal['settings']['title']['value'] ); |
211
|
|
|
$this->assertEquals( '[email protected]', $paypal['settings']['email']['value'] ); |
212
|
|
|
$this->assertEquals( 'yes', $paypal['settings']['testmode']['value'] ); |
213
|
|
|
|
214
|
|
|
// test bogus |
215
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
216
|
|
|
$request->set_body_params( |
217
|
|
|
array( |
218
|
|
|
'settings' => array( |
219
|
|
|
'paymentaction' => 'afasfasf', |
220
|
|
|
), |
221
|
|
|
) |
222
|
|
|
); |
223
|
|
|
$response = $this->server->dispatch( $request ); |
224
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
225
|
|
|
|
226
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
227
|
|
|
$request->set_body_params( |
228
|
|
|
array( |
229
|
|
|
'settings' => array( |
230
|
|
|
'paymentaction' => 'authorization', |
231
|
|
|
), |
232
|
|
|
) |
233
|
|
|
); |
234
|
|
|
$response = $this->server->dispatch( $request ); |
235
|
|
|
$paypal = $response->get_data(); |
236
|
|
|
$this->assertEquals( 'authorization', $paypal['settings']['paymentaction']['value'] ); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Test updating a payment gateway without valid permissions. |
241
|
|
|
* |
242
|
|
|
* @since 3.0.0 |
243
|
|
|
*/ |
244
|
|
|
public function test_update_payment_gateway_without_permission() { |
245
|
|
|
wp_set_current_user( 0 ); |
246
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/paypal' ); |
247
|
|
|
$request->set_body_params( |
248
|
|
|
array( |
249
|
|
|
'settings' => array( |
250
|
|
|
'testmode' => 'yes', |
251
|
|
|
'title' => 'PayPal - New Title', |
252
|
|
|
), |
253
|
|
|
) |
254
|
|
|
); |
255
|
|
|
$response = $this->server->dispatch( $request ); |
256
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Test updating a payment gateway with an invalid id. |
261
|
|
|
* |
262
|
|
|
* @since 3.0.0 |
263
|
|
|
*/ |
264
|
|
|
public function test_update_payment_gateway_invalid_id() { |
265
|
|
|
wp_set_current_user( $this->user ); |
266
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/payment_gateways/totally_fake_method' ); |
267
|
|
|
$request->set_body_params( |
268
|
|
|
array( |
269
|
|
|
'enabled' => true, |
270
|
|
|
) |
271
|
|
|
); |
272
|
|
|
$response = $this->server->dispatch( $request ); |
273
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Test the payment gateway schema. |
278
|
|
|
* |
279
|
|
|
* @since 3.0.0 |
280
|
|
|
*/ |
281
|
|
|
public function test_payment_gateway_schema() { |
282
|
|
|
wp_set_current_user( $this->user ); |
283
|
|
|
|
284
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/payment_gateways' ); |
285
|
|
|
$response = $this->server->dispatch( $request ); |
286
|
|
|
$data = $response->get_data(); |
287
|
|
|
$properties = $data['schema']['properties']; |
288
|
|
|
|
289
|
|
|
$this->assertEquals( 8, count( $properties ) ); |
290
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
291
|
|
|
$this->assertArrayHasKey( 'title', $properties ); |
292
|
|
|
$this->assertArrayHasKey( 'description', $properties ); |
293
|
|
|
$this->assertArrayHasKey( 'order', $properties ); |
294
|
|
|
$this->assertArrayHasKey( 'enabled', $properties ); |
295
|
|
|
$this->assertArrayHasKey( 'method_title', $properties ); |
296
|
|
|
$this->assertArrayHasKey( 'method_description', $properties ); |
297
|
|
|
$this->assertArrayHasKey( 'settings', $properties ); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Loads a particular gateway's settings so we can correctly test API output. |
302
|
|
|
* |
303
|
|
|
* @since 3.0.0 |
304
|
|
|
* @param string $gateway_class Name of WC_Payment_Gateway class. |
305
|
|
|
*/ |
306
|
|
|
private function get_settings( $gateway_class ) { |
307
|
|
|
$gateway = new $gateway_class(); |
308
|
|
|
$settings = array(); |
309
|
|
|
$gateway->init_form_fields(); |
310
|
|
|
foreach ( $gateway->form_fields as $id => $field ) { |
311
|
|
|
// Make sure we at least have a title and type |
312
|
|
|
if ( empty( $field['title'] ) || empty( $field['type'] ) ) { |
313
|
|
|
continue; |
314
|
|
|
} |
315
|
|
|
// Ignore 'title' settings/fields -- they are UI only |
316
|
|
|
if ( 'title' === $field['type'] ) { |
317
|
|
|
continue; |
318
|
|
|
} |
319
|
|
|
$data = array( |
320
|
|
|
'id' => $id, |
321
|
|
|
'label' => empty( $field['label'] ) ? $field['title'] : $field['label'], |
322
|
|
|
'description' => empty( $field['description'] ) ? '' : $field['description'], |
323
|
|
|
'type' => $field['type'], |
324
|
|
|
'value' => $gateway->settings[ $id ], |
325
|
|
|
'default' => empty( $field['default'] ) ? '' : $field['default'], |
326
|
|
|
'tip' => empty( $field['description'] ) ? '' : $field['description'], |
327
|
|
|
'placeholder' => empty( $field['placeholder'] ) ? '' : $field['placeholder'], |
328
|
|
|
); |
329
|
|
|
if ( ! empty( $field['options'] ) ) { |
330
|
|
|
$data['options'] = $field['options']; |
331
|
|
|
} |
332
|
|
|
$settings[ $id ] = $data; |
333
|
|
|
} |
334
|
|
|
return $settings; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|
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