From 07883abe4508d1d0f151f561e2aadf625149771f Mon Sep 17 00:00:00 2001 From: Md Abul Bashar Date: Sat, 7 Nov 2026 01:24:19 +0600 Subject: [PATCH] Tests: Add unit tests for the server-side rendering of the Navigation Overlay Close block. See #66578. --- .../blocks/renderNavigationOverlayClose.php | 208 ++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 tests/phpunit/tests/blocks/renderNavigationOverlayClose.php diff --git a/tests/phpunit/tests/blocks/renderNavigationOverlayClose.php b/tests/phpunit/tests/blocks/renderNavigationOverlayClose.php new file mode 100644 index 0000000000000..94a32de138158 --- /dev/null +++ b/tests/phpunit/tests/blocks/renderNavigationOverlayClose.php @@ -0,0 +1,208 @@ +original_block_to_render = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'blockName' => 'core/navigation-overlay-close', + 'attrs' => array(), + ); + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_to_render; + + parent::tear_down(); + } + + /** + * Returns the attributes of the button of the output. + * + * @param string $output The output. + * @return array|null The attributes, or null when there is no button. + */ + private function get_button( $output ) { + $processor = new WP_HTML_Tag_Processor( $output ); + + if ( ! $processor->next_tag( 'button' ) ) { + return null; + } + + $attributes = array(); + + foreach ( $processor->get_attribute_names_with_prefix( '' ) as $name ) { + $attributes[ $name ] = $processor->get_attribute( $name ); + } + + return $attributes; + } + + /** + * Tests the button for the default display mode. + * + * @ticket 66578 + */ + public function test_should_render_the_icon_by_default() { + $output = render_block_core_navigation_overlay_close( array() ); + $button = $this->get_button( $output ); + + $this->assertSame( 'wp-block-navigation-overlay-close', $button['class'] ); + $this->assertSame( 'button', $button['type'] ); + $this->assertStringContainsString( 'assertStringNotContainsString( 'wp-block-navigation-overlay-close__text', $output, 'The text should not be output.' ); + $this->assertSame( 'Close', $button['aria-label'], 'The button without text should have a label.' ); + } + + /** + * Tests the parts of the button for each display mode. + * + * @ticket 66578 + * + * @dataProvider data_display_modes + * + * @param array $attributes The attributes of the block. + * @param bool $icon Whether the icon is expected. + * @param bool $text Whether the text is expected. + */ + public function test_should_render_the_parts_of_the_display_mode( $attributes, $icon, $text ) { + $output = render_block_core_navigation_overlay_close( $attributes ); + $button = $this->get_button( $output ); + + $this->assertSame( $icon, str_contains( $output, 'assertSame( $text, str_contains( $output, 'class="wp-block-navigation-overlay-close__text"' ), 'The text should match.' ); + $this->assertSame( ! $text, isset( $button['aria-label'] ), 'The label should be set only without the text.' ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_display_modes() { + return array( + 'icon' => array( array( 'displayMode' => 'icon' ), true, false ), + 'text' => array( array( 'displayMode' => 'text' ), false, true ), + 'both' => array( array( 'displayMode' => 'both' ), true, true ), + 'empty' => array( array( 'displayMode' => '' ), true, false ), + 'a mode not known' => array( array( 'displayMode' => 'other' ), false, false ), + ); + } + + /** + * Tests the default text. + * + * @ticket 66578 + * + * @dataProvider data_empty_texts + * + * @param array $attributes The attributes of the block. + */ + public function test_should_use_the_default_text( $attributes ) { + $output = render_block_core_navigation_overlay_close( $attributes + array( 'displayMode' => 'text' ) ); + + $this->assertStringContainsString( 'Close', $output ); + } + + /** + * Data provider. + * + * @return array[] + */ + public static function data_empty_texts() { + return array( + 'not set' => array( array() ), + 'empty' => array( array( 'text' => '' ) ), + ); + } + + /** + * Tests the text of the attributes with the markup that is allowed in posts. + * + * @ticket 66578 + */ + public function test_should_use_the_text_of_the_attributes_filtered_for_posts() { + $output = render_block_core_navigation_overlay_close( + array( + 'displayMode' => 'both', + 'text' => 'Shut this', + ) + ); + + $this->assertStringContainsString( '>Shut this', $output ); + $this->assertStringNotContainsString( 'assertStringNotContainsString( '>Close', $output ); + } + + /** + * Tests that the button of the text has no label. + * + * @ticket 66578 + */ + public function test_should_not_label_a_button_that_has_text() { + $button = $this->get_button( render_block_core_navigation_overlay_close( array( 'displayMode' => 'both' ) ) ); + + $this->assertArrayNotHasKey( 'aria-label', $button ); + } + + /** + * Tests that the icon is hidden from the assistive technologies. + * + * @ticket 66578 + */ + public function test_should_hide_the_icon_from_assistive_technologies() { + $output = render_block_core_navigation_overlay_close( array( 'displayMode' => 'icon' ) ); + + $this->assertStringContainsString( 'aria-hidden="true" focusable="false"', $output ); + } + + /** + * Tests that the block is registered with the render callback. + * + * @ticket 66578 + * + * @covers ::register_block_core_navigation_overlay_close + */ + public function test_should_register_the_block_with_the_render_callback() { + $registry = WP_Block_Type_Registry::get_instance(); + $original_block = $registry->get_registered( 'core/navigation-overlay-close' ); + + if ( $original_block ) { + unregister_block_type( 'core/navigation-overlay-close' ); + } + + try { + register_block_core_navigation_overlay_close(); + $block_type = $registry->get_registered( 'core/navigation-overlay-close' ); + + $this->assertInstanceOf( WP_Block_Type::class, $block_type ); + $this->assertSame( 'render_block_core_navigation_overlay_close', $block_type->render_callback ); + } finally { + if ( $registry->is_registered( 'core/navigation-overlay-close' ) ) { + unregister_block_type( 'core/navigation-overlay-close' ); + } + + if ( $original_block ) { + $registry->register( $original_block ); + } + } + } +}