mirror of
https://github.com/nextcloud/server.git
synced 2026-04-27 09:08:22 -04:00
Merge pull request #45904 from nextcloud/backport/45875/stable29
[stable29] fix(theming): also apply enforced theme for guests
This commit is contained in:
commit
fd08bb8d3d
2 changed files with 64 additions and 8 deletions
|
|
@ -33,24 +33,22 @@ use OCA\Theming\Themes\LightTheme;
|
|||
use OCP\IConfig;
|
||||
use OCP\IUser;
|
||||
use OCP\IUserSession;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class ThemesService {
|
||||
private IUserSession $userSession;
|
||||
private IConfig $config;
|
||||
|
||||
/** @var ITheme[] */
|
||||
private array $themesProviders;
|
||||
|
||||
public function __construct(IUserSession $userSession,
|
||||
IConfig $config,
|
||||
DefaultTheme $defaultTheme,
|
||||
public function __construct(
|
||||
private IUserSession $userSession,
|
||||
private IConfig $config,
|
||||
private LoggerInterface $logger,
|
||||
private DefaultTheme $defaultTheme,
|
||||
LightTheme $lightTheme,
|
||||
DarkTheme $darkTheme,
|
||||
HighContrastTheme $highContrastTheme,
|
||||
DarkHighContrastTheme $darkHighContrastTheme,
|
||||
DyslexiaFont $dyslexiaFont) {
|
||||
$this->userSession = $userSession;
|
||||
$this->config = $config;
|
||||
|
||||
// Register themes
|
||||
$this->themesProviders = [
|
||||
|
|
@ -69,6 +67,22 @@ class ThemesService {
|
|||
* @return ITheme[]
|
||||
*/
|
||||
public function getThemes(): array {
|
||||
// Enforced theme if configured
|
||||
$enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
|
||||
if ($enforcedTheme !== '') {
|
||||
if (!isset($this->themesProviders[$enforcedTheme])) {
|
||||
$this->logger->error('Enforced theme not found', ['theme' => $enforcedTheme]);
|
||||
return $this->themesProviders;
|
||||
}
|
||||
|
||||
$defaultTheme = $this->themesProviders[$this->defaultTheme->getId()];
|
||||
$theme = $this->themesProviders[$enforcedTheme];
|
||||
return [
|
||||
$defaultTheme->getId() => $defaultTheme,
|
||||
$theme->getId() => $theme,
|
||||
];
|
||||
}
|
||||
|
||||
return $this->themesProviders;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ use OCP\IURLGenerator;
|
|||
use OCP\IUser;
|
||||
use OCP\IUserSession;
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Test\TestCase;
|
||||
|
||||
class ThemesServiceTest extends TestCase {
|
||||
|
|
@ -51,6 +52,9 @@ class ThemesServiceTest extends TestCase {
|
|||
private $userSession;
|
||||
/** @var IConfig|MockObject */
|
||||
private $config;
|
||||
/** @var LoggerInterface|MockObject */
|
||||
private $logger;
|
||||
|
||||
/** @var ThemingDefaults|MockObject */
|
||||
private $themingDefaults;
|
||||
|
||||
|
|
@ -60,6 +64,7 @@ class ThemesServiceTest extends TestCase {
|
|||
protected function setUp(): void {
|
||||
$this->userSession = $this->createMock(IUserSession::class);
|
||||
$this->config = $this->createMock(IConfig::class);
|
||||
$this->logger = $this->createMock(LoggerInterface::class);
|
||||
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
||||
|
||||
$this->themingDefaults->expects($this->any())
|
||||
|
|
@ -75,6 +80,7 @@ class ThemesServiceTest extends TestCase {
|
|||
$this->themesService = new ThemesService(
|
||||
$this->userSession,
|
||||
$this->config,
|
||||
$this->logger,
|
||||
...array_values($this->themes)
|
||||
);
|
||||
|
||||
|
|
@ -93,6 +99,42 @@ class ThemesServiceTest extends TestCase {
|
|||
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
|
||||
}
|
||||
|
||||
public function testGetThemesEnforced() {
|
||||
$this->config->expects($this->once())
|
||||
->method('getSystemValueString')
|
||||
->with('enforce_theme', '')
|
||||
->willReturn('dark');
|
||||
$this->logger->expects($this->never())
|
||||
->method('error');
|
||||
|
||||
$expected = [
|
||||
'default',
|
||||
'dark',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
|
||||
}
|
||||
|
||||
public function testGetThemesEnforcedInvalid() {
|
||||
$this->config->expects($this->once())
|
||||
->method('getSystemValueString')
|
||||
->with('enforce_theme', '')
|
||||
->willReturn('invalid');
|
||||
$this->logger->expects($this->once())
|
||||
->method('error')
|
||||
->with('Enforced theme not found', ['theme' => 'invalid']);
|
||||
|
||||
$expected = [
|
||||
'default',
|
||||
'light',
|
||||
'dark',
|
||||
'light-highcontrast',
|
||||
'dark-highcontrast',
|
||||
'opendyslexic',
|
||||
];
|
||||
|
||||
$this->assertEquals($expected, array_keys($this->themesService->getThemes()));
|
||||
}
|
||||
|
||||
public function dataTestEnableTheme() {
|
||||
return [
|
||||
|
|
|
|||
Loading…
Reference in a new issue