From 06f4930879024711a1f323948a6ae970705364f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Thu, 16 Jul 2026 14:41:32 +0200 Subject: [PATCH] Tests: Improve coverage for url_shorten() and fix an ineffective length assertion. --- tests/phpunit/tests/formatting/urlShorten.php | 86 ++++++++++++++++--- 1 file changed, 73 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/formatting/urlShorten.php b/tests/phpunit/tests/formatting/urlShorten.php index 4909130dbce21..4f4b85c2b26e0 100644 --- a/tests/phpunit/tests/formatting/urlShorten.php +++ b/tests/phpunit/tests/formatting/urlShorten.php @@ -6,20 +6,80 @@ * @covers ::url_shorten */ class Tests_Formatting_UrlShorten extends WP_UnitTestCase { - public function test_url_shorten() { - $tests = array( - 'wordpress\.org/about/philosophy' => 'wordpress\.org/about/philosophy', // No longer strips slashes. - 'wordpress.org/about/philosophy' => 'wordpress.org/about/philosophy', - 'http://wordpress.org/about/philosophy/' => 'wordpress.org/about/philosophy', // Remove http, trailing slash. - 'http://www.wordpress.org/about/philosophy/' => 'wordpress.org/about/philosophy', // Remove http, www. - 'http://wordpress.org/about/philosophy/#box' => 'wordpress.org/about/philosophy/#box', // Don't shorten 35 characters. - 'http://wordpress.org/about/philosophy/#decisions' => 'wordpress.org/about/philosophy/#…', // Shorten to 32 if > 35 after cleaning. + + /** + * @dataProvider data_url_shorten + * + * @param string $expected Expected shortened URL. + * @param string $url URL to shorten. + */ + public function test_url_shorten( $expected, $url ) { + $this->assertSame( $expected, url_shorten( $url ) ); + } + + public function data_url_shorten() { + // When shortened, the URL is cut to ( $length - 3 ) characters before '…' is appended. + return array( + 'escaped slashes are not stripped' => array( + 'wordpress\.org/about/philosophy', + 'wordpress\.org/about/philosophy', + ), + 'no change needed' => array( + 'wordpress.org/about/philosophy', + 'wordpress.org/about/philosophy', + ), + 'http and trailing slash removed' => array( + 'wordpress.org/about/philosophy', + 'http://wordpress.org/about/philosophy/', + ), + 'http and www removed' => array( + 'wordpress.org/about/philosophy', + 'http://www.wordpress.org/about/philosophy/', + ), + 'exactly 35 characters kept' => array( + 'wordpress.org/about/philosophy/#box', + 'http://wordpress.org/about/philosophy/#box', + ), + 'shortened to 32 when over 35' => array( + 'wordpress.org/about/philosophy/#…', + 'http://wordpress.org/about/philosophy/#decisions', + ), ); - foreach ( $tests as $k => $v ) { - $this->assertSame( $v, url_shorten( $k ) ); - } + } - // Shorten to 31 if > 34 after cleaning. - $this->assertSame( 'wordpress.org/about/philosophy/#…', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 ); + /** + * Ensures the optional $length parameter overrides the default of 35. + * + * @dataProvider data_url_shorten_custom_length + * + * @param string $expected Expected shortened URL. + * @param string $url URL to shorten. + * @param int $length Maximum length of the shortened URL. + */ + public function test_url_shorten_respects_custom_length( $expected, $url, $length ) { + $this->assertSame( $expected, url_shorten( $url, $length ) ); + } + + public function data_url_shorten_custom_length() { + // The URL below is 41 characters long after cleaning. + $url = 'http://wordpress.org/about/philosophy/#decisions'; + + return array( + 'kept when length exceeds the URL' => array( + 'wordpress.org/about/philosophy/#decisions', + $url, + 100, + ), + 'kept when length equals the URL' => array( + 'wordpress.org/about/philosophy/#decisions', + $url, + 41, + ), + 'shortened when length is smaller' => array( + 'wordpress.org/about/philosophy/#decis…', + $url, + 40, + ), + ); } }