Settings_V2::test_woocommerce_store_city()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 29
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * Settings API Tests.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.0.0
7
 */
8
9
class Settings_V2 extends WC_REST_Unit_Test_Case {
0 ignored issues
show
Bug introduced by
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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