Skip to content

Commit

Permalink
Update /comments endpoint (#450)
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzkocer authored Dec 17, 2024
1 parent 6fccea1 commit bfb228e
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import uniffi.wp_api.CommentDeleteParams
import uniffi.wp_api.CommentListParams
import uniffi.wp_api.CommentRetrieveParams
import uniffi.wp_api.CommentStatus
import uniffi.wp_api.CommentUpdateParams
import uniffi.wp_api.SparseCommentFieldWithEditContext
import uniffi.wp_api.wpAuthenticationFromUsernameAndPassword
import kotlin.test.assertEquals
Expand Down Expand Up @@ -97,4 +98,14 @@ class CommentsEndpointTest {
assertEquals(CommentStatus.Trash, trashedComment.status)
restoreTestServer()
}
}

@Test
fun updateCommentRequest() = runTest {
val updatedComment = client.request { requestBuilder ->
requestBuilder.comments()
.update(commentId = 1, CommentUpdateParams(content = "foo"))
}.assertSuccessAndRetrieveData().data
assertEquals("foo", updatedComment.content.raw)
restoreTestServer()
}
}
53 changes: 53 additions & 0 deletions wp_api/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,59 @@ impl CommentCreateParams {
}
}

#[derive(Debug, Default, Serialize, uniffi::Record)]
pub struct CommentUpdateParams {
/// The ID of the user object, if author was a user.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author: Option<UserId>,
/// Email address for the comment author.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author_email: Option<String>,
/// IP address for the comment author.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author_ip: Option<String>,
/// Display name for the comment author.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author_name: Option<String>,
/// URL for the comment author.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author_url: Option<String>,
/// User agent for the comment author.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author_user_agent: Option<String>,
/// The content for the comment.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
/// The date the comment was published, in the site's timezone.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub date: Option<String>,
/// The date the comment was published, as GMT.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub date_gmt: Option<String>,
/// The ID for the parent of the comment.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub parent: Option<CommentId>,
/// The ID of the associated post object.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub post: Option<PostId>,
/// State of the comment.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<CommentStatus>,
// meta field is omitted for now: https://github.com/Automattic/wordpress-rs/issues/422
}

#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
pub struct SparseComment {
#[WpContext(edit, embed, view)]
Expand Down
4 changes: 3 additions & 1 deletion wp_api/src/request/endpoint/comments_endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::comments::{
CommentId, CommentListParams, SparseCommentFieldWithEditContext,
CommentId, CommentListParams, CommentUpdateParams, SparseCommentFieldWithEditContext,
SparseCommentFieldWithEmbedContext, SparseCommentFieldWithViewContext,
};
use crate::SparseField;
Expand All @@ -18,6 +18,8 @@ enum CommentsRequest {
Delete,
#[delete(url = "/comments/<comment_id>", params = &crate::comments::CommentDeleteParams, output = crate::comments::CommentWithEditContext)]
Trash,
#[post(url = "/comments/<comment_id>", params = &CommentUpdateParams, output = crate::comments::CommentWithEditContext)]
Update,
}

impl DerivedRequest for CommentsRequest {
Expand Down
2 changes: 2 additions & 0 deletions wp_api_integration_tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub const HELLO_DOLLY_PLUGIN_SLUG: &str = "hello-dolly/hello";
pub const CLASSIC_EDITOR_PLUGIN_SLUG: &str = "classic-editor/classic-editor";
pub const WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS: &str = "classic-widgets";
pub const FIRST_COMMENT_ID: CommentId = CommentId(1);
pub const SECOND_COMMENT_ID: CommentId = CommentId(2);
pub const FIRST_POST_ID: PostId = PostId(1);
pub const POST_ID_555: PostId = PostId(555);
pub const MEDIA_ID_611: MediaId = MediaId(611);
pub const MEDIA_TEST_FILE_PATH: &str = "../test_media.jpg";
pub const MEDIA_TEST_FILE_CONTENT_TYPE: &str = "image/jpeg";
Expand Down
170 changes: 166 additions & 4 deletions wp_api_integration_tests/tests/test_comments_mut.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use macro_helper::generate_update_test;
use serial_test::serial;
use wp_api::comments::{
CommentCreateParams, CommentDeleteParams, CommentStatus, CommentWithEditContext,
CommentCreateParams, CommentDeleteParams, CommentStatus, CommentUpdateParams,
CommentWithEditContext,
};
use wp_api_integration_tests::{
api_client,
backend::{Backend, RestoreServer},
AssertResponse, FIRST_COMMENT_ID, FIRST_POST_ID,
AssertResponse, FIRST_COMMENT_ID, FIRST_POST_ID, POST_ID_555, SECOND_COMMENT_ID,
SECOND_USER_ID,
};
use wp_cli::WpCliComment;

Expand All @@ -16,7 +19,7 @@ async fn create_comment_with_just_content() {
&CommentCreateParams::new(FIRST_POST_ID, "foo".to_string()),
|created_comment, comment_from_wp_cli| {
assert_eq!(created_comment.content.raw, "foo");
assert_eq!(comment_from_wp_cli.comment_content, "foo");
assert_eq!(comment_from_wp_cli.content, "foo");
},
)
.await;
Expand All @@ -30,7 +33,7 @@ async fn create_comment_with_content_and_status() {
|created_comment, comment_from_wp_cli| {
assert_eq!(created_comment.content.raw, "foo");
assert_eq!(created_comment.status, CommentStatus::Hold);
assert_eq!(comment_from_wp_cli.comment_content, "foo");
assert_eq!(comment_from_wp_cli.content, "foo");
},
)
.await;
Expand Down Expand Up @@ -87,6 +90,128 @@ async fn trash_comment() {
RestoreServer::db().await;
}

generate_update_test!(
update_author,
author,
SECOND_USER_ID,
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author, SECOND_USER_ID);
assert_eq!(updated_comment_from_wp_cli.author, SECOND_USER_ID.0);
}
);

generate_update_test!(
update_author_email,
author_email,
"[email protected]".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author_email, "[email protected]");
assert_eq!(updated_comment_from_wp_cli.author_email, "[email protected]");
}
);

generate_update_test!(
update_author_ip,
author_ip,
"127.0.0.1".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author_ip, "127.0.0.1");
assert_eq!(updated_comment_from_wp_cli.author_ip, "127.0.0.1");
}
);

generate_update_test!(
update_author_name,
author_name,
"foo".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author_name, "foo");
assert_eq!(updated_comment_from_wp_cli.author_name, "foo");
}
);

generate_update_test!(
update_author_url,
author_url,
"https://example.com".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author_url, "https://example.com");
assert_eq!(
updated_comment_from_wp_cli.author_url,
"https://example.com"
);
}
);

generate_update_test!(
update_author_user_agent,
author_user_agent,
"foo".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.author_user_agent, "foo");
assert_eq!(updated_comment_from_wp_cli.author_user_agent, "foo");
}
);

generate_update_test!(
update_content,
content,
"foo".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.content.raw, "foo");
assert_eq!(updated_comment_from_wp_cli.content, "foo");
}
);

generate_update_test!(
update_date,
date,
"2024-09-09T12:00:00".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.date, "2024-09-09T12:00:00");
assert_eq!(updated_comment_from_wp_cli.date, "2024-09-09 12:00:00");
}
);

generate_update_test!(
update_date_gmt,
date_gmt,
"2024-09-09T12:00:00".to_string(),
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.date_gmt, "2024-09-09T12:00:00");
assert_eq!(updated_comment_from_wp_cli.date_gmt, "2024-09-09 12:00:00");
}
);

generate_update_test!(
update_parent,
parent,
SECOND_COMMENT_ID,
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.parent, SECOND_COMMENT_ID);
assert_eq!(updated_comment_from_wp_cli.parent, SECOND_COMMENT_ID.0);
}
);

generate_update_test!(
update_post,
post,
POST_ID_555,
|updated_comment, updated_comment_from_wp_cli| {
assert_eq!(updated_comment.post, POST_ID_555);
assert_eq!(updated_comment_from_wp_cli.post, POST_ID_555.0);
}
);

generate_update_test!(
update_status,
status,
CommentStatus::Hold,
|updated_comment, _| {
assert_eq!(updated_comment.status, CommentStatus::Hold);
}
);

async fn test_create_comment<F>(params: &CommentCreateParams, assert: F)
where
F: Fn(CommentWithEditContext, WpCliComment),
Expand All @@ -101,3 +226,40 @@ where
assert(created_comment, created_comment_from_wp_cli);
RestoreServer::db().await;
}

async fn test_update_comment<F>(params: &CommentUpdateParams, assert: F)
where
F: Fn(CommentWithEditContext, WpCliComment),
{
let updated_comment = api_client()
.comments()
.update(&FIRST_COMMENT_ID, params)
.await
.assert_response()
.data;
let updated_comment_from_wp_cli = Backend::comment(&FIRST_COMMENT_ID).await;
assert(updated_comment, updated_comment_from_wp_cli);
RestoreServer::db().await;
}

mod macro_helper {
macro_rules! generate_update_test {
($ident:ident, $field:ident, $new_value:expr, $assertion:expr) => {
paste::paste! {
#[tokio::test]
#[serial]
async fn $ident() {
let updated_value = $new_value;
test_update_comment(
&CommentUpdateParams {
$field: Some(updated_value),
..Default::default()
}, $assertion)
.await;
}
}
};
}

pub(super) use generate_update_test;
}
34 changes: 23 additions & 11 deletions wp_cli/src/wp_cli_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,33 @@ pub struct WpCliComment {
#[serde(deserialize_with = "deserialize_i64_or_string")]
pub comment_id: i64,
#[serde(rename = "comment_post_ID")]
pub comment_post_id: String,
pub comment_author: String,
pub comment_author_email: String,
pub comment_author_url: String,
#[serde(deserialize_with = "deserialize_i64_or_string")]
pub post: i64,
#[serde(rename = "user_id")]
#[serde(deserialize_with = "deserialize_i64_or_string")]
pub author: i64,
#[serde(rename = "comment_author")]
pub author_name: String,
#[serde(rename = "comment_author_email")]
pub author_email: String,
#[serde(rename = "comment_author_url")]
pub author_url: String,
#[serde(rename = "comment_author_IP")]
pub comment_author_ip: String,
pub comment_date: String,
pub comment_date_gmt: String,
pub comment_content: String,
pub author_ip: String,
#[serde(rename = "comment_agent")]
pub author_user_agent: String,
#[serde(rename = "comment_content")]
pub content: String,
#[serde(rename = "comment_date")]
pub date: String,
#[serde(rename = "comment_date_gmt")]
pub date_gmt: String,
#[serde(rename = "comment_parent")]
#[serde(deserialize_with = "deserialize_i64_or_string")]
pub parent: i64,
pub comment_karma: String,
pub comment_approved: String,
pub comment_agent: String,
pub comment_type: String,
pub comment_parent: String,
pub user_id: String,
}

impl WpCliComment {
Expand Down

0 comments on commit bfb228e

Please sign in to comment.