PersonalTest.php 6.0 KB

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