1
0

ThemesServiceTest.php 9.0 KB

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