diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 5a611fd72aac6..c20c9459141ff 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -939,4 +939,86 @@ public function test_menu_property_is_defined() { $this->assertTrue( isset( $admin_bar->menu ), 'WP_Admin_Bar::$menu should be set.' ); $this->assertSame( array(), $admin_bar->menu, 'WP_Admin_Bar::$menu should be equal to an empty array.' ); } + + /** + * Tests that the Shortlink node is not added when shortlink is empty. + * + * @ticket 66223 + * + * @covers ::wp_admin_bar_shortlink_menu + */ + public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlink_is_empty() { + $this->go_to( home_url( '/' ) ); + $this->assertFalse( is_singular(), 'Precondition: the query should not be singular.' ); + + $admin_bar = new WP_Admin_Bar(); + + wp_admin_bar_shortlink_menu( $admin_bar ); + + $node = $admin_bar->get_node( 'get-shortlink' ); + + $this->assertNull( $node, 'The Shortlink node should not be added when shortlink is empty.' ); + } + + /** + * Tests that the Shortlink node is added when a shortlink exists. + * + * @ticket 66223 + * + * @covers ::wp_admin_bar_shortlink_menu + */ + public function test_wp_admin_bar_shortlink_menu_adds_node_when_shortlink_exists() { + $post_id = self::factory()->post->create(); + $expected = home_url( '?p=' . $post_id ); + + $this->go_to( get_permalink( $post_id ) ); + + $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_shortlink_menu( $admin_bar ); + $node = $admin_bar->get_node( 'get-shortlink' ); + + $this->assertNotNull( $node, 'The Shortlink node should be added when shortlink exists.' ); + $this->assertSame( $expected, $node->href, 'The node href should be the post shortlink.' ); + $this->assertSame( 'Shortlink', $node->title, 'The node title should be "Shortlink".' ); + $this->assertStringContainsString( + 'value="' . esc_attr( $expected ) . '"', + $node->meta['html'], + 'The node HTML should contain the shortlink as the input value.' + ); + $this->assertStringContainsString( + 'aria-label="Shortlink"', + $node->meta['html'], + 'The node HTML should contain "Shortlink" as the aria label.' + ); + } + + /** + * Tests that the Shortlink node escapes the shortlink value in the HTML input value. + * + * @ticket 66223 + * + * @covers ::wp_admin_bar_shortlink_menu + */ + public function test_wp_admin_bar_shortlink_menu_escapes_shortlink_in_input_value() { + $expected = home_url( '?test="first"&coverage="second"' ); + + add_filter( + 'pre_get_shortlink', + static function () { + return home_url( '?test="first"&coverage="second"' ); + } + ); + + $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_shortlink_menu( $admin_bar ); + + $node = $admin_bar->get_node( 'get-shortlink' ); + + $this->assertNotNull( $node, 'The Shortlink node should be added when a shortlink exists.' ); + $this->assertStringContainsString( + 'value="' . $expected . '"', + $node->meta['html'], + 'The node HTML should contain the properly escaped shortlink in the input value.' + ); + } }