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