Skip to content

Commit 52ba94e

Browse files
committed
Update /templates endpoint
1 parent 24452bd commit 52ba94e

File tree

4 files changed

+124
-3
lines changed

4 files changed

+124
-3
lines changed

wp_api/src/request/endpoint/templates_endpoint.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ enum TemplatesRequest {
1616
Delete,
1717
#[delete(url = "/templates/<template_id>", output = crate::templates::TemplateWithEditContext)]
1818
Trash,
19+
#[post(url = "/templates/<template_id>", params = &crate::templates::TemplateUpdateParams, output = crate::templates::TemplateWithEditContext)]
20+
Update,
1921
}
2022

2123
impl DerivedRequest for TemplatesRequest {
@@ -191,6 +193,14 @@ mod tests {
191193
);
192194
}
193195

196+
#[rstest]
197+
fn update_template(endpoint: TemplatesRequestEndpoint) {
198+
validate_wp_v2_endpoint(
199+
endpoint.update(&TemplateId("foo".to_string())),
200+
"/templates/foo",
201+
);
202+
}
203+
194204
const EXPECTED_QUERY_PAIRS_FOR_TEMPLATE_LIST_PARAMS_WITH_ALL_FIELDS: &str =
195205
"wp_id=2&area=header&post_type=page";
196206
fn template_list_params_with_all_fields() -> TemplateListParams {

wp_api/src/templates.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,40 @@ pub struct TemplateDeleteResponse {
208208
pub deleted: bool,
209209
pub previous: TemplateWithEditContext,
210210
}
211+
212+
#[derive(Debug, Default, Serialize, uniffi::Record)]
213+
pub struct TemplateUpdateParams {
214+
// Unique slug identifying the template.
215+
#[uniffi(default = None)]
216+
#[serde(skip_serializing_if = "Option::is_none")]
217+
pub slug: Option<String>,
218+
// Theme identifier for the template.
219+
#[uniffi(default = None)]
220+
#[serde(skip_serializing_if = "Option::is_none")]
221+
pub theme: Option<String>,
222+
// Type of template.
223+
#[uniffi(default = None)]
224+
#[serde(skip_serializing_if = "Option::is_none")]
225+
pub template_type: Option<String>,
226+
// Content of template.
227+
#[uniffi(default = None)]
228+
#[serde(skip_serializing_if = "Option::is_none")]
229+
pub content: Option<String>,
230+
// Title of template.
231+
#[uniffi(default = None)]
232+
#[serde(skip_serializing_if = "Option::is_none")]
233+
pub title: Option<String>,
234+
// Description of template.
235+
#[uniffi(default = None)]
236+
#[serde(skip_serializing_if = "Option::is_none")]
237+
pub description: Option<String>,
238+
// Status of template.
239+
// One of: publish, future, draft, pending, private
240+
#[uniffi(default = None)]
241+
#[serde(skip_serializing_if = "Option::is_none")]
242+
pub status: Option<TemplateStatus>,
243+
// The ID for the author of the template.
244+
#[uniffi(default = None)]
245+
#[serde(skip_serializing_if = "Option::is_none")]
246+
pub author: Option<UserId>,
247+
}

wp_api_integration_tests/tests/test_templates_err.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use serial_test::parallel;
2-
use wp_api::{WpErrorCode, templates::TemplateId};
2+
use wp_api::{
3+
WpErrorCode,
4+
templates::{TemplateId, TemplateUpdateParams},
5+
};
36
use wp_api_integration_tests::{AssertWpError, TEMPLATE_TWENTY_TWENTY_FOUR_SINGLE, api_client};
47

58
#[tokio::test]
@@ -21,3 +24,16 @@ async fn delete_template_err_template_not_found() {
2124
.await
2225
.assert_wp_error(WpErrorCode::TemplateNotFound)
2326
}
27+
28+
#[tokio::test]
29+
#[parallel]
30+
async fn update_template_err_template_not_found() {
31+
api_client()
32+
.templates()
33+
.update(
34+
&TemplateId("foo".to_string()),
35+
&TemplateUpdateParams::default(),
36+
)
37+
.await
38+
.assert_wp_error(WpErrorCode::TemplateNotFound)
39+
}

wp_api_integration_tests/tests/test_templates_mut.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use macro_helper::generate_update_test;
12
use serial_test::serial;
2-
use wp_api::templates::{TemplateId, TemplateStatus};
3-
use wp_api_integration_tests::{TestCredentials, api_client, backend::RestoreServer};
3+
use wp_api::templates::{TemplateId, TemplateStatus, TemplateUpdateParams};
4+
use wp_api_integration_tests::{
5+
AssertResponse, SECOND_USER_ID, TestCredentials, api_client, backend::RestoreServer,
6+
};
47

58
#[tokio::test]
69
#[serial]
@@ -46,3 +49,58 @@ async fn trash_template() {
4649

4750
RestoreServer::db().await;
4851
}
52+
53+
generate_update_test!(update_slug, slug, "new_slug".to_string());
54+
generate_update_test!(update_theme, theme, "new_theme".to_string());
55+
generate_update_test!(
56+
update_template_type,
57+
template_type,
58+
"new_template_type".to_string()
59+
);
60+
generate_update_test!(update_content, content, "new_content".to_string());
61+
generate_update_test!(update_title, title, "new_title".to_string());
62+
generate_update_test!(
63+
update_description,
64+
description,
65+
"new_description".to_string()
66+
);
67+
generate_update_test!(update_status, status, TemplateStatus::Pending);
68+
generate_update_test!(update_author, author, SECOND_USER_ID);
69+
70+
async fn test_update_template(params: &TemplateUpdateParams) {
71+
api_client()
72+
.templates()
73+
.update(
74+
&TemplateId(
75+
TestCredentials::instance()
76+
.integration_test_custom_template_id
77+
.to_string(),
78+
),
79+
params,
80+
)
81+
.await
82+
.assert_response();
83+
RestoreServer::db().await;
84+
}
85+
86+
mod macro_helper {
87+
macro_rules! generate_update_test {
88+
($ident:ident, $field:ident, $new_value:expr) => {
89+
paste::paste! {
90+
#[tokio::test]
91+
#[serial]
92+
async fn $ident() {
93+
let updated_value = $new_value;
94+
test_update_template(
95+
&TemplateUpdateParams {
96+
$field: Some(updated_value),
97+
..Default::default()
98+
})
99+
.await;
100+
}
101+
}
102+
};
103+
}
104+
105+
pub(super) use generate_update_test;
106+
}

0 commit comments

Comments
 (0)