@@ -85,6 +85,20 @@ wp theme activate twentytwentyfour
8585wp comment trash 22
8686wp comment spam 23
8787
88+ create_post_revision () {
89+ local revision_number=" $1 "
90+ local post_id=" $2 "
91+
92+ curl --silent --user " $ADMIN_USERNAME " :" $ADMIN_PASSWORD " -H " Content-Type: application/json" -d " {\" content\" :\" content_revision_$revision_number \" , \" author\" : $ADMIN_USER_ID }" " http://localhost/wp-json/wp/v2/posts/$post_id " > /dev/null
93+ }
94+
95+ create_post_autosave () {
96+ local autosave_number=" $1 "
97+ local post_id=" $2 "
98+
99+ curl --silent --user " $ADMIN_USERNAME " :" $ADMIN_PASSWORD " -H " Content-Type: application/json" -d " {\" content\" :\" content_autosave_$autosave_number \" , \" author\" : $ADMIN_USER_ID }" " http://localhost/wp-json/wp/v2/posts/$post_id /autosaves"
100+ }
101+
88102create_test_credentials () {
89103 local SITE_URL
90104 local ADMIN_USERNAME
@@ -98,6 +112,8 @@ create_test_credentials () {
98112 local PASSWORD_PROTECTED_COMMENT_ID
99113 local PASSWORD_PROTECTED_COMMENT_AUTHOR
100114 local REVISIONED_POST_ID
115+ local AUTOSAVED_POST_ID
116+ local AUTOSAVE_ID_FOR_AUTOSAVED_POST_ID
101117 local FIRST_POST_DATE_GMT
102118 local WORDPRESS_VERSION
103119 local INTEGRATION_TEST_CUSTOM_TEMPLATE_ID
@@ -133,13 +149,26 @@ create_test_credentials () {
133149
134150 echo " Setting up a post with 10 revisions for integration tests.."
135151 REVISIONED_POST_ID=" $( wp post create --post_type=post --post_title=Revisioned_POST_FOR_INTEGRATION_TESTS --porcelain) "
152+ # Create revisions
136153 for i in {1..10};
137154 do
138- 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" > /dev/null
155+ create_post_revision " $i " " $ REVISIONED_POST_ID"
139156 done
140157 # 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
141158 REVISION_ID_FOR_REVISIONED_POST_ID=$(( REVISIONED_POST_ID + 1 ))
142159
160+ echo " Setting up a post with autosave for integration tests.."
161+ # Autosaves require special setup: WordPress only creates separate autosave revisions when
162+ # the autosave is made by a different user than the post author. If the same user creates
163+ # an autosave, WordPress just updates the original post instead of creating a revision.
164+ # See: https://github.com/WordPress/WordPress/blob/c90da2b67323e36b0390a9efa95f2b4bbb3be31b/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php#L235-L244
165+ # Create post as author user to enable proper autosave behavior
166+ AUTHOR_USER_ID=" $( wp user get " $AUTHOR_USERNAME " --field=ID) "
167+ AUTOSAVED_POST_ID=" $( wp post create --post_type=post --post_title=Autosaved_POST_FOR_INTEGRATION_TESTS --post_author=" $AUTHOR_USER_ID " --porcelain) "
168+ # Create autosave as admin user (different from post author) and capture its ID
169+ AUTOSAVE_RESPONSE=" $( create_post_autosave " 1" " $AUTOSAVED_POST_ID " ) "
170+ AUTOSAVE_ID_FOR_AUTOSAVED_POST_ID=" $( echo " $AUTOSAVE_RESPONSE " | jq -r ' .id' ) "
171+
143172 rm -rf /app/test_credentials.json
144173 jo -p \
145174 site_url=" $SITE_URL " \
@@ -162,6 +191,8 @@ create_test_credentials () {
162191 integration_test_custom_template_id=" $INTEGRATION_TEST_CUSTOM_TEMPLATE_ID " \
163192 revisioned_post_id=" $REVISIONED_POST_ID " \
164193 revision_id_for_revisioned_post_id=" $REVISION_ID_FOR_REVISIONED_POST_ID " \
194+ autosaved_post_id=" $AUTOSAVED_POST_ID " \
195+ autosave_id_for_autosaved_post_id=" $AUTOSAVE_ID_FOR_AUTOSAVED_POST_ID " \
165196 > /app/test_credentials.json
166197}
167198create_test_credentials
0 commit comments