|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tests for the orders REST API. |
|
4
|
|
|
* |
|
5
|
|
|
* @package WooCommerce\Tests\API |
|
6
|
|
|
* @since 3.0.0 |
|
7
|
|
|
*/ |
|
8
|
|
|
class WC_Tests_API_Orders_V2 extends WC_REST_Unit_Test_Case { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Array of order to track |
|
12
|
|
|
* @var array |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $orders = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Setup our test server. |
|
18
|
|
|
*/ |
|
19
|
|
|
public function setUp() { |
|
20
|
|
|
parent::setUp(); |
|
21
|
|
|
$this->endpoint = new WC_REST_Orders_Controller(); |
|
|
|
|
|
|
22
|
|
|
$this->user = $this->factory->user->create( |
|
|
|
|
|
|
23
|
|
|
array( |
|
24
|
|
|
'role' => 'administrator', |
|
25
|
|
|
) |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Test route registration. |
|
31
|
|
|
* @since 3.0.0 |
|
32
|
|
|
*/ |
|
33
|
|
|
public function test_register_routes() { |
|
34
|
|
|
$routes = $this->server->get_routes(); |
|
35
|
|
|
$this->assertArrayHasKey( '/wc/v2/orders', $routes ); |
|
36
|
|
|
$this->assertArrayHasKey( '/wc/v2/orders/batch', $routes ); |
|
37
|
|
|
$this->assertArrayHasKey( '/wc/v2/orders/(?P<id>[\d]+)', $routes ); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Test getting all orders. |
|
42
|
|
|
* @since 3.0.0 |
|
43
|
|
|
*/ |
|
44
|
|
|
public function test_get_items() { |
|
45
|
|
|
wp_set_current_user( $this->user ); |
|
46
|
|
|
|
|
47
|
|
|
// Create 10 orders. |
|
48
|
|
|
for ( $i = 0; $i < 10; $i++ ) { |
|
49
|
|
|
$this->orders[] = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order( $this->user ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) ); |
|
53
|
|
|
$orders = $response->get_data(); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
56
|
|
|
$this->assertEquals( 10, count( $orders ) ); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Tests to make sure orders cannot be viewed without valid permissions. |
|
61
|
|
|
* |
|
62
|
|
|
* @since 3.0.0 |
|
63
|
|
|
*/ |
|
64
|
|
|
public function test_get_items_without_permission() { |
|
65
|
|
|
wp_set_current_user( 0 ); |
|
66
|
|
|
$this->orders[] = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
67
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders' ) ); |
|
68
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tests getting a single order. |
|
73
|
|
|
* @since 3.0.0 |
|
74
|
|
|
*/ |
|
75
|
|
|
public function test_get_item() { |
|
76
|
|
|
wp_set_current_user( $this->user ); |
|
77
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
78
|
|
|
$order->add_meta_data( 'key', 'value' ); |
|
79
|
|
|
$order->add_meta_data( 'key2', 'value2' ); |
|
80
|
|
|
$order->save(); |
|
81
|
|
|
$this->orders[] = $order; |
|
82
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) ); |
|
83
|
|
|
$data = $response->get_data(); |
|
84
|
|
|
|
|
85
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
86
|
|
|
$this->assertEquals( $order->get_id(), $data['id'] ); |
|
87
|
|
|
|
|
88
|
|
|
// Test meta data is set. |
|
89
|
|
|
$this->assertEquals( 'key', $data['meta_data'][0]->key ); |
|
90
|
|
|
$this->assertEquals( 'value', $data['meta_data'][0]->value ); |
|
91
|
|
|
$this->assertEquals( 'key2', $data['meta_data'][1]->key ); |
|
92
|
|
|
$this->assertEquals( 'value2', $data['meta_data'][1]->value ); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Tests getting a single order without the correct permissions. |
|
97
|
|
|
* @since 3.0.0 |
|
98
|
|
|
*/ |
|
99
|
|
|
public function test_get_item_without_permission() { |
|
100
|
|
|
wp_set_current_user( 0 ); |
|
101
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
102
|
|
|
$this->orders[] = $order; |
|
103
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() ) ); |
|
104
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Tests getting an order with an invalid ID. |
|
109
|
|
|
* @since 3.0.0 |
|
110
|
|
|
*/ |
|
111
|
|
|
public function test_get_item_invalid_id() { |
|
112
|
|
|
wp_set_current_user( $this->user ); |
|
113
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/99999999' ) ); |
|
114
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Tests getting an order with an invalid ID. |
|
119
|
|
|
* @since 3.5.0 |
|
120
|
|
|
*/ |
|
121
|
|
|
public function test_get_item_refund_id() { |
|
122
|
|
|
wp_set_current_user( $this->user ); |
|
123
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
124
|
|
|
$refund = wc_create_refund( |
|
125
|
|
|
array( |
|
126
|
|
|
'order_id' => $order->get_id(), |
|
127
|
|
|
) |
|
128
|
|
|
); |
|
129
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/orders/' . $refund->get_id() ) ); |
|
|
|
|
|
|
130
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Tests creating an order. |
|
135
|
|
|
* @since 3.0.0 |
|
136
|
|
|
*/ |
|
137
|
|
|
public function test_create_order() { |
|
138
|
|
|
wp_set_current_user( $this->user ); |
|
139
|
|
|
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
|
140
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' ); |
|
141
|
|
|
$request->set_body_params( |
|
142
|
|
|
array( |
|
143
|
|
|
'payment_method' => 'bacs', |
|
144
|
|
|
'payment_method_title' => 'Direct Bank Transfer', |
|
145
|
|
|
'set_paid' => true, |
|
146
|
|
|
'billing' => array( |
|
147
|
|
|
'first_name' => 'John', |
|
148
|
|
|
'last_name' => 'Doe', |
|
149
|
|
|
'address_1' => '969 Market', |
|
150
|
|
|
'address_2' => '', |
|
151
|
|
|
'city' => 'San Francisco', |
|
152
|
|
|
'state' => 'CA', |
|
153
|
|
|
'postcode' => '94103', |
|
154
|
|
|
'country' => 'US', |
|
155
|
|
|
'email' => '[email protected]', |
|
156
|
|
|
'phone' => '(555) 555-5555', |
|
157
|
|
|
), |
|
158
|
|
|
'shipping' => array( |
|
159
|
|
|
'first_name' => 'John', |
|
160
|
|
|
'last_name' => 'Doe', |
|
161
|
|
|
'address_1' => '969 Market', |
|
162
|
|
|
'address_2' => '', |
|
163
|
|
|
'city' => 'San Francisco', |
|
164
|
|
|
'state' => 'CA', |
|
165
|
|
|
'postcode' => '94103', |
|
166
|
|
|
'country' => 'US', |
|
167
|
|
|
), |
|
168
|
|
|
'line_items' => array( |
|
169
|
|
|
array( |
|
170
|
|
|
'product_id' => $product->get_id(), |
|
171
|
|
|
'quantity' => 2, |
|
172
|
|
|
), |
|
173
|
|
|
), |
|
174
|
|
|
'shipping_lines' => array( |
|
175
|
|
|
array( |
|
176
|
|
|
'method_id' => 'flat_rate', |
|
177
|
|
|
'method_title' => 'Flat rate', |
|
178
|
|
|
'total' => '10', |
|
179
|
|
|
), |
|
180
|
|
|
), |
|
181
|
|
|
) |
|
182
|
|
|
); |
|
183
|
|
|
$response = $this->server->dispatch( $request ); |
|
184
|
|
|
$data = $response->get_data(); |
|
185
|
|
|
$order = wc_get_order( $data['id'] ); |
|
186
|
|
|
$this->assertEquals( 201, $response->get_status() ); |
|
187
|
|
|
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); |
|
188
|
|
|
$this->assertEquals( $order->get_payment_method_title(), $data['payment_method_title'] ); |
|
189
|
|
|
$this->assertEquals( $order->get_billing_first_name(), $data['billing']['first_name'] ); |
|
190
|
|
|
$this->assertEquals( $order->get_billing_last_name(), $data['billing']['last_name'] ); |
|
191
|
|
|
$this->assertEquals( '', $data['billing']['company'] ); |
|
192
|
|
|
$this->assertEquals( $order->get_billing_address_1(), $data['billing']['address_1'] ); |
|
193
|
|
|
$this->assertEquals( $order->get_billing_address_2(), $data['billing']['address_2'] ); |
|
194
|
|
|
$this->assertEquals( $order->get_billing_city(), $data['billing']['city'] ); |
|
195
|
|
|
$this->assertEquals( $order->get_billing_state(), $data['billing']['state'] ); |
|
196
|
|
|
$this->assertEquals( $order->get_billing_postcode(), $data['billing']['postcode'] ); |
|
197
|
|
|
$this->assertEquals( $order->get_billing_country(), $data['billing']['country'] ); |
|
198
|
|
|
$this->assertEquals( $order->get_billing_email(), $data['billing']['email'] ); |
|
199
|
|
|
$this->assertEquals( $order->get_billing_phone(), $data['billing']['phone'] ); |
|
200
|
|
|
$this->assertEquals( $order->get_shipping_first_name(), $data['shipping']['first_name'] ); |
|
201
|
|
|
$this->assertEquals( $order->get_shipping_last_name(), $data['shipping']['last_name'] ); |
|
202
|
|
|
$this->assertEquals( '', $data['shipping']['company'] ); |
|
203
|
|
|
$this->assertEquals( $order->get_shipping_address_1(), $data['shipping']['address_1'] ); |
|
204
|
|
|
$this->assertEquals( $order->get_shipping_address_2(), $data['shipping']['address_2'] ); |
|
205
|
|
|
$this->assertEquals( $order->get_shipping_city(), $data['shipping']['city'] ); |
|
206
|
|
|
$this->assertEquals( $order->get_shipping_state(), $data['shipping']['state'] ); |
|
207
|
|
|
$this->assertEquals( $order->get_shipping_postcode(), $data['shipping']['postcode'] ); |
|
208
|
|
|
$this->assertEquals( $order->get_shipping_country(), $data['shipping']['country'] ); |
|
209
|
|
|
$this->assertEquals( 1, count( $data['line_items'] ) ); |
|
210
|
|
|
$this->assertEquals( 1, count( $data['shipping_lines'] ) ); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Test the sanitization of the payment_method_title field through the API. |
|
215
|
|
|
* |
|
216
|
|
|
* @since 3.5.2 |
|
217
|
|
|
*/ |
|
218
|
|
|
public function test_create_update_order_payment_method_title_sanitize() { |
|
219
|
|
|
wp_set_current_user( $this->user ); |
|
220
|
|
|
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
|
221
|
|
|
|
|
222
|
|
|
// Test when creating order. |
|
223
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/orders' ); |
|
224
|
|
|
$request->set_body_params( |
|
225
|
|
|
array( |
|
226
|
|
|
'payment_method' => 'bacs', |
|
227
|
|
|
'payment_method_title' => '<h1>Sanitize this <script>alert(1);</script></h1>', |
|
228
|
|
|
'set_paid' => true, |
|
229
|
|
|
'billing' => array( |
|
230
|
|
|
'first_name' => 'John', |
|
231
|
|
|
'last_name' => 'Doe', |
|
232
|
|
|
'address_1' => '969 Market', |
|
233
|
|
|
'address_2' => '', |
|
234
|
|
|
'city' => 'San Francisco', |
|
235
|
|
|
'state' => 'CA', |
|
236
|
|
|
'postcode' => '94103', |
|
237
|
|
|
'country' => 'US', |
|
238
|
|
|
'email' => '[email protected]', |
|
239
|
|
|
'phone' => '(555) 555-5555', |
|
240
|
|
|
), |
|
241
|
|
|
'shipping' => array( |
|
242
|
|
|
'first_name' => 'John', |
|
243
|
|
|
'last_name' => 'Doe', |
|
244
|
|
|
'address_1' => '969 Market', |
|
245
|
|
|
'address_2' => '', |
|
246
|
|
|
'city' => 'San Francisco', |
|
247
|
|
|
'state' => 'CA', |
|
248
|
|
|
'postcode' => '94103', |
|
249
|
|
|
'country' => 'US', |
|
250
|
|
|
), |
|
251
|
|
|
'line_items' => array( |
|
252
|
|
|
array( |
|
253
|
|
|
'product_id' => $product->get_id(), |
|
254
|
|
|
'quantity' => 2, |
|
255
|
|
|
), |
|
256
|
|
|
), |
|
257
|
|
|
'shipping_lines' => array( |
|
258
|
|
|
array( |
|
259
|
|
|
'method_id' => 'flat_rate', |
|
260
|
|
|
'method_title' => 'Flat rate', |
|
261
|
|
|
'total' => '10', |
|
262
|
|
|
), |
|
263
|
|
|
), |
|
264
|
|
|
) |
|
265
|
|
|
); |
|
266
|
|
|
$response = $this->server->dispatch( $request ); |
|
267
|
|
|
$data = $response->get_data(); |
|
268
|
|
|
$order = wc_get_order( $data['id'] ); |
|
269
|
|
|
$this->assertEquals( 201, $response->get_status() ); |
|
270
|
|
|
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); |
|
271
|
|
|
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this' ); |
|
272
|
|
|
|
|
273
|
|
|
// Test when updating order. |
|
274
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/orders/' . $data['id'] ); |
|
275
|
|
|
$request->set_body_params( |
|
276
|
|
|
array( |
|
277
|
|
|
'payment_method' => 'bacs', |
|
278
|
|
|
'payment_method_title' => '<h1>Sanitize this too <script>alert(1);</script></h1>', |
|
279
|
|
|
) |
|
280
|
|
|
); |
|
281
|
|
|
$response = $this->server->dispatch( $request ); |
|
282
|
|
|
$data = $response->get_data(); |
|
283
|
|
|
$order = wc_get_order( $data['id'] ); |
|
284
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
285
|
|
|
$this->assertEquals( $order->get_payment_method(), $data['payment_method'] ); |
|
286
|
|
|
$this->assertEquals( $order->get_payment_method_title(), 'Sanitize this too' ); |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Tests creating an order without required fields. |
|
291
|
|
|
* @since 3.0.0 |
|
292
|
|
|
*/ |
|
293
|
|
|
public function test_create_order_invalid_fields() { |
|
294
|
|
|
wp_set_current_user( $this->user ); |
|
295
|
|
|
$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
|
296
|
|
|
|
|
297
|
|
|
// non-existent customer |
|
298
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/orders' ); |
|
299
|
|
|
$request->set_body_params( |
|
300
|
|
|
array( |
|
301
|
|
|
'payment_method' => 'bacs', |
|
302
|
|
|
'payment_method_title' => 'Direct Bank Transfer', |
|
303
|
|
|
'set_paid' => true, |
|
304
|
|
|
'customer_id' => 99999, |
|
305
|
|
|
'billing' => array( |
|
306
|
|
|
'first_name' => 'John', |
|
307
|
|
|
'last_name' => 'Doe', |
|
308
|
|
|
'address_1' => '969 Market', |
|
309
|
|
|
'address_2' => '', |
|
310
|
|
|
'city' => 'San Francisco', |
|
311
|
|
|
'state' => 'CA', |
|
312
|
|
|
'postcode' => '94103', |
|
313
|
|
|
'country' => 'US', |
|
314
|
|
|
'email' => '[email protected]', |
|
315
|
|
|
'phone' => '(555) 555-5555', |
|
316
|
|
|
), |
|
317
|
|
|
'shipping' => array( |
|
318
|
|
|
'first_name' => 'John', |
|
319
|
|
|
'last_name' => 'Doe', |
|
320
|
|
|
'address_1' => '969 Market', |
|
321
|
|
|
'address_2' => '', |
|
322
|
|
|
'city' => 'San Francisco', |
|
323
|
|
|
'state' => 'CA', |
|
324
|
|
|
'postcode' => '94103', |
|
325
|
|
|
'country' => 'US', |
|
326
|
|
|
), |
|
327
|
|
|
'line_items' => array( |
|
328
|
|
|
array( |
|
329
|
|
|
'product_id' => $product->get_id(), |
|
330
|
|
|
'quantity' => 2, |
|
331
|
|
|
), |
|
332
|
|
|
), |
|
333
|
|
|
'shipping_lines' => array( |
|
334
|
|
|
array( |
|
335
|
|
|
'method_id' => 'flat_rate', |
|
336
|
|
|
'method_title' => 'Flat rate', |
|
337
|
|
|
'total' => 10, |
|
338
|
|
|
), |
|
339
|
|
|
), |
|
340
|
|
|
) |
|
341
|
|
|
); |
|
342
|
|
|
$response = $this->server->dispatch( $request ); |
|
343
|
|
|
$data = $response->get_data(); |
|
344
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Tests updating an order. |
|
349
|
|
|
* |
|
350
|
|
|
* @since 3.0.0 |
|
351
|
|
|
*/ |
|
352
|
|
|
public function test_update_order() { |
|
353
|
|
|
wp_set_current_user( $this->user ); |
|
354
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
355
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); |
|
356
|
|
|
$request->set_body_params( |
|
357
|
|
|
array( |
|
358
|
|
|
'payment_method' => 'test-update', |
|
359
|
|
|
'billing' => array( |
|
360
|
|
|
'first_name' => 'Fish', |
|
361
|
|
|
'last_name' => 'Face', |
|
362
|
|
|
), |
|
363
|
|
|
) |
|
364
|
|
|
); |
|
365
|
|
|
$response = $this->server->dispatch( $request ); |
|
366
|
|
|
$data = $response->get_data(); |
|
367
|
|
|
|
|
368
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
369
|
|
|
$this->assertEquals( 'test-update', $data['payment_method'] ); |
|
370
|
|
|
$this->assertEquals( 'Fish', $data['billing']['first_name'] ); |
|
371
|
|
|
$this->assertEquals( 'Face', $data['billing']['last_name'] ); |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
/** |
|
375
|
|
|
* Tests updating an order and removing items. |
|
376
|
|
|
* |
|
377
|
|
|
* @since 3.0.0 |
|
378
|
|
|
*/ |
|
379
|
|
|
public function test_update_order_remove_items() { |
|
380
|
|
|
wp_set_current_user( $this->user ); |
|
381
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
382
|
|
|
$fee = new WC_Order_Item_Fee(); |
|
383
|
|
|
$fee->set_props( |
|
384
|
|
|
array( |
|
385
|
|
|
'name' => 'Some Fee', |
|
386
|
|
|
'tax_status' => 'taxable', |
|
387
|
|
|
'total' => '100', |
|
388
|
|
|
'tax_class' => '', |
|
389
|
|
|
) |
|
390
|
|
|
); |
|
391
|
|
|
$order->add_item( $fee ); |
|
392
|
|
|
$order->save(); |
|
393
|
|
|
|
|
394
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); |
|
395
|
|
|
$fee_data = current( $order->get_items( 'fee' ) ); |
|
396
|
|
|
|
|
397
|
|
|
$request->set_body_params( |
|
398
|
|
|
array( |
|
399
|
|
|
'fee_lines' => array( |
|
400
|
|
|
array( |
|
401
|
|
|
'id' => $fee_data->get_id(), |
|
402
|
|
|
'name' => null, |
|
403
|
|
|
), |
|
404
|
|
|
), |
|
405
|
|
|
) |
|
406
|
|
|
); |
|
407
|
|
|
$response = $this->server->dispatch( $request ); |
|
408
|
|
|
$data = $response->get_data(); |
|
409
|
|
|
|
|
410
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
411
|
|
|
$this->assertTrue( empty( $data['fee_lines'] ) ); |
|
412
|
|
|
} |
|
413
|
|
|
|
|
414
|
|
|
/** |
|
415
|
|
|
* Tests updating an order and adding a coupon. |
|
416
|
|
|
* |
|
417
|
|
|
* @since 3.3.0 |
|
418
|
|
|
*/ |
|
419
|
|
|
public function test_update_order_add_coupons() { |
|
420
|
|
|
wp_set_current_user( $this->user ); |
|
421
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
422
|
|
|
$order_item = current( $order->get_items() ); |
|
423
|
|
|
$coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'fake-coupon' ); |
|
424
|
|
|
$coupon->set_amount( 5 ); |
|
425
|
|
|
$coupon->save(); |
|
426
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); |
|
427
|
|
|
$request->set_body_params( |
|
428
|
|
|
array( |
|
429
|
|
|
'coupon_lines' => array( |
|
430
|
|
|
array( |
|
431
|
|
|
'code' => 'fake-coupon', |
|
432
|
|
|
'discount_total' => '5', |
|
433
|
|
|
'discount_tax' => '0', |
|
434
|
|
|
), |
|
435
|
|
|
), |
|
436
|
|
|
'line_items' => array( |
|
437
|
|
|
array( |
|
438
|
|
|
'id' => $order_item->get_id(), |
|
439
|
|
|
'product_id' => $order_item->get_product_id(), |
|
440
|
|
|
'total' => '35.00', |
|
441
|
|
|
), |
|
442
|
|
|
), |
|
443
|
|
|
) |
|
444
|
|
|
); |
|
445
|
|
|
$response = $this->server->dispatch( $request ); |
|
446
|
|
|
$data = $response->get_data(); |
|
447
|
|
|
|
|
448
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
449
|
|
|
$this->assertCount( 1, $data['coupon_lines'] ); |
|
450
|
|
|
$this->assertEquals( '45.00', $data['total'] ); |
|
451
|
|
|
} |
|
452
|
|
|
|
|
453
|
|
|
/** |
|
454
|
|
|
* Tests updating an order and removing a coupon. |
|
455
|
|
|
* |
|
456
|
|
|
* @since 3.3.0 |
|
457
|
|
|
*/ |
|
458
|
|
|
public function test_update_order_remove_coupons() { |
|
459
|
|
|
wp_set_current_user( $this->user ); |
|
460
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
461
|
|
|
$order_item = current( $order->get_items() ); |
|
462
|
|
|
$coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'fake-coupon' ); |
|
463
|
|
|
$coupon->set_amount( 5 ); |
|
464
|
|
|
$coupon->save(); |
|
465
|
|
|
|
|
466
|
|
|
$order->apply_coupon( $coupon ); |
|
467
|
|
|
$order->save(); |
|
468
|
|
|
|
|
469
|
|
|
// Check that the coupon is applied. |
|
470
|
|
|
$this->assertEquals( '45.00', $order->get_total() ); |
|
471
|
|
|
|
|
472
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); |
|
473
|
|
|
$coupon_data = current( $order->get_items( 'coupon' ) ); |
|
474
|
|
|
|
|
475
|
|
|
$request->set_body_params( |
|
476
|
|
|
array( |
|
477
|
|
|
'coupon_lines' => array( |
|
478
|
|
|
array( |
|
479
|
|
|
'id' => $coupon_data->get_id(), |
|
480
|
|
|
'code' => null, |
|
481
|
|
|
), |
|
482
|
|
|
), |
|
483
|
|
|
'line_items' => array( |
|
484
|
|
|
array( |
|
485
|
|
|
'id' => $order_item->get_id(), |
|
486
|
|
|
'product_id' => $order_item->get_product_id(), |
|
487
|
|
|
'total' => '40.00', |
|
488
|
|
|
), |
|
489
|
|
|
), |
|
490
|
|
|
) |
|
491
|
|
|
); |
|
492
|
|
|
$response = $this->server->dispatch( $request ); |
|
493
|
|
|
$data = $response->get_data(); |
|
494
|
|
|
|
|
495
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
496
|
|
|
$this->assertTrue( empty( $data['coupon_lines'] ) ); |
|
497
|
|
|
$this->assertEquals( '50.00', $data['total'] ); |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Tests updating an order without the correct permissions. |
|
502
|
|
|
* |
|
503
|
|
|
* @since 3.0.0 |
|
504
|
|
|
*/ |
|
505
|
|
|
public function test_update_order_without_permission() { |
|
506
|
|
|
wp_set_current_user( 0 ); |
|
507
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
508
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v2/orders/' . $order->get_id() ); |
|
509
|
|
|
$request->set_body_params( |
|
510
|
|
|
array( |
|
511
|
|
|
'payment_method' => 'test-update', |
|
512
|
|
|
'billing' => array( |
|
513
|
|
|
'first_name' => 'Fish', |
|
514
|
|
|
'last_name' => 'Face', |
|
515
|
|
|
), |
|
516
|
|
|
) |
|
517
|
|
|
); |
|
518
|
|
|
$response = $this->server->dispatch( $request ); |
|
519
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* Tests that updating an order with an invalid id fails. |
|
524
|
|
|
* @since 3.0.0 |
|
525
|
|
|
*/ |
|
526
|
|
|
public function test_update_order_invalid_id() { |
|
527
|
|
|
wp_set_current_user( $this->user ); |
|
528
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/999999' ); |
|
529
|
|
|
$request->set_body_params( |
|
530
|
|
|
array( |
|
531
|
|
|
'payment_method' => 'test-update', |
|
532
|
|
|
'billing' => array( |
|
533
|
|
|
'first_name' => 'Fish', |
|
534
|
|
|
'last_name' => 'Face', |
|
535
|
|
|
), |
|
536
|
|
|
) |
|
537
|
|
|
); |
|
538
|
|
|
$response = $this->server->dispatch( $request ); |
|
539
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
|
540
|
|
|
} |
|
541
|
|
|
|
|
542
|
|
|
/** |
|
543
|
|
|
* Test deleting an order. |
|
544
|
|
|
* @since 3.0.0 |
|
545
|
|
|
*/ |
|
546
|
|
|
public function test_delete_order() { |
|
547
|
|
|
wp_set_current_user( $this->user ); |
|
548
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
549
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() ); |
|
550
|
|
|
$request->set_param( 'force', true ); |
|
551
|
|
|
$response = $this->server->dispatch( $request ); |
|
552
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
|
553
|
|
|
$this->assertEquals( null, get_post( $order->get_id() ) ); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
/** |
|
557
|
|
|
* Test deleting an order without permission/creds. |
|
558
|
|
|
* @since 3.0.0 |
|
559
|
|
|
*/ |
|
560
|
|
|
public function test_delete_order_without_permission() { |
|
561
|
|
|
wp_set_current_user( 0 ); |
|
562
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
563
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/' . $order->get_id() ); |
|
564
|
|
|
$request->set_param( 'force', true ); |
|
565
|
|
|
$response = $this->server->dispatch( $request ); |
|
566
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* Test deleting an order with an invalid id. |
|
571
|
|
|
* |
|
572
|
|
|
* @since 3.0.0 |
|
573
|
|
|
*/ |
|
574
|
|
|
public function test_delete_order_invalid_id() { |
|
575
|
|
|
wp_set_current_user( $this->user ); |
|
576
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v2/orders/9999999' ); |
|
577
|
|
|
$request->set_param( 'force', true ); |
|
578
|
|
|
$response = $this->server->dispatch( $request ); |
|
579
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
/** |
|
583
|
|
|
* Test batch managing product reviews. |
|
584
|
|
|
*/ |
|
585
|
|
|
public function test_orders_batch() { |
|
586
|
|
|
wp_set_current_user( $this->user ); |
|
587
|
|
|
|
|
588
|
|
|
$order1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
589
|
|
|
$order2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
590
|
|
|
$order3 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
591
|
|
|
|
|
592
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v2/orders/batch' ); |
|
593
|
|
|
$request->set_body_params( |
|
594
|
|
|
array( |
|
595
|
|
|
'update' => array( |
|
596
|
|
|
array( |
|
597
|
|
|
'id' => $order1->get_id(), |
|
598
|
|
|
'payment_method' => 'updated', |
|
599
|
|
|
), |
|
600
|
|
|
), |
|
601
|
|
|
'delete' => array( |
|
602
|
|
|
$order2->get_id(), |
|
603
|
|
|
$order3->get_id(), |
|
604
|
|
|
), |
|
605
|
|
|
) |
|
606
|
|
|
); |
|
607
|
|
|
$response = $this->server->dispatch( $request ); |
|
608
|
|
|
$data = $response->get_data(); |
|
609
|
|
|
|
|
610
|
|
|
$this->assertEquals( 'updated', $data['update'][0]['payment_method'] ); |
|
611
|
|
|
$this->assertEquals( $order2->get_id(), $data['delete'][0]['id'] ); |
|
612
|
|
|
$this->assertEquals( $order3->get_id(), $data['delete'][1]['id'] ); |
|
613
|
|
|
|
|
614
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v2/orders' ); |
|
615
|
|
|
$response = $this->server->dispatch( $request ); |
|
616
|
|
|
$data = $response->get_data(); |
|
617
|
|
|
$this->assertEquals( 1, count( $data ) ); |
|
618
|
|
|
} |
|
619
|
|
|
|
|
620
|
|
|
/** |
|
621
|
|
|
* Test the order schema. |
|
622
|
|
|
* @since 3.0.0 |
|
623
|
|
|
*/ |
|
624
|
|
|
public function test_order_schema() { |
|
625
|
|
|
wp_set_current_user( $this->user ); |
|
626
|
|
|
$order = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\OrderHelper::create_order(); |
|
627
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v2/orders/' . $order->get_id() ); |
|
628
|
|
|
$response = $this->server->dispatch( $request ); |
|
629
|
|
|
$data = $response->get_data(); |
|
630
|
|
|
$properties = $data['schema']['properties']; |
|
631
|
|
|
|
|
632
|
|
|
$this->assertEquals( 42, count( $properties ) ); |
|
633
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
|
634
|
|
|
} |
|
635
|
|
|
} |
|
636
|
|
|
|
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