Refactor some janky URL tests into data providers for clarity and better error reporting.

See #35954


git-svn-id: https://develop.svn.wordpress.org/trunk@36722 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2016-02-26 02:10:32 +00:00
parent 1ebe4658f1
commit 48aa555ced
1 changed files with 167 additions and 103 deletions

View File

@ -5,132 +5,196 @@
* @group url * @group url
*/ */
class Tests_URL extends WP_UnitTestCase { class Tests_URL extends WP_UnitTestCase {
var $_old_server;
function setUp() { function setUp() {
parent::setUp(); parent::setUp();
$this->_old_server = $_SERVER;
$GLOBALS['pagenow'] = ''; $GLOBALS['pagenow'] = '';
} }
function tearDown() { /**
$_SERVER = $this->_old_server; * @dataProvider data_is_ssl
parent::tearDown(); */
function test_is_ssl( $value, $expected ) {
$_SERVER['HTTPS'] = $value;
$is_ssl = is_ssl();
$this->assertSame( $expected, $is_ssl );
} }
function test_is_ssl_positive() { function data_is_ssl() {
$_SERVER['HTTPS'] = 'on'; return array(
$this->assertTrue( is_ssl() ); array(
'on',
$_SERVER['HTTPS'] = 'ON'; true,
$this->assertTrue( is_ssl() ); ),
array(
$_SERVER['HTTPS'] = '1'; 'ON',
$this->assertTrue( is_ssl() ); true,
),
array(
'1',
true,
),
array(
'off',
false,
),
array(
'OFF',
false,
),
);
}
function test_is_ssl_by_port() {
unset( $_SERVER['HTTPS'] ); unset( $_SERVER['HTTPS'] );
$_SERVER['SERVER_PORT'] = '443'; $_SERVER['SERVER_PORT'] = '443';
$this->assertTrue( is_ssl() );
$is_ssl = is_ssl();
$this->assertTrue( $is_ssl );
} }
function test_is_ssl_negative() { function test_is_ssl_with_no_value() {
$_SERVER['HTTPS'] = 'off';
$this->assertFalse( is_ssl() );
$_SERVER['HTTPS'] = 'OFF';
$this->assertFalse( is_ssl() );
unset( $_SERVER['HTTPS'] ); unset( $_SERVER['HTTPS'] );
$this->assertFalse( is_ssl() );
$is_ssl = is_ssl();
$this->assertFalse( $is_ssl );
} }
function test_admin_url_valid() { /**
$paths = array( * @dataProvider data_admin_urls
'' => "/wp-admin/", *
'foo' => "/wp-admin/foo", * @param string $url Test URL.
'/foo' => "/wp-admin/foo", * @param string $expected Expected result.
'/foo/' => "/wp-admin/foo/", */
'foo.php' => "/wp-admin/foo.php", function test_admin_url( $url, $expected ) {
'/foo.php' => "/wp-admin/foo.php", $siteurl_http = get_option( 'siteurl' );
'/foo.php?bar=1' => "/wp-admin/foo.php?bar=1", $admin_url_http = admin_url( $url );
$_SERVER['HTTPS'] = 'on';
$siteurl_https = set_url_scheme( $siteurl_http, 'https' );
$admin_url_https = admin_url( $url );
$this->assertEquals( $siteurl_http . $expected, $admin_url_http );
$this->assertEquals( $siteurl_https . $expected, $admin_url_https );
}
function data_admin_urls() {
return array(
array(
null,
'/wp-admin/'
),
array(
0,
'/wp-admin/'
),
array(
-1,
'/wp-admin/'
),
array(
'///',
'/wp-admin/'
),
array(
'',
'/wp-admin/',
),
array(
'foo',
'/wp-admin/foo',
),
array(
'/foo',
'/wp-admin/foo',
),
array(
'/foo/',
'/wp-admin/foo/',
),
array(
'foo.php',
'/wp-admin/foo.php',
),
array(
'/foo.php',
'/wp-admin/foo.php',
),
array(
'/foo.php?bar=1',
'/wp-admin/foo.php?bar=1',
),
); );
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$siteurl = get_option('siteurl');
if ( $val == 'on' )
$siteurl = str_replace('http://', 'https://', $siteurl);
foreach ($paths as $in => $out) {
$this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
}
}
} }
function test_admin_url_invalid() { /**
$paths = array( * @dataProvider data_home_urls
null => "/wp-admin/", *
0 => "/wp-admin/", * @param string $url Test URL.
-1 => "/wp-admin/", * @param string $expected Expected result.
'///' => "/wp-admin/", */
function test_home_url( $url, $expected ) {
$homeurl_http = get_option( 'home' );
$home_url_http = home_url( $url );
$_SERVER['HTTPS'] = 'on';
$homeurl_https = set_url_scheme( $homeurl_http, 'https' );
$home_url_https = home_url( $url );
$this->assertEquals( $homeurl_http . $expected, $home_url_http );
$this->assertEquals( $homeurl_https . $expected, $home_url_https );
}
function data_home_urls() {
return array(
array(
null,
"",
),
array(
0,
"",
),
array(
-1,
"",
),
array(
'///',
"/",
),
array(
'',
"",
),
array(
'foo',
"/foo",
),
array(
'/foo',
"/foo",
),
array(
'/foo/',
"/foo/",
),
array(
'foo.php',
"/foo.php",
),
array(
'/foo.php',
"/foo.php",
),
array(
'/foo.php?bar=1',
"/foo.php?bar=1",
),
); );
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$siteurl = get_option('siteurl');
if ( $val == 'on' )
$siteurl = str_replace('http://', 'https://', $siteurl);
foreach ($paths as $in => $out) {
$this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
}
}
}
function test_home_url_valid() {
$paths = array(
'' => "",
'foo' => "/foo",
'/foo' => "/foo",
'/foo/' => "/foo/",
'foo.php' => "/foo.php",
'/foo.php' => "/foo.php",
'/foo.php?bar=1' => "/foo.php?bar=1",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$home = get_option('home');
if ( $val == 'on' )
$home = str_replace('http://', 'https://', $home);
foreach ($paths as $in => $out) {
$this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
}
}
}
function test_home_url_invalid() {
$paths = array(
null => "",
0 => "",
-1 => "",
'///' => "/",
);
$https = array('on', 'off');
foreach ($https as $val) {
$_SERVER['HTTPS'] = $val;
$home = get_option('home');
if ( $val == 'on' )
$home = str_replace('http://', 'https://', $home);
foreach ($paths as $in => $out) {
$this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
}
}
} }
function test_home_url_from_admin() { function test_home_url_from_admin() {