Skip to content

Commit 3cdbc6a

Browse files
committed
Tests: Cover note editing permissions for comment moderators.
Verify that `moderate_comments` alone does not allow updating or deleting a note when the user cannot edit its post. Confirm rejected REST requests leave the note unchanged. Developed in: #15187 Props ravipatel, sainathpoojary. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65083 602fd350-edb4-49c9-b593-d223f7449a82
1 parent f17a0cf commit 3cdbc6a

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5567,6 +5567,67 @@ public function test_update_note_is_still_allowed() {
55675567
$this->assertSame( 200, $response->get_status() );
55685568
}
55695569

5570+
/**
5571+
* Creates a note on a post the given moderator cannot edit.
5572+
*
5573+
* @return array{0: int, 1: int} The note ID and the ID of a user who can moderate
5574+
* comments but cannot edit the note's post.
5575+
*/
5576+
private function create_note_and_moderator_without_edit_access() {
5577+
$post_id = self::factory()->post->create( array( 'post_author' => self::$editor_id ) );
5578+
$note_id = self::factory()->comment->create(
5579+
array(
5580+
'comment_post_ID' => $post_id,
5581+
'comment_type' => 'note',
5582+
'comment_approved' => 0,
5583+
'user_id' => self::$editor_id,
5584+
'comment_content' => 'Test note',
5585+
)
5586+
);
5587+
5588+
$moderator = self::factory()->user->create_and_get( array( 'role' => 'subscriber' ) );
5589+
$moderator->add_cap( 'moderate_comments' );
5590+
5591+
$this->assertFalse( user_can( $moderator, 'edit_post', $post_id ), 'The moderator should not be able to edit the post.' );
5592+
5593+
return array( $note_id, $moderator->ID );
5594+
}
5595+
5596+
/**
5597+
* A user who can moderate comments but cannot edit the note's post cannot update the note.
5598+
*
5599+
* @ticket 65819
5600+
*/
5601+
public function test_moderator_without_edit_access_cannot_update_note() {
5602+
list( $note_id, $moderator_id ) = $this->create_note_and_moderator_without_edit_access();
5603+
5604+
wp_set_current_user( $moderator_id );
5605+
$request = new WP_REST_Request( 'PUT', '/wp/v2/comments/' . $note_id );
5606+
$request->add_header( 'Content-Type', 'application/json' );
5607+
$request->set_body( wp_json_encode( array( 'content' => 'Edited note' ) ) );
5608+
$response = rest_get_server()->dispatch( $request );
5609+
5610+
$this->assertErrorResponse( 'rest_cannot_edit', $response, 403 );
5611+
$this->assertSame( 'Test note', get_comment( $note_id )->comment_content );
5612+
}
5613+
5614+
/**
5615+
* A user who can moderate comments but cannot edit the note's post cannot delete the note.
5616+
*
5617+
* @ticket 65819
5618+
*/
5619+
public function test_moderator_without_edit_access_cannot_delete_note() {
5620+
list( $note_id, $moderator_id ) = $this->create_note_and_moderator_without_edit_access();
5621+
5622+
wp_set_current_user( $moderator_id );
5623+
$request = new WP_REST_Request( 'DELETE', '/wp/v2/comments/' . $note_id );
5624+
$request->set_param( 'force', true );
5625+
$response = rest_get_server()->dispatch( $request );
5626+
5627+
$this->assertErrorResponse( 'rest_cannot_delete', $response, 403 );
5628+
$this->assertInstanceOf( WP_Comment::class, get_comment( $note_id ) );
5629+
}
5630+
55705631
/**
55715632
* The note response exposes a `reaction_summary` field aggregating
55725633
* counts per emoji hex key, plus the current user's reaction ID as

0 commit comments

Comments
 (0)