1 | <?php |
||
2 | /** |
||
3 | * Tests for Variations API. |
||
4 | * |
||
5 | * @package WooCommerce\Tests\API |
||
6 | * @since 3.0.0 |
||
7 | */ |
||
8 | |||
9 | class Product_Variations_API_V2 extends WC_REST_Unit_Test_Case { |
||
0 ignored issues
–
show
|
|||
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_Product_Variations_Controller(); |
||
17 | $this->user = $this->factory->user->create( |
||
18 | array( |
||
19 | 'role' => 'administrator', |
||
20 | ) |
||
21 | ); |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Test route registration. |
||
26 | * |
||
27 | * @since 3.0.0 |
||
28 | */ |
||
29 | public function test_register_routes() { |
||
30 | $routes = $this->server->get_routes(); |
||
31 | $this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations', $routes ); |
||
32 | $this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/(?P<id>[\d]+)', $routes ); |
||
33 | $this->assertArrayHasKey( '/wc/v2/products/(?P<product_id>[\d]+)/variations/batch', $routes ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Test getting variations. |
||
38 | * |
||
39 | * @since 3.0.0 |
||
40 | */ |
||
41 | public function test_get_variations() { |
||
42 | wp_set_current_user( $this->user ); |
||
43 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
44 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); |
||
45 | $variations = $response->get_data(); |
||
46 | $this->assertEquals( 200, $response->get_status() ); |
||
47 | $this->assertEquals( 2, count( $variations ) ); |
||
48 | $this->assertEquals( 'DUMMY SKU VARIABLE LARGE', $variations[0]['sku'] ); |
||
49 | $this->assertEquals( 'size', $variations[0]['attributes'][0]['name'] ); |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * Test getting variations without permission. |
||
54 | * |
||
55 | * @since 3.0.0 |
||
56 | */ |
||
57 | public function test_get_variations_without_permission() { |
||
58 | wp_set_current_user( 0 ); |
||
59 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
60 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); |
||
61 | $this->assertEquals( 401, $response->get_status() ); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Test getting a single variation. |
||
66 | * |
||
67 | * @since 3.0.0 |
||
68 | */ |
||
69 | public function test_get_variation() { |
||
70 | wp_set_current_user( $this->user ); |
||
71 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
72 | $children = $product->get_children(); |
||
73 | $variation_id = $children[0]; |
||
74 | |||
75 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); |
||
76 | $variation = $response->get_data(); |
||
77 | |||
78 | $this->assertEquals( 200, $response->get_status() ); |
||
79 | $this->assertEquals( $variation_id, $variation['id'] ); |
||
80 | $this->assertEquals( 'size', $variation['attributes'][0]['name'] ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Test getting single variation without permission. |
||
85 | * |
||
86 | * @since 3.0.0 |
||
87 | */ |
||
88 | public function test_get_variation_without_permission() { |
||
89 | wp_set_current_user( 0 ); |
||
90 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
91 | $children = $product->get_children(); |
||
92 | $variation_id = $children[0]; |
||
93 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); |
||
94 | $this->assertEquals( 401, $response->get_status() ); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Test deleting a single variation. |
||
99 | * |
||
100 | * @since 3.0.0 |
||
101 | */ |
||
102 | public function test_delete_variation() { |
||
103 | wp_set_current_user( $this->user ); |
||
104 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
105 | $children = $product->get_children(); |
||
106 | $variation_id = $children[0]; |
||
107 | |||
108 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
109 | $request->set_param( 'force', true ); |
||
110 | $response = $this->server->dispatch( $request ); |
||
111 | $this->assertEquals( 200, $response->get_status() ); |
||
112 | |||
113 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); |
||
114 | $variations = $response->get_data(); |
||
115 | $this->assertEquals( 1, count( $variations ) ); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Test deleting a single variation without permission. |
||
120 | * |
||
121 | * @since 3.0.0 |
||
122 | */ |
||
123 | public function test_delete_variation_without_permission() { |
||
124 | wp_set_current_user( 0 ); |
||
125 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
126 | $children = $product->get_children(); |
||
127 | $variation_id = $children[0]; |
||
128 | |||
129 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
130 | $request->set_param( 'force', true ); |
||
131 | $response = $this->server->dispatch( $request ); |
||
132 | $this->assertEquals( 401, $response->get_status() ); |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Test deleting a single variation with an invalid ID. |
||
137 | * |
||
138 | * @since 3.0.0 |
||
139 | */ |
||
140 | public function test_delete_variation_with_invalid_id() { |
||
141 | wp_set_current_user( 0 ); |
||
142 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
143 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/products/' . $product->get_id() . '/variations/0' ); |
||
144 | $request->set_param( 'force', true ); |
||
145 | $response = $this->server->dispatch( $request ); |
||
146 | $this->assertEquals( 404, $response->get_status() ); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Test editing a single variation. |
||
151 | * |
||
152 | * @since 3.0.0 |
||
153 | */ |
||
154 | public function test_update_variation() { |
||
155 | wp_set_current_user( $this->user ); |
||
156 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
157 | $children = $product->get_children(); |
||
158 | $variation_id = $children[0]; |
||
159 | |||
160 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ) ); |
||
161 | $variation = $response->get_data(); |
||
162 | |||
163 | $this->assertEquals( 'DUMMY SKU VARIABLE SMALL', $variation['sku'] ); |
||
164 | $this->assertEquals( 10, $variation['regular_price'] ); |
||
165 | $this->assertEmpty( $variation['sale_price'] ); |
||
166 | $this->assertEquals( 'small', $variation['attributes'][0]['option'] ); |
||
167 | |||
168 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
169 | $request->set_body_params( |
||
170 | array( |
||
171 | 'sku' => 'FIXED-SKU', |
||
172 | 'sale_price' => '8', |
||
173 | 'description' => 'O_O', |
||
174 | 'image' => array( |
||
175 | 'position' => 0, |
||
176 | 'src' => 'http://cldup.com/Dr1Bczxq4q.png', |
||
177 | 'alt' => 'test upload image', |
||
178 | ), |
||
179 | 'attributes' => array( |
||
180 | array( |
||
181 | 'name' => 'pa_size', |
||
182 | 'option' => 'medium', |
||
183 | ), |
||
184 | ), |
||
185 | ) |
||
186 | ); |
||
187 | $response = $this->server->dispatch( $request ); |
||
188 | $variation = $response->get_data(); |
||
189 | |||
190 | $this->assertTrue( isset( $variation['description'] ), print_r( $variation, true ) ); |
||
191 | $this->assertContains( 'O_O', $variation['description'], print_r( $variation, true ) ); |
||
192 | $this->assertEquals( '8', $variation['price'], print_r( $variation, true ) ); |
||
193 | $this->assertEquals( '8', $variation['sale_price'], print_r( $variation, true ) ); |
||
194 | $this->assertEquals( '10', $variation['regular_price'], print_r( $variation, true ) ); |
||
195 | $this->assertEquals( 'FIXED-SKU', $variation['sku'], print_r( $variation, true ) ); |
||
196 | $this->assertEquals( 'medium', $variation['attributes'][0]['option'], print_r( $variation, true ) ); |
||
197 | $this->assertContains( 'Dr1Bczxq4q', $variation['image']['src'], print_r( $variation, true ) ); |
||
198 | $this->assertContains( 'test upload image', $variation['image']['alt'], print_r( $variation, true ) ); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Test updating a single variation without permission. |
||
203 | * |
||
204 | * @since 3.0.0 |
||
205 | */ |
||
206 | public function test_update_variation_without_permission() { |
||
207 | wp_set_current_user( 0 ); |
||
208 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
209 | $children = $product->get_children(); |
||
210 | $variation_id = $children[0]; |
||
211 | |||
212 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
213 | $request->set_body_params( |
||
214 | array( |
||
215 | 'sku' => 'FIXED-SKU-NO-PERMISSION', |
||
216 | ) |
||
217 | ); |
||
218 | $response = $this->server->dispatch( $request ); |
||
219 | $this->assertEquals( 401, $response->get_status() ); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Test updating a single variation with an invalid ID. |
||
224 | * |
||
225 | * @since 3.0.0 |
||
226 | */ |
||
227 | public function test_update_variation_with_invalid_id() { |
||
228 | wp_set_current_user( $this->user ); |
||
229 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
230 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/0' ); |
||
231 | $request->set_body_params( |
||
232 | array( |
||
233 | 'sku' => 'FIXED-SKU-NO-PERMISSION', |
||
234 | ) |
||
235 | ); |
||
236 | $response = $this->server->dispatch( $request ); |
||
237 | $this->assertEquals( 400, $response->get_status() ); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Test creating a single variation. |
||
242 | * |
||
243 | * @since 3.0.0 |
||
244 | */ |
||
245 | public function test_create_variation() { |
||
246 | wp_set_current_user( $this->user ); |
||
247 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
248 | |||
249 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); |
||
250 | $variations = $response->get_data(); |
||
251 | $this->assertEquals( 2, count( $variations ) ); |
||
252 | |||
253 | $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' ); |
||
254 | $request->set_body_params( |
||
255 | array( |
||
256 | 'sku' => 'DUMMY SKU VARIABLE MEDIUM', |
||
257 | 'regular_price' => '12', |
||
258 | 'description' => 'A medium size.', |
||
259 | 'attributes' => array( |
||
260 | array( |
||
261 | 'name' => 'pa_size', |
||
262 | 'option' => 'medium', |
||
263 | ), |
||
264 | ), |
||
265 | ) |
||
266 | ); |
||
267 | $response = $this->server->dispatch( $request ); |
||
268 | $variation = $response->get_data(); |
||
269 | |||
270 | $this->assertContains( 'A medium size.', $variation['description'] ); |
||
271 | $this->assertEquals( '12', $variation['price'] ); |
||
272 | $this->assertEquals( '12', $variation['regular_price'] ); |
||
273 | $this->assertTrue( $variation['purchasable'] ); |
||
274 | $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $variation['sku'] ); |
||
275 | $this->assertEquals( 'medium', $variation['attributes'][0]['option'] ); |
||
276 | |||
277 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ) ); |
||
278 | $variations = $response->get_data(); |
||
279 | $this->assertEquals( 3, count( $variations ) ); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Test creating a single variation without permission. |
||
284 | * |
||
285 | * @since 3.0.0 |
||
286 | */ |
||
287 | public function test_create_variation_without_permission() { |
||
288 | wp_set_current_user( 0 ); |
||
289 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
290 | |||
291 | $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations' ); |
||
292 | $request->set_body_params( |
||
293 | array( |
||
294 | 'sku' => 'DUMMY SKU VARIABLE MEDIUM', |
||
295 | 'regular_price' => '12', |
||
296 | 'description' => 'A medium size.', |
||
297 | 'attributes' => array( |
||
298 | array( |
||
299 | 'name' => 'pa_size', |
||
300 | 'option' => 'medium', |
||
301 | ), |
||
302 | ), |
||
303 | ) |
||
304 | ); |
||
305 | $response = $this->server->dispatch( $request ); |
||
306 | $this->assertEquals( 401, $response->get_status() ); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * Test batch managing product variations. |
||
311 | */ |
||
312 | public function test_product_variations_batch() { |
||
313 | wp_set_current_user( $this->user ); |
||
314 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
315 | $children = $product->get_children(); |
||
316 | $request = new WP_REST_Request( 'POST', '/wc/v2/products/' . $product->get_id() . '/variations/batch' ); |
||
317 | $request->set_body_params( |
||
318 | array( |
||
319 | 'update' => array( |
||
320 | array( |
||
321 | 'id' => $children[0], |
||
322 | 'description' => 'Updated description.', |
||
323 | 'image' => array( |
||
324 | 'position' => 0, |
||
325 | 'src' => 'http://cldup.com/Dr1Bczxq4q.png', |
||
326 | 'alt' => 'test upload image', |
||
327 | ), |
||
328 | ), |
||
329 | ), |
||
330 | 'delete' => array( |
||
331 | $children[1], |
||
332 | ), |
||
333 | 'create' => array( |
||
334 | array( |
||
335 | 'sku' => 'DUMMY SKU VARIABLE MEDIUM', |
||
336 | 'regular_price' => '12', |
||
337 | 'description' => 'A medium size.', |
||
338 | 'attributes' => array( |
||
339 | array( |
||
340 | 'name' => 'pa_size', |
||
341 | 'option' => 'medium', |
||
342 | ), |
||
343 | ), |
||
344 | ), |
||
345 | ), |
||
346 | ) |
||
347 | ); |
||
348 | $response = $this->server->dispatch( $request ); |
||
349 | $data = $response->get_data(); |
||
350 | |||
351 | $this->assertContains( 'Updated description.', $data['update'][0]['description'] ); |
||
352 | $this->assertEquals( 'DUMMY SKU VARIABLE MEDIUM', $data['create'][0]['sku'] ); |
||
353 | $this->assertEquals( 'medium', $data['create'][0]['attributes'][0]['option'] ); |
||
354 | $this->assertEquals( $children[1], $data['delete'][0]['id'] ); |
||
355 | |||
356 | $request = new WP_REST_Request( 'GET', '/wc/v2/products/' . $product->get_id() . '/variations' ); |
||
357 | $response = $this->server->dispatch( $request ); |
||
358 | $data = $response->get_data(); |
||
359 | |||
360 | $this->assertEquals( 2, count( $data ) ); |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * Test variation schema. |
||
365 | * |
||
366 | * @since 3.0.0 |
||
367 | */ |
||
368 | public function test_variation_schema() { |
||
369 | wp_set_current_user( $this->user ); |
||
370 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
371 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/products/' . $product->get_id() . '/variations' ); |
||
372 | $response = $this->server->dispatch( $request ); |
||
373 | $data = $response->get_data(); |
||
374 | $properties = $data['schema']['properties']; |
||
375 | |||
376 | $this->assertEquals( 37, count( $properties ) ); |
||
377 | $this->assertArrayHasKey( 'id', $properties ); |
||
378 | $this->assertArrayHasKey( 'date_created', $properties ); |
||
379 | $this->assertArrayHasKey( 'date_modified', $properties ); |
||
380 | $this->assertArrayHasKey( 'description', $properties ); |
||
381 | $this->assertArrayHasKey( 'permalink', $properties ); |
||
382 | $this->assertArrayHasKey( 'sku', $properties ); |
||
383 | $this->assertArrayHasKey( 'price', $properties ); |
||
384 | $this->assertArrayHasKey( 'regular_price', $properties ); |
||
385 | $this->assertArrayHasKey( 'sale_price', $properties ); |
||
386 | $this->assertArrayHasKey( 'date_on_sale_from', $properties ); |
||
387 | $this->assertArrayHasKey( 'date_on_sale_to', $properties ); |
||
388 | $this->assertArrayHasKey( 'on_sale', $properties ); |
||
389 | $this->assertArrayHasKey( 'visible', $properties ); |
||
390 | $this->assertArrayHasKey( 'purchasable', $properties ); |
||
391 | $this->assertArrayHasKey( 'virtual', $properties ); |
||
392 | $this->assertArrayHasKey( 'downloadable', $properties ); |
||
393 | $this->assertArrayHasKey( 'downloads', $properties ); |
||
394 | $this->assertArrayHasKey( 'download_limit', $properties ); |
||
395 | $this->assertArrayHasKey( 'download_expiry', $properties ); |
||
396 | $this->assertArrayHasKey( 'tax_status', $properties ); |
||
397 | $this->assertArrayHasKey( 'tax_class', $properties ); |
||
398 | $this->assertArrayHasKey( 'manage_stock', $properties ); |
||
399 | $this->assertArrayHasKey( 'stock_quantity', $properties ); |
||
400 | $this->assertArrayHasKey( 'in_stock', $properties ); |
||
401 | $this->assertArrayHasKey( 'backorders', $properties ); |
||
402 | $this->assertArrayHasKey( 'backorders_allowed', $properties ); |
||
403 | $this->assertArrayHasKey( 'backordered', $properties ); |
||
404 | $this->assertArrayHasKey( 'weight', $properties ); |
||
405 | $this->assertArrayHasKey( 'dimensions', $properties ); |
||
406 | $this->assertArrayHasKey( 'shipping_class', $properties ); |
||
407 | $this->assertArrayHasKey( 'shipping_class_id', $properties ); |
||
408 | $this->assertArrayHasKey( 'image', $properties ); |
||
409 | $this->assertArrayHasKey( 'attributes', $properties ); |
||
410 | $this->assertArrayHasKey( 'menu_order', $properties ); |
||
411 | $this->assertArrayHasKey( 'meta_data', $properties ); |
||
412 | } |
||
413 | |||
414 | /** |
||
415 | * Test updating a variation stock. |
||
416 | * |
||
417 | * @since 3.0.0 |
||
418 | */ |
||
419 | public function test_update_variation_manage_stock() { |
||
420 | wp_set_current_user( $this->user ); |
||
421 | |||
422 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_variation_product(); |
||
423 | $product->set_manage_stock( false ); |
||
424 | $product->save(); |
||
425 | |||
426 | $children = $product->get_children(); |
||
427 | $variation_id = $children[0]; |
||
428 | |||
429 | // Set stock to true. |
||
430 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
431 | $request->set_body_params( |
||
432 | array( |
||
433 | 'manage_stock' => true, |
||
434 | ) |
||
435 | ); |
||
436 | |||
437 | $response = $this->server->dispatch( $request ); |
||
438 | $variation = $response->get_data(); |
||
439 | |||
440 | $this->assertEquals( 200, $response->get_status() ); |
||
441 | $this->assertEquals( true, $variation['manage_stock'] ); |
||
442 | |||
443 | // Set stock to false. |
||
444 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
445 | $request->set_body_params( |
||
446 | array( |
||
447 | 'manage_stock' => false, |
||
448 | ) |
||
449 | ); |
||
450 | |||
451 | $response = $this->server->dispatch( $request ); |
||
452 | $variation = $response->get_data(); |
||
453 | |||
454 | $this->assertEquals( 200, $response->get_status() ); |
||
455 | $this->assertEquals( false, $variation['manage_stock'] ); |
||
456 | |||
457 | // Set stock to false but parent is managing stock. |
||
458 | $product->set_manage_stock( true ); |
||
459 | $product->save(); |
||
460 | $request = new WP_REST_Request( 'PUT', '/wc/v2/products/' . $product->get_id() . '/variations/' . $variation_id ); |
||
461 | $request->set_body_params( |
||
462 | array( |
||
463 | 'manage_stock' => false, |
||
464 | ) |
||
465 | ); |
||
466 | |||
467 | $response = $this->server->dispatch( $request ); |
||
468 | $variation = $response->get_data(); |
||
469 | |||
470 | $this->assertEquals( 200, $response->get_status() ); |
||
471 | $this->assertEquals( 'parent', $variation['manage_stock'] ); |
||
472 | } |
||
473 | } |
||
474 |
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