Completed
Push — master ( 3cc586...6d7c23 )
by Rodrigo
08:38 queued 03:35
created

unit-tests/Tests/Version2/products.php (1 issue)

Labels
Severity
1
<?php
2
/**
3
 * Tests for Products API.
4
 *
5
 * @package WooCommerce\Tests\API
6
 * @since 3.0.0
7
 */
8
9
/**
10
 * Products_API_V2 class.
11
 */
12
class Products_API_V2 extends WC_REST_Unit_Test_Case {
0 ignored issues
show
The type WC_REST_Unit_Test_Case was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
13
14
	/**
15
	 * Setup our test server, endpoints, and user info.
16
	 */
17
	public function setUp() {
18
		parent::setUp();
19
		$this->endpoint = new WC_REST_Products_Controller();
20
		$this->user     = $this->factory->user->create(
21
			array(
22
				'role' => 'administrator',
23
			)
24
		);
25
	}
26
27
	/**
28
	 * Test route registration.
29
	 *
30
	 * @since 3.0.0
31
	 */
32
	public function test_register_routes() {
33
		$routes = $this->server->get_routes();
34
		$this->assertArrayHasKey( '/wc/v2/products', $routes );
35
		$this->assertArrayHasKey( '/wc/v2/products/(?P<id>[\d]+)', $routes );
36
		$this->assertArrayHasKey( '/wc/v2/products/batch', $routes );
37
	}
38
39
	/**
40
	 * Test getting products.
41
	 *
42
	 * @since 3.0.0
43
	 */
44
	public function test_get_products() {
45
		wp_set_current_user( $this->user );
46
		\Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_external_product();
47
		sleep( 1 ); // So both products have different timestamps.
48
		\Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
49
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
50
		$products = $response->get_data();
51
52
		$this->assertEquals( 200, $response->get_status() );
53
54
		$this->assertEquals( 2, count( $products ) );
55
		$this->assertEquals( 'Dummy Product', $products[0]['name'] );
56
		$this->assertEquals( 'DUMMY SKU', $products[0]['sku'] );
57
		$this->assertEquals( 'Dummy External Product', $products[1]['name'] );
58
		$this->assertEquals( 'DUMMY EXTERNAL SKU', $products[1]['sku'] );
59
	}
60
61
	/**
62
	 * Test getting products without permission.
63
	 *
64
	 * @since 3.0.0
65
	 */
66
	public function test_get_products_without_permission() {
67
		wp_set_current_user( 0 );
68
		\Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
69
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
70
		$this->assertEquals( 401, $response->get_status() );
71
	}
72
73
	/**
74
	 * Test getting a single product.
75
	 *
76
	 * @since 3.0.0
77
	 */
78
	public function test_get_product() {
79
		wp_set_current_user( $this->user );
80
		$simple   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_external_product();
81
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $simple->get_id() ) );
82
		$product  = $response->get_data();
83
84
		$this->assertEquals( 200, $response->get_status() );
85
		$this->assertContains(
86
			array(
87
				'id'            => $simple->get_id(),
88
				'name'          => 'Dummy External Product',
89
				'type'          => 'simple',
90
				'status'        => 'publish',
91
				'sku'           => 'DUMMY EXTERNAL SKU',
92
				'regular_price' => 10,
93
			),
94
			$product
95
		);
96
	}
97
98
	/**
99
	 * Test getting single product without permission.
100
	 *
101
	 * @since 3.0.0
102
	 */
103
	public function test_get_product_without_permission() {
104
		wp_set_current_user( 0 );
105
		$product  = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
106
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
107
		$this->assertEquals( 401, $response->get_status() );
108
	}
109
110
	/**
111
	 * Test deleting a single product.
112
	 *
113
	 * @since 3.0.0
114
	 */
115
	public function test_delete_product() {
116
		wp_set_current_user( $this->user );
117
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
118
119
		$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
120
		$request->set_param( 'force', true );
121
		$response = $this->server->dispatch( $request );
122
		$this->assertEquals( 200, $response->get_status() );
123
124
		$response   = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
125
		$variations = $response->get_data();
126
		$this->assertEquals( 0, count( $variations ) );
127
	}
128
129
	/**
130
	 * Test deleting a single product without permission.
131
	 *
132
	 * @since 3.0.0
133
	 */
134
	public function test_delete_product_without_permission() {
135
		wp_set_current_user( 0 );
136
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
137
		$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() );
138
		$request->set_param( 'force', true );
139
		$response = $this->server->dispatch( $request );
140
		$this->assertEquals( 401, $response->get_status() );
141
	}
142
143
	/**
144
	 * Test deleting a single product with an invalid ID.
145
	 *
146
	 * @since 3.0.0
147
	 */
148
	public function test_delete_product_with_invalid_id() {
149
		wp_set_current_user( 0 );
150
		$request = new WP_REST_Request( 'DELETE', '/wc/v2/products/0' );
151
		$request->set_param( 'force', true );
152
		$response = $this->server->dispatch( $request );
153
		$this->assertEquals( 404, $response->get_status() );
154
	}
155
156
	/**
157
	 * Test editing a single product. Tests multiple product types.
158
	 *
159
	 * @since 3.0.0
160
	 */
161
	public function test_update_product() {
162
		wp_set_current_user( $this->user );
163
164
		// test simple products.
165
		$product  = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
166
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
167
		$data     = $response->get_data();
168
169
		$this->assertEquals( 'DUMMY SKU', $data['sku'] );
170
		$this->assertEquals( 10, $data['regular_price'] );
171
		$this->assertEmpty( $data['sale_price'] );
172
173
		$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
174
		$request->set_body_params(
175
			array(
176
				'sku'         => 'FIXED-SKU',
177
				'sale_price'  => '8',
178
				'description' => 'Testing',
179
				'images'      => array(
180
					array(
181
						'position' => 0,
182
						'src'      => 'http://cldup.com/Dr1Bczxq4q.png',
183
						'alt'      => 'test upload image',
184
					),
185
				),
186
			)
187
		);
188
		$response = $this->server->dispatch( $request );
189
		$data     = $response->get_data();
190
191
		$this->assertContains( 'Testing', $data['description'] );
192
		$this->assertEquals( '8', $data['price'] );
193
		$this->assertEquals( '8', $data['sale_price'] );
194
		$this->assertEquals( '10', $data['regular_price'] );
195
		$this->assertEquals( 'FIXED-SKU', $data['sku'] );
196
		$this->assertContains( 'Dr1Bczxq4q', $data['images'][0]['src'] );
197
		$this->assertContains( 'test upload image', $data['images'][0]['alt'] );
198
		$product->delete( true );
199
200
		// test variable product (variations are tested in product-variations.php).
201
		$product  = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product();
202
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
203
		$data     = $response->get_data();
204
205
		foreach ( array( 'small', 'large' ) as $term_name ) {
206
			$this->assertContains( $term_name, $data['attributes'][0]['options'] );
207
		}
208
209
		$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
210
		$request->set_body_params(
211
			array(
212
				'attributes' => array(
213
					array(
214
						'id'        => 0,
215
						'name'      => 'pa_color',
216
						'options'   => array(
217
							'red',
218
							'yellow',
219
						),
220
						'visible'   => false,
221
						'variation' => 1,
222
					),
223
					array(
224
						'id'        => 0,
225
						'name'      => 'pa_size',
226
						'options'   => array(
227
							'small',
228
						),
229
						'visible'   => false,
230
						'variation' => 1,
231
					),
232
				),
233
			)
234
		);
235
		$response = $this->server->dispatch( $request );
236
		$data     = $response->get_data();
237
238
		$this->assertEquals( array( 'small' ), $data['attributes'][0]['options'] );
239
		$this->assertEquals( array( 'red', 'yellow' ), $data['attributes'][1]['options'] );
240
		$product->delete( true );
241
242
		// test external product.
243
		$product  = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_external_product();
244
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() ) );
245
		$data     = $response->get_data();
246
247
		$this->assertEquals( 'Buy external product', $data['button_text'] );
248
		$this->assertEquals( 'http://woocommerce.com', $data['external_url'] );
249
250
		$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
251
		$request->set_body_params(
252
			array(
253
				'button_text'  => 'Test API Update',
254
				'external_url' => 'http://automattic.com',
255
			)
256
		);
257
		$response = $this->server->dispatch( $request );
258
		$data     = $response->get_data();
259
260
		$this->assertEquals( 'Test API Update', $data['button_text'] );
261
		$this->assertEquals( 'http://automattic.com', $data['external_url'] );
262
	}
263
264
	/**
265
	 * Test updating a single product without permission.
266
	 *
267
	 * @since 3.0.0
268
	 */
269
	public function test_update_product_without_permission() {
270
		wp_set_current_user( 0 );
271
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
272
		$request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() );
273
		$request->set_body_params(
274
			array(
275
				'sku' => 'FIXED-SKU-NO-PERMISSION',
276
			)
277
		);
278
		$response = $this->server->dispatch( $request );
279
		$this->assertEquals( 401, $response->get_status() );
280
	}
281
282
	/**
283
	 * Test updating a single product with an invalid ID.
284
	 *
285
	 * @since 3.0.0
286
	 */
287
	public function test_update_product_with_invalid_id() {
288
		wp_set_current_user( $this->user );
289
		$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
290
		$request = new WP_REST_Request( 'PUT', '/wc/v2/products/0' );
291
		$request->set_body_params(
292
			array(
293
				'sku' => 'FIXED-SKU-INVALID-ID',
294
			)
295
		);
296
		$response = $this->server->dispatch( $request );
297
		$this->assertEquals( 400, $response->get_status() );
298
	}
299
300
	/**
301
	 * Test creating a single product.
302
	 *
303
	 * @since 3.0.0
304
	 */
305
	public function test_create_product() {
306
		wp_set_current_user( $this->user );
307
308
		$request = new WP_REST_Request( 'POST', '/wc/v2/products/shipping_classes' );
309
		$request->set_body_params(
310
			array(
311
				'name' => 'Test',
312
			)
313
		);
314
		$response          = $this->server->dispatch( $request );
315
		$data              = $response->get_data();
316
		$shipping_class_id = $data['id'];
317
318
		// Create simple.
319
		$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
320
		$request->set_body_params(
321
			array(
322
				'type'           => 'simple',
323
				'name'           => 'Test Simple Product',
324
				'sku'            => 'DUMMY SKU SIMPLE API',
325
				'regular_price'  => '10',
326
				'shipping_class' => 'test',
327
			)
328
		);
329
		$response = $this->server->dispatch( $request );
330
		$data     = $response->get_data();
331
332
		$this->assertEquals( '10', $data['price'] );
333
		$this->assertEquals( '10', $data['regular_price'] );
334
		$this->assertTrue( $data['purchasable'] );
335
		$this->assertEquals( 'DUMMY SKU SIMPLE API', $data['sku'] );
336
		$this->assertEquals( 'Test Simple Product', $data['name'] );
337
		$this->assertEquals( 'simple', $data['type'] );
338
		$this->assertEquals( $shipping_class_id, $data['shipping_class_id'] );
339
340
		// Create external.
341
		$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
342
		$request->set_body_params(
343
			array(
344
				'type'          => 'external',
345
				'name'          => 'Test External Product',
346
				'sku'           => 'DUMMY SKU EXTERNAL API',
347
				'regular_price' => '10',
348
				'button_text'   => 'Test Button',
349
				'external_url'  => 'https://wordpress.org',
350
			)
351
		);
352
		$response = $this->server->dispatch( $request );
353
		$data     = $response->get_data();
354
355
		$this->assertEquals( '10', $data['price'] );
356
		$this->assertEquals( '10', $data['regular_price'] );
357
		$this->assertFalse( $data['purchasable'] );
358
		$this->assertEquals( 'DUMMY SKU EXTERNAL API', $data['sku'] );
359
		$this->assertEquals( 'Test External Product', $data['name'] );
360
		$this->assertEquals( 'external', $data['type'] );
361
		$this->assertEquals( 'Test Button', $data['button_text'] );
362
		$this->assertEquals( 'https://wordpress.org', $data['external_url'] );
363
364
		// Create variable.
365
		$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
366
		$request->set_body_params(
367
			array(
368
				'type'       => 'variable',
369
				'name'       => 'Test Variable Product',
370
				'sku'        => 'DUMMY SKU VARIABLE API',
371
				'attributes' => array(
372
					array(
373
						'id'        => 0,
374
						'name'      => 'pa_size',
375
						'options'   => array(
376
							'small',
377
							'medium',
378
						),
379
						'visible'   => false,
380
						'variation' => 1,
381
					),
382
				),
383
			)
384
		);
385
		$response = $this->server->dispatch( $request );
386
		$data     = $response->get_data();
387
388
		$this->assertEquals( 'DUMMY SKU VARIABLE API', $data['sku'] );
389
		$this->assertEquals( 'Test Variable Product', $data['name'] );
390
		$this->assertEquals( 'variable', $data['type'] );
391
		$this->assertEquals( array( 'small', 'medium' ), $data['attributes'][0]['options'] );
392
393
		$response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products' ) );
394
		$products = $response->get_data();
395
		$this->assertEquals( 3, count( $products ) );
396
	}
397
398
	/**
399
	 * Test creating a single product without permission.
400
	 *
401
	 * @since 3.0.0
402
	 */
403
	public function test_create_product_without_permission() {
404
		wp_set_current_user( 0 );
405
406
		$request = new WP_REST_Request( 'POST', '/wc/v2/products' );
407
		$request->set_body_params(
408
			array(
409
				'name'          => 'Test Product',
410
				'regular_price' => '12',
411
			)
412
		);
413
		$response = $this->server->dispatch( $request );
414
		$this->assertEquals( 401, $response->get_status() );
415
	}
416
417
	/**
418
	 * Test batch managing products.
419
	 */
420
	public function test_products_batch() {
421
		wp_set_current_user( $this->user );
422
		$product   = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
423
		$product_2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
424
		$request   = new WP_REST_Request( 'POST', '/wc/v2/products/batch' );
425
		$request->set_body_params(
426
			array(
427
				'update' => array(
428
					array(
429
						'id'          => $product->get_id(),
430
						'description' => 'Updated description.',
431
					),
432
				),
433
				'delete' => array(
434
					$product_2->get_id(),
435
				),
436
				'create' => array(
437
					array(
438
						'sku'           => 'DUMMY SKU BATCH TEST 1',
439
						'regular_price' => '10',
440
						'name'          => 'Test Batch Create 1',
441
						'type'          => 'external',
442
						'button_text'   => 'Test Button',
443
					),
444
					array(
445
						'sku'           => 'DUMMY SKU BATCH TEST 2',
446
						'regular_price' => '20',
447
						'name'          => 'Test Batch Create 2',
448
						'type'          => 'simple',
449
					),
450
				),
451
			)
452
		);
453
		$response = $this->server->dispatch( $request );
454
		$data     = $response->get_data();
455
456
		$this->assertContains( 'Updated description.', $data['update'][0]['description'] );
457
		$this->assertEquals( 'DUMMY SKU BATCH TEST 1', $data['create'][0]['sku'] );
458
		$this->assertEquals( 'DUMMY SKU BATCH TEST 2', $data['create'][1]['sku'] );
459
		$this->assertEquals( 'Test Button', $data['create'][0]['button_text'] );
460
		$this->assertEquals( 'external', $data['create'][0]['type'] );
461
		$this->assertEquals( 'simple', $data['create'][1]['type'] );
462
		$this->assertEquals( $product_2->get_id(), $data['delete'][0]['id'] );
463
464
		$request  = new WP_REST_Request( 'GET', '/wc/v2/products' );
465
		$response = $this->server->dispatch( $request );
466
		$data     = $response->get_data();
467
468
		$this->assertEquals( 3, count( $data ) );
469
	}
470
471
	/**
472
	 * Tests to make sure you can filter products post statuses by both
473
	 * the status query arg and WP_Query.
474
	 *
475
	 * @since 3.0.0
476
	 */
477
	public function test_products_filter_post_status() {
478
		wp_set_current_user( $this->user );
479
		for ( $i = 0; $i < 8; $i++ ) {
480
			$product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
481
			if ( 0 === $i % 2 ) {
482
				wp_update_post(
483
					array(
484
						'ID'          => $product->get_id(),
485
						'post_status' => 'draft',
486
					)
487
				);
488
			}
489
		}
490
491
		// Test filtering with status=publish.
492
		$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
493
		$request->set_param( 'status', 'publish' );
494
		$response = $this->server->dispatch( $request );
495
		$products = $response->get_data();
496
497
		$this->assertEquals( 4, count( $products ) );
498
		foreach ( $products as $product ) {
499
			$this->assertEquals( 'publish', $product['status'] );
500
		}
501
502
		// Test filtering with status=draft.
503
		$request = new WP_REST_Request( 'GET', '/wc/v2/products' );
504
		$request->set_param( 'status', 'draft' );
505
		$response = $this->server->dispatch( $request );
506
		$products = $response->get_data();
507
508
		$this->assertEquals( 4, count( $products ) );
509
		foreach ( $products as $product ) {
510
			$this->assertEquals( 'draft', $product['status'] );
511
		}
512
513
		// Test filtering with no filters - which should return 'any' (all 8).
514
		$request  = new WP_REST_Request( 'GET', '/wc/v2/products' );
515
		$response = $this->server->dispatch( $request );
516
		$products = $response->get_data();
517
518
		$this->assertEquals( 8, count( $products ) );
519
	}
520
521
	/**
522
	 * Test product schema.
523
	 *
524
	 * @since 3.0.0
525
	 */
526
	public function test_product_schema() {
527
		wp_set_current_user( $this->user );
528
		$product    = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product();
529
		$request    = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() );
530
		$response   = $this->server->dispatch( $request );
531
		$data       = $response->get_data();
532
		$properties = $data['schema']['properties'];
533
		$this->assertEquals( 65, count( $properties ) );
534
	}
535
}
536