Make WordPress Core


Ignore:
Timestamp:
02/26/2016 02:10:32 AM (10 years ago)
Author:
johnbillion
Message:

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

See #35954

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/url.php

    r35242 r36722  
    66 */
    77class Tests_URL extends WP_UnitTestCase {
    8         var $_old_server;
     8
    99        function setUp() {
    1010                parent::setUp();
    11                 $this->_old_server = $_SERVER;
    1211                $GLOBALS['pagenow'] = '';
    1312        }
    1413
    15         function tearDown() {
    16                 $_SERVER = $this->_old_server;
    17                 parent::tearDown();
    18         }
    19 
    20         function test_is_ssl_positive() {
    21                 $_SERVER['HTTPS'] = 'on';
    22                 $this->assertTrue( is_ssl() );
    23 
    24                 $_SERVER['HTTPS'] = 'ON';
    25                 $this->assertTrue( is_ssl() );
    26 
    27                 $_SERVER['HTTPS'] = '1';
    28                 $this->assertTrue( is_ssl() );
    29 
     14        /**
     15         * @dataProvider data_is_ssl
     16         */
     17        function test_is_ssl( $value, $expected ) {
     18                $_SERVER['HTTPS'] = $value;
     19
     20                $is_ssl = is_ssl();
     21                $this->assertSame( $expected, $is_ssl );
     22        }
     23
     24        function data_is_ssl() {
     25                return array(
     26                        array(
     27                                'on',
     28                                true,
     29                        ),
     30                        array(
     31                                'ON',
     32                                true,
     33                        ),
     34                        array(
     35                                '1',
     36                                true,
     37                        ),
     38                        array(
     39                                'off',
     40                                false,
     41                        ),
     42                        array(
     43                                'OFF',
     44                                false,
     45                        ),
     46                );
     47        }
     48
     49        function test_is_ssl_by_port() {
    3050                unset( $_SERVER['HTTPS'] );
    3151                $_SERVER['SERVER_PORT'] = '443';
    32                 $this->assertTrue( is_ssl() );
    33         }
    34 
    35         function test_is_ssl_negative() {
    36                 $_SERVER['HTTPS'] = 'off';
    37                 $this->assertFalse( is_ssl() );
    38 
    39                 $_SERVER['HTTPS'] = 'OFF';
    40                 $this->assertFalse( is_ssl() );
    41 
    42                 unset($_SERVER['HTTPS']);
    43                 $this->assertFalse( is_ssl() );
    44         }
    45 
    46         function test_admin_url_valid() {
    47                 $paths = array(
    48                         '' => "/wp-admin/",
    49                         'foo' => "/wp-admin/foo",
    50                         '/foo' => "/wp-admin/foo",
    51                         '/foo/' => "/wp-admin/foo/",
    52                         'foo.php' => "/wp-admin/foo.php",
    53                         '/foo.php' => "/wp-admin/foo.php",
    54                         '/foo.php?bar=1' => "/wp-admin/foo.php?bar=1",
    55                 );
    56                 $https = array('on', 'off');
    57 
    58                 foreach ($https as $val) {
    59                         $_SERVER['HTTPS'] = $val;
    60                         $siteurl = get_option('siteurl');
    61                         if ( $val == 'on' )
    62                                 $siteurl = str_replace('http://', 'https://', $siteurl);
    63 
    64                         foreach ($paths as $in => $out) {
    65                                 $this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
    66                         }
    67                 }
    68         }
    69 
    70         function test_admin_url_invalid() {
    71                 $paths = array(
    72                         null => "/wp-admin/",
    73                         0 => "/wp-admin/",
    74                         -1 => "/wp-admin/",
    75                         '///' => "/wp-admin/",
    76                 );
    77                 $https = array('on', 'off');
    78 
    79                 foreach ($https as $val) {
    80                         $_SERVER['HTTPS'] = $val;
    81                         $siteurl = get_option('siteurl');
    82                         if ( $val == 'on' )
    83                                 $siteurl = str_replace('http://', 'https://', $siteurl);
    84 
    85                         foreach ($paths as $in => $out) {
    86                                 $this->assertEquals( $siteurl.$out, admin_url($in), "admin_url('{$in}') should equal '{$siteurl}{$out}'");
    87                         }
    88                 }
    89         }
    90 
    91         function test_home_url_valid() {
    92                 $paths = array(
    93                         '' => "",
    94                         'foo' => "/foo",
    95                         '/foo' => "/foo",
    96                         '/foo/' => "/foo/",
    97                         'foo.php' => "/foo.php",
    98                         '/foo.php' => "/foo.php",
    99                         '/foo.php?bar=1' => "/foo.php?bar=1",
    100                 );
    101                 $https = array('on', 'off');
    102 
    103                 foreach ($https as $val) {
    104                         $_SERVER['HTTPS'] = $val;
    105                         $home = get_option('home');
    106                         if ( $val == 'on' )
    107                                 $home = str_replace('http://', 'https://', $home);
    108 
    109                         foreach ($paths as $in => $out) {
    110                                 $this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
    111                         }
    112                 }
    113         }
    114 
    115         function test_home_url_invalid() {
    116                 $paths = array(
    117                         null => "",
    118                         0 => "",
    119                         -1 => "",
    120                         '///' => "/",
    121                 );
    122                 $https = array('on', 'off');
    123 
    124                 foreach ($https as $val) {
    125                         $_SERVER['HTTPS'] = $val;
    126                         $home = get_option('home');
    127                         if ( $val == 'on' )
    128                                 $home = str_replace('http://', 'https://', $home);
    129 
    130                         foreach ($paths as $in => $out) {
    131                                 $this->assertEquals( $home.$out, home_url($in), "home_url('{$in}') should equal '{$home}{$out}'");
    132                         }
    133                 }
     52
     53                $is_ssl = is_ssl();
     54                $this->assertTrue( $is_ssl );
     55        }
     56
     57        function test_is_ssl_with_no_value() {
     58                unset( $_SERVER['HTTPS'] );
     59
     60                $is_ssl = is_ssl();
     61                $this->assertFalse( $is_ssl );
     62        }
     63
     64        /**
     65         * @dataProvider data_admin_urls
     66         *
     67         * @param string $url      Test URL.
     68         * @param string $expected Expected result.
     69         */
     70        function test_admin_url( $url, $expected ) {
     71                $siteurl_http   = get_option( 'siteurl' );
     72                $admin_url_http = admin_url( $url );
     73
     74                $_SERVER['HTTPS'] = 'on';
     75
     76                $siteurl_https   = set_url_scheme( $siteurl_http, 'https' );
     77                $admin_url_https = admin_url( $url );
     78
     79                $this->assertEquals( $siteurl_http . $expected, $admin_url_http );
     80                $this->assertEquals( $siteurl_https . $expected, $admin_url_https );
     81        }
     82
     83        function data_admin_urls() {
     84                return array(
     85                        array(
     86                                null,
     87                                '/wp-admin/'
     88                        ),
     89                        array(
     90                                0,
     91                                '/wp-admin/'
     92                        ),
     93                        array(
     94                                -1,
     95                                '/wp-admin/'
     96                        ),
     97                        array(
     98                                '///',
     99                                '/wp-admin/'
     100                        ),
     101                        array(
     102                                '',
     103                                '/wp-admin/',
     104                        ),
     105                        array(
     106                                'foo',
     107                                '/wp-admin/foo',
     108                        ),
     109                        array(
     110                                '/foo',
     111                                '/wp-admin/foo',
     112                        ),
     113                        array(
     114                                '/foo/',
     115                                '/wp-admin/foo/',
     116                        ),
     117                        array(
     118                                'foo.php',
     119                                '/wp-admin/foo.php',
     120                        ),
     121                        array(
     122                                '/foo.php',
     123                                '/wp-admin/foo.php',
     124                        ),
     125                        array(
     126                                '/foo.php?bar=1',
     127                                '/wp-admin/foo.php?bar=1',
     128                        ),
     129                );
     130        }
     131
     132        /**
     133         * @dataProvider data_home_urls
     134         *
     135         * @param string $url      Test URL.
     136         * @param string $expected Expected result.
     137         */
     138        function test_home_url( $url, $expected ) {
     139                $homeurl_http  = get_option( 'home' );
     140                $home_url_http = home_url( $url );
     141
     142                $_SERVER['HTTPS'] = 'on';
     143
     144                $homeurl_https  = set_url_scheme( $homeurl_http, 'https' );
     145                $home_url_https = home_url( $url );
     146
     147                $this->assertEquals( $homeurl_http . $expected, $home_url_http );
     148                $this->assertEquals( $homeurl_https . $expected, $home_url_https );
     149        }
     150
     151        function data_home_urls() {
     152                return array(
     153                        array(
     154                                null,
     155                                "",
     156                        ),
     157                        array(
     158                                0,
     159                                "",
     160                        ),
     161                        array(
     162                                -1,
     163                                "",
     164                        ),
     165                        array(
     166                                '///',
     167                                "/",
     168                        ),
     169                        array(
     170                                '',
     171                                "",
     172                        ),
     173                        array(
     174                                'foo',
     175                                "/foo",
     176                        ),
     177                        array(
     178                                '/foo',
     179                                "/foo",
     180                        ),
     181                        array(
     182                                '/foo/',
     183                                "/foo/",
     184                        ),
     185                        array(
     186                                'foo.php',
     187                                "/foo.php",
     188                        ),
     189                        array(
     190                                '/foo.php',
     191                                "/foo.php",
     192                        ),
     193                        array(
     194                                '/foo.php?bar=1',
     195                                "/foo.php?bar=1",
     196                        ),
     197                );
    134198        }
    135199
Note: See TracChangeset for help on using the changeset viewer.