PersonalTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests\Settings;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\ImageManager;
  9. use OCA\Theming\ITheme;
  10. use OCA\Theming\Service\BackgroundService;
  11. use OCA\Theming\Service\ThemesService;
  12. use OCA\Theming\Settings\Personal;
  13. use OCA\Theming\Themes\DarkHighContrastTheme;
  14. use OCA\Theming\Themes\DarkTheme;
  15. use OCA\Theming\Themes\DefaultTheme;
  16. use OCA\Theming\Themes\DyslexiaFont;
  17. use OCA\Theming\Themes\HighContrastTheme;
  18. use OCA\Theming\Themes\LightTheme;
  19. use OCA\Theming\ThemingDefaults;
  20. use OCA\Theming\Util;
  21. use OCP\App\IAppManager;
  22. use OCP\AppFramework\Http\TemplateResponse;
  23. use OCP\AppFramework\Services\IInitialState;
  24. use OCP\IConfig;
  25. use OCP\IL10N;
  26. use OCP\IURLGenerator;
  27. use OCP\IUserSession;
  28. use Test\TestCase;
  29. class PersonalTest extends TestCase {
  30. private IConfig $config;
  31. private ThemesService $themesService;
  32. private IInitialState $initialStateService;
  33. private ThemingDefaults $themingDefaults;
  34. private IAppManager $appManager;
  35. private Personal $admin;
  36. /** @var ITheme[] */
  37. private $themes;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->config = $this->createMock(IConfig::class);
  41. $this->themesService = $this->createMock(ThemesService::class);
  42. $this->initialStateService = $this->createMock(IInitialState::class);
  43. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  44. $this->appManager = $this->createMock(IAppManager::class);
  45. $this->initThemes();
  46. $this->themesService
  47. ->expects($this->any())
  48. ->method('getThemes')
  49. ->willReturn($this->themes);
  50. $this->admin = new Personal(
  51. Application::APP_ID,
  52. 'admin',
  53. $this->config,
  54. $this->themesService,
  55. $this->initialStateService,
  56. $this->themingDefaults,
  57. $this->appManager,
  58. );
  59. }
  60. public function dataTestGetForm() {
  61. return [
  62. ['', [
  63. $this->formatThemeForm('default'),
  64. $this->formatThemeForm('light'),
  65. $this->formatThemeForm('dark'),
  66. $this->formatThemeForm('light-highcontrast'),
  67. $this->formatThemeForm('dark-highcontrast'),
  68. $this->formatThemeForm('opendyslexic'),
  69. ]],
  70. ['dark', [
  71. $this->formatThemeForm('dark'),
  72. $this->formatThemeForm('opendyslexic'),
  73. ]],
  74. ];
  75. }
  76. /**
  77. * @dataProvider dataTestGetForm
  78. *
  79. * @param string $toEnable
  80. * @param string[] $enabledThemes
  81. */
  82. public function testGetForm(string $enforcedTheme, $themesState) {
  83. $this->config->expects($this->once())
  84. ->method('getSystemValueString')
  85. ->with('enforce_theme', '')
  86. ->willReturn($enforcedTheme);
  87. $this->config->expects($this->any())
  88. ->method('getUserValue')
  89. ->willReturnMap([
  90. ['admin', 'core', 'apporder', '[]', '[]'],
  91. ['admin', 'theming', 'background_image', BackgroundService::BACKGROUND_DEFAULT],
  92. ]);
  93. $this->appManager->expects($this->once())
  94. ->method('getDefaultAppForUser')
  95. ->willReturn('forcedapp');
  96. $this->initialStateService->expects($this->exactly(7))
  97. ->method('provideInitialState')
  98. ->withConsecutive(
  99. ['shippedBackgrounds', BackgroundService::SHIPPED_BACKGROUNDS],
  100. ['themingDefaults'],
  101. ['userBackgroundImage'],
  102. ['themes', $themesState],
  103. ['enforceTheme', $enforcedTheme],
  104. ['isUserThemingDisabled', false],
  105. ['navigationBar', ['userAppOrder' => [], 'enforcedDefaultApp' => 'forcedapp']],
  106. );
  107. $expected = new TemplateResponse('theming', 'settings-personal');
  108. $this->assertEquals($expected, $this->admin->getForm());
  109. }
  110. public function testGetSection() {
  111. $this->assertSame('theming', $this->admin->getSection());
  112. }
  113. public function testGetPriority() {
  114. $this->assertSame(40, $this->admin->getPriority());
  115. }
  116. private function initThemes() {
  117. $util = $this->createMock(Util::class);
  118. $themingDefaults = $this->createMock(ThemingDefaults::class);
  119. $userSession = $this->createMock(IUserSession::class);
  120. $urlGenerator = $this->createMock(IURLGenerator::class);
  121. $imageManager = $this->createMock(ImageManager::class);
  122. $config = $this->createMock(IConfig::class);
  123. $l10n = $this->createMock(IL10N::class);
  124. $appManager = $this->createMock(IAppManager::class);
  125. $themingDefaults->expects($this->any())
  126. ->method('getColorPrimary')
  127. ->willReturn('#0082c9');
  128. $themingDefaults->expects($this->any())
  129. ->method('getDefaultColorPrimary')
  130. ->willReturn('#0082c9');
  131. $this->themes = [
  132. 'default' => new DefaultTheme(
  133. $util,
  134. $themingDefaults,
  135. $userSession,
  136. $urlGenerator,
  137. $imageManager,
  138. $config,
  139. $l10n,
  140. $appManager,
  141. ),
  142. 'light' => new LightTheme(
  143. $util,
  144. $themingDefaults,
  145. $userSession,
  146. $urlGenerator,
  147. $imageManager,
  148. $config,
  149. $l10n,
  150. $appManager,
  151. ),
  152. 'dark' => new DarkTheme(
  153. $util,
  154. $themingDefaults,
  155. $userSession,
  156. $urlGenerator,
  157. $imageManager,
  158. $config,
  159. $l10n,
  160. $appManager,
  161. ),
  162. 'light-highcontrast' => new HighContrastTheme(
  163. $util,
  164. $themingDefaults,
  165. $userSession,
  166. $urlGenerator,
  167. $imageManager,
  168. $config,
  169. $l10n,
  170. $appManager,
  171. ),
  172. 'dark-highcontrast' => new DarkHighContrastTheme(
  173. $util,
  174. $themingDefaults,
  175. $userSession,
  176. $urlGenerator,
  177. $imageManager,
  178. $config,
  179. $l10n,
  180. $appManager,
  181. ),
  182. 'opendyslexic' => new DyslexiaFont(
  183. $util,
  184. $themingDefaults,
  185. $userSession,
  186. $urlGenerator,
  187. $imageManager,
  188. $config,
  189. $l10n,
  190. $appManager,
  191. ),
  192. ];
  193. }
  194. private function formatThemeForm(string $themeId): array {
  195. $this->initThemes();
  196. $theme = $this->themes[$themeId];
  197. return [
  198. 'id' => $theme->getId(),
  199. 'type' => $theme->getType(),
  200. 'title' => $theme->getTitle(),
  201. 'enableLabel' => $theme->getEnableLabel(),
  202. 'description' => $theme->getDescription(),
  203. 'enabled' => false,
  204. ];
  205. }
  206. }