Make WordPress Core

Changeset 62768


Ignore:
Timestamp:
07/17/2026 03:47:23 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Improve coverage for url_shorten() and test the $length parameter.

The case meant to test the optional $length parameter passed it as an argument to assertSame() instead of url_shorten(). So the latter ran with its default length of 35, the expected value happened to match the default output, and the assertion passed — while the $length parameter was never actually tested.

This commit:

  • Converts the tests to a data provider with descriptive keys, so a failure points to the exact case.
  • Separates the default-behavior cases from the custom-length cases.
  • Adds real coverage for $length: the URL is kept when the length exceeds or equals the cleaned URL (pinning the strict > comparison) and shortened when it is smaller.
  • Adds a comment clarifying that a shortened URL is cut to ( $length - 3 ) characters before … is appended.

No production code is changed — this is a test-only improvement.

Developed in https://github.com/WordPress/wordpress-develop/pull/12558.

Follow-up to [35314].

Props Soean, mukesh27, SergeyBiryukov.
See #64894.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/formatting/urlShorten.php

    r53562 r62768  
    77 */
    88class Tests_Formatting_UrlShorten extends WP_UnitTestCase {
    9         public function test_url_shorten() {
    10                 $tests = array(
    11                         'wordpress\.org/about/philosophy'            => 'wordpress\.org/about/philosophy', // No longer strips slashes.
    12                         'wordpress.org/about/philosophy'             => 'wordpress.org/about/philosophy',
    13                         'http://wordpress.org/about/philosophy/'     => 'wordpress.org/about/philosophy',  // Remove http, trailing slash.
    14                         'http://www.wordpress.org/about/philosophy/' => 'wordpress.org/about/philosophy',  // Remove http, www.
    15                         'http://wordpress.org/about/philosophy/#box' => 'wordpress.org/about/philosophy/#box',            // Don't shorten 35 characters.
    16                         'http://wordpress.org/about/philosophy/#decisions' => 'wordpress.org/about/philosophy/#…', // Shorten to 32 if > 35 after cleaning.
     9
     10        /**
     11         * @dataProvider data_url_shorten
     12         *
     13         * @param string $url      URL to shorten.
     14         * @param string $expected Expected shortened URL.
     15         */
     16        public function test_url_shorten( $url, $expected ) {
     17                $this->assertSame( $expected, url_shorten( $url ) );
     18        }
     19
     20        public function data_url_shorten() {
     21                // When shortened, the URL is cut to ( $length - 3 ) characters before '…' is appended.
     22                return array(
     23                        'escaped slashes are not stripped' => array(
     24                                'wordpress\.org/about/philosophy',
     25                                'wordpress\.org/about/philosophy',
     26                        ),
     27                        'no change needed'                 => array(
     28                                'wordpress.org/about/philosophy',
     29                                'wordpress.org/about/philosophy',
     30                        ),
     31                        'http and trailing slash removed'  => array(
     32                                'http://wordpress.org/about/philosophy/',
     33                                'wordpress.org/about/philosophy',
     34                        ),
     35                        'http and www removed'             => array(
     36                                'http://www.wordpress.org/about/philosophy/',
     37                                'wordpress.org/about/philosophy',
     38                        ),
     39                        'exactly 35 characters kept'       => array(
     40                                'http://wordpress.org/about/philosophy/#box',
     41                                'wordpress.org/about/philosophy/#box',
     42                        ),
     43                        'shortened to 32 when over 35'     => array(
     44                                'http://wordpress.org/about/philosophy/#decisions',
     45                                'wordpress.org/about/philosophy/#…',
     46                        ),
    1747                );
    18                 foreach ( $tests as $k => $v ) {
    19                         $this->assertSame( $v, url_shorten( $k ) );
    20                 }
     48        }
    2149
    22                 // Shorten to 31 if > 34 after cleaning.
    23                 $this->assertSame( 'wordpress.org/about/philosophy/#…', url_shorten( 'http://wordpress.org/about/philosophy/#decisions' ), 31 );
     50        /**
     51         * Ensures the optional $length parameter overrides the default of 35.
     52         *
     53         * @dataProvider data_url_shorten_custom_length
     54         *
     55         * @param string $url      URL to shorten.
     56         * @param int    $length   Maximum length of the shortened URL.
     57         * @param string $expected Expected shortened URL.
     58         */
     59        public function test_url_shorten_respects_custom_length( $url, $length, $expected ) {
     60                $this->assertSame( $expected, url_shorten( $url, $length ) );
     61        }
     62
     63        public function data_url_shorten_custom_length() {
     64                // The URL below is 41 characters long after cleaning.
     65                $url = 'http://wordpress.org/about/philosophy/#decisions';
     66
     67                return array(
     68                        'kept when length exceeds the URL' => array(
     69                                $url,
     70                                100,
     71                                'wordpress.org/about/philosophy/#decisions',
     72                        ),
     73                        'kept when length equals the URL'  => array(
     74                                $url,
     75                                41,
     76                                'wordpress.org/about/philosophy/#decisions',
     77                        ),
     78                        'shortened when length is smaller' => array(
     79                                $url,
     80                                40,
     81                                'wordpress.org/about/philosophy/#decis…',
     82                        ),
     83                );
    2484        }
    2585}
Note: See TracChangeset for help on using the changeset viewer.