Skip to content

Commit 250b4e2

Browse files
authored
Merge pull request #148 from wp-graphql/fix/#133-taxonomy-field-on-blocks
fix: #133 taxonomy field on blocks
2 parents 0eb0db5 + ed4a449 commit 250b4e2

File tree

3 files changed

+217
-7
lines changed

3 files changed

+217
-7
lines changed

src/FieldType/Taxonomy.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,29 @@ public static function register_field_type(): void {
2626
return null;
2727
}
2828

29+
$values = [];
2930
if ( ! is_array( $value ) ) {
30-
$value[] = $value;
31+
$values[] = $value;
32+
} else {
33+
$values = $value;
3134
}
3235

33-
$value = array_map(
36+
$ids = array_map(
3437
static function ( $id ) {
3538
return absint( $id );
3639
},
37-
$value
40+
$values
3841
);
3942

43+
if ( empty( $ids ) ) {
44+
return null;
45+
}
46+
4047
$resolver = new TermObjectConnectionResolver( $root, $args, $context, $info );
4148
return $resolver
4249
// Set the query to include only the IDs passed in from the field
4350
// and orderby the ids
44-
->set_query_arg( 'include', $value )
51+
->set_query_arg( 'include', $ids )
4552
->set_query_arg( 'orderby', 'include' )
4653
->get_connection();
4754
},

src/FieldType/User.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ public static function register_field_type(): void {
3939
return null;
4040
}
4141

42+
$values = [];
4243
if ( ! is_array( $value ) ) {
43-
$value = [ $value ];
44+
$values[] = $value;
4445
}
4546

4647
$value = array_map(
@@ -50,10 +51,9 @@ static function ( $user ) {
5051
}
5152
return absint( $user );
5253
},
53-
$value
54+
$values
5455
);
5556

56-
5757
$resolver = new UserConnectionResolver( $root, $args, $context, $info );
5858
return $resolver->set_query_arg( 'include', $value )->set_query_arg( 'orderby', 'include' )->get_connection();
5959
},

tests/wpunit/FieldTypes/TaxonomyFieldTest.php

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use WPGraphQL\Utils\Utils;
4+
35
class TaxonomyFieldTest extends \Tests\WPGraphQL\Acf\WPUnit\AcfFieldTestCase {
46

57
/**
@@ -90,4 +92,205 @@ public function get_expected_block_fragment_response() {
9092
]
9193
];
9294
}
95+
96+
public function testQueryTaxononomyFieldOnBlock() {
97+
98+
// if ACF PRO is not active, skip the test
99+
if ( ! defined( 'ACF_PRO' ) ) {
100+
$this->markTestSkipped( 'ACF Pro is not active so this test will not run.' );
101+
}
102+
103+
// If WPGraphQL Content Blocks couldn't be activated, skip
104+
if ( ! defined( 'WPGRAPHQL_CONTENT_BLOCKS_DIR' ) ) {
105+
$this->markTestSkipped( 'This test is skipped when WPGraphQL Content Blocks is not active' );
106+
}
107+
108+
acf_register_block_type([
109+
'name' => 'block_with_category_field',
110+
'title' => 'Block with Category Field',
111+
'post_types' => [ 'post' ],
112+
]);
113+
114+
$field_key = $this->register_acf_field([
115+
'type' => 'taxonomy',
116+
'name' => 'Category Test',
117+
'show_in_graphql' => true,
118+
'graphql_field_name' => 'category',
119+
'required' => 1,
120+
'taxonomy' => 'category',
121+
'add_term' => 0,
122+
'save_terms' => 0,
123+
'load_terms' => 0,
124+
'return_format' => 'object',
125+
'field_type' => 'select',
126+
'multiple' => 0,
127+
'bidirectonal' => 0,
128+
'bidirectional_target' => [],
129+
], [
130+
'name' => 'Block Taxonomy Test',
131+
'graphql_field_name' => 'BlockTaxonomyTest',
132+
'location' => [
133+
[
134+
[
135+
'param' => 'block',
136+
'operator' => '==',
137+
'value' => 'acf/block-with-category-field',
138+
]
139+
]
140+
],
141+
'graphql_types' => [ 'AcfBlockWithCategoryField' ],
142+
]);
143+
144+
$query = '
145+
query GetType( $name: String! ) {
146+
__type( name: $name ) {
147+
fields {
148+
name
149+
}
150+
interfaces {
151+
name
152+
}
153+
}
154+
}
155+
';
156+
157+
$actual = $this->graphql( [
158+
'query' => $query,
159+
'variables' => [
160+
'name' => 'AcfBlockWithCategoryField',
161+
]
162+
]);
163+
164+
codecept_debug( [
165+
'$actual' => $actual,
166+
]);
167+
168+
// Assert that the AcfBlock is in the Schema
169+
// Assert the field group shows on the block as expected
170+
self::assertQuerySuccessful( $actual, [
171+
$this->expectedNode( '__type.fields', [
172+
'name' => Utils::format_field_name( 'blockTaxonomyTest' ),
173+
]),
174+
$this->expectedNode( '__type.interfaces', [
175+
'name' => 'AcfBlock',
176+
]),
177+
// Should implement the With${FieldGroup} Interface
178+
$this->expectedNode( '__type.interfaces', [
179+
'name' => 'WithAcfBlockTaxonomyTest',
180+
])
181+
]);
182+
183+
$category_id = self::factory()->category->create([
184+
'name' => uniqid( 'Test Category', true ),
185+
]);
186+
187+
codecept_debug( [
188+
'$field_key' => $field_key,
189+
'$category_id' => $category_id,
190+
]);
191+
192+
$content = '
193+
<!-- wp:acf/block-with-category-field {"name":"acf/block-with-category-field","data":{"' . $field_key . '":"' . $category_id . '"},"align":"","mode":"edit"} /-->
194+
';
195+
196+
$post_id = self::factory()->post->create([
197+
'post_type' => 'post',
198+
'post_status' => 'publish',
199+
'post_title' => 'Test Block With Taxonomy Field',
200+
'post_content' => $content,
201+
]);
202+
203+
$query = '
204+
query GetPostWithBlocks( $postId: ID! ){
205+
post(id:$postId idType:DATABASE_ID) {
206+
id
207+
title
208+
...Blocks
209+
}
210+
}
211+
212+
fragment Blocks on NodeWithEditorBlocks {
213+
editorBlocks {
214+
__typename
215+
...on AcfBlockWithCategoryField {
216+
blockTaxonomyTest {
217+
category {
218+
nodes {
219+
__typename
220+
databaseId
221+
}
222+
}
223+
}
224+
}
225+
}
226+
}
227+
';
228+
229+
$variables = [
230+
'postId' => $post_id,
231+
];
232+
233+
$actual = self::graphql([
234+
'query' => $query,
235+
'variables' => $variables,
236+
]);
237+
238+
codecept_debug( [
239+
'$actual' => $actual,
240+
]);
241+
242+
self::assertQuerySuccessful( $actual, [
243+
$this->expectedNode( 'post.editorBlocks', [
244+
// Expect a block with the __typename AcfBlockWithCategoryField
245+
$this->expectedField('__typename', 'AcfBlockWithCategoryField' ),
246+
$this->expectedNode( 'blockTaxonomyTest.category.nodes', [
247+
'__typename' => 'Category',
248+
'databaseId' => $category_id,
249+
], 0 ),
250+
], 0 ),
251+
]);
252+
253+
$category_2_id = self::factory()->category->create([
254+
'name' => uniqid( 'Test Category 2', true ),
255+
]);
256+
257+
$content = '
258+
<!-- wp:acf/block-with-category-field {"name":"acf/block-with-category-field","data":{"' . $field_key . '":["' . $category_id . '", "' . $category_2_id . '"]},"align":"","mode":"edit"} /-->
259+
';
260+
261+
$post_id = self::factory()->post->create([
262+
'post_type' => 'post',
263+
'post_status' => 'publish',
264+
'post_title' => 'Test Block With Taxonomy Field',
265+
'post_content' => $content,
266+
]);
267+
268+
$actual = self::graphql([
269+
'query' => $query,
270+
'variables' => $variables,
271+
]);
272+
273+
codecept_debug( [
274+
'$actual' => $actual,
275+
]);
276+
277+
self::assertQuerySuccessful( $actual, [
278+
$this->expectedNode( 'post.editorBlocks', [
279+
// Expect a block with the __typename AcfBlockWithCategoryField
280+
$this->expectedField('__typename', 'AcfBlockWithCategoryField' ),
281+
$this->expectedNode( 'blockTaxonomyTest.category.nodes', [
282+
'__typename' => 'Category',
283+
'databaseId' => $category_id,
284+
], 0 ),
285+
$this->expectedNode( 'blockTaxonomyTest.category.nodes', [
286+
'__typename' => 'Category',
287+
'databaseId' => $category_2_id,
288+
], 0 ),
289+
], 0 ),
290+
]);
291+
292+
wp_delete_post( $post_id );
293+
wp_delete_term( $category_id, 'category' );
294+
wp_delete_term( $category_2_id, 'category' );
295+
}
93296
}

0 commit comments

Comments
 (0)