diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | config/config.default.php | 3 | ||||
-rw-r--r-- | frontend/js/forms.js | 4 | ||||
-rw-r--r-- | includes/model/User_model.php | 2 | ||||
-rw-r--r-- | includes/sys_form.php | 10 | ||||
-rw-r--r-- | includes/sys_menu.php | 4 | ||||
-rw-r--r-- | src/Exceptions/Handler.php | 2 | ||||
-rw-r--r-- | src/Routing/LegacyUrlGenerator.php | 30 | ||||
-rw-r--r-- | src/Routing/RoutingServiceProvider.php | 14 | ||||
-rw-r--r-- | src/Routing/UrlGenerator.php | 11 | ||||
-rw-r--r-- | src/Routing/UrlGeneratorInterface.php | 16 | ||||
-rw-r--r-- | src/helpers.php | 12 | ||||
-rw-r--r-- | tests/Unit/Exceptions/HandlerTest.php | 4 | ||||
-rw-r--r-- | tests/Unit/HelpersTest.php | 7 | ||||
-rw-r--r-- | tests/Unit/Routing/LegacyUrlGeneratorTest.php | 54 | ||||
-rw-r--r-- | tests/Unit/Routing/RoutingServiceProviderTest.php | 49 | ||||
-rw-r--r-- | tests/Unit/Routing/UrlGeneratorTest.php | 11 |
17 files changed, 195 insertions, 39 deletions
@@ -22,6 +22,7 @@ _vimrc_local.vim /config/config.php /test/coverage /public/coverage +/coverage # Composer files /vendor/ diff --git a/config/config.default.php b/config/config.default.php index ed40aaf9..c0b00715 100644 --- a/config/config.default.php +++ b/config/config.default.php @@ -43,6 +43,9 @@ return [ '1' => 'Engelsystem dark' ], + // Rewrite URLs with mod_rewrite + 'rewrite_urls' => true, + // Number of News shown on one site 'display_news' => 6, diff --git a/frontend/js/forms.js b/frontend/js/forms.js index 05069be5..13e42d26 100644 --- a/frontend/js/forms.js +++ b/frontend/js/forms.js @@ -16,9 +16,9 @@ global.checkAll = (id, checked) => { * @param {string} id The elements ID * @param {list} shifts_list A list of numbers */ -global.checkOwnTypes = (id, shifts_list) => { +global.checkOwnTypes = (id, shiftsList) => { $('#' + id + ' input[type="checkbox"]').each(function () { - this.checked = $.inArray(parseInt(this.value), shifts_list) != -1; + this.checked = $.inArray(parseInt(this.value), shiftsList) != -1; }); }; diff --git a/includes/model/User_model.php b/includes/model/User_model.php index 5a31e7b8..7ea3ca3e 100644 --- a/includes/model/User_model.php +++ b/includes/model/User_model.php @@ -33,7 +33,7 @@ function User_tshirt_score($user) { WHERE `User`.`UID` = ? AND `Shifts`.`end` < ? GROUP BY `User`.`UID` - ',[ + ', [ $user['UID'], time() ]); diff --git a/includes/sys_form.php b/includes/sys_form.php index 797ba52e..c974c1d1 100644 --- a/includes/sys_form.php +++ b/includes/sys_form.php @@ -154,17 +154,17 @@ function form_multi_checkboxes($names, $label, $items, $selected, $disabled = [] * @param string $label * @param string $selected * @param string $value - * @param string $id + * @param string $html_id * @return string */ -function form_checkbox($name, $label, $selected, $value = 'checked', $id = null) +function form_checkbox($name, $label, $selected, $value = 'checked', $html_id = null) { - if (is_null($id)) { - $id = $name; + if (is_null($html_id)) { + $html_id = $name; } return '<div class="checkbox"><label>' - . '<input type="checkbox" id="' . $id . '" name="' . $name . '" value="' . htmlspecialchars($value) . '" ' + . '<input type="checkbox" id="' . $html_id . '" name="' . $name . '" value="' . htmlspecialchars($value) . '" ' . ($selected ? ' checked="checked"' : '') . ' /> ' . $label . '</label></div>'; diff --git a/includes/sys_menu.php b/includes/sys_menu.php index 85ef1287..fae94de5 100644 --- a/includes/sys_menu.php +++ b/includes/sys_menu.php @@ -89,8 +89,8 @@ function header_toolbar() if (in_array('user_myshifts', $privileges)) { $toolbar_items[] = toolbar_item_link( page_link_to('users', ['action' => 'view']), - ' icon-icon_angel', - $user['Nick'], + '', + '<span class="icon-icon_angel"></span> ' . $user['Nick'], $page == 'users' ); } diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php index f737bdd0..6b3300e6 100644 --- a/src/Exceptions/Handler.php +++ b/src/Exceptions/Handler.php @@ -76,7 +76,7 @@ class Handler protected function terminateApplicationImmediately($message = '') { echo $message; - die(); + die(1); } /** diff --git a/src/Routing/LegacyUrlGenerator.php b/src/Routing/LegacyUrlGenerator.php new file mode 100644 index 00000000..4c1e736b --- /dev/null +++ b/src/Routing/LegacyUrlGenerator.php @@ -0,0 +1,30 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * Provides urls when webserver rewriting is disabled. + * + * The urls have the form <app url>/index.php?p=<path>&<parameters> + */ +class LegacyUrlGenerator extends UrlGenerator +{ + /** + * @param string $path + * @param array $parameters + * @return string urls in the form <app url>/index.php?p=<path>&<parameters> + */ + public function linkTo($path, $parameters = []) + { + $page = ltrim($path, '/'); + if (!empty($page)) { + $page = str_replace('-', '_', $page); + $parameters = array_merge(['p' => $page], $parameters); + } + + $uri = parent::linkTo('index.php', $parameters); + $uri = preg_replace('~(/index\.php)+~', '/index.php', $uri); + + return $uri; + } +} diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php index b7db1383..beaa6a94 100644 --- a/src/Routing/RoutingServiceProvider.php +++ b/src/Routing/RoutingServiceProvider.php @@ -1,14 +1,24 @@ <?php - namespace Engelsystem\Routing; use Engelsystem\Container\ServiceProvider; +/** + * Registers the url generator depending on config. + */ class RoutingServiceProvider extends ServiceProvider { + public function register() { - $urlGenerator = $this->app->make(UrlGenerator::class); + $config = $this->app->get('config'); + $class = UrlGenerator::class; + if (! $config->get('rewrite_urls', true)) { + $class = LegacyUrlGenerator::class; + } + + $urlGenerator = $this->app->make($class); $this->app->instance('routing.urlGenerator', $urlGenerator); + $this->app->bind(UrlGeneratorInterface::class, 'routing.urlGenerator'); } } diff --git a/src/Routing/UrlGenerator.php b/src/Routing/UrlGenerator.php index 6df52425..188bac3b 100644 --- a/src/Routing/UrlGenerator.php +++ b/src/Routing/UrlGenerator.php @@ -2,14 +2,19 @@ namespace Engelsystem\Routing; -class UrlGenerator +/** + * Provides urls when rewriting on the webserver is enabled. (default) + * + * The urls have the form <app url>/<path>?<parameters> + */ +class UrlGenerator implements UrlGeneratorInterface { /** * @param string $path * @param array $parameters - * @return string + * @return string url in the form [app url]/[path]?[parameters] */ - public function to($path, $parameters = []) + public function linkTo($path, $parameters = []) { $path = '/' . ltrim($path, '/'); $request = app('request'); diff --git a/src/Routing/UrlGeneratorInterface.php b/src/Routing/UrlGeneratorInterface.php new file mode 100644 index 00000000..f1a8ffed --- /dev/null +++ b/src/Routing/UrlGeneratorInterface.php @@ -0,0 +1,16 @@ +<?php + +namespace Engelsystem\Routing; + +/** + * To switch between different URL schemes. + */ +interface UrlGeneratorInterface +{ + /** + * @param string $path + * @param array $parameters + * @return string + */ + public function linkTo($path, $parameters = []); +} diff --git a/src/helpers.php b/src/helpers.php index 5a48498a..3f118bf3 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,7 +5,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -14,13 +14,13 @@ use Symfony\Component\HttpFoundation\Session\SessionInterface; * @param string $id * @return mixed */ -function app($id = null) +function app($instance_id = null) { - if (is_null($id)) { + if (is_null($instance_id)) { return Application::getInstance(); } - return Application::getInstance()->get($id); + return Application::getInstance()->get($instance_id); } /** @@ -114,7 +114,7 @@ function session($key = null, $default = null) /** * @param string $path * @param array $parameters - * @return UrlGenerator|string + * @return UrlGeneratorInterface|string */ function url($path = null, $parameters = []) { @@ -124,7 +124,7 @@ function url($path = null, $parameters = []) return $urlGenerator; } - return $urlGenerator->to($path, $parameters); + return $urlGenerator->linkTo($path, $parameters); } /** diff --git a/tests/Unit/Exceptions/HandlerTest.php b/tests/Unit/Exceptions/HandlerTest.php index 40202be8..5a6ffe16 100644 --- a/tests/Unit/Exceptions/HandlerTest.php +++ b/tests/Unit/Exceptions/HandlerTest.php @@ -61,10 +61,10 @@ class HandlerTest extends TestCase /** @var Handler|Mock $handler */ $handler = $this->getMockBuilder(Handler::class) - ->setMethods(['die']) + ->setMethods(['terminateApplicationImmediately']) ->getMock(); $handler->expects($this->once()) - ->method('die'); + ->method('terminateApplicationImmediately'); $handler->setHandler(Handler::ENV_PRODUCTION, $handlerMock); diff --git a/tests/Unit/HelpersTest.php b/tests/Unit/HelpersTest.php index 43c29c84..9514ad8c 100644 --- a/tests/Unit/HelpersTest.php +++ b/tests/Unit/HelpersTest.php @@ -7,7 +7,7 @@ use Engelsystem\Config\Config; use Engelsystem\Container\Container; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; -use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use PHPUnit\Framework\TestCase; use PHPUnit_Framework_MockObject_MockObject as MockObject; use Symfony\Component\HttpFoundation\Session\Session; @@ -171,14 +171,13 @@ class HelpersTest extends TestCase */ public function testUrl() { - $urlGeneratorMock = $this->getMockBuilder(UrlGenerator::class) - ->getMock(); + $urlGeneratorMock = $this->getMockForAbstractClass(UrlGeneratorInterface::class); $this->getAppMock('routing.urlGenerator', $urlGeneratorMock); $this->assertEquals($urlGeneratorMock, url()); $urlGeneratorMock->expects($this->once()) - ->method('to') + ->method('linkTo') ->with('foo/bar', ['param' => 'value']) ->willReturn('http://lorem.ipsum/foo/bar?param=value'); diff --git a/tests/Unit/Routing/LegacyUrlGeneratorTest.php b/tests/Unit/Routing/LegacyUrlGeneratorTest.php new file mode 100644 index 00000000..55b3d721 --- /dev/null +++ b/tests/Unit/Routing/LegacyUrlGeneratorTest.php @@ -0,0 +1,54 @@ +<?php + +namespace Engelsystem\Test\Unit\Routing; + +use Engelsystem\Application; +use Engelsystem\Container\Container; +use Engelsystem\Http\Request; +use Engelsystem\Routing\LegacyUrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; +use PHPUnit\Framework\TestCase; + +class LegacyUrlGeneratorTest extends TestCase +{ + public function provideLinksTo() + { + return [ + ['/', 'http://foo.bar/index.php', [], 'http://foo.bar/index.php'], + ['/foo-path', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo_path'], + ['/foo', 'http://foo.bar/index.php/index.php', [], 'http://foo.bar/index.php?p=foo'], + ['foo', 'http://foo.bar/index.php', ['test' => 'abc'], 'http://foo.bar/index.php?p=foo&test=abc'], + ]; + } + + /** + * @dataProvider provideLinksTo + * @covers \Engelsystem\Routing\LegacyUrlGenerator::linkTo + * + * @param string $urlToPath + * @param string $willReturn + * @param string[] $arguments + * @param string $expectedUrl + */ + public function testLinkTo($urlToPath, $willReturn, $arguments, $expectedUrl) + { + $app = new Container(); + Application::setInstance($app); + + $request = $this->getMockBuilder(Request::class) + ->getMock(); + + $request->expects($this->once()) + ->method('getUriForPath') + ->with('/index.php') + ->willReturn($willReturn); + + $app->instance('request', $request); + + $urlGenerator = new LegacyUrlGenerator(); + $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); + + $url = $urlGenerator->linkTo($urlToPath, $arguments); + $this->assertEquals($expectedUrl, $url); + } +} diff --git a/tests/Unit/Routing/RoutingServiceProviderTest.php b/tests/Unit/Routing/RoutingServiceProviderTest.php index dd9441eb..ce3d7290 100644 --- a/tests/Unit/Routing/RoutingServiceProviderTest.php +++ b/tests/Unit/Routing/RoutingServiceProviderTest.php @@ -2,10 +2,13 @@ namespace Engelsystem\Test\Unit\Routing; +use Engelsystem\Config\Config; +use Engelsystem\Routing\LegacyUrlGenerator; use Engelsystem\Routing\RoutingServiceProvider; use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use Engelsystem\Test\Unit\ServiceProviderTest; -use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_MockObject_MockObject as MockObject; class RoutingServiceProviderTest extends ServiceProviderTest { @@ -14,16 +17,48 @@ class RoutingServiceProviderTest extends ServiceProviderTest */ public function testRegister() { - /** @var PHPUnit_Framework_MockObject_MockObject|UrlGenerator $urlGenerator */ - $urlGenerator = $this->getMockBuilder(UrlGenerator::class) - ->getMock(); + $app = $this->getApp(['make', 'instance', 'bind', 'get']); + /** @var MockObject|Config $config */ + $config = $this->getMockBuilder(Config::class)->getMock(); + /** @var MockObject|UrlGeneratorInterface $urlGenerator */ + $urlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class); + /** @var MockObject|UrlGeneratorInterface $legacyUrlGenerator */ + $legacyUrlGenerator = $this->getMockForAbstractClass(UrlGeneratorInterface::class); - $app = $this->getApp(); + $config->expects($this->atLeastOnce()) + ->method('get') + ->with('rewrite_urls') + ->willReturnOnConsecutiveCalls( + true, + false + ); - $this->setExpects($app, 'make', [UrlGenerator::class], $urlGenerator); - $this->setExpects($app, 'instance', ['routing.urlGenerator', $urlGenerator]); + $this->setExpects($app, 'get', ['config'], $config, $this->atLeastOnce()); + + $app->expects($this->atLeastOnce()) + ->method('make') + ->withConsecutive( + [UrlGenerator::class], + [LegacyUrlGenerator::class] + ) + ->willReturnOnConsecutiveCalls( + $urlGenerator, + $legacyUrlGenerator + ); + $app->expects($this->atLeastOnce()) + ->method('instance') + ->withConsecutive( + ['routing.urlGenerator', $urlGenerator], + ['routing.urlGenerator', $legacyUrlGenerator] + ); + $this->setExpects( + $app, 'bind', + [UrlGeneratorInterface::class, 'routing.urlGenerator'], null, + $this->atLeastOnce() + ); $serviceProvider = new RoutingServiceProvider($app); $serviceProvider->register(); + $serviceProvider->register(); } } diff --git a/tests/Unit/Routing/UrlGeneratorTest.php b/tests/Unit/Routing/UrlGeneratorTest.php index 6da59a4f..2d2efd31 100644 --- a/tests/Unit/Routing/UrlGeneratorTest.php +++ b/tests/Unit/Routing/UrlGeneratorTest.php @@ -6,6 +6,7 @@ use Engelsystem\Application; use Engelsystem\Container\Container; use Engelsystem\Http\Request; use Engelsystem\Routing\UrlGenerator; +use Engelsystem\Routing\UrlGeneratorInterface; use PHPUnit\Framework\TestCase; class UrlGeneratorTest extends TestCase @@ -21,7 +22,7 @@ class UrlGeneratorTest extends TestCase /** * @dataProvider provideLinksTo - * @covers \Engelsystem\Routing\UrlGenerator::to + * @covers \Engelsystem\Routing\UrlGenerator::linkTo * * @param string $path * @param string $willReturn @@ -29,10 +30,9 @@ class UrlGeneratorTest extends TestCase * @param string[] $arguments * @param string $expectedUrl */ - public function testTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) + public function testLinkTo($urlToPath, $path, $willReturn, $arguments, $expectedUrl) { $app = new Container(); - $urlGenerator = new UrlGenerator(); Application::setInstance($app); $request = $this->getMockBuilder(Request::class) @@ -45,7 +45,10 @@ class UrlGeneratorTest extends TestCase $app->instance('request', $request); - $url = $urlGenerator->to($urlToPath, $arguments); + $urlGenerator = new UrlGenerator(); + $this->assertInstanceOf(UrlGeneratorInterface::class, $urlGenerator); + + $url = $urlGenerator->linkTo($urlToPath, $arguments); $this->assertEquals($expectedUrl, $url); } } |