Skip to content

Commit b34e2a1

Browse files
authored
Merge pull request #45875 from nextcloud/fix/guest-enforce-theme
fix(theming): also apply enforced theme for guests
2 parents f15adce + 88e83d9 commit b34e2a1

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

apps/theming/lib/Service/ThemesService.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,22 @@
1616
use OCP\IConfig;
1717
use OCP\IUser;
1818
use OCP\IUserSession;
19+
use Psr\Log\LoggerInterface;
1920

2021
class ThemesService {
21-
private IUserSession $userSession;
22-
private IConfig $config;
23-
2422
/** @var ITheme[] */
2523
private array $themesProviders;
2624

27-
public function __construct(IUserSession $userSession,
28-
IConfig $config,
29-
DefaultTheme $defaultTheme,
25+
public function __construct(
26+
private IUserSession $userSession,
27+
private IConfig $config,
28+
private LoggerInterface $logger,
29+
private DefaultTheme $defaultTheme,
3030
LightTheme $lightTheme,
3131
DarkTheme $darkTheme,
3232
HighContrastTheme $highContrastTheme,
3333
DarkHighContrastTheme $darkHighContrastTheme,
3434
DyslexiaFont $dyslexiaFont) {
35-
$this->userSession = $userSession;
36-
$this->config = $config;
3735

3836
// Register themes
3937
$this->themesProviders = [
@@ -52,6 +50,22 @@ public function __construct(IUserSession $userSession,
5250
* @return ITheme[]
5351
*/
5452
public function getThemes(): array {
53+
// Enforced theme if configured
54+
$enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
55+
if ($enforcedTheme !== '') {
56+
if (!isset($this->themesProviders[$enforcedTheme])) {
57+
$this->logger->error('Enforced theme not found', ['theme' => $enforcedTheme]);
58+
return $this->themesProviders;
59+
}
60+
61+
$defaultTheme = $this->themesProviders[$this->defaultTheme->getId()];
62+
$theme = $this->themesProviders[$enforcedTheme];
63+
return [
64+
$defaultTheme->getId() => $defaultTheme,
65+
$theme->getId() => $theme,
66+
];
67+
}
68+
5569
return $this->themesProviders;
5670
}
5771

apps/theming/tests/Service/ThemesServiceTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use OCP\IUser;
2525
use OCP\IUserSession;
2626
use PHPUnit\Framework\MockObject\MockObject;
27+
use Psr\Log\LoggerInterface;
2728
use Test\TestCase;
2829

2930
class ThemesServiceTest extends TestCase {
@@ -34,6 +35,9 @@ class ThemesServiceTest extends TestCase {
3435
private $userSession;
3536
/** @var IConfig|MockObject */
3637
private $config;
38+
/** @var LoggerInterface|MockObject */
39+
private $logger;
40+
3741
/** @var ThemingDefaults|MockObject */
3842
private $themingDefaults;
3943

@@ -43,6 +47,7 @@ class ThemesServiceTest extends TestCase {
4347
protected function setUp(): void {
4448
$this->userSession = $this->createMock(IUserSession::class);
4549
$this->config = $this->createMock(IConfig::class);
50+
$this->logger = $this->createMock(LoggerInterface::class);
4651
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
4752

4853
$this->themingDefaults->expects($this->any())
@@ -58,6 +63,7 @@ protected function setUp(): void {
5863
$this->themesService = new ThemesService(
5964
$this->userSession,
6065
$this->config,
66+
$this->logger,
6167
...array_values($this->themes)
6268
);
6369

@@ -76,6 +82,42 @@ public function testGetThemes() {
7682
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
7783
}
7884

85+
public function testGetThemesEnforced() {
86+
$this->config->expects($this->once())
87+
->method('getSystemValueString')
88+
->with('enforce_theme', '')
89+
->willReturn('dark');
90+
$this->logger->expects($this->never())
91+
->method('error');
92+
93+
$expected = [
94+
'default',
95+
'dark',
96+
];
97+
98+
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
99+
}
100+
101+
public function testGetThemesEnforcedInvalid() {
102+
$this->config->expects($this->once())
103+
->method('getSystemValueString')
104+
->with('enforce_theme', '')
105+
->willReturn('invalid');
106+
$this->logger->expects($this->once())
107+
->method('error')
108+
->with('Enforced theme not found', ['theme' => 'invalid']);
109+
110+
$expected = [
111+
'default',
112+
'light',
113+
'dark',
114+
'light-highcontrast',
115+
'dark-highcontrast',
116+
'opendyslexic',
117+
];
118+
119+
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
120+
}
79121

80122
public function dataTestEnableTheme() {
81123
return [

0 commit comments

Comments
 (0)