Skip to content

Commit d3542ef

Browse files
authored
Merge pull request #3201 from wp-graphql/fix/3200-connectedTerms-resolver
fix: ensure connectedTerms returns terms for the specified taxonomy only
2 parents eae8607 + 924bbf5 commit d3542ef

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/Data/Connection/TermObjectConnectionResolver.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ public function __construct( $source, array $args, AppContext $context, ResolveI
3535
*/
3636
protected function prepare_query_args( array $args ): array {
3737
$all_taxonomies = \WPGraphQL::get_allowed_taxonomies();
38-
$taxonomy = ! empty( $this->taxonomy ) && in_array( $this->taxonomy, $all_taxonomies, true ) ? [ $this->taxonomy ] : $all_taxonomies;
38+
39+
if ( ! is_array( $this->taxonomy ) ) {
40+
$taxonomy = ! empty( $this->taxonomy ) && in_array( $this->taxonomy, $all_taxonomies, true ) ? [ $this->taxonomy ] : $all_taxonomies;
41+
} else {
42+
$taxonomy = $this->taxonomy;
43+
}
3944

4045
if ( ! empty( $args['where']['taxonomies'] ) ) {
4146
/**

tests/wpunit/TermObjectConnectionQueriesTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,62 @@ public function getQuery() {
109109
';
110110
}
111111

112+
public function testConnectedTermsConnectionReturnsTermNodesOfExpectedTaxonomies() {
113+
$term_one_id = self::factory()->term->create(
114+
[
115+
'taxonomy' => 'category',
116+
'name' => 'Term One',
117+
]
118+
);
119+
120+
$term_two_id = self::factory()->term->create(
121+
[
122+
'taxonomy' => 'post_tag',
123+
'name' => 'Term Two',
124+
]
125+
);
126+
127+
$query = '
128+
query GetConnectedTerms {
129+
taxonomies {
130+
nodes {
131+
name
132+
connectedTerms {
133+
nodes {
134+
__typename
135+
databaseId
136+
name
137+
uri
138+
}
139+
}
140+
}
141+
}
142+
}
143+
';
144+
145+
$actual = $this->graphql([
146+
'query' => $query
147+
]);
148+
149+
// codecept_debug( [ '$actual' => $actual ]);
150+
$this->assertArrayNotHasKey( 'errors', $actual );
151+
152+
// map over the connected terms nodes and assert that all of them are category
153+
$connected_categories = $actual['data']['taxonomies']['nodes'][0]['connectedTerms']['nodes'];
154+
foreach ( $connected_categories as $connected_category ) {
155+
$this->assertEquals( 'Category', $connected_category['__typename'] );
156+
}
157+
158+
// map over the connected terms nodes and assert that all of them are tags
159+
$connected_tags = $actual['data']['taxonomies']['nodes'][1]['connectedTerms']['nodes'];
160+
foreach ( $connected_tags as $connected_tag ) {
161+
$this->assertEquals( 'Tag', $connected_tag['__typename'] );
162+
}
163+
164+
wp_delete_term( $term_one_id, 'category' );
165+
wp_delete_term( $term_two_id, 'post_tag' );
166+
}
167+
112168
public function testForwardPagination() {
113169
$query = $this->getQuery();
114170
$wp_query = new WP_Term_Query();
@@ -945,4 +1001,5 @@ public function testPaginateTermsWithDuplicateNamesAndOrderbyName() {
9451001

9461002
wp_delete_term( $parent_category, 'category' );
9471003
}
1004+
9481005
}

0 commit comments

Comments
 (0)