-
Notifications
You must be signed in to change notification settings - Fork 3.2k
render_block_context test for #62046
#7344
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
Changes from all commits
d558031
898ab5a
6a37f03
8b67ab2
aec7a6e
3800984
90c63a2
fcafff2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -192,4 +192,123 @@ 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 --> | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, it runs the same filter in a different place:
|
||||||
| <!-- 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"' ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
| $this->assertSame( 'ok', $provided_context['example'], 'Test block is inner block: "example" in context should be "ok"' ); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Tests that the 'render_block_context' filter arbitrary 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 ''; | ||||||
| }, | ||||||
| ), | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Old PHP failed fix |
||||||
| ); | ||||||
|
|
||||||
| // 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['arbitrary'] = 'ok'; | ||||||
| } | ||||||
|
|
||||||
| return $context; | ||||||
| }, | ||||||
| 10, | ||||||
| 2 | ||||||
| ); | ||||||
|
|
||||||
| do_blocks( | ||||||
| <<<HTML | ||||||
| <!-- wp:tests/context-consumer /--> | ||||||
| HTML | ||||||
| ); | ||||||
| $this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is top-level block: Block context should include "arbitrary"' ); | ||||||
| $this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is top-level block: "arbitrary" in context should be "ok"' ); | ||||||
|
|
||||||
| do_blocks( | ||||||
| <<<HTML | ||||||
| <!-- wp:group {"layout":{"type":"constrained"}} --> | ||||||
| <!-- wp:tests/context-consumer /--> | ||||||
| <!-- /wp:group --> | ||||||
| HTML | ||||||
| ); | ||||||
| $this->assertTrue( isset( $provided_context['arbitrary'] ), 'Test block is inner block: Block context should include "arbitrary"' ); | ||||||
| $this->assertSame( 'ok', $provided_context['arbitrary'], 'Test block is inner block: "arbitrary" in context should be "ok"' ); | ||||||
| } | ||||||
| } | ||||||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the top level, it runs the filter inside
render_block:wordpress-develop/src/wp-includes/blocks.php
Line 2110 in a78540b