ThemesServiceTest.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests\Service;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\ImageManager;
  9. use OCA\Theming\ITheme;
  10. use OCA\Theming\Service\ThemesService;
  11. use OCA\Theming\Themes\DarkHighContrastTheme;
  12. use OCA\Theming\Themes\DarkTheme;
  13. use OCA\Theming\Themes\DefaultTheme;
  14. use OCA\Theming\Themes\DyslexiaFont;
  15. use OCA\Theming\Themes\HighContrastTheme;
  16. use OCA\Theming\Themes\LightTheme;
  17. use OCA\Theming\ThemingDefaults;
  18. use OCA\Theming\Util;
  19. use OCP\App\IAppManager;
  20. use OCP\IConfig;
  21. use OCP\IL10N;
  22. use OCP\IURLGenerator;
  23. use OCP\IUser;
  24. use OCP\IUserSession;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. use Psr\Log\LoggerInterface;
  27. use Test\TestCase;
  28. class ThemesServiceTest extends TestCase {
  29. /** @var ThemesService */
  30. private $themesService;
  31. /** @var IUserSession|MockObject */
  32. private $userSession;
  33. /** @var IConfig|MockObject */
  34. private $config;
  35. /** @var LoggerInterface|MockObject */
  36. private $logger;
  37. /** @var ThemingDefaults|MockObject */
  38. private $themingDefaults;
  39. /** @var ITheme[] */
  40. private $themes;
  41. protected function setUp(): void {
  42. $this->userSession = $this->createMock(IUserSession::class);
  43. $this->config = $this->createMock(IConfig::class);
  44. $this->logger = $this->createMock(LoggerInterface::class);
  45. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  46. $this->themingDefaults->expects($this->any())
  47. ->method('getColorPrimary')
  48. ->willReturn('#0082c9');
  49. $this->themingDefaults->expects($this->any())
  50. ->method('getDefaultColorPrimary')
  51. ->willReturn('#0082c9');
  52. $this->initThemes();
  53. $this->themesService = new ThemesService(
  54. $this->userSession,
  55. $this->config,
  56. $this->logger,
  57. ...array_values($this->themes)
  58. );
  59. parent::setUp();
  60. }
  61. public function testGetThemes() {
  62. $expected = [
  63. 'default',
  64. 'light',
  65. 'dark',
  66. 'light-highcontrast',
  67. 'dark-highcontrast',
  68. 'opendyslexic',
  69. ];
  70. $this->assertEquals($expected, array_keys($this->themesService->getThemes()));
  71. }
  72. public function testGetThemesEnforced() {
  73. $this->config->expects($this->once())
  74. ->method('getSystemValueString')
  75. ->with('enforce_theme', '')
  76. ->willReturn('dark');
  77. $this->logger->expects($this->never())
  78. ->method('error');
  79. $expected = [
  80. 'default',
  81. 'dark',
  82. ];
  83. $this->assertEquals($expected, array_keys($this->themesService->getThemes()));
  84. }
  85. public function testGetThemesEnforcedInvalid() {
  86. $this->config->expects($this->once())
  87. ->method('getSystemValueString')
  88. ->with('enforce_theme', '')
  89. ->willReturn('invalid');
  90. $this->logger->expects($this->once())
  91. ->method('error')
  92. ->with('Enforced theme not found', ['theme' => 'invalid']);
  93. $expected = [
  94. 'default',
  95. 'light',
  96. 'dark',
  97. 'light-highcontrast',
  98. 'dark-highcontrast',
  99. 'opendyslexic',
  100. ];
  101. $this->assertEquals($expected, array_keys($this->themesService->getThemes()));
  102. }
  103. public function dataTestEnableTheme() {
  104. return [
  105. ['default', [], ['default']],
  106. ['dark', [], ['dark']],
  107. ['dark', ['dark'], ['dark']],
  108. ['opendyslexic', ['dark'], ['dark', 'opendyslexic']],
  109. ['dark', ['light-highcontrast', 'opendyslexic'], ['opendyslexic', 'dark']],
  110. ];
  111. }
  112. /**
  113. * @dataProvider dataTestEnableTheme
  114. *
  115. * @param string $toEnable
  116. * @param string[] $enabledThemes
  117. * @param string[] $expectedEnabled
  118. */
  119. public function testEnableTheme(string $toEnable, array $enabledThemes, array $expectedEnabled) {
  120. $user = $this->createMock(IUser::class);
  121. $this->userSession->expects($this->any())
  122. ->method('getUser')
  123. ->willReturn($user);
  124. $user->expects($this->any())
  125. ->method('getUID')
  126. ->willReturn('user');
  127. $this->config->expects($this->once())
  128. ->method('getUserValue')
  129. ->with('user', Application::APP_ID, 'enabled-themes', '[]')
  130. ->willReturn(json_encode($enabledThemes));
  131. $this->assertEquals($expectedEnabled, $this->themesService->enableTheme($this->themes[$toEnable]));
  132. }
  133. public function dataTestDisableTheme() {
  134. return [
  135. ['dark', [], []],
  136. ['dark', ['dark'], []],
  137. ['opendyslexic', ['dark', 'opendyslexic'], ['dark'], ],
  138. ['light-highcontrast', ['opendyslexic'], ['opendyslexic']],
  139. ];
  140. }
  141. /**
  142. * @dataProvider dataTestDisableTheme
  143. *
  144. * @param string $toEnable
  145. * @param string[] $enabledThemes
  146. * @param string[] $expectedEnabled
  147. */
  148. public function testDisableTheme(string $toDisable, array $enabledThemes, array $expectedEnabled) {
  149. $user = $this->createMock(IUser::class);
  150. $this->userSession->expects($this->any())
  151. ->method('getUser')
  152. ->willReturn($user);
  153. $user->expects($this->any())
  154. ->method('getUID')
  155. ->willReturn('user');
  156. $this->config->expects($this->once())
  157. ->method('getUserValue')
  158. ->with('user', Application::APP_ID, 'enabled-themes', '[]')
  159. ->willReturn(json_encode($enabledThemes));
  160. $this->assertEquals($expectedEnabled, $this->themesService->disableTheme($this->themes[$toDisable]));
  161. }
  162. public function dataTestIsEnabled() {
  163. return [
  164. ['dark', [], false],
  165. ['dark', ['dark'], true],
  166. ['opendyslexic', ['dark', 'opendyslexic'], true],
  167. ['light-highcontrast', ['opendyslexic'], false],
  168. ];
  169. }
  170. /**
  171. * @dataProvider dataTestIsEnabled
  172. *
  173. * @param string $toEnable
  174. * @param string[] $enabledThemes
  175. */
  176. public function testIsEnabled(string $themeId, array $enabledThemes, $expected) {
  177. $user = $this->createMock(IUser::class);
  178. $this->userSession->expects($this->any())
  179. ->method('getUser')
  180. ->willReturn($user);
  181. $user->expects($this->any())
  182. ->method('getUID')
  183. ->willReturn('user');
  184. $this->config->expects($this->once())
  185. ->method('getUserValue')
  186. ->with('user', Application::APP_ID, 'enabled-themes', '[]')
  187. ->willReturn(json_encode($enabledThemes));
  188. $this->assertEquals($expected, $this->themesService->isEnabled($this->themes[$themeId]));
  189. }
  190. public function testGetEnabledThemes() {
  191. $user = $this->createMock(IUser::class);
  192. $this->userSession->expects($this->any())
  193. ->method('getUser')
  194. ->willReturn($user);
  195. $user->expects($this->any())
  196. ->method('getUID')
  197. ->willReturn('user');
  198. $this->config->expects($this->once())
  199. ->method('getUserValue')
  200. ->with('user', Application::APP_ID, 'enabled-themes', '[]')
  201. ->willReturn(json_encode([]));
  202. $this->config->expects($this->once())
  203. ->method('getSystemValueString')
  204. ->with('enforce_theme', '')
  205. ->willReturn('');
  206. $this->assertEquals([], $this->themesService->getEnabledThemes());
  207. }
  208. public function testGetEnabledThemesEnforced() {
  209. $user = $this->createMock(IUser::class);
  210. $this->userSession->expects($this->any())
  211. ->method('getUser')
  212. ->willReturn($user);
  213. $user->expects($this->any())
  214. ->method('getUID')
  215. ->willReturn('user');
  216. $this->config->expects($this->once())
  217. ->method('getUserValue')
  218. ->with('user', Application::APP_ID, 'enabled-themes', '[]')
  219. ->willReturn(json_encode([]));
  220. $this->config->expects($this->once())
  221. ->method('getSystemValueString')
  222. ->with('enforce_theme', '')
  223. ->willReturn('light');
  224. $this->assertEquals(['light'], $this->themesService->getEnabledThemes());
  225. }
  226. public function dataTestSetEnabledThemes() {
  227. return [
  228. [[], []],
  229. [['light'], ['light']],
  230. [['dark'], ['dark']],
  231. [['dark', 'dark', 'opendyslexic'], ['dark', 'opendyslexic']],
  232. ];
  233. }
  234. /**
  235. * @dataProvider dataTestSetEnabledThemes
  236. *
  237. * @param string[] $enabledThemes
  238. * @param string[] $expected
  239. */
  240. public function testSetEnabledThemes(array $enabledThemes, array $expected) {
  241. $user = $this->createMock(IUser::class);
  242. $this->userSession->expects($this->any())
  243. ->method('getUser')
  244. ->willReturn($user);
  245. $user->expects($this->any())
  246. ->method('getUID')
  247. ->willReturn('user');
  248. $this->config->expects($this->once())
  249. ->method('setUserValue')
  250. ->with('user', Application::APP_ID, 'enabled-themes', json_encode($expected));
  251. $this->invokePrivate($this->themesService, 'setEnabledThemes', [$enabledThemes]);
  252. }
  253. private function initThemes() {
  254. $util = $this->createMock(Util::class);
  255. $urlGenerator = $this->createMock(IURLGenerator::class);
  256. $imageManager = $this->createMock(ImageManager::class);
  257. $l10n = $this->createMock(IL10N::class);
  258. $appManager = $this->createMock(IAppManager::class);
  259. $this->themes = [
  260. 'default' => new DefaultTheme(
  261. $util,
  262. $this->themingDefaults,
  263. $this->userSession,
  264. $urlGenerator,
  265. $imageManager,
  266. $this->config,
  267. $l10n,
  268. $appManager,
  269. null,
  270. ),
  271. 'light' => new LightTheme(
  272. $util,
  273. $this->themingDefaults,
  274. $this->userSession,
  275. $urlGenerator,
  276. $imageManager,
  277. $this->config,
  278. $l10n,
  279. $appManager,
  280. null,
  281. ),
  282. 'dark' => new DarkTheme(
  283. $util,
  284. $this->themingDefaults,
  285. $this->userSession,
  286. $urlGenerator,
  287. $imageManager,
  288. $this->config,
  289. $l10n,
  290. $appManager,
  291. null,
  292. ),
  293. 'light-highcontrast' => new HighContrastTheme(
  294. $util,
  295. $this->themingDefaults,
  296. $this->userSession,
  297. $urlGenerator,
  298. $imageManager,
  299. $this->config,
  300. $l10n,
  301. $appManager,
  302. null,
  303. ),
  304. 'dark-highcontrast' => new DarkHighContrastTheme(
  305. $util,
  306. $this->themingDefaults,
  307. $this->userSession,
  308. $urlGenerator,
  309. $imageManager,
  310. $this->config,
  311. $l10n,
  312. $appManager,
  313. null,
  314. ),
  315. 'opendyslexic' => new DyslexiaFont(
  316. $util,
  317. $this->themingDefaults,
  318. $this->userSession,
  319. $urlGenerator,
  320. $imageManager,
  321. $this->config,
  322. $l10n,
  323. $appManager,
  324. null,
  325. ),
  326. ];
  327. }
  328. }