From eca7d154191cd10f2598d2b447b38289b22cb4df Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Tue, 2 Jul 2024 12:36:13 +0200 Subject: [PATCH] Fix: move state init to a service Signed-off-by: Hamza Mahjoubi --- lib/Controller/ViewController.php | 110 +-------------- lib/Listener/CalendarReferenceListener.php | 85 +----------- lib/Service/CalendarInitialStateService.php | 125 ++++++++++++++++++ .../CalendarInitialStateServiceTest.php} | 43 ++---- 4 files changed, 147 insertions(+), 216 deletions(-) create mode 100644 lib/Service/CalendarInitialStateService.php rename tests/php/unit/{Controller/ViewControllerTest.php => Service/CalendarInitialStateServiceTest.php} (88%) diff --git a/lib/Controller/ViewController.php b/lib/Controller/ViewController.php index c7a905e60..220c34422 100644 --- a/lib/Controller/ViewController.php +++ b/lib/Controller/ViewController.php @@ -8,28 +8,21 @@ namespace OCA\Calendar\Controller; use OC\App\CompareVersion; -use OCA\Calendar\Service\Appointments\AppointmentConfigService; +use OCA\Calendar\Service\CalendarInitialStateService; use OCP\App\IAppManager; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\FileDisplayResponse; use OCP\AppFramework\Http\TemplateResponse; -use OCP\AppFramework\Services\IInitialState; use OCP\Files\IAppData; use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\IConfig; use OCP\IRequest; -use function in_array; class ViewController extends Controller { /** @var IConfig */ private $config; - /** @var AppointmentConfigService */ - private $appointmentConfigService; - - /** @var IInitialState */ - private $initialStateService; /** @var IAppManager */ private $appManager; @@ -42,23 +35,19 @@ class ViewController extends Controller { private IAppData $appData; + private CalendarInitialStateService $calendarInitialStateService; + public function __construct(string $appName, IRequest $request, IConfig $config, - AppointmentConfigService $appointmentConfigService, - IInitialState $initialStateService, - IAppManager $appManager, - CompareVersion $compareVersion, ?string $userId, - IAppData $appData) { + IAppData $appData, + CalendarInitialStateService $calendarInitialStateService) { parent::__construct($appName, $request); $this->config = $config; - $this->appointmentConfigService = $appointmentConfigService; - $this->initialStateService = $initialStateService; - $this->appManager = $appManager; - $this->compareVersion = $compareVersion; $this->userId = $userId; $this->appData = $appData; + $this->calendarInitialStateService = $calendarInitialStateService; } /** @@ -70,96 +59,11 @@ public function __construct(string $appName, * @return TemplateResponse */ public function index():TemplateResponse { - $defaultEventLimit = $this->config->getAppValue($this->appName, 'eventLimit', 'yes'); - $defaultInitialView = $this->config->getAppValue($this->appName, 'currentView', 'dayGridMonth'); - $defaultShowWeekends = $this->config->getAppValue($this->appName, 'showWeekends', 'yes'); - $defaultWeekNumbers = $this->config->getAppValue($this->appName, 'showWeekNr', 'no'); - $defaultSkipPopover = $this->config->getAppValue($this->appName, 'skipPopover', 'no'); - $defaultTimezone = $this->config->getAppValue($this->appName, 'timezone', 'automatic'); - $defaultSlotDuration = $this->config->getAppValue($this->appName, 'slotDuration', '00:30:00'); - $defaultDefaultReminder = $this->config->getAppValue($this->appName, 'defaultReminder', 'none'); - $defaultShowTasks = $this->config->getAppValue($this->appName, 'showTasks', 'yes'); - - $appVersion = $this->config->getAppValue($this->appName, 'installed_version', ''); - $eventLimit = $this->config->getUserValue($this->userId, $this->appName, 'eventLimit', $defaultEventLimit) === 'yes'; - $firstRun = $this->config->getUserValue($this->userId, $this->appName, 'firstRun', 'yes') === 'yes'; - $initialView = $this->getView($this->config->getUserValue($this->userId, $this->appName, 'currentView', $defaultInitialView)); - $showWeekends = $this->config->getUserValue($this->userId, $this->appName, 'showWeekends', $defaultShowWeekends) === 'yes'; - $showWeekNumbers = $this->config->getUserValue($this->userId, $this->appName, 'showWeekNr', $defaultWeekNumbers) === 'yes'; - $skipPopover = $this->config->getUserValue($this->userId, $this->appName, 'skipPopover', $defaultSkipPopover) === 'yes'; - $timezone = $this->config->getUserValue($this->userId, $this->appName, 'timezone', $defaultTimezone); - $attachmentsFolder = $this->config->getUserValue($this->userId, 'dav', 'attachmentsFolder', '/Calendar'); - $slotDuration = $this->config->getUserValue($this->userId, $this->appName, 'slotDuration', $defaultSlotDuration); - $defaultReminder = $this->config->getUserValue($this->userId, $this->appName, 'defaultReminder', $defaultDefaultReminder); - $showTasks = $this->config->getUserValue($this->userId, $this->appName, 'showTasks', $defaultShowTasks) === 'yes'; - $hideEventExport = $this->config->getAppValue($this->appName, 'hideEventExport', 'no') === 'yes'; - $disableAppointments = $this->config->getAppValue($this->appName, 'disableAppointments', 'no') === 'yes'; - $forceEventAlarmType = $this->config->getAppValue($this->appName, 'forceEventAlarmType', ''); - if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) { - $forceEventAlarmType = false; - } - $canSubscribeLink = $this->config->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes'; - $showResources = $this->config->getAppValue($this->appName, 'showResources', 'yes') === 'yes'; - $publicCalendars = $this->config->getAppValue($this->appName, 'publicCalendars', ''); - - $talkEnabled = $this->appManager->isEnabledForUser('spreed'); - $talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1'; - $tasksEnabled = $this->appManager->isEnabledForUser('tasks'); - - $circleVersion = $this->appManager->getAppVersion('circles'); - $isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true; - // if circles is not installed, we use 0.0.0 - $isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22'); - - $this->initialStateService->provideInitialState('app_version', $appVersion); - $this->initialStateService->provideInitialState('event_limit', $eventLimit); - $this->initialStateService->provideInitialState('first_run', $firstRun); - $this->initialStateService->provideInitialState('initial_view', $initialView); - $this->initialStateService->provideInitialState('show_weekends', $showWeekends); - $this->initialStateService->provideInitialState('show_week_numbers', $showWeekNumbers); - $this->initialStateService->provideInitialState('skip_popover', $skipPopover); - $this->initialStateService->provideInitialState('talk_enabled', $talkEnabled); - $this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion); - $this->initialStateService->provideInitialState('timezone', $timezone); - $this->initialStateService->provideInitialState('attachments_folder', $attachmentsFolder); - $this->initialStateService->provideInitialState('slot_duration', $slotDuration); - $this->initialStateService->provideInitialState('default_reminder', $defaultReminder); - $this->initialStateService->provideInitialState('show_tasks', $showTasks); - $this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled); - $this->initialStateService->provideInitialState('hide_event_export', $hideEventExport); - $this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType); - $this->initialStateService->provideInitialState('appointmentConfigs', $this->appointmentConfigService->getAllAppointmentConfigurations($this->userId)); - $this->initialStateService->provideInitialState('disable_appointments', $disableAppointments); - $this->initialStateService->provideInitialState('can_subscribe_link', $canSubscribeLink); - $this->initialStateService->provideInitialState('show_resources', $showResources); - $this->initialStateService->provideInitialState('isCirclesEnabled', $isCirclesEnabled && $isCircleVersionCompatible); - $this->initialStateService->provideInitialState('publicCalendars', $publicCalendars); + $this->calendarInitialStateService->run(); return new TemplateResponse($this->appName, 'main'); } - /** - * Makes sure we don't use the old views anymore - * - * @param string $view - * @return string - */ - private function getView(string $view): string { - switch ($view) { - case 'agendaDay': - return 'timeGridDay'; - - case 'agendaWeek': - return 'timeGridWeek'; - - case 'month': - return 'dayGridMonth'; - - default: - return $view; - } - } - /** * @NoAdminRequired * @NoCSRFRequired diff --git a/lib/Listener/CalendarReferenceListener.php b/lib/Listener/CalendarReferenceListener.php index 4b5e224d3..d668d72d6 100644 --- a/lib/Listener/CalendarReferenceListener.php +++ b/lib/Listener/CalendarReferenceListener.php @@ -9,104 +9,27 @@ namespace OCA\Calendar\Listener; -use OC\App\CompareVersion; use OCA\Calendar\AppInfo\Application; -use OCP\App\IAppManager; -use OCP\AppFramework\Services\IInitialState; +use OCA\Calendar\Service\CalendarInitialStateService; use OCP\Collaboration\Reference\RenderReferenceEvent; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; -use OCP\Files\IAppData; -use OCP\IConfig; - - - use OCP\Util; /** * @template-implements IEventListener */ class CalendarReferenceListener implements IEventListener { - - /** @var IInitialState */ - private $initialStateService; - - /** @var IAppManager */ - private $appManager; - - /** @var IConfig */ - private $config; - - /** @var CompareVersion */ - private $compareVersion; - - private IAppData $appData; - public function __construct( - IInitialState $initialStateService, - IAppManager $appManager, - IConfig $config, - IAppData $appData, - CompareVersion $compareVersion, - ) { - $this->config = $config; - $this->initialStateService = $initialStateService; - $this->appManager = $appManager; - $this->appData = $appData; - $this->compareVersion = $compareVersion; - + private CalendarInitialStateService $calendarinitialStateService) { + $this->calendarinitialStateService = $calendarinitialStateService; } public function handle(Event $event): void { if (!$event instanceof RenderReferenceEvent) { return; } - $defaultEventLimit = $this->config->getAppValue('calendar', 'eventLimit', 'yes'); - $defaultInitialView = $this->config->getAppValue('calendar', 'currentView', 'dayGridMonth'); - $defaultShowWeekends = $this->config->getAppValue('calendar', 'showWeekends', 'yes'); - $defaultWeekNumbers = $this->config->getAppValue('calendar', 'showWeekNr', 'no'); - $defaultSkipPopover = $this->config->getAppValue('calendar', 'skipPopover', 'no'); - $defaultTimezone = $this->config->getAppValue('calendar', 'timezone', 'automatic'); - $defaultSlotDuration = $this->config->getAppValue('calendar', 'slotDuration', '00:30:00'); - $defaultDefaultReminder = $this->config->getAppValue('calendar', 'defaultReminder', 'none'); - - $appVersion = $this->config->getAppValue('calendar', 'installed_version', ''); - $forceEventAlarmType = $this->config->getAppValue('calendar', 'forceEventAlarmType', ''); - if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) { - $forceEventAlarmType = false; - } - $showResources = $this->config->getAppValue('calendar', 'showResources', 'yes') === 'yes'; - $publicCalendars = $this->config->getAppValue('calendar', 'publicCalendars', ''); - - $talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1'; - $tasksEnabled = $this->appManager->isEnabledForUser('tasks'); - - $circleVersion = $this->appManager->getAppVersion('circles'); - $isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true; - // if circles is not installed, we use 0.0.0 - $isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22'); - - $this->initialStateService->provideInitialState('app_version', $appVersion); - $this->initialStateService->provideInitialState('event_limit', $defaultEventLimit); - $this->initialStateService->provideInitialState('first_run', false); - $this->initialStateService->provideInitialState('initial_view', $defaultInitialView); - $this->initialStateService->provideInitialState('show_weekends', $defaultShowWeekends); - $this->initialStateService->provideInitialState('show_week_numbers', $defaultWeekNumbers === 'yes'); - $this->initialStateService->provideInitialState('skip_popover', true); - $this->initialStateService->provideInitialState('talk_enabled', false); - $this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion); - $this->initialStateService->provideInitialState('show_tasks', false); - $this->initialStateService->provideInitialState('timezone', $defaultTimezone); - $this->initialStateService->provideInitialState('attachments_folder', '/Calendar'); - $this->initialStateService->provideInitialState('slot_duration', $defaultSlotDuration); - $this->initialStateService->provideInitialState('default_reminder', $defaultDefaultReminder); - $this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled); - $this->initialStateService->provideInitialState('hide_event_export', true); - $this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType); - $this->initialStateService->provideInitialState('disable_appointments', true); - $this->initialStateService->provideInitialState('can_subscribe_link', false); - $this->initialStateService->provideInitialState('show_resources', $showResources); - $this->initialStateService->provideInitialState('publicCalendars', $publicCalendars); + $this->calendarinitialStateService->run(); Util::addScript(Application::APP_ID, 'calendar-reference'); } diff --git a/lib/Service/CalendarInitialStateService.php b/lib/Service/CalendarInitialStateService.php new file mode 100644 index 000000000..fa590edb4 --- /dev/null +++ b/lib/Service/CalendarInitialStateService.php @@ -0,0 +1,125 @@ +appName = $appName; + $this->config = $config; + $this->initialStateService = $initialStateService; + $this->appointmentConfigService = $appointmentConfigService; + $this->appManager = $appManager; + $this->compareVersion = $compareVersion; + $this->userId = $userId; + } + + public function run(): void { + $defaultEventLimit = $this->config->getAppValue($this->appName, 'eventLimit', 'yes'); + $defaultInitialView = $this->config->getAppValue($this->appName, 'currentView', 'dayGridMonth'); + $defaultShowWeekends = $this->config->getAppValue($this->appName, 'showWeekends', 'yes'); + $defaultWeekNumbers = $this->config->getAppValue($this->appName, 'showWeekNr', 'no'); + $defaultSkipPopover = $this->config->getAppValue($this->appName, 'skipPopover', 'no'); + $defaultTimezone = $this->config->getAppValue($this->appName, 'timezone', 'automatic'); + $defaultSlotDuration = $this->config->getAppValue($this->appName, 'slotDuration', '00:30:00'); + $defaultDefaultReminder = $this->config->getAppValue($this->appName, 'defaultReminder', 'none'); + $defaultShowTasks = $this->config->getAppValue($this->appName, 'showTasks', 'yes'); + + $appVersion = $this->config->getAppValue($this->appName, 'installed_version', ''); + $eventLimit = $this->config->getUserValue($this->userId, $this->appName, 'eventLimit', $defaultEventLimit) === 'yes'; + $firstRun = $this->config->getUserValue($this->userId, $this->appName, 'firstRun', 'yes') === 'yes'; + $initialView = $this->getView($this->config->getUserValue($this->userId, $this->appName, 'currentView', $defaultInitialView)); + $showWeekends = $this->config->getUserValue($this->userId, $this->appName, 'showWeekends', $defaultShowWeekends) === 'yes'; + $showWeekNumbers = $this->config->getUserValue($this->userId, $this->appName, 'showWeekNr', $defaultWeekNumbers) === 'yes'; + $skipPopover = $this->config->getUserValue($this->userId, $this->appName, 'skipPopover', $defaultSkipPopover) === 'yes'; + $timezone = $this->config->getUserValue($this->userId, $this->appName, 'timezone', $defaultTimezone); + $attachmentsFolder = $this->config->getUserValue($this->userId, 'dav', 'attachmentsFolder', '/Calendar'); + $slotDuration = $this->config->getUserValue($this->userId, $this->appName, 'slotDuration', $defaultSlotDuration); + $defaultReminder = $this->config->getUserValue($this->userId, $this->appName, 'defaultReminder', $defaultDefaultReminder); + $showTasks = $this->config->getUserValue($this->userId, $this->appName, 'showTasks', $defaultShowTasks) === 'yes'; + $hideEventExport = $this->config->getAppValue($this->appName, 'hideEventExport', 'no') === 'yes'; + $disableAppointments = $this->config->getAppValue($this->appName, 'disableAppointments', 'no') === 'yes'; + $forceEventAlarmType = $this->config->getAppValue($this->appName, 'forceEventAlarmType', ''); + if (!in_array($forceEventAlarmType, ['DISPLAY', 'EMAIL'], true)) { + $forceEventAlarmType = false; + } + $canSubscribeLink = $this->config->getAppValue('dav', 'allow_calendar_link_subscriptions', 'yes') === 'yes'; + $showResources = $this->config->getAppValue($this->appName, 'showResources', 'yes') === 'yes'; + $publicCalendars = $this->config->getAppValue($this->appName, 'publicCalendars', ''); + + $talkEnabled = $this->appManager->isEnabledForUser('spreed'); + $talkApiVersion = version_compare($this->appManager->getAppVersion('spreed'), '12.0.0', '>=') ? 'v4' : 'v1'; + $tasksEnabled = $this->appManager->isEnabledForUser('tasks'); + + $circleVersion = $this->appManager->getAppVersion('circles'); + $isCirclesEnabled = $this->appManager->isEnabledForUser('circles') === true; + // if circles is not installed, we use 0.0.0 + $isCircleVersionCompatible = $this->compareVersion->isCompatible($circleVersion ? $circleVersion : '0.0.0', '22'); + + $this->initialStateService->provideInitialState('app_version', $appVersion); + $this->initialStateService->provideInitialState('event_limit', $eventLimit); + $this->initialStateService->provideInitialState('first_run', $firstRun); + $this->initialStateService->provideInitialState('initial_view', $initialView); + $this->initialStateService->provideInitialState('show_weekends', $showWeekends); + $this->initialStateService->provideInitialState('show_week_numbers', $showWeekNumbers); + $this->initialStateService->provideInitialState('skip_popover', $skipPopover); + $this->initialStateService->provideInitialState('talk_enabled', $talkEnabled); + $this->initialStateService->provideInitialState('talk_api_version', $talkApiVersion); + $this->initialStateService->provideInitialState('timezone', $timezone); + $this->initialStateService->provideInitialState('attachments_folder', $attachmentsFolder); + $this->initialStateService->provideInitialState('slot_duration', $slotDuration); + $this->initialStateService->provideInitialState('default_reminder', $defaultReminder); + $this->initialStateService->provideInitialState('show_tasks', $showTasks); + $this->initialStateService->provideInitialState('tasks_enabled', $tasksEnabled); + $this->initialStateService->provideInitialState('hide_event_export', $hideEventExport); + $this->initialStateService->provideInitialState('force_event_alarm_type', $forceEventAlarmType); + $this->initialStateService->provideInitialState('appointmentConfigs', $this->appointmentConfigService->getAllAppointmentConfigurations($this->userId)); + $this->initialStateService->provideInitialState('disable_appointments', $disableAppointments); + $this->initialStateService->provideInitialState('can_subscribe_link', $canSubscribeLink); + $this->initialStateService->provideInitialState('show_resources', $showResources); + $this->initialStateService->provideInitialState('isCirclesEnabled', $isCirclesEnabled && $isCircleVersionCompatible); + $this->initialStateService->provideInitialState('publicCalendars', $publicCalendars); + } + + /** + * Makes sure we don't use the old views anymore + * + * @param string $view + * @return string + */ + private function getView(string $view): string { + switch ($view) { + case 'agendaDay': + return 'timeGridDay'; + + case 'agendaWeek': + return 'timeGridWeek'; + + case 'month': + return 'dayGridMonth'; + + default: + return $view; + } + } +} diff --git a/tests/php/unit/Controller/ViewControllerTest.php b/tests/php/unit/Service/CalendarInitialStateServiceTest.php similarity index 88% rename from tests/php/unit/Controller/ViewControllerTest.php rename to tests/php/unit/Service/CalendarInitialStateServiceTest.php index 17841f6b8..7ede0129b 100755 --- a/tests/php/unit/Controller/ViewControllerTest.php +++ b/tests/php/unit/Service/CalendarInitialStateServiceTest.php @@ -2,30 +2,25 @@ declare(strict_types=1); /** - * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Calendar\Controller; +namespace OCA\Calendar\Service; use ChristophWurst\Nextcloud\Testing\TestCase; use OC\App\CompareVersion; use OCA\Calendar\Db\AppointmentConfig; use OCA\Calendar\Service\Appointments\AppointmentConfigService; use OCP\App\IAppManager; -use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; -use OCP\Files\IAppData; use OCP\IConfig; -use OCP\IRequest; use PHPUnit\Framework\MockObject\MockObject; -class ViewControllerTest extends TestCase { +class CalendarInitialStateServiceTest extends TestCase { + /** @var string */ private $appName; - /** @var IRequest|MockObject */ - private $request; - /** @var IAppManager|MockObject */ private $appManager; @@ -41,36 +36,29 @@ class ViewControllerTest extends TestCase { /** @var string */ private $userId; - /** @var ViewController */ - private $controller; - - /** @var IAppData|MockObject */ - private $appData; + /** @var CalendarInitialStateService */ + private $service; /** @var CompareVersion|MockObject*/ private $compareVersion; protected function setUp(): void { $this->appName = 'calendar'; - $this->request = $this->createMock(IRequest::class); $this->appManager = $this->createMock(IAppManager::class); $this->config = $this->createMock(IConfig::class); $this->appointmentContfigService = $this->createMock(AppointmentConfigService::class); $this->initialStateService = $this->createMock(IInitialState::class); $this->compareVersion = $this->createMock(CompareVersion::class); $this->userId = 'user123'; - $this->appData = $this->createMock(IAppData::class); - $this->controller = new ViewController( + $this->service = new CalendarInitialStateService( $this->appName, - $this->request, - $this->config, - $this->appointmentContfigService, $this->initialStateService, $this->appManager, + $this->config, + $this->appointmentContfigService, $this->compareVersion, $this->userId, - $this->appData, ); } @@ -155,12 +143,7 @@ public function testIndex(): void { ['publicCalendars', null], ); - $response = $this->controller->index(); - - $this->assertInstanceOf(TemplateResponse::class, $response); - $this->assertEquals([], $response->getParams()); - $this->assertEquals('user', $response->getRenderAs()); - $this->assertEquals('main', $response->getTemplateName()); + $this->service->run(); } /** @@ -250,12 +233,8 @@ public function testIndexViewFix(string $savedView, string $expectedView): void ['publicCalendars', null], ); - $response = $this->controller->index(); + $this->service->run(); - $this->assertInstanceOf(TemplateResponse::class, $response); - $this->assertEquals([], $response->getParams()); - $this->assertEquals('user', $response->getRenderAs()); - $this->assertEquals('main', $response->getTemplateName()); } /**