2323use OCP \App \IAppManager ;
2424use OCP \IConfig ;
2525use OCP \IL10N ;
26+ use OCP \IRequest ;
2627use OCP \IURLGenerator ;
2728use OCP \IUser ;
2829use OCP \IUserSession ;
3334class ThemesServiceTest extends TestCase {
3435 private IUserSession &MockObject $ userSession ;
3536 private IConfig &MockObject $ config ;
37+ private IRequest &MockObject $ request ;
3638 private LoggerInterface &MockObject $ logger ;
3739
3840 private ThemingDefaults &MockObject $ themingDefaults ;
@@ -44,6 +46,7 @@ class ThemesServiceTest extends TestCase {
4446 protected function setUp (): void {
4547 $ this ->userSession = $ this ->createMock (IUserSession::class);
4648 $ this ->config = $ this ->createMock (IConfig::class);
49+ $ this ->request = $ this ->createMock (IRequest::class);
4750 $ this ->logger = $ this ->createMock (LoggerInterface::class);
4851 $ this ->themingDefaults = $ this ->createMock (ThemingDefaults::class);
4952
@@ -60,6 +63,7 @@ protected function setUp(): void {
6063 $ this ->themesService = new ThemesService (
6164 $ this ->userSession ,
6265 $ this ->config ,
66+ $ this ->request ,
6367 $ this ->logger ,
6468 ...array_values ($ this ->themes )
6569 );
@@ -232,6 +236,93 @@ public function testGetEnabledThemes(): void {
232236 $ this ->assertEquals (['default ' ], $ this ->themesService ->getEnabledThemes ());
233237 }
234238
239+ public function testGetEnabledThemesRequestThemeForGuest (): void {
240+ $ this ->userSession ->expects ($ this ->any ())
241+ ->method ('getUser ' )
242+ ->willReturn (null );
243+ $ this ->config ->expects ($ this ->once ())
244+ ->method ('getSystemValueString ' )
245+ ->with ('enforce_theme ' , '' )
246+ ->willReturn ('' );
247+ $ this ->request ->expects ($ this ->once ())
248+ ->method ('getParam ' )
249+ ->with ('theme ' , '' )
250+ ->willReturn ('dark ' );
251+
252+ $ this ->assertEquals (['dark ' ], $ this ->themesService ->getEnabledThemes ());
253+ }
254+
255+ public function testGetEnabledThemesRequestThemeOverridesUserTheme (): void {
256+ $ user = $ this ->createMock (IUser::class);
257+ $ this ->userSession ->expects ($ this ->any ())
258+ ->method ('getUser ' )
259+ ->willReturn ($ user );
260+ $ user ->expects ($ this ->any ())
261+ ->method ('getUID ' )
262+ ->willReturn ('user ' );
263+
264+ $ this ->config ->expects ($ this ->once ())
265+ ->method ('getUserValue ' )
266+ ->with ('user ' , Application::APP_ID , 'enabled-themes ' , '["default"] ' )
267+ ->willReturn (json_encode (['dark ' , 'opendyslexic ' ]));
268+ $ this ->config ->expects ($ this ->once ())
269+ ->method ('getSystemValueString ' )
270+ ->with ('enforce_theme ' , '' )
271+ ->willReturn ('' );
272+ $ this ->request ->expects ($ this ->once ())
273+ ->method ('getParam ' )
274+ ->with ('theme ' , '' )
275+ ->willReturn ('light ' );
276+
277+ $ this ->assertEquals (['opendyslexic ' , 'light ' ], $ this ->themesService ->getEnabledThemes ());
278+ }
279+
280+ public function testGetEnabledThemesInvalidRequestTheme (): void {
281+ $ this ->userSession ->expects ($ this ->any ())
282+ ->method ('getUser ' )
283+ ->willReturn (null );
284+ $ this ->config ->expects ($ this ->once ())
285+ ->method ('getSystemValueString ' )
286+ ->with ('enforce_theme ' , '' )
287+ ->willReturn ('' );
288+ $ this ->request ->expects ($ this ->once ())
289+ ->method ('getParam ' )
290+ ->with ('theme ' , '' )
291+ ->willReturn ('sepia ' );
292+
293+ $ this ->assertEquals ([], $ this ->themesService ->getEnabledThemes ());
294+ }
295+
296+ public function testGetEnabledThemesNonStringRequestThemeIsIgnored (): void {
297+ $ this ->userSession ->expects ($ this ->any ())
298+ ->method ('getUser ' )
299+ ->willReturn (null );
300+ $ this ->config ->expects ($ this ->once ())
301+ ->method ('getSystemValueString ' )
302+ ->with ('enforce_theme ' , '' )
303+ ->willReturn ('' );
304+ $ this ->request ->expects ($ this ->once ())
305+ ->method ('getParam ' )
306+ ->with ('theme ' , '' )
307+ ->willReturn (['dark ' ]);
308+
309+ $ this ->assertEquals ([], $ this ->themesService ->getEnabledThemes ());
310+ }
311+
312+ public function testGetEnabledThemesRequestThemeDoesNotOverrideEnforcedTheme (): void {
313+ $ this ->userSession ->expects ($ this ->any ())
314+ ->method ('getUser ' )
315+ ->willReturn (null );
316+ $ this ->config ->expects ($ this ->once ())
317+ ->method ('getSystemValueString ' )
318+ ->with ('enforce_theme ' , '' )
319+ ->willReturn ('light ' );
320+ $ this ->request ->expects ($ this ->never ())
321+ ->method ('getParam ' );
322+
323+ $ this ->assertEquals (['light ' ], $ this ->themesService ->getEnabledThemes ());
324+ }
325+
235326 public function testGetEnabledThemesEnforced (): void {
236327 $ user = $ this ->createMock (IUser::class);
237328 $ this ->userSession ->expects ($ this ->any ())
0 commit comments