diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index b6e3739..41cd54a 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -1046,32 +1046,18 @@ protected function initializeSettings(Request $request) { */ protected function initializeRequestGlobals(Request $request) { global $base_url; - // Set and derived from $base_url by this function. global $base_path, $base_root; global $base_secure_url, $base_insecure_url; - - // Create base URL. + $base_path = $request->getBasePath(); + // Requests to 'core/foo.php' bypass the front controller, causing + // $request->getBasePath() to return a string ending with '/core'. In these + // cases, remove '/core' to obtain the correct $base_path. + if (substr($base_path, -5) == '/core') { + $base_path = substr($base_path, 0, strlen($base_path) - 5); + } + $base_path .= '/'; $base_root = $request->getSchemeAndHttpHost(); - $base_url = $base_root; - - // For a request URI of '/index.php/foo', $_SERVER['SCRIPT_NAME'] is - // '/index.php', whereas $_SERVER['PHP_SELF'] is '/index.php/foo'. - if ($dir = rtrim(dirname($request->server->get('SCRIPT_NAME')), '\/')) { - // Remove "core" directory if present, allowing install.php, - // authorize.php, and others to auto-detect a base path. - $core_position = strrpos($dir, '/core'); - if ($core_position !== FALSE && strlen($dir) - 5 == $core_position) { - $base_path = substr($dir, 0, $core_position); - } - else { - $base_path = $dir; - } - $base_url .= $base_path; - $base_path .= '/'; - } - else { - $base_path = '/'; - } + $base_url = rtrim($base_root . $base_path, '\/'); $base_secure_url = str_replace('http://', 'https://', $base_url); $base_insecure_url = str_replace('https://', 'http://', $base_url); } diff --git a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php index 36caa00..eb7b377 100644 --- a/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php @@ -13,6 +13,8 @@ */ class DrupalKernelTest extends UnitTestCase { + protected $backupGlobals = FALSE; + /** * Tests hostname validation with settings. * @@ -134,6 +136,38 @@ public function testFindSitePath() { $this->assertEquals('sites/example', DrupalKernel::findSitePath($request, FALSE, $vfs_root->url('drupal_root'))); } + /** + * @covers ::initializeRequestGlobals + * @dataProvider initializeSettingsProvider + */ + public function testInitializeSettings($path, $url, $expected_base_url, $expected_base_path, $expected_base_root) { + global $base_url; + global $base_path, $base_root; + + $request = $this->prophesize(Request::class); + $request->getBasePath()->willReturn($path); + $request->getSchemeAndHttpHost()->willReturn($url); + $drupalKernel = new DrupalKernel('test', NULL); + $method = new \ReflectionMethod('Drupal\Core\DrupalKernel', 'initializeRequestGlobals'); + $method->setAccessible(TRUE); + $method->invoke($drupalKernel, $request->reveal()); + $this->assertSame($expected_base_path, $base_path); + $this->assertSame($expected_base_url, $base_url); + $this->assertSame($expected_base_root, $base_root); + } + + /** + * Provides data for testInitializeSettings(). + */ + public function initializeSettingsProvider() { + return [ + ['', 'http://localhost', 'http://localhost', '/', 'http://localhost'], + ['/drupal', 'http://localhost', 'http://localhost/drupal', '/drupal/', 'http://localhost'], + ['/core', 'http://localhost', 'http://localhost', '/', 'http://localhost'], + ['/drupal/core', 'http://localhost', 'http://localhost/drupal', '/drupal/', 'http://localhost'], + ]; + } + } }