aboutsummaryrefslogtreecommitdiffstats
path: root/apps/theming/tests/Controller/UserThemeControllerTest.php
blob: 7d369463c0db0447621085b9807322e63e8a3408 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
 * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OCA\Theming\Tests\Controller;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\Controller\UserThemeController;
use OCA\Theming\ITheme;
use OCA\Theming\Service\BackgroundService;
use OCA\Theming\Service\ThemesService;
use OCA\Theming\Themes\DarkHighContrastTheme;
use OCA\Theming\Themes\DarkTheme;
use OCA\Theming\Themes\DefaultTheme;
use OCA\Theming\Themes\DyslexiaFont;
use OCA\Theming\Themes\HighContrastTheme;
use OCA\Theming\Themes\LightTheme;
use OCA\Theming\ThemingDefaults;
use OCP\AppFramework\Http\DataResponse;
use OCP\AppFramework\OCS\OCSBadRequestException;
use OCP\IConfig;
use OCP\IRequest;
use OCP\IUser;
use OCP\IUserSession;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

class UserThemeControllerTest extends TestCase {
	/** @var UserThemeController */
	private $userThemeController;

	/** @var IRequest|MockObject */
	private $request;
	/** @var IConfig|MockObject */
	private $config;
	/** @var IUserSession|MockObject */
	private $userSession;
	/** @var ThemeService|MockObject */
	private $themesService;
	/** @var ThemingDefaults */
	private $themingDefaults;
	/** @var BackgroundService|MockObject */
	private $backgroundService;


	/** @var ITheme[] */
	private $themes;

	protected function setUp(): void {
		$this->request = $this->createMock(IRequest::class);
		$this->config = $this->createMock(IConfig::class);
		$this->userSession = $this->createMock(IUserSession::class);
		$this->themesService = $this->createMock(ThemesService::class);
		$this->themingDefaults = $this->createMock(ThemingDefaults::class);
		$this->backgroundService = $this->createMock(BackgroundService::class);

		$this->themes = [
			'default' => $this->createMock(DefaultTheme::class),
			'light' => $this->createMock(LightTheme::class),
			'dark' => $this->createMock(DarkTheme::class),
			'light-highcontrast' => $this->createMock(HighContrastTheme::class),
			'dark-highcontrast' => $this->createMock(DarkHighContrastTheme::class),
			'opendyslexic' => $this->createMock(DyslexiaFont::class),
		];

		$user = $this->createMock(IUser::class);
		$this->userSession->expects($this->any())
			->method('getUser')
			->willReturn($user);
		$user->expects($this->any())
			->method('getUID')
			->willReturn('user');

		$this->userThemeController = new UserThemeController(
			Application::APP_ID,
			$this->request,
			$this->config,
			$this->userSession,
			$this->themesService,
			$this->themingDefaults,
			$this->backgroundService,
		);

		parent::setUp();
	}

	public function dataTestThemes() {
		return [
			['default'],
			['light'],
			['dark'],
			['light-highcontrast'],
			['dark-highcontrast'],
			['opendyslexic'],
			['', OCSBadRequestException::class],
			['badTheme', OCSBadRequestException::class],
		];
	}

	/**
	 * @dataProvider dataTestThemes
	 *
	 * @param string $themeId
	 * @param string $exception
	 */
	public function testEnableTheme($themeId, ?string $exception = null): void {
		$this->themesService
			->expects($this->any())
			->method('getThemes')
			->willReturn($this->themes);

		if ($exception) {
			$this->expectException($exception);
		}

		$expected = new DataResponse();
		$this->assertEquals($expected, $this->userThemeController->enableTheme($themeId));
	}

	/**
	 * @dataProvider dataTestThemes
	 *
	 * @param string $themeId
	 * @param string $exception
	 */
	public function testDisableTheme($themeId, ?string $exception = null): void {
		$this->themesService
			->expects($this->any())
			->method('getThemes')
			->willReturn($this->themes);

		if ($exception) {
			$this->expectException($exception);
		}

		$expected = new DataResponse();
		$this->assertEquals($expected, $this->userThemeController->disableTheme($themeId));
	}
}