Commit 5f7e2d03 authored by Tino Goratsch's avatar Tino Goratsch

add a check for the homepage when generating routes

parent 70be3bbd
Pipeline #19147033 passed with stages
in 8 minutes and 6 seconds
......@@ -10,7 +10,7 @@ namespace ACP3\Core\Router;
use ACP3\Core\Environment\ApplicationPath;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\Settings\SettingsInterface;
use ACP3\Modules\ACP3\System;
use ACP3\Modules\ACP3\System\Installer\Schema;
class Router implements RouterInterface
{
......@@ -59,6 +59,10 @@ class Router implements RouterInterface
}
}
if ($path === '/') {
$path = '';
}
return $this->addUriPrefix($path, $isAbsolute, $isSecure) . $path;
}
......@@ -98,6 +102,10 @@ class Router implements RouterInterface
}
}
if ($this->isHomepage($path) === true) {
$path = '/';
}
return $path;
}
......@@ -126,7 +134,11 @@ class Router implements RouterInterface
$prefix .= $this->request->getHost();
}
$prefix .= $this->useModRewrite($path) ? $this->appPath->getWebRoot() : $this->appPath->getPhpSelf() . '/';
if ($this->useModRewrite($path) || $path === '') {
$prefix .= $this->appPath->getWebRoot();
} else {
$prefix .= $this->appPath->getPhpSelf() . '/';
}
return $prefix;
}
......@@ -156,7 +168,17 @@ class Router implements RouterInterface
*/
protected function useModRewrite(string $path): bool
{
return (bool) $this->config->getSettings(System\Installer\Schema::MODULE_NAME)['mod_rewrite'] === true &&
$this->isAdminUri($path) === false;
return (bool) $this->getSystemSettings()['mod_rewrite'] === true
&& $this->isAdminUri($path) === false;
}
protected function getSystemSettings(): array
{
return $this->config->getSettings(Schema::MODULE_NAME);
}
protected function isHomepage(string $path): bool
{
return $path === $this->getSystemSettings()['homepage'];
}
}
......@@ -44,18 +44,9 @@ class RouterTest extends \PHPUnit_Framework_TestCase
protected function initializeMockObjects()
{
$this->requestMock = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->setMethods(['getScheme', 'getHost'])
->getMock();
$this->appPathMock = $this->getMockBuilder(ApplicationPath::class)
->disableOriginalConstructor()
->setMethods(['getWebRoot', 'getPhpSelf'])
->getMock();
$this->configMock = $this->getMockBuilder(SettingsInterface::class)
->disableOriginalConstructor()
->setMethods(['getSettings', 'saveSettings'])
->getMock();
$this->requestMock = $this->createMock(Request::class);
$this->appPathMock = $this->createMock(ApplicationPath::class);
$this->configMock = $this->createMock(SettingsInterface::class);
}
public function testRouteUseNoModRewrite()
......@@ -84,7 +75,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
* @param int $callCountWebRoot
* @param int $callCountPhpSelf
*/
protected function setAppPathMockExpectations($callCountWebRoot, $callCountPhpSelf)
protected function setAppPathMockExpectations(int $callCountWebRoot, int $callCountPhpSelf)
{
$this->appPathMock->expects($this->exactly($callCountWebRoot))
->method('getWebRoot')
......@@ -95,22 +86,21 @@ class RouterTest extends \PHPUnit_Framework_TestCase
}
/**
* @param int $callCount
* @param bool $useModRewrite
*/
protected function setUpConfigMockExpectations($callCount = 1, $useModRewrite = false)
protected function setUpConfigMockExpectations(bool $useModRewrite = false)
{
$this->configMock->expects($this->exactly($callCount))
$this->configMock->expects($this->atLeastOnce())
->method('getSettings')
->with('system')
->willReturn(['mod_rewrite' => $useModRewrite]);
->willReturn(['mod_rewrite' => $useModRewrite, 'homepage' => 'foo/bar/baz/']);
}
public function testRouteUseModRewrite()
{
$this->setUpRequestMockExpectations();
$this->setAppPathMockExpectations(1, 0);
$this->setUpConfigMockExpectations(1, true);
$this->setUpConfigMockExpectations(true);
$path = 'news/index/index/';
$expected = '/' . $path;
......@@ -158,7 +148,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
{
$this->setUpRequestMockExpectations();
$this->setAppPathMockExpectations(0, 1);
$this->setUpConfigMockExpectations(1, true);
$this->setUpConfigMockExpectations(true);
$path = 'acp/news/index/index/';
$expected = '/index.php/' . $path;
......@@ -213,4 +203,31 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->router->route($path));
}
/**
* @dataProvider homepageRouteDataProvider()
*
* @param string $path
* @param bool $absolute
* @param bool|null $isSecure
* @param string $expected
*/
public function testRouteIsHomepage(string $path, bool $absolute, ?bool $isSecure, string $expected)
{
$this->setUpRequestMockExpectations();
$this->setAppPathMockExpectations(1, 0);
$this->setUpConfigMockExpectations();
$this->assertEquals($expected, $this->router->route($path, $absolute, $isSecure));
}
public function homepageRouteDataProvider(): array
{
return [
['foo/bar/baz', false, null, '/'],
['foo/bar/baz', true, null, 'http://example.com/'],
['foo/bar/baz', true, false, 'http://example.com/'],
['foo/bar/baz', true, true, 'https://example.com/'],
];
}
}
......@@ -55,7 +55,7 @@ class Aliases
$this->aliasesCache = $this->seoCache->getCache();
}
$path .= !\preg_match('/\/$/', $path) ? '/' : '';
$path .= (!\preg_match('/\/$/', $path) ? '/' : '');
return $this->aliasesCache[$path]['alias'] ?? ($emptyOnNoResult === true ? '' : $path);
}
......
......@@ -28,14 +28,8 @@ class AliasesTest extends \PHPUnit_Framework_TestCase
protected function setUp()
{
$this->seoCacheMock = $this->getMockBuilder(Cache::class)
->disableOriginalConstructor()
->setMethods(['getCache'])
->getMock();
$this->modulesMock = $this->getMockBuilder(Modules::class)
->disableOriginalConstructor()
->setMethods(['isActive'])
->getMock();
$this->seoCacheMock = $this->createMock(Cache::class);
$this->modulesMock = $this->createMock(Modules::class);
$this->aliases = new Aliases($this->modulesMock, $this->seoCacheMock);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment