Skip to content

Commit

Permalink
Add additional test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
chriszarate committed Aug 23, 2024
1 parent ccbc687 commit 12f50d9
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tests/parser/test-block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,52 @@ public function test_single_paragraph_block_binding() {
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], false, wp_json_encode( $blocks['blocks'] ) );
}

/* Image block with multiple bindings */

public function test_image_block_with_multiple_bindings() {
$this->register_block_bindings_source(
'test/block-binding-image-url',
[
'label' => 'Test image block binding for URL',
'get_value_callback' => static function ( array $args, WP_Block $block ) {
return sprintf( 'https://example.com/image.webp?block=%s&foo=%s', $block->name, $args['foo'] );
},
]
);

$this->register_block_bindings_source(
'test/block-binding-image-alt',
[
'label' => 'Test image block binding for alt text',
'get_value_callback' => static function ( array $args, WP_Block $block ) {
return sprintf( 'Block binding for %s with arg foo=%s', $block->name, $args['foo'] );
},
]
);

$html = '
<!-- wp:core/image {"metadata":{"bindings":{"alt":{"source":"test/block-binding-image-alt","args":{"foo":"bar"}},"url":{"source":"test/block-binding-image-url","args":{"foo":"bar"}}}}} -->
<img src="https://example.com/fallback.jpg" alt="Fallback alt text" />
<!-- /wp:core/image -->
';

$expected_blocks = [
[
'name' => 'core/image',
'attributes' => [
'alt' => 'Block binding for core/image with arg foo=bar',
'url' => 'https://example.com/image.webp?block=core/image&foo=bar',
],
],
];

$content_parser = new ContentParser( $this->get_block_registry() );
$blocks = $content_parser->parse( $html );

$this->assertArrayHasKey( 'blocks', $blocks, sprintf( 'Unexpected parser output: %s', wp_json_encode( $blocks ) ) );
$this->assertArraySubset( $expected_blocks, $blocks['blocks'], false, wp_json_encode( $blocks['blocks'] ) );
}

/* Nested paragraph block binding with context */

public function test_nested_paragraph_block_binding_with_custom_context() {
Expand Down

0 comments on commit 12f50d9

Please sign in to comment.