|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use WPGraphQL\Utils\Utils; |
| 4 | + |
3 | 5 | class TaxonomyFieldTest extends \Tests\WPGraphQL\Acf\WPUnit\AcfFieldTestCase { |
4 | 6 |
|
5 | 7 | /** |
@@ -90,4 +92,195 @@ public function get_expected_block_fragment_response() { |
90 | 92 | ] |
91 | 93 | ]; |
92 | 94 | } |
| 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 | + } |
93 | 286 | } |
0 commit comments