From fb29d99c9b9c3ac764d50ac9d049bedb0b0a44b1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 6 Mar 2024 13:01:02 +0100 Subject: [PATCH] Add basic unit test for filter --- tests/phpunit/tests/block-template-utils.php | 61 ++++++++++++++++++++ 1 file changed, 61 insertions(+) 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.' + ); + } }