Skip to content

Update /templates endpoint #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: delete-templates
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions wp_api/src/request/endpoint/templates_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ enum TemplatesRequest {
Delete,
#[delete(url = "/templates/<template_id>", output = crate::templates::TemplateWithEditContext)]
Trash,
#[post(url = "/templates/<template_id>", params = &crate::templates::TemplateUpdateParams, output = crate::templates::TemplateWithEditContext)]
Update,
}

impl DerivedRequest for TemplatesRequest {
Expand Down Expand Up @@ -189,6 +191,14 @@ mod tests {
);
}

#[rstest]
fn update_template(endpoint: TemplatesRequestEndpoint) {
validate_wp_v2_endpoint(
endpoint.update(&TemplateId("foo".to_string())),
"/templates/foo",
);
}

const EXPECTED_QUERY_PAIRS_FOR_TEMPLATE_LIST_PARAMS_WITH_ALL_FIELDS: &str =
"wp_id=2&area=header&post_type=page";
fn template_list_params_with_all_fields() -> TemplateListParams {
Expand Down
37 changes: 37 additions & 0 deletions wp_api/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,40 @@ pub struct TemplateDeleteResponse {
pub deleted: bool,
pub previous: TemplateWithEditContext,
}

#[derive(Debug, Default, Serialize, uniffi::Record)]
pub struct TemplateUpdateParams {
// Unique slug identifying the template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub slug: Option<String>,
// Theme identifier for the template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub theme: Option<String>,
// Type of template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub template_type: Option<String>,
// Content of template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
// Title of template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
// Description of template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
// Status of template.
// One of: publish, future, draft, pending, private
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<TemplateStatus>,
// The ID for the author of the template.
#[uniffi(default = None)]
#[serde(skip_serializing_if = "Option::is_none")]
pub author: Option<UserId>,
}
18 changes: 17 additions & 1 deletion wp_api_integration_tests/tests/test_templates_err.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use serial_test::parallel;
use wp_api::{WpErrorCode, templates::TemplateId};
use wp_api::{
WpErrorCode,
templates::{TemplateId, TemplateUpdateParams},
};
use wp_api_integration_tests::{AssertWpError, TEMPLATE_TWENTY_TWENTY_FOUR_SINGLE, api_client};

#[tokio::test]
Expand All @@ -21,3 +24,16 @@ async fn delete_template_err_template_not_found() {
.await
.assert_wp_error(WpErrorCode::TemplateNotFound)
}

#[tokio::test]
#[parallel]
async fn update_template_err_template_not_found() {
api_client()
.templates()
.update(
&TemplateId("foo".to_string()),
&TemplateUpdateParams::default(),
)
.await
.assert_wp_error(WpErrorCode::TemplateNotFound)
}
40 changes: 38 additions & 2 deletions wp_api_integration_tests/tests/test_templates_mut.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use macro_helper::generate_update_test;
use serial_test::serial;
use wp_api::templates::TemplateId;
use wp_api_integration_tests::{TEMPLATE_CUSTOM, api_client, backend::RestoreServer};
use wp_api::templates::{TemplateId, TemplateUpdateParams};
use wp_api_integration_tests::{
AssertResponse, TEMPLATE_CUSTOM, api_client, backend::RestoreServer,
};

#[tokio::test]
#[serial]
Expand All @@ -18,3 +21,36 @@ async fn delete_template() {

RestoreServer::db().await;
}

generate_update_test!(update_slug, slug, "new_slug".to_string());

async fn test_update_template(params: &TemplateUpdateParams) {
api_client()
.templates()
.update(&TemplateId(TEMPLATE_CUSTOM.to_string()), params)
.await
.assert_response();
RestoreServer::db().await;
}

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

pub(super) use generate_update_test;
}