From dff556f758520d9133d114904968baf43a89966f Mon Sep 17 00:00:00 2001 From: 3kori <127242906+3kori@users.noreply.github.com> Date: Thu, 1 Oct 2026 13:40:54 +0300 Subject: [PATCH 1/5] Tests: Add tests for wp_admin_bar_shortlink_menu --- tests/phpunit/tests/adminbar.php | 49 ++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index fb29eaf39af42..5c3f87b717e2b 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -932,4 +932,53 @@ 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.' ); } + + /** + * This test should not add a Shortlink node when shortlink is empty. + * + * @covers ::wp_admin_bar_shortlink_menu + */ + public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlink_is_empty() { + // Establish a non-singular query context. + $short = wp_get_shortlink( 0, 'query' ); + + $this->assertSame( '', $short ); + + $admin_bar = new WP_Admin_Bar(); + + wp_admin_bar_shortlink_menu( $admin_bar ); + + $node = $admin_bar->get_node( 'get-shortlink' ); + + $this->assertNull( $node ); + } + + /** + * This test should add a Shortlink node when a shortlink exists. + * + * @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(); + + $this->go_to( get_permalink( $post_id ) ); + + $short = wp_get_shortlink( 0, 'query' ); + + $this->assertNotSame( '', $short ); + + $admin_bar = new WP_Admin_Bar(); + + wp_admin_bar_shortlink_menu( $admin_bar ); + + $node = $admin_bar->get_node( 'get-shortlink' ); + + $this->assertNotNull( $node ); + $this->assertSame( $short, $node->href ); + $this->assertSame( 'Shortlink', $node->title ); + $this->assertStringContainsString( + 'value="' . esc_attr( $short ) . '"', + $node->meta['html'] + ); + } } From 0cbe15b8b027cab04870ef18a6995fbf359c8d35 Mon Sep 17 00:00:00 2001 From: 3kori <127242906+3kori@users.noreply.github.com> Date: Thu, 1 Oct 2026 16:02:22 +0300 Subject: [PATCH 2/5] Add assertion messages --- tests/phpunit/tests/adminbar.php | 35 +++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 5c3f87b717e2b..3fb435edd5f68 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -936,13 +936,19 @@ public function test_menu_property_is_defined() { /** * This test should not add a Shortlink node 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() { // Establish a non-singular query context. $short = wp_get_shortlink( 0, 'query' ); - $this->assertSame( '', $short ); + $this->assertSame( + '', + $short, + 'The shortlink should be empty in a non-singular query context.' + ); $admin_bar = new WP_Admin_Bar(); @@ -950,12 +956,14 @@ public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlin $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNull( $node ); + $this->assertNull( $node, 'The Shortlink admin-bar node should not be added when shortlink is empty' ); } /** * This test should add a Shortlink node 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() { @@ -965,7 +973,11 @@ public function test_wp_admin_bar_shortlink_menu_adds_node_when_shortlink_exists $short = wp_get_shortlink( 0, 'query' ); - $this->assertNotSame( '', $short ); + $this->assertNotSame( + '', + $short, + 'The shortlink should exist for the singular post query context.' + ); $admin_bar = new WP_Admin_Bar(); @@ -973,12 +985,21 @@ public function test_wp_admin_bar_shortlink_menu_adds_node_when_shortlink_exists $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNotNull( $node ); - $this->assertSame( $short, $node->href ); - $this->assertSame( 'Shortlink', $node->title ); + $this->assertNotNull( $node, 'The Shortlink admin-bar node should be added when shortlink exists' ); + $this->assertSame( + $short, + $node->href, + 'The admin-bar node href should use the generated shortlink as its href.' + ); + $this->assertSame( + 'Shortlink', + $node->title, + 'The admin-bar node title should have \'Shortlink\' as title.' + ); $this->assertStringContainsString( 'value="' . esc_attr( $short ) . '"', - $node->meta['html'] + $node->meta['html'], + 'The admin-bar node HTML should use the generated shortlink as its href.' ); } } From 846cb039136f5229625d6d2edb3658153ec6567b Mon Sep 17 00:00:00 2001 From: 3kori <127242906+3kori@users.noreply.github.com> Date: Thu, 1 Oct 2026 16:03:36 +0300 Subject: [PATCH 3/5] Fix cover doc --- tests/phpunit/tests/adminbar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index 3fb435edd5f68..37dc80be6770d 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -964,7 +964,7 @@ public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlin * *@ticket 66223 * - * @covers ::wp_admin-bar_shortlink_menu + * @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(); From 0ffff70088eef8abec613133b19e0f0b0f8d7fb4 Mon Sep 17 00:00:00 2001 From: 3kori <127242906+3kori@users.noreply.github.com> Date: Mon, 5 Oct 2026 09:56:38 +0300 Subject: [PATCH 4/5] Add special chars shortlink test --- tests/phpunit/tests/adminbar.php | 82 +++++++++++++++++++------------- 1 file changed, 50 insertions(+), 32 deletions(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index df8361863731e..f6ea088cebc46 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -941,21 +941,15 @@ public function test_menu_property_is_defined() { } /** - * This test should not add a Shortlink node when shortlink is empty. + * 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() { - // Establish a non-singular query context. - $short = wp_get_shortlink( 0, 'query' ); - - $this->assertSame( - '', - $short, - 'The shortlink should be empty in a non-singular query context.' - ); + $this->go_to( home_url( '/' ) ); + $this->assertFalse( is_singular(), 'Precondition: the query should not be singular.' ); $admin_bar = new WP_Admin_Bar(); @@ -963,50 +957,74 @@ public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlin $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNull( $node, 'The Shortlink admin-bar node should not be added when shortlink is empty' ); + $this->assertNull( $node, 'The Shortlink node should not be added when shortlink is empty' ); } /** - * This test should add a Shortlink node when a shortlink exists. + * Tests that the Shortlink node is added when a shortlink exists. * - *@ticket 66223 + * @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(); + $post_id = self::factory()->post->create(); + $expected = home_url( '?p=' . $post_id ); $this->go_to( get_permalink( $post_id ) ); - $short = wp_get_shortlink( 0, 'query' ); + $admin_bar = new WP_Admin_Bar(); + wp_admin_bar_shortlink_menu( $admin_bar ); + $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNotSame( - '', - $short, - 'The shortlink should exist for the singular post query context.' + $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( + 'arial-label="Shortlink"', + $node->meta['html'], + 'The node HTML should contain "Shortlink" as the aria label.' ); + } - $admin_bar = new WP_Admin_Bar(); + /** + * 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() { + $escaped_shortlink = home_url( '?test="first"&coverage="second"' ); + add_filter( 'pre_get_shortlink', array( $this, 'filter_pre_get_shortlink_with_special_chars' ) ); + + $admin_bar = new WP_Admin_Bar(); wp_admin_bar_shortlink_menu( $admin_bar ); + remove_filter( 'pre_get_shortlink', array( $this, 'filter_pre_get_shortlink_with_special_chars' ) ); + $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNotNull( $node, 'The Shortlink admin-bar node should be added when shortlink exists' ); - $this->assertSame( - $short, - $node->href, - 'The admin-bar node href should use the generated shortlink as its href.' - ); - $this->assertSame( - 'Shortlink', - $node->title, - 'The admin-bar node title should have \'Shortlink\' as title.' - ); + $this->assertNotNull( $node, 'The Shortlink node should be added when a shortlink exists.' ); $this->assertStringContainsString( - 'value="' . esc_attr( $short ) . '"', + 'value="' . $escaped_shortlink . '"', $node->meta['html'], - 'The admin-bar node HTML should use the generated shortlink as its href.' + 'The node HTML should contain the properly escaped shortlink in the input value.' ); } + + /** + * Helper function to return a link containing special characters query args. + * + * @return string + */ + public function filter_pre_get_shortlink_with_special_chars() { + return home_url( '?test="first"&coverage="second"' ); + } } From b4c5f6ce0d9b3e758da8061c09fb80304485c712 Mon Sep 17 00:00:00 2001 From: 3kori <127242906+3kori@users.noreply.github.com> Date: Tue, 6 Oct 2026 08:00:50 +0300 Subject: [PATCH 5/5] Change the helper function to a closure --- tests/phpunit/tests/adminbar.php | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/adminbar.php b/tests/phpunit/tests/adminbar.php index f6ea088cebc46..c20c9459141ff 100644 --- a/tests/phpunit/tests/adminbar.php +++ b/tests/phpunit/tests/adminbar.php @@ -957,7 +957,7 @@ public function test_wp_admin_bar_shortlink_menu_does_not_add_node_when_shortlin $node = $admin_bar->get_node( 'get-shortlink' ); - $this->assertNull( $node, 'The Shortlink node should not be added when shortlink is empty' ); + $this->assertNull( $node, 'The Shortlink node should not be added when shortlink is empty.' ); } /** @@ -977,16 +977,16 @@ public function test_wp_admin_bar_shortlink_menu_adds_node_when_shortlink_exists 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->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->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( - 'arial-label="Shortlink"', + 'aria-label="Shortlink"', $node->meta['html'], 'The node HTML should contain "Shortlink" as the aria label.' ); @@ -1000,31 +1000,25 @@ public function test_wp_admin_bar_shortlink_menu_adds_node_when_shortlink_exists * @covers ::wp_admin_bar_shortlink_menu */ public function test_wp_admin_bar_shortlink_menu_escapes_shortlink_in_input_value() { - $escaped_shortlink = home_url( '?test="first"&coverage="second"' ); + $expected = home_url( '?test="first"&coverage="second"' ); - add_filter( 'pre_get_shortlink', array( $this, 'filter_pre_get_shortlink_with_special_chars' ) ); + 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 ); - remove_filter( 'pre_get_shortlink', array( $this, 'filter_pre_get_shortlink_with_special_chars' ) ); - $node = $admin_bar->get_node( 'get-shortlink' ); $this->assertNotNull( $node, 'The Shortlink node should be added when a shortlink exists.' ); $this->assertStringContainsString( - 'value="' . $escaped_shortlink . '"', + 'value="' . $expected . '"', $node->meta['html'], 'The node HTML should contain the properly escaped shortlink in the input value.' ); } - - /** - * Helper function to return a link containing special characters query args. - * - * @return string - */ - public function filter_pre_get_shortlink_with_special_chars() { - return home_url( '?test="first"&coverage="second"' ); - } }