Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] FrontendCommandController: Fix behaviour with absolute URLs in makeAbsolute method #536

Merged
merged 11 commits into from
Aug 13, 2017
23 changes: 20 additions & 3 deletions Classes/Command/FrontendCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Helhum\Typo3Console\Mvc\Controller\CommandController;
use Symfony\Component\Process\PhpProcess;
use TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Response;
use TYPO3\CMS\Core\Utility\GeneralUtility;

/**
* Class SchedulerCommandController
Expand Down Expand Up @@ -61,14 +62,30 @@ public function requestCommand($requestUrl)
*/
protected function makeAbsolute($url)
{
// Check if the URL is valid
// If so return the URL directly
if (GeneralUtility::isValidUrl($url)) {
return $url;
}

// return URL with open scheme with proper http://
if (preg_match('/^\/\//', $url) && GeneralUtility::isValidUrl('http://' . ltrim($url, '//'))) {
return 'http://' . ltrim($url, '//');
}

$parsedUrl = parse_url($url);
$finalUrl = '';
if (!isset($parsedUrl['scheme'])) {
$finalUrl .= 'http://';

$finalUrl = 'http://';
// prepend trailing slash if missing
if (!preg_match('/^\/.*$/', $parsedUrl['path'])) {
$url = '/' . $url;
}

// if there is just a relative path
if (!isset($parsedUrl['host'])) {
$finalUrl .= 'localhost';
}

return $finalUrl . '/' . ltrim($url, '/');
}
}
61 changes: 61 additions & 0 deletions Tests/Unit/Command/FrontendCommandControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
namespace Helhum\Typo3Console\Tests\Unit\Command;

use Helhum\Typo3Console\Command\FrontendCommandController;
use Nimut\TestingFramework\TestCase\UnitTestCase;

class FrontendCommandControllerTest extends UnitTestCase
{
/**
* Call protected/private method of a class.
*
* @param object &$object Instantiated object that we will run method on.
* @param string $methodName Method name to call
* @param array $parameters Array of parameters to pass into method.
*
* @return mixed Method return.
*/
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);

return $method->invokeArgs($object, $parameters);
}

/**
* @test
* @dataProvider makeAbsoluteDataProvider
* @param mixed $expected
* @param mixed $given
*/
public function makeAbsoluteTest($expected, $given)
{
/** @var FrontendCommandController $frontendRequest */
$frontendRequest = new FrontendCommandController();
$parameter = [ $given ];
$this->assertEquals($expected, $this->invokeMethod($frontendRequest, 'makeAbsolute', $parameter));
}

/**
* @return array
*/
public function makeAbsoluteDataProvider()
{
return [
'Relative path to home' => ['http://localhost/', '/'],
'Relative path to foo without trailing slash' => ['http://localhost/foo', '/foo'],
'Relative path to foo without leading slash' => ['http://localhost/foo', 'foo'],
'Relative path to foo with trailing slash' => ['http://localhost/foo/', '/foo/'],
'Test with HTTP scheme' => ['http://typo3.org/', 'http://typo3.org/'],
'Test with HTTPS scheme' => ['https://typo3.org/', 'https://typo3.org/'],
'Test with open scheme' => ['http://typo3.org/', '//typo3.org/'],
'No scheme but domain' => ['http://localhost/typo3.org', 'typo3.org'],
'No scheme but domain with trailing slash' => ['http://localhost/typo3.org/', 'typo3.org/'],
'No scheme but domain with path without trailing slash' => ['http://localhost/typo3.org/foo', 'typo3.org/foo'],
'No scheme but domain with path and trailing slash' => ['http://localhost/typo3.org/foo/', 'typo3.org/foo/'],
'Path with query string' => ['http://localhost/foo/?foo=bar&bla=baz', '/foo/?foo=bar&bla=baz'],
];
}
}