|
4 | 4 |
|
5 | 5 | use WPGraphQL\ContentBlocks\Data\ContentBlocksResolver; |
6 | 6 | use WPGraphQL\Model\Post; |
| 7 | +use WPGraphQL\Model\Term; |
7 | 8 |
|
8 | 9 | final class ContentBlocksResolverTest extends PluginTestCase { |
9 | 10 | public $instance; |
@@ -350,6 +351,82 @@ protected function assertEqualBlocks( $expected, $actual, $message = '' ) { |
350 | 351 | } |
351 | 352 | } |
352 | 353 |
|
| 354 | + /** |
| 355 | + * Tests that post content inner blocks are resolved correctly. |
| 356 | + */ |
| 357 | + public function test_resolve_content_blocks_resolves_post_content_inner_blocks() { |
| 358 | + // The post content holds what will be the inner blocks. |
| 359 | + $post_content = ' |
| 360 | + <!-- wp:columns --> |
| 361 | + <div class="wp-block-columns"> |
| 362 | + <!-- wp:column --> |
| 363 | + <div class="wp-block-column"> |
| 364 | + <!-- wp:paragraph --> |
| 365 | + <p>Column 1 Paragraph</p> |
| 366 | + <!-- /wp:paragraph --> |
| 367 | + </div> |
| 368 | + <!-- /wp:column --> |
| 369 | + <!-- wp:column --> |
| 370 | + <div class="wp-block-column"> |
| 371 | + <!-- wp:heading --> |
| 372 | + <h2>Column 2 Heading</h2> |
| 373 | + <!-- /wp:heading --> |
| 374 | + </div> |
| 375 | + <!-- /wp:column --> |
| 376 | + </div> |
| 377 | + <!-- /wp:columns --> |
| 378 | + '; |
| 379 | + wp_update_post( |
| 380 | + [ |
| 381 | + 'ID' => $this->post_id, |
| 382 | + 'post_content' => $post_content, |
| 383 | + ] |
| 384 | + ); |
| 385 | + |
| 386 | + // The term holds the PostContent block. |
| 387 | + $term_id = $this->factory()->term->create( |
| 388 | + [ |
| 389 | + 'taxonomy' => 'category', |
| 390 | + 'name' => 'Post Content Term', |
| 391 | + 'description' => '<!-- wp:post-content /-->', |
| 392 | + ] |
| 393 | + ); |
| 394 | + $model_with_blocks = new Term( get_term( $term_id ) ); |
| 395 | + |
| 396 | + add_filter( 'wpgraphql_content_blocks_resolver_content' , function( $content, $node ) { |
| 397 | + if ( $node instanceof Term ) { |
| 398 | + // The PostContent Block resolves based on the global. |
| 399 | + global $post; |
| 400 | + $post = get_post( $this->post_id ); |
| 401 | + |
| 402 | + return $node->description; |
| 403 | + } |
| 404 | + |
| 405 | + return $content; |
| 406 | + }, 10, 2 ); |
| 407 | + |
| 408 | + // Resolve blocks as nested. |
| 409 | + $resolved_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => false ] ); |
| 410 | + |
| 411 | + // Assertions for nested blocks. |
| 412 | + $this->assertCount( 1, $resolved_blocks, 'There should be only one top-level block (post-content).' ); |
| 413 | + $this->assertEquals( 'core/post-content', $resolved_blocks[0]['blockName'] ); |
| 414 | + // $this->assertCount( 1, $resolved_blocks[0]['innerBlocks'], 'There should be one top-level block in post content.' ); |
| 415 | + $this->assertEquals( 'core/columns', $resolved_blocks[0]['innerBlocks'][0]['blockName'] ); |
| 416 | + |
| 417 | + // Resolve blocks as flat |
| 418 | + $resolved_flat_blocks = $this->instance->resolve_content_blocks( $model_with_blocks, [ 'flat' => true ] ); |
| 419 | + |
| 420 | + // Assertions for flat blocks |
| 421 | + $this->assertCount( 6, $resolved_flat_blocks, 'There should be five blocks when flattened.' ); |
| 422 | + $this->assertEquals( 'core/post-content', $resolved_flat_blocks[0]['blockName'] ); |
| 423 | + $this->assertEquals( 'core/columns', $resolved_flat_blocks[1]['blockName'] ); |
| 424 | + $this->assertEquals( 'core/column', $resolved_flat_blocks[2]['blockName'] ); |
| 425 | + $this->assertEquals( 'core/paragraph', $resolved_flat_blocks[3]['blockName'] ); |
| 426 | + $this->assertEquals( 'core/column', $resolved_flat_blocks[4]['blockName'] ); |
| 427 | + $this->assertEquals( 'core/heading', $resolved_flat_blocks[5]['blockName'] ); |
| 428 | + } |
| 429 | + |
353 | 430 | /** |
354 | 431 | * Tests that pattern inner blocks are resolved correctly. |
355 | 432 | */ |
|
0 commit comments