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