1 | <?php |
||
2 | /** |
||
3 | * Coupon API Tests |
||
4 | * @package WooCommerce\Tests\API |
||
5 | * @since 3.0.0 |
||
6 | */ |
||
7 | class WC_Tests_API_Coupons_V2 extends WC_REST_Unit_Test_Case { |
||
0 ignored issues
–
show
|
|||
8 | |||
9 | protected $endpoint; |
||
10 | |||
11 | /** |
||
12 | * Setup test coupon data. |
||
13 | * @since 3.0.0 |
||
14 | */ |
||
15 | public function setUp() { |
||
16 | parent::setUp(); |
||
17 | $this->endpoint = new WC_REST_Coupons_Controller(); |
||
18 | $this->user = $this->factory->user->create( |
||
0 ignored issues
–
show
|
|||
19 | array( |
||
20 | 'role' => 'administrator', |
||
21 | ) |
||
22 | ); |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Test route registration. |
||
27 | * @since 3.0.0 |
||
28 | */ |
||
29 | public function test_register_routes() { |
||
30 | $routes = $this->server->get_routes(); |
||
31 | $this->assertArrayHasKey( '/wc/v2/coupons', $routes ); |
||
32 | $this->assertArrayHasKey( '/wc/v2/coupons/(?P<id>[\d]+)', $routes ); |
||
33 | $this->assertArrayHasKey( '/wc/v2/coupons/batch', $routes ); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * Test getting coupons. |
||
38 | * @since 3.0.0 |
||
39 | */ |
||
40 | public function test_get_coupons() { |
||
41 | wp_set_current_user( $this->user ); |
||
42 | |||
43 | $coupon_1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
44 | $post_1 = get_post( $coupon_1->get_id() ); |
||
45 | $coupon_2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-2' ); |
||
46 | |||
47 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) ); |
||
48 | $coupons = $response->get_data(); |
||
49 | |||
50 | $this->assertEquals( 200, $response->get_status() ); |
||
51 | $this->assertEquals( 2, count( $coupons ) ); |
||
52 | $this->assertContains( |
||
53 | array( |
||
54 | 'id' => $coupon_1->get_id(), |
||
55 | 'code' => 'dummycoupon-1', |
||
56 | 'amount' => '1.00', |
||
57 | 'date_created' => wc_rest_prepare_date_response( $post_1->post_date_gmt, false ), |
||
58 | 'date_created_gmt' => wc_rest_prepare_date_response( $post_1->post_date_gmt ), |
||
59 | 'date_modified' => wc_rest_prepare_date_response( $post_1->post_modified_gmt, false ), |
||
60 | 'date_modified_gmt' => wc_rest_prepare_date_response( $post_1->post_modified_gmt ), |
||
61 | 'discount_type' => 'fixed_cart', |
||
62 | 'description' => 'This is a dummy coupon', |
||
63 | 'date_expires' => '', |
||
64 | 'date_expires_gmt' => '', |
||
65 | 'usage_count' => 0, |
||
66 | 'individual_use' => false, |
||
67 | 'product_ids' => array(), |
||
68 | 'excluded_product_ids' => array(), |
||
69 | 'usage_limit' => '', |
||
70 | 'usage_limit_per_user' => '', |
||
71 | 'limit_usage_to_x_items' => null, |
||
72 | 'free_shipping' => false, |
||
73 | 'product_categories' => array(), |
||
74 | 'excluded_product_categories' => array(), |
||
75 | 'exclude_sale_items' => false, |
||
76 | 'minimum_amount' => '0.00', |
||
77 | 'maximum_amount' => '0.00', |
||
78 | 'email_restrictions' => array(), |
||
79 | 'used_by' => array(), |
||
80 | 'meta_data' => array(), |
||
81 | '_links' => array( |
||
82 | 'self' => array( |
||
83 | array( |
||
84 | 'href' => rest_url( '/wc/v2/coupons/' . $coupon_1->get_id() ), |
||
85 | ), |
||
86 | ), |
||
87 | 'collection' => array( |
||
88 | array( |
||
89 | 'href' => rest_url( '/wc/v2/coupons' ), |
||
90 | ), |
||
91 | ), |
||
92 | ), |
||
93 | ), |
||
94 | $coupons |
||
95 | ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Test getting coupons without valid permissions. |
||
100 | * @since 3.0.0 |
||
101 | */ |
||
102 | public function test_get_coupons_without_permission() { |
||
103 | wp_set_current_user( 0 ); |
||
104 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons' ) ); |
||
105 | $this->assertEquals( 401, $response->get_status() ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Test getting a single coupon. |
||
110 | * @since 3.0.0 |
||
111 | */ |
||
112 | public function test_get_coupon() { |
||
113 | wp_set_current_user( $this->user ); |
||
114 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
115 | $post = get_post( $coupon->get_id() ); |
||
116 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); |
||
117 | $data = $response->get_data(); |
||
118 | |||
119 | $this->assertEquals( 200, $response->get_status() ); |
||
120 | $this->assertEquals( |
||
121 | array( |
||
122 | 'id' => $coupon->get_id(), |
||
123 | 'code' => 'dummycoupon-1', |
||
124 | 'amount' => '1.00', |
||
125 | 'date_created' => wc_rest_prepare_date_response( $post->post_date_gmt, false ), |
||
126 | 'date_created_gmt' => wc_rest_prepare_date_response( $post->post_date_gmt ), |
||
127 | 'date_modified' => wc_rest_prepare_date_response( $post->post_modified_gmt, false ), |
||
128 | 'date_modified_gmt' => wc_rest_prepare_date_response( $post->post_modified_gmt ), |
||
129 | 'discount_type' => 'fixed_cart', |
||
130 | 'description' => 'This is a dummy coupon', |
||
131 | 'date_expires' => null, |
||
132 | 'date_expires_gmt' => null, |
||
133 | 'usage_count' => 0, |
||
134 | 'individual_use' => false, |
||
135 | 'product_ids' => array(), |
||
136 | 'excluded_product_ids' => array(), |
||
137 | 'usage_limit' => null, |
||
138 | 'usage_limit_per_user' => null, |
||
139 | 'limit_usage_to_x_items' => null, |
||
140 | 'free_shipping' => false, |
||
141 | 'product_categories' => array(), |
||
142 | 'excluded_product_categories' => array(), |
||
143 | 'exclude_sale_items' => false, |
||
144 | 'minimum_amount' => '0.00', |
||
145 | 'maximum_amount' => '0.00', |
||
146 | 'email_restrictions' => array(), |
||
147 | 'used_by' => array(), |
||
148 | 'meta_data' => array(), |
||
149 | ), |
||
150 | $data |
||
151 | ); |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Test getting a single coupon with an invalid ID. |
||
156 | * @since 3.0.0 |
||
157 | */ |
||
158 | public function test_get_coupon_invalid_id() { |
||
159 | wp_set_current_user( $this->user ); |
||
160 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/0' ) ); |
||
161 | $this->assertEquals( 404, $response->get_status() ); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Test getting a single coupon without valid permissions. |
||
166 | * @since 3.0.0 |
||
167 | */ |
||
168 | public function test_get_coupon_without_permission() { |
||
169 | wp_set_current_user( 0 ); |
||
170 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
171 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); |
||
172 | $this->assertEquals( 401, $response->get_status() ); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Test creating a single coupon. |
||
177 | * @since 3.0.0 |
||
178 | */ |
||
179 | public function test_create_coupon() { |
||
180 | wp_set_current_user( $this->user ); |
||
181 | $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); |
||
182 | $request->set_body_params( |
||
183 | array( |
||
184 | 'code' => 'test', |
||
185 | 'amount' => '5.00', |
||
186 | 'discount_type' => 'fixed_product', |
||
187 | 'description' => 'Test', |
||
188 | 'usage_limit' => 10, |
||
189 | ) |
||
190 | ); |
||
191 | $response = $this->server->dispatch( $request ); |
||
192 | $data = $response->get_data(); |
||
193 | |||
194 | $this->assertEquals( 201, $response->get_status() ); |
||
195 | $this->assertEquals( |
||
196 | array( |
||
197 | 'id' => $data['id'], |
||
198 | 'code' => 'test', |
||
199 | 'amount' => '5.00', |
||
200 | 'date_created' => $data['date_created'], |
||
201 | 'date_created_gmt' => $data['date_created_gmt'], |
||
202 | 'date_modified' => $data['date_modified'], |
||
203 | 'date_modified_gmt' => $data['date_modified_gmt'], |
||
204 | 'discount_type' => 'fixed_product', |
||
205 | 'description' => 'Test', |
||
206 | 'date_expires' => null, |
||
207 | 'date_expires_gmt' => null, |
||
208 | 'usage_count' => 0, |
||
209 | 'individual_use' => false, |
||
210 | 'product_ids' => array(), |
||
211 | 'excluded_product_ids' => array(), |
||
212 | 'usage_limit' => 10, |
||
213 | 'usage_limit_per_user' => null, |
||
214 | 'limit_usage_to_x_items' => null, |
||
215 | 'free_shipping' => false, |
||
216 | 'product_categories' => array(), |
||
217 | 'excluded_product_categories' => array(), |
||
218 | 'exclude_sale_items' => false, |
||
219 | 'minimum_amount' => '0.00', |
||
220 | 'maximum_amount' => '0.00', |
||
221 | 'email_restrictions' => array(), |
||
222 | 'used_by' => array(), |
||
223 | 'meta_data' => array(), |
||
224 | ), |
||
225 | $data |
||
226 | ); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Test creating a single coupon with invalid fields. |
||
231 | * @since 3.0.0 |
||
232 | */ |
||
233 | public function test_create_coupon_invalid_fields() { |
||
234 | wp_set_current_user( $this->user ); |
||
235 | |||
236 | // test no code... |
||
237 | $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); |
||
238 | $request->set_body_params( |
||
239 | array( |
||
240 | 'amount' => '5.00', |
||
241 | 'discount_type' => 'fixed_product', |
||
242 | ) |
||
243 | ); |
||
244 | $response = $this->server->dispatch( $request ); |
||
245 | $data = $response->get_data(); |
||
246 | |||
247 | $this->assertEquals( 400, $response->get_status() ); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Test creating a single coupon without valid permissions. |
||
252 | * @since 3.0.0 |
||
253 | */ |
||
254 | public function test_create_coupon_without_permission() { |
||
255 | wp_set_current_user( 0 ); |
||
256 | |||
257 | // test no code... |
||
258 | $request = new WP_REST_Request( 'POST', '/wc/v2/coupons' ); |
||
259 | $request->set_body_params( |
||
260 | array( |
||
261 | 'code' => 'fail', |
||
262 | 'amount' => '5.00', |
||
263 | 'discount_type' => 'fixed_product', |
||
264 | ) |
||
265 | ); |
||
266 | $response = $this->server->dispatch( $request ); |
||
267 | $data = $response->get_data(); |
||
268 | |||
269 | $this->assertEquals( 401, $response->get_status() ); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * Test updating a single coupon. |
||
274 | * @since 3.0.0 |
||
275 | */ |
||
276 | public function test_update_coupon() { |
||
277 | wp_set_current_user( $this->user ); |
||
278 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
279 | $post = get_post( $coupon->get_id() ); |
||
280 | |||
281 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v2/coupons/' . $coupon->get_id() ) ); |
||
282 | $data = $response->get_data(); |
||
283 | $this->assertEquals( 'This is a dummy coupon', $data['description'] ); |
||
284 | $this->assertEquals( 'fixed_cart', $data['discount_type'] ); |
||
285 | $this->assertEquals( '1.00', $data['amount'] ); |
||
286 | |||
287 | $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() ); |
||
288 | $request->set_body_params( |
||
289 | array( |
||
290 | 'amount' => '10.00', |
||
291 | 'description' => 'New description', |
||
292 | ) |
||
293 | ); |
||
294 | $response = $this->server->dispatch( $request ); |
||
295 | $data = $response->get_data(); |
||
296 | |||
297 | $this->assertEquals( '10.00', $data['amount'] ); |
||
298 | $this->assertEquals( 'New description', $data['description'] ); |
||
299 | $this->assertEquals( 'fixed_cart', $data['discount_type'] ); |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Test updating a single coupon with an invalid ID. |
||
304 | * @since 3.0.0 |
||
305 | */ |
||
306 | public function test_update_coupon_invalid_id() { |
||
307 | wp_set_current_user( $this->user ); |
||
308 | |||
309 | $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/0' ); |
||
310 | $request->set_body_params( |
||
311 | array( |
||
312 | 'code' => 'tester', |
||
313 | 'amount' => '10.00', |
||
314 | 'description' => 'New description', |
||
315 | ) |
||
316 | ); |
||
317 | $response = $this->server->dispatch( $request ); |
||
318 | $data = $response->get_data(); |
||
319 | |||
320 | $this->assertEquals( 400, $response->get_status() ); |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * Test updating a single coupon without valid permissions. |
||
325 | * @since 3.0.0 |
||
326 | */ |
||
327 | public function test_update_coupon_without_permission() { |
||
328 | wp_set_current_user( 0 ); |
||
329 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
330 | $post = get_post( $coupon->get_id() ); |
||
331 | |||
332 | $request = new WP_REST_Request( 'PUT', '/wc/v2/coupons/' . $coupon->get_id() ); |
||
333 | $request->set_body_params( |
||
334 | array( |
||
335 | 'amount' => '10.00', |
||
336 | 'description' => 'New description', |
||
337 | ) |
||
338 | ); |
||
339 | $response = $this->server->dispatch( $request ); |
||
340 | |||
341 | $this->assertEquals( 401, $response->get_status() ); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Test deleting a single coupon. |
||
346 | * @since 3.0.0 |
||
347 | */ |
||
348 | public function test_delete_coupon() { |
||
349 | wp_set_current_user( $this->user ); |
||
350 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
351 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() ); |
||
352 | $request->set_param( 'force', true ); |
||
353 | $response = $this->server->dispatch( $request ); |
||
354 | $this->assertEquals( 200, $response->get_status() ); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Test deleting a single coupon with an invalid ID. |
||
359 | * @since 3.0.0 |
||
360 | */ |
||
361 | public function test_delete_coupon_invalid_id() { |
||
362 | wp_set_current_user( $this->user ); |
||
363 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/0' ); |
||
364 | $request->set_param( 'force', true ); |
||
365 | $response = $this->server->dispatch( $request ); |
||
366 | |||
367 | $this->assertEquals( 404, $response->get_status() ); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Test deleting a single coupon without valid permissions. |
||
372 | * @since 3.0.0 |
||
373 | */ |
||
374 | public function test_delete_coupon_without_permission() { |
||
375 | wp_set_current_user( 0 ); |
||
376 | $coupon = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
377 | $request = new WP_REST_Request( 'DELETE', '/wc/v2/coupons/' . $coupon->get_id() ); |
||
378 | $response = $this->server->dispatch( $request ); |
||
379 | |||
380 | $this->assertEquals( 401, $response->get_status() ); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Test batch operations on coupons. |
||
385 | * @since 3.0.0 |
||
386 | */ |
||
387 | public function test_batch_coupon() { |
||
388 | wp_set_current_user( $this->user ); |
||
389 | |||
390 | $coupon_1 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-1' ); |
||
391 | $coupon_2 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-2' ); |
||
392 | $coupon_3 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-3' ); |
||
393 | $coupon_4 = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\CouponHelper::create_coupon( 'dummycoupon-4' ); |
||
394 | |||
395 | $request = new WP_REST_Request( 'POST', '/wc/v2/coupons/batch' ); |
||
396 | $request->set_body_params( |
||
397 | array( |
||
398 | 'update' => array( |
||
399 | array( |
||
400 | 'id' => $coupon_1->get_id(), |
||
401 | 'amount' => '5.15', |
||
402 | ), |
||
403 | ), |
||
404 | 'delete' => array( |
||
405 | $coupon_2->get_id(), |
||
406 | $coupon_3->get_id(), |
||
407 | ), |
||
408 | 'create' => array( |
||
409 | array( |
||
410 | 'code' => 'new-coupon', |
||
411 | 'amount' => '11.00', |
||
412 | ), |
||
413 | ), |
||
414 | ) |
||
415 | ); |
||
416 | $response = $this->server->dispatch( $request ); |
||
417 | $data = $response->get_data(); |
||
418 | |||
419 | $this->assertEquals( '5.15', $data['update'][0]['amount'] ); |
||
420 | $this->assertEquals( '11.00', $data['create'][0]['amount'] ); |
||
421 | $this->assertEquals( 'new-coupon', $data['create'][0]['code'] ); |
||
422 | $this->assertEquals( $coupon_2->get_id(), $data['delete'][0]['id'] ); |
||
423 | $this->assertEquals( $coupon_3->get_id(), $data['delete'][1]['id'] ); |
||
424 | |||
425 | $request = new WP_REST_Request( 'GET', '/wc/v2/coupons' ); |
||
426 | $response = $this->server->dispatch( $request ); |
||
427 | $data = $response->get_data(); |
||
428 | |||
429 | $this->assertEquals( 3, count( $data ) ); |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Test coupon schema. |
||
434 | * @since 3.0.0 |
||
435 | */ |
||
436 | public function test_coupon_schema() { |
||
437 | wp_set_current_user( $this->user ); |
||
438 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v2/coupons' ); |
||
439 | $response = $this->server->dispatch( $request ); |
||
440 | $data = $response->get_data(); |
||
441 | $properties = $data['schema']['properties']; |
||
442 | |||
443 | $this->assertEquals( 27, count( $properties ) ); |
||
444 | $this->assertArrayHasKey( 'id', $properties ); |
||
445 | $this->assertArrayHasKey( 'code', $properties ); |
||
446 | $this->assertArrayHasKey( 'date_created', $properties ); |
||
447 | $this->assertArrayHasKey( 'date_created_gmt', $properties ); |
||
448 | $this->assertArrayHasKey( 'date_modified', $properties ); |
||
449 | $this->assertArrayHasKey( 'date_modified_gmt', $properties ); |
||
450 | $this->assertArrayHasKey( 'description', $properties ); |
||
451 | $this->assertArrayHasKey( 'discount_type', $properties ); |
||
452 | $this->assertArrayHasKey( 'amount', $properties ); |
||
453 | $this->assertArrayHasKey( 'date_expires', $properties ); |
||
454 | $this->assertArrayHasKey( 'date_expires_gmt', $properties ); |
||
455 | $this->assertArrayHasKey( 'usage_count', $properties ); |
||
456 | $this->assertArrayHasKey( 'individual_use', $properties ); |
||
457 | $this->assertArrayHasKey( 'product_ids', $properties ); |
||
458 | $this->assertArrayHasKey( 'excluded_product_ids', $properties ); |
||
459 | $this->assertArrayHasKey( 'usage_limit', $properties ); |
||
460 | $this->assertArrayHasKey( 'usage_limit_per_user', $properties ); |
||
461 | $this->assertArrayHasKey( 'limit_usage_to_x_items', $properties ); |
||
462 | $this->assertArrayHasKey( 'free_shipping', $properties ); |
||
463 | $this->assertArrayHasKey( 'product_categories', $properties ); |
||
464 | $this->assertArrayHasKey( 'excluded_product_categories', $properties ); |
||
465 | $this->assertArrayHasKey( 'exclude_sale_items', $properties ); |
||
466 | $this->assertArrayHasKey( 'minimum_amount', $properties ); |
||
467 | $this->assertArrayHasKey( 'maximum_amount', $properties ); |
||
468 | $this->assertArrayHasKey( 'email_restrictions', $properties ); |
||
469 | $this->assertArrayHasKey( 'used_by', $properties ); |
||
470 | } |
||
471 | } |
||
472 |
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