@@ -192,4 +192,70 @@ public function test_default_context_is_filterable() {
192192
193193 $ this ->assertSame ( array ( 'example ' => 'ok ' ), $ provided_context [0 ] );
194194 }
195+
196+ /**
197+ * Tests the behavior of the 'render_block_context' filter based on the location of the filtered block.
198+ *
199+ * @ticket 62046
200+ */
201+ public function test_render_block_context_inner_blocks () {
202+ $ provided_context = array ();
203+
204+ register_block_type (
205+ 'tests/context-provider ' ,
206+ array (
207+ 'provides_context ' => array ( 'example ' ),
208+ ),
209+ );
210+
211+ register_block_type (
212+ 'tests/context-consumer ' ,
213+ array (
214+ 'uses_context ' => array ( 'example ' ),
215+ 'render_callback ' => static function ( $ attributes , $ content , $ block ) use ( &$ provided_context ) {
216+ $ provided_context = $ block ->context ;
217+
218+ return '' ;
219+ },
220+ )
221+ );
222+
223+ // Filter the context provided by the test block.
224+ add_filter (
225+ 'render_block_context ' ,
226+ function ( $ context , $ parsed_block ) {
227+ if ( isset ( $ parsed_block ['blockName ' ] ) && 'tests/context-provider ' === $ parsed_block ['blockName ' ] ) {
228+ $ context ['example ' ] = 'ok ' ;
229+ }
230+
231+ return $ context ;
232+ },
233+ 10 ,
234+ 2
235+ );
236+
237+ // Test inner block context when the provider block is a top-level block.
238+ do_blocks (
239+ <<<HTML
240+ <!-- wp:tests/context-provider -->
241+ <!-- wp:tests/context-consumer /-->
242+ <!-- /wp:tests/context-provider -->
243+ HTML
244+ );
245+ $ this ->assertTrue ( isset ( $ provided_context ['example ' ] ), 'Test block is top-level block: Context should include "example" ' );
246+ $ this ->assertSame ( 'ok ' , $ provided_context ['example ' ], 'Test block is top-level block: "example" in context should be "ok" ' );
247+
248+ // Test inner block context when the provider block is an inner block.
249+ do_blocks (
250+ <<<HTML
251+ <!-- wp:group {"layout":{"type":"constrained"}} -->
252+ <!-- wp:tests/context-provider -->
253+ <!-- wp:tests/context-consumer /-->
254+ <!-- /wp:tests/context-provider -->
255+ <!-- /wp:group -->
256+ HTML
257+ );
258+ $ this ->assertTrue ( isset ( $ provided_context ['example ' ] ), 'Test block is inner block: Block context should include "example" ' );
259+ $ this ->assertSame ( 'ok ' , $ provided_context ['example ' ], 'Test block is inner block: "example" in context should be "ok" ' );
260+ }
195261}
0 commit comments