PersonalTest.php 5.9 KB

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