Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 73 additions & 13 deletions tests/phpunit/tests/formatting/urlShorten.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
);
}
}
Loading