Skip to content

Commit ccf51d2

Browse files
committed
- update variable names in Taxonomy.php to be more clear
- Add test case for querying taxonomy field on ACF Block when only 1 category is stored
1 parent 968b0aa commit ccf51d2

File tree

2 files changed

+195
-4
lines changed

2 files changed

+195
-4
lines changed

src/FieldType/Taxonomy.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
class Taxonomy {
1010

1111
/**
12-
* Register support for the "taxonomy" ACF field type
1312
*/
1413
public static function register_field_type(): void {
1514
register_graphql_acf_field_type(
@@ -26,16 +25,15 @@ public static function register_field_type(): void {
2625
return null;
2726
}
2827

29-
$values = [];
3028
if ( ! is_array( $value ) ) {
31-
$values[] = $value;
29+
$value[] = $value;
3230
}
3331

3432
$value = array_map(
3533
static function ( $id ) {
3634
return absint( $id );
3735
},
38-
$values
36+
$value
3937
);
4038

4139
$resolver = new TermObjectConnectionResolver( $root, $args, $context, $info );

tests/wpunit/FieldTypes/TaxonomyFieldTest.php

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

0 commit comments

Comments
 (0)