Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add block binding support #74

Merged
merged 20 commits into from
Aug 30, 2024
Merged
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add additional test cases
  • Loading branch information
chriszarate committed Aug 23, 2024
commit 12f50d9f724b6c3e276fcbf1dae0d3ef145bba06
46 changes: 46 additions & 0 deletions tests/parser/test-block-bindings.php
Original file line number Diff line number Diff line change
@@ -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() {