Customers_V2   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 556
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 356
dl 0
loc 556
rs 10
c 0
b 0
f 0
wmc 17

17 Methods

Rating   Name   Duplication   Size   Complexity  
A test_get_customers_without_permission() 0 4 1
A test_create_customer_without_permission() 0 12 1
A test_get_customer_invalid_id() 0 4 1
A setUp() 0 3 1
A test_register_routes() 0 6 1
A test_delete_customer_invalid_id() 0 6 1
A test_get_customer_without_permission() 0 5 1
A test_update_customer_without_permission() 0 5 1
A test_update_customer_invalid_id() 0 4 1
A test_batch_customer() 0 44 1
A test_get_customer() 0 49 1
B test_get_customers() 0 73 1
B test_create_customer() 0 142 1
A test_update_customer() 0 21 1
A test_delete_customer() 0 7 1
A test_customer_schema() 0 44 1
A test_delete_customer_without_permission() 0 7 1
1
<?php
2
/**
3
 * Tests for the Customers REST API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.0.0
7
 */
8
9
class Customers_V2 extends WC_REST_Unit_Test_Case {
0 ignored issues
show
Bug introduced by
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

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

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