Make WordPress Core

Changeset 36721


Ignore:
Timestamp:
02/26/2016 02:08:47 AM (9 years ago)
Author:
johnbillion
Message:

Remove (or at least reduce) the need to reset common $_SERVER variables before assertions or between tests, by introducing a method which automatically resets them during test setup.

See #35954

Location:
trunk/tests/phpunit
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/bootstrap.php

    r36715 r36721  
    2525require_once dirname( __FILE__ ) . '/functions.php';
    2626
    27 tests_reset_SERVER();
     27tests_reset__SERVER();
    2828
    2929define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
  • trunk/tests/phpunit/includes/functions.php

    r36715 r36721  
    44 * Resets various `$_SERVER` variables that can get altered during tests.
    55 */
    6 function tests_reset_SERVER() {
     6function tests_reset__SERVER() {
    77    $_SERVER['HTTP_HOST']       = WP_TESTS_DOMAIN;
    88    $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
    99    $_SERVER['REQUEST_METHOD']  = 'GET';
     10    $_SERVER['REQUEST_URI']     = '';
    1011    $_SERVER['SERVER_NAME']     = WP_TESTS_DOMAIN;
    1112    $_SERVER['SERVER_PORT']     = '80';
  • trunk/tests/phpunit/includes/install.php

    r36715 r36721  
    1414require_once dirname( __FILE__ ) . '/functions.php';
    1515
    16 tests_reset_SERVER();
     16tests_reset__SERVER();
    1717
    1818$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';
  • trunk/tests/phpunit/includes/testcase.php

    r35849 r36721  
    102102            $this->reset_taxonomies();
    103103            $this->reset_post_statuses();
     104            $this->reset__SERVER();
    104105
    105106            if ( $wp_rewrite->permalink_structure ) {
     
    184185            _unregister_post_status( $post_status );
    185186        }
     187    }
     188
     189    /**
     190     * Reset `$_SERVER` variables
     191     */
     192    protected function reset__SERVER() {
     193        tests_reset__SERVER();
    186194    }
    187195
  • trunk/tests/phpunit/tests/multisite/site.php

    r35446 r36721  
    10281028        $is_ssl  = is_ssl();
    10291029        $address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
    1030 
    1031         unset( $_SERVER['HTTPS'] );
    10321030
    10331031        $this->assertTrue( $is_ssl );
  • trunk/tests/phpunit/tests/post/attachments.php

    r36711 r36721  
    297297        $attachment_id = $this->_make_attachment( $upload );
    298298
    299         // Save server data for cleanup.
    300         $is_ssl = is_ssl();
    301299        $_SERVER['HTTPS'] = 'off';
    302300
    303301        $url = wp_get_attachment_url( $attachment_id );
    304 
    305         // Cleanup.
    306         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    307302
    308303        $this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
     
    327322        $attachment_id = $this->_make_attachment( $upload );
    328323
    329         // Save server data for cleanup.
    330         $is_ssl = is_ssl();
    331324        $_SERVER['HTTPS'] = 'off';
    332325
    333326        $url = wp_get_attachment_url( $attachment_id );
    334 
    335         // Cleanup.
    336         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    337327
    338328        $this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
     
    357347        $attachment_id = $this->_make_attachment( $upload );
    358348
    359         // Save server data for cleanup
    360         $is_ssl = is_ssl();
    361         $http_host = $_SERVER['HTTP_HOST'];
    362 
    363349        $_SERVER['HTTPS'] = 'on';
    364350
     
    370356        $url = wp_get_attachment_url( $attachment_id );
    371357
    372         // Cleanup.
    373         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    374         $_SERVER['HTTP_HOST'] = $http_host;
    375 
    376358        $this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
    377359    }
     
    393375        $attachment_id = $this->_make_attachment( $upload );
    394376
    395         // Save server data for cleanup.
    396         $is_ssl = is_ssl();
    397         $http_host = $_SERVER['HTTP_HOST'];
    398 
    399377        $_SERVER['HTTPS'] = 'on';
    400378
     
    405383        // Test that wp_get_attachemt_url returns with https scheme.
    406384        $url = wp_get_attachment_url( $attachment_id );
    407 
    408         // Cleanup.
    409         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    410         $_SERVER['HTTP_HOST'] = $http_host;
    411385
    412386        $this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
     
    429403        $attachment_id = $this->_make_attachment( $upload );
    430404
    431         $is_ssl = is_ssl();
    432405        $_SERVER['HTTPS'] = 'on';
    433406        set_current_screen( 'dashboard' );
     
    436409
    437410        // Cleanup.
    438         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    439411        set_current_screen( 'front' );
    440412
     
    458430        $attachment_id = $this->_make_attachment( $upload );
    459431
    460         $is_ssl = is_ssl();
    461432        $_SERVER['HTTPS'] = 'on';
    462433        set_current_screen( 'dashboard' );
     
    465436
    466437        // Cleanup.
    467         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    468438        set_current_screen( 'front' );
    469439        remove_filter( 'upload_dir', '_upload_dir_https' );
  • trunk/tests/phpunit/tests/rest-api.php

    r35651 r36721  
    286286     */
    287287    public function test_rest_url_scheme() {
    288         if ( isset( $_SERVER['HTTPS'] ) ) {
    289             $_https = $_SERVER['HTTPS'];
    290         }
    291         $_name = $_SERVER['SERVER_NAME'];
    292288        $_SERVER['SERVER_NAME'] = parse_url( home_url(), PHP_URL_HOST );
    293289        $_siteurl = get_option( 'siteurl' );
     
    322318
    323319        // Reset
    324         if ( isset( $_https ) ) {
    325             $_SERVER['HTTPS'] = $_https;
    326         } else {
    327             unset( $_SERVER['HTTPS'] );
    328         }
    329         $_SERVER['SERVER_NAME'] = $_name;
    330320        update_option( 'siteurl', $_siteurl );
    331321        set_current_screen( 'front' );
  • trunk/tests/phpunit/tests/rewrite.php

    r36711 r36721  
    104104
    105105    function test_url_to_postid_set_url_scheme_http_to_https() {
    106         // Save server data for cleanup
    107         $is_ssl = is_ssl();
    108 
    109106        $_SERVER['HTTPS'] = 'on';
    110107
     
    116113        $page_permalink = get_permalink( $page_id );
    117114        $page_url_to_id = url_to_postid( set_url_scheme( $page_permalink, 'http' ) );
    118 
    119         // Cleanup.
    120         $_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
    121115
    122116        $this->assertEquals( $post_id, $post_url_to_id );
Note: See TracChangeset for help on using the changeset viewer.