summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2010-11-23 03:08:34 +0000
committerDries Buytaert <dries@buytaert.net>2010-11-23 03:08:34 +0000
commit7aab0ab3680734e2973ce66da5486e1e6f233605 (patch)
tree8c8e5a879e4f38f3e0574210ad53cc1b38b50da9
parent952445f5abb3dcd93a3eb208c6b90f1bb4624d7e (diff)
downloadbrdo-7aab0ab3680734e2973ce66da5486e1e6f233605.tar.gz
brdo-7aab0ab3680734e2973ce66da5486e1e6f233605.tar.bz2
- Patch #557298 by Arancaytar, David_Rothstein: drupal_override_server_variables() does not properly override SCRIPT_NAME.
-rw-r--r--includes/bootstrap.inc22
-rw-r--r--modules/simpletest/tests/bootstrap.test47
2 files changed, 59 insertions, 10 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc
index 3c550b759..9060a0e20 100644
--- a/includes/bootstrap.inc
+++ b/includes/bootstrap.inc
@@ -443,21 +443,23 @@ function conf_path($require_settings = TRUE, $reset = FALSE) {
* @see ip_address()
*/
function drupal_override_server_variables($variables = array()) {
- // Set defaults based on the provided URL.
+ // Allow the provided URL to override any existing values in $_SERVER.
if (isset($variables['url'])) {
$url = parse_url($variables['url']);
+ if (isset($url['host'])) {
+ $_SERVER['HTTP_HOST'] = $url['host'];
+ }
+ if (isset($url['path'])) {
+ $_SERVER['SCRIPT_NAME'] = $url['path'];
+ }
unset($variables['url']);
}
- else {
- $url = array();
- }
- $url += array(
- 'path' => '',
- 'host' => 'localhost',
- );
+ // Define default values for $_SERVER keys. These will be used if $_SERVER
+ // does not already define them and no other values are passed in to this
+ // function.
$defaults = array(
- 'HTTP_HOST' => $url['host'],
- 'SCRIPT_NAME' => $url['path'],
+ 'HTTP_HOST' => 'localhost',
+ 'SCRIPT_NAME' => NULL,
'REMOTE_ADDR' => '127.0.0.1',
'REQUEST_METHOD' => 'GET',
'SERVER_NAME' => NULL,
diff --git a/modules/simpletest/tests/bootstrap.test b/modules/simpletest/tests/bootstrap.test
index 99bdca774..333f34b76 100644
--- a/modules/simpletest/tests/bootstrap.test
+++ b/modules/simpletest/tests/bootstrap.test
@@ -451,3 +451,50 @@ class BootstrapMiscTestCase extends DrupalUnitTestCase {
$this->assertIdentical(drupal_array_merge_deep($link_options_1, $link_options_2), $expected, t('drupal_array_merge_deep() returned a properly merged array.'));
}
}
+
+/**
+ * Tests for overriding server variables via the API.
+ */
+class BootstrapOverrideServerVariablesTestCase extends DrupalUnitTestCase {
+ public static function getInfo() {
+ return array(
+ 'name' => 'Overriding server variables',
+ 'description' => 'Test that drupal_override_server_variables() works correctly.',
+ 'group' => 'Bootstrap',
+ );
+ }
+
+ /**
+ * Test providing a direct URL to to drupal_override_server_variables().
+ */
+ function testDrupalOverrideServerVariablesProvidedURL() {
+ $tests = array(
+ 'http://example.com' => array(
+ 'HTTP_HOST' => 'example.com',
+ 'SCRIPT_NAME' => isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : NULL,
+ ),
+ 'http://example.com/index.php' => array(
+ 'HTTP_HOST' => 'example.com',
+ 'SCRIPT_NAME' => '/index.php',
+ ),
+ 'http://example.com/subdirectory/index.php' => array(
+ 'HTTP_HOST' => 'example.com',
+ 'SCRIPT_NAME' => '/subdirectory/index.php',
+ ),
+ );
+ foreach ($tests as $url => $expected_server_values) {
+ // Remember the original value of $_SERVER, since the function call below
+ // will modify it.
+ $original_server = $_SERVER;
+ // Call drupal_override_server_variables() and ensure that all expected
+ // $_SERVER variables were modified correctly.
+ drupal_override_server_variables(array('url' => $url));
+ foreach ($expected_server_values as $key => $value) {
+ $this->assertIdentical($_SERVER[$key], $value);
+ }
+ // Restore the original value of $_SERVER.
+ $_SERVER = $original_server;
+ }
+ }
+}
+