mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Fix incorrect BASE_DIR inferred in CLI
BUG Fix Director::mockRequest() mocking incorrect $url Fixes #7689
This commit is contained in:
parent
e4bf9a31ed
commit
c5bd9bb424
@ -263,7 +263,7 @@ class Director implements TemplateGlobalProvider
|
|||||||
$newVars = HTTPRequestBuilder::cleanEnvironment($newVars);
|
$newVars = HTTPRequestBuilder::cleanEnvironment($newVars);
|
||||||
|
|
||||||
// Create new request
|
// Create new request
|
||||||
$request = HTTPRequestBuilder::createFromVariables($newVars, $body);
|
$request = HTTPRequestBuilder::createFromVariables($newVars, $body, ltrim($url, '/'));
|
||||||
if ($headers) {
|
if ($headers) {
|
||||||
foreach ($headers as $k => $v) {
|
foreach ($headers as $k => $v) {
|
||||||
$request->addHeader($k, $v);
|
$request->addHeader($k, $v);
|
||||||
|
@ -28,10 +28,13 @@ class HTTPRequestBuilder
|
|||||||
*
|
*
|
||||||
* @param array $variables
|
* @param array $variables
|
||||||
* @param string $input Request body
|
* @param string $input Request body
|
||||||
|
* @param string|null $url Provide specific url (relative to base)
|
||||||
* @return HTTPRequest
|
* @return HTTPRequest
|
||||||
*/
|
*/
|
||||||
public static function createFromVariables(array $variables, $input)
|
public static function createFromVariables(array $variables, $input, $url = null)
|
||||||
{
|
{
|
||||||
|
// Infer URL from REQUEST_URI unless explicitly provided
|
||||||
|
if (!isset($url)) {
|
||||||
// Remove query parameters (they're retained separately through $server['_GET']
|
// Remove query parameters (they're retained separately through $server['_GET']
|
||||||
$url = parse_url($variables['_SERVER']['REQUEST_URI'], PHP_URL_PATH);
|
$url = parse_url($variables['_SERVER']['REQUEST_URI'], PHP_URL_PATH);
|
||||||
|
|
||||||
@ -39,6 +42,7 @@ class HTTPRequestBuilder
|
|||||||
if (substr(strtolower($url), 0, strlen(BASE_URL)) === strtolower(BASE_URL)) {
|
if (substr(strtolower($url), 0, strlen(BASE_URL)) === strtolower(BASE_URL)) {
|
||||||
$url = substr($url, strlen(BASE_URL));
|
$url = substr($url, strlen(BASE_URL));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
$request = new HTTPRequest(
|
$request = new HTTPRequest(
|
||||||
|
@ -95,6 +95,11 @@ if (!defined('BASE_URL')) {
|
|||||||
return rtrim(parse_url($base, PHP_URL_PATH), '/');
|
return rtrim(parse_url($base, PHP_URL_PATH), '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Unless specified, use empty string for base in CLI
|
||||||
|
if (in_array(php_sapi_name(), ['cli', 'phpdbg'])) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
|
// Determine the base URL by comparing SCRIPT_NAME to SCRIPT_FILENAME and getting common elements
|
||||||
// This tends not to work on CLI
|
// This tends not to work on CLI
|
||||||
$path = realpath($_SERVER['SCRIPT_FILENAME']);
|
$path = realpath($_SERVER['SCRIPT_FILENAME']);
|
||||||
|
@ -929,4 +929,30 @@ class DirectorTest extends SapphireTest
|
|||||||
{
|
{
|
||||||
$this->assertTrue(Director::is_cli(), 'is_cli should be true for PHP CLI and phpdbg');
|
$this->assertTrue(Director::is_cli(), 'is_cli should be true for PHP CLI and phpdbg');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMockRequest()
|
||||||
|
{
|
||||||
|
Director::config()->set('alternate_base_url', 'http://www.mysite.com/some-subdir/');
|
||||||
|
|
||||||
|
// Can handle root-relative $url
|
||||||
|
Director::mockRequest(function (HTTPRequest $request) {
|
||||||
|
$this->assertEquals('some-page/nested', $request->getURL());
|
||||||
|
$this->assertEquals(1, $request->getVar('query'));
|
||||||
|
$this->assertEquals('/some-subdir/some-page/nested', $_SERVER['REQUEST_URI']);
|
||||||
|
}, '/some-subdir/some-page/nested?query=1');
|
||||||
|
|
||||||
|
// Can handle absolute $url
|
||||||
|
Director::mockRequest(function (HTTPRequest $request) {
|
||||||
|
$this->assertEquals('some-page/nested', $request->getURL());
|
||||||
|
$this->assertEquals(1, $request->getVar('query'));
|
||||||
|
$this->assertEquals('/some-subdir/some-page/nested', $_SERVER['REQUEST_URI']);
|
||||||
|
}, 'http://www.mysite.com/some-subdir/some-page/nested?query=1');
|
||||||
|
|
||||||
|
// Can handle relative $url
|
||||||
|
Director::mockRequest(function (HTTPRequest $request) {
|
||||||
|
$this->assertEquals('some-page/nested', $request->getURL());
|
||||||
|
$this->assertEquals(1, $request->getVar('query'));
|
||||||
|
$this->assertEquals('/some-subdir/some-page/nested', $_SERVER['REQUEST_URI']);
|
||||||
|
}, 'some-page/nested?query=1');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user