Skip to content

Commit 898ab5a

Browse files
committed
Test the render_block_context filter's effect on calculated context
1 parent d558031 commit 898ab5a

File tree

1 file changed

+51
-4
lines changed

1 file changed

+51
-4
lines changed

tests/phpunit/tests/blocks/renderBlock.php

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ function ( $context, $parsed_block ) {
235235
);
236236

237237
// Test inner block context when the provider block is a top-level block.
238-
do_blocks(
239-
<<<HTML
238+
do_blocks( <<<HTML
240239
<!-- wp:tests/context-provider -->
241240
<!-- wp:tests/context-consumer /-->
242241
<!-- /wp:tests/context-provider -->
@@ -246,8 +245,7 @@ function ( $context, $parsed_block ) {
246245
$this->assertSame( 'ok', $provided_context['example'], 'Test block is top-level block: "example" in context should be "ok"' );
247246

248247
// Test inner block context when the provider block is an inner block.
249-
do_blocks(
250-
<<<HTML
248+
do_blocks( <<<HTML
251249
<!-- wp:group {"layout":{"type":"constrained"}} -->
252250
<!-- wp:tests/context-provider -->
253251
<!-- wp:tests/context-consumer /-->
@@ -258,4 +256,53 @@ function ( $context, $parsed_block ) {
258256
$this->assertTrue( isset( $provided_context['example'] ), 'Test block is inner block: Block context should include "example"' );
259257
$this->assertSame( 'ok', $provided_context['example'], 'Test block is inner block: "example" in context should be "ok"' );
260258
}
259+
260+
/**
261+
* Tests that the 'render_block_context' filter provides available context, not actual context.
262+
*
263+
* @ticket 62046
264+
*/
265+
public function test_render_block_context_allowed_context() {
266+
$provided_context = array();
267+
268+
register_block_type(
269+
'tests/context-consumer',
270+
array(
271+
'uses_context' => array( 'example' ),
272+
'render_callback' => static function ( $attributes, $content, $block ) use ( &$provided_context ) {
273+
$provided_context = $block->context;
274+
275+
return '';
276+
},
277+
),
278+
);
279+
280+
// Filter the context provided to the test block.
281+
add_filter(
282+
'render_block_context',
283+
function ( $context, $parsed_block ) {
284+
if ( isset( $parsed_block['blockName'] ) && 'tests/context-consumer' === $parsed_block['blockName'] ) {
285+
$context['invalid'] = 'ok';
286+
}
287+
288+
return $context;
289+
},
290+
10,
291+
2
292+
);
293+
294+
do_blocks( <<<HTML
295+
<!-- wp:tests/context-consumer /-->
296+
HTML
297+
);
298+
$this->assertFalse( isset( $provided_context['invalid'] ), 'Test block is top-level block: Context should include not include unsupported properties"' );
299+
300+
do_blocks( <<<HTML
301+
<!-- wp:group {"layout":{"type":"constrained"}} -->
302+
<!-- wp:tests/context-consumer /-->
303+
<!-- /wp:group -->
304+
HTML
305+
);
306+
$this->assertFalse( isset( $provided_context['invalid'] ), 'Test block is inner block: Context should include not include unsupported properties' );
307+
}
261308
}

0 commit comments

Comments
 (0)