1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Settings API Tests. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
* @since 3.5.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
class Settings 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_Setting_Options_Controller(); |
|
|
|
|
17
|
|
|
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\SettingsHelper::register(); |
18
|
|
|
$this->user = $this->factory->user->create( |
|
|
|
|
19
|
|
|
array( |
20
|
|
|
'role' => 'administrator', |
21
|
|
|
) |
22
|
|
|
); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test route registration. |
27
|
|
|
* |
28
|
|
|
* @since 3.5.0 |
29
|
|
|
*/ |
30
|
|
|
public function test_register_routes() { |
31
|
|
|
$routes = $this->server->get_routes(); |
32
|
|
|
$this->assertArrayHasKey( '/wc/v3/settings', $routes ); |
33
|
|
|
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)', $routes ); |
34
|
|
|
$this->assertArrayHasKey( '/wc/v3/settings/(?P<group_id>[\w-]+)/(?P<id>[\w-]+)', $routes ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test getting all groups. |
39
|
|
|
* |
40
|
|
|
* @since 3.5.0 |
41
|
|
|
*/ |
42
|
|
|
public function test_get_groups() { |
43
|
|
|
wp_set_current_user( $this->user ); |
44
|
|
|
|
45
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); |
46
|
|
|
$data = $response->get_data(); |
47
|
|
|
|
48
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
49
|
|
|
|
50
|
|
|
$this->assertContains( |
51
|
|
|
array( |
52
|
|
|
'id' => 'test', |
53
|
|
|
'label' => 'Test extension', |
54
|
|
|
'parent_id' => '', |
55
|
|
|
'description' => 'My awesome test settings.', |
56
|
|
|
'sub_groups' => array( 'sub-test' ), |
57
|
|
|
'_links' => array( |
58
|
|
|
'options' => array( |
59
|
|
|
array( |
60
|
|
|
'href' => rest_url( '/wc/v3/settings/test' ), |
61
|
|
|
), |
62
|
|
|
), |
63
|
|
|
), |
64
|
|
|
), |
65
|
|
|
$data |
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
$this->assertContains( |
69
|
|
|
array( |
70
|
|
|
'id' => 'sub-test', |
71
|
|
|
'label' => 'Sub test', |
72
|
|
|
'parent_id' => 'test', |
73
|
|
|
'description' => '', |
74
|
|
|
'sub_groups' => array(), |
75
|
|
|
'_links' => array( |
76
|
|
|
'options' => array( |
77
|
|
|
array( |
78
|
|
|
'href' => rest_url( '/wc/v3/settings/sub-test' ), |
79
|
|
|
), |
80
|
|
|
), |
81
|
|
|
), |
82
|
|
|
), |
83
|
|
|
$data |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Test /settings without valid permissions/creds. |
89
|
|
|
* |
90
|
|
|
* @since 3.5.0 |
91
|
|
|
*/ |
92
|
|
|
public function test_get_groups_without_permission() { |
93
|
|
|
wp_set_current_user( 0 ); |
94
|
|
|
|
95
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); |
96
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Test /settings without valid permissions/creds. |
101
|
|
|
* |
102
|
|
|
* @since 3.5.0 |
103
|
|
|
* @covers WC_Rest_Settings_Controller::get_items |
104
|
|
|
*/ |
105
|
|
|
public function test_get_groups_none_registered() { |
106
|
|
|
wp_set_current_user( $this->user ); |
107
|
|
|
|
108
|
|
|
remove_all_filters( 'woocommerce_settings_groups' ); |
109
|
|
|
|
110
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings' ) ); |
111
|
|
|
$this->assertEquals( 500, $response->get_status() ); |
112
|
|
|
|
113
|
|
|
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\SettingsHelper::register(); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Test groups schema. |
118
|
|
|
* |
119
|
|
|
* @since 3.5.0 |
120
|
|
|
*/ |
121
|
|
|
public function test_get_group_schema() { |
122
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings' ); |
123
|
|
|
$response = $this->server->dispatch( $request ); |
124
|
|
|
$data = $response->get_data(); |
125
|
|
|
$properties = $data['schema']['properties']; |
126
|
|
|
$this->assertEquals( 5, count( $properties ) ); |
127
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
128
|
|
|
$this->assertArrayHasKey( 'parent_id', $properties ); |
129
|
|
|
$this->assertArrayHasKey( 'label', $properties ); |
130
|
|
|
$this->assertArrayHasKey( 'description', $properties ); |
131
|
|
|
$this->assertArrayHasKey( 'sub_groups', $properties ); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Test settings schema. |
136
|
|
|
* |
137
|
|
|
* @since 3.5.0 |
138
|
|
|
*/ |
139
|
|
|
public function test_get_setting_schema() { |
140
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/settings/test/woocommerce_shop_page_display' ); |
141
|
|
|
$response = $this->server->dispatch( $request ); |
142
|
|
|
$data = $response->get_data(); |
143
|
|
|
$properties = $data['schema']['properties']; |
144
|
|
|
$this->assertEquals( 10, count( $properties ) ); |
145
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
146
|
|
|
$this->assertArrayHasKey( 'label', $properties ); |
147
|
|
|
$this->assertArrayHasKey( 'description', $properties ); |
148
|
|
|
$this->assertArrayHasKey( 'value', $properties ); |
149
|
|
|
$this->assertArrayHasKey( 'default', $properties ); |
150
|
|
|
$this->assertArrayHasKey( 'tip', $properties ); |
151
|
|
|
$this->assertArrayHasKey( 'placeholder', $properties ); |
152
|
|
|
$this->assertArrayHasKey( 'type', $properties ); |
153
|
|
|
$this->assertArrayHasKey( 'options', $properties ); |
154
|
|
|
$this->assertArrayHasKey( 'group_id', $properties ); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Test getting a single group. |
159
|
|
|
* |
160
|
|
|
* @since 3.5.0 |
161
|
|
|
*/ |
162
|
|
|
public function test_get_group() { |
163
|
|
|
wp_set_current_user( $this->user ); |
164
|
|
|
|
165
|
|
|
// test route callback receiving an empty group id |
166
|
|
|
$result = $this->endpoint->get_group_settings( '' ); |
167
|
|
|
$this->assertWPError( $result ); |
168
|
|
|
|
169
|
|
|
// test getting a group that does not exist |
170
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real' ) ); |
171
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
172
|
|
|
|
173
|
|
|
// test getting the 'invalid' group |
174
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid' ) ); |
175
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
176
|
|
|
|
177
|
|
|
// test getting a valid group with settings attached to it |
178
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) ); |
179
|
|
|
$data = $response->get_data(); |
180
|
|
|
$this->assertEquals( 1, count( $data ) ); |
181
|
|
|
$this->assertEquals( 'woocommerce_shop_page_display', $data[0]['id'] ); |
182
|
|
|
$this->assertEmpty( $data[0]['value'] ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Test getting a single group without permission. |
187
|
|
|
* |
188
|
|
|
* @since 3.5.0 |
189
|
|
|
*/ |
190
|
|
|
public function test_get_group_without_permission() { |
191
|
|
|
wp_set_current_user( 0 ); |
192
|
|
|
|
193
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/coupon-data' ) ); |
194
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Test updating a single setting. |
199
|
|
|
* |
200
|
|
|
* @since 3.5.0 |
201
|
|
|
*/ |
202
|
|
|
public function test_update_setting() { |
203
|
|
|
wp_set_current_user( $this->user ); |
204
|
|
|
|
205
|
|
|
// test defaults first |
206
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); |
207
|
|
|
$data = $response->get_data(); |
208
|
|
|
$this->assertEquals( '', $data['value'] ); |
209
|
|
|
|
210
|
|
|
// test updating shop display setting |
211
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); |
212
|
|
|
$request->set_body_params( |
213
|
|
|
array( |
214
|
|
|
'value' => 'both', |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
$response = $this->server->dispatch( $request ); |
218
|
|
|
$data = $response->get_data(); |
219
|
|
|
|
220
|
|
|
$this->assertEquals( 'both', $data['value'] ); |
221
|
|
|
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); |
222
|
|
|
|
223
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); |
224
|
|
|
$request->set_body_params( |
225
|
|
|
array( |
226
|
|
|
'value' => 'subcategories', |
227
|
|
|
) |
228
|
|
|
); |
229
|
|
|
$response = $this->server->dispatch( $request ); |
230
|
|
|
$data = $response->get_data(); |
231
|
|
|
|
232
|
|
|
$this->assertEquals( 'subcategories', $data['value'] ); |
233
|
|
|
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); |
234
|
|
|
|
235
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); |
236
|
|
|
$request->set_body_params( |
237
|
|
|
array( |
238
|
|
|
'value' => '', |
239
|
|
|
) |
240
|
|
|
); |
241
|
|
|
$response = $this->server->dispatch( $request ); |
242
|
|
|
$data = $response->get_data(); |
243
|
|
|
|
244
|
|
|
$this->assertEquals( '', $data['value'] ); |
245
|
|
|
$this->assertEquals( '', get_option( 'woocommerce_shop_page_display' ) ); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Test updating multiple settings at once. |
250
|
|
|
* |
251
|
|
|
* @since 3.5.0 |
252
|
|
|
*/ |
253
|
|
|
public function test_update_settings() { |
254
|
|
|
wp_set_current_user( $this->user ); |
255
|
|
|
|
256
|
|
|
// test defaults first |
257
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test' ) ); |
258
|
|
|
$data = $response->get_data(); |
259
|
|
|
$this->assertEquals( '', $data[0]['value'] ); |
260
|
|
|
|
261
|
|
|
// test setting both at once |
262
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); |
263
|
|
|
$request->set_body_params( |
264
|
|
|
array( |
265
|
|
|
'update' => array( |
266
|
|
|
array( |
267
|
|
|
'id' => 'woocommerce_shop_page_display', |
268
|
|
|
'value' => 'both', |
269
|
|
|
), |
270
|
|
|
), |
271
|
|
|
) |
272
|
|
|
); |
273
|
|
|
$response = $this->server->dispatch( $request ); |
274
|
|
|
$data = $response->get_data(); |
275
|
|
|
|
276
|
|
|
$this->assertEquals( 'both', $data['update'][0]['value'] ); |
277
|
|
|
$this->assertEquals( 'both', get_option( 'woocommerce_shop_page_display' ) ); |
278
|
|
|
|
279
|
|
|
// test updating one, but making sure the other value stays the same |
280
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); |
281
|
|
|
$request->set_body_params( |
282
|
|
|
array( |
283
|
|
|
'update' => array( |
284
|
|
|
array( |
285
|
|
|
'id' => 'woocommerce_shop_page_display', |
286
|
|
|
'value' => 'subcategories', |
287
|
|
|
), |
288
|
|
|
), |
289
|
|
|
) |
290
|
|
|
); |
291
|
|
|
$response = $this->server->dispatch( $request ); |
292
|
|
|
$data = $response->get_data(); |
293
|
|
|
$this->assertEquals( 'subcategories', $data['update'][0]['value'] ); |
294
|
|
|
$this->assertEquals( 'subcategories', get_option( 'woocommerce_shop_page_display' ) ); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Test getting a single setting. |
299
|
|
|
* |
300
|
|
|
* @since 3.5.0 |
301
|
|
|
*/ |
302
|
|
|
public function test_get_setting() { |
303
|
|
|
wp_set_current_user( $this->user ); |
304
|
|
|
|
305
|
|
|
// test getting an invalid setting from a group that does not exist |
306
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/not-real/woocommerce_shop_page_display' ) ); |
307
|
|
|
$data = $response->get_data(); |
308
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
309
|
|
|
|
310
|
|
|
// test getting an invalid setting from a group that does exist |
311
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/invalid/invalid' ) ); |
312
|
|
|
$data = $response->get_data(); |
313
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
314
|
|
|
|
315
|
|
|
// test getting a valid setting |
316
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); |
317
|
|
|
$data = $response->get_data(); |
318
|
|
|
|
319
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
320
|
|
|
|
321
|
|
|
$this->assertEquals( 'woocommerce_shop_page_display', $data['id'] ); |
322
|
|
|
$this->assertEquals( 'Shop page display', $data['label'] ); |
323
|
|
|
$this->assertEquals( '', $data['default'] ); |
324
|
|
|
$this->assertEquals( 'select', $data['type'] ); |
325
|
|
|
$this->assertEquals( '', $data['value'] ); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Test getting a single setting without valid user permissions. |
330
|
|
|
* |
331
|
|
|
* @since 3.5.0 |
332
|
|
|
*/ |
333
|
|
|
public function test_get_setting_without_permission() { |
334
|
|
|
wp_set_current_user( 0 ); |
335
|
|
|
|
336
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/test/woocommerce_shop_page_display' ) ); |
337
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* Tests the GET single setting route handler receiving an empty setting ID. |
342
|
|
|
* |
343
|
|
|
* @since 3.5.0 |
344
|
|
|
*/ |
345
|
|
|
public function test_get_setting_empty_setting_id() { |
346
|
|
|
$result = $this->endpoint->get_setting( 'test', '' ); |
347
|
|
|
|
348
|
|
|
$this->assertWPError( $result ); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Tests the GET single setting route handler receiving an invalid setting ID. |
353
|
|
|
* |
354
|
|
|
* @since 3.5.0 |
355
|
|
|
*/ |
356
|
|
|
public function test_get_setting_invalid_setting_id() { |
357
|
|
|
$result = $this->endpoint->get_setting( 'test', 'invalid' ); |
358
|
|
|
|
359
|
|
|
$this->assertWPError( $result ); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Tests the GET single setting route handler encountering an invalid setting type. |
364
|
|
|
* |
365
|
|
|
* @since 3.5.0 |
366
|
|
|
*/ |
367
|
|
|
public function test_get_setting_invalid_setting_type() { |
368
|
|
|
// $controller = $this->getMock( 'WC_Rest_Setting_Options_Controller', array( 'get_group_settings', 'is_setting_type_valid' ) ); |
369
|
|
|
$controller = $this->getMockBuilder( 'WC_Rest_Setting_Options_Controller' )->setMethods( array( 'get_group_settings', 'is_setting_type_valid' ) )->getMock(); |
370
|
|
|
|
371
|
|
|
$controller |
372
|
|
|
->expects( $this->any() ) |
373
|
|
|
->method( 'get_group_settings' ) |
374
|
|
|
->will( $this->returnValue( \Automattic\WooCommerce\RestApi\UnitTests\Helpers\SettingsHelper::register_test_settings( array() ) ) ); |
375
|
|
|
|
376
|
|
|
$controller |
377
|
|
|
->expects( $this->any() ) |
378
|
|
|
->method( 'is_setting_type_valid' ) |
379
|
|
|
->will( $this->returnValue( false ) ); |
380
|
|
|
|
381
|
|
|
$result = $controller->get_setting( 'test', 'woocommerce_shop_page_display' ); |
382
|
|
|
|
383
|
|
|
$this->assertWPError( $result ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Test updating a single setting without valid user permissions. |
388
|
|
|
* |
389
|
|
|
* @since 3.5.0 |
390
|
|
|
*/ |
391
|
|
|
public function test_update_setting_without_permission() { |
392
|
|
|
wp_set_current_user( 0 ); |
393
|
|
|
|
394
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'test', 'woocommerce_shop_page_display' ) ); |
395
|
|
|
$request->set_body_params( |
396
|
|
|
array( |
397
|
|
|
'value' => 'subcategories', |
398
|
|
|
) |
399
|
|
|
); |
400
|
|
|
$response = $this->server->dispatch( $request ); |
401
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* Test updating multiple settings without valid user permissions. |
407
|
|
|
* |
408
|
|
|
* @since 3.5.0 |
409
|
|
|
*/ |
410
|
|
|
public function test_update_settings_without_permission() { |
411
|
|
|
wp_set_current_user( 0 ); |
412
|
|
|
|
413
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/settings/test/batch' ); |
414
|
|
|
$request->set_body_params( |
415
|
|
|
array( |
416
|
|
|
'update' => array( |
417
|
|
|
array( |
418
|
|
|
'id' => 'woocommerce_shop_page_display', |
419
|
|
|
'value' => 'subcategories', |
420
|
|
|
), |
421
|
|
|
), |
422
|
|
|
) |
423
|
|
|
); |
424
|
|
|
$response = $this->server->dispatch( $request ); |
425
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Test updating a bad setting ID. |
430
|
|
|
* |
431
|
|
|
* @since 3.5.0 |
432
|
|
|
* @covers WC_Rest_Setting_Options_Controller::update_item |
433
|
|
|
*/ |
434
|
|
|
public function test_update_setting_bad_setting_id() { |
435
|
|
|
wp_set_current_user( $this->user ); |
436
|
|
|
|
437
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/test/invalid' ); |
438
|
|
|
$request->set_body_params( |
439
|
|
|
array( |
440
|
|
|
'value' => 'test', |
441
|
|
|
) |
442
|
|
|
); |
443
|
|
|
$response = $this->server->dispatch( $request ); |
444
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* Tests our classic setting registration to make sure settings added for WP-Admin are available over the API. |
449
|
|
|
* |
450
|
|
|
* @since 3.5.0 |
451
|
|
|
*/ |
452
|
|
|
public function test_classic_settings() { |
453
|
|
|
wp_set_current_user( $this->user ); |
454
|
|
|
|
455
|
|
|
// Make sure the group is properly registered |
456
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products' ) ); |
457
|
|
|
$data = $response->get_data(); |
458
|
|
|
$this->assertTrue( is_array( $data ) ); |
459
|
|
|
$this->assertContains( |
460
|
|
|
array( |
461
|
|
|
'id' => 'woocommerce_downloads_require_login', |
462
|
|
|
'label' => 'Access restriction', |
463
|
|
|
'description' => 'Downloads require login', |
464
|
|
|
'type' => 'checkbox', |
465
|
|
|
'default' => 'no', |
466
|
|
|
'tip' => 'This setting does not apply to guest purchases.', |
467
|
|
|
'value' => 'no', |
468
|
|
|
'_links' => array( |
469
|
|
|
'self' => array( |
470
|
|
|
array( |
471
|
|
|
'href' => rest_url( '/wc/v3/settings/products/woocommerce_downloads_require_login' ), |
472
|
|
|
), |
473
|
|
|
), |
474
|
|
|
'collection' => array( |
475
|
|
|
array( |
476
|
|
|
'href' => rest_url( '/wc/v3/settings/products' ), |
477
|
|
|
), |
478
|
|
|
), |
479
|
|
|
), |
480
|
|
|
), |
481
|
|
|
$data |
482
|
|
|
); |
483
|
|
|
|
484
|
|
|
// test get single |
485
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/products/woocommerce_dimension_unit' ) ); |
486
|
|
|
$data = $response->get_data(); |
487
|
|
|
|
488
|
|
|
$this->assertEquals( 'cm', $data['default'] ); |
489
|
|
|
|
490
|
|
|
// test update |
491
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_dimension_unit' ) ); |
492
|
|
|
$request->set_body_params( |
493
|
|
|
array( |
494
|
|
|
'value' => 'yd', |
495
|
|
|
) |
496
|
|
|
); |
497
|
|
|
$response = $this->server->dispatch( $request ); |
498
|
|
|
$data = $response->get_data(); |
499
|
|
|
|
500
|
|
|
$this->assertEquals( 'yd', $data['value'] ); |
501
|
|
|
$this->assertEquals( 'yd', get_option( 'woocommerce_dimension_unit' ) ); |
502
|
|
|
} |
503
|
|
|
|
504
|
|
|
/** |
505
|
|
|
* Tests our email etting registration to make sure settings added for WP-Admin are available over the API. |
506
|
|
|
* |
507
|
|
|
* @since 3.5.0 |
508
|
|
|
*/ |
509
|
|
|
public function test_email_settings() { |
510
|
|
|
wp_set_current_user( $this->user ); |
511
|
|
|
|
512
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order' ) ); |
513
|
|
|
$settings = $response->get_data(); |
514
|
|
|
|
515
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
516
|
|
|
|
517
|
|
|
$this->assertContains( |
518
|
|
|
array( |
519
|
|
|
'id' => 'recipient', |
520
|
|
|
'label' => 'Recipient(s)', |
521
|
|
|
'description' => 'Enter recipients (comma separated) for this email. Defaults to <code>[email protected]</code>.', |
522
|
|
|
'type' => 'text', |
523
|
|
|
'default' => '', |
524
|
|
|
'tip' => 'Enter recipients (comma separated) for this email. Defaults to <code>[email protected]</code>.', |
525
|
|
|
'value' => '', |
526
|
|
|
'_links' => array( |
527
|
|
|
'self' => array( |
528
|
|
|
array( |
529
|
|
|
'href' => rest_url( '/wc/v3/settings/email_new_order/recipient' ), |
530
|
|
|
), |
531
|
|
|
), |
532
|
|
|
'collection' => array( |
533
|
|
|
array( |
534
|
|
|
'href' => rest_url( '/wc/v3/settings/email_new_order' ), |
535
|
|
|
), |
536
|
|
|
), |
537
|
|
|
), |
538
|
|
|
), |
539
|
|
|
$settings |
540
|
|
|
); |
541
|
|
|
|
542
|
|
|
// test get single |
543
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) ); |
544
|
|
|
$setting = $response->get_data(); |
545
|
|
|
|
546
|
|
|
$this->assertEquals( |
547
|
|
|
array( |
548
|
|
|
'id' => 'subject', |
549
|
|
|
'label' => 'Subject', |
550
|
|
|
'description' => 'Available placeholders: <code>{site_title}</code>, <code>{site_address}</code>, <code>{order_date}</code>, <code>{order_number}</code>', |
551
|
|
|
'type' => 'text', |
552
|
|
|
'default' => '', |
553
|
|
|
'tip' => 'Available placeholders: <code>{site_title}</code>, <code>{site_address}</code>, <code>{order_date}</code>, <code>{order_number}</code>', |
554
|
|
|
'value' => '', |
555
|
|
|
'group_id' => 'email_new_order', |
556
|
|
|
), |
557
|
|
|
$setting |
558
|
|
|
); |
559
|
|
|
|
560
|
|
|
// test update |
561
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_new_order', 'subject' ) ); |
562
|
|
|
$request->set_body_params( |
563
|
|
|
array( |
564
|
|
|
'value' => 'This is my subject', |
565
|
|
|
) |
566
|
|
|
); |
567
|
|
|
$response = $this->server->dispatch( $request ); |
568
|
|
|
$setting = $response->get_data(); |
569
|
|
|
|
570
|
|
|
$this->assertEquals( |
571
|
|
|
array( |
572
|
|
|
'id' => 'subject', |
573
|
|
|
'label' => 'Subject', |
574
|
|
|
'description' => 'Available placeholders: <code>{site_title}</code>, <code>{site_address}</code>, <code>{order_date}</code>, <code>{order_number}</code>', |
575
|
|
|
'type' => 'text', |
576
|
|
|
'default' => '', |
577
|
|
|
'tip' => 'Available placeholders: <code>{site_title}</code>, <code>{site_address}</code>, <code>{order_date}</code>, <code>{order_number}</code>', |
578
|
|
|
'value' => 'This is my subject', |
579
|
|
|
'group_id' => 'email_new_order', |
580
|
|
|
), |
581
|
|
|
$setting |
582
|
|
|
); |
583
|
|
|
|
584
|
|
|
// test updating another subject and making sure it works with a "similar" id |
585
|
|
|
$request = new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); |
586
|
|
|
$response = $this->server->dispatch( $request ); |
587
|
|
|
$setting = $response->get_data(); |
588
|
|
|
|
589
|
|
|
$this->assertEmpty( $setting['value'] ); |
590
|
|
|
|
591
|
|
|
// test update |
592
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_customer_new_account', 'subject' ) ); |
593
|
|
|
$request->set_body_params( |
594
|
|
|
array( |
595
|
|
|
'value' => 'This is my new subject', |
596
|
|
|
) |
597
|
|
|
); |
598
|
|
|
$response = $this->server->dispatch( $request ); |
599
|
|
|
$setting = $response->get_data(); |
600
|
|
|
|
601
|
|
|
$this->assertEquals( 'This is my new subject', $setting['value'] ); |
602
|
|
|
|
603
|
|
|
// make sure the other is what we left it |
604
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/email_new_order/subject' ) ); |
605
|
|
|
$setting = $response->get_data(); |
606
|
|
|
|
607
|
|
|
$this->assertEquals( 'This is my subject', $setting['value'] ); |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* Test validation of checkbox settings. |
612
|
|
|
* |
613
|
|
|
* @since 3.5.0 |
614
|
|
|
*/ |
615
|
|
|
public function test_validation_checkbox() { |
616
|
|
|
wp_set_current_user( $this->user ); |
617
|
|
|
|
618
|
|
|
// test bogus value |
619
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); |
620
|
|
|
$request->set_body_params( |
621
|
|
|
array( |
622
|
|
|
'value' => 'not_yes_or_no', |
623
|
|
|
) |
624
|
|
|
); |
625
|
|
|
$response = $this->server->dispatch( $request ); |
626
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
627
|
|
|
|
628
|
|
|
// test yes |
629
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); |
630
|
|
|
$request->set_body_params( |
631
|
|
|
array( |
632
|
|
|
'value' => 'yes', |
633
|
|
|
) |
634
|
|
|
); |
635
|
|
|
$response = $this->server->dispatch( $request ); |
636
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
637
|
|
|
|
638
|
|
|
// test no |
639
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'email_cancelled_order', 'enabled' ) ); |
640
|
|
|
$request->set_body_params( |
641
|
|
|
array( |
642
|
|
|
'value' => 'no', |
643
|
|
|
) |
644
|
|
|
); |
645
|
|
|
$response = $this->server->dispatch( $request ); |
646
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
647
|
|
|
} |
648
|
|
|
|
649
|
|
|
/** |
650
|
|
|
* Test validation of radio settings. |
651
|
|
|
* |
652
|
|
|
* @since 3.5.0 |
653
|
|
|
*/ |
654
|
|
|
public function test_validation_radio() { |
655
|
|
|
wp_set_current_user( $this->user ); |
656
|
|
|
|
657
|
|
|
// not a valid option |
658
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); |
659
|
|
|
$request->set_body_params( |
660
|
|
|
array( |
661
|
|
|
'value' => 'billing2', |
662
|
|
|
) |
663
|
|
|
); |
664
|
|
|
$response = $this->server->dispatch( $request ); |
665
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
666
|
|
|
|
667
|
|
|
// valid |
668
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'shipping', 'woocommerce_ship_to_destination' ) ); |
669
|
|
|
$request->set_body_params( |
670
|
|
|
array( |
671
|
|
|
'value' => 'billing', |
672
|
|
|
) |
673
|
|
|
); |
674
|
|
|
$response = $this->server->dispatch( $request ); |
675
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* Test validation of multiselect. |
680
|
|
|
* |
681
|
|
|
* @since 3.5.0 |
682
|
|
|
*/ |
683
|
|
|
public function test_validation_multiselect() { |
684
|
|
|
wp_set_current_user( $this->user ); |
685
|
|
|
|
686
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ) ); |
687
|
|
|
$setting = $response->get_data(); |
688
|
|
|
$this->assertEmpty( $setting['value'] ); |
689
|
|
|
|
690
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'general', 'woocommerce_specific_allowed_countries' ) ); |
691
|
|
|
$request->set_body_params( |
692
|
|
|
array( |
693
|
|
|
'value' => array( 'AX', 'DZ', 'MMM' ), |
694
|
|
|
) |
695
|
|
|
); |
696
|
|
|
$response = $this->server->dispatch( $request ); |
697
|
|
|
$setting = $response->get_data(); |
698
|
|
|
$this->assertEquals( array( 'AX', 'DZ' ), $setting['value'] ); |
699
|
|
|
} |
700
|
|
|
|
701
|
|
|
/** |
702
|
|
|
* Test validation of select. |
703
|
|
|
* |
704
|
|
|
* @since 3.5.0 |
705
|
|
|
*/ |
706
|
|
|
public function test_validation_select() { |
707
|
|
|
wp_set_current_user( $this->user ); |
708
|
|
|
|
709
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ) ); |
710
|
|
|
$setting = $response->get_data(); |
711
|
|
|
$this->assertEquals( 'kg', $setting['value'] ); |
712
|
|
|
|
713
|
|
|
// invalid |
714
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); |
715
|
|
|
$request->set_body_params( |
716
|
|
|
array( |
717
|
|
|
'value' => 'pounds', // invalid, should be lbs |
718
|
|
|
) |
719
|
|
|
); |
720
|
|
|
$response = $this->server->dispatch( $request ); |
721
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
722
|
|
|
|
723
|
|
|
// valid |
724
|
|
|
$request = new WP_REST_Request( 'PUT', sprintf( '/wc/v3/settings/%s/%s', 'products', 'woocommerce_weight_unit' ) ); |
725
|
|
|
$request->set_body_params( |
726
|
|
|
array( |
727
|
|
|
'value' => 'lbs', // invalid, should be lbs |
728
|
|
|
) |
729
|
|
|
); |
730
|
|
|
$response = $this->server->dispatch( $request ); |
731
|
|
|
$setting = $response->get_data(); |
732
|
|
|
$this->assertEquals( 'lbs', $setting['value'] ); |
733
|
|
|
} |
734
|
|
|
|
735
|
|
|
/** |
736
|
|
|
* Test to make sure the 'base location' setting is present in the response. |
737
|
|
|
* That it is returned as 'select' and not 'single_select_country', |
738
|
|
|
* and that both state and country options are returned. |
739
|
|
|
* |
740
|
|
|
* @since 3.5.0 |
741
|
|
|
*/ |
742
|
|
|
public function test_woocommerce_default_country() { |
743
|
|
|
wp_set_current_user( $this->user ); |
744
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_default_country' ) ); |
745
|
|
|
$setting = $response->get_data(); |
746
|
|
|
|
747
|
|
|
$this->assertEquals( 'select', $setting['type'] ); |
748
|
|
|
$this->assertArrayHasKey( 'GB', $setting['options'] ); |
749
|
|
|
$this->assertArrayHasKey( 'US:OR', $setting['options'] ); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
/** |
753
|
|
|
* Test to make sure the store address setting can be fetched and updated. |
754
|
|
|
* |
755
|
|
|
* @since 3.5.0 |
756
|
|
|
*/ |
757
|
|
|
public function test_woocommerce_store_address() { |
758
|
|
|
wp_set_current_user( $this->user ); |
759
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address' ) ); |
760
|
|
|
$setting = $response->get_data(); |
761
|
|
|
$this->assertEquals( 'text', $setting['type'] ); |
762
|
|
|
|
763
|
|
|
// Repalce the old value with something uniquely new |
764
|
|
|
$old_value = $setting['value']; |
765
|
|
|
$new_value = $old_value . ' ' . rand( 1000, 9999 ); |
766
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' ); |
767
|
|
|
$request->set_body_params( |
768
|
|
|
array( |
769
|
|
|
'value' => $new_value, |
770
|
|
|
) |
771
|
|
|
); |
772
|
|
|
$response = $this->server->dispatch( $request ); |
773
|
|
|
$setting = $response->get_data(); |
774
|
|
|
$this->assertEquals( $new_value, $setting['value'] ); |
775
|
|
|
|
776
|
|
|
// Put the original value back |
777
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address' ); |
778
|
|
|
$request->set_body_params( |
779
|
|
|
array( |
780
|
|
|
'value' => $old_value, |
781
|
|
|
) |
782
|
|
|
); |
783
|
|
|
$response = $this->server->dispatch( $request ); |
784
|
|
|
$setting = $response->get_data(); |
785
|
|
|
$this->assertEquals( $old_value, $setting['value'] ); |
786
|
|
|
} |
787
|
|
|
|
788
|
|
|
/** |
789
|
|
|
* Test to make sure the store address 2 (line 2) setting can be fetched and updated. |
790
|
|
|
* |
791
|
|
|
* @since 3.5.0 |
792
|
|
|
*/ |
793
|
|
|
public function test_woocommerce_store_address_2() { |
794
|
|
|
wp_set_current_user( $this->user ); |
795
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_address_2' ) ); |
796
|
|
|
$setting = $response->get_data(); |
797
|
|
|
$this->assertEquals( 'text', $setting['type'] ); |
798
|
|
|
|
799
|
|
|
// Repalce the old value with something uniquely new |
800
|
|
|
$old_value = $setting['value']; |
801
|
|
|
$new_value = $old_value . ' ' . rand( 1000, 9999 ); |
802
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' ); |
803
|
|
|
$request->set_body_params( |
804
|
|
|
array( |
805
|
|
|
'value' => $new_value, |
806
|
|
|
) |
807
|
|
|
); |
808
|
|
|
$response = $this->server->dispatch( $request ); |
809
|
|
|
$setting = $response->get_data(); |
810
|
|
|
$this->assertEquals( $new_value, $setting['value'] ); |
811
|
|
|
|
812
|
|
|
// Put the original value back |
813
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_address_2' ); |
814
|
|
|
$request->set_body_params( |
815
|
|
|
array( |
816
|
|
|
'value' => $old_value, |
817
|
|
|
) |
818
|
|
|
); |
819
|
|
|
$response = $this->server->dispatch( $request ); |
820
|
|
|
$setting = $response->get_data(); |
821
|
|
|
$this->assertEquals( $old_value, $setting['value'] ); |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
/** |
825
|
|
|
* Test to make sure the store city setting can be fetched and updated. |
826
|
|
|
* |
827
|
|
|
* @since 3.5.0 |
828
|
|
|
*/ |
829
|
|
|
public function test_woocommerce_store_city() { |
830
|
|
|
wp_set_current_user( $this->user ); |
831
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_city' ) ); |
832
|
|
|
$setting = $response->get_data(); |
833
|
|
|
$this->assertEquals( 'text', $setting['type'] ); |
834
|
|
|
|
835
|
|
|
// Repalce the old value with something uniquely new |
836
|
|
|
$old_value = $setting['value']; |
837
|
|
|
$new_value = $old_value . ' ' . rand( 1000, 9999 ); |
838
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' ); |
839
|
|
|
$request->set_body_params( |
840
|
|
|
array( |
841
|
|
|
'value' => $new_value, |
842
|
|
|
) |
843
|
|
|
); |
844
|
|
|
$response = $this->server->dispatch( $request ); |
845
|
|
|
$setting = $response->get_data(); |
846
|
|
|
$this->assertEquals( $new_value, $setting['value'] ); |
847
|
|
|
|
848
|
|
|
// Put the original value back |
849
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_city' ); |
850
|
|
|
$request->set_body_params( |
851
|
|
|
array( |
852
|
|
|
'value' => $old_value, |
853
|
|
|
) |
854
|
|
|
); |
855
|
|
|
$response = $this->server->dispatch( $request ); |
856
|
|
|
$setting = $response->get_data(); |
857
|
|
|
$this->assertEquals( $old_value, $setting['value'] ); |
858
|
|
|
} |
859
|
|
|
|
860
|
|
|
/** |
861
|
|
|
* Test to make sure the store postcode setting can be fetched and updated. |
862
|
|
|
* |
863
|
|
|
* @since 3.5.0 |
864
|
|
|
*/ |
865
|
|
|
public function test_woocommerce_store_postcode() { |
866
|
|
|
wp_set_current_user( $this->user ); |
867
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/settings/general/woocommerce_store_postcode' ) ); |
868
|
|
|
$setting = $response->get_data(); |
869
|
|
|
$this->assertEquals( 'text', $setting['type'] ); |
870
|
|
|
|
871
|
|
|
// Repalce the old value with something uniquely new |
872
|
|
|
$old_value = $setting['value']; |
873
|
|
|
$new_value = $old_value . ' ' . rand( 1000, 9999 ); |
874
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' ); |
875
|
|
|
$request->set_body_params( |
876
|
|
|
array( |
877
|
|
|
'value' => $new_value, |
878
|
|
|
) |
879
|
|
|
); |
880
|
|
|
$response = $this->server->dispatch( $request ); |
881
|
|
|
$setting = $response->get_data(); |
882
|
|
|
$this->assertEquals( $new_value, $setting['value'] ); |
883
|
|
|
|
884
|
|
|
// Put the original value back |
885
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/settings/general/woocommerce_store_postcode' ); |
886
|
|
|
$request->set_body_params( |
887
|
|
|
array( |
888
|
|
|
'value' => $old_value, |
889
|
|
|
) |
890
|
|
|
); |
891
|
|
|
$response = $this->server->dispatch( $request ); |
892
|
|
|
$setting = $response->get_data(); |
893
|
|
|
$this->assertEquals( $old_value, $setting['value'] ); |
894
|
|
|
} |
895
|
|
|
} |
896
|
|
|
|
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