Skip to content

Commit 071cfec

Browse files
authored
Merge pull request #189 from wp-graphql/fix/187-preview-issues-with-image-fields
fix: image fields (and other connection fields) not properly resolving when queried `asPreview`
2 parents ece620b + c4f7595 commit 071cfec

File tree

7 files changed

+198
-8
lines changed

7 files changed

+198
-8
lines changed

src/FieldConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ public function get_graphql_field_config(): ?array {
245245
// bail and let the connection handle registration to the schema
246246
// and resolution
247247
if ( 'connection' === $field_type ) {
248+
$this->registry->register_field( $this->acf_field );
248249
return null;
249250
}
250251

src/Registry.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public function get_registered_fields() {
6565
return $this->registered_fields;
6666
}
6767

68+
/**
69+
* @param array<mixed> $acf_field
70+
*
71+
* @return void
72+
*/
73+
public function register_field( $acf_field ) {
74+
75+
if ( isset( $acf_field['name'] ) && ! in_array( $acf_field['name'], $this->registered_fields, true ) ) {
76+
$this->registered_fields[] = $acf_field['name'];
77+
}
78+
79+
if ( isset( $acf_field['key'] ) && ! in_array( $acf_field['key'], $this->registered_fields, true ) ) {
80+
$this->registered_fields[] = $acf_field['key'];
81+
}
82+
}
83+
6884
/**
6985
* Get the TypeRegistry instance
7086
*/
@@ -505,11 +521,8 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
505521
$field_config = ( new FieldConfig( $acf_field, $acf_field_group, $this ) )->get_graphql_field_config();
506522

507523
if ( ! empty( $field_config['acf_field'] ) ) {
508-
if ( isset( $field_config['acf_field']['key'] ) && ! in_array( $field_config['acf_field']['key'], $this->registered_fields, true ) ) {
509-
$this->registered_fields[] = $field_config['acf_field']['key'];
510-
}
511-
if ( isset( $field_config['acf_field']['name'] ) && ! in_array( $field_config['acf_field']['name'], $this->registered_fields, true ) ) {
512-
$this->registered_fields[] = $field_config['acf_field']['name'];
524+
if ( isset( $field_config['acf_field'] ) ) {
525+
$this->register_field( $field_config['acf_field'] );
513526
}
514527
}
515528

tests/_data/images/test2.png

3.49 KB
Loading

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
*/
@@ -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
*/

tests/_support/WPUnit/WPGraphQLAcfTestCase.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,22 @@ class WPGraphQLAcfTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
7878
public $test_image;
7979

8080
/**
81-
* @var \WP_Post
81+
* @var string
82+
*/
83+
public $test_image_2;
84+
85+
/**
86+
* @var int
8287
*/
8388
public $imageId;
8489

8590
/**
86-
* @var \WP_Post
91+
* @var int
92+
*/
93+
public $imageId_2;
94+
95+
/**
96+
* @var int
8797
*/
8898
public $fileId;
8999

@@ -103,6 +113,7 @@ public function setUp(): void {
103113
$this->is_acf_pro = in_array( 'advanced-custom-fields-pro/acf.php', $active_plugins, true );
104114

105115
$this->test_image = dirname( __FILE__, 3 ) . '/_data/images/test.png';
116+
$this->test_image_2 = dirname( __FILE__, 3 ) . '/_data/images/test2.png';
106117

107118
// create users for use within tests
108119
$this->admin = self::factory()->user->create_and_get( [ 'role' => 'administrator' ] );
@@ -165,6 +176,7 @@ public function setUp(): void {
165176
$this->fileId = self::factory()->attachment->create_upload_object( $this->test_image, 0 );
166177

167178
$this->imageId = self::factory()->attachment->create_upload_object( $this->test_image, $this->published_post->ID );
179+
$this->imageId_2 = self::factory()->attachment->create_upload_object( $this->test_image_2, $this->published_post->ID );
168180

169181
$this->menu_location_name = 'test-location';
170182
add_theme_support( 'nav_menus' );

tests/wpunit/FieldTypes/ImageFieldTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ public function setUp(): void {
99
parent::setUp();
1010
}
1111

12-
1312
/**
1413
* @return void
1514
*/
@@ -29,6 +28,44 @@ public function get_expected_field_resolve_kind(): ?string {
2928
return 'OBJECT';
3029
}
3130

31+
public function get_query_fragment(): string {
32+
return '
33+
fragment AcfTestGroupFragment on AcfTestGroup {
34+
testImage {
35+
node {
36+
__typename
37+
databaseId
38+
}
39+
}
40+
}';
41+
}
42+
43+
public function get_data_to_store() {
44+
return $this->imageId;
45+
}
46+
47+
public function get_expected_value() {
48+
return [
49+
'node' => [
50+
'__typename' => 'MediaItem',
51+
'databaseId' => $this->imageId
52+
]
53+
];
54+
}
55+
56+
public function get_preview_data_to_store() {
57+
return $this->imageId_2;
58+
}
59+
60+
public function get_expected_preview_value() {
61+
return [
62+
'node' => [
63+
'__typename' => 'MediaItem',
64+
'databaseId' => $this->imageId_2
65+
]
66+
];;
67+
}
68+
3269
public function get_block_query_fragment() {
3370
return '
3471
fragment BlockQueryFragment on AcfTestGroup {
@@ -46,6 +83,7 @@ public function get_block_data_to_store() {
4683
return $this->imageId;
4784
}
4885

86+
4987
public function get_expected_block_fragment_response() {
5088
return [
5189
'node' => [

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)