diff --git a/apps/dashboard/lib/Service/DashboardService.php b/apps/dashboard/lib/Service/DashboardService.php index bb5333c2cc77b..4967ce58f1b7d 100644 --- a/apps/dashboard/lib/Service/DashboardService.php +++ b/apps/dashboard/lib/Service/DashboardService.php @@ -32,6 +32,25 @@ public function getLayout(): array { return array_values(array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== '')); } + /** + * @param list $layout + * @return list + */ + public function sanitizeLayout(array $layout): array { + $seen = []; + $result = []; + foreach ($layout as $value) { + if ($value === '' || isset($seen[$value])) { + continue; + } + + $seen[$value] = true; + $result[] = $value; + } + + return $result; + } + /** * @return list */ diff --git a/apps/dashboard/tests/DashboardServiceTest.php b/apps/dashboard/tests/DashboardServiceTest.php index ebcd06cdf033a..73a7906ec7fbd 100644 --- a/apps/dashboard/tests/DashboardServiceTest.php +++ b/apps/dashboard/tests/DashboardServiceTest.php @@ -40,6 +40,25 @@ protected function setUp(): void { ); } + public function testGetLayoutRemovesEmptyAndDuplicateEntries(): void { + $this->appConfig->method('getAppValueString') + ->with('layout', 'recommendations,spreed,mail,calendar') + ->willReturn('recommendations,spreed,mail,calendar'); + $this->userConfig->method('getValueString') + ->with('alice', 'dashboard', 'layout', 'recommendations,spreed,mail,calendar') + ->willReturn('spreed,,mail,mail,calendar,spreed'); + + $layout = $this->service->getLayout(); + + $this->assertSame(['spreed', 'mail', 'calendar'], $layout); + } + + public function testSanitizeLayoutRemovesEmptyAndDuplicateEntries(): void { + $layout = $this->service->sanitizeLayout(['files', 'calendar', 'files', '', 'mail', 'calendar']); + + $this->assertSame(['files', 'calendar', 'mail'], $layout); + } + public function testGetBirthdate(): void { $user = $this->createMock(IUser::class); $this->userManager->method('get')