|
6 | 6 | * @covers ::url_shorten |
7 | 7 | */ |
8 | 8 | class 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 | + ), |
17 | 47 | ); |
18 | | - foreach ( $tests as $k => $v ) { |
19 | | - $this->assertSame( $v, url_shorten( $k ) ); |
20 | | - } |
| 48 | + } |
21 | 49 |
|
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 | + ); |
24 | 84 | } |
25 | 85 | } |
0 commit comments