Skip to content
Closed
Show file tree
Hide file tree
Changes from 11 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 @@ -1890,7 +1890,7 @@ protected function check_read_post_permission( $post, $request ) {
* @return bool Whether the comment can be read.
*/
protected function check_read_permission( $comment, $request ) {
if ( ! empty( $comment->comment_post_ID ) ) {
if ( 'note' !== $comment->comment_type && ! empty( $comment->comment_post_ID ) ) {
$post = get_post( $comment->comment_post_ID );
if ( $post ) {
if ( $this->check_read_post_permission( $post, $request ) && 1 === (int) $comment->comment_approved ) {
Expand Down
111 changes: 111 additions & 0 deletions tests/phpunit/tests/rest-api/rest-comments-controller.php
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The various assertions could do with assertSame and for the tests with multiple assertions a different message for each assertion.

Original file line number Diff line number Diff line change
Expand Up @@ -4133,4 +4133,115 @@ public function test_get_note_with_children_link() {
$this->assertStringContainsString( 'status=all', $children[0]['href'] );
$this->assertStringContainsString( 'type=note', $children[0]['href'] );
}
/**
* Data provider for comment type tests.
*
* @return array
*/
public function data_comment_type_provider() {
return array(
'comment type' => array( 'comment', 5 ),
'annotation type' => array( 'annotation', 5 ),
'discussion type' => array( 'discussion', 9 ),
'note type' => array( 'note', 3 ),
);
}

/**
* Test retrieving comments by type as authenticated user.
*
* @dataProvider data_comment_type_provider
* @ticket 44157
*
* @param string $comment_type The comment type to test.
* @param int $count The number of comments to create.
*/
public function test_get_items_type_arg_authenticated( $comment_type, $count ) {
wp_set_current_user( self::$admin_id );

$args = array(
'comment_approved' => 1,
'comment_post_ID' => self::$post_id,
'user_id' => self::$author_id,
'comment_type' => $comment_type,
);

// Create comments of the specified type.
for ( $i = 0; $i < $count; $i++ ) {
self::factory()->comment->create( $args );
}

$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'type', $comment_type );
$request->set_param( 'per_page', self::$per_page );

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

$comments = $response->get_data();
$this->assertCount( 'comment' === $comment_type ? $count + self::$total_comments : $count, $comments );

// Next, test getting the individual comments.
foreach ( $comments as $comment ) {
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment['id'] ) );
$response = rest_get_server()->dispatch( $request );

$this->assertEquals( 200, $response->get_status() );
$data = $response->get_data();
$this->assertEquals( $comment_type, $data['type'] );
}
}

/**
* Test retrieving comments by type as unauthenticated user.
*
* @dataProvider data_comment_type_provider
* @ticket 44157
*
* @param string $comment_type The comment type to test.
* @param int $count The number of comments to create.
*/
public function test_get_items_type_arg_unauthenticated( $comment_type, $count ) {
// First, create comments as admin.
wp_set_current_user( self::$admin_id );

$args = array(
'comment_approved' => 1,
'comment_post_ID' => self::$post_id,
'user_id' => self::$author_id,
'comment_type' => $comment_type,
);

$comments = array();

for ( $i = 0; $i < $count; $i++ ) {
$comments[] = self::factory()->comment->create( $args );
}

// Log out and test as unauthenticated user.
wp_logout();

$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
$request->set_param( 'type', $comment_type );
$request->set_param( 'per_page', self::$per_page );

$response = rest_get_server()->dispatch( $request );

// Only comments can be retrieved from the /comments (multiple) endpoint when unauthenticated.
$this->assertEquals( 'comment' === $comment_type ? 200 : 401, $response->get_status() );
if ( 'comment' !== $comment_type ) {
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
}

// Individual comments.
foreach ( $comments as $comment ) {
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment ) );
$response = rest_get_server()->dispatch( $request );

// Individual comments using the /comments/<id> endpoint can (unexpectedly) be
// retrieved by unauthenticated users - except for the 'note' type which is restricted.
// See https://core.trac.wordpress.org/ticket/44157.
$this->assertEquals( 'note' === $comment_type ? 401 : 200, $response->get_status() );
}
}
}
Loading