|
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,205 @@ public function get_expected_block_fragment_response() { |
90 | 92 | ] |
91 | 93 | ]; |
92 | 94 | } |
| 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 | + } |
93 | 296 | } |
0 commit comments