Skip to content

Commit 1ee7a86

Browse files
Add additional tests
1 parent 5fba5f6 commit 1ee7a86

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1680,7 +1680,7 @@ public function get_collection_params() {
16801680
$query_params['status'] = array(
16811681
'default' => 'approve',
16821682
'description' => __( 'Limit result set to comments assigned a specific status. Requires authorization.' ),
1683-
'sanitize_callback' => 'sanitize_comment_statuses',
1683+
'sanitize_callback' => array( $this, 'sanitize_comment_statuses' ),
16841684
'type' => 'array',
16851685
'validate_callback' => 'rest_validate_request_arg',
16861686
);

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ public function test_get_items() {
227227

228228
/**
229229
* Test getting items of a specific status.
230+
*
231+
* @ticket 63982
230232
*/
231233
public function test_get_items_by_status() {
232234
wp_set_current_user( self::$admin_id );
@@ -253,6 +255,8 @@ public function test_get_items_by_status() {
253255

254256
/**
255257
* Test getting comments of all statuses.
258+
*
259+
* @ticket 63982
256260
*/
257261
public function test_get_items_by_all_status() {
258262
wp_set_current_user( self::$admin_id );
@@ -278,6 +282,8 @@ public function test_get_items_by_all_status() {
278282

279283
/**
280284
* Test getting items of multiple statuses.
285+
*
286+
* @ticket 63982
281287
*/
282288
public function test_get_items_by_multiple_status() {
283289
wp_set_current_user( self::$admin_id );
@@ -301,6 +307,69 @@ public function test_get_items_by_multiple_status() {
301307
$this->assertCount( $found, $comments );
302308
}
303309

310+
/**
311+
* Test sanization of the status parameter.
312+
*
313+
* @ticket 63982
314+
*
315+
* @dataProvider data_get_items_by_status_sanitize
316+
*/
317+
public function test_get_items_by_status_sanitize( $key, $expected ) {
318+
wp_set_current_user( self::$admin_id );
319+
320+
// Create a post with the test status.
321+
$params = array(
322+
'post' => self::$post_id,
323+
'author_name' => 'Comic Book Guy',
324+
'author_email' => '[email protected]',
325+
'author_url' => 'http://androidsdungeon.com',
326+
'content' => 'Worst Comment Ever!',
327+
'status' => $key,
328+
);
329+
330+
$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
331+
$request->add_header( 'Content-Type', 'application/json' );
332+
$request->set_body( wp_json_encode( $params ) );
333+
334+
$response = rest_get_server()->dispatch( $request );
335+
$this->assertSame( 201, $response->get_status() );
336+
337+
$comment = $response->get_data();
338+
339+
$this->assertEquals( $expected, $comment['status'] );
340+
}
341+
342+
/**
343+
* Data provider.
344+
*
345+
* @return array
346+
*/
347+
public function data_get_items_by_status_sanitize() {
348+
return array(
349+
'an empty string key' => array(
350+
'key' => '',
351+
'expected' => 'hold',
352+
),
353+
'a lowercase key with commas' => array(
354+
'key' => 'howdy,admin',
355+
'expected' => 'hold',
356+
),
357+
'a lowercase key with commas' => array(
358+
'key' => 'HOWDY,ADMIN',
359+
'expected' => 'hold',
360+
),
361+
'a mixed case key with commas' => array(
362+
'key' => 'HoWdY,aDmIn',
363+
'expected' => 'hold',
364+
),
365+
'a string with unicode' => array(
366+
'key' => array( 'howdy admin', 'another-value' ),
367+
'expected' => 'hold',
368+
),
369+
);
370+
}
371+
372+
304373
/**
305374
* @ticket 38692
306375
*/

0 commit comments

Comments
 (0)