@@ -75,6 +75,13 @@ public function get_data_to_store() {
7575 return 'text value... ' ;
7676 }
7777
78+ /**
79+ * @return mixed
80+ */
81+ public function get_preview_data_to_store () {
82+ return 'preview - ' . $ this ->get_data_to_store ();
83+ }
84+
7885 /**
7986 * Override this in the testing class to test the field against blocks
8087 *
@@ -123,6 +130,16 @@ public function get_expected_value() {
123130 return 'null ' ;
124131 }
125132
133+ /**
134+ * Test class should override this with the expected value
135+ *
136+ * @return mixed
137+ */
138+ public function get_expected_preview_value () {
139+ return 'null ' ;
140+ }
141+
142+
126143 /**
127144 * @return string
128145 */
@@ -1032,6 +1049,111 @@ public function testQueryFieldOnPostReturnsExpectedValue() {
10321049
10331050 }
10341051
1052+ public function testQueryFieldOnPostAsPreviewReturnsExpectedValue () {
1053+
1054+ // disable the block editor
1055+ add_filter ('use_block_editor_for_post ' , '__return_false ' );
1056+
1057+ $ field_key = $ this ->register_acf_field ();
1058+
1059+ // Save data to the post
1060+ update_field ( $ field_key , $ this ->get_data_to_store (), $ this ->published_post ->ID );
1061+
1062+ $ fragment = $ this ->get_query_fragment ();
1063+
1064+ if ( 'null ' === $ fragment ) {
1065+ $ this ->markTestIncomplete ( 'get_query_fragment() not defined ' );
1066+ }
1067+
1068+ $ expected_value = $ this ->get_expected_value ();
1069+
1070+ if ( 'null ' === $ expected_value ) {
1071+ $ this ->markTestIncomplete ( 'get_expected_value() not defined ' );
1072+ }
1073+
1074+ $ expected_preview_value = $ this ->get_expected_preview_value ();
1075+
1076+ if ( 'null ' === $ expected_preview_value ) {
1077+ $ this ->markTestIncomplete ( 'get_expected_preview_value() not defined ' );
1078+ }
1079+
1080+ $ preview_data_to_store = $ this ->get_preview_data_to_store ();
1081+
1082+ if ( 'null ' === $ preview_data_to_store ) {
1083+ $ this ->markTestIncomplete ( 'get_preview_data_to_store() not defined ' );
1084+ }
1085+
1086+ $ query = '
1087+ query AcfFieldOnPost ($id: ID! $asPreview:Boolean) {
1088+ post( id: $id idType: DATABASE_ID asPreview: $asPreview) {
1089+ databaseId
1090+ __typename
1091+ title
1092+ ...on WithAcfAcfTestGroup {
1093+ acfTestGroup {
1094+ ...AcfTestGroupFragment
1095+ }
1096+ }
1097+ }
1098+ }
1099+ ' . $ fragment ;
1100+
1101+ $ actual = $ this ->graphql ([
1102+ 'query ' => $ query ,
1103+ 'variables ' => [
1104+ 'id ' => $ this ->published_post ->ID ,
1105+ 'asPreview ' => false ,
1106+ ]
1107+ ]);
1108+
1109+ // querying as preview without any preview revision will
1110+ // return the same result as querying as published
1111+ self ::assertQuerySuccessful ( $ actual , [
1112+ $ this ->expectedField ( 'post.databaseId ' , $ this ->published_post ->ID ),
1113+ $ this ->expectedField ( 'post.__typename ' , 'Post ' ),
1114+ $ this ->expectedField ( 'post.acfTestGroup. ' . $ this ->get_formatted_field_name (), $ this ->get_expected_value () )
1115+ ]);
1116+
1117+ // set the current user as admin
1118+ wp_set_current_user ( $ this ->admin ->ID );
1119+
1120+ // create a preview revision
1121+ $ preview_id = wp_create_post_autosave ( [
1122+ 'post_ID ' => $ this ->published_post ->ID ,
1123+ 'post_type ' => 'post ' ,
1124+ 'post_title ' => 'Preview Title ' ,
1125+ ] );
1126+
1127+ if ( is_wp_error ( $ preview_id ) ) {
1128+ // Handle error; autosave creation failed
1129+ codecept_debug ( 'Error creating autosave: ' . $ preview_id ->get_error_message () );
1130+ }
1131+
1132+ // update the preview revision with the preview data
1133+ update_field ( $ field_key , $ this ->get_preview_data_to_store (), $ preview_id );
1134+
1135+ $ actual = $ this ->graphql ([
1136+ 'query ' => $ query ,
1137+ 'variables ' => [
1138+ 'id ' => $ this ->published_post ->ID ,
1139+ 'asPreview ' => true ,
1140+ ]
1141+ ]);
1142+
1143+ self ::assertQuerySuccessful ( $ actual , [
1144+ $ this ->expectedField ( 'post.databaseId ' , $ preview_id ),
1145+ $ this ->expectedField ( 'post.__typename ' , 'Post ' ),
1146+ $ this ->expectedField ( 'post.title ' , 'Preview Title ' ),
1147+ $ this ->expectedField ( 'post.acfTestGroup. ' . $ this ->get_formatted_field_name (), $ this ->get_expected_preview_value () )
1148+ ]);
1149+
1150+ wp_delete_post ( $ preview_id , true );
1151+
1152+ // re-enable the block editor
1153+ add_filter ('use_block_editor_for_post ' , '__return_true ' );
1154+
1155+ }
1156+
10351157 /**
10361158 * @todo: implement the below tests
10371159 */
0 commit comments