Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

MTKA-1542: Ensure relationship fields resolve if model slug mismatches singular label #566

Merged
merged 3 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Fixed
- Issue where adding a new repeating field to an existing model schema could break GraphQL queries under certain conditions.
- Empty field values are no longer saved to the database.
- Relationship fields no longer resolve as “null” in GraphQL results for models with different singular names and API identifiers.

### Added
- Added email validation when using insert_model_entry() crud function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,8 @@ function register_relationship_connection( array $parent_model, array $reference
array(
'from_type' => camelcase( $parent_model['singular'] ),
'to_type' => camelcase( $reference_model['singular'] ),
'from_slug' => $parent_model['slug'],
'to_slug' => $reference_model['slug'],
'slug' => $field['slug'],
'one_to_one' => ( $field['cardinality'] === 'one-to-one' || $field['cardinality'] === 'many-to-one' ),
'reference' => $field['reference'],
Expand All @@ -670,6 +672,8 @@ function register_relationship_connection( array $parent_model, array $reference
$connections[] = array(
'from_type' => camelcase( $reference_model['singular'] ),
'to_type' => camelcase( $parent_model['singular'] ),
'from_slug' => $reference_model['slug'],
'to_slug' => $parent_model['slug'],
'slug' => $field['reverseSlug'],
'one_to_one' => false,
'reference' => $parent_model['slug'],
Expand All @@ -690,8 +694,8 @@ function register_relationship_connection( array $parent_model, array $reference
$registry = \WPE\AtlasContentModeler\ContentConnect\Plugin::instance()->get_registry();

$relationship = $registry->get_post_to_post_relationship(
sanitize_key( $connection_args['to_type'] ),
sanitize_key( $connection_args['from_type'] ),
sanitize_key( $connection_args['to_slug'] ),
sanitize_key( $connection_args['from_slug'] ),
$connection_args['name']
);

Expand Down
28 changes: 28 additions & 0 deletions tests/integration/api-validation/test-data/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,32 @@
'plural' => 'Attachments',
'fields' => array(),
),
'different-slug' => array(
'show_in_rest' => true,
'show_in_graphql' => true,
'singular' => 'Custom Slug',
'plural' => 'Custom Slugs',
'slug' => 'different-slug',
'api_visibility' => 'public',
'model_icon' => 'dashicons-admin-post',
'description' => 'Singular name differs from slug.',
'with_front' => false,
'fields' => [
'1630411590619' => [
'show_in_rest' => true,
'show_in_graphql' => true,
'type' => 'relationship',
'id' => '1630411590619',
'position' => '170000',
'name' => 'many-to-many-Relationship',
'slug' => 'manytoManyRelationship',
'required' => false,
'minChars' => '',
'maxChars' => '',
'reference' => 'different-slug',
'cardinality' => 'many-to-many',
'description' => '',
],
],
),
);
24 changes: 24 additions & 0 deletions tests/integration/api-validation/test-data/posts.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use function WPE\AtlasContentModeler\API\add_relationship;

/**
* Saves mock post data for processing
*/
Expand Down Expand Up @@ -61,6 +64,27 @@ function create_test_posts( $test_class ) {
)
);

$ids['custom_slug_1'] = $test_class->factory->post->create(
array(
'post_status' => 'publish',
'post_type' => 'different-slug',
)
);

$ids['custom_slug_2'] = $test_class->factory->post->create(
array(
'post_status' => 'publish',
'post_type' => 'different-slug',
)
);

// Create a relationship between the two previous posts.
add_relationship(
$ids['custom_slug_1'],
'manytoManyRelationship', // The relationship field slug from models.php.
$ids['custom_slug_2']
);

return $ids;
}

Expand Down
33 changes: 33 additions & 0 deletions tests/integration/api-validation/test-graphql-model-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,39 @@ public function test_graphql_query_result_has_custom_fields_data(): void {
}
}

public function test_graphql_query_resolves_relationship_field_with_different_model_singular_name_and_id(): void {
try {
$results = graphql(
[
'query' => '
{
customSlugs {
nodes {
databaseId
manytoManyRelationship {
edges {
node {
databaseId
}
}
}
}
}
}
',
]
);

self::assertArrayHasKey( 'databaseId', $results['data']['customSlugs']['nodes'][0] );

self::assertArrayHasKey( 'manytoManyRelationship', $results['data']['customSlugs']['nodes'][0] );
self::assertNotNull( $results['data']['customSlugs']['nodes'][0]['manytoManyRelationship']['edges'] );
self::assertEquals( $this->post_ids['custom_slug_1'], $results['data']['customSlugs']['nodes'][0]['manytoManyRelationship']['edges'][0]['node']['databaseId'] );
} catch ( Exception $exception ) {
throw new PHPUnitRunnerException( sprintf( __FUNCTION__ . ' failed with exception: %s', $exception->getMessage() ) );
}
}

public function test_graphql_create_mutations_accept_acm_fields_as_inputs(): void {
wp_set_current_user( 1 );
try {
Expand Down