You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserThemeControllerTest.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming\Tests\Controller;
  24. use OCA\Theming\AppInfo\Application;
  25. use OCA\Theming\Controller\UserThemeController;
  26. use OCA\Theming\ITheme;
  27. use OCA\Theming\Service\BackgroundService;
  28. use OCA\Theming\Service\ThemesService;
  29. use OCA\Theming\Themes\DarkHighContrastTheme;
  30. use OCA\Theming\Themes\DarkTheme;
  31. use OCA\Theming\Themes\DefaultTheme;
  32. use OCA\Theming\Themes\DyslexiaFont;
  33. use OCA\Theming\Themes\HighContrastTheme;
  34. use OCA\Theming\Themes\LightTheme;
  35. use OCA\Theming\ThemingDefaults;
  36. use OCP\AppFramework\Http\DataResponse;
  37. use OCP\AppFramework\OCS\OCSBadRequestException;
  38. use OCP\IConfig;
  39. use OCP\IRequest;
  40. use OCP\IUser;
  41. use OCP\IUserSession;
  42. use PHPUnit\Framework\MockObject\MockObject;
  43. use Test\TestCase;
  44. class UserThemeControllerTest extends TestCase {
  45. /** @var UserThemeController */
  46. private $userThemeController;
  47. /** @var IRequest|MockObject */
  48. private $request;
  49. /** @var IConfig|MockObject */
  50. private $config;
  51. /** @var IUserSession|MockObject */
  52. private $userSession;
  53. /** @var ThemeService|MockObject */
  54. private $themesService;
  55. /** @var ThemingDefaults */
  56. private $themingDefaults;
  57. /** @var BackgroundService|MockObject */
  58. private $backgroundService;
  59. /** @var ITheme[] */
  60. private $themes;
  61. protected function setUp(): void {
  62. $this->request = $this->createMock(IRequest::class);
  63. $this->config = $this->createMock(IConfig::class);
  64. $this->userSession = $this->createMock(IUserSession::class);
  65. $this->themesService = $this->createMock(ThemesService::class);
  66. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  67. $this->backgroundService = $this->createMock(BackgroundService::class);
  68. $this->themes = [
  69. 'default' => $this->createMock(DefaultTheme::class),
  70. 'light' => $this->createMock(LightTheme::class),
  71. 'dark' => $this->createMock(DarkTheme::class),
  72. 'light-highcontrast' => $this->createMock(HighContrastTheme::class),
  73. 'dark-highcontrast' => $this->createMock(DarkHighContrastTheme::class),
  74. 'opendyslexic' => $this->createMock(DyslexiaFont::class),
  75. ];
  76. $user = $this->createMock(IUser::class);
  77. $this->userSession->expects($this->any())
  78. ->method('getUser')
  79. ->willReturn($user);
  80. $user->expects($this->any())
  81. ->method('getUID')
  82. ->willReturn('user');
  83. $this->userThemeController = new UserThemeController(
  84. Application::APP_ID,
  85. $this->request,
  86. $this->config,
  87. $this->userSession,
  88. $this->themesService,
  89. $this->themingDefaults,
  90. $this->backgroundService,
  91. );
  92. parent::setUp();
  93. }
  94. public function dataTestThemes() {
  95. return [
  96. ['default'],
  97. ['light'],
  98. ['dark'],
  99. ['light-highcontrast'],
  100. ['dark-highcontrast'],
  101. ['opendyslexic'],
  102. ['', OCSBadRequestException::class],
  103. ['badTheme', OCSBadRequestException::class],
  104. ];
  105. }
  106. /**
  107. * @dataProvider dataTestThemes
  108. *
  109. * @param string $themeId
  110. * @param string $exception
  111. */
  112. public function testEnableTheme($themeId, ?string $exception = null) {
  113. $this->themesService
  114. ->expects($this->any())
  115. ->method('getThemes')
  116. ->willReturn($this->themes);
  117. if ($exception) {
  118. $this->expectException($exception);
  119. }
  120. $expected = new DataResponse();
  121. $this->assertEquals($expected, $this->userThemeController->enableTheme($themeId));
  122. }
  123. /**
  124. * @dataProvider dataTestThemes
  125. *
  126. * @param string $themeId
  127. * @param string $exception
  128. */
  129. public function testDisableTheme($themeId, ?string $exception = null) {
  130. $this->themesService
  131. ->expects($this->any())
  132. ->method('getThemes')
  133. ->willReturn($this->themes);
  134. if ($exception) {
  135. $this->expectException($exception);
  136. }
  137. $expected = new DataResponse();
  138. $this->assertEquals($expected, $this->userThemeController->disableTheme($themeId));
  139. }
  140. }