'Test source', ); /** * Sets up shared fixtures. */ public static function wpSetUpBeforeClass() { register_block_type( 'test/block', array( 'attributes' => array( 'myAttribute' => array( 'type' => 'string', ), ), 'render_callback' => function ( $attributes ) { if ( isset( $attributes['myAttribute'] ) ) { return '

' . esc_html( $attributes['myAttribute'] ) . '

'; } }, ) ); } /** * Sets up the test fixture. */ public function set_up() { parent::set_up(); add_filter( 'block_bindings_supported_attributes_test/block', function ( $supported_attributes ) { $supported_attributes[] = 'myAttribute'; return $supported_attributes; } ); add_filter( 'block_bindings_supported_attributes_core/image', function ( $supported_attributes ) { $supported_attributes[] = 'caption'; return $supported_attributes; } ); } /** * Tear down after each test. */ public function tear_down() { foreach ( get_all_registered_block_bindings_sources() as $source_name => $source_properties ) { if ( str_starts_with( $source_name, 'test/' ) ) { unregister_block_bindings_source( $source_name ); } } parent::tear_down(); } /** * Tear down after class. */ public static function wpTearDownAfterClass() { unregister_block_type( 'test/block' ); } public function data_update_block_with_value_from_source() { return array( 'paragraph block' => array( 'content', <<

This should not appear

HTML , '

test source value

', ), 'button block' => array( 'text', <<
This should not appear
HTML , '
test source value
', ), 'test block' => array( 'myAttribute', <<

This should not appear

HTML , '

test source value

', ), ); } /** * Test if the block content is updated with the value returned by the source. * * @covers ::register_block_bindings_source * * @dataProvider data_update_block_with_value_from_source */ public function test_update_block_with_value_from_source( $bound_attribute, $block_content, $expected_result ) { $get_value_callback = function () { return 'test source value'; }; register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => $get_value_callback, ) ); $parsed_blocks = parse_blocks( $block_content ); $parsed_blocks[0]['attrs']['metadata'] = array( 'bindings' => array( $bound_attribute => array( 'source' => self::SOURCE_NAME, ), ), ); $block = new WP_Block( $parsed_blocks[0] ); $result = $block->render(); $this->assertSame( 'test source value', $block->attributes[ $bound_attribute ], "The '{$bound_attribute}' attribute should be updated with the value returned by the source." ); $this->assertSame( $expected_result, trim( $result ), 'The block content should be updated with the value returned by the source.' ); } public function data_different_get_value_callbacks() { return array( 'pass arguments to source' => array( function ( $source_args, $block_instance, $attribute_name ) { $value = $source_args['key']; return "The attribute name is '$attribute_name' and its binding has argument 'key' with value '$value'."; }, "

The attribute name is 'content' and its binding has argument 'key' with value 'test'.

", ), 'symbols and numbers should be rendered correctly' => array( function () { return '$12.50'; }, '

$12.50

', ), ); } /** * Test passing arguments to the source. * * @covers ::register_block_bindings_source * * @dataProvider data_different_get_value_callbacks */ public function test_different_get_value_callbacks( $get_value_callback, $expected ) { register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => $get_value_callback, ) ); $block_content = <<

This should not appear

HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); $result = $block->render(); $this->assertSame( $expected, trim( $result ), 'The block content should be updated with the value returned by the source.' ); } /** * Tests that unsafe HTML returned by the source is sanitized. * * @covers ::register_block_bindings_source */ public function test_unsafe_html_from_source_is_sanitized() { register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => function () { return ''; }, ) ); $block_content = <<

This should not appear

HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); $result = $block->render(); $processor = new WP_HTML_Tag_Processor( $result ); $this->assertFalse( $processor->next_tag( 'SCRIPT' ), 'The SCRIPT element should be removed.' ); } /** * Tests passing `uses_context` as argument to the source. * * @covers ::register_block_bindings_source */ public function test_passing_uses_context_to_source() { $get_value_callback = function ( $source_args, $block_instance ) { $this->assertArrayNotHasKey( 'forbiddenSourceContext', $block_instance->context, "Only context that was made available through the source's uses_context property should be accessible." ); $value = $block_instance->context['sourceContext']; return "Value: $value"; }; register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => $get_value_callback, 'uses_context' => array( 'sourceContext' ), ) ); $block_content = <<

This should not appear

HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0], array( 'sourceContext' => 'source context value', 'forbiddenSourceContext' => 'forbidden donut', ) ); $result = $block->render(); $this->assertSame( 'Value: source context value', $block->attributes['myAttribute'], "The 'myAttribute' should be updated with the value of the source context." ); $this->assertSame( '

Value: source context value

', trim( $result ), 'The block content should be updated with the value of the source context.' ); } /** * Tests if the block content is updated with the value returned by the source * for the Image block in the placeholder state. * * Furthermore tests if the caption attribute, for which support is added via the * `block_bindings_supported_attributes_core/image` filter, is correctly processed. * * @covers ::register_block_bindings_source */ public function test_update_block_with_value_from_source_image_placeholder() { $get_value_callback = function ( $source_args, $block_instance, $attribute_name ) { if ( 'url' === $attribute_name ) { return 'https://example.com/image.jpg'; } if ( 'caption' === $attribute_name ) { return 'Example Image'; } }; register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => $get_value_callback, ) ); $block_content = <<
HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); $result = $block->render(); $this->assertSame( 'https://example.com/image.jpg', $block->attributes['url'], "The 'url' attribute should be updated with the value returned by the source." ); $this->assertSame( 'Example Image', $block->attributes['caption'], "The 'caption' attribute should be updated with the value returned by the source." ); $this->assertSame( '
Example Image
', trim( $result ), 'The block content should be updated with the value returned by the source.' ); } /** * Tests if the Icon block's icon attribute is updated with the value returned by the source. * * @covers ::register_block_bindings_source */ public function test_update_icon_block_with_value_from_source() { register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => function () { return 'core/arrow-right'; }, ) ); $block_content = << HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); $block->render(); $this->assertSame( 'core/arrow-right', $block->attributes['icon'], "The 'icon' attribute should be updated with the value returned by the source." ); } /** * Tests if the `__default` attribute is replaced with real attributes for * pattern overrides. * * @covers WP_Block::process_block_bindings */ public function test_default_binding_for_pattern_overrides() { $block_content = <<

This should not appear

HTML; $expected_content = 'This is the content value'; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( 'Test' => array( 'myAttribute' => $expected_content ) ) ) ); $result = $block->render(); $this->assertSame( "

$expected_content

", trim( $result ), 'The `__default` attribute should be replaced with the real attribute prior to the callback.' ); $expected_bindings_metadata = array( 'myAttribute' => array( 'source' => 'core/pattern-overrides' ), ); $this->assertSame( $expected_bindings_metadata, $block->attributes['metadata']['bindings'], 'The __default binding should be updated with the individual binding attributes in the block metadata.' ); } /** * Tests that filter `block_bindings_source_value` is applied. */ public function test_filter_block_bindings_source_value() { register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => function () { return ''; }, ) ); $filter_value = function ( $value, $source_name, $source_args, $block_instance, $attribute_name ) { if ( self::SOURCE_NAME !== $source_name ) { return $value; } return "Filtered value: {$source_args['test_key']}. Block instance: {$block_instance->name}. Attribute name: {$attribute_name}."; }; add_filter( 'block_bindings_source_value', $filter_value, 10, 5 ); $block_content = <<

Default content

HTML; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); $result = $block->render(); remove_filter( 'block_bindings_source_value', $filter_value ); $this->assertSame( '

Filtered value: test_arg. Block instance: core/paragraph. Attribute name: content.

', trim( $result ), 'The block content should show the filtered value.' ); } /** * Renders a list item with a source value bound to its content attribute. * * @param mixed $source_value The source value. * @param string $block_content The block content. * @return string Rendered block content. */ private function render_list_item_with_source_value( $source_value, $block_content ) { register_block_bindings_source( self::SOURCE_NAME, array( 'label' => self::SOURCE_LABEL, 'get_value_callback' => function () use ( $source_value ) { return $source_value; }, ) ); $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0] ); return trim( $block->render() ); } /** * Tests if list item content is updated with a plain text value returned by the source. * * @covers WP_Block::render */ public function test_update_list_item_with_plain_text_value_from_source() { $block_content = <<
  • This should not appear
  • HTML; $result = $this->render_list_item_with_source_value( 'test source value', $block_content ); $this->assertSame( '
  • test source value
  • ', $result, 'The list item content should be replaced by the source text.' ); } /** * Tests if raw nested list markup is replaced when it is not a list inner block. * * @covers WP_Block::render */ public function test_update_list_item_with_raw_nested_list_markup_without_inner_block_replaces_markup() { $block_content = <<
  • Default content
  • HTML; $result = $this->render_list_item_with_source_value( 'Bound text', $block_content ); $this->assertSame( '
  • Bound text
  • ', $result, 'Raw nested list markup should be replaced with the rest of the list item content.' ); } /** * Tests if source image markup renders after post KSES sanitization. * * @covers WP_Block::render */ public function test_update_list_item_source_image_renders_after_kses() { $block_content = <<
  • This should not appear
  • HTML; $result = $this->render_list_item_with_source_value( 'Bound Inline image', $block_content ); $this->assertEqualHTML( '
  • Bound Inline image
  • ', $result, '', 'The source image should render in the list item content after sanitization.' ); } /** * Tests if unsafe source markup is sanitized from list item content. * * @covers WP_Block::render */ public function test_update_list_item_unsafe_source_markup_is_sanitized() { $block_content = <<
  • This should not appear
  • HTML; $result = $this->render_list_item_with_source_value( 'Bound Inline image', $block_content ); $this->assertStringNotContainsString( 'assertStringNotContainsString( 'onerror', $result, 'Event handler attributes should be stripped from list item source content.' ); $processor = new WP_HTML_Tag_Processor( $result ); $this->assertTrue( $processor->next_tag( 'IMG' ), 'Safe image markup should remain after sanitization.' ); $this->assertSame( 'https://example.com/inline-image.jpg', $processor->get_attribute( 'src' ), 'The image src should be preserved.' ); $this->assertSame( 'Inline image', $processor->get_attribute( 'alt' ), 'The image alt should be preserved.' ); $processor = new WP_HTML_Tag_Processor( $result ); $this->assertFalse( $processor->next_tag( 'SCRIPT' ), 'The SCRIPT element should be removed.' ); } /** * Tests if original inline images are replaced by bound list item text. * * @covers WP_Block::render */ public function test_update_list_item_original_inline_image_is_replaced_by_source_text() { $block_content = <<
  • Original Original inline image content
  • HTML; $result = $this->render_list_item_with_source_value( 'Bound text', $block_content ); $this->assertSame( '
  • Bound text
  • ', $result, 'The original inline image should be replaced with the rest of the list item content.' ); $this->assertStringNotContainsString( 'original-image.jpg', $result, 'The original inline image source should not remain after binding replacement.' ); $this->assertStringNotContainsString( 'Original inline image', $result, 'The original inline image alt text should not remain after binding replacement.' ); } /** * Tests if list item content updates preserve nested list inner blocks. * * @covers WP_Block::render */ public function test_update_list_item_with_nested_list_preserves_nested_list() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( 'Bound list item', $block_content ); $normalized = preg_replace( '/>\s+<', trim( $result ) ); $this->assertStringNotContainsString( 'Default content', $result, 'The original list item content should be replaced by the source value.' ); $this->assertMatchesRegularExpression( '#
  • Bound list item\s*
  • #', $normalized, 'The list item should render the source text and preserve nested list inner blocks.' ); } /** * Tests if raw list markup before a nested list inner block is replaced with the original content. * * @covers WP_Block::render */ public function test_update_list_item_with_raw_list_markup_before_nested_list_replaces_raw_markup() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( 'Bound list item', $block_content ); $normalized = preg_replace( '/>\s+<', trim( $result ) ); $this->assertStringNotContainsString( 'Raw list markup should be replaced', $result, 'Raw list markup before the nested block should be replaced by the source value.' ); $this->assertMatchesRegularExpression( '#
  • Bound list item\s*
  • #', $normalized, 'The list item should preserve only the delimiter-backed nested list inner block.' ); } /** * Tests if an empty source value clears the content but preserves nested list inner blocks. * * @covers WP_Block::render */ public function test_update_list_item_with_empty_value_preserves_nested_list() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( '', $block_content ); $normalized = preg_replace( '/>\s+<', trim( $result ) ); $this->assertStringNotContainsString( 'Default content', $result, 'An empty source value should clear the original list item content.' ); $this->assertMatchesRegularExpression( '#
  • \s*
  • #', $normalized, 'An empty source value should still preserve nested list inner blocks.' ); } /** * Tests if content updates preserve a nested ordered list inner block. * * @covers WP_Block::render */ public function test_update_list_item_with_nested_ordered_list_preserves_nested_list() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( 'Bound list item', $block_content ); $normalized = preg_replace( '/>\s+<', trim( $result ) ); $this->assertStringNotContainsString( 'Default content', $result, 'The original list item content should be replaced by the source value.' ); $this->assertMatchesRegularExpression( '#
  • Bound list item\s*
    1. Nested ordered child
  • #', $normalized, 'The list item should render the source text and preserve the nested ordered list.' ); } /** * Tests if content updates preserve multiple levels of nested list inner blocks. * * @covers WP_Block::render */ public function test_update_list_item_with_deeply_nested_list_preserves_all_levels() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( 'Bound list item', $block_content ); $this->assertStringNotContainsString( 'Level one default', $result, 'Only the bound list item content should be replaced.' ); $this->assertStringContainsString( 'Level two', $result, 'The first nested level should be preserved.' ); $this->assertStringContainsString( 'Level three', $result, 'The second nested level should be preserved.' ); } /** * Tests that a non-list inner block is preserved when the content is bound. * * The list item block only permits `core/list` children through the editor, * but block markup can be authored or pasted with other inner blocks. The * render path detects the first inner block by structure (not by tag name), * so any block type before which the rich text ends must be preserved. * * @covers WP_Block::render */ public function test_update_list_item_preserves_non_list_inner_block() { $block_content = << HTML; $result = $this->render_list_item_with_source_value( 'Bound list item', $block_content ); $this->assertStringNotContainsString( 'Default content', $result, 'The original list item content should be replaced by the source value.' ); $this->assertStringContainsString( 'Bound list item', $result, 'The source value should be rendered as the list item content.' ); $this->assertStringContainsString( 'wp-block-button', $result, 'The nested button inner block should be preserved.' ); $this->assertStringContainsString( 'Keep this button', $result, 'The nested button inner block content should be preserved.' ); } /** * Tests if pattern overrides update list item content without removing nested lists. * * @covers WP_Block::process_block_bindings */ public function test_default_binding_for_list_item_pattern_overrides() { $list_item_name = 'Editable List Item'; $block_content = << HTML; $expected_content = 'Pattern override'; $parsed_blocks = parse_blocks( $block_content ); $block = new WP_Block( $parsed_blocks[0], array( 'pattern/overrides' => array( $list_item_name => array( 'content' => $expected_content ), ), ) ); $result = $block->render(); $normalized = preg_replace( '/>\s+<', trim( $result ) ); $this->assertStringNotContainsString( 'Default content', $result, 'The original list item content should be replaced by the pattern override.' ); $this->assertStringContainsString( '
  • Nested child
  • ', $normalized, 'Nested list inner blocks should remain in the rendered output.' ); $this->assertMatchesRegularExpression( '#
  • Pattern override
  • #', $normalized, 'The list item should render the pattern override before its nested list.' ); $expected_bindings_metadata = array( 'content' => array( 'source' => 'core/pattern-overrides' ), ); $this->assertSame( $expected_bindings_metadata, $block->inner_blocks[0]->attributes['metadata']['bindings'], 'The __default binding should be updated with the list item content binding metadata.' ); } }