From 50383673ca9103247a50414954c5189517526c66 Mon Sep 17 00:00:00 2001 From: hmr-it-jr <36242595+hmr-it-jr@users.noreply.github.com> Date: Sat, 4 Jan 2020 01:44:11 +0100 Subject: [PATCH] added api endpoint to return instance specific timesheet config (#1335) --- config/packages/nelmio_api_doc.yaml | 1 + src/API/ConfigurationController.php | 47 +++++++++- src/API/Model/I18n.php | 16 ++-- src/API/Model/TimesheetConfig.php | 101 ++++++++++++++++++++++ symfony.lock | 3 + tests/API/ConfigurationControllerTest.php | 35 +++++++- tests/API/Model/TimesheetConfigTest.php | 46 ++++++++++ 7 files changed, 233 insertions(+), 16 deletions(-) create mode 100644 src/API/Model/TimesheetConfig.php create mode 100644 tests/API/Model/TimesheetConfigTest.php diff --git a/config/packages/nelmio_api_doc.yaml b/config/packages/nelmio_api_doc.yaml index 981d336e..d97b83a8 100644 --- a/config/packages/nelmio_api_doc.yaml +++ b/config/packages/nelmio_api_doc.yaml @@ -26,6 +26,7 @@ nelmio_api_doc: - { alias: TeamEntity, type: App\Entity\Team, groups: [Default, Entity, Team, Team_Entity] } - { alias: TeamCollection, type: App\Entity\Team, groups: [Default, Collection, Team] } - { alias: I18nConfig, type: App\API\Model\I18n, groups: [Default] } + - { alias: TimesheetConfig, type: App\API\Model\TimesheetConfig, groups: [Default] } areas: path_patterns: - ^/api(?!/doc) diff --git a/src/API/ConfigurationController.php b/src/API/ConfigurationController.php index efcdf8a1..8ed79c04 100644 --- a/src/API/ConfigurationController.php +++ b/src/API/ConfigurationController.php @@ -12,7 +12,9 @@ declare(strict_types=1); namespace App\API; use App\API\Model\I18n; +use App\API\Model\TimesheetConfig; use App\Configuration\LanguageFormattings; +use App\Configuration\TimesheetConfiguration; use App\Entity\User; use FOS\RestBundle\Controller\Annotations as Rest; use FOS\RestBundle\View\View; @@ -25,25 +27,31 @@ use Symfony\Component\HttpFoundation\Response; /** * @Security("is_granted('IS_AUTHENTICATED_REMEMBERED')") */ -class ConfigurationController extends BaseApiController +final class ConfigurationController extends BaseApiController { /** * @var ViewHandlerInterface */ - protected $viewHandler; + private $viewHandler; /** * @var LanguageFormattings */ - protected $formats; + private $formats; + /** + * @var TimesheetConfiguration + */ + private $timesheetConfiguration; /** * @param ViewHandlerInterface $viewHandler * @param LanguageFormattings $formats + * @param TimesheetConfiguration $timesheetConfiguration */ - public function __construct(ViewHandlerInterface $viewHandler, LanguageFormattings $formats) + public function __construct(ViewHandlerInterface $viewHandler, LanguageFormattings $formats, TimesheetConfiguration $timesheetConfiguration) { $this->viewHandler = $viewHandler; $this->formats = $formats; + $this->timesheetConfiguration = $timesheetConfiguration; } /** @@ -82,4 +90,35 @@ class ConfigurationController extends BaseApiController return $this->viewHandler->handle($view); } + + /** + * Returns the instance specific timesheet configuration + * + * @SWG\Response( + * response=200, + * description="Returns the instance specific timesheet configuration", + * @SWG\Schema(ref="#/definitions/TimesheetConfig") + * ) + * + * @Rest\Get(path="/config/timesheet") + * + * @ApiSecurity(name="apiUser") + * @ApiSecurity(name="apiToken") + */ + public function timesheetConfigAction(): Response + { + $model = new TimesheetConfig(); + $model + ->setTrackingMode($this->timesheetConfiguration->getTrackingMode()) + ->setDefaultBeginTime($this->timesheetConfiguration->getDefaultBeginTime()) + ->setActiveEntriesHardLimit($this->timesheetConfiguration->getActiveEntriesHardLimit()) + ->setActiveEntriesSoftLimit($this->timesheetConfiguration->getActiveEntriesSoftLimit()) + ->setIsAllowFutureTimes($this->timesheetConfiguration->isAllowFutureTimes()) + ; + + $view = new View($model, 200); + $view->getContext()->setGroups(['Default', 'Config']); + + return $this->viewHandler->handle($view); + } } diff --git a/src/API/Model/I18n.php b/src/API/Model/I18n.php index 43ea485e..b99d896a 100644 --- a/src/API/Model/I18n.php +++ b/src/API/Model/I18n.php @@ -11,49 +11,49 @@ declare(strict_types=1); namespace App\API\Model; -class I18n +final class I18n { /** * Format used for 'begin' and 'end' * * @var string */ - protected $formDateTime = ''; + private $formDateTime = ''; /** * Format used for toolbar queries * * @var string */ - protected $formDate = ''; + private $formDate = ''; /** * Format used to display date-time values (see PHP function date_format) * * @var string */ - protected $dateTime = ''; + private $dateTime = ''; /** * Format used to display date values (see PHP function date_format) * * @var string */ - protected $date = ''; + private $date = ''; /** * Format used to display times (see PHP function date_format) * * @var string */ - protected $time = ''; + private $time = ''; /** * Format used to display durations (replace: %h with hours, %m with minutes, %s with seconds) * * @var string */ - protected $duration = ''; + private $duration = ''; /** * Whether a twenty-four hour format is used (true) or 12-hours AM/PM format (false) * @var bool */ - protected $is24hours = true; + private $is24hours = true; /** * @return string diff --git a/src/API/Model/TimesheetConfig.php b/src/API/Model/TimesheetConfig.php new file mode 100644 index 00000000..3560c728 --- /dev/null +++ b/src/API/Model/TimesheetConfig.php @@ -0,0 +1,101 @@ +trackingMode; + } + + public function setTrackingMode(string $trackingMode): TimesheetConfig + { + $this->trackingMode = $trackingMode; + + return $this; + } + + public function getDefaultBeginTime(): string + { + return $this->defaultBeginTime; + } + + public function setDefaultBeginTime(string $defaultBeginTime): TimesheetConfig + { + $this->defaultBeginTime = $defaultBeginTime; + + return $this; + } + + public function getActiveEntriesHardLimit(): int + { + return $this->activeEntriesHardLimit; + } + + public function setActiveEntriesHardLimit(int $activeEntriesHardLimit): TimesheetConfig + { + $this->activeEntriesHardLimit = $activeEntriesHardLimit; + + return $this; + } + + public function getActiveEntriesSoftLimit(): int + { + return $this->activeEntriesSoftLimit; + } + + public function setActiveEntriesSoftLimit(int $activeEntriesSoftLimit): TimesheetConfig + { + $this->activeEntriesSoftLimit = $activeEntriesSoftLimit; + + return $this; + } + + public function isAllowFutureTimes(): bool + { + return $this->isAllowFutureTimes; + } + + public function setIsAllowFutureTimes(bool $isAllowFutureTimes): TimesheetConfig + { + $this->isAllowFutureTimes = $isAllowFutureTimes; + + return $this; + } +} diff --git a/symfony.lock b/symfony.lock index 154d9783..0179d64f 100644 --- a/symfony.lock +++ b/symfony.lock @@ -318,6 +318,9 @@ "phar-io/version": { "version": "1.0.1" }, + "php": { + "version": "7.2" + }, "php-cs-fixer/diff": { "version": "v1.2.0" }, diff --git a/tests/API/ConfigurationControllerTest.php b/tests/API/ConfigurationControllerTest.php index 9db000fc..9c5bf613 100644 --- a/tests/API/ConfigurationControllerTest.php +++ b/tests/API/ConfigurationControllerTest.php @@ -16,7 +16,7 @@ use App\Entity\User; */ class ConfigurationControllerTest extends APIControllerBaseTest { - public function testIsSecure() + public function testIsI18nSecure() { $this->assertUrlIsSecured('/api/config/i18n'); } @@ -30,16 +30,43 @@ class ConfigurationControllerTest extends APIControllerBaseTest $this->assertIsArray($result); $this->assertNotEmpty($result); $this->assertEquals(7, count($result)); - $this->assertStructure($result); + $this->assertI18nStructure($result); } - protected function assertStructure(array $result) + protected function assertI18nStructure(array $result) { $expectedKeys = ['date', 'dateTime', 'duration', 'formDate', 'formDateTime', 'is24hours', 'time']; $actual = array_keys($result); sort($actual); sort($expectedKeys); - $this->assertEquals($expectedKeys, $actual, 'Activity structure does not match'); + $this->assertEquals($expectedKeys, $actual, 'Config structure does not match'); + } + + public function testIsTimesheetSecure() + { + $this->assertUrlIsSecured('/api/config/timesheet'); + } + + public function testGetTimesheet() + { + $client = $this->getClientForAuthenticatedUser(User::ROLE_USER); + $this->assertAccessIsGranted($client, '/api/config/timesheet', 'GET'); + $result = json_decode($client->getResponse()->getContent(), true); + + $this->assertIsArray($result); + $this->assertNotEmpty($result); + $this->assertEquals(5, count($result)); + $this->assertTimesheetStructure($result); + } + + protected function assertTimesheetStructure(array $result) + { + $expectedKeys = ['activeEntriesHardLimit', 'activeEntriesSoftLimit', 'defaultBeginTime', 'isAllowFutureTimes', 'trackingMode']; + $actual = array_keys($result); + sort($actual); + sort($expectedKeys); + + $this->assertEquals($expectedKeys, $actual, 'Config structure does not match'); } } diff --git a/tests/API/Model/TimesheetConfigTest.php b/tests/API/Model/TimesheetConfigTest.php new file mode 100644 index 00000000..a238d469 --- /dev/null +++ b/tests/API/Model/TimesheetConfigTest.php @@ -0,0 +1,46 @@ +assertTrue($sut->isAllowFutureTimes()); + $this->assertEquals('now', $sut->getDefaultBeginTime()); + $this->assertEquals('default', $sut->getTrackingMode()); + $this->assertEquals(1, $sut->getActiveEntriesSoftLimit()); + $this->assertEquals(1, $sut->getActiveEntriesHardLimit()); + } + + public function testSetter() + { + $sut = new TimesheetConfig(); + + $this->assertInstanceOf(TimesheetConfig::class, $sut->setIsAllowFutureTimes(false)); + $this->assertInstanceOf(TimesheetConfig::class, $sut->setDefaultBeginTime('08:00')); + $this->assertInstanceOf(TimesheetConfig::class, $sut->setTrackingMode('punch')); + $this->assertInstanceOf(TimesheetConfig::class, $sut->setActiveEntriesSoftLimit(2)); + $this->assertInstanceOf(TimesheetConfig::class, $sut->setActiveEntriesHardLimit(3)); + + $this->assertFalse($sut->isAllowFutureTimes()); + $this->assertEquals('08:00', $sut->getDefaultBeginTime()); + $this->assertEquals('punch', $sut->getTrackingMode()); + $this->assertEquals(2, $sut->getActiveEntriesSoftLimit()); + $this->assertEquals(3, $sut->getActiveEntriesHardLimit()); + } +}