diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 5aa0b65929c47..d5f6a6b19ecff 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -86,6 +86,28 @@ public function set_up() { switch_theme( self::TEST_THEME ); } + /** + * Tear down after each test. + * + * @since 6.5.0 + */ + public function tear_down() { + global $wp_current_filter; + + if ( + 'rest_pre_insert_wp_template' === current_filter() || + 'rest_pre_insert_wp_template_part' === current_filter() + ) { + array_pop( $wp_current_filter ); + } + + if ( WP_Block_Type_Registry::get_instance()->is_registered( 'tests/block' ) ) { + unregister_block_type( 'tests/hooked-block' ); + } + + parent::tear_down(); + } + /** * @ticket 59338 * @@ -390,4 +412,43 @@ public function test_wp_generate_block_templates_export_file() { } $this->assertTrue( $has_html_files, 'contains at least one html file' ); } + + /** + * @ticket 60671 + * + * @covers inject_ignored_hooked_blocks_metadata_attributes + */ + public function test_inject_ignored_hooked_blocks_metadata_attributes_into_template() { + global $wp_current_filter; + // Mock currently set filter. + $wp_current_filter[] = 'rest_pre_insert_wp_template'; + + register_block_type( + 'tests/hooked-block', + array( + 'block_hooks' => array( + 'tests/anchor-block' => 'after', + ), + ) + ); + + $id = self::TEST_THEME . '//' . 'my_template'; + $request = new WP_REST_Request( 'POST', '/wp/v2/templates/' . $id ); + + $changes = new stdClass(); + $changes->post_name = 'my_template'; + $changes->post_type = 'wp_template'; + $changes->post_status = 'publish'; + $changes->post_content = 'Hello'; + $changes->tax_input = array( + 'wp_theme' => self::TEST_THEME, + ); + + $post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request ); + $this->assertSame( + 'Hello', + $post->post_content, + 'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.' + ); + } }