PersonalTest.php 6.4 KB

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