ThemesServiceTest.php 10 KB

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