Skip to content

Commit

Permalink
Add basic unit test for filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Mar 6, 2024
1 parent 98cdebd commit fb29d99
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tests/phpunit/tests/block-template-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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();

Check warning on line 438 in tests/phpunit/tests/block-template-utils.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned with surrounding assignments; expected 15 spaces but found 1 space
$changes->post_name = 'my_template';
$changes->post_type = 'wp_template';
$changes->post_status = 'publish';
$changes->post_content = '<!-- wp:tests/anchor-block -->Hello<!-- /wp:tests/anchor-block -->';
$changes->tax_input = array(
'wp_theme' => self::TEST_THEME,
);

$post = inject_ignored_hooked_blocks_metadata_attributes( $changes, $request );
$this->assertSame(
'<!-- wp:tests/anchor-block {"metadata":{"ignoredHookedBlocks":["tests/hooked-block"]}} -->Hello<!-- /wp:tests/anchor-block -->',
$post->post_content,
'The hooked block was not injected into the anchor block\'s ignoredHookedBlocks metadata.'
);
}
}

0 comments on commit fb29d99

Please sign in to comment.