BUG Fix incorrect BASE_DIR inferred in CLI

BUG Fix Director::mockRequest() mocking incorrect $url
Fixes #7689
This commit is contained in:
Damian Mooyman 2017-12-14 17:42:00 +13:00
parent e4bf9a31ed
commit c5bd9bb424
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
4 changed files with 42 additions and 7 deletions

View File

@ -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);

View File

@ -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(

View File

@ -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']);

View File

@ -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');
}
} }