UserThemeControllerTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Theming\Tests\Controller;
  7. use OCA\Theming\AppInfo\Application;
  8. use OCA\Theming\Controller\UserThemeController;
  9. use OCA\Theming\ITheme;
  10. use OCA\Theming\Service\BackgroundService;
  11. use OCA\Theming\Service\ThemesService;
  12. use OCA\Theming\Themes\DarkHighContrastTheme;
  13. use OCA\Theming\Themes\DarkTheme;
  14. use OCA\Theming\Themes\DefaultTheme;
  15. use OCA\Theming\Themes\DyslexiaFont;
  16. use OCA\Theming\Themes\HighContrastTheme;
  17. use OCA\Theming\Themes\LightTheme;
  18. use OCA\Theming\ThemingDefaults;
  19. use OCP\AppFramework\Http\DataResponse;
  20. use OCP\AppFramework\OCS\OCSBadRequestException;
  21. use OCP\IConfig;
  22. use OCP\IRequest;
  23. use OCP\IUser;
  24. use OCP\IUserSession;
  25. use PHPUnit\Framework\MockObject\MockObject;
  26. use Test\TestCase;
  27. class UserThemeControllerTest extends TestCase {
  28. /** @var UserThemeController */
  29. private $userThemeController;
  30. /** @var IRequest|MockObject */
  31. private $request;
  32. /** @var IConfig|MockObject */
  33. private $config;
  34. /** @var IUserSession|MockObject */
  35. private $userSession;
  36. /** @var ThemeService|MockObject */
  37. private $themesService;
  38. /** @var ThemingDefaults */
  39. private $themingDefaults;
  40. /** @var BackgroundService|MockObject */
  41. private $backgroundService;
  42. /** @var ITheme[] */
  43. private $themes;
  44. protected function setUp(): void {
  45. $this->request = $this->createMock(IRequest::class);
  46. $this->config = $this->createMock(IConfig::class);
  47. $this->userSession = $this->createMock(IUserSession::class);
  48. $this->themesService = $this->createMock(ThemesService::class);
  49. $this->themingDefaults = $this->createMock(ThemingDefaults::class);
  50. $this->backgroundService = $this->createMock(BackgroundService::class);
  51. $this->themes = [
  52. 'default' => $this->createMock(DefaultTheme::class),
  53. 'light' => $this->createMock(LightTheme::class),
  54. 'dark' => $this->createMock(DarkTheme::class),
  55. 'light-highcontrast' => $this->createMock(HighContrastTheme::class),
  56. 'dark-highcontrast' => $this->createMock(DarkHighContrastTheme::class),
  57. 'opendyslexic' => $this->createMock(DyslexiaFont::class),
  58. ];
  59. $user = $this->createMock(IUser::class);
  60. $this->userSession->expects($this->any())
  61. ->method('getUser')
  62. ->willReturn($user);
  63. $user->expects($this->any())
  64. ->method('getUID')
  65. ->willReturn('user');
  66. $this->userThemeController = new UserThemeController(
  67. Application::APP_ID,
  68. $this->request,
  69. $this->config,
  70. $this->userSession,
  71. $this->themesService,
  72. $this->themingDefaults,
  73. $this->backgroundService,
  74. );
  75. parent::setUp();
  76. }
  77. public function dataTestThemes() {
  78. return [
  79. ['default'],
  80. ['light'],
  81. ['dark'],
  82. ['light-highcontrast'],
  83. ['dark-highcontrast'],
  84. ['opendyslexic'],
  85. ['', OCSBadRequestException::class],
  86. ['badTheme', OCSBadRequestException::class],
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataTestThemes
  91. *
  92. * @param string $themeId
  93. * @param string $exception
  94. */
  95. public function testEnableTheme($themeId, ?string $exception = null): void {
  96. $this->themesService
  97. ->expects($this->any())
  98. ->method('getThemes')
  99. ->willReturn($this->themes);
  100. if ($exception) {
  101. $this->expectException($exception);
  102. }
  103. $expected = new DataResponse();
  104. $this->assertEquals($expected, $this->userThemeController->enableTheme($themeId));
  105. }
  106. /**
  107. * @dataProvider dataTestThemes
  108. *
  109. * @param string $themeId
  110. * @param string $exception
  111. */
  112. public function testDisableTheme($themeId, ?string $exception = null): void {
  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->disableTheme($themeId));
  122. }
  123. }