Skip to content

Commit fd08bb8

Browse files
Merge pull request #45904 from nextcloud/backport/45875/stable29
[stable29] fix(theming): also apply enforced theme for guests
2 parents 91ecdc7 + df1026f commit fd08bb8

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
@@ -33,24 +33,22 @@
3333
use OCP\IConfig;
3434
use OCP\IUser;
3535
use OCP\IUserSession;
36+
use Psr\Log\LoggerInterface;
3637

3738
class ThemesService {
38-
private IUserSession $userSession;
39-
private IConfig $config;
40-
4139
/** @var ITheme[] */
4240
private array $themesProviders;
4341

44-
public function __construct(IUserSession $userSession,
45-
IConfig $config,
46-
DefaultTheme $defaultTheme,
42+
public function __construct(
43+
private IUserSession $userSession,
44+
private IConfig $config,
45+
private LoggerInterface $logger,
46+
private DefaultTheme $defaultTheme,
4747
LightTheme $lightTheme,
4848
DarkTheme $darkTheme,
4949
HighContrastTheme $highContrastTheme,
5050
DarkHighContrastTheme $darkHighContrastTheme,
5151
DyslexiaFont $dyslexiaFont) {
52-
$this->userSession = $userSession;
53-
$this->config = $config;
5452

5553
// Register themes
5654
$this->themesProviders = [
@@ -69,6 +67,22 @@ public function __construct(IUserSession $userSession,
6967
* @return ITheme[]
7068
*/
7169
public function getThemes(): array {
70+
// Enforced theme if configured
71+
$enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
72+
if ($enforcedTheme !== '') {
73+
if (!isset($this->themesProviders[$enforcedTheme])) {
74+
$this->logger->error('Enforced theme not found', ['theme' => $enforcedTheme]);
75+
return $this->themesProviders;
76+
}
77+
78+
$defaultTheme = $this->themesProviders[$this->defaultTheme->getId()];
79+
$theme = $this->themesProviders[$enforcedTheme];
80+
return [
81+
$defaultTheme->getId() => $defaultTheme,
82+
$theme->getId() => $theme,
83+
];
84+
}
85+
7286
return $this->themesProviders;
7387
}
7488

apps/theming/tests/Service/ThemesServiceTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
use OCP\IUser;
4242
use OCP\IUserSession;
4343
use PHPUnit\Framework\MockObject\MockObject;
44+
use Psr\Log\LoggerInterface;
4445
use Test\TestCase;
4546

4647
class ThemesServiceTest extends TestCase {
@@ -51,6 +52,9 @@ class ThemesServiceTest extends TestCase {
5152
private $userSession;
5253
/** @var IConfig|MockObject */
5354
private $config;
55+
/** @var LoggerInterface|MockObject */
56+
private $logger;
57+
5458
/** @var ThemingDefaults|MockObject */
5559
private $themingDefaults;
5660

@@ -60,6 +64,7 @@ class ThemesServiceTest extends TestCase {
6064
protected function setUp(): void {
6165
$this->userSession = $this->createMock(IUserSession::class);
6266
$this->config = $this->createMock(IConfig::class);
67+
$this->logger = $this->createMock(LoggerInterface::class);
6368
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
6469

6570
$this->themingDefaults->expects($this->any())
@@ -75,6 +80,7 @@ protected function setUp(): void {
7580
$this->themesService = new ThemesService(
7681
$this->userSession,
7782
$this->config,
83+
$this->logger,
7884
...array_values($this->themes)
7985
);
8086

@@ -93,6 +99,42 @@ public function testGetThemes() {
9399
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
94100
}
95101

102+
public function testGetThemesEnforced() {
103+
$this->config->expects($this->once())
104+
->method('getSystemValueString')
105+
->with('enforce_theme', '')
106+
->willReturn('dark');
107+
$this->logger->expects($this->never())
108+
->method('error');
109+
110+
$expected = [
111+
'default',
112+
'dark',
113+
];
114+
115+
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
116+
}
117+
118+
public function testGetThemesEnforcedInvalid() {
119+
$this->config->expects($this->once())
120+
->method('getSystemValueString')
121+
->with('enforce_theme', '')
122+
->willReturn('invalid');
123+
$this->logger->expects($this->once())
124+
->method('error')
125+
->with('Enforced theme not found', ['theme' => 'invalid']);
126+
127+
$expected = [
128+
'default',
129+
'light',
130+
'dark',
131+
'light-highcontrast',
132+
'dark-highcontrast',
133+
'opendyslexic',
134+
];
135+
136+
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
137+
}
96138

97139
public function dataTestEnableTheme() {
98140
return [

0 commit comments

Comments
 (0)