@@ -349,4 +349,58 @@ protected function assertEqualBlocks( $expected, $actual, $message = '' ) {
349349 $ this ->assertEqualBlocks ( $ expected_inner_block , $ actual_inner_blocks [ $ index ], $ message );
350350 }
351351 }
352+
353+ /**
354+ * Tests that template part inner blocks are resolved correctly.
355+ */
356+ public function test_resolve_content_blocks_resolves_template_part_inner_blocks () {
357+ // Skip if template part functionality is not supported
358+ if ( ! function_exists ( 'get_block_templates ' ) ) {
359+ $ this ->markTestSkipped ( 'Template part functionality not supported in this WordPress version. ' );
360+ }
361+
362+ // Mock the get_block_templates function to control the output.
363+ $ mock_template = (object ) [
364+ 'content ' => '<!-- wp:paragraph /--><!-- wp:heading /--> ' ,
365+ ];
366+
367+ add_filter (
368+ 'get_block_templates ' ,
369+ function () use ( $ mock_template ) {
370+ return [ $ mock_template ];
371+ }
372+ );
373+
374+ // Update post content to include template part block
375+ $ post_content = '
376+ <!-- wp:template-part {"slug":"test-template-part"} /-->
377+ ' ;
378+
379+ wp_update_post (
380+ [
381+ 'ID ' => $ this ->post_id ,
382+ 'post_content ' => $ post_content ,
383+ ]
384+ );
385+
386+ $ post_model = new Post ( get_post ( $ this ->post_id ) );
387+
388+ // Resolve blocks as nested
389+ $ resolved_blocks = $ this ->instance ->resolve_content_blocks ( $ post_model , [ 'flat ' => false ] );
390+
391+ // Assertions
392+ $ this ->assertCount ( 1 , $ resolved_blocks , 'There should be only one top-level block (template-part). ' );
393+ $ this ->assertEquals ( 'core/template-part ' , $ resolved_blocks [0 ]['blockName ' ] );
394+ $ this ->assertCount ( 2 , $ resolved_blocks [0 ]['innerBlocks ' ], 'There should be two inner blocks in the template part. ' );
395+ $ this ->assertEquals ( 'core/paragraph ' , $ resolved_blocks [0 ]['innerBlocks ' ][0 ]['blockName ' ] );
396+ $ this ->assertEquals ( 'core/heading ' , $ resolved_blocks [0 ]['innerBlocks ' ][1 ]['blockName ' ] );
397+
398+ // Resolve blocks as flat
399+ $ resolved_flat_blocks = $ this ->instance ->resolve_content_blocks ( $ post_model , [ 'flat ' => true ] );
400+
401+ $ this ->assertCount ( 3 , $ resolved_flat_blocks , 'There should be three blocks when flattened. ' );
402+ $ this ->assertEquals ( 'core/template-part ' , $ resolved_flat_blocks [0 ]['blockName ' ] );
403+ $ this ->assertEquals ( 'core/paragraph ' , $ resolved_flat_blocks [1 ]['blockName ' ] );
404+ $ this ->assertEquals ( 'core/heading ' , $ resolved_flat_blocks [2 ]['blockName ' ] );
405+ }
352406}
0 commit comments