From cacc27e19893181147b91e8f8c3b0a8ea9835462 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 13:46:20 +0600 Subject: [PATCH 1/4] Tests: Add unit tests for the server-side rendering of the Query block. See #66530. --- tests/phpunit/tests/blocks/renderQuery.php | 380 +++++++++++++++++++++ 1 file changed, 380 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderQuery.php diff --git a/tests/phpunit/tests/blocks/renderQuery.php b/tests/phpunit/tests/blocks/renderQuery.php new file mode 100644 index 0000000000000..1dcafeae4cb81 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderQuery.php @@ -0,0 +1,380 @@ +original_script_modules = $wp_script_modules; + $this->original_interactivity = $wp_interactivity; + $wp_script_modules = null; + $wp_interactivity = new WP_Interactivity_API(); + + $this->was_registered = WP_Block_Type_Registry::get_instance()->is_registered( 'core/query' ); + + if ( ! $this->was_registered ) { + register_block_core_query(); + } + } + + public function tear_down() { + global $wp_script_modules, $wp_interactivity; + + if ( ! $this->was_registered ) { + unregister_block_type( 'core/query' ); + } + + wp_dequeue_style( 'wp-block-query' ); + + $wp_script_modules = $this->original_script_modules; + $wp_interactivity = $this->original_interactivity; + + parent::tear_down(); + } + + /** + * Creates a parsed block. + * + * @param string|null $name Name of the block. + * @param array $attrs Attributes of the block. + * @param array $inner_blocks Parsed inner blocks. + * @return array The parsed block. + */ + private function parsed_block( $name, $attrs = array(), $inner_blocks = array() ) { + return array( + 'blockName' => $name, + 'attrs' => $attrs, + 'innerBlocks' => $inner_blocks, + 'innerHTML' => '
' . str_repeat( ' ', count( $inner_blocks ) ) . '
', + 'innerContent' => array_merge( array( '
' ), array_fill( 0, count( $inner_blocks ), null ), array( '
' ) ), + ); + } + + /** + * Calls the render callback of the Query block. + * + * @param array $attributes Attributes of the block. + * @param string $content Content of the block. + * @return string The rendered content. + */ + private function render_callback( $attributes, $content = '

Posts

' ) { + $block = new WP_Block( $this->parsed_block( 'core/query', $attributes ) ); + + return render_block_core_query( $attributes, $content, $block ); + } + + /** + * Tests that the content is not changed when the block is not interactive. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + * + * @dataProvider data_attributes_that_are_not_interactive + * + * @param array $attributes Attributes of the block. + */ + public function test_should_not_change_the_content_when_the_block_is_not_interactive( $attributes ) { + $content = '

Posts

'; + + $this->assertSame( $content, $this->render_callback( $attributes, $content ) ); + $this->assertNotContains( '@wordpress/block-library/query/view', wp_script_modules()->get_queue(), 'The view script module should not be enqueued.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_attributes_that_are_not_interactive() { + return array( + 'no attributes' => array( array() ), + 'the pagination is disabled' => array( + array( + 'enhancedPagination' => false, + 'queryId' => 1, + ), + ), + 'no query ID' => array( array( 'enhancedPagination' => true ) ), + 'a query ID that is not scalar' => array( + array( + 'enhancedPagination' => true, + 'queryId' => array( 1 ), + ), + ), + 'a null query ID' => array( + array( + 'enhancedPagination' => true, + 'queryId' => null, + ), + ), + 'a string for the pagination' => array( + array( + 'enhancedPagination' => 'true', + 'queryId' => 1, + ), + ), + 'an integer for the pagination' => array( + array( + 'enhancedPagination' => 1, + 'queryId' => 1, + ), + ), + ); + } + + /** + * Tests the directives that are added to an interactive block. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + * + * @dataProvider data_query_ids + * + * @param int|string|float $query_id Query ID. + * @param string $region Expected router region. + */ + public function test_should_add_the_directives_when_the_block_is_interactive( $query_id, $region ) { + $output = $this->render_callback( + array( + 'enhancedPagination' => true, + 'queryId' => $query_id, + ) + ); + + $processor = new WP_HTML_Tag_Processor( $output ); + $this->assertTrue( $processor->next_tag(), 'The block should have an element.' ); + + $this->assertSame( 'core/query', $processor->get_attribute( 'data-wp-interactive' ) ); + $this->assertSame( $region, $processor->get_attribute( 'data-wp-router-region' ) ); + $this->assertSame( '{}', $processor->get_attribute( 'data-wp-context' ) ); + $this->assertSame( (string) $query_id, $processor->get_attribute( 'data-wp-key' ) ); + $this->assertStringContainsString( '

Posts

', $output, 'The content should be kept.' ); + $this->assertContains( '@wordpress/block-library/query/view', wp_script_modules()->get_queue(), 'The view script module should be enqueued.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_query_ids() { + return array( + 'an integer' => array( 3, 'query-3' ), + 'zero' => array( 0, 'query-0' ), + 'a string' => array( 'main', 'query-main' ), + ); + } + + /** + * Tests that the directives are only added to the first element. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_only_add_the_directives_to_the_first_element() { + $output = $this->render_callback( + array( + 'enhancedPagination' => true, + 'queryId' => 1, + ), + '
' + ); + + $this->assertSame( 1, substr_count( $output, 'data-wp-interactive' ) ); + + $processor = new WP_HTML_Tag_Processor( $output ); + $processor->next_tag(); + $this->assertSame( 'first', $processor->get_attribute( 'class' ), 'The first element should have the directives.' ); + $this->assertNotNull( $processor->get_attribute( 'data-wp-interactive' ) ); + $processor->next_tag(); + $this->assertNull( $processor->get_attribute( 'data-wp-interactive' ), 'The second element should not have the directives.' ); + } + + /** + * Tests that the script module is enqueued even when the content has no element. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_enqueue_the_script_module_for_content_without_an_element() { + $output = $this->render_callback( + array( + 'enhancedPagination' => true, + 'queryId' => 1, + ), + 'Text' + ); + + $this->assertSame( 'Text', $output ); + $this->assertContains( '@wordpress/block-library/query/view', wp_script_modules()->get_queue() ); + } + + /** + * Tests that the styles are removed from the block when it is not interactive. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_remove_the_styles_when_the_block_is_not_interactive() { + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + + $this->assertContains( 'wp-block-query', $block->block_type->style_handles, 'The styles should be in the block type at first.' ); + + render_block_core_query( array(), '
', $block ); + + $this->assertNotContains( 'wp-block-query', $block->block_type->style_handles ); + } + + /** + * Tests that the styles are added back to the block when it is interactive. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_add_the_styles_back_when_the_block_is_interactive() { + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + + render_block_core_query( array(), '
', $block ); + $this->assertNotContains( 'wp-block-query', $block->block_type->style_handles, 'The styles should be removed at first.' ); + + render_block_core_query( + array( + 'enhancedPagination' => true, + 'queryId' => 1, + ), + '
', + $block + ); + + $this->assertContains( 'wp-block-query', $block->block_type->style_handles ); + } + + /** + * Tests that the styles are kept when the block is interactive. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_keep_the_styles_when_the_block_is_interactive() { + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + + render_block_core_query( + array( + 'enhancedPagination' => true, + 'queryId' => 1, + ), + '
', + $block + ); + + $this->assertSame( array( 'wp-block-query' ), array_values( $block->block_type->style_handles ) ); + } + + /** + * Tests that the other styles are kept when the styles are added back. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_keep_the_other_styles_when_the_styles_are_added_back() { + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + $block->block_type->style_handles = array( 'other-style' ); + + render_block_core_query( + array( + 'enhancedPagination' => true, + 'queryId' => 1, + ), + '
', + $block + ); + + $this->assertSame( array( 'other-style', 'wp-block-query' ), array_values( $block->block_type->style_handles ) ); + } + + /** + * Tests that the styles are not changed when they are already enqueued. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_not_change_the_styles_when_they_are_enqueued() { + wp_enqueue_style( 'wp-block-query' ); + + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + + render_block_core_query( array(), '
', $block ); + + $this->assertContains( 'wp-block-query', $block->block_type->style_handles ); + } + + /** + * Tests that the other styles of the block are kept. + * + * @ticket 66530 + * + * @covers ::render_block_core_query + */ + public function test_should_keep_the_other_styles_of_the_block() { + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + $block->block_type->style_handles = array( 'wp-block-query', 'other-style' ); + + render_block_core_query( array(), '
', $block ); + + $this->assertSame( array( 'other-style' ), array_values( $block->block_type->style_handles ) ); + } + + /** + * Tests that the block is registered with the render callback. + * + * @ticket 66530 + * + * @covers ::register_block_core_query + */ + public function test_should_register_the_block_with_the_render_callback() { + $block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/query' ); + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_query', $block_type->render_callback ); + } +} From cdd6bbe1c37723d1dcc5cfe19eac4575d401e3d3 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 21:27:22 +0600 Subject: [PATCH 2/4] Tests: Restore the styles of the Query block type after each test. See #66530. --- tests/phpunit/tests/blocks/renderQuery.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/renderQuery.php b/tests/phpunit/tests/blocks/renderQuery.php index 1dcafeae4cb81..103162e178582 100644 --- a/tests/phpunit/tests/blocks/renderQuery.php +++ b/tests/phpunit/tests/blocks/renderQuery.php @@ -24,6 +24,13 @@ class Tests_Blocks_RenderQuery extends WP_UnitTestCase { */ private $original_interactivity; + /** + * Original style handles of the block type, when it was registered. + * + * @var string[] + */ + private $original_style_handles = array(); + /** * Whether the Query block was registered before the test. * @@ -43,6 +50,10 @@ public function set_up() { $this->was_registered = WP_Block_Type_Registry::get_instance()->is_registered( 'core/query' ); + if ( $this->was_registered ) { + $this->original_style_handles = WP_Block_Type_Registry::get_instance()->get_registered( 'core/query' )->style_handles; + } + if ( ! $this->was_registered ) { register_block_core_query(); } @@ -53,6 +64,9 @@ public function tear_down() { if ( ! $this->was_registered ) { unregister_block_type( 'core/query' ); + } else { + // The tests remove the styles from the block type that is registered. + WP_Block_Type_Registry::get_instance()->get_registered( 'core/query' )->style_handles = $this->original_style_handles; } wp_dequeue_style( 'wp-block-query' ); @@ -341,7 +355,8 @@ public function test_should_keep_the_other_styles_when_the_styles_are_added_back public function test_should_not_change_the_styles_when_they_are_enqueued() { wp_enqueue_style( 'wp-block-query' ); - $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + $block = new WP_Block( $this->parsed_block( 'core/query' ) ); + $block->block_type->style_handles = array( 'wp-block-query' ); render_block_core_query( array(), '
', $block ); From bff52cdae9d2e613e07edb545163278d3794d260 Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 21:41:46 +0600 Subject: [PATCH 3/4] Tests: Give the Query block type its styles in the tests, as the build does. See #66530. --- tests/phpunit/tests/blocks/renderQuery.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderQuery.php b/tests/phpunit/tests/blocks/renderQuery.php index 103162e178582..112c21af061b4 100644 --- a/tests/phpunit/tests/blocks/renderQuery.php +++ b/tests/phpunit/tests/blocks/renderQuery.php @@ -57,6 +57,9 @@ public function set_up() { if ( ! $this->was_registered ) { register_block_core_query(); } + + // The block has its styles only when the assets are built, so the tests set them. + WP_Block_Type_Registry::get_instance()->get_registered( 'core/query' )->style_handles = array( 'wp-block-query' ); } public function tear_down() { From 6565c8f2ffe38c18a6aa9455b5f5f197913968ea Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Fri, 6 Nov 2026 21:59:49 +0600 Subject: [PATCH 4/4] Tests: Register the style of the Query block before enqueuing it in the test. See #66530. --- tests/phpunit/tests/blocks/renderQuery.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/blocks/renderQuery.php b/tests/phpunit/tests/blocks/renderQuery.php index 112c21af061b4..6f04aa8168334 100644 --- a/tests/phpunit/tests/blocks/renderQuery.php +++ b/tests/phpunit/tests/blocks/renderQuery.php @@ -356,6 +356,8 @@ public function test_should_keep_the_other_styles_when_the_styles_are_added_back * @covers ::render_block_core_query */ public function test_should_not_change_the_styles_when_they_are_enqueued() { + // The style is registered by the build of the block, so it is registered here. + wp_register_style( 'wp-block-query', false ); wp_enqueue_style( 'wp-block-query' ); $block = new WP_Block( $this->parsed_block( 'core/query' ) );