Skip to content

Commit d509da8

Browse files
authored
Delete /posts/<post_id>/revisions/<post_revision_id> endpoint (#863)
1 parent 1658f1e commit d509da8

File tree

8 files changed

+94
-12
lines changed

8 files changed

+94
-12
lines changed

native/swift/Example/Example/ListViewData.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ extension SiteSettingsWithEditContext {
133133

134134
extension PostWithEditContext: ListViewDataConvertable {
135135
var asListViewData: ListViewData {
136-
ListViewData(id: self.slug, title: self.title.raw, subtitle: self.slug, fields: [:])
136+
ListViewData(id: self.slug, title: self.title.rendered, subtitle: self.slug, fields: [:])
137137
}
138138
}
139139

@@ -143,7 +143,7 @@ extension MediaWithEditContext: ListViewDataConvertable {
143143
return ListViewData(
144144
id: self.slug,
145145
title: details.emoji + " " + (URL(string: self.sourceUrl)?.lastPathComponent ?? "<invalid-source-url>"),
146-
subtitle: self.title.raw,
146+
subtitle: self.title.rendered,
147147
fields: details.fields
148148
)
149149
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/*
3+
Plugin Name: Allow Revision Deletion for Integration Tests
4+
Description: Allows administrators to delete revisions via REST API for integration testing
5+
Version: 1.0
6+
*/
7+
8+
add_filter('map_meta_cap', function($caps, $cap, $user_id, $args) {
9+
if ($cap === 'delete_post' && !empty($args[0])) {
10+
$post = get_post($args[0]);
11+
if ($post && $post->post_type === 'revision') {
12+
if (current_user_can('delete_posts')) {
13+
return ['delete_posts'];
14+
}
15+
}
16+
}
17+
return $caps;
18+
}, 10, 4);

scripts/setup-test-site.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ wp import /tmp/testdata.xml --authors=create
7171
wp plugin deactivate wordpress-importer
7272
wp plugin delete wordpress-importer
7373

74+
# Install custom must-use plugins for integration tests
75+
mkdir -p wp-content/mu-plugins
76+
cp /app/scripts/setup-test-site-custom-plugins/*.php wp-content/mu-plugins/
77+
7478
# We need an `author` user for some of the integration tests
7579
wp user create test_author [email protected] --role=author
7680

wp_api/src/post_revisions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ pub struct SparsePostRevision {
188188
pub meta: Option<crate::posts::PostMeta>,
189189
}
190190

191+
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
192+
pub struct PostRevisionDeleteResponse {
193+
pub deleted: bool,
194+
pub previous: PostRevisionWithEditContext,
195+
}
196+
191197
#[cfg(test)]
192198
mod tests {
193199
use super::*;

wp_api/src/posts.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ pub struct SparsePost {
531531
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
532532
pub struct SparsePostGuid {
533533
#[WpContext(edit)]
534+
#[WpContextualOption]
534535
pub raw: Option<String>,
535536
#[WpContext(edit, view)]
536537
pub rendered: Option<String>,
@@ -539,6 +540,7 @@ pub struct SparsePostGuid {
539540
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
540541
pub struct SparsePostTitle {
541542
#[WpContext(edit)]
543+
#[WpContextualOption]
542544
pub raw: Option<String>,
543545
#[WpContext(edit, embed, view)]
544546
pub rendered: Option<String>,
@@ -547,6 +549,7 @@ pub struct SparsePostTitle {
547549
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
548550
pub struct SparsePostContent {
549551
#[WpContext(edit)]
552+
#[WpContextualOption]
550553
pub raw: Option<String>,
551554
#[WpContext(edit, view)]
552555
pub rendered: Option<String>,
@@ -561,6 +564,7 @@ pub struct SparsePostContent {
561564
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WpContextual)]
562565
pub struct SparsePostExcerpt {
563566
#[WpContext(edit)]
567+
#[WpContextualOption]
564568
pub raw: Option<String>,
565569
#[WpContext(edit, embed, view)]
566570
pub rendered: Option<String>,

wp_api/src/request/endpoint/post_revisions_endpoint.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ enum PostRevisionsRequest {
1515
List,
1616
#[contextual_get(url = "/posts/<post_id>/revisions/<post_revision_id>", output = crate::post_revisions::SparsePostRevision, filter_by = crate::post_revisions::SparsePostRevisionField)]
1717
Retrieve,
18+
#[delete(url = "/posts/<post_id>/revisions/<post_revision_id>", output = crate::post_revisions::PostRevisionDeleteResponse)]
19+
Delete,
1820
}
1921

2022
impl DerivedRequest for PostRevisionsRequest {
2123
fn namespace() -> impl AsNamespace {
2224
WpNamespace::WpV2
2325
}
26+
27+
fn additional_query_pairs(&self) -> Vec<(&str, String)> {
28+
match self {
29+
PostRevisionsRequest::Delete => vec![("force", "true".to_string())],
30+
_ => vec![],
31+
}
32+
}
2433
}
2534

2635
super::macros::default_sparse_field_implementation_from_field_name!(
@@ -162,6 +171,16 @@ mod tests {
162171
);
163172
}
164173

174+
#[rstest]
175+
fn delete_post_revision(endpoint: PostRevisionsRequestEndpoint) {
176+
let post_id = PostId(777);
177+
let revision_id = PostRevisionId(888);
178+
validate_wp_v2_endpoint(
179+
endpoint.delete(&post_id, &revision_id),
180+
&format!("/posts/{post_id}/revisions/{revision_id}?force=true"),
181+
);
182+
}
183+
165184
const EXPECTED_QUERY_PAIRS_FOR_ALL_SPARSE_POST_REVISION_FIELDS_WITH_EDIT_CONTEXT: &str = "_fields=id%2Cauthor%2Cdate%2Cdate_gmt%2Cmodified%2Cmodified_gmt%2Cparent%2Cslug%2Cguid%2Ctitle%2Ccontent%2Cexcerpt%2Cmeta";
166185
const ALL_SPARSE_POST_REVISION_FIELDS_WITH_EDIT_CONTEXT: &[SparsePostRevisionFieldWithEditContext;
167186
13] = &[
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use wp_api::{post_revisions::PostRevisionId, posts::PostId};
2+
use wp_api_integration_tests::prelude::*;
3+
4+
#[tokio::test]
5+
#[serial]
6+
async fn delete_post_revision() {
7+
let revision_id = revision_id_for_revisioned_post_id();
8+
let revision_delete_response = api_client()
9+
.post_revisions()
10+
.delete(&revisioned_post_id(), &revision_id)
11+
.await;
12+
13+
assert!(
14+
revision_delete_response.is_ok(),
15+
"{revision_delete_response:#?}"
16+
);
17+
18+
let delete_response = revision_delete_response.unwrap().data;
19+
assert!(delete_response.deleted);
20+
assert_eq!(delete_response.previous.id, revision_id);
21+
22+
RestoreServer::db().await;
23+
}
24+
25+
fn revisioned_post_id() -> PostId {
26+
PostId(TestCredentials::instance().revisioned_post_id)
27+
}
28+
29+
fn revision_id_for_revisioned_post_id() -> PostRevisionId {
30+
PostRevisionId(TestCredentials::instance().revision_id_for_revisioned_post_id)
31+
}

wp_api_integration_tests/tests/test_posts_mut.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async fn create_post_with_just_title() {
1717
..Default::default()
1818
},
1919
|created_post, post_from_wp_cli| {
20-
assert_eq!(created_post.title.raw, "foo");
20+
assert_eq!(created_post.title.raw, Some("foo".to_string()));
2121
assert_eq!(post_from_wp_cli.title, "foo");
2222
},
2323
)
@@ -40,7 +40,7 @@ async fn create_post_with_title_and_meta() {
4040
},
4141
|created_post, post_from_wp_cli| {
4242
let footnote = created_post.meta.footnotes.first().unwrap();
43-
assert_eq!(created_post.title.raw, "foo");
43+
assert_eq!(created_post.title.raw, Some("foo".to_string()));
4444
assert_eq!(post_from_wp_cli.title, "foo");
4545
assert_eq!(footnote.id, "bar");
4646
assert_eq!(footnote.content, "baz");
@@ -58,7 +58,7 @@ async fn create_post_with_just_content() {
5858
..Default::default()
5959
},
6060
|created_post, post_from_wp_cli| {
61-
assert_eq!(created_post.content.raw, "foo");
61+
assert_eq!(created_post.content.raw, Some("foo".to_string()));
6262
assert_eq!(post_from_wp_cli.content, "foo");
6363
},
6464
)
@@ -74,7 +74,7 @@ async fn create_post_with_just_excerpt() {
7474
..Default::default()
7575
},
7676
|created_post, post_from_wp_cli| {
77-
assert_eq!(created_post.excerpt.raw, "foo");
77+
assert_eq!(created_post.excerpt.raw, Some("foo".to_string()));
7878
assert_eq!(post_from_wp_cli.excerpt, "foo");
7979
},
8080
)
@@ -92,11 +92,11 @@ async fn create_post_with_title_content_and_excerpt() {
9292
..Default::default()
9393
},
9494
|created_post, post_from_wp_cli| {
95-
assert_eq!(created_post.title.raw, "foo");
95+
assert_eq!(created_post.title.raw, Some("foo".to_string()));
9696
assert_eq!(post_from_wp_cli.title, "foo");
97-
assert_eq!(created_post.content.raw, "bar");
97+
assert_eq!(created_post.content.raw, Some("bar".to_string()));
9898
assert_eq!(post_from_wp_cli.content, "bar");
99-
assert_eq!(created_post.excerpt.raw, "baz");
99+
assert_eq!(created_post.excerpt.raw, Some("baz".to_string()));
100100
assert_eq!(post_from_wp_cli.excerpt, "baz");
101101
},
102102
)
@@ -193,7 +193,7 @@ generate_update_test!(
193193
title,
194194
"new_title".to_string(),
195195
|updated_post, updated_post_from_wp_cli| {
196-
assert_eq!(updated_post.title.raw, "new_title");
196+
assert_eq!(updated_post.title.raw, Some("new_title".to_string()));
197197
assert_eq!(updated_post_from_wp_cli.title, "new_title");
198198
}
199199
);
@@ -203,7 +203,7 @@ generate_update_test!(
203203
content,
204204
"new_content".to_string(),
205205
|updated_post, updated_post_from_wp_cli| {
206-
assert_eq!(updated_post.content.raw, "new_content");
206+
assert_eq!(updated_post.content.raw, Some("new_content".to_string()));
207207
assert_eq!(updated_post_from_wp_cli.content, "new_content");
208208
}
209209
);
@@ -223,7 +223,7 @@ generate_update_test!(
223223
excerpt,
224224
"new_excerpt".to_string(),
225225
|updated_post, updated_post_from_wp_cli| {
226-
assert_eq!(updated_post.excerpt.raw, "new_excerpt");
226+
assert_eq!(updated_post.excerpt.raw, Some("new_excerpt".to_string()));
227227
assert_eq!(updated_post_from_wp_cli.excerpt, "new_excerpt");
228228
}
229229
);

0 commit comments

Comments
 (0)