PersonalTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Jan-Christoph Borchardt <hey@jancborchardt.net>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OCA\Theming\Tests\Settings;
  29. use OCA\Theming\AppInfo\Application;
  30. use OCA\Theming\ImageManager;
  31. use OCA\Theming\ITheme;
  32. use OCA\Theming\Service\ThemesService;
  33. use OCA\Theming\Settings\Personal;
  34. use OCA\Theming\Themes\DarkHighContrastTheme;
  35. use OCA\Theming\Themes\DarkTheme;
  36. use OCA\Theming\Themes\DefaultTheme;
  37. use OCA\Theming\Themes\DyslexiaFont;
  38. use OCA\Theming\Themes\HighContrastTheme;
  39. use OCA\Theming\Themes\LightTheme;
  40. use OCA\Theming\ThemingDefaults;
  41. use OCA\Theming\Util;
  42. use OCP\App\IAppManager;
  43. use OCP\AppFramework\Http\TemplateResponse;
  44. use OCP\AppFramework\Services\IInitialState;
  45. use OCP\IConfig;
  46. use OCP\IL10N;
  47. use OCP\IURLGenerator;
  48. use OCP\IUserSession;
  49. use PHPUnit\Framework\MockObject\MockObject;
  50. use Test\TestCase;
  51. class PersonalTest extends TestCase {
  52. private IConfig|MockObject $config;
  53. private ThemesService|MockObject $themesService;
  54. private IInitialState|MockObject $initialStateService;
  55. private ThemingDefaults|MockObject $themingDefaults;
  56. private IAppManager|MockObject $appManager;
  57. private Personal $admin;
  58. /** @var ITheme[] */
  59. private $themes;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->config = $this->createMock(IConfig::class);
  63. $this->themesService = $this->createMock(ThemesService::class);
  64. $this->initialStateService = $this->createMock(IInitialState::class);
  65. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  66. $this->appManager = $this->createMock(IAppManager::class);
  67. $this->initThemes();
  68. $this->themesService
  69. ->expects($this->any())
  70. ->method('getThemes')
  71. ->willReturn($this->themes);
  72. $this->admin = new Personal(
  73. Application::APP_ID,
  74. 'admin',
  75. $this->config,
  76. $this->themesService,
  77. $this->initialStateService,
  78. $this->themingDefaults,
  79. $this->appManager,
  80. );
  81. }
  82. public function dataTestGetForm() {
  83. return [
  84. ['', [
  85. $this->formatThemeForm('default'),
  86. $this->formatThemeForm('light'),
  87. $this->formatThemeForm('dark'),
  88. $this->formatThemeForm('light-highcontrast'),
  89. $this->formatThemeForm('dark-highcontrast'),
  90. $this->formatThemeForm('opendyslexic'),
  91. ]],
  92. ['dark', [
  93. $this->formatThemeForm('dark'),
  94. $this->formatThemeForm('opendyslexic'),
  95. ]],
  96. ];
  97. }
  98. /**
  99. * @dataProvider dataTestGetForm
  100. *
  101. * @param string $toEnable
  102. * @param string[] $enabledThemes
  103. */
  104. public function testGetForm(string $enforcedTheme, $themesState) {
  105. $this->config->expects($this->once())
  106. ->method('getSystemValueString')
  107. ->with('enforce_theme', '')
  108. ->willReturn($enforcedTheme);
  109. $this->config->expects($this->exactly(2))
  110. ->method('getUserValue')
  111. ->willReturnMap([
  112. ['admin', 'core', 'apporder', '[]', '[]'],
  113. ['admin', 'theming', 'force_enable_blur_filter', '', ''],
  114. ]);
  115. $this->appManager->expects($this->once())
  116. ->method('getDefaultAppForUser')
  117. ->willReturn('forcedapp');
  118. $this->initialStateService->expects($this->exactly(5))
  119. ->method('provideInitialState')
  120. ->willReturnMap([
  121. ['themes', $themesState],
  122. ['enableBlurFilter', ''],
  123. ['enforceTheme', $enforcedTheme],
  124. ['isUserThemingDisabled', false],
  125. ['navigationBar', ['userAppOrder' => [], 'enforcedDefaultApp' => 'forcedapp']],
  126. ]);
  127. $expected = new TemplateResponse('theming', 'settings-personal');
  128. $this->assertEquals($expected, $this->admin->getForm());
  129. }
  130. public function testGetSection() {
  131. $this->assertSame('theming', $this->admin->getSection());
  132. }
  133. public function testGetPriority() {
  134. $this->assertSame(40, $this->admin->getPriority());
  135. }
  136. private function initThemes() {
  137. $util = $this->createMock(Util::class);
  138. $themingDefaults = $this->createMock(ThemingDefaults::class);
  139. $userSession = $this->createMock(IUserSession::class);
  140. $urlGenerator = $this->createMock(IURLGenerator::class);
  141. $imageManager = $this->createMock(ImageManager::class);
  142. $config = $this->createMock(IConfig::class);
  143. $l10n = $this->createMock(IL10N::class);
  144. $appManager = $this->createMock(IAppManager::class);
  145. $themingDefaults->expects($this->any())
  146. ->method('getColorPrimary')
  147. ->willReturn('#0082c9');
  148. $themingDefaults->expects($this->any())
  149. ->method('getDefaultColorPrimary')
  150. ->willReturn('#0082c9');
  151. $this->themes = [
  152. 'default' => new DefaultTheme(
  153. $util,
  154. $themingDefaults,
  155. $userSession,
  156. $urlGenerator,
  157. $imageManager,
  158. $config,
  159. $l10n,
  160. $appManager,
  161. null,
  162. ),
  163. 'light' => new LightTheme(
  164. $util,
  165. $themingDefaults,
  166. $userSession,
  167. $urlGenerator,
  168. $imageManager,
  169. $config,
  170. $l10n,
  171. $appManager,
  172. null,
  173. ),
  174. 'dark' => new DarkTheme(
  175. $util,
  176. $themingDefaults,
  177. $userSession,
  178. $urlGenerator,
  179. $imageManager,
  180. $config,
  181. $l10n,
  182. $appManager,
  183. null,
  184. ),
  185. 'light-highcontrast' => new HighContrastTheme(
  186. $util,
  187. $themingDefaults,
  188. $userSession,
  189. $urlGenerator,
  190. $imageManager,
  191. $config,
  192. $l10n,
  193. $appManager,
  194. null,
  195. ),
  196. 'dark-highcontrast' => new DarkHighContrastTheme(
  197. $util,
  198. $themingDefaults,
  199. $userSession,
  200. $urlGenerator,
  201. $imageManager,
  202. $config,
  203. $l10n,
  204. $appManager,
  205. null,
  206. ),
  207. 'opendyslexic' => new DyslexiaFont(
  208. $util,
  209. $themingDefaults,
  210. $userSession,
  211. $urlGenerator,
  212. $imageManager,
  213. $config,
  214. $l10n,
  215. $appManager,
  216. null,
  217. ),
  218. ];
  219. }
  220. private function formatThemeForm(string $themeId): array {
  221. $this->initThemes();
  222. $theme = $this->themes[$themeId];
  223. return [
  224. 'id' => $theme->getId(),
  225. 'type' => $theme->getType(),
  226. 'title' => $theme->getTitle(),
  227. 'enableLabel' => $theme->getEnableLabel(),
  228. 'description' => $theme->getDescription(),
  229. 'enabled' => false,
  230. ];
  231. }
  232. }