Passed
Push — master ( a70374...09ef1e )
by Mike
05:06
created

WC_Tests_API_Orders   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 652
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 376
dl 0
loc 652
rs 10
c 0
b 0
f 0
wmc 24

23 Methods

Rating   Name   Duplication   Size   Complexity  
A test_get_items_without_permission() 0 5 1
A test_update_order_remove_items() 0 33 1
A test_get_item_without_permission() 0 6 1
A test_update_order_add_coupons() 0 25 1
A test_register_routes() 0 5 1
A test_delete_order() 0 8 1
A test_orders_batch() 0 33 1
A test_update_order_remove_coupons() 0 40 1
A test_delete_order_invalid_id() 0 6 1
A test_get_items() 0 13 2
A test_get_item_refund_id() 0 10 1
A test_create_update_order_payment_method_title_sanitize() 0 69 1
A setUp() 0 6 1
A test_get_item() 0 18 1
B test_create_order() 0 74 1
A test_update_order_invalid_id() 0 14 1
A test_update_order_without_permission() 0 15 1
A test_update_order() 0 20 1
A test_create_order_invalid_fields() 0 52 1
A test_invalid_coupon() 0 20 1
A test_delete_order_without_permission() 0 7 1
A test_order_schema() 0 10 1
A test_get_item_invalid_id() 0 4 1
1
<?php
2
/**
3
 * Tests for the orders REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.5.0
7
 */
8
9
/**
10
 * Class WC_Tests_API_Orders
11
 */
12
class WC_Tests_API_Orders 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...
13
14
	/**
15
	 * Array of order to track
16
	 * @var array
17
	 */
18
	protected $orders = array();
19
20
	/**
21
	 * Setup our test server.
22
	 */
23
	public function setUp() {
24
		parent::setUp();
25
		$this->endpoint = new WC_REST_Orders_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...
26
		$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...
27
			array(
28
				'role' => 'administrator',
29
			)
30
		);
31
	}
32
33
	/**
34
	 * Test route registration.
35
	 * @since 3.5.0
36
	 */
37
	public function test_register_routes() {
38
		$routes = $this->server->get_routes();
39
		$this->assertArrayHasKey( '/wc/v3/orders', $routes );
40
		$this->assertArrayHasKey( '/wc/v3/orders/batch', $routes );
41
		$this->assertArrayHasKey( '/wc/v3/orders/(?P<id>[\d]+)', $routes );
42
	}
43
44
	/**
45
	 * Test getting all orders.
46
	 * @since 3.5.0
47
	 */
48
	public function test_get_items() {
49
		wp_set_current_user( $this->user );
50
51
		// Create 10 orders.
52
		for ( $i = 0; $i < 10; $i++ ) {
53
			$this->orders[] = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user );
54
		}
55
56
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
57
		$orders   = $response->get_data();
58
59
		$this->assertEquals( 200, $response->get_status() );
60
		$this->assertEquals( 10, count( $orders ) );
61
	}
62
63
	/**
64
	 * Tests to make sure orders cannot be viewed without valid permissions.
65
	 *
66
	 * @since 3.5.0
67
	 */
68
	public function test_get_items_without_permission() {
69
		wp_set_current_user( 0 );
70
		$this->orders[] = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
71
		$response       = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders' ) );
72
		$this->assertEquals( 401, $response->get_status() );
73
	}
74
75
	/**
76
	 * Tests getting a single order.
77
	 * @since 3.5.0
78
	 */
79
	public function test_get_item() {
80
		wp_set_current_user( $this->user );
81
		$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
82
		$order->add_meta_data( 'key', 'value' );
83
		$order->add_meta_data( 'key2', 'value2' );
84
		$order->save();
85
		$this->orders[] = $order;
86
		$response       = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
87
		$data           = $response->get_data();
88
89
		$this->assertEquals( 200, $response->get_status() );
90
		$this->assertEquals( $order->get_id(), $data['id'] );
91
92
		// Test meta data is set.
93
		$this->assertEquals( 'key', $data['meta_data'][0]->key );
94
		$this->assertEquals( 'value', $data['meta_data'][0]->value );
95
		$this->assertEquals( 'key2', $data['meta_data'][1]->key );
96
		$this->assertEquals( 'value2', $data['meta_data'][1]->value );
97
	}
98
99
	/**
100
	 * Tests getting a single order without the correct permissions.
101
	 * @since 3.5.0
102
	 */
103
	public function test_get_item_without_permission() {
104
		wp_set_current_user( 0 );
105
		$order          = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
106
		$this->orders[] = $order;
107
		$response       = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order->get_id() ) );
108
		$this->assertEquals( 401, $response->get_status() );
109
	}
110
111
	/**
112
	 * Tests getting an order with an invalid ID.
113
	 * @since 3.5.0
114
	 */
115
	public function test_get_item_invalid_id() {
116
		wp_set_current_user( $this->user );
117
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/99999999' ) );
118
		$this->assertEquals( 404, $response->get_status() );
119
	}
120
121
	/**
122
	 * Tests getting an order with an invalid ID.
123
	 * @since 3.5.0
124
	 */
125
	public function test_get_item_refund_id() {
126
		wp_set_current_user( $this->user );
127
		$order    = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
128
		$refund   = wc_create_refund(
129
			array(
130
				'order_id' => $order->get_id(),
131
			)
132
		);
133
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $refund->get_id() ) );
0 ignored issues
show
Bug introduced by
The method get_id() does not exist on WP_Error. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

133
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/orders/' . $refund->/** @scrutinizer ignore-call */ get_id() ) );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
		$this->assertEquals( 404, $response->get_status() );
135
	}
136
137
	/**
138
	 * Tests creating an order.
139
	 * @since 3.5.0
140
	 */
141
	public function test_create_order() {
142
		wp_set_current_user( $this->user );
143
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
144
		$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
145
		$request->set_body_params(
146
			array(
147
				'payment_method'       => 'bacs',
148
				'payment_method_title' => 'Direct Bank Transfer',
149
				'set_paid'             => true,
150
				'billing'              => array(
151
					'first_name' => 'John',
152
					'last_name'  => 'Doe',
153
					'address_1'  => '969 Market',
154
					'address_2'  => '',
155
					'city'       => 'San Francisco',
156
					'state'      => 'CA',
157
					'postcode'   => '94103',
158
					'country'    => 'US',
159
					'email'      => '[email protected]',
160
					'phone'      => '(555) 555-5555',
161
				),
162
				'shipping'             => array(
163
					'first_name' => 'John',
164
					'last_name'  => 'Doe',
165
					'address_1'  => '969 Market',
166
					'address_2'  => '',
167
					'city'       => 'San Francisco',
168
					'state'      => 'CA',
169
					'postcode'   => '94103',
170
					'country'    => 'US',
171
				),
172
				'line_items'           => array(
173
					array(
174
						'product_id' => $product->get_id(),
175
						'quantity'   => 2,
176
					),
177
				),
178
				'shipping_lines'       => array(
179
					array(
180
						'method_id'    => 'flat_rate',
181
						'method_title' => 'Flat rate',
182
						'total'        => '10',
183
					),
184
				),
185
			)
186
		);
187
		$response = $this->server->dispatch( $request );
188
		$data     = $response->get_data();
189
		$order    = wc_get_order( $data['id'] );
190
		$this->assertEquals( 201, $response->get_status() );
191
		$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
192
		$this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] );
193
		$this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] );
194
		$this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] );
195
		$this->assertEquals( '', $data['billing']['company'] );
196
		$this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] );
197
		$this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] );
198
		$this->assertEquals( $order->get_billing_city(), $data['billing']['city'] );
199
		$this->assertEquals( $order->get_billing_state(), $data['billing']['state'] );
200
		$this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] );
201
		$this->assertEquals( $order->get_billing_country(), $data['billing']['country'] );
202
		$this->assertEquals( $order->get_billing_email(), $data['billing']['email'] );
203
		$this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] );
204
		$this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] );
205
		$this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] );
206
		$this->assertEquals( '', $data['shipping']['company'] );
207
		$this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] );
208
		$this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] );
209
		$this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] );
210
		$this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] );
211
		$this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] );
212
		$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] );
213
		$this->assertEquals( 1, count( $data['line_items'] ) );
214
		$this->assertEquals( 1, count( $data['shipping_lines'] ) );
215
	}
216
217
	/**
218
	 * Test the sanitization of the payment_method_title field through the API.
219
	 *
220
	 * @since 3.5.2
221
	 */
222
	public function test_create_update_order_payment_method_title_sanitize() {
223
		wp_set_current_user( $this->user );
224
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
225
226
		// Test when creating order.
227
		$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
228
		$request->set_body_params(
229
			array(
230
				'payment_method'       => 'bacs',
231
				'payment_method_title' => '<h1>Sanitize this <script>alert(1);</script></h1>',
232
				'set_paid'             => true,
233
				'billing'              => array(
234
					'first_name' => 'John',
235
					'last_name'  => 'Doe',
236
					'address_1'  => '969 Market',
237
					'address_2'  => '',
238
					'city'       => 'San Francisco',
239
					'state'      => 'CA',
240
					'postcode'   => '94103',
241
					'country'    => 'US',
242
					'email'      => '[email protected]',
243
					'phone'      => '(555) 555-5555',
244
				),
245
				'shipping'             => array(
246
					'first_name' => 'John',
247
					'last_name'  => 'Doe',
248
					'address_1'  => '969 Market',
249
					'address_2'  => '',
250
					'city'       => 'San Francisco',
251
					'state'      => 'CA',
252
					'postcode'   => '94103',
253
					'country'    => 'US',
254
				),
255
				'line_items'           => array(
256
					array(
257
						'product_id' => $product->get_id(),
258
						'quantity'   => 2,
259
					),
260
				),
261
				'shipping_lines'       => array(
262
					array(
263
						'method_id'    => 'flat_rate',
264
						'method_title' => 'Flat rate',
265
						'total'        => '10',
266
					),
267
				),
268
			)
269
		);
270
		$response = $this->server->dispatch( $request );
271
		$data     = $response->get_data();
272
		$order    = wc_get_order( $data['id'] );
273
		$this->assertEquals( 201, $response->get_status() );
274
		$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
275
		$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' );
276
277
		// Test when updating order.
278
		$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] );
279
		$request->set_body_params(
280
			array(
281
				'payment_method'       => 'bacs',
282
				'payment_method_title' => '<h1>Sanitize this too <script>alert(1);</script></h1>',
283
			)
284
		);
285
		$response = $this->server->dispatch( $request );
286
		$data     = $response->get_data();
287
		$order    = wc_get_order( $data['id'] );
288
		$this->assertEquals( 200, $response->get_status() );
289
		$this->assertEquals( $order->get_payment_method(), $data['payment_method'] );
290
		$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' );
291
	}
292
293
	/**
294
	 * Tests creating an order without required fields.
295
	 * @since 3.5.0
296
	 */
297
	public function test_create_order_invalid_fields() {
298
		wp_set_current_user( $this->user );
299
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
300
301
		// Non-existent customer.
302
		$request = new WP_REST_Request( 'POST', '/wc/v3/orders' );
303
		$request->set_body_params(
304
			array(
305
				'payment_method'       => 'bacs',
306
				'payment_method_title' => 'Direct Bank Transfer',
307
				'set_paid'             => true,
308
				'customer_id'          => 99999,
309
				'billing'              => array(
310
					'first_name' => 'John',
311
					'last_name'  => 'Doe',
312
					'address_1'  => '969 Market',
313
					'address_2'  => '',
314
					'city'       => 'San Francisco',
315
					'state'      => 'CA',
316
					'postcode'   => '94103',
317
					'country'    => 'US',
318
					'email'      => '[email protected]',
319
					'phone'      => '(555) 555-5555',
320
				),
321
				'shipping'             => array(
322
					'first_name' => 'John',
323
					'last_name'  => 'Doe',
324
					'address_1'  => '969 Market',
325
					'address_2'  => '',
326
					'city'       => 'San Francisco',
327
					'state'      => 'CA',
328
					'postcode'   => '94103',
329
					'country'    => 'US',
330
				),
331
				'line_items'           => array(
332
					array(
333
						'product_id' => $product->get_id(),
334
						'quantity'   => 2,
335
					),
336
				),
337
				'shipping_lines'       => array(
338
					array(
339
						'method_id'    => 'flat_rate',
340
						'method_title' => 'Flat rate',
341
						'total'        => 10,
342
					),
343
				),
344
			)
345
		);
346
		$response = $this->server->dispatch( $request );
347
		$data     = $response->get_data();
348
		$this->assertEquals( 400, $response->get_status() );
349
	}
350
351
	/**
352
	 * Tests updating an order.
353
	 *
354
	 * @since 3.5.0
355
	 */
356
	public function test_update_order() {
357
		wp_set_current_user( $this->user );
358
		$order   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
359
		$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
360
		$request->set_body_params(
361
			array(
362
				'payment_method' => 'test-update',
363
				'billing'        => array(
364
					'first_name' => 'Fish',
365
					'last_name'  => 'Face',
366
				),
367
			)
368
		);
369
		$response = $this->server->dispatch( $request );
370
		$data     = $response->get_data();
371
372
		$this->assertEquals( 200, $response->get_status() );
373
		$this->assertEquals( 'test-update', $data['payment_method'] );
374
		$this->assertEquals( 'Fish', $data['billing']['first_name'] );
375
		$this->assertEquals( 'Face', $data['billing']['last_name'] );
376
	}
377
378
	/**
379
	 * Tests updating an order and removing items.
380
	 *
381
	 * @since 3.5.0
382
	 */
383
	public function test_update_order_remove_items() {
384
		wp_set_current_user( $this->user );
385
		$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
386
		$fee   = new WC_Order_Item_Fee();
387
		$fee->set_props(
388
			array(
389
				'name'       => 'Some Fee',
390
				'tax_status' => 'taxable',
391
				'total'      => '100',
392
				'tax_class'  => '',
393
			)
394
		);
395
		$order->add_item( $fee );
396
		$order->save();
397
398
		$request  = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
399
		$fee_data = current( $order->get_items( 'fee' ) );
400
401
		$request->set_body_params(
402
			array(
403
				'fee_lines' => array(
404
					array(
405
						'id'   => $fee_data->get_id(),
406
						'name' => null,
407
					),
408
				),
409
			)
410
		);
411
		$response = $this->server->dispatch( $request );
412
		$data     = $response->get_data();
413
414
		$this->assertEquals( 200, $response->get_status() );
415
		$this->assertTrue( empty( $data['fee_lines'] ) );
416
	}
417
418
	/**
419
	 * Tests updating an order and adding a coupon.
420
	 *
421
	 * @since 3.5.0
422
	 */
423
	public function test_update_order_add_coupons() {
424
		wp_set_current_user( $this->user );
425
426
		$order      = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
427
		$order_item = current( $order->get_items() );
428
		$coupon     = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'fake-coupon' );
429
		$coupon->set_amount( 5 );
430
		$coupon->save();
431
432
		$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
433
		$request->set_body_params(
434
			array(
435
				'coupon_lines' => array(
436
					array(
437
						'code' => 'fake-coupon',
438
					),
439
				),
440
			)
441
		);
442
		$response = $this->server->dispatch( $request );
443
		$data     = $response->get_data();
444
445
		$this->assertEquals( 200, $response->get_status() );
446
		$this->assertCount( 1, $data['coupon_lines'] );
447
		$this->assertEquals( '45.00', $data['total'] );
448
	}
449
450
	/**
451
	 * Tests updating an order and removing a coupon.
452
	 *
453
	 * @since 3.5.0
454
	 */
455
	public function test_update_order_remove_coupons() {
456
		wp_set_current_user( $this->user );
457
		$order      = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
458
		$order_item = current( $order->get_items() );
459
		$coupon     = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'fake-coupon' );
460
		$coupon->set_amount( 5 );
461
		$coupon->save();
462
463
		$order->apply_coupon( $coupon );
464
		$order->save();
465
466
		// Check that the coupon is applied.
467
		$this->assertEquals( '45.00', $order->get_total() );
468
469
		$request     = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
470
		$coupon_data = current( $order->get_items( 'coupon' ) );
471
472
		$request->set_body_params(
473
			array(
474
				'coupon_lines' => array(
475
					array(
476
						'id'   => $coupon_data->get_id(),
477
						'code' => null,
478
					),
479
				),
480
				'line_items'   => array(
481
					array(
482
						'id'         => $order_item->get_id(),
483
						'product_id' => $order_item->get_product_id(),
484
						'total'      => '40.00',
485
					),
486
				),
487
			)
488
		);
489
		$response = $this->server->dispatch( $request );
490
		$data     = $response->get_data();
491
492
		$this->assertEquals( 200, $response->get_status() );
493
		$this->assertTrue( empty( $data['coupon_lines'] ) );
494
		$this->assertEquals( '50.00', $data['total'] );
495
	}
496
497
	/**
498
	 * Tests updating an order with an invalid coupon.
499
	 *
500
	 * @since 3.5.0
501
	 */
502
	public function test_invalid_coupon() {
503
		wp_set_current_user( $this->user );
504
		$order   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
505
		$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
506
507
		$request->set_body_params(
508
			array(
509
				'coupon_lines' => array(
510
					array(
511
						'code' => 'NON_EXISTING_COUPON',
512
					),
513
				),
514
			)
515
		);
516
		$response = $this->server->dispatch( $request );
517
		$data     = $response->get_data();
518
519
		$this->assertEquals( 400, $response->get_status() );
520
		$this->assertEquals( 'woocommerce_rest_invalid_coupon', $data['code'] );
521
		$this->assertEquals( 'Coupon "non_existing_coupon" does not exist!', $data['message'] );
522
	}
523
524
	/**
525
	 * Tests updating an order without the correct permissions.
526
	 *
527
	 * @since 3.5.0
528
	 */
529
	public function test_update_order_without_permission() {
530
		wp_set_current_user( 0 );
531
		$order   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
532
		$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $order->get_id() );
533
		$request->set_body_params(
534
			array(
535
				'payment_method' => 'test-update',
536
				'billing'        => array(
537
					'first_name' => 'Fish',
538
					'last_name'  => 'Face',
539
				),
540
			)
541
		);
542
		$response = $this->server->dispatch( $request );
543
		$this->assertEquals( 401, $response->get_status() );
544
	}
545
546
	/**
547
	 * Tests that updating an order with an invalid id fails.
548
	 *
549
	 * @since 3.5.0
550
	 */
551
	public function test_update_order_invalid_id() {
552
		wp_set_current_user( $this->user );
553
		$request = new WP_REST_Request( 'POST', '/wc/v3/orders/999999' );
554
		$request->set_body_params(
555
			array(
556
				'payment_method' => 'test-update',
557
				'billing'        => array(
558
					'first_name' => 'Fish',
559
					'last_name'  => 'Face',
560
				),
561
			)
562
		);
563
		$response = $this->server->dispatch( $request );
564
		$this->assertEquals( 400, $response->get_status() );
565
	}
566
567
	/**
568
	 * Test deleting an order.
569
	 *
570
	 * @since 3.5.0
571
	 */
572
	public function test_delete_order() {
573
		wp_set_current_user( $this->user );
574
		$order   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
575
		$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
576
		$request->set_param( 'force', true );
577
		$response = $this->server->dispatch( $request );
578
		$this->assertEquals( 200, $response->get_status() );
579
		$this->assertEquals( null, get_post( $order->get_id() ) );
580
	}
581
582
	/**
583
	 * Test deleting an order without permission/creds.
584
	 *
585
	 * @since 3.5.0
586
	 */
587
	public function test_delete_order_without_permission() {
588
		wp_set_current_user( 0 );
589
		$order   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
590
		$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/' . $order->get_id() );
591
		$request->set_param( 'force', true );
592
		$response = $this->server->dispatch( $request );
593
		$this->assertEquals( 401, $response->get_status() );
594
	}
595
596
	/**
597
	 * Test deleting an order with an invalid id.
598
	 *
599
	 * @since 3.5.0
600
	 */
601
	public function test_delete_order_invalid_id() {
602
		wp_set_current_user( $this->user );
603
		$request = new WP_REST_Request( 'DELETE', '/wc/v3/orders/9999999' );
604
		$request->set_param( 'force', true );
605
		$response = $this->server->dispatch( $request );
606
		$this->assertEquals( 404, $response->get_status() );
607
	}
608
609
	/**
610
	 * Test batch managing product reviews.
611
	 *
612
	 * @since 3.5.0
613
	 */
614
	public function test_orders_batch() {
615
		wp_set_current_user( $this->user );
616
617
		$order1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
618
		$order2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
619
		$order3 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
620
621
		$request = new WP_REST_Request( 'POST', '/wc/v3/orders/batch' );
622
		$request->set_body_params(
623
			array(
624
				'update' => array(
625
					array(
626
						'id'             => $order1->get_id(),
627
						'payment_method' => 'updated',
628
					),
629
				),
630
				'delete' => array(
631
					$order2->get_id(),
632
					$order3->get_id(),
633
				),
634
			)
635
		);
636
		$response = $this->server->dispatch( $request );
637
		$data     = $response->get_data();
638
639
		$this->assertEquals( 'updated', $data['update'][0]['payment_method'] );
640
		$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] );
641
		$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] );
642
643
		$request  = new WP_REST_Request( 'GET', '/wc/v3/orders' );
644
		$response = $this->server->dispatch( $request );
645
		$data     = $response->get_data();
646
		$this->assertEquals( 1, count( $data ) );
647
	}
648
649
	/**
650
	 * Test the order schema.
651
	 *
652
	 * @since 3.5.0
653
	 */
654
	public function test_order_schema() {
655
		wp_set_current_user( $this->user );
656
		$order      = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order();
657
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v3/orders/' . $order->get_id() );
658
		$response   = $this->server->dispatch( $request );
659
		$data       = $response->get_data();
660
		$properties = $data['schema']['properties'];
661
662
		$this->assertEquals( 42, count( $properties ) );
663
		$this->assertArrayHasKey( 'id', $properties );
664
	}
665
}
666