Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1680,8 +1680,8 @@ public function get_collection_params() {
$query_params['status'] = array(
'default' => 'approve',
'description' => __( 'Limit result set to comments assigned a specific status. Requires authorization.' ),
'sanitize_callback' => 'sanitize_key',
'type' => 'string',
'sanitize_callback' => array( $this, 'sanitize_comment_statuses' ),
'type' => 'array',
'validate_callback' => 'rest_validate_request_arg',
);

Expand Down Expand Up @@ -1928,4 +1928,17 @@ protected function check_is_comment_content_allowed( $prepared_comment ) {
*/
return '' !== $check['comment_content'];
}

/**
* Sanitize a single comment status or a list of comment statuses with `sanitize_key`.
*
* @since 6.9.0
*
* @param string|array $statuses Comment status or array of comment statuses.
* @return array Sanitized array of comment statuses.
*/
public function sanitize_comment_statuses( $statuses ) {
$statuses = wp_parse_list( $statuses );
return array_unique( array_map( 'sanitize_key', $statuses ) );
}
}
146 changes: 146 additions & 0 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
array(
'comment_content' => "Comment {$i}",
'comment_post_ID' => self::$post_id,
'status' => ( 0 === $i % 2 ) ? 'approve' : 'hold',
)
);
}
Expand Down Expand Up @@ -224,6 +225,151 @@
$this->assertCount( self::$total_comments, $comments );
}

/**
* Test getting items of a specific status.
*
* @ticket 63982
*/
public function test_get_items_by_status() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'status', 'approve' );
$request->set_param( 'per_page', self::$per_page );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status() );

$q = new WP_Comment_Query();
$found = $q->query(
array(
'status' => 'approve',
'count' => true,
)
);

$comments = $response->get_data();

$this->assertCount( $found, $comments );
}

/**
* Test getting comments of all statuses.
*
* @ticket 63982
*/
public function test_get_items_by_all_status() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'status', 'all' );
$request->set_param( 'per_page', self::$per_page );
$response = rest_get_server()->dispatch( $request );

$this->assertSame( 200, $response->get_status() );

$q = new WP_Comment_Query();
$found = $q->query(
array(
'status' => 'all',
'count' => true,
)
);

$comments = $response->get_data();
$this->assertCount( $found, $comments );
}

/**
* Test getting items of multiple statuses.
*
* @ticket 63982
*/
public function test_get_items_by_multiple_status() {
wp_set_current_user( self::$admin_id );

$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'status', array( 'approve', 'hold' ) );
$request->set_param( 'per_page', self::$per_page );

$response = rest_get_server()->dispatch( $request );
$this->assertSame( 200, $response->get_status() );

$q = new WP_Comment_Query();
$found = $q->query(
array(
'status' => array( 'approve', 'hold' ),
'count' => true,
)
);

$comments = $response->get_data();
$this->assertCount( $found, $comments );
}

/**
* Test sanization of the status parameter.
*
* @ticket 63982
*
* @dataProvider data_get_items_by_status_sanitize
*/
public function test_get_items_by_status_sanitize( $key, $expected ) {
wp_set_current_user( self::$admin_id );

// Create a post with the test status.
$params = array(
'post' => self::$post_id,
'author_name' => 'Comic Book Guy',
'author_email' => 'cbg@androidsdungeon.com',
'author_url' => 'http://androidsdungeon.com',
'content' => 'Worst Comment Ever!',
'status' => $key,
);

$request = new WP_REST_Request( 'POST', '/wp/v2/comments' );
$request->add_header( 'Content-Type', 'application/json' );
$request->set_body( wp_json_encode( $params ) );

$response = rest_get_server()->dispatch( $request );
$this->assertSame( 201, $response->get_status() );

$comment = $response->get_data();

$this->assertEquals( $expected, $comment['status'] );
}

/**
* Data provider.
*
* @return array
*/
public function data_get_items_by_status_sanitize() {
return array(
'an empty string key' => array(

Check warning on line 349 in tests/phpunit/tests/rest-api/rest-comments-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 10 space(s) between "'an empty string key'" and double arrow, but found 12.
'key' => '',
'expected' => 'hold',
),
'a lowercase key with commas' => array(

Check warning on line 353 in tests/phpunit/tests/rest-api/rest-comments-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 2 space(s) between "'a lowercase key with commas'" and double arrow, but found 4.
'key' => 'howdy,admin',
'expected' => 'hold',
),
'a lowercase key with commas' => array(

Check warning on line 357 in tests/phpunit/tests/rest-api/rest-comments-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 2 space(s) between "'a lowercase key with commas'" and double arrow, but found 4.
'key' => 'HOWDY,ADMIN',
'expected' => 'hold',
),
'a mixed case key with commas' => array(

Check warning on line 361 in tests/phpunit/tests/rest-api/rest-comments-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 1 space(s) between "'a mixed case key with commas'" and double arrow, but found 3.
'key' => 'HoWdY,aDmIn',
'expected' => 'hold',
),
'a string with unicode' => array(

Check warning on line 365 in tests/phpunit/tests/rest-api/rest-comments-controller.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Array double arrow not aligned correctly; expected 8 space(s) between "'a string with unicode'" and double arrow, but found 3.
'key' => array( 'howdy admin', 'another-value' ),
'expected' => 'hold',
),
);
}


/**
* @ticket 38692
*/
Expand Down
5 changes: 4 additions & 1 deletion tests/qunit/fixtures/wp-api-generated.js
Original file line number Diff line number Diff line change
Expand Up @@ -10335,7 +10335,10 @@ mockedApiResponse.Schema = {
"status": {
"default": "approve",
"description": "Limit result set to comments assigned a specific status. Requires authorization.",
"type": "string",
"type": [
"string",
"array"
],
"required": false
},
"type": {
Expand Down
Loading