1
0

PersonalTest.php 5.9 KB

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