Skip to content

Commit a73075d

Browse files
committed
- update ACfFieldTestCase to provide support for testing querying against previews
- update TextFieldTest.php to test querying against a preview
1 parent 1a27b31 commit a73075d

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

tests/_support/WPUnit/AcfFieldTestCase.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
@@ -1026,6 +1043,111 @@ public function testQueryFieldOnPostReturnsExpectedValue() {
10261043

10271044
}
10281045

1046+
public function testQueryFieldOnPostAsPreviewReturnsExpectedValue() {
1047+
1048+
// disable the block editor
1049+
add_filter('use_block_editor_for_post', '__return_false');
1050+
1051+
$field_key = $this->register_acf_field();
1052+
1053+
// Save data to the post
1054+
update_field( $field_key, $this->get_data_to_store(), $this->published_post->ID );
1055+
1056+
$fragment = $this->get_query_fragment();
1057+
1058+
if ( 'null' === $fragment ) {
1059+
$this->markTestIncomplete( 'get_query_fragment() not defined' );
1060+
}
1061+
1062+
$expected_value = $this->get_expected_value();
1063+
1064+
if ( 'null' === $expected_value ) {
1065+
$this->markTestIncomplete( 'get_expected_value() not defined' );
1066+
}
1067+
1068+
$expected_preview_value = $this->get_expected_preview_value();
1069+
1070+
if ( 'null' === $expected_preview_value ) {
1071+
$this->markTestIncomplete( 'get_expected_preview_value() not defined' );
1072+
}
1073+
1074+
$preview_data_to_store = $this->get_preview_data_to_store();
1075+
1076+
if ( 'null' === $preview_data_to_store ) {
1077+
$this->markTestIncomplete( 'get_preview_data_to_store() not defined' );
1078+
}
1079+
1080+
$query = '
1081+
query AcfFieldOnPost ($id: ID! $asPreview:Boolean) {
1082+
post( id: $id idType: DATABASE_ID asPreview: $asPreview) {
1083+
databaseId
1084+
__typename
1085+
title
1086+
...on WithAcfAcfTestGroup {
1087+
acfTestGroup {
1088+
...AcfTestGroupFragment
1089+
}
1090+
}
1091+
}
1092+
}
1093+
' . $fragment;
1094+
1095+
$actual = $this->graphql([
1096+
'query' => $query,
1097+
'variables' => [
1098+
'id' => $this->published_post->ID,
1099+
'asPreview' => false,
1100+
]
1101+
]);
1102+
1103+
// querying as preview without any preview revision will
1104+
// return the same result as querying as published
1105+
self::assertQuerySuccessful( $actual, [
1106+
$this->expectedField( 'post.databaseId', $this->published_post->ID ),
1107+
$this->expectedField( 'post.__typename', 'Post' ),
1108+
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_value() )
1109+
]);
1110+
1111+
// set the current user as admin
1112+
wp_set_current_user( $this->admin->ID );
1113+
1114+
// create a preview revision
1115+
$preview_id = wp_create_post_autosave( [
1116+
'post_ID' => $this->published_post->ID,
1117+
'post_type' => 'post',
1118+
'post_title' => 'Preview Title',
1119+
] );
1120+
1121+
if ( is_wp_error( $preview_id ) ) {
1122+
// Handle error; autosave creation failed
1123+
codecept_debug( 'Error creating autosave: ' . $preview_id->get_error_message() );
1124+
}
1125+
1126+
// update the preview revision with the preview data
1127+
update_field( $field_key, $this->get_preview_data_to_store(), $preview_id );
1128+
1129+
$actual = $this->graphql([
1130+
'query' => $query,
1131+
'variables' => [
1132+
'id' => $this->published_post->ID,
1133+
'asPreview' => true,
1134+
]
1135+
]);
1136+
1137+
self::assertQuerySuccessful( $actual, [
1138+
$this->expectedField( 'post.databaseId', $preview_id ),
1139+
$this->expectedField( 'post.__typename', 'Post' ),
1140+
$this->expectedField( 'post.title', 'Preview Title' ),
1141+
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_preview_value() )
1142+
]);
1143+
1144+
wp_delete_post( $preview_id, true );
1145+
1146+
// re-enable the block editor
1147+
add_filter('use_block_editor_for_post', '__return_true');
1148+
1149+
}
1150+
10291151
/**
10301152
* @todo: implement the below tests
10311153
*/

tests/wpunit/FieldTypes/TextFieldTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ public function get_expected_value() {
7272
return $this->get_data_to_store();
7373
}
7474

75+
public function get_expected_preview_value() {
76+
return $this->get_preview_data_to_store();
77+
}
78+
7579
public function get_expected_block_fragment_response() {
7680
return $this->get_block_data_to_store();
7781
}

0 commit comments

Comments
 (0)