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