123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace OCA\Theming\Tests\Service;
- use OCA\Theming\AppInfo\Application;
- use OCA\Theming\ImageManager;
- use OCA\Theming\ITheme;
- use OCA\Theming\Service\ThemesService;
- use OCA\Theming\Themes\DarkHighContrastTheme;
- use OCA\Theming\Themes\DarkTheme;
- use OCA\Theming\Themes\DefaultTheme;
- use OCA\Theming\Themes\DyslexiaFont;
- use OCA\Theming\Themes\HighContrastTheme;
- use OCA\Theming\Themes\LightTheme;
- use OCA\Theming\ThemingDefaults;
- use OCA\Theming\Util;
- use OCP\App\IAppManager;
- use OCP\IConfig;
- use OCP\IL10N;
- 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 {
- /** @var ThemesService */
- private $themesService;
- /** @var IUserSession|MockObject */
- private $userSession;
- /** @var IConfig|MockObject */
- private $config;
- /** @var LoggerInterface|MockObject */
- private $logger;
- /** @var ThemingDefaults|MockObject */
- private $themingDefaults;
- /** @var ITheme[] */
- private $themes;
- 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())
- ->method('getColorPrimary')
- ->willReturn('#0082c9');
- $this->themingDefaults->expects($this->any())
- ->method('getDefaultColorPrimary')
- ->willReturn('#0082c9');
- $this->initThemes();
- $this->themesService = new ThemesService(
- $this->userSession,
- $this->config,
- $this->logger,
- ...array_values($this->themes)
- );
- parent::setUp();
- }
- public function testGetThemes() {
- $expected = [
- 'default',
- 'light',
- 'dark',
- 'light-highcontrast',
- 'dark-highcontrast',
- 'opendyslexic',
- ];
- $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 [
- ['default', [], ['default']],
- ['dark', [], ['dark']],
- ['dark', ['dark'], ['dark']],
- ['opendyslexic', ['dark'], ['dark', 'opendyslexic']],
- ['dark', ['light-highcontrast', 'opendyslexic'], ['opendyslexic', 'dark']],
- ];
- }
- /**
- * @dataProvider dataTestEnableTheme
- *
- * @param string $toEnable
- * @param string[] $enabledThemes
- * @param string[] $expectedEnabled
- */
- public function testEnableTheme(string $toEnable, array $enabledThemes, array $expectedEnabled) {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', '[]')
- ->willReturn(json_encode($enabledThemes));
- $this->assertEquals($expectedEnabled, $this->themesService->enableTheme($this->themes[$toEnable]));
- }
- public function dataTestDisableTheme() {
- return [
- ['dark', [], []],
- ['dark', ['dark'], []],
- ['opendyslexic', ['dark', 'opendyslexic'], ['dark'], ],
- ['light-highcontrast', ['opendyslexic'], ['opendyslexic']],
- ];
- }
- /**
- * @dataProvider dataTestDisableTheme
- *
- * @param string $toEnable
- * @param string[] $enabledThemes
- * @param string[] $expectedEnabled
- */
- public function testDisableTheme(string $toDisable, array $enabledThemes, array $expectedEnabled) {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', '[]')
- ->willReturn(json_encode($enabledThemes));
- $this->assertEquals($expectedEnabled, $this->themesService->disableTheme($this->themes[$toDisable]));
- }
- public function dataTestIsEnabled() {
- return [
- ['dark', [], false],
- ['dark', ['dark'], true],
- ['opendyslexic', ['dark', 'opendyslexic'], true],
- ['light-highcontrast', ['opendyslexic'], false],
- ];
- }
- /**
- * @dataProvider dataTestIsEnabled
- *
- * @param string $toEnable
- * @param string[] $enabledThemes
- */
- public function testIsEnabled(string $themeId, array $enabledThemes, $expected) {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', '[]')
- ->willReturn(json_encode($enabledThemes));
- $this->assertEquals($expected, $this->themesService->isEnabled($this->themes[$themeId]));
- }
- public function testGetEnabledThemes() {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', '[]')
- ->willReturn(json_encode([]));
- $this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with('enforce_theme', '')
- ->willReturn('');
- $this->assertEquals([], $this->themesService->getEnabledThemes());
- }
- public function testGetEnabledThemesEnforced() {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('getUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', '[]')
- ->willReturn(json_encode([]));
- $this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with('enforce_theme', '')
- ->willReturn('light');
- $this->assertEquals(['light'], $this->themesService->getEnabledThemes());
- }
- public function dataTestSetEnabledThemes() {
- return [
- [[], []],
- [['light'], ['light']],
- [['dark'], ['dark']],
- [['dark', 'dark', 'opendyslexic'], ['dark', 'opendyslexic']],
- ];
- }
- /**
- * @dataProvider dataTestSetEnabledThemes
- *
- * @param string[] $enabledThemes
- * @param string[] $expected
- */
- public function testSetEnabledThemes(array $enabledThemes, array $expected) {
- $user = $this->createMock(IUser::class);
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($user);
- $user->expects($this->any())
- ->method('getUID')
- ->willReturn('user');
- $this->config->expects($this->once())
- ->method('setUserValue')
- ->with('user', Application::APP_ID, 'enabled-themes', json_encode($expected));
- $this->invokePrivate($this->themesService, 'setEnabledThemes', [$enabledThemes]);
- }
- private function initThemes() {
- $util = $this->createMock(Util::class);
- $urlGenerator = $this->createMock(IURLGenerator::class);
- $imageManager = $this->createMock(ImageManager::class);
- $l10n = $this->createMock(IL10N::class);
- $appManager = $this->createMock(IAppManager::class);
- $this->themes = [
- 'default' => new DefaultTheme(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- 'light' => new LightTheme(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- 'dark' => new DarkTheme(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- 'light-highcontrast' => new HighContrastTheme(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- 'dark-highcontrast' => new DarkHighContrastTheme(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- 'opendyslexic' => new DyslexiaFont(
- $util,
- $this->themingDefaults,
- $this->userSession,
- $urlGenerator,
- $imageManager,
- $this->config,
- $l10n,
- $appManager,
- null,
- ),
- ];
- }
- }
|