Skip to content

Commit 68c5cdf

Browse files
authored
Retrieve /posts/<post_id>/revisions/<post_revision_id> endpoint (#859)
1 parent f8ccb9b commit 68c5cdf

File tree

5 files changed

+139
-4
lines changed

5 files changed

+139
-4
lines changed

scripts/setup-test-site.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ create_test_credentials () {
9999
local INTEGRATION_TEST_CUSTOM_TEMPLATE_ID
100100
SITE_URL="http://localhost"
101101
ADMIN_USERNAME="test@example.com"
102+
ADMIN_USER_ID="$(wp user get "$ADMIN_USERNAME" --field=ID)"
102103
ADMIN_PASSWORD="$(wp user application-password create test@example.com test --porcelain)"
103104
ADMIN_PASSWORD_UUID="$(wp user application-password list test@example.com --fields=uuid --format=csv | sed -n '2 p')"
104105
SUBSCRIBER_USERNAME="themedemos"
@@ -130,8 +131,10 @@ create_test_credentials () {
130131
REVISIONED_POST_ID="$(wp post create --post_type=post --post_title=Revisioned_POST_FOR_INTEGRATION_TESTS --porcelain)"
131132
for i in {1..10};
132133
do
133-
curl --user "$ADMIN_USERNAME":"$ADMIN_PASSWORD" -H "Content-Type: application/json" -d "{\"content\":\"content_revision_$i\"}" "http://localhost/wp-json/wp/v2/posts/$REVISIONED_POST_ID"
134+
curl --silent --user "$ADMIN_USERNAME":"$ADMIN_PASSWORD" -H "Content-Type: application/json" -d "{\"content\":\"content_revision_$i\", \"author\": $ADMIN_USER_ID}" "http://localhost/wp-json/wp/v2/posts/$REVISIONED_POST_ID"
134135
done
136+
# Generating revisions don't return an id, but since we just created the `REVISIONED_POST_ID`, we can use it to calculate the revision id
137+
REVISION_ID_FOR_REVISIONED_POST_ID=$((REVISIONED_POST_ID + 1))
135138

136139
rm -rf /app/test_credentials.json
137140
jo -p \
@@ -154,6 +157,7 @@ create_test_credentials () {
154157
wordpress_core_version="\"$WORDPRESS_VERSION\"" \
155158
integration_test_custom_template_id="$INTEGRATION_TEST_CUSTOM_TEMPLATE_ID" \
156159
revisioned_post_id="$REVISIONED_POST_ID" \
160+
revision_id_for_revisioned_post_id="$REVISION_ID_FOR_REVISIONED_POST_ID" \
157161
> /app/test_credentials.json
158162
}
159163
create_test_credentials

wordpress.Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ RUN wget -O - https://apt.corretto.aws/corretto.key | gpg --dearmor -o /usr/shar
1212
echo "deb [signed-by=/usr/share/keyrings/corretto-keyring.gpg] https://apt.corretto.aws stable main" | tee /etc/apt/sources.list.d/corretto.list
1313

1414
RUN apt-get update \
15-
&& apt-get install -y java-21-amazon-corretto-jdk android-sdk wget default-mysql-client less libssl-dev jo \
15+
&& apt-get install -y java-21-amazon-corretto-jdk android-sdk wget default-mysql-client less libssl-dev jo jq \
1616
&& apt-get -y autoclean
1717

1818
# Install wp-cli
19-
RUN curl -L https://github.com/wp-cli/wp-cli/releases/download/v2.6.0/wp-cli-2.6.0.phar --output /usr/bin/wp
19+
RUN curl -L https://github.com/wp-cli/wp-cli/releases/download/v2.12.0/wp-cli-2.12.0.phar --output /usr/bin/wp
2020
RUN chmod +x /usr/bin/wp
2121

2222
# Create wpcli working directory

wp_api/src/request/endpoint/post_revisions_endpoint.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{AsNamespace, DerivedRequest, WpNamespace};
22
use crate::{
33
SparseField,
44
post_revisions::{
5-
PostRevisionListParams, SparsePostRevisionFieldWithEditContext,
5+
PostRevisionId, PostRevisionListParams, SparsePostRevisionFieldWithEditContext,
66
SparsePostRevisionFieldWithEmbedContext, SparsePostRevisionFieldWithViewContext,
77
},
88
posts::PostId,
@@ -13,6 +13,8 @@ use wp_derive_request_builder::WpDerivedRequest;
1313
enum PostRevisionsRequest {
1414
#[contextual_paged(url = "/posts/<post_id>/revisions", params = &PostRevisionListParams, output = Vec<crate::post_revisions::SparsePostRevision>, filter_by = crate::post_revisions::SparsePostRevisionField)]
1515
List,
16+
#[contextual_get(url = "/posts/<post_id>/revisions/<post_revision_id>", output = crate::post_revisions::SparsePostRevision, filter_by = crate::post_revisions::SparsePostRevisionField)]
17+
Retrieve,
1618
}
1719

1820
impl DerivedRequest for PostRevisionsRequest {
@@ -126,6 +128,40 @@ mod tests {
126128
}
127129
}
128130

131+
#[rstest]
132+
fn retrieve_post_revision(endpoint: PostRevisionsRequestEndpoint) {
133+
let post_id = PostId(777);
134+
let revision_id = PostRevisionId(888);
135+
let expected_path =
136+
|context: &str| format!("/posts/{post_id}/revisions/{revision_id}?context={context}");
137+
validate_wp_v2_endpoint(
138+
endpoint.retrieve_with_edit_context(&post_id, &revision_id),
139+
&expected_path("edit"),
140+
);
141+
validate_wp_v2_endpoint(
142+
endpoint.retrieve_with_embed_context(&post_id, &revision_id),
143+
&expected_path("embed"),
144+
);
145+
validate_wp_v2_endpoint(
146+
endpoint.retrieve_with_view_context(&post_id, &revision_id),
147+
&expected_path("view"),
148+
);
149+
}
150+
151+
#[rstest]
152+
#[case(&[], "/posts/777/revisions/888?context=edit&_fields=")]
153+
#[case(&[SparsePostRevisionFieldWithEditContext::Date], "/posts/777/revisions/888?context=edit&_fields=date")]
154+
fn filter_retrieve_post_revision_with_edit_context(
155+
endpoint: PostRevisionsRequestEndpoint,
156+
#[case] fields: &[SparsePostRevisionFieldWithEditContext],
157+
#[case] expected_path: &str,
158+
) {
159+
validate_wp_v2_endpoint(
160+
endpoint.filter_retrieve_with_edit_context(&PostId(777), &PostRevisionId(888), fields),
161+
expected_path,
162+
);
163+
}
164+
129165
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";
130166
const ALL_SPARSE_POST_REVISION_FIELDS_WITH_EDIT_CONTEXT: &[SparsePostRevisionFieldWithEditContext;
131167
13] = &[

wp_api_integration_tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub struct TestCredentials {
2828
pub wordpress_core_version: &'static str,
2929
pub integration_test_custom_template_id: &'static str,
3030
pub revisioned_post_id: i64,
31+
pub revision_id_for_revisioned_post_id: i64,
3132
}
3233

3334
impl TestCredentials {

wp_api_integration_tests/tests/test_post_revisions_immut.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,44 @@ async fn list_with_view_context(#[case] params: PostRevisionListParams) {
4141
.assert_response();
4242
}
4343

44+
#[tokio::test]
45+
#[parallel]
46+
async fn retrieve_with_edit_context() {
47+
api_client()
48+
.post_revisions()
49+
.retrieve_with_edit_context(&revisioned_post_id(), &revision_id_for_revisioned_post_id())
50+
.await
51+
.assert_response();
52+
}
53+
54+
#[tokio::test]
55+
#[parallel]
56+
async fn retrieve_with_embed_context() {
57+
api_client()
58+
.post_revisions()
59+
.retrieve_with_embed_context(&revisioned_post_id(), &revision_id_for_revisioned_post_id())
60+
.await
61+
.assert_response();
62+
}
63+
64+
#[tokio::test]
65+
#[parallel]
66+
async fn retrieve_with_view_context() {
67+
api_client()
68+
.post_revisions()
69+
.retrieve_with_view_context(&revisioned_post_id(), &revision_id_for_revisioned_post_id())
70+
.await
71+
.assert_response();
72+
}
73+
4474
fn revisioned_post_id() -> PostId {
4575
PostId(TestCredentials::instance().revisioned_post_id)
4676
}
4777

78+
fn revision_id_for_revisioned_post_id() -> PostRevisionId {
79+
PostRevisionId(TestCredentials::instance().revision_id_for_revisioned_post_id)
80+
}
81+
4882
#[template]
4983
#[rstest]
5084
#[case::default(PostRevisionListParams::default())]
@@ -139,4 +173,64 @@ mod filter {
139173
post.assert_that_instance_fields_nullability_match_provided_fields(fields)
140174
});
141175
}
176+
177+
#[apply(sparse_post_revision_field_with_edit_context_test_cases)]
178+
#[case(&[SparsePostRevisionFieldWithEditContext::Id, SparsePostRevisionFieldWithEditContext::Author])]
179+
#[tokio::test]
180+
#[parallel]
181+
async fn filter_retrieve_with_edit_context(
182+
#[case] fields: &[SparsePostRevisionFieldWithEditContext],
183+
) {
184+
api_client()
185+
.post_revisions()
186+
.filter_retrieve_with_edit_context(
187+
&revisioned_post_id(),
188+
&revision_id_for_revisioned_post_id(),
189+
fields,
190+
)
191+
.await
192+
.assert_response()
193+
.data
194+
.assert_that_instance_fields_nullability_match_provided_fields(fields);
195+
}
196+
197+
#[apply(sparse_post_revision_field_with_embed_context_test_cases)]
198+
#[case(&[SparsePostRevisionFieldWithEmbedContext::Id, SparsePostRevisionFieldWithEmbedContext::Author])]
199+
#[tokio::test]
200+
#[parallel]
201+
async fn filter_retrieve_with_embed_context(
202+
#[case] fields: &[SparsePostRevisionFieldWithEmbedContext],
203+
) {
204+
api_client()
205+
.post_revisions()
206+
.filter_retrieve_with_embed_context(
207+
&revisioned_post_id(),
208+
&revision_id_for_revisioned_post_id(),
209+
fields,
210+
)
211+
.await
212+
.assert_response()
213+
.data
214+
.assert_that_instance_fields_nullability_match_provided_fields(fields);
215+
}
216+
217+
#[apply(sparse_post_revision_field_with_view_context_test_cases)]
218+
#[case(&[SparsePostRevisionFieldWithViewContext::Id, SparsePostRevisionFieldWithViewContext::Author])]
219+
#[tokio::test]
220+
#[parallel]
221+
async fn filter_retrieve_with_view_context(
222+
#[case] fields: &[SparsePostRevisionFieldWithViewContext],
223+
) {
224+
api_client()
225+
.post_revisions()
226+
.filter_retrieve_with_view_context(
227+
&revisioned_post_id(),
228+
&revision_id_for_revisioned_post_id(),
229+
fields,
230+
)
231+
.await
232+
.assert_response()
233+
.data
234+
.assert_that_instance_fields_nullability_match_provided_fields(fields);
235+
}
142236
}

0 commit comments

Comments
 (0)