1 | <?php |
||
2 | /** |
||
3 | * Tests for the product reviews REST API. |
||
4 | * |
||
5 | * @package WooCommerce\Tests\API |
||
6 | * @since 3.5.0 |
||
7 | */ |
||
8 | |||
9 | class WC_Tests_API_Product_Reviews_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->user = $this->factory->user->create( |
||
17 | array( |
||
18 | 'role' => 'administrator', |
||
19 | ) |
||
20 | ); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * Test route registration. |
||
25 | * |
||
26 | * @since 3.0.0 |
||
27 | */ |
||
28 | public function test_register_routes() { |
||
29 | $routes = $this->server->get_routes(); |
||
30 | $this->assertArrayHasKey( '/wc/v3/products/reviews', $routes ); |
||
31 | $this->assertArrayHasKey( '/wc/v3/products/reviews/(?P<id>[\d]+)', $routes ); |
||
32 | $this->assertArrayHasKey( '/wc/v3/products/reviews/batch', $routes ); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Test getting all product reviews. |
||
37 | * |
||
38 | * @since 3.0.0 |
||
39 | */ |
||
40 | public function test_get_product_reviews() { |
||
41 | wp_set_current_user( $this->user ); |
||
42 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
43 | // Create 10 products reviews for the product |
||
44 | for ( $i = 0; $i < 10; $i++ ) { |
||
45 | $review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
46 | } |
||
47 | |||
48 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); |
||
49 | $product_reviews = $response->get_data(); |
||
50 | |||
51 | $this->assertEquals( 200, $response->get_status() ); |
||
52 | $this->assertEquals( 10, count( $product_reviews ) ); |
||
53 | $this->assertContains( |
||
54 | array( |
||
55 | 'id' => $review_id, |
||
56 | 'date_created' => $product_reviews[0]['date_created'], |
||
57 | 'date_created_gmt' => $product_reviews[0]['date_created_gmt'], |
||
58 | 'product_id' => $product->get_id(), |
||
59 | 'status' => 'approved', |
||
60 | 'reviewer' => 'admin', |
||
61 | 'reviewer_email' => '[email protected]', |
||
62 | 'review' => "<p>Review content here</p>\n", |
||
63 | 'rating' => 0, |
||
64 | 'verified' => false, |
||
65 | 'reviewer_avatar_urls' => $product_reviews[0]['reviewer_avatar_urls'], |
||
66 | '_links' => array( |
||
67 | 'self' => array( |
||
68 | array( |
||
69 | 'href' => rest_url( '/wc/v3/products/reviews/' . $review_id ), |
||
70 | ), |
||
71 | ), |
||
72 | 'collection' => array( |
||
73 | array( |
||
74 | 'href' => rest_url( '/wc/v3/products/reviews' ), |
||
75 | ), |
||
76 | ), |
||
77 | 'up' => array( |
||
78 | array( |
||
79 | 'href' => rest_url( '/wc/v3/products/' . $product->get_id() ), |
||
80 | ), |
||
81 | ), |
||
82 | ), |
||
83 | ), |
||
84 | $product_reviews |
||
85 | ); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Tests to make sure product reviews cannot be viewed without valid permissions. |
||
90 | * |
||
91 | * @since 3.0.0 |
||
92 | */ |
||
93 | public function test_get_product_reviews_without_permission() { |
||
94 | wp_set_current_user( 0 ); |
||
95 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
96 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ) ); |
||
97 | $this->assertEquals( 401, $response->get_status() ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Tests to make sure an error is returned when an invalid product is loaded. |
||
102 | * |
||
103 | * @since 3.0.0 |
||
104 | */ |
||
105 | public function test_get_product_reviews_invalid_product() { |
||
106 | wp_set_current_user( $this->user ); |
||
107 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/0/reviews' ) ); |
||
108 | $this->assertEquals( 404, $response->get_status() ); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Tests getting a single product review. |
||
113 | * |
||
114 | * @since 3.0.0 |
||
115 | */ |
||
116 | public function test_get_product_review() { |
||
117 | wp_set_current_user( $this->user ); |
||
118 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
119 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
120 | |||
121 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); |
||
122 | $data = $response->get_data(); |
||
123 | |||
124 | $this->assertEquals( 200, $response->get_status() ); |
||
125 | $this->assertEquals( |
||
126 | array( |
||
127 | 'id' => $product_review_id, |
||
128 | 'date_created' => $data['date_created'], |
||
129 | 'date_created_gmt' => $data['date_created_gmt'], |
||
130 | 'product_id' => $product->get_id(), |
||
131 | 'status' => 'approved', |
||
132 | 'reviewer' => 'admin', |
||
133 | 'reviewer_email' => '[email protected]', |
||
134 | 'review' => "<p>Review content here</p>\n", |
||
135 | 'rating' => 0, |
||
136 | 'verified' => false, |
||
137 | 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], |
||
138 | ), |
||
139 | $data |
||
140 | ); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Tests getting a single product review without the correct permissions. |
||
145 | * |
||
146 | * @since 3.0.0 |
||
147 | */ |
||
148 | public function test_get_product_review_without_permission() { |
||
149 | wp_set_current_user( 0 ); |
||
150 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
151 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
152 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); |
||
153 | $this->assertEquals( 401, $response->get_status() ); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Tests getting a product review with an invalid ID. |
||
158 | * |
||
159 | * @since 3.0.0 |
||
160 | */ |
||
161 | public function test_get_product_review_invalid_id() { |
||
162 | wp_set_current_user( $this->user ); |
||
163 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
164 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/0' ) ); |
||
165 | $this->assertEquals( 404, $response->get_status() ); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Tests creating a product review. |
||
170 | * |
||
171 | * @since 3.0.0 |
||
172 | */ |
||
173 | public function test_create_product_review() { |
||
174 | wp_set_current_user( $this->user ); |
||
175 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
176 | $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); |
||
177 | $request->set_body_params( |
||
178 | array( |
||
179 | 'review' => 'Hello world.', |
||
180 | 'reviewer' => 'Admin', |
||
181 | 'reviewer_email' => '[email protected]', |
||
182 | 'rating' => '5', |
||
183 | 'product_id' => $product->get_id(), |
||
184 | ) |
||
185 | ); |
||
186 | $response = $this->server->dispatch( $request ); |
||
187 | $data = $response->get_data(); |
||
188 | |||
189 | $this->assertEquals( 201, $response->get_status() ); |
||
190 | $this->assertEquals( |
||
191 | array( |
||
192 | 'id' => $data['id'], |
||
193 | 'date_created' => $data['date_created'], |
||
194 | 'date_created_gmt' => $data['date_created_gmt'], |
||
195 | 'product_id' => $product->get_id(), |
||
196 | 'status' => 'approved', |
||
197 | 'reviewer' => 'Admin', |
||
198 | 'reviewer_email' => '[email protected]', |
||
199 | 'review' => 'Hello world.', |
||
200 | 'rating' => 5, |
||
201 | 'verified' => false, |
||
202 | 'reviewer_avatar_urls' => $data['reviewer_avatar_urls'], |
||
203 | ), |
||
204 | $data |
||
205 | ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Tests creating a product review without required fields. |
||
210 | * |
||
211 | * @since 3.0.0 |
||
212 | */ |
||
213 | public function test_create_product_review_invalid_fields() { |
||
214 | wp_set_current_user( $this->user ); |
||
215 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
216 | |||
217 | // missing review |
||
218 | $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); |
||
219 | $request->set_body_params( |
||
220 | array( |
||
221 | 'reviewer' => 'Admin', |
||
222 | 'reviewer_email' => '[email protected]', |
||
223 | ) |
||
224 | ); |
||
225 | $response = $this->server->dispatch( $request ); |
||
226 | $data = $response->get_data(); |
||
227 | |||
228 | $this->assertEquals( 400, $response->get_status() ); |
||
229 | |||
230 | // Missing reviewer. |
||
231 | $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); |
||
232 | $request->set_body_params( |
||
233 | array( |
||
234 | 'review' => 'Hello world.', |
||
235 | 'reviewer_email' => '[email protected]', |
||
236 | ) |
||
237 | ); |
||
238 | $response = $this->server->dispatch( $request ); |
||
239 | $data = $response->get_data(); |
||
240 | |||
241 | $this->assertEquals( 400, $response->get_status() ); |
||
242 | |||
243 | // missing reviewer_email |
||
244 | $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews' ); |
||
245 | $request->set_body_params( |
||
246 | array( |
||
247 | 'review' => 'Hello world.', |
||
248 | 'reviewer' => 'Admin', |
||
249 | ) |
||
250 | ); |
||
251 | $response = $this->server->dispatch( $request ); |
||
252 | $data = $response->get_data(); |
||
253 | |||
254 | $this->assertEquals( 400, $response->get_status() ); |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Tests updating a product review. |
||
259 | * |
||
260 | * @since 3.0.0 |
||
261 | */ |
||
262 | public function test_update_product_review() { |
||
263 | wp_set_current_user( $this->user ); |
||
264 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
265 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
266 | |||
267 | $response = $this->server->dispatch( new WP_REST_Request( 'GET', '/wc/v3/products/reviews/' . $product_review_id ) ); |
||
268 | $data = $response->get_data(); |
||
269 | $this->assertEquals( "<p>Review content here</p>\n", $data['review'] ); |
||
270 | $this->assertEquals( 'admin', $data['reviewer'] ); |
||
271 | $this->assertEquals( '[email protected]', $data['reviewer_email'] ); |
||
272 | $this->assertEquals( 0, $data['rating'] ); |
||
273 | |||
274 | $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); |
||
275 | $request->set_body_params( |
||
276 | array( |
||
277 | 'review' => 'Hello world - updated.', |
||
278 | 'reviewer' => 'Justin', |
||
279 | 'reviewer_email' => '[email protected]', |
||
280 | 'rating' => 3, |
||
281 | ) |
||
282 | ); |
||
283 | $response = $this->server->dispatch( $request ); |
||
284 | $data = $response->get_data(); |
||
285 | $this->assertEquals( 'Hello world - updated.', $data['review'] ); |
||
286 | $this->assertEquals( 'Justin', $data['reviewer'] ); |
||
287 | $this->assertEquals( '[email protected]', $data['reviewer_email'] ); |
||
288 | $this->assertEquals( 3, $data['rating'] ); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Tests updating a product review without the correct permissions. |
||
293 | * |
||
294 | * @since 3.0.0 |
||
295 | */ |
||
296 | public function test_update_product_review_without_permission() { |
||
297 | wp_set_current_user( 0 ); |
||
298 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
299 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
300 | |||
301 | $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/' . $product_review_id ); |
||
302 | $request->set_body_params( |
||
303 | array( |
||
304 | 'review' => 'Hello world.', |
||
305 | 'reviewer' => 'Admin', |
||
306 | 'reviewer_email' => '[email protected]', |
||
307 | ) |
||
308 | ); |
||
309 | $response = $this->server->dispatch( $request ); |
||
310 | $data = $response->get_data(); |
||
311 | |||
312 | $this->assertEquals( 401, $response->get_status() ); |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Tests that updating a product review with an invalid id fails. |
||
317 | * |
||
318 | * @since 3.0.0 |
||
319 | */ |
||
320 | public function test_update_product_review_invalid_id() { |
||
321 | wp_set_current_user( $this->user ); |
||
322 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
323 | |||
324 | $request = new WP_REST_Request( 'PUT', '/wc/v3/products/reviews/0' ); |
||
325 | $request->set_body_params( |
||
326 | array( |
||
327 | 'review' => 'Hello world.', |
||
328 | 'reviewer' => 'Admin', |
||
329 | 'reviewer_email' => '[email protected]', |
||
330 | ) |
||
331 | ); |
||
332 | $response = $this->server->dispatch( $request ); |
||
333 | $data = $response->get_data(); |
||
334 | |||
335 | $this->assertEquals( 404, $response->get_status() ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Test deleting a product review. |
||
340 | * |
||
341 | * @since 3.0.0 |
||
342 | */ |
||
343 | public function test_delete_product_review() { |
||
344 | wp_set_current_user( $this->user ); |
||
345 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
346 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
347 | |||
348 | $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); |
||
349 | $request->set_param( 'force', true ); |
||
350 | $response = $this->server->dispatch( $request ); |
||
351 | $this->assertEquals( 200, $response->get_status() ); |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Test deleting a product review without permission/creds. |
||
356 | * |
||
357 | * @since 3.0.0 |
||
358 | */ |
||
359 | public function test_delete_product_without_permission() { |
||
360 | wp_set_current_user( 0 ); |
||
361 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
362 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
363 | |||
364 | $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/' . $product_review_id ); |
||
365 | $response = $this->server->dispatch( $request ); |
||
366 | |||
367 | $this->assertEquals( 401, $response->get_status() ); |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * Test deleting a product review with an invalid id. |
||
372 | * |
||
373 | * @since 3.0.0 |
||
374 | */ |
||
375 | public function test_delete_product_review_invalid_id() { |
||
376 | wp_set_current_user( $this->user ); |
||
377 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
378 | $product_review_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
379 | |||
380 | $request = new WP_REST_Request( 'DELETE', '/wc/v3/products/reviews/0' ); |
||
381 | $request->set_param( 'force', true ); |
||
382 | $response = $this->server->dispatch( $request ); |
||
383 | |||
384 | $this->assertEquals( 404, $response->get_status() ); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Test batch managing product reviews. |
||
389 | */ |
||
390 | public function test_product_reviews_batch() { |
||
391 | wp_set_current_user( $this->user ); |
||
392 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
393 | |||
394 | $review_1_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
395 | $review_2_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
396 | $review_3_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
397 | $review_4_id = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_product_review( $product->get_id() ); |
||
398 | |||
399 | $request = new WP_REST_Request( 'POST', '/wc/v3/products/reviews/batch' ); |
||
400 | $request->set_body_params( |
||
401 | array( |
||
402 | 'update' => array( |
||
403 | array( |
||
404 | 'id' => $review_1_id, |
||
405 | 'review' => 'Updated review.', |
||
406 | ), |
||
407 | ), |
||
408 | 'delete' => array( |
||
409 | $review_2_id, |
||
410 | $review_3_id, |
||
411 | ), |
||
412 | 'create' => array( |
||
413 | array( |
||
414 | 'review' => 'New review.', |
||
415 | 'reviewer' => 'Justin', |
||
416 | 'reviewer_email' => '[email protected]', |
||
417 | 'product_id' => $product->get_id(), |
||
418 | ), |
||
419 | ), |
||
420 | ) |
||
421 | ); |
||
422 | $response = $this->server->dispatch( $request ); |
||
423 | $data = $response->get_data(); |
||
424 | |||
425 | $this->assertEquals( 'Updated review.', $data['update'][0]['review'] ); |
||
426 | $this->assertEquals( 'New review.', $data['create'][0]['review'] ); |
||
427 | $this->assertEquals( $review_2_id, $data['delete'][0]['previous']['id'] ); |
||
428 | $this->assertEquals( $review_3_id, $data['delete'][1]['previous']['id'] ); |
||
429 | |||
430 | $request = new WP_REST_Request( 'GET', '/wc/v3/products/reviews' ); |
||
431 | $request->set_param( 'product', $product->get_id() ); |
||
432 | |||
433 | $response = $this->server->dispatch( $request ); |
||
434 | $data = $response->get_data(); |
||
435 | |||
436 | $this->assertEquals( 3, count( $data ) ); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Test the product review schema. |
||
441 | * |
||
442 | * @since 3.0.0 |
||
443 | */ |
||
444 | public function test_product_review_schema() { |
||
445 | wp_set_current_user( $this->user ); |
||
446 | $product = \Automattic\WooCommerce\RestApi\UnitTests\Helpers\ProductHelper::create_simple_product(); |
||
447 | $request = new WP_REST_Request( 'OPTIONS', '/wc/v3/products/reviews' ); |
||
448 | $response = $this->server->dispatch( $request ); |
||
449 | $data = $response->get_data(); |
||
450 | $properties = $data['schema']['properties']; |
||
451 | |||
452 | $this->assertEquals( 11, count( $properties ) ); |
||
453 | $this->assertArrayHasKey( 'id', $properties ); |
||
454 | $this->assertArrayHasKey( 'date_created', $properties ); |
||
455 | $this->assertArrayHasKey( 'date_created_gmt', $properties ); |
||
456 | $this->assertArrayHasKey( 'product_id', $properties ); |
||
457 | $this->assertArrayHasKey( 'status', $properties ); |
||
458 | $this->assertArrayHasKey( 'reviewer', $properties ); |
||
459 | $this->assertArrayHasKey( 'reviewer_email', $properties ); |
||
460 | $this->assertArrayHasKey( 'review', $properties ); |
||
461 | $this->assertArrayHasKey( 'rating', $properties ); |
||
462 | $this->assertArrayHasKey( 'verified', $properties ); |
||
463 | |||
464 | if ( get_option( 'show_avatars' ) ) { |
||
465 | $this->assertArrayHasKey( 'reviewer_avatar_urls', $properties ); |
||
466 | } |
||
467 | } |
||
468 | } |
||
469 |
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