Skip to content

Commit

Permalink
Merge pull request #189 from wp-graphql/fix/187-preview-issues-with-i…
Browse files Browse the repository at this point in the history
…mage-fields

fix: image fields (and other connection fields) not properly resolving when queried `asPreview`
  • Loading branch information
jasonbahl committed Mar 15, 2024
2 parents ece620b + c4f7595 commit 071cfec
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/FieldConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ public function get_graphql_field_config(): ?array {
// bail and let the connection handle registration to the schema
// and resolution
if ( 'connection' === $field_type ) {
$this->registry->register_field( $this->acf_field );
return null;
}

Expand Down
23 changes: 18 additions & 5 deletions src/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ public function get_registered_fields() {
return $this->registered_fields;
}

/**
* @param array<mixed> $acf_field
*
* @return void
*/
public function register_field( $acf_field ) {

if ( isset( $acf_field['name'] ) && ! in_array( $acf_field['name'], $this->registered_fields, true ) ) {
$this->registered_fields[] = $acf_field['name'];
}

if ( isset( $acf_field['key'] ) && ! in_array( $acf_field['key'], $this->registered_fields, true ) ) {
$this->registered_fields[] = $acf_field['key'];
}
}

/**
* Get the TypeRegistry instance
*/
Expand Down Expand Up @@ -505,11 +521,8 @@ public function map_acf_field_to_graphql( array $acf_field, array $acf_field_gro
$field_config = ( new FieldConfig( $acf_field, $acf_field_group, $this ) )->get_graphql_field_config();

if ( ! empty( $field_config['acf_field'] ) ) {
if ( isset( $field_config['acf_field']['key'] ) && ! in_array( $field_config['acf_field']['key'], $this->registered_fields, true ) ) {
$this->registered_fields[] = $field_config['acf_field']['key'];
}
if ( isset( $field_config['acf_field']['name'] ) && ! in_array( $field_config['acf_field']['name'], $this->registered_fields, true ) ) {
$this->registered_fields[] = $field_config['acf_field']['name'];
if ( isset( $field_config['acf_field'] ) ) {
$this->register_field( $field_config['acf_field'] );
}
}

Expand Down
Binary file added tests/_data/images/test2.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
122 changes: 122 additions & 0 deletions tests/_support/WPUnit/AcfFieldTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ public function get_data_to_store() {
return 'text value...';
}

/**
* @return mixed
*/
public function get_preview_data_to_store() {
return 'preview - ' . $this->get_data_to_store();
}

/**
* Override this in the testing class to test the field against blocks
*
Expand Down Expand Up @@ -123,6 +130,16 @@ public function get_expected_value() {
return 'null';
}

/**
* Test class should override this with the expected value
*
* @return mixed
*/
public function get_expected_preview_value() {
return 'null';
}


/**
* @return string
*/
Expand Down Expand Up @@ -1032,6 +1049,111 @@ public function testQueryFieldOnPostReturnsExpectedValue() {

}

public function testQueryFieldOnPostAsPreviewReturnsExpectedValue() {

// disable the block editor
add_filter('use_block_editor_for_post', '__return_false');

$field_key = $this->register_acf_field();

// Save data to the post
update_field( $field_key, $this->get_data_to_store(), $this->published_post->ID );

$fragment = $this->get_query_fragment();

if ( 'null' === $fragment ) {
$this->markTestIncomplete( 'get_query_fragment() not defined' );
}

$expected_value = $this->get_expected_value();

if ( 'null' === $expected_value ) {
$this->markTestIncomplete( 'get_expected_value() not defined' );
}

$expected_preview_value = $this->get_expected_preview_value();

if ( 'null' === $expected_preview_value ) {
$this->markTestIncomplete( 'get_expected_preview_value() not defined' );
}

$preview_data_to_store = $this->get_preview_data_to_store();

if ( 'null' === $preview_data_to_store ) {
$this->markTestIncomplete( 'get_preview_data_to_store() not defined' );
}

$query = '
query AcfFieldOnPost ($id: ID! $asPreview:Boolean) {
post( id: $id idType: DATABASE_ID asPreview: $asPreview) {
databaseId
__typename
title
...on WithAcfAcfTestGroup {
acfTestGroup {
...AcfTestGroupFragment
}
}
}
}
' . $fragment;

$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $this->published_post->ID,
'asPreview' => false,
]
]);

// querying as preview without any preview revision will
// return the same result as querying as published
self::assertQuerySuccessful( $actual, [
$this->expectedField( 'post.databaseId', $this->published_post->ID ),
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_value() )
]);

// set the current user as admin
wp_set_current_user( $this->admin->ID );

// create a preview revision
$preview_id = wp_create_post_autosave( [
'post_ID' => $this->published_post->ID,
'post_type' => 'post',
'post_title' => 'Preview Title',
] );

if ( is_wp_error( $preview_id ) ) {
// Handle error; autosave creation failed
codecept_debug( 'Error creating autosave: ' . $preview_id->get_error_message() );
}

// update the preview revision with the preview data
update_field( $field_key, $this->get_preview_data_to_store(), $preview_id );

$actual = $this->graphql([
'query' => $query,
'variables' => [
'id' => $this->published_post->ID,
'asPreview' => true,
]
]);

self::assertQuerySuccessful( $actual, [
$this->expectedField( 'post.databaseId', $preview_id ),
$this->expectedField( 'post.__typename', 'Post' ),
$this->expectedField( 'post.title', 'Preview Title' ),
$this->expectedField( 'post.acfTestGroup.' . $this->get_formatted_field_name(), $this->get_expected_preview_value() )
]);

wp_delete_post( $preview_id, true );

// re-enable the block editor
add_filter('use_block_editor_for_post', '__return_true');

}

/**
* @todo: implement the below tests
*/
Expand Down
16 changes: 14 additions & 2 deletions tests/_support/WPUnit/WPGraphQLAcfTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ class WPGraphQLAcfTestCase extends \Tests\WPGraphQL\TestCase\WPGraphQLTestCase {
public $test_image;

/**
* @var \WP_Post
* @var string
*/
public $test_image_2;

/**
* @var int
*/
public $imageId;

/**
* @var \WP_Post
* @var int
*/
public $imageId_2;

/**
* @var int
*/
public $fileId;

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

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

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

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

$this->menu_location_name = 'test-location';
add_theme_support( 'nav_menus' );
Expand Down
40 changes: 39 additions & 1 deletion tests/wpunit/FieldTypes/ImageFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ public function setUp(): void {
parent::setUp();
}


/**
* @return void
*/
Expand All @@ -29,6 +28,44 @@ public function get_expected_field_resolve_kind(): ?string {
return 'OBJECT';
}

public function get_query_fragment(): string {
return '
fragment AcfTestGroupFragment on AcfTestGroup {
testImage {
node {
__typename
databaseId
}
}
}';
}

public function get_data_to_store() {
return $this->imageId;
}

public function get_expected_value() {
return [
'node' => [
'__typename' => 'MediaItem',
'databaseId' => $this->imageId
]
];
}

public function get_preview_data_to_store() {
return $this->imageId_2;
}

public function get_expected_preview_value() {
return [
'node' => [
'__typename' => 'MediaItem',
'databaseId' => $this->imageId_2
]
];;
}

public function get_block_query_fragment() {
return '
fragment BlockQueryFragment on AcfTestGroup {
Expand All @@ -46,6 +83,7 @@ public function get_block_data_to_store() {
return $this->imageId;
}


public function get_expected_block_fragment_response() {
return [
'node' => [
Expand Down
4 changes: 4 additions & 0 deletions tests/wpunit/FieldTypes/TextFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public function get_expected_value() {
return $this->get_data_to_store();
}

public function get_expected_preview_value() {
return $this->get_preview_data_to_store();
}

public function get_expected_block_fragment_response() {
return $this->get_block_data_to_store();
}
Expand Down

0 comments on commit 071cfec

Please sign in to comment.