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


git-svn-id: https://develop.svn.wordpress.org/trunk@36721 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2016-02-26 02:08:47 +00:00
parent 825e2e45fe
commit 1ebe4658f1
8 changed files with 12 additions and 51 deletions

View File

@ -24,7 +24,7 @@ if ( !is_readable( $config_file_path ) ) {
require_once $config_file_path;
require_once dirname( __FILE__ ) . '/functions.php';
tests_reset_SERVER();
tests_reset__SERVER();
define( 'WP_TESTS_TABLE_PREFIX', $table_prefix );
define( 'DIR_TESTDATA', dirname( __FILE__ ) . '/../data' );

View File

@ -3,10 +3,11 @@
/**
* Resets various `$_SERVER` variables that can get altered during tests.
*/
function tests_reset_SERVER() {
function tests_reset__SERVER() {
$_SERVER['HTTP_HOST'] = WP_TESTS_DOMAIN;
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '';
$_SERVER['SERVER_NAME'] = WP_TESTS_DOMAIN;
$_SERVER['SERVER_PORT'] = '80';
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';

View File

@ -13,7 +13,7 @@ define( 'WP_INSTALLING', true );
require_once $config_file_path;
require_once dirname( __FILE__ ) . '/functions.php';
tests_reset_SERVER();
tests_reset__SERVER();
$PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php';

View File

@ -101,6 +101,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$this->reset_post_types();
$this->reset_taxonomies();
$this->reset_post_statuses();
$this->reset__SERVER();
if ( $wp_rewrite->permalink_structure ) {
$this->set_permalink_structure( '' );
@ -185,6 +186,13 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
}
/**
* Reset `$_SERVER` variables
*/
protected function reset__SERVER() {
tests_reset__SERVER();
}
/**
* Saves the action and filter-related globals so they can be restored later.
*

View File

@ -1028,8 +1028,6 @@ class Tests_Multisite_Site extends WP_UnitTestCase {
$is_ssl = is_ssl();
$address = parse_url( get_blogaddress_by_id( $blog ), PHP_URL_SCHEME );
unset( $_SERVER['HTTPS'] );
$this->assertTrue( $is_ssl );
$this->assertSame( 'http', $address );
}

View File

@ -296,15 +296,10 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
// Save server data for cleanup.
$is_ssl = is_ssl();
$_SERVER['HTTPS'] = 'off';
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
}
@ -326,15 +321,10 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
// Save server data for cleanup.
$is_ssl = is_ssl();
$_SERVER['HTTPS'] = 'off';
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
}
@ -356,10 +346,6 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID
$attachment_id = $this->_make_attachment( $upload );
// Save server data for cleanup
$is_ssl = is_ssl();
$http_host = $_SERVER['HTTP_HOST'];
$_SERVER['HTTPS'] = 'on';
// Ensure that server host matches the host of wp_upload_dir().
@ -369,10 +355,6 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Test that wp_get_attachemt_url returns with https scheme.
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
$_SERVER['HTTP_HOST'] = $http_host;
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
}
@ -392,10 +374,6 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID.
$attachment_id = $this->_make_attachment( $upload );
// Save server data for cleanup.
$is_ssl = is_ssl();
$http_host = $_SERVER['HTTP_HOST'];
$_SERVER['HTTPS'] = 'on';
// Ensure that server host matches the host of wp_upload_dir().
@ -405,10 +383,6 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Test that wp_get_attachemt_url returns with https scheme.
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
$_SERVER['HTTP_HOST'] = $http_host;
$this->assertSame( 'https', parse_url( $url, PHP_URL_SCHEME ) );
}
@ -428,14 +402,12 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID
$attachment_id = $this->_make_attachment( $upload );
$is_ssl = is_ssl();
$_SERVER['HTTPS'] = 'on';
set_current_screen( 'dashboard' );
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
set_current_screen( 'front' );
$this->assertSame( set_url_scheme( $url, 'http' ), $url );
@ -457,14 +429,12 @@ class Tests_Post_Attachments extends WP_UnitTestCase {
// Set attachment ID
$attachment_id = $this->_make_attachment( $upload );
$is_ssl = is_ssl();
$_SERVER['HTTPS'] = 'on';
set_current_screen( 'dashboard' );
$url = wp_get_attachment_url( $attachment_id );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
set_current_screen( 'front' );
remove_filter( 'upload_dir', '_upload_dir_https' );

View File

@ -285,10 +285,6 @@ class Tests_REST_API extends WP_UnitTestCase {
* @ticket 34299
*/
public function test_rest_url_scheme() {
if ( isset( $_SERVER['HTTPS'] ) ) {
$_https = $_SERVER['HTTPS'];
}
$_name = $_SERVER['SERVER_NAME'];
$_SERVER['SERVER_NAME'] = parse_url( home_url(), PHP_URL_HOST );
$_siteurl = get_option( 'siteurl' );
@ -321,12 +317,6 @@ class Tests_REST_API extends WP_UnitTestCase {
$this->assertSame( 'http', parse_url( $url, PHP_URL_SCHEME ) );
// Reset
if ( isset( $_https ) ) {
$_SERVER['HTTPS'] = $_https;
} else {
unset( $_SERVER['HTTPS'] );
}
$_SERVER['SERVER_NAME'] = $_name;
update_option( 'siteurl', $_siteurl );
set_current_screen( 'front' );

View File

@ -103,9 +103,6 @@ class Tests_Rewrite extends WP_UnitTestCase {
}
function test_url_to_postid_set_url_scheme_http_to_https() {
// Save server data for cleanup
$is_ssl = is_ssl();
$_SERVER['HTTPS'] = 'on';
$post_id = self::factory()->post->create();
@ -116,9 +113,6 @@ class Tests_Rewrite extends WP_UnitTestCase {
$page_permalink = get_permalink( $page_id );
$page_url_to_id = url_to_postid( set_url_scheme( $page_permalink, 'http' ) );
// Cleanup.
$_SERVER['HTTPS'] = $is_ssl ? 'on' : 'off';
$this->assertEquals( $post_id, $post_url_to_id );
$this->assertEquals( $page_id, $page_url_to_id );
}