Commit 4047ad0a authored by Tino Goratsch's avatar Tino Goratsch

- type hint against the ContainerInterface in most places

- covered the RewriteInternalUri helper class with unit tests
- minor fixes and improvements
parent a701f0e4
......@@ -4,7 +4,7 @@ namespace ACP3\Core;
use ACP3\Core\Controller\AreaEnum;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* @package ACP3\Core
......@@ -31,7 +31,7 @@ class Breadcrumb
protected $breadcrumbCache = [];
/**
* @var \Symfony\Component\DependencyInjection\Container
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
......@@ -50,13 +50,13 @@ class Breadcrumb
/**
* Breadcrumb constructor.
*
* @param \Symfony\Component\DependencyInjection\Container $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
*/
public function __construct(
Container $container,
ContainerInterface $container,
Translator $translator,
RequestInterface $request,
RouterInterface $router
......
......@@ -2,6 +2,7 @@
namespace ACP3\Core\Helpers;
use ACP3\Core\Http\Request;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Core\Validation\ValidationRules\DateValidationRule;
......@@ -20,7 +21,7 @@ class Date
*/
protected $translator;
/**
* @var \ACP3\Core\Http\Request
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
......@@ -37,14 +38,14 @@ class Date
*
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\Request $request
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\Helpers\Forms $formsHelper
* @param \ACP3\Core\Validation\ValidationRules\DateValidationRule $dateValidationRule
*/
public function __construct(
\ACP3\Core\Date $date,
Translator $translator,
Request $request,
RequestInterface $request,
Forms $formsHelper,
DateValidationRule $dateValidationRule
) {
......@@ -74,7 +75,7 @@ class Date
'Australia' => \DateTimeZone::listIdentifiers(\DateTimeZone::AUSTRALIA),
'Europe' => \DateTimeZone::listIdentifiers(\DateTimeZone::EUROPE),
'Indian' => \DateTimeZone::listIdentifiers(\DateTimeZone::INDIAN),
'Pacitic' => \DateTimeZone::listIdentifiers(\DateTimeZone::PACIFIC),
'Pacific' => \DateTimeZone::listIdentifiers(\DateTimeZone::PACIFIC),
'UTC' => \DateTimeZone::listIdentifiers(\DateTimeZone::UTC),
];
......@@ -82,8 +83,11 @@ class Date
$i = 0;
foreach ($values as $row) {
unset($timeZones[$key][$i]);
$timeZones[$key][$row]['selected'] = $this->formsHelper->selectEntry('date_time_zone', $row,
$currentValue);
$timeZones[$key][$row]['selected'] = $this->formsHelper->selectEntry(
'date_time_zone',
$row,
$currentValue
);
++$i;
}
}
......
......@@ -19,7 +19,7 @@ class RewriteInternalUri
*/
protected $controllerActionExists;
/**
* @var \ACP3\Core\Http\Request
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
......@@ -36,14 +36,14 @@ class RewriteInternalUri
*
* @param \ACP3\Core\Environment\ApplicationPath $appPath
* @param \ACP3\Core\Modules\Helper\ControllerActionExists $controllerActionExists
* @param \ACP3\Core\Http\Request $request
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Modules\ACP3\Seo\Validation\ValidationRules\UriAliasValidationRule $uriAliasValidationRule
*/
public function __construct(
Core\Environment\ApplicationPath $appPath,
Core\Modules\Helper\ControllerActionExists $controllerActionExists,
Core\Http\Request $request,
Core\Http\RequestInterface $request,
Core\RouterInterface $router,
UriAliasValidationRule $uriAliasValidationRule
) {
......@@ -55,8 +55,6 @@ class RewriteInternalUri
}
/**
* Ersetzt interne ACP3 interne URIs in Texten mit ihren jeweiligen Aliasen
*
* @param string $text
*
* @return string
......@@ -65,42 +63,51 @@ class RewriteInternalUri
{
$rootDir = str_replace('/', '\/', $this->appPath->getWebRoot());
$host = $this->request->getServer()->get('HTTP_HOST');
$pattern = '/<a([^>]+)href="(http(s?):\/\/' . $host . ')?(' . $rootDir . ')?(index\.php)?(\/?)((?i:[a-z\d_\-]+\/){2,})"/i';
return preg_replace_callback(
'/<a([^>]+)href="(http(s?):\/\/' . $host . ')?(' . $rootDir . ')?(index\.php)?(\/?)((?i:[a-z\d_\-]+\/){2,})"/i',
$pattern,
[$this, "rewriteInternalUriCallback"],
$text
);
}
/**
* Callback-Funktion zum Ersetzen der ACP3 internen URIs gegen ihre Aliase
*
* @param array $matches
*
* @return string
*/
private function rewriteInternalUriCallback(array $matches)
{
if ($this->uriAliasValidationRule->isValid($matches[7]) === true) {
return $matches[0];
} else {
$uriArray = explode('/', $matches[7]);
$path = 'frontend/' . $uriArray[0];
if (!empty($uriArray[1])) {
$path .= '/' . $uriArray[1];
}
if (!empty($uriArray[2])) {
$path .= '/' . $uriArray[2];
}
if (!empty($uriArray[3])) {
$path .= '/' . $uriArray[3];
}
if ($this->uriAliasValidationRule->isValid($matches[7]) !== true) {
$resourceParts = explode('/', $matches[7]);
$path = $this->getResourcePath($resourceParts);
if ($this->controllerActionExists->controllerActionExists($path) === true) {
return '<a' . $matches[1] . 'href="' . $this->router->route($matches[7]) . '"';
} else {
return $matches[0];
}
}
return $matches[0];
}
/**
* @param array $resourceParts
*
* @return string
*/
private function getResourcePath(array $resourceParts)
{
$path = 'frontend/' . $resourceParts[0];
if (!empty($resourceParts[1])) {
$path .= '/' . $resourceParts[1];
}
if (!empty($resourceParts[2])) {
$path .= '/' . $resourceParts[2];
}
if (!empty($resourceParts[3])) {
$path .= '/' . $resourceParts[3];
}
return $path;
}
}
<?php
/**
* Copyright (c) 2016 by the ACP3 Developers.
* See the LICENCE file at the top-level module directory for licencing details.
*/
namespace ACP3\Core\Test\Helpers\Formatter;
use ACP3\Core\Environment\ApplicationPath;
use ACP3\Core\Helpers\Formatter\RewriteInternalUri;
use ACP3\Core\Http\Request;
use ACP3\Core\Modules\Helper\ControllerActionExists;
use ACP3\Modules\ACP3\Seo\Core\Router;
use ACP3\Modules\ACP3\Seo\Validation\ValidationRules\UriAliasValidationRule;
use Symfony\Component\HttpFoundation\ServerBag;
class RewriteInternalUriTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Helpers\Formatter\RewriteInternalUri
*/
private $rewriteInternalUri;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $appPathMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $controllerActionExistsMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $requestMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $routerMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $uriAliasValidationRuleMock;
protected function setUp()
{
$this->initializeMockObjects();
$this->rewriteInternalUri = new RewriteInternalUri(
$this->appPathMock,
$this->controllerActionExistsMock,
$this->requestMock,
$this->routerMock,
$this->uriAliasValidationRuleMock
);
}
private function initializeMockObjects()
{
$this->appPathMock = $this->getMockBuilder(ApplicationPath::class)
->disableOriginalConstructor()
->getMock();
$this->controllerActionExistsMock = $this->getMockBuilder(ControllerActionExists::class)
->disableOriginalConstructor()
->getMock();
$this->requestMock = $this->getMockBuilder(Request::class)
->disableOriginalConstructor()
->getMock();
$this->routerMock = $this->getMockBuilder(Router::class)
->disableOriginalConstructor()
->getMock();
$this->uriAliasValidationRuleMock = $this->getMockBuilder(UriAliasValidationRule::class)
->disableOriginalConstructor()
->getMock();
}
public function testRewriteInternalUriWithNotMatchingUrl()
{
$this->setUpAppPathExpectations();
$this->setUpRequestMockExpectations();
$this->setUpValidationRuleMockExpectations(0, '', false);
$this->setUpControllerActionExistsMockExpectations(0, '', true);
$this->setUpRouterMockExpectations(0, '', '');
$content = <<<HTML
<p>Test</p>
<p><a href="http://google.com">www.google.com</a></p>
HTML;
$this->assertEquals($content, $this->rewriteInternalUri->rewriteInternalUri($content));
}
public function testRewriteInternalUriWithMatchingExternalUri()
{
$this->setUpAppPathExpectations();
$this->setUpRequestMockExpectations();
$this->setUpValidationRuleMockExpectations(1, 'foo/bar/baz/', false);
$this->setUpControllerActionExistsMockExpectations(1, 'frontend/foo/bar/baz', false);
$this->setUpRouterMockExpectations(0, '', '');
$content = <<<HTML
<p>Test</p>
<p><a href="http://example.com/foo/bar/baz/">www.example.com</a></p>
HTML;
$this->assertEquals($content, $this->rewriteInternalUri->rewriteInternalUri($content));
}
public function testRewriteInternalUriWithMatchingUriAndExistingAlias()
{
$this->setUpAppPathExpectations();
$this->setUpRequestMockExpectations();
$this->setUpValidationRuleMockExpectations(1, 'foo/bar/baz/', false);
$this->setUpControllerActionExistsMockExpectations(1, 'frontend/foo/bar/baz', true);
$this->setUpRouterMockExpectations(1, 'foo/bar/baz/', '/foo-bar/');
$content = <<<HTML
<p>Test</p>
<p><a href="http://example.com/foo/bar/baz/">www.example.com</a></p>
HTML;
$expected = <<<HTML
<p>Test</p>
<p><a href="/foo-bar/">www.example.com</a></p>
HTML;
$this->assertEquals($expected, $this->rewriteInternalUri->rewriteInternalUri($content));
}
private function setUpAppPathExpectations()
{
$this->appPathMock->expects($this->once())
->method('getWebRoot')
->willReturn('/');
}
/**
* @param string $httpHost
*/
private function setUpRequestMockExpectations($httpHost = 'example.com')
{
$this->requestMock->expects($this->once())
->method('getServer')
->willReturn(new ServerBag([
'HTTP_HOST' => $httpHost
]));
}
/**
* @param int $callCount
* @param string $uri
* @param bool $isValid
*/
private function setUpValidationRuleMockExpectations($callCount, $uri, $isValid)
{
$this->uriAliasValidationRuleMock->expects($this->exactly($callCount))
->method('isValid')
->with($uri)
->willReturn($isValid);
}
/**
* @param int $callCount
* @param string $path
* @param bool $isControllerAction
*/
private function setUpControllerActionExistsMockExpectations($callCount, $path, $isControllerAction)
{
$this->controllerActionExistsMock->expects($this->exactly($callCount))
->method('controllerActionExists')
->with($path)
->willReturn($isControllerAction);
}
/**
* @param int $callCount
* @param string $route
* @param string $alias
*/
private function setUpRouterMockExpectations($callCount, $route, $alias)
{
$this->routerMock->expects($this->exactly($callCount))
->method('route')
->with($route)
->willReturn($alias);
}
}
......@@ -26,7 +26,7 @@ class Helpers
*/
protected $secureHelper;
/**
* @var \ACP3\Core\Http\Request
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
......@@ -46,7 +46,7 @@ class Helpers
* Helpers constructor.
*
* @param \ACP3\Core\User $user
* @param \ACP3\Core\Http\Request $request
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\Session\SessionHandlerInterface $sessionHandler
* @param \ACP3\Core\View $view
......@@ -54,7 +54,7 @@ class Helpers
*/
public function __construct(
Core\User $user,
Core\Http\Request $request,
Core\Http\RequestInterface $request,
Core\RouterInterface $router,
Core\Session\SessionHandlerInterface $sessionHandler,
Core\View $view,
......
......@@ -4,7 +4,7 @@ namespace ACP3\Modules\ACP3\Menus\Core;
use ACP3\Core;
use ACP3\Core\Http\RequestInterface;
use ACP3\Modules\ACP3\Menus;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Generates the breadcrumb and page title
......@@ -28,22 +28,20 @@ class Breadcrumb extends Core\Breadcrumb
/**
* Breadcrumb constructor.
*
* @param \Symfony\Component\DependencyInjection\Container $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\Config $config
* @param \ACP3\Modules\ACP3\Menus\Model\MenuItemRepository $menuItemRepository
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Modules\ACP3\Menus\Model\MenuItemRepository $menuItemRepository
*/
public function __construct(
Container $container,
ContainerInterface $container,
Core\I18n\Translator $translator,
RequestInterface $request,
Core\RouterInterface $router,
Core\Config $config,
Menus\Model\MenuItemRepository $menuItemRepository
) {
parent::__construct($container, $translator, $request, $router, $config);
parent::__construct($container, $translator, $request, $router);
$this->menuItemRepository = $menuItemRepository;
......
......@@ -20,7 +20,7 @@ class Navbar extends AbstractFunction
*/
protected $menus = [];
/**
* @var \ACP3\Core\Http\Request
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
......@@ -39,13 +39,13 @@ class Navbar extends AbstractFunction
/**
* Navbar constructor.
*
* @param \ACP3\Core\Http\Request $request
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Modules\ACP3\Menus\Model\MenuItemRepository $menuItemRepository
* @param \ACP3\Modules\ACP3\Menus\Cache $menusCache
*/
public function __construct(
Core\Http\Request $request,
Core\Http\RequestInterface $request,
Core\RouterInterface $router,
Menus\Model\MenuItemRepository $menuItemRepository,
Menus\Cache $menusCache
......
......@@ -9,5 +9,4 @@ services:
- '@core.lang'
- '@core.request'
- '@core.router'
- '@core.config'
- '@menus.model.menuitemrepository'
......@@ -23,7 +23,7 @@ class Subscribe
*/
protected $mailer;
/**
* @var \ACP3\Core\Http\Request
* @var \ACP3\Core\Http\RequestInterface
*/
protected $request;
/**
......@@ -57,7 +57,7 @@ class Subscribe
* @param \ACP3\Core\Date $date
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Mailer $mailer
* @param \ACP3\Core\Http\Request $request
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\Helpers\StringFormatter $stringFormatter
* @param \ACP3\Core\Helpers\Secure $secureHelper
......@@ -69,14 +69,14 @@ class Subscribe
Core\Date $date,
Core\I18n\Translator $translator,
Core\Mailer $mailer,
Core\Http\Request $request,
Core\Http\RequestInterface $request,
Core\RouterInterface $router,
Core\Helpers\StringFormatter $stringFormatter,
Core\Helpers\Secure $secureHelper,
Core\Config $config,
AccountStatus $accountStatusHelper,
AccountRepository $accountRepository)
{
AccountRepository $accountRepository
) {
$this->date = $date;
$this->translator = $translator;
$this->mailer = $mailer;
......
......@@ -11,7 +11,7 @@ use ACP3\Core\Config;
use ACP3\Core\Http\RequestInterface;
use ACP3\Core\I18n\Translator;
use ACP3\Core\RouterInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Class Breadcrumb
......@@ -27,14 +27,14 @@ class Breadcrumb extends \ACP3\Core\Breadcrumb
/**
* Breadcrumb constructor.
*
* @param \Symfony\Component\DependencyInjection\Container $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\Config $config
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* @param \ACP3\Core\I18n\Translator $translator
* @param \ACP3\Core\Http\RequestInterface $request
* @param \ACP3\Core\RouterInterface $router
* @param \ACP3\Core\Config $config
*/
public function __construct(
Container $container,
ContainerInterface $container,
Translator $translator,
RequestInterface $request,
RouterInterface $router,
......
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