Skip to content

Commit 831fbaa

Browse files
Test comment permissions for 44157
1 parent 61ae275 commit 831fbaa

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,4 +4133,86 @@ public function test_get_note_with_children_link() {
41334133
$this->assertStringContainsString( 'status=all', $children[0]['href'] );
41344134
$this->assertStringContainsString( 'type=note', $children[0]['href'] );
41354135
}
4136+
/**
4137+
* Test comment permissions.
4138+
*
4139+
* @ticket 44157
4140+
*
4141+
* @return void
4142+
*/
4143+
public function test_get_items_type_arg() {
4144+
// Authorized admin user.
4145+
wp_set_current_user( self::$admin_id );
4146+
$comment_type_1 = 'annotation';
4147+
$comment_type_2 = 'discussion';
4148+
$comment_type_3 = 'note';
4149+
$args = array(
4150+
'comment_approved' => 1,
4151+
'comment_post_ID' => self::$post_id,
4152+
'user_id' => self::$author_id,
4153+
'post_id' => self::$post_id,
4154+
);
4155+
4156+
$count_1 = 5;
4157+
$args['comment_type'] = $comment_type_1;
4158+
for ( $i = 0; $i < $count_1; $i++ ) {
4159+
self::factory()->comment->create( $args );
4160+
}
4161+
4162+
$count_2 = 9;
4163+
$args['comment_type'] = $comment_type_2;
4164+
for ( $i = 0; $i < $count_2; $i++ ) {
4165+
self::factory()->comment->create( $args );
4166+
}
4167+
4168+
$count_3 = 3;
4169+
$args['comment_type'] = $comment_type_3;
4170+
for ( $i = 0; $i < $count_3; $i++ ) {
4171+
self::factory()->comment->create( $args );
4172+
}
4173+
4174+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
4175+
$request->set_param( 'type', $comment_type_1 );
4176+
4177+
// Admin user and no type gets the two comments of comment type 'all' (the default).
4178+
$response = rest_get_server()->dispatch( $request );
4179+
$this->assertEquals( 200, $response->get_status() );
4180+
$comments = $response->get_data();
4181+
$this->assertCount( $count_1, $comments );
4182+
4183+
$request->set_param( 'type', $comment_type_2 );
4184+
$response = rest_get_server()->dispatch( $request );
4185+
$this->assertEquals( 200, $response->get_status() );
4186+
$comments = $response->get_data();
4187+
$this->assertCount( $count_2, $comments );
4188+
$comment_type_ids = wp_list_pluck( $comments, 'id' ); // So we can iterate through them later :) .
4189+
4190+
$request->set_param( 'type', $comment_type_3 );
4191+
$response = rest_get_server()->dispatch( $request );
4192+
$this->assertEquals( 200, $response->get_status() );
4193+
$comments = $response->get_data();
4194+
$this->assertCount( $count_3, $comments );
4195+
4196+
// Unset the current user.
4197+
wp_set_current_user( null );
4198+
4199+
$request->set_param( 'type', 'comments' );
4200+
$request->set_param( 'per_page', self::$per_page );
4201+
$response = rest_get_server()->dispatch( $request );
4202+
$this->assertEquals( 401, $response->get_status() );
4203+
$comments = $response->get_data();
4204+
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
4205+
4206+
$request->set_param( 'type', $comment_type_2 );
4207+
$response = rest_get_server()->dispatch( $request );
4208+
$comments = $response->get_data();
4209+
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
4210+
4211+
// But the unauthenticated user can see them at their individual endpoints.
4212+
foreach ( $comment_type_ids as $comment_type_id ) {
4213+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment_type_id ) );
4214+
$response = rest_get_server()->dispatch( $request );
4215+
$this->assertEquals( 401, $response->get_status() );
4216+
}
4217+
}
41364218
}

0 commit comments

Comments
 (0)