1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Tests for the Customers REST API. |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce\Tests\API |
6
|
|
|
* @since 3.5.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Tests for the Customers REST API. |
11
|
|
|
* |
12
|
|
|
* @package WooCommerce\Tests\API |
13
|
|
|
* @extends WC_REST_Unit_Test_Case |
14
|
|
|
*/ |
15
|
|
|
class Customers extends WC_REST_Unit_Test_Case { |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Setup our test server, endpoints, and user info. |
19
|
|
|
*/ |
20
|
|
|
public function setUp() { |
21
|
|
|
parent::setUp(); |
22
|
|
|
$this->endpoint = new WC_REST_Customers_Controller(); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Test route registration. |
27
|
|
|
* |
28
|
|
|
* @since 3.5.0 |
29
|
|
|
*/ |
30
|
|
|
public function test_register_routes() { |
31
|
|
|
$routes = $this->server->get_routes(); |
32
|
|
|
|
33
|
|
|
$this->assertArrayHasKey( '/wc/v3/customers', $routes ); |
34
|
|
|
$this->assertArrayHasKey( '/wc/v3/customers/(?P<id>[\d]+)', $routes ); |
35
|
|
|
$this->assertArrayHasKey( '/wc/v3/customers/batch', $routes ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Test getting customers. |
40
|
|
|
* |
41
|
|
|
* @since 3.5.0 |
42
|
|
|
*/ |
43
|
|
|
public function test_get_customers() { |
44
|
|
|
wp_set_current_user( 1 ); |
45
|
|
|
|
46
|
|
|
$customer_1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer(); |
47
|
|
|
\Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'test2', 'test2', '[email protected]' ); |
48
|
|
|
|
49
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); |
50
|
|
|
$request->set_query_params( |
51
|
|
|
array( |
52
|
|
|
'orderby' => 'id', |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
$response = $this->server->dispatch( $request ); |
56
|
|
|
$customers = $response->get_data(); |
57
|
|
|
$date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_1->get_date_created() ) ) ); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
60
|
|
|
$this->assertEquals( 2, count( $customers ) ); |
61
|
|
|
|
62
|
|
|
$this->assertContains( |
63
|
|
|
array( |
64
|
|
|
'id' => $customer_1->get_id(), |
65
|
|
|
'date_created' => wc_rest_prepare_date_response( $date_created, false ), |
66
|
|
|
'date_created_gmt' => wc_rest_prepare_date_response( $date_created ), |
67
|
|
|
'date_modified' => wc_rest_prepare_date_response( $customer_1->get_date_modified(), false ), |
68
|
|
|
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_1->get_date_modified() ), |
69
|
|
|
'email' => '[email protected]', |
70
|
|
|
'first_name' => 'Justin', |
71
|
|
|
'last_name' => '', |
72
|
|
|
'role' => 'customer', |
73
|
|
|
'username' => 'testcustomer', |
74
|
|
|
'billing' => array( |
75
|
|
|
'first_name' => '', |
76
|
|
|
'last_name' => '', |
77
|
|
|
'company' => '', |
78
|
|
|
'address_1' => '123 South Street', |
79
|
|
|
'address_2' => 'Apt 1', |
80
|
|
|
'city' => 'Philadelphia', |
81
|
|
|
'state' => 'PA', |
82
|
|
|
'postcode' => '19123', |
83
|
|
|
'country' => 'US', |
84
|
|
|
'email' => '', |
85
|
|
|
'phone' => '', |
86
|
|
|
), |
87
|
|
|
'shipping' => array( |
88
|
|
|
'first_name' => '', |
89
|
|
|
'last_name' => '', |
90
|
|
|
'company' => '', |
91
|
|
|
'address_1' => '123 South Street', |
92
|
|
|
'address_2' => 'Apt 1', |
93
|
|
|
'city' => 'Philadelphia', |
94
|
|
|
'state' => 'PA', |
95
|
|
|
'postcode' => '19123', |
96
|
|
|
'country' => 'US', |
97
|
|
|
), |
98
|
|
|
'is_paying_customer' => false, |
99
|
|
|
'avatar_url' => $customer_1->get_avatar_url(), |
100
|
|
|
'meta_data' => array(), |
101
|
|
|
'_links' => array( |
102
|
|
|
'self' => array( |
103
|
|
|
array( |
104
|
|
|
'href' => rest_url( '/wc/v3/customers/' . $customer_1->get_id() . '' ), |
105
|
|
|
), |
106
|
|
|
), |
107
|
|
|
'collection' => array( |
108
|
|
|
array( |
109
|
|
|
'href' => rest_url( '/wc/v3/customers' ), |
110
|
|
|
), |
111
|
|
|
), |
112
|
|
|
), |
113
|
|
|
), |
114
|
|
|
$customers |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
update_option( 'timezone_tring', 'America/New York' ); |
118
|
|
|
$customer_3 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'timezonetest', 'timezonetest', '[email protected]' ); |
119
|
|
|
|
120
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); |
121
|
|
|
$request->set_query_params( |
122
|
|
|
array( |
123
|
|
|
'orderby' => 'id', |
124
|
|
|
) |
125
|
|
|
); |
126
|
|
|
$response = $this->server->dispatch( $request ); |
127
|
|
|
$customers = $response->get_data(); |
128
|
|
|
$date_created = get_date_from_gmt( date( 'Y-m-d H:i:s', strtotime( $customer_3->get_date_created() ) ) ); |
129
|
|
|
|
130
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
131
|
|
|
|
132
|
|
|
$this->assertContains( |
133
|
|
|
array( |
134
|
|
|
'id' => $customer_3->get_id(), |
135
|
|
|
'date_created' => wc_rest_prepare_date_response( $date_created, false ), |
136
|
|
|
'date_created_gmt' => wc_rest_prepare_date_response( $date_created ), |
137
|
|
|
'date_modified' => wc_rest_prepare_date_response( $customer_3->get_date_modified(), false ), |
138
|
|
|
'date_modified_gmt' => wc_rest_prepare_date_response( $customer_3->get_date_modified() ), |
139
|
|
|
'email' => '[email protected]', |
140
|
|
|
'first_name' => 'Justin', |
141
|
|
|
'last_name' => '', |
142
|
|
|
'role' => 'customer', |
143
|
|
|
'username' => 'timezonetest', |
144
|
|
|
'billing' => array( |
145
|
|
|
'first_name' => '', |
146
|
|
|
'last_name' => '', |
147
|
|
|
'company' => '', |
148
|
|
|
'address_1' => '123 South Street', |
149
|
|
|
'address_2' => 'Apt 1', |
150
|
|
|
'city' => 'Philadelphia', |
151
|
|
|
'state' => 'PA', |
152
|
|
|
'postcode' => '19123', |
153
|
|
|
'country' => 'US', |
154
|
|
|
'email' => '', |
155
|
|
|
'phone' => '', |
156
|
|
|
), |
157
|
|
|
'shipping' => array( |
158
|
|
|
'first_name' => '', |
159
|
|
|
'last_name' => '', |
160
|
|
|
'company' => '', |
161
|
|
|
'address_1' => '123 South Street', |
162
|
|
|
'address_2' => 'Apt 1', |
163
|
|
|
'city' => 'Philadelphia', |
164
|
|
|
'state' => 'PA', |
165
|
|
|
'postcode' => '19123', |
166
|
|
|
'country' => 'US', |
167
|
|
|
), |
168
|
|
|
'is_paying_customer' => false, |
169
|
|
|
'avatar_url' => $customer_3->get_avatar_url(), |
170
|
|
|
'meta_data' => array(), |
171
|
|
|
'_links' => array( |
172
|
|
|
'self' => array( |
173
|
|
|
array( |
174
|
|
|
'href' => rest_url( '/wc/v3/customers/' . $customer_3->get_id() . '' ), |
175
|
|
|
), |
176
|
|
|
), |
177
|
|
|
'collection' => array( |
178
|
|
|
array( |
179
|
|
|
'href' => rest_url( '/wc/v3/customers' ), |
180
|
|
|
), |
181
|
|
|
), |
182
|
|
|
), |
183
|
|
|
), |
184
|
|
|
$customers |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Test getting customers without valid permissions. |
191
|
|
|
* |
192
|
|
|
* @since 3.5.0 |
193
|
|
|
*/ |
194
|
|
|
public function test_get_customers_without_permission() { |
195
|
|
|
wp_set_current_user( 0 ); |
196
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers' ) ); |
197
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Test creating a new customer. |
202
|
|
|
* |
203
|
|
|
* @since 3.5.0 |
204
|
|
|
*/ |
205
|
|
|
public function test_create_customer() { |
206
|
|
|
wp_set_current_user( 1 ); |
207
|
|
|
|
208
|
|
|
// Test just the basics first.. |
209
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); |
210
|
|
|
$request->set_body_params( |
211
|
|
|
array( |
212
|
|
|
'username' => 'create_customer_test', |
213
|
|
|
'password' => 'test123', |
214
|
|
|
'email' => '[email protected]', |
215
|
|
|
) |
216
|
|
|
); |
217
|
|
|
$response = $this->server->dispatch( $request ); |
218
|
|
|
$data = $response->get_data(); |
219
|
|
|
|
220
|
|
|
$this->assertEquals( 201, $response->get_status() ); |
221
|
|
|
$this->assertEquals( |
222
|
|
|
array( |
223
|
|
|
'id' => $data['id'], |
224
|
|
|
'date_created' => $data['date_created'], |
225
|
|
|
'date_created_gmt' => $data['date_created_gmt'], |
226
|
|
|
'date_modified' => $data['date_modified'], |
227
|
|
|
'date_modified_gmt' => $data['date_modified_gmt'], |
228
|
|
|
'email' => '[email protected]', |
229
|
|
|
'first_name' => '', |
230
|
|
|
'last_name' => '', |
231
|
|
|
'role' => 'customer', |
232
|
|
|
'username' => 'create_customer_test', |
233
|
|
|
'billing' => array( |
234
|
|
|
'first_name' => '', |
235
|
|
|
'last_name' => '', |
236
|
|
|
'company' => '', |
237
|
|
|
'address_1' => '', |
238
|
|
|
'address_2' => '', |
239
|
|
|
'city' => '', |
240
|
|
|
'state' => '', |
241
|
|
|
'postcode' => '', |
242
|
|
|
'country' => '', |
243
|
|
|
'email' => '', |
244
|
|
|
'phone' => '', |
245
|
|
|
), |
246
|
|
|
'shipping' => array( |
247
|
|
|
'first_name' => '', |
248
|
|
|
'last_name' => '', |
249
|
|
|
'company' => '', |
250
|
|
|
'address_1' => '', |
251
|
|
|
'address_2' => '', |
252
|
|
|
'city' => '', |
253
|
|
|
'state' => '', |
254
|
|
|
'postcode' => '', |
255
|
|
|
'country' => '', |
256
|
|
|
), |
257
|
|
|
'is_paying_customer' => false, |
258
|
|
|
'meta_data' => array(), |
259
|
|
|
'avatar_url' => $data['avatar_url'], |
260
|
|
|
), |
261
|
|
|
$data |
262
|
|
|
); |
263
|
|
|
|
264
|
|
|
// Test extra data. |
265
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); |
266
|
|
|
$request->set_body_params( |
267
|
|
|
array( |
268
|
|
|
'username' => 'create_customer_test2', |
269
|
|
|
'password' => 'test123', |
270
|
|
|
'email' => '[email protected]', |
271
|
|
|
'first_name' => 'Test', |
272
|
|
|
'last_name' => 'McTestFace', |
273
|
|
|
'billing' => array( |
274
|
|
|
'country' => 'US', |
275
|
|
|
'state' => 'WA', |
276
|
|
|
), |
277
|
|
|
'shipping' => array( |
278
|
|
|
'state' => 'CA', |
279
|
|
|
'country' => 'US', |
280
|
|
|
), |
281
|
|
|
) |
282
|
|
|
); |
283
|
|
|
$response = $this->server->dispatch( $request ); |
284
|
|
|
$data = $response->get_data(); |
285
|
|
|
|
286
|
|
|
$this->assertEquals( 201, $response->get_status() ); |
287
|
|
|
$this->assertEquals( |
288
|
|
|
array( |
289
|
|
|
'id' => $data['id'], |
290
|
|
|
'date_created' => $data['date_created'], |
291
|
|
|
'date_created_gmt' => $data['date_created_gmt'], |
292
|
|
|
'date_modified' => $data['date_modified'], |
293
|
|
|
'date_modified_gmt' => $data['date_modified_gmt'], |
294
|
|
|
'email' => '[email protected]', |
295
|
|
|
'first_name' => 'Test', |
296
|
|
|
'last_name' => 'McTestFace', |
297
|
|
|
'role' => 'customer', |
298
|
|
|
'username' => 'create_customer_test2', |
299
|
|
|
'billing' => array( |
300
|
|
|
'first_name' => '', |
301
|
|
|
'last_name' => '', |
302
|
|
|
'company' => '', |
303
|
|
|
'address_1' => '', |
304
|
|
|
'address_2' => '', |
305
|
|
|
'city' => '', |
306
|
|
|
'state' => 'WA', |
307
|
|
|
'postcode' => '', |
308
|
|
|
'country' => 'US', |
309
|
|
|
'email' => '', |
310
|
|
|
'phone' => '', |
311
|
|
|
), |
312
|
|
|
'shipping' => array( |
313
|
|
|
'first_name' => '', |
314
|
|
|
'last_name' => '', |
315
|
|
|
'company' => '', |
316
|
|
|
'address_1' => '', |
317
|
|
|
'address_2' => '', |
318
|
|
|
'city' => '', |
319
|
|
|
'state' => 'CA', |
320
|
|
|
'postcode' => '', |
321
|
|
|
'country' => 'US', |
322
|
|
|
), |
323
|
|
|
'is_paying_customer' => false, |
324
|
|
|
'meta_data' => array(), |
325
|
|
|
'avatar_url' => $data['avatar_url'], |
326
|
|
|
), |
327
|
|
|
$data |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
// Test without required field. |
331
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); |
332
|
|
|
$request->set_body_params( |
333
|
|
|
array( |
334
|
|
|
'username' => 'create_customer_test3', |
335
|
|
|
'first_name' => 'Test', |
336
|
|
|
'last_name' => 'McTestFace', |
337
|
|
|
) |
338
|
|
|
); |
339
|
|
|
$response = $this->server->dispatch( $request ); |
340
|
|
|
$data = $response->get_data(); |
341
|
|
|
|
342
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Test creating customers without valid permissions. |
347
|
|
|
* |
348
|
|
|
* @since 3.5.0 |
349
|
|
|
*/ |
350
|
|
|
public function test_create_customer_without_permission() { |
351
|
|
|
wp_set_current_user( 0 ); |
352
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/customers' ); |
353
|
|
|
$request->set_body_params( |
354
|
|
|
array( |
355
|
|
|
'username' => 'create_customer_test_without_permission', |
356
|
|
|
'password' => 'test123', |
357
|
|
|
'email' => '[email protected]', |
358
|
|
|
) |
359
|
|
|
); |
360
|
|
|
$response = $this->server->dispatch( $request ); |
361
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* Test getting a single customer. |
366
|
|
|
* |
367
|
|
|
* @since 3.5.0 |
368
|
|
|
*/ |
369
|
|
|
public function test_get_customer() { |
370
|
|
|
wp_set_current_user( 1 ); |
371
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'get_customer_test', 'test123', '[email protected]' ); |
372
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); |
373
|
|
|
$data = $response->get_data(); |
374
|
|
|
|
375
|
|
|
$this->assertEquals( |
376
|
|
|
array( |
377
|
|
|
'id' => $data['id'], |
378
|
|
|
'date_created' => $data['date_created'], |
379
|
|
|
'date_created_gmt' => $data['date_created_gmt'], |
380
|
|
|
'date_modified' => $data['date_modified'], |
381
|
|
|
'date_modified_gmt' => $data['date_modified_gmt'], |
382
|
|
|
'email' => '[email protected]', |
383
|
|
|
'first_name' => 'Justin', |
384
|
|
|
'billing' => array( |
385
|
|
|
'first_name' => '', |
386
|
|
|
'last_name' => '', |
387
|
|
|
'company' => '', |
388
|
|
|
'address_1' => '123 South Street', |
389
|
|
|
'address_2' => 'Apt 1', |
390
|
|
|
'city' => 'Philadelphia', |
391
|
|
|
'state' => 'PA', |
392
|
|
|
'postcode' => '19123', |
393
|
|
|
'country' => 'US', |
394
|
|
|
'email' => '', |
395
|
|
|
'phone' => '', |
396
|
|
|
), |
397
|
|
|
'shipping' => array( |
398
|
|
|
'first_name' => '', |
399
|
|
|
'last_name' => '', |
400
|
|
|
'company' => '', |
401
|
|
|
'address_1' => '123 South Street', |
402
|
|
|
'address_2' => 'Apt 1', |
403
|
|
|
'city' => 'Philadelphia', |
404
|
|
|
'state' => 'PA', |
405
|
|
|
'postcode' => '19123', |
406
|
|
|
'country' => 'US', |
407
|
|
|
), |
408
|
|
|
'is_paying_customer' => false, |
409
|
|
|
'meta_data' => array(), |
410
|
|
|
'last_name' => '', |
411
|
|
|
'role' => 'customer', |
412
|
|
|
'username' => 'get_customer_test', |
413
|
|
|
'avatar_url' => $data['avatar_url'], |
414
|
|
|
), |
415
|
|
|
$data |
416
|
|
|
); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Test getting a single customer without valid permissions. |
421
|
|
|
* |
422
|
|
|
* @since 3.5.0 |
423
|
|
|
*/ |
424
|
|
|
public function test_get_customer_without_permission() { |
425
|
|
|
wp_set_current_user( 0 ); |
426
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'get_customer_test_without_permission', 'test123', '[email protected]' ); |
427
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); |
428
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* Test getting a single customer with an invalid ID. |
433
|
|
|
* |
434
|
|
|
* @since 3.5.0 |
435
|
|
|
*/ |
436
|
|
|
public function test_get_customer_invalid_id() { |
437
|
|
|
wp_set_current_user( 1 ); |
438
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) ); |
439
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Test updating a customer. |
444
|
|
|
* |
445
|
|
|
* @since 3.5.0 |
446
|
|
|
*/ |
447
|
|
|
public function test_update_customer() { |
448
|
|
|
wp_set_current_user( 1 ); |
449
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'update_customer_test', 'test123', '[email protected]' ); |
450
|
|
|
|
451
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); |
452
|
|
|
$data = $response->get_data(); |
453
|
|
|
$this->assertEquals( 'update_customer_test', $data['username'] ); |
454
|
|
|
$this->assertEquals( '[email protected]', $data['email'] ); |
455
|
|
|
|
456
|
|
|
$request = new WP_REST_Request( 'PUT', '/wc/v3/customers/' . $customer->get_id() ); |
457
|
|
|
$request->set_body_params( |
458
|
|
|
array( |
459
|
|
|
'email' => '[email protected]', |
460
|
|
|
'first_name' => 'UpdatedTest', |
461
|
|
|
) |
462
|
|
|
); |
463
|
|
|
$response = $this->server->dispatch( $request ); |
464
|
|
|
$data = $response->get_data(); |
465
|
|
|
|
466
|
|
|
$this->assertEquals( '[email protected]', $data['email'] ); |
467
|
|
|
$this->assertEquals( 'UpdatedTest', $data['first_name'] ); |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Test updating a customer without valid permissions. |
472
|
|
|
* |
473
|
|
|
* @since 3.5.0 |
474
|
|
|
*/ |
475
|
|
|
public function test_update_customer_without_permission() { |
476
|
|
|
wp_set_current_user( 0 ); |
477
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'update_customer_test_without_permission', 'test123', '[email protected]' ); |
478
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/' . $customer->get_id() ) ); |
479
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Test updating a customer with an invalid ID. |
484
|
|
|
* |
485
|
|
|
* @since 3.5.0 |
486
|
|
|
*/ |
487
|
|
|
public function test_update_customer_invalid_id() { |
488
|
|
|
wp_set_current_user( 1 ); |
489
|
|
|
$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/customers/0' ) ); |
490
|
|
|
$this->assertEquals( 404, $response->get_status() ); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
|
494
|
|
|
/** |
495
|
|
|
* Test deleting a customer. |
496
|
|
|
* |
497
|
|
|
* @since 3.5.0 |
498
|
|
|
*/ |
499
|
|
|
public function test_delete_customer() { |
500
|
|
|
wp_set_current_user( 1 ); |
501
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'delete_customer_test', 'test123', '[email protected]' ); |
502
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() ); |
503
|
|
|
$request->set_param( 'force', true ); |
504
|
|
|
$response = $this->server->dispatch( $request ); |
505
|
|
|
$this->assertEquals( 200, $response->get_status() ); |
506
|
|
|
} |
507
|
|
|
|
508
|
|
|
/** |
509
|
|
|
* Test deleting a customer with an invalid ID. |
510
|
|
|
* |
511
|
|
|
* @since 3.5.0 |
512
|
|
|
*/ |
513
|
|
|
public function test_delete_customer_invalid_id() { |
514
|
|
|
wp_set_current_user( 1 ); |
515
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/0' ); |
516
|
|
|
$request->set_param( 'force', true ); |
517
|
|
|
$response = $this->server->dispatch( $request ); |
518
|
|
|
$this->assertEquals( 400, $response->get_status() ); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Test deleting a customer without valid permissions. |
523
|
|
|
* |
524
|
|
|
* @since 3.5.0 |
525
|
|
|
*/ |
526
|
|
|
public function test_delete_customer_without_permission() { |
527
|
|
|
wp_set_current_user( 0 ); |
528
|
|
|
$customer = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'delete_customer_test_without_permission', 'test123', '[email protected]' ); |
529
|
|
|
$request = new WP_REST_Request( 'DELETE', '/wc/v3/customers/' . $customer->get_id() ); |
530
|
|
|
$request->set_param( 'force', true ); |
531
|
|
|
$response = $this->server->dispatch( $request ); |
532
|
|
|
$this->assertEquals( 401, $response->get_status() ); |
533
|
|
|
} |
534
|
|
|
|
535
|
|
|
/** |
536
|
|
|
* Test customer batch endpoint. |
537
|
|
|
* |
538
|
|
|
* @since 3.5.0 |
539
|
|
|
*/ |
540
|
|
|
public function test_batch_customer() { |
541
|
|
|
wp_set_current_user( 1 ); |
542
|
|
|
|
543
|
|
|
$customer_1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'test_batch_customer', 'test123', '[email protected]' ); |
544
|
|
|
$customer_2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'test_batch_customer2', 'test123', '[email protected]' ); |
545
|
|
|
$customer_3 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'test_batch_customer3', 'test123', '[email protected]' ); |
546
|
|
|
$customer_4 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CustomerHelper::create_customer( 'test_batch_customer4', 'test123', '[email protected]' ); |
547
|
|
|
|
548
|
|
|
$request = new WP_REST_Request( 'POST', '/wc/v3/customers/batch' ); |
549
|
|
|
$request->set_body_params( |
550
|
|
|
array( |
551
|
|
|
'update' => array( |
552
|
|
|
array( |
553
|
|
|
'id' => $customer_1->get_id(), |
554
|
|
|
'last_name' => 'McTest', |
555
|
|
|
), |
556
|
|
|
), |
557
|
|
|
'delete' => array( |
558
|
|
|
$customer_2->get_id(), |
559
|
|
|
$customer_3->get_id(), |
560
|
|
|
), |
561
|
|
|
'create' => array( |
562
|
|
|
array( |
563
|
|
|
'username' => 'newuser', |
564
|
|
|
'password' => 'test123', |
565
|
|
|
'email' => '[email protected]', |
566
|
|
|
), |
567
|
|
|
), |
568
|
|
|
) |
569
|
|
|
); |
570
|
|
|
$response = $this->server->dispatch( $request ); |
571
|
|
|
$data = $response->get_data(); |
572
|
|
|
|
573
|
|
|
$this->assertEquals( 'McTest', $data['update'][0]['last_name'] ); |
574
|
|
|
$this->assertEquals( 'newuser', $data['create'][0]['username'] ); |
575
|
|
|
$this->assertEmpty( $data['create'][0]['last_name'] ); |
576
|
|
|
$this->assertEquals( $customer_2->get_id(), $data['delete'][0]['id'] ); |
577
|
|
|
$this->assertEquals( $customer_3->get_id(), $data['delete'][1]['id'] ); |
578
|
|
|
|
579
|
|
|
$request = new WP_REST_Request( 'GET', '/wc/v3/customers' ); |
580
|
|
|
$response = $this->server->dispatch( $request ); |
581
|
|
|
$data = $response->get_data(); |
582
|
|
|
|
583
|
|
|
$this->assertEquals( 3, count( $data ) ); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* Test customer schema. |
588
|
|
|
* |
589
|
|
|
* @since 3.5.0 |
590
|
|
|
*/ |
591
|
|
|
public function test_customer_schema() { |
592
|
|
|
wp_set_current_user( 1 ); |
593
|
|
|
$request = new WP_REST_Request( 'OPTIONS', '/wc/v3/customers' ); |
594
|
|
|
$response = $this->server->dispatch( $request ); |
595
|
|
|
$data = $response->get_data(); |
596
|
|
|
$properties = $data['schema']['properties']; |
597
|
|
|
|
598
|
|
|
$this->assertEquals( 16, count( $properties ) ); |
599
|
|
|
$this->assertArrayHasKey( 'id', $properties ); |
600
|
|
|
$this->assertArrayHasKey( 'date_created', $properties ); |
601
|
|
|
$this->assertArrayHasKey( 'date_created_gmt', $properties ); |
602
|
|
|
$this->assertArrayHasKey( 'date_modified', $properties ); |
603
|
|
|
$this->assertArrayHasKey( 'date_modified_gmt', $properties ); |
604
|
|
|
$this->assertArrayHasKey( 'email', $properties ); |
605
|
|
|
$this->assertArrayHasKey( 'first_name', $properties ); |
606
|
|
|
$this->assertArrayHasKey( 'last_name', $properties ); |
607
|
|
|
$this->assertArrayHasKey( 'role', $properties ); |
608
|
|
|
$this->assertArrayHasKey( 'username', $properties ); |
609
|
|
|
$this->assertArrayHasKey( 'password', $properties ); |
610
|
|
|
$this->assertArrayHasKey( 'avatar_url', $properties ); |
611
|
|
|
$this->assertArrayHasKey( 'billing', $properties ); |
612
|
|
|
$this->assertArrayHasKey( 'first_name', $properties['billing']['properties'] ); |
613
|
|
|
$this->assertArrayHasKey( 'last_name', $properties['billing']['properties'] ); |
614
|
|
|
$this->assertArrayHasKey( 'company', $properties['billing']['properties'] ); |
615
|
|
|
$this->assertArrayHasKey( 'address_1', $properties['billing']['properties'] ); |
616
|
|
|
$this->assertArrayHasKey( 'address_2', $properties['billing']['properties'] ); |
617
|
|
|
$this->assertArrayHasKey( 'city', $properties['billing']['properties'] ); |
618
|
|
|
$this->assertArrayHasKey( 'state', $properties['billing']['properties'] ); |
619
|
|
|
$this->assertArrayHasKey( 'postcode', $properties['billing']['properties'] ); |
620
|
|
|
$this->assertArrayHasKey( 'country', $properties['billing']['properties'] ); |
621
|
|
|
$this->assertArrayHasKey( 'email', $properties['billing']['properties'] ); |
622
|
|
|
$this->assertArrayHasKey( 'phone', $properties['billing']['properties'] ); |
623
|
|
|
$this->assertArrayHasKey( 'shipping', $properties ); |
624
|
|
|
$this->assertArrayHasKey( 'first_name', $properties['shipping']['properties'] ); |
625
|
|
|
$this->assertArrayHasKey( 'last_name', $properties['shipping']['properties'] ); |
626
|
|
|
$this->assertArrayHasKey( 'company', $properties['shipping']['properties'] ); |
627
|
|
|
$this->assertArrayHasKey( 'address_1', $properties['shipping']['properties'] ); |
628
|
|
|
$this->assertArrayHasKey( 'address_2', $properties['shipping']['properties'] ); |
629
|
|
|
$this->assertArrayHasKey( 'city', $properties['shipping']['properties'] ); |
630
|
|
|
$this->assertArrayHasKey( 'state', $properties['shipping']['properties'] ); |
631
|
|
|
$this->assertArrayHasKey( 'postcode', $properties['shipping']['properties'] ); |
632
|
|
|
$this->assertArrayHasKey( 'country', $properties['shipping']['properties'] ); |
633
|
|
|
} |
634
|
|
|
} |
635
|
|
|
|
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