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

Testing: render_block_context another approach #7522

Draft
wants to merge 4 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 21 additions & 9 deletions src/wp-includes/class-wp-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public function __construct( $block, $available_context = array(), $registry = n
$this->block_type = $registry->get_registered( $this->name );

$this->available_context = $available_context;
$this->update_available_context( $block, $available_context );

if ( ! empty( $block['innerHTML'] ) ) {
$this->inner_html = $block['innerHTML'];
}

if ( ! empty( $block['innerContent'] ) ) {
$this->inner_content = $block['innerContent'];
}
}

public function update_available_context( $block, $available_context ) {
$this->context = array();
if ( null === $this->available_context ) {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$this->available_context = $available_context;
} else {
$this->available_context = array_merge( $this->available_context, $available_context );
}

if ( ! empty( $this->block_type->uses_context ) ) {
foreach ( $this->block_type->uses_context as $context_name ) {
Expand All @@ -159,15 +177,7 @@ public function __construct( $block, $available_context = array(), $registry = n
}
}

$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $registry );
}

if ( ! empty( $block['innerHTML'] ) ) {
$this->inner_html = $block['innerHTML'];
}

if ( ! empty( $block['innerContent'] ) ) {
$this->inner_content = $block['innerContent'];
$this->inner_blocks = new WP_Block_List( $block['innerBlocks'], $child_context, $this->registry );
}
}

Expand Down Expand Up @@ -514,6 +524,8 @@ public function render( $options = array() ) {
/** This filter is documented in wp-includes/blocks.php */
$inner_block->context = apply_filters( 'render_block_context', $inner_block->context, $inner_block->parsed_block, $parent_block );

$inner_block->update_available_context( $inner_block->parsed_block, $inner_block->context );

$block_content .= $inner_block->render();
}

Expand Down
117 changes: 117 additions & 0 deletions tests/phpunit/tests/blocks/renderBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,121 @@ public function test_default_context_is_filterable() {

$this->assertSame( array( 'example' => 'ok' ), $provided_context[0] );
}

/**
* Tests the behavior of the 'render_block_context' filter based on the location of the filtered block.
*
* @ticket 62046
*/
public function test_render_block_context_inner_blocks() {
$provided_context = array();

register_block_type(
'tests/context-provider',
array(
'provides_context' => array( 'example' ),
)
);

register_block_type(
'tests/context-consumer',
array(
'uses_context' => array( 'example' ),
'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context = $block->context;

return '';
},
)
);

// Filter the context provided by the test block.
add_filter(
'render_block_context',
function ( $context, $parsed_block ) {
if ( isset( $parsed_block['blockName'] ) && 'tests/context-provider' === $parsed_block['blockName'] ) {
$context['example'] = 'ok';
}

return $context;
},
10,
2
);

// Test inner block context when the provider block is a top-level block.
do_blocks(
<<<HTML
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
HTML
);
$this->assertTrue( isset( $provided_context['example'] ), 'Test block is top-level block: Context should include "example"' );
$this->assertSame( 'ok', $provided_context['example'], 'Test block is top-level block: "example" in context should be "ok"' );

// Test inner block context when the provider block is an inner block.
do_blocks(
<<<HTML
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-provider -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:tests/context-provider -->
<!-- /wp:group -->
HTML
);
$this->assertTrue( isset( $provided_context['example'] ), 'Test block is inner block: Block context should include "example"' );
$this->assertSame( 'ok', $provided_context['example'], 'Test block is inner block: "example" in context should be "ok"' );
}

/**
* Tests that the 'render_block_context' filter provides available context, not actual context.
*
* @ticket 62046
*/
public function test_render_block_context_allowed_context() {
$provided_context = array();

register_block_type(
'tests/context-consumer',
array(
'uses_context' => array( 'example' ),
'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
$provided_context = $block->context;

return '';
},
)
);

// Filter the context provided to the test block.
add_filter(
'render_block_context',
function ( $context, $parsed_block ) {
if ( isset( $parsed_block['blockName'] ) && 'tests/context-consumer' === $parsed_block['blockName'] ) {
$context['invalid'] = 'ok';
}

return $context;
},
10,
2
);

do_blocks(
<<<HTML
<!-- wp:tests/context-consumer /-->
HTML
);
$this->assertFalse( isset( $provided_context['invalid'] ), 'Test block is top-level block: Context should not include unsupported properties"' );

do_blocks(
<<<HTML
<!-- wp:group {"layout":{"type":"constrained"}} -->
<!-- wp:tests/context-consumer /-->
<!-- /wp:group -->
HTML
);
$this->assertFalse( isset( $provided_context['invalid'] ), 'Test block is inner block: Context should not include unsupported properties' );
}
}
Loading