Skip to content

Commit 4b04b7b

Browse files
test single comment endpoint permissions
1 parent 2f0af73 commit 4b04b7b

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4137,16 +4137,13 @@ public function test_get_note_with_children_link() {
41374137
* Test comment permissions.
41384138
*
41394139
* @ticket 44157
4140-
*
4141-
* @return void
41424140
*/
41434141
public function test_get_items_type_arg() {
4144-
// Authorized admin user.
41454142
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(
4143+
$comment_type_1 = 'annotation';
4144+
$comment_type_2 = 'discussion';
4145+
$note_comment_type = 'note';
4146+
$args = array(
41504147
'comment_approved' => 1,
41514148
'comment_post_ID' => self::$post_id,
41524149
'user_id' => self::$author_id,
@@ -4166,15 +4163,14 @@ public function test_get_items_type_arg() {
41664163
}
41674164

41684165
$count_3 = 3;
4169-
$args['comment_type'] = $comment_type_3;
4166+
$args['comment_type'] = $note_comment_type;
41704167
for ( $i = 0; $i < $count_3; $i++ ) {
41714168
self::factory()->comment->create( $args );
41724169
}
41734170

41744171
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
41754172
$request->set_param( 'type', $comment_type_1 );
41764173

4177-
// Admin user and no type gets the two comments of comment type 'all' (the default).
41784174
$response = rest_get_server()->dispatch( $request );
41794175
$this->assertEquals( 200, $response->get_status() );
41804176
$comments = $response->get_data();
@@ -4185,16 +4181,17 @@ public function test_get_items_type_arg() {
41854181
$this->assertEquals( 200, $response->get_status() );
41864182
$comments = $response->get_data();
41874183
$this->assertCount( $count_2, $comments );
4188-
$comment_type_ids = wp_list_pluck( $comments, 'id' ); // So we can iterate through them later :) .
4184+
$comment_type_ids = wp_list_pluck( $comments, 'id' );
41894185

4190-
$request->set_param( 'type', $comment_type_3 );
4186+
$request->set_param( 'type', $note_comment_type );
41914187
$response = rest_get_server()->dispatch( $request );
41924188
$this->assertEquals( 200, $response->get_status() );
41934189
$comments = $response->get_data();
41944190
$this->assertCount( $count_3, $comments );
4191+
$note_type_ids = wp_list_pluck( $comments, 'id' );
41954192

4196-
// Unset the current user.
4197-
wp_set_current_user( null );
4193+
// Log out the current user.
4194+
wp_logout();
41984195

41994196
$request->set_param( 'type', 'comments' );
42004197
$request->set_param( 'per_page', self::$per_page );
@@ -4203,12 +4200,19 @@ public function test_get_items_type_arg() {
42034200
$comments = $response->get_data();
42044201
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
42054202

4206-
$request->set_param( 'type', $comment_type_2 );
4203+
$request->set_param( 'comment_type', $comment_type_2 );
42074204
$response = rest_get_server()->dispatch( $request );
42084205
$comments = $response->get_data();
42094206
$this->assertErrorResponse( 'rest_forbidden_param', $response, 401 );
42104207

4211-
// But the unauthenticated user can see them at their individual endpoints.
4208+
$request->set_param( 'comment_type', $note_comment_type );
4209+
foreach( $note_type_ids as $note_type_id ) {
4210+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $note_type_id ) );
4211+
$response = rest_get_server()->dispatch( $request );
4212+
$this->assertEquals( 401, $response->get_status() );
4213+
}
4214+
4215+
// Custom comment types should also not be visible to unauthenticated users.
42124216
foreach ( $comment_type_ids as $comment_type_id ) {
42134217
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%d', $comment_type_id ) );
42144218
$response = rest_get_server()->dispatch( $request );

0 commit comments

Comments
 (0)